How to clear AssetContainer.instantiateModelsToScene() all instances

After digging around the forum, I still haven’t figured out if anyone has managed to delete a created instance? In my project, the scene is “regenerated” very often. That is, I delete all the objects and clone them again. But those objects that are created via the instantiateModelsToScene function are not deleted. If you play my game for 10 minutes, the fps will become terribly low due to the accumulated garbage. removeAllScene doesn’t help.

Please help :persevering_face:

I believe we need a repro to understand better :slight_smile:

Hello! :smiling_face:

I believe you’d just have to dispose() the cloned mesh.

You could also add the meshes to the container you created, then removeallfromscene would take them into consideration:

PG:

2 Likes

dispose() working, but If you check alert(scene.meshes.length), you can see that there are still some objects on the scene, and the lower method displays 0, thank you very much, it works!

I have a problem again. The code does not work for re-creating an instance. After creating 2 instances, another one appears from somewhere in the position of the previously deleted instance. And then when deleting again, instance 2 disappears, and the one that appeared from nowhere remains in place. Help :disappointed_face:

       //+inst
       setTimeout(() => {
        let newStalker = window.stalkerContainer.instantiateModelsToScene();
        newStalker.rootNodes[0].position.x = 0.5;
        window.stalkerContainer.meshes.push(newStalker.rootNodes[0])
       }, 1000);
       //remove inst
       setTimeout(() => {
        window.stalkerContainer.removeAllFromScene();
       }, 2000);
       //repeat +inst
       setTimeout(() => {
         let newStalker2 = window.stalkerContainer.instantiateModelsToScene();
         newStalker2.rootNodes[0].position.x = 0.1;
         window.stalkerContainer.meshes.push(newStalker2.rootNodes[0])
       }, 3000);
       //repeat remove inst
       setTimeout(() => {
         window.stalkerContainer.removeAllFromScene(); //PROBLEM
       }, 4000);

cc @AlitarSemiramis

2 Likes

ACK! Looking into this and I will respond again once I have more to share.

2 Likes

Hey @Alex_Ismailov here is a playground that handles the instantiatedModels addition and removal:

Bugs | Babylon.js Playground

The thing with AssetContainer is that the API has two different ways of working:

  1. You just use the assets inside the container, by using the functions AssetContainer.addAllToScene(), and AssetContainer.removeAllFromScene()
  2. You clone the assets in the container by using AssetContainer.instantiateModelsToScene() and then remove them by using InstantiatedEntries.dispose()

In your Playground you were mixing both paradigms, which was causing the issues, but in the new playground I am just using the second approach of duplicating the assets to simplify things.

You can read about Asset Containers in more detail here in the docs:
Asset Containers | Babylon.js Documentation

Hope it helps!

2 Likes

Thank you for your detailed answer :heart:

1 Like