# Vertex error and no colors on thin instances after upgrade

**URL:** <https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492>\
**Category:** Bugs\
**Created:** [March 17, 2022, 6:00am UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492 "2022-03-17T06:00:52Z")\
**Posts on this page:** 19\
**Page:** 2

<div class="post-metadata">

**Author:** ![tibotiber](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/tibotiber/32/26735_2.png) [@tibotiber](https://forum.babylonjs.com/u/tibotiber)\
**Post date:** [April 24, 2022, 7:15am UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/21 "2022-04-24T07:15:03Z")

</div>

Ah, I had tried that but I probably screwed up somewhere else when I did 😅. Thanks for the help and PR for backward compatibility. So i’m up to the next bug (please bear with me 😬). Now if you try to clone the instanced mesh in that PG, you get a shader error. I don’t really “see” anything wrong on the scene but the shader is screaming something that seems related to the color to instanceColor change. I thought it might be worth reporting.

PG below and interesting line is 107.

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

Thanks again!

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [April 24, 2022, 9:04pm UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/22 "2022-04-24T21:04:12Z")

</div>

Hmm, after copying over the instancesCount then it works:

```auto
clone._thinInstanceDataStorage.instancesCount = source._thinInstanceDataStorage.instancesCount;

```

For example here’s your [PG](https://playground.babylonjs.com/#RC2IAH#58) with this fix and also here’s a simpler [PG](https://playground.babylonjs.com/#217750#72) to show the issue (if you comment out line 45 then you can see how it isn’t working without setting the instancesCount).

EDIT: I started a new thread for this issue of needing to set the internal count directly since it turned out to be unrelated. 🙂

> [@Can't set thinInstanceCount for clone](https://forum.babylonjs.com/t/cant-set-thininstancecount-for-clone/29674):
>
> Heya, while investigating a separate issue on [this thread](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/22) I stumbled onto an issue where the thinInstanceCount for a clone can’t be set to any value except for zero. Here’s a simple PG to show the issue. It looks like a simple way to fix it is to fall back on using the source mesh to compute the clone’s numMaxInstances value, like on this PR for example.

---

<div class="post-metadata">

**Author:** ![tibotiber](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/tibotiber/32/26735_2.png) [@tibotiber](https://forum.babylonjs.com/u/tibotiber)\
**Post date:** [April 25, 2022, 1:37am UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/23 "2022-04-25T01:37:27Z")

</div>

Hi Blake,

So this is not the bug I’m seeing actually. I’ve modified your simpler PG to reflect the bug:

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

It’s basically the same but with PBR material on the sphere, and I’m cloning the instanced mesh into a non instances mesh. I think that when switching from instanced to non-instanced, some color / instanceColor reference in the shader must be missing. Maybe the material / shader should preserve both values so we can switch between instanced or not without issue? (I honestly don’t understand the internals)

Notes about the PG:

1. if you clone from instanced to instanced (uncomment line 54), there is no error.
2. if you remove the PBR material (set false line 25), there is no error.

I hope this helps. Happy to help further but i’m not sure where to look.  
Cheers.

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [April 25, 2022, 2:30am UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/24 "2022-04-25T02:30:58Z")

</div>

Oops, I didn’t realize that you were trying to clone the mesh that has thin instances and get a mesh without thin instances. I don’t think that will work because they share the same vertices and buffers… I’m pretty sure what you need for that is unique geometry.

For example you can clone the mesh and call makeGeometryUnique() on the clone before you start with any thin instances and then clone that mesh whenever you want a clone without thin instances. Maybe others will have a better way but that’s the only way I’ve ever found to do it… 🙂

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

---

<div class="post-metadata">

**Author:** ![tibotiber](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/tibotiber/32/26735_2.png) [@tibotiber](https://forum.babylonjs.com/u/tibotiber)\
**Post date:** [April 25, 2022, 2:57am UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/25 "2022-04-25T02:57:48Z")

</div>

Thanks for the super quick reply.

That’s strange because I’ve been cloning like this (without making the geometry unique) without any issue until this change to color / instanceColor. I appreciate the solution but it’s not ideal as I potentially have a large number of meshes to clone “just in case” they’re interacted with. At the moment, if a mesh is selected to make changes, then I clone it on the fly to put gizmos on it, etc. I’m worried about the impact of having to keep a disabled clone for each mesh in my scene.

So far the only precautions I’ve had to take when cloning were to call `clone.thinInstanceCount = 0` and `clone.useVertexColors = false`.

Interestingly, the code actually works and the scene is behaving as expected right now. Just there is this shader error that gets printed. Is there a chance we might find a simple fix to maintain the behaviour I’ve relied on without having this error printed? It seems to boil down to the color / instanceColor variables being kept in sync, but I’m not sure exactly where to do that.

Thanks again for the help 🙂

---

<div class="post-metadata">

**Author:** ![Blake](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blake/32/21509_2.png) [@Blake](https://forum.babylonjs.com/u/Blake)\
**Post date:** [April 25, 2022, 3:14am UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/26 "2022-04-25T03:14:37Z")

</div>

Ooh I never thought to try it that way, sounds good if don’t want to use vertex colors on it anyway… I’m not sure what would be needed to keep that behavior when using PBR thou, better wait for @Cedric and @Evgeni_Popov. 🙂

Have a good one 🍻 😴 💤

---

<div class="post-metadata">

**Author:** ![tibotiber](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/tibotiber/32/26735_2.png) [@tibotiber](https://forum.babylonjs.com/u/tibotiber)\
**Post date:** [April 25, 2022, 3:31am UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/27 "2022-04-25T03:31:41Z")

</div>

> [@Blake](#):
>
> Have a good one 🍻 😴 💤

I’d love to… but it’s Monday morning here 😭.

Thanks for your help and have a good one Blake.

---

<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:** [April 25, 2022, 8:28am UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/28 "2022-04-25T08:28:46Z")

</div>

This PR will fix the crash:

> <https://github.com/BabylonJS/Babylon.js/pull/12436>
>
> See https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-a…fter-upgrade/28492/23

However, due to how (thin) instance colors are now handled in 5.0, the cloned mesh will not inherit the color of the first thin instance as it happens in 4.2. That’s because vertex colors and instance colors are now two separate things whereas previously vertex colors were reused for instance colors (and so it was not possible to have both vertex and instance colors).

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [April 25, 2022, 8:34am UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/29 "2022-04-25T08:34:39Z")

</div>

It would be nice (and it’s possible) to mix instance color and vertex colors. I’ll add that to my todo list.

---

<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:** [April 25, 2022, 8:43am UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/30 "2022-04-25T08:43:13Z")

</div>

That should not be difficult as with your changes we now have separate data. I think we would simply need in the shader to multiply the vertex and instance color when both are used. Currently we have:

```nohighlight
#ifdef VERTEXCOLOR
	vColor = color;
#elif INSTANCESCOLOR
	vColor = instanceColor;
#endif

```

It could be replaced by something like:

```nohighlight
#if defined(VERTEXCOLOR) || defined(INSTANCESCOLOR)
    vColor = vec4(1.0);
    #ifdef VERTEXCOLOR
        vColor *= color;
    #endif
    #ifdef INSTANCESCOLOR
        vColor *= instanceColor;
    #endif
#endif

```

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [April 25, 2022, 9:03am UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/31 "2022-04-25T09:03:43Z")

</div>

Yes, I was thinking of something like that. Maybe moving that code block into its own macro as it’s duplicated a huge number of times in various shaders.

---

<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:** [April 25, 2022, 10:26am UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/32 "2022-04-25T10:26:36Z")

</div>

Also, related to the instance color change, I commented lately on your PR, I don’t know if you saw the comment:

[https://github.com/BabylonJS/Babylon.js/pull/12333#discussion\_r856880325](https://github.com/BabylonJS/Babylon.js/pull/12333#discussion_r856880325)

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [April 25, 2022, 11:43am UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/33 "2022-04-25T11:43:55Z")

</div>

Yes I did. I think both issues can be solved in the same PR.

---

<div class="post-metadata">

**Author:** ![tibotiber](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/tibotiber/32/26735_2.png) [@tibotiber](https://forum.babylonjs.com/u/tibotiber)\
**Post date:** [April 25, 2022, 1:55pm UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/34 "2022-04-25T13:55:31Z")

</div>

Love the conversation in here 🤩 and glad to see both backward compatibility improve and capabilities grow. Looking forward to @Evgeni_Popov’s PR being merged and published as well as @Cedric’s one. I’ll keep you posted on my experience once that’s ready for testing. Thanks again!

---

<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:** [April 25, 2022, 5:12pm UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/35 "2022-04-25T17:12:59Z")

</div>

Just for me to better understand, what is the missing part after:

> <https://github.com/BabylonJS/Babylon.js/pull/12436>
>
> See https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-a…fter-upgrade/28492/23

and

> <https://github.com/BabylonJS/Babylon.js/pull/12434>
>
> After cloning a mesh that has thin instances, setting thinInstanceCount on the c…lone currently doesn't work (unless it is set to 0 the setter will return without updating the internal instanceCount).
> 
> With this PR, if a mesh doesn't have the thin instance matrixData, then it checks to see if the mesh's source has it before defaulting the max instance count to 0.
> 
> PG Repro: https://playground.babylonjs.com/#217750#76
> Forum: https://forum.babylonjs.com/t/cant-set-thininstancecount-for-clone/29674

went in ?

I guess the shader part ? @Cedric @Evgeni_Popov @Blake is anyone creating the PR for it or should it be in [Fix thinInstanceSetAttributeAt to work with "color" kind by BlakeOne · Pull Request #12433 · BabylonJS/Babylon.js · GitHub](https://github.com/BabylonJS/Babylon.js/pull/12433)

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [April 25, 2022, 5:16pm UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/36 "2022-04-25T17:16:44Z")

</div>

Yes, shader part. I’m doing it as I need it for the demo.

---

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [May 4, 2022, 1:17pm UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/37 "2022-05-04T13:17:01Z")

</div>

> <https://github.com/BabylonJS/Babylon.js/pull/12472>
>
> Follow up https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-insta…nces-after-upgrade/28492
> 
> Mixing color instances from thin instances and vertex colors from mesh.

---

<div class="post-metadata">

**Author:** ![tibotiber](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/tibotiber/32/26735_2.png) [@tibotiber](https://forum.babylonjs.com/u/tibotiber)\
**Post date:** [May 25, 2022, 9:15am UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/38 "2022-05-25T09:15:07Z")

</div>

Hey folks!

I just got back to this, been a rough couple of weeks 😑.

I basically reverted all the changes I had done to get around the various bugs, upgraded from 5.0.0-alpha.60 to 5.7.0, tested and it automagically worked. Of course, I know who are the automagicians: @Cedric @Blake @Evgeni_Popov @Evgeni_Popov @sebavan – incredible team effort 🙂.

Thanks a lot for the support here, and the hardcore devotion to backward compatibility. This is one of the reasons I picked BJS 3 years ago and it keeps paying back 🙏.

Thanks again and kudos!

---

<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:** [May 25, 2022, 3:40pm UTC](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492/39 "2022-05-25T15:40:46Z")

</div>

Always love reading this kind of message and the main reason we are all pumped to continue 🙂

[Previous page](https://forum.babylonjs.com/t/vertex-error-and-no-colors-on-thin-instances-after-upgrade/28492.md?page=1)
