Graphics.drawmeshinstanced with an array of materialpropertyblocks?

Let’s say you want to draw many identical objects with different material properties using graphics.drawmeshinstanced

There is the MaterialPropertyBlock that allows you to override material properties without making a new material. This works great with graphics.drawmesh but graphics.drawmeshinstanced takes a whole array of matrices but only one MaterialPropertyBlock.

So how to pass data to each instance of the mesh when it’s rendered with drawmeshinstanced. Or if is only way is to do call graphics.drawmesh for each instance with a different materialpropertyblock.

What id really love is for the function to handle an array of materialpropertyblocks but since it doesn’t I’m wonder what the alternatives are…

Use the property unity_InstanceID to distinguish instance in the shader. You pass the parameter array to the material itself using a “compute buffer” and not to

C# land:

Shader land:
#ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
col = colorBuffer[unity_InstanceID];
#else
col = float4(0, 0, 1, 1);
#endif

Also see:
DrawMeshInstancedIndirect

Good luck. DRCS