API

Asset Containers

An AssetContainer can be used to manage a list of assets (like nodes, cameras, lights, meshes, etc.). These assets are still linked to a scene but are not taken into account. Consider asset containers as pools of entities.

Creating an AssetContainer

It can be created manually:

import { AssetContainer } from "@babylonjs/core/assetContainer";
const container = new 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 scene
container.addAllToScene();
// Remove all objects in the container from the scene
container.removeAllFromScene();
Asset Container Adding and Removing Assets

This can be used to add/remove all objects in a scene without the need to exit WebVR. Asset Container Adding and Removing Assets in WebVR

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.

import { KeepAssets } from "@babylonjs/core/assetContainer";
const keepAssets = new KeepAssets();
keepAssets.cameras.push(camera);
container.moveAllFromScene(keepAssets);
Moving Assets Into an Asset Container

Duplicating 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 returned 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
Instantiating Asset Container Assets

You can also set two parameters to the call to instantiateModelsToScene:

  • nameFunction: This lets you decide what the names of the cloned entities will be (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:

entries.dispose();