Thanks for posting these tips. I was devastated when my project dropped to 3 FPS because material properties don't work out of the box. I was able to edit the generated shader to go from 5000 draw calls to ~15. Here are some relevant snippets. I am only changing the color of my game objects in this example
Find all of these blocks of code (there are several copies)
Code (CSharp):
-
CBUFFER_START(UnityPerMaterial)
-
float4 _BaseColor;
-
CBUFFER_END
Replace them with this
Code (CSharp):
-
CBUFFER_START(UnityPerMaterial)
-
UNITY_INSTANCING_BUFFER_START(Props)
-
UNITY_DEFINE_INSTANCED_PROP(float4, _BaseColor)
-
UNITY_INSTANCING_BUFFER_END(Props)
-
CBUFFER_END
Then find every instance of _BaseColor in the shader functions (there are several copies)
Code (CSharp):
-
SurfaceDescription SurfaceDescriptionFunction(SurfaceDescriptionInputs IN) {
-
SurfaceDescription surface = (SurfaceDescription)0;
-
float4 _Property_A850853A_Out_0 = _BaseColor;
-
....
Replace them with UNITY_ACCESS_INSTANCED_PROP(Props, _BaseColor)
Code (CSharp):
-
SurfaceDescription SurfaceDescriptionFunction(SurfaceDescriptionInputs IN) {
-
SurfaceDescription surface = (SurfaceDescription)0;
-
float4 _Property_A850853A_Out_0 = UNITY_ACCESS_INSTANCED_PROP(Props, _BaseColor);
-
...
And lastly don't forget to set the material properties on your game object
Code (CSharp):
-
int colorPropId = Shader.PropertyToID("_BaseColor");
-
var matProps = new MaterialPropertyBlock();
-
matProps.SetColor(colorPropId, color);
-
renderer.SetPropertyBlock(matProps);