An AssetContainer can be used to manage a list of assets (like nodes, cameras, lights, meshes, etc..). This assets are still linked to a scene but are not taken in account. Consider asset containers like pool of entities.
Creating an AssetContainer
It can be created manually
const container = new BABYLON.AssetContainer(scene);
Or by loading from a file. See loading from files
Adding or removing assets to the scene
The container can then be used to add or remove contents of the container from the scene.
// Add all objects in the asset container to the scenecontainer.addAllToScene();// Remove all objects in the container from the scenecontainer.removeAllFromScene();
When creating assets manually the moveAllFromScene method can be used to move all assets currently in a scene into an AssetContainer and remove them from the scene for later use.
const keepAssets = new BABYLON.KeepAssets();keepAssets.cameras.push(camera);container.moveAllFromScene(keepAssets);
Duplicating the models
Asset containers can also be used as "templates" to duplicate models without reloading them.
To do so, you only need to call:
let entries = container.instantiateModelsToScene();
The return entries object will contain:
- rootNodes: A list of all the root nodes created by the duplication process
- skeletons: A list of all the skeletons created by the duplication process
- animationGroups: A list of all the animation groups created by the duplication process
You can also set two parameters to the call to
- nameFunction: This will let you decide what will be the name of the cloned entities (instead of "Clone of...")
- cloneMaterials: By default materials are not cloned but shared. With this parameter you can force the system to also clone the materials
const entries = container.instantiateModelsToScene(name => "p" + name, true);
You can dispose a specific set of instantiated models again with: