Material Property Block

Per renderer override to properties of the material


//Never use:
GetComponent<MeshRenderer>().material.color = Color.red; //will duplicate the material (in runtine extra drawcall) and set value (probable material leak)
GetComponent<MeshRenderer>().sharedMaterial.color = Color.red; //will modify the material asset

//Use:
Color color = Color.red;
static readonly int shaderPropertyColor = Shader.PropertyToID( "_Color");
MaterialPropertyBlock materialPropertyBlock;

public MaterialPropertyBlock MaterialPropertyBlock{
	get{
		if(materialPropertyBlock==null) {
			materialPropertyBlock = new MaterialPropertyBlock();
		return materialPropertyBlock;
	}
}

void ApplyColor(){
	MeshRenderer renderer = GetComponent<MeshRenderer>();
	MaterialPropertyBlock.SetColor( shaderPropertyColor, color);
	renderer.SetPropertyBlock( Mpb );
}

void OnEnable(){
	ApplyColor();
}

//If I want to change the color in a editor tool => Called on property change
void OnValidate(){
	ApplyColor();
}

Inserire qua il codice di material property block con preview in editor

<aside> 💡

Less materials can mean more performant?

In the general sense, yes. More specifically it's fewer set pass calls. Each material requires a new set pass which for the narrow view of this thread can be thought of as "Hey GPU, you're rendering with these material property values now". Using a material property block does not avoid this as the material properties are still changing, so there's little to no impact on the GPU performance in using multiple materials vs property blocks.

On the CPU side there's some memory benefits to using property blocks as only those properties that are changing have to be stored in memory vs. storing a complete copy of all of the material's settings. This does come at the cost of a little

bit of CPU performance, but it's mostly inconsequential and likely less than swapping out a full material most of the time. If you're going to be changing values frequently, then a property block is definitely faster. It's potentially faster to have multiple materials if the modified values aren't ever changed after their initial modification. But there's a big asterisk for both of those.

If you're changing the property block often, but are modifying the properties from multiple scripts (or from an animation, which also uses property blocks) then you may need to use GetPropertyBlock() to not stomp on existing settings. That can end up being slower than modifying the material itself.

If you're using instancing then you have to use property blocks to modify the instanced properties, otherwise multiple materials will never be instanced even if all of their settings are identical.

So; TLDR: Yes, use property blocks. No, don't use [PerRendererData] unless you don't want the value visible in the editor. Avoiding multiple materials is good for memory usage, and avoids potential memory leaks from not destroying them properly.

</aside>

Useful Video

https://www.youtube.com/watch?v=Ci6AuQq_uI4