# Cloning after thin instance back compat issue

**URL:** <https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688>\
**Category:** Questions\
**Created:** [January 2, 2025, 3:32am UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688 "2025-01-02T03:32:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![phaselock](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@phaselock](https://forum.babylonjs.com/u/phaselock)\
**Post date:** [January 2, 2025, 3:32am UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/1 "2025-01-02T03:32:37Z")

</div>

repro: [https://playground.babylonjs.com/#CP2RN9#214](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?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 2, 2025, 11:10am UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/2 "2025-01-02T11:10:02Z")

</div>

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

> <https://github.com/BabylonJS/Babylon.js/pull/15997>
>
> See https://forum.babylonjs.com/t/there-is-an-issue-with-model-cloning/55387/3

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

- 7.40.2: [https://playground.babylonjs.com/?version=7.40.2#CP2RN9#214](https://playground.babylonjs.com/?version=7.40.2#CP2RN9#214)
- 7.40.3: [https://playground.babylonjs.com/?version=7.40.3#CP2RN9#214](https://playground.babylonjs.com/?version=7.40.3#CP2RN9#214)

---

<div class="post-metadata">

**Author:** ![phaselock](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@phaselock](https://forum.babylonjs.com/u/phaselock)\
**Post date:** [January 2, 2025, 11:26am UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/3 "2025-01-02T11:26:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [January 2, 2025, 7:37pm UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/4 "2025-01-02T19:37:15Z")

</div>



---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 6, 2025, 10:19am UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/5 "2025-01-06T10:19:23Z")

</div>

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

> **[Babylon.js Playground](https://playground.babylonjs.com/#CP2RN9%23216)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL and WebGPU 3D scenes

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](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:

| Before 7.40.3 | 7.40.3+ |
| --- | --- |
| ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/0/f/0f8e9758d1b8a252d664fdddb4c3be2cde5b2652.png) | ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/6/7/6700f99bd2e6720d999bbd48a935fab0b2d7444b.png) |

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.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 6, 2025, 10:19am UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/6 "2025-01-06T10:19:28Z")

</div>



---

<div class="post-metadata">

**Author:** ![phaselock](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@phaselock](https://forum.babylonjs.com/u/phaselock)\
**Post date:** [January 6, 2025, 11:39am UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/7 "2025-01-06T11:39:53Z")

</div>

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](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?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 6, 2025, 11:59am UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/8 "2025-01-06T11:59:07Z")

</div>

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`:

> **[Babylon.js Playground](https://playground.babylonjs.com/#CP2RN9%23218)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL and WebGPU 3D scenes

---

<div class="post-metadata">

**Author:** ![phaselock](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@phaselock](https://forum.babylonjs.com/u/phaselock)\
**Post date:** [January 6, 2025, 1:23pm UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/9 "2025-01-06T13:23:52Z")

</div>

> [@Evgeni\_Popov](#):
>
> If you don’t want them, you can simply set `mesh.thinInstanceCount = 0`

Awesome! Thanks, I got it now!

edit: nvm, I figured it out.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 6, 2025, 1:32pm UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/10 "2025-01-06T13:32:22Z")

</div>

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:

> **[Babylon.js Playground](https://playground.babylonjs.com/#V1JE4Z%23219)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL and WebGPU 3D scenes

---

<div class="post-metadata">

**Author:** ![phaselock](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@phaselock](https://forum.babylonjs.com/u/phaselock)\
**Post date:** [January 6, 2025, 1:44pm UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/11 "2025-01-06T13:44:28Z")

</div>

You replied before my edit 🙂. 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 !

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 6, 2025, 3:45pm UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/12 "2025-01-06T15:45:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![phaselock](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@phaselock](https://forum.babylonjs.com/u/phaselock)\
**Post date:** [January 7, 2025, 12:48am UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/13 "2025-01-07T00:48:20Z")

</div>

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?

 ![comparison](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/5/c/5c78a418ad243e2b5c309518270ed84465362994.jpeg)  
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…

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 8, 2025, 10:12am UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/14 "2025-01-08T10:12:14Z")

</div>

> [@phaselock](#):
>
> a) `mesh.thinInstanceCount = 0;` vs `mesh.clone(name,newParent?,doNotCloneChildren?,clonePhysicsImpostor?,doNotGenThinInstances?)`

Good idea! Would you like to make a PR for it? However, I think we should now support passing an `option` object to `clone`, instead of separate flags, to avoid bloating the interface.

> [@phaselock](#):
>
> b) If cloning is supposed to retain thin instances, then it should also do the same for normal instances?

Yes, I think it’s debatable (we never had a request for it, that’s probably why regular instances are not cloned).

> [@phaselock](#):
>
> 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?

Sharing thin instance buffers would introduce additional complexities, as we’d have to refcount them. Also, I’m not sure there are many use cases where several different meshes would use exactly the same list of thin instances? If this happens, it’s always possible to share the matrix buffer by calling `thinInstanceSetBuffer` on both meshes and passing the same buffer.

Thin instances have been introduced as a thin layer to manage instantiation, so we would like to keep this interface as simple as possible and not introduce too much complexity.

---

<div class="post-metadata">

**Author:** ![phaselock](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@phaselock](https://forum.babylonjs.com/u/phaselock)\
**Post date:** [January 8, 2025, 11:45am UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/15 "2025-01-08T11:45:32Z")

</div>

> [@Evgeni\_Popov](#):
>
> Would you like to make a PR for it?

Not yet, still testing this breaking change. Found other broken visuals.

> [@Evgeni\_Popov](#):
>
> Sharing thin instance buffers would introduce additional complexities, as we’d have to refcount them. Also, I’m not sure there are many use cases where several different meshes would use exactly the same list of thin instances? If this happens, it’s always possible to share the matrix buffer by calling `thinInstanceSetBuffer` on both meshes and passing the same buffer.

Erm, I think there is some miscommunication, I’m not advocating sharing buffers. To be clear, what I meant was the previous 7.40.3 PG: [https://playground.babylonjs.com/#CP2RN9#216](https://playground.babylonjs.com/#CP2RN9#216) is equivalent to this: [https://playground.babylonjs.com/?version=7.40.2#CP2RN9#220](https://playground.babylonjs.com/?version=7.40.2#CP2RN9#220). I’m not understanding the need for clones to also clone thin instance buffers.

Given my extensive use of clones and thin instances, this ‘bug’ should have been caught when thin instances were first introduced and not after 4/5 yrs. I just don’t understand where the bug is, shrugs…

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 8, 2025, 12:39pm UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/16 "2025-01-08T12:39:23Z")

</div>

The problem was that [this PG](https://playground.babylonjs.com/#92Y727#453) (linked in [There is an issue with model cloning](https://forum.babylonjs.com/t/there-is-an-issue-with-model-cloning/55387)) failed because of thin instances not being copied when the mesh is cloned.

We think that copying the thin instances when cloning is the right fix, but I understand your point. Maybe we could update the behavior so that cloning doesn’t copy thin instances by default, and add a parameter to clone to force copying the thin instances. What do you think @sebavan and @Deltakosh? This problem only surfaced 3 weeks ago, so I guess only very few people were impacted by the fact that thin instances weren’t copied.

---

<div class="post-metadata">

**Author:** ![phaselock](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@phaselock](https://forum.babylonjs.com/u/phaselock)\
**Post date:** [January 8, 2025, 1:22pm UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/17 "2025-01-08T13:22:59Z")

</div>

> [@Evgeni\_Popov](#):
>
> The problem was that [this PG](https://playground.babylonjs.com/#92Y727#453) (linked in [There is an issue with model cloning](https://forum.babylonjs.com/t/there-is-an-issue-with-model-cloning/55387)) failed because of thin instances not being copied when the mesh is cloned.

Sorry, its late here, I’m not sure if I’m seeing stuff correctly. I did a quick look between the old PG: [https://playground.babylonjs.com/?version=7.40.2#92Y727#453](https://playground.babylonjs.com/?version=7.40.2#92Y727#453) and the new:[https://playground.babylonjs.com/#92Y727#453](https://playground.babylonjs.com/#92Y727#453) using `console.log(container.meshes.length)`. They both show `193` before and after cloning so the number of meshes match. The glb has a mix of `Mesh` and `InstancedMesh`, the obvious difference is that `node180` to `node191` have scaling of 1. I suspect the user’s model might not have applied transformations during export? But otherwise, I’m not seeing missing thin instances…maybe I’ll take a closer look tom.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 8, 2025, 2:31pm UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/18 "2025-01-08T14:31:43Z")

</div>

The glb file uses the “EXT\_mesh\_gpu\_instancing” extension, which is implemented with thin instances in Babylon. Look at the console log in this modified PG, which dumps meshes with thin instances:

> **[Babylon.js Playground](https://playground.babylonjs.com/#92Y727%23460)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL and WebGPU 3D scenes

If you browse this PG in 7.40.2 ([https://playground.babylonjs.com/?version=7.40.2#92Y727#460](https://playground.babylonjs.com/?version=7.40.2#92Y727#460)), you will see that cloned meshes don’t have thin instances.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 8, 2025, 4:26pm UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/19 "2025-01-08T16:26:46Z")

</div>

Here’s the PR that removes the breaking change:

> <https://github.com/BabylonJS/Babylon.js/pull/16046>
>
> See https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/…55688/16 for context.
> 
> Follow up to #15997. It removes the breaking change, as the user will now have to explicitely ask for thin instances cloning.
> 
> Original PG fixed with the change from this PR:
> 
> https://playground.babylonjs.com/#92Y727#461

---

<div class="post-metadata">

**Author:** ![phaselock](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@phaselock](https://forum.babylonjs.com/u/phaselock)\
**Post date:** [January 9, 2025, 3:31am UTC](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688/20 "2025-01-09T03:31:08Z")

</div>

> [@Evgeni\_Popov](#):
>
> The glb file uses the “EXT\_mesh\_gpu\_instancing” extension, which is implemented with thin instances in Babylon.

Ah, so that’s the reason, got it!

> [@Evgeni\_Popov](#):
>
> you will see that cloned meshes don’t have thin instances.

Yes, I see it now. I had to switch off 180 meshes and compare PGs. (Idea: a little quality of life fix for the Inspector to allow users to toggle vis/bounding box by clicking & dragging the mouse over the icons instead of having to click each time).

> [@Evgeni\_Popov](#):
>
> Here’s the PR that removes the breaking change

Awesome, tyvm! 👍

[Next page](https://forum.babylonjs.com/t/cloning-after-thin-instance-back-compat-issue/55688.md?page=2)
