How to set Instanced SubMesh with its own color / material?

Hey guys, I would like to first push forward my PG example here:

The question is simple! Here I have a mergedMesh of a group of spheres, which has a multiMaterial consist of 3 standardMaterials. Then an instancedMesh is created from this mergedMesh, resulting in two groups of spheres in the scene.

The problem is that I wish to change the color / material of any one sphere in the instancedMesh without influencing the spheres in the mergedMesh, yet I found it hard to achieve. Here is some of my attemps:

  1. I’ve tried to alter the instanceBuffer.color property of the instancedMesh, but it changed the color of all spheres rather than the one I wanted.

  2. I’ve tried to set subMesh[i].materialIndex = 1 for the instancedMesh, which did not work at all, and if I set subMesh[i].materialIndex = 1 for the mergedMesh, two spheres changed their color simutaneously.

  3. I’ve tried to set instancedMesh.material = mutiMaterial, yet an error came out indicating that instancedMesh has only a getter on material property.

So what is the right way to do this? :upside_down_face: It bothers me for more than a weeeeeek.

All instances of a mesh use the same material (or multi material if the material is a multi material) than the source mesh. That’s what makes instances performant (we can draw all of them in a single draw call), so you won’t be able to change that.

Maybe you can try not to use a multi-material and use the instance color buffer to change the color of one sphere?

Ya I’ve done a little change to my PG with your advice, the problem is the same that if you could notice ~ I could only register instance buffer on the level of that merged mesh rather than its submesh, so the result is that all spheres shares the same color for each group, the merged ones and the instanced ones, how could I change only one color of an instanced sphere?
HAHA maybe I’m just being greedy, but it could be great if there is some sort of solution :slight_smile:

@Evgeni_Popov

What you want to do is not possible. You will have to create 5 instances of a sphere and set the color of each instance with instancedBuffers.color instead.