How to stop mergeMeshes from adding the mesh to the scene

I am trying to perform Mesh.mergeMeshes to an array of meshes after a AssetsManager.containerTask finishes. When I do this it apears that mergeMeshes actually adds to mesh to the scene which I do not want to do. Is this a feature or a bug? I dont see a flag to stop this from happening. Here is my merge meshes code…

// merge all meshes
function mergeMeshes (meshes) {
let rootMesh;
let merged;
meshes = meshes.filter((mesh) => {
if (mesh.id === ‘root’) {
rootMesh = mesh;
mesh.getChildren().forEach(object => mesh.removeChild(object));
return false;
}
return true;
})
// Dispose off imported root mesh and merge all into one mesh
if (rootMesh){
rootMesh.dispose();
}
// NOTE: the third parameter must be set to true for meshes with more than 60k verts
merged = Mesh.MergeMeshes(meshes, true,true);
return merged;
}

1 Like

You could use

mergedMeshes.setEnabled(false)

to remove from scene, change false to true to add to scene.

1 Like

yea the meshes still show up in the inspector even if setEnabled(false) hence technically they are in the scene just not visible. It seems to me mergeMeshes should not add anything to the scene. Seems to me the sole responsibility should be to merge a list of meshes and return a new mesh without changing the scene state? I feel like this is a new feature request or a tweak to an existing feature?

It is more than not being visible, meshes that are not enabled take no part in any processing of the scene and so have no cost.

The alternative would be to have a store of meshes that are not part of the scene and then these meshes would have to be transferred back and forth between the store and the scene. There would have to be a mechanism for finding, removing and adding from one to the other. Just having an enabled flag is more straightforward.

indeed, im starting to see your point. Let them live in the scene and just enable/disable/duplicate them as needed. Basically the scene becomes the store. Keep It Simple Stupid solution ;).

To fully remove (at your own risk if dispose is called on the scene in between) you can use:

scene.removeMesh(mergedMeshes); and

scene.addMesh(mergedMeshes); to add it back

2 Likes

Thank you all. We chose to just have the mesh live in the scene and just make it inactive until needed via
mergedMeshes.setEnabled(false). however we did test just removing it from the scene right after the merge and that worked as well but was ultimately not necessary.

Sometimes its just simpler to use the scene itself as the central “store” for all your needed meshes and just use setEnabled rather than relying on moving stuff back and fourth through some other mechanism.

There are many ways to achieve the same thing in Babylon, and I would say a good rule of thumb is to try to keep it simple and add more complexity only when absolutely necessary.