Cloning after thin instance back compat issue

repro: https://playground.babylonjs.com/#CP2RN9#214

Switch to 6.49.0 to see the difference. On local dev, I managed to repro in 7.40.3, 7.40.2 does not show the bug. Unless this is a breaking change…

Basically, cloning after setting thin instance fouls up the position of the clone. Did not test rotations/scaling, I presume the transform matrix should be borked somehow?

I’m not sure what we should see in the PG, but we fixed a bug lately wrt to mesh cloning and thin instances:

Note that you can test a PG with different versions of Babylon by adding ?version=XXX in the url:

I cloned the mesh (p1), placed it at (1,0,0). Then added a thin instance to mesh at position(2,2,2). Then I cloned same mesh again (p2), placed it at (2,0,0). In 7.40.2, the clones and thin instance are independent of each other, so I can clone and add thin instances w/o affecting each other.

In 7.40.3, the p2 clone’s position is not at (2,0,0), it seems to be offset from the thin instance’s. I have to place p2 at p2.position.copyFromFloats(0,-2,-2); to get identical output as 7.40.2. Also, if you change rotation of the thin instance with const rotmat = BABYLON.Matrix.RotationY(Math.PI);, the result becomes more obvious.

I tried to build a simple repro according to your description:

White is original mesh, red is p1 and green is p2.

p2 is at (2,0,0) in this PG. The difference with 7.40.2 (https://playground.babylonjs.com/?version=7.40.2#CP2RN9#216) is that thin instances are not lost when cloning, so p2 also has an additional thin instance:

Thin instances of p2 are independent from thin instances of mesh, so you can remove/add thin instances to one mesh without affecting the other one.

Before 7.40.3, when you clone a mesh, the behavior is that: if said mesh has thin instances, the clone does not have thin instances. This is the default. I can have 1 million thin instances and clone mesh twice, visually, I will see 1 million+2, right?

After 7.40.3, when you clone a mesh: if said mesh has thin instances, any subsequent cloning of said mesh will also generate thin instances. This is the new behavior. So, if I have 1 million thin instances and clone mesh twice, suddenly, I will see 3 million?! c.f., https://playground.babylonjs.com/#CP2RN9#217

I just want to get the original behavior (pre-7.40.3) back. What should I do such that when I clone a mesh (that already has thin instances), it does not generate visuals I never asked for/wanted?

Yes, it’s the new behavior, because the old one was a bug: thin instances pertain to a mesh, if you clone a mesh, you should have the thin instances too.

If you don’t want them, you can simply set mesh.thinInstanceCount = 0:

1 Like

Awesome! Thanks, I got it now!

edit: nvm, I figured it out.

Everything is carried over.

There’s no light in the PG you’ve linked, which is why it’s the emissive color that’s defined, not the diffuse color:

1 Like

You replied before my edit :slight_smile:. In hindsight, I think this is a breaking change and the doc should show? I have to hit the sack , will do more testing tom, cheers !

I’ve updated the comment of my PR to indicate it could be a breaking change and how to fix it.

So after sleep and testing on my local dev, I made some observations:

a) mesh.thinInstanceCount = 0; vs mesh.clone(name,newParent?,doNotCloneChildren?,clonePhysicsImpostor?,doNotGenThinInstances?)
I’d prefer for a doNotGenThinInstances flag (default:true) on the clone method to bypass the new code/retain old behavior for some potential savings in mem/cycles. What do you think?

b) If cloning is supposed to retain thin instances, then it should also do the same for normal instances?

c) As I kept testing, this question kept popping up: If clones are supposed to retain thin instances, why not just add thin instances to the buffers of the mesh in the first place?


Had to make a pic just to be sure. M1 is the master mesh, C1 is the clone. In Fig1: As per 7.40.3, we are retaining thin instances at creation time which is equivalent to Fig2 where the user should be adding to the buffers of M1 if he/she so require thin instances?

Isn’t 1 set of buffers easier to manage and maintain then passing buffers down to clones? I tot that was the whole point of thin instances in the first place? There must be some burning reason I’m missing…hrm…