Scaling instances of a mesh with multimaterial negatively, changes the material of the instance

I am facing a problem with instances. I have a mesh with a multimaterial associated with it. When I scale any of it’s instances negatively, the material of that instance changes.

Here is the playground for your reference,

Any solution or fix for this?

Each instance has its own copy of the sub meshes of its master mesh.

If you update a sub mesh of the master mesh (sphere.subMeshes[0].materialIndex = 1; in your PG) after having created the instances, this update won’t be visible to the instances.

Normally it does not have any impact because when rendering the instances, the parameters from the master are used. So, materialIndex = 1 will be in effect for the master mesh + instances.

However, when doing scale.[x|y|z] = -value on an instance, this instance can’t be rendered anymore with the other instances because they don’t share the same sign for the determinant of their world matrix (meaning the winding of the faces is not the same for all instances), so the system must issue two draw calls, one for instances with sign > 0 and another for instances with sign < 0.

In your PG, it means sphere2 will be rendered alone, and in this case the parameters of the sub mesh used for the rendering are the parameters of the sub mesh owned by sphere2. As this sub mesh has still materialIndex = 0, you get the wrong color.

You have two choices to correct the problem:

The second solution works for me. Thanks @Evgeni_Popov