timetocode

Load mesh without rendering it, then adding it to the world many times

Recommended Posts

What's the right way to load multiple models from blender (or anything), and then spawn them many times into the game world?

Let's say I have a tree.obj and a house.obj... and then I want to load it, without rendering anything yet, and then I'm going to programmatically generate terrain and add 200 trees and 20 houses. 

I've tried LoadAssetContainer, but I couldn't seem to invoke scene.createMesh or createMaterial on the data... all I could get to work was  container.addAllToScene(). Also is there a friendly name for the loaded model? I was able to position it by doing container.meshes[0].position.y = 30, but I assume there's another way to interact with it.

So the questions are:

  1. which file format for the blender objects (obj, babylon.. then one model per file..?)
  2. which loader to use
  3. how to load without spawning the object into the world
  4. how to spawn multiple of the object into the world (clone..? then position+rotate?)
  5. how to remove individual objects without unloading the source mesh

Thanks for the help :D

Share this post


Link to post
Share on other sites

Hi Timetocode ! 

Well, for points 1 and 2.. I don't think there is a perfect solution. it depends of what you need. I suggest that if you want to have a 'master' tree model to spawn on your scene, then using a OBJ loader (https://doc.babylonjs.com/how_to/obj) should be enough. But maybe GLTF or .babylon could be better, especially if you want additional information like lights etc...

3. You just have to disable the mesh. try setEnabled(false) and your mesh won't appear.
It is better way than just setting the opacity to 0 or isVisible to false because a disabled mesh is not taking in account during hidden babylon computation.

4. You may use instances : https://doc.babylonjs.com/how_to/how_to_use_instances
From my experience, you will get better performances by cloning your 'master' mesh, and then merging all the clones https://doc.babylonjs.com/how_to/how_to_merge_meshes
It is incredibly powerful if theses clones don't need to be edited later, but then it is gonna be harder to remove/modify individual objects as said in 5. (Do not forget to make sure clones or instances are enabled !)

5. Easy using instances. You may use subMesh to do it while working with a big mesh made from merged clones, but it is way heavier.

To conclude : I guess you should use instances if you know your loaded-and-spawned meshes will be modified later. Clones and Merge otherwise.

Hope it will help !

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.