Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Standard shader, instancing and MaterialPropertyBlock

Discussion in '5.6 Beta' started by iivo_k, Feb 25, 2017.

  1. iivo_k

    iivo_k

    Joined:
    Jan 28, 2013
    Posts:
    314
    While doing some performance testing drawing cubes, I noticed that when using the standard shader the batching doesn't work when I use MaterialPropertyBlocks in the code to change colors. I then tested a new surface shader I created with the Create Standard Surface Shader menu option which had the same problem. However an instanced shader I created with the Create Standard Surface Shader (Instanced) menu option in 5.4 or 5.5 worked properly, but that option is no longer available. Did some more testing and noticed the cause in the shader code.

    Fast with MPB colors:
    Code (CSharp):
    1. UNITY_DEFINE_INSTANCED_PROP(fixed4, _Color)
    Slow with MPB colors:
    Code (CSharp):
    1. fixed4, _Color;
    Does this mean that if you want to use MPB:s to set colors to instanced objects, you should use your own shader, or is this something that can be fixed with the standard shader?
     
  2. zeroyao

    zeroyao

    Unity Technologies

    Joined:
    Mar 28, 2013
    Posts:
    169
    Hey,

    Yes you will need to create a shader to explicitly declare Color as a per-instance property. Keep the number of per-instance properties as small as possible because they are under the hood uniforms arrays and are collected from all objects by the instancing engine hence some performance costs. For the same reason we don't by default make Color per-instance for Standard shader.
     
    iivo_k likes this.