AssetContainer.dispose() disposes everything in the scene, but does not keep them

I was trying to remove everything except a few assets in the scene. For example, in this code, when I try to add a box and sphere into scene, then put box into KeepAssets and then move sphere into the container. I then tried to use method dispose(), but this actually disposed my box also. But I only want to dispose the sphere.

    const container = new AssetContainer(scene);

    const keepAssets = new KeepAssets();
    keepAssets.cameras.push(this.camera);

    const box = MeshBuilder.CreateBox("");

    const sphere = MeshBuilder.CreateSphere("");

    keepAssets.meshes.push(box);

    sphere.position.x = 5;

    container.moveAllFromScene(keepAssets);

    container.dispose();

I would appreciate any help. Thank you!

Strangely, if I use container.removeAllFromScene(keepAssets) it works.
Example - https://playground.babylonjs.com/#LMDZWR#1

Should be vice versa, according to the docs - AssetContainer | Babylon.js Documentation , or I miss something?

Thank you for reply.

I just read the documentation. The difference between container.moveAllFromScene and container.removeAllFromScene is, the first one moves all stuffs from scene into the container, while the later will remove the stuffs that is already in the container from the scene. I noticed the later should not take any argument. Since you put sphere mesh deliberately into the container, so it think that would delete the stuffs you deliberately put in. But I was wondering how to remove everything except for the stuffs I want to keep in the cleanest way? I may have many meshes, textures, etc inside the scene, and can I avoid loop through all of meshes, bones, textures, etc? Especially if I want to import model from outside, this can get hectic.

Your code does not work because you also have to add the geometry of the box to the keepAssets container:

keepAssets.geometries.push(box.geometry);
2 Likes