Per Renderer Variables w/ Multiple Materials

Hello,

I have a shader that implements a [PerRendererData] color property.

I have a cube that has 2 materials which both use this shader.

Now my question, how can I set the MaterialPropertyBlock for each material separately? When I set the renderer’s MaterialPropertyBlock it of course sets the color property for both materials. It would make sense that you can’t because it’s ‘PerRendererData’ and not ‘PerMaterialData’, but curious to know if there is a way to achieve this?

As a side note, I’m trying to add a ‘Tattoo’ to the surface of the cube. Using MaterialPropertyBlocks is just so different blocks can have different colors without creating material instances.

If I understand you correctly, you have a mesh with two submeshes using separate materials which both utilize the same shader, correct?

If so, you’d need to do something like this:

Material[] mats = renderer.sharedMaterials;
mats[0].SetColor("_NameOfColorAttr", firstColor);
mats[1].SetColor("_NameOfColorAttr", secondColor);
renderer.sharedMaterials = mats;

Note that this would require the materials to be different on start (e.g. not the same material in both slots) and it would affect every object using those materials.

If you don’t want that, then use the ‘materials’ attribute rather than the ‘sharedMaterials’ attribute in the above code.

@I-am-Lawrence Im like 2 yrs late but I think getpropertyblock and setpropertyblock have a second parameter to make it into a per material set or get.