How to load 3D models as needed throughout a game

I am starting to add 3D model loading into my game, and reading all the docs it’s not clear which approach I should take.

The disconnect seems to come from the fact that I want to load models s they are needed. The game has a lot of models, most of which will not be needed for specific game (depends on the player’s strategy).

I definitely don’t want to load a scene with cameras and lights etc. A model in my case be a building, a vehicle or a weapon etc. The AssetManager seems to work by adding tasks then calling load to load all of the assets, which doesn’t fit very well with my use case.

I guess I could create a new AssetManager every time I want to load a model, but this feels like a lot of overhead. I looked into trying to use the glFX loader directly but the documentation is very thin on how to do this.

Anyone done something like this before?

I built an implementation that works, but not 100% sure this is the best approach.
For anyone else reading this thread, here is my solution - it works for me, but no guaranties!

    const models = {};

    const loadModel = function(scene, key, root, asset, onLoaded){
        var model = models[key];
        if (model) {
            if (model.container) onLoaded(model.container);
            else model.loaders.push(onLoaded);
            return;
        }
        model = {
            loaders: [onLoaded],
            container: null
        };
        models[key] = model;
        BABYLON.SceneLoader.LoadAssetContainer(root, asset + '.glb', scene, function (container) {
            model.container = container;
            model.loaders.forEach(l => l(container));
            model.loaders = [];
        });
    }

1 Like

Looks good to me