Multiple meshes

You can’t use mesh instances with skeletons, right? So if you want several (skeletal) meshes of the same type, you need to import the same mesh multiple times? Is it any better (in terms of memory usage) to import once, then clone multiple times?

I do use instantiateModelsToScene of asset container to duplicate:

Depending on your scenario even clone() might do the job:

2 Likes

Example - https://playground.babylonjs.com/#AJA5J6#192

Thank you, that clarifies things a bit.

  • So, one container per mesh file. There can be multiple meshes in the file, but every file is loaded into a separate container. This is good, because we can dispose of containers that are no longer needed, while keeping containers that are still used.
  • Skeletons, meshes, and animation groups get cloned, but materials don’t.
  • The doNotInstantiate option doesn’t seem to matter for skeletal meshes, since they are always cloned. But we should probably use false, b/c instances are preferred whenever possible… Probably.

I still have a couple of questions:

  • Not sure how to reuse meshes across scenes, because the container is associated with a scene. When the current scene is disposed, how to transfer the assets to the new scene? The instantiate* method doesn’t take a scene arg.
  • What’s the difference between addAllToScene and instantiateModelsToScene? I commented out addAllToScene, and replaced it with a second call to duplicate, and it works, and the scene graph still looks the same. The reverse doesn’t work: we can’t addAllToScene twice, but we can just use instantiateModelsToScene as many times as we want.

UPDATE: After doing some more reading and thinking, I think I have answers to my questions:
You can’t share containers across scenes. Different game locations should be loaded into containers, instead of scenes. Switching locations is then simply a matter of calling location1Container.removeAllFromScene(), followed by location2Container.addAllToScene(). This answers the second question… addAll is for the initial load, and instantiateModels is for creating duplicates, e.g. spawning more enemies, items, etc.

Thanks again!

3 Likes