I need to load an object “prototype” meshs and clone them dynamicly all over the scene according to a certain algorithm.
Let’s imagine that one of them is the cars that I want to line up 20.
Here’s what I get
var canvas = document.getElementById('canvas');
engine = new BABYLON.Engine(canvas, true);
scene = _createScene();
assetsManager = new BABYLON.AssetsManager(scene);
// Load car model, lets's call it "prototype"
var meshTask = assetsManager.addMeshTask('loadCar', '', '/Content/', 'car.glb');
var carTmp;
meshTask.onSuccess = function (task) {
carTmp = task.loadedMeshes[0];
}
assetsManager.onFinish = function () {
// Arrange the cars on the scene
for (var i = -10; i < 10; i++) {
var carNew = carTmp.clone("car" + i);
carNew.position = new BABYLON.Vector3(0, 0, i * 6);
}
// Delete carTmp...?
engine.runRenderLoop(function () {
scene.render();
});
};
assetsManager.load();
This is certainly not optimal
As far as I understand, I shoild use the AssetContainer some way.
What are the best practics for this?
There are such assets as a car and a track that the application loads so that later using them as templates to build a scheme of railway tracks in accordance with the JSON file of each scheme.
I put the car and track templates in the “templates” assets container. During the construction of the schema itself, I also put clones of templates in the “schema” assets container, so that it is more convenient to remove them when loading another schema using BABYLON.AssetContainer.dispose() method.
Here’s what goes wrong.
After loading all the templates, I call the method BABYLON.AssetContainer.removeAllFromScene in order not to show them on stage at all. And yes. They don’t really show up in the scene inspector window.
But they stay in camera viewport!
It looks like problems arise only if you use *.glb files.
As soon as I changed the file format of the models to *.babylon everything worked as expected