Search Unity

Question Per-instance shader properties with DrawMeshInstanced + MaterialPropBlocks

Discussion in 'General Graphics' started by TheSmokingGnu, Aug 3, 2021.

  1. TheSmokingGnu

    TheSmokingGnu

    Joined:
    May 1, 2017
    Posts:
    22
    I am trying to batch-draw objects with DrawMeshInstanced. They all have same mesh and material, except I need to set them different _MainTex_ST (to implement texture cycling) per instance, without breaking batching.

    What I do now - I pass a MaterialPropertyBlocks parameter to DrawMeshInstanced, with an array of Vector4 values that holds a value for _MainTex_ST property of each object:


    Code (CSharp):
    1. var propBlock = new MaterialPropertyBlock();
    2.         int texScaleOffsetPropertyId = Shader.PropertyToID("_MainTex_ST");
    3.  
    4.         Vector4 ScaleOffset(float offset) => new Vector4(1, 1, 0, offset);
    5.         propBlock.SetVectorArray(texScaleOffsetPropertyId, new Vector4[2] { ScaleOffset(0.6f), ScaleOffset(0) });
    6.  
    7.         var mesh = GetComponent<MeshFilter>().mesh;
    8.         var material = GetComponent<Renderer>().material;
    9.  
    10.      
    11.         var transforms = new Matrix4x4[] {
    12.             Matrix4x4.TRS(transform.position + Vector3.right, Quaternion.identity, Vector3.one),
    13.             Matrix4x4.TRS(transform.position + Vector3.right * 2, Quaternion.identity, Vector3.one),
    14.         };
    15.         Graphics.DrawMeshInstanced(mesh, 0, material, transforms, transforms.Length, propBlock);
    However, as a result I get this:
    all models (2 in the example) are rendered with the value of the first element of property block array - seems like all the others are ignored. Batching still works - only 1 draw call for DrawMeshInstanced call.

    Am I doing something wrong? Or is this the limitation of batching in unity - are per-instance material properties not supported?

    Is the custom shader required to access those per-instance properties from MaterialPropertyBlocks?

    P.S I tried this in both URP and Built-in, just to be sure. Same effect.
     
  2. Desoxi

    Desoxi

    Joined:
    Apr 12, 2015
    Posts:
    195
    This probably happens because you are not using a shader that can use the information you send to it through your property blocks. Which shader are you using?