Duplicating meshes without binding their geometry

Hello there,
I am new to babylon.js and got a question: is there any way to duplicate a mesh (similar to createInstance() or clone()), but without letting it’s geometry being effected by changes to the “original” mesh?
Example: I clone a mesh and want it to be independent from the “original” mesh. But every time i change the original mesh, my new mesh also changes its geometry.
Is there any way to break this link between the two meshes?

Thanks a lot, have a nice day!

Welcome aboard!

You can use:

mesh.makeGeometryUnique();

That’s for meshes. I don’t think something similar exist for instances…

3 Likes

Super useful, thanks! How would I go about doing something similar for thin instances?

My use-case: I have a scene with thin instances, I pick one, hide it (using thinInstanceCount) and replace it by a clone to attach gizmos on it. However if I change the colour of another instance (on hover), the cloned mesh seems to share the color buffer and its color gets changed as well. I’d like to call makeGeometryUnique on the clone mesh but it crashes with RangeError: Offset is outside the bounds of the DataView, I’m guessing because it’s instantiated now, not just a mesh (?). Any prescribed method to get around this?

PS: Let me know if I should open a new topic instead of commenting on this old existing one that’s related. :thinking:

Are you cloning the mesh that has thin instances? If yes, you should make sure this clone has no thin instances (thinInstanceCount = 0 should be enough). The thin instance picking sample from the doc is basically doing what you are doing: when you click on a thin instance it “selects” it by moving a pre-existing copy of the mesh to this thin instance location.

1 Like

Thank you @Evgeni_Popov. This PG link was super handy as a base to repro my issue. I’ve edited it to match what I’m doing: https://playground.babylonjs.com/#RC2IAH#11.

The box is now cloned after the picking happens, so after the instances are set on the mesh. That’s because I have many different meshes and don’t want to keep many clones just in case. Then I call makeGeometryUnique on the cloned mesh, because in my app I can hover another instance and change its color so I don’t want the color buffer to be shared.

It seems to work here, although the white color is not working anymore. In my app calling makeGeometryUnique leads to the error below:

RangeError: Offset is outside the bounds of the DataView
    at DataView.getFloat32 (<anonymous>)
    at Function.VertexBuffer._GetFloatValue (buffer.js:741)
    at Function.VertexBuffer.ForEach (buffer.js:672)
    at VertexBuffer.forEach (buffer.js:578)
    at VertexBuffer.getFloatData (buffer.js:423)
    at Geometry.getVerticesData (geometry.js:444)
    at Geometry.copy (geometry.js:1050)
    at Mesh.makeGeometryUnique (mesh.js:1625)

Any idea what might be happening?

makeGeometryUnique is not really compatible with meshes that have thin instances.

In your case, simply setting thinInstanceCount = 0 should work. In your modified PG, you need to set useVertexColors = false on the cloned mesh:

https://playground.babylonjs.com/#RC2IAH#12

1 Like

Thank you! Setting thinInstanceCount = 0 did not have any effect, however useVertexColors = false efficiently cuts the color buffer leaking onto my cloned mesh. Problem solved! :beers:

Not sure why thinInstanceCount isn’t doing anything, but seems cleaner so I’ll leave it there in case I think.

In fact you don’t need to set thinInstanceCount to 0 because when cloning a mesh with thin instances, the thin instance data are not copied to the cloned mesh, so the cloned mesh has no thin instances defined for it.