issue using GetWorldToObjectMatrix() with DrawMeshInstancedIndirect

Hello,

I am using Graphics.DrawMeshInstancedIndirect to render instances of a cube in my scene, and passing a unique transformation matrix to each instance. A computebuffer stores the transformation matrices (created using matrix.trs in c#), the matrices are edited in compute shader, and then the final shader applied to the mesh reads from this buffer of matrices and assigns the appropriate matrix using InstanceId as the index.

HOWEVER, while this works to transform the instanced mesh, I am noticing that it does not update GetWorldToObjectMatrix() for each instance. For example, I am using this matrix in the fragment shader to make an adjustment texture coordinates. The method works just fine using a regular mesh that doesn’t use a structuredbuffer to look up transformation matrix. The appropriate behavior is: when I rotate my object, the texture coordinates also rotate.

However, in the case where I am instancing with DrawMeshInstancedIndirect, the GetWorldToObjectMatrix() seems to do nothing at all. The instanced meshes all rotate correct, but the texture is not affected.

How do I get a similar function that updates in accordance with my per-instance transformation matrix?

Are you using procedural setup via UNITY_INSTANCING_PROCEDURAL_FUNC or manually modifying matrices related shader code? For first option you should set values for both unity_ObjectToWorld and unity_WorldToObject matrices. For the second option you should just set value for unity_WorldToObject at start of fragment shader.
GetWorldToObjectMatrix() and GetObjectToWorldMatrix() is trivial function that return UNITY_MATRIX_M and UNITY_MATRIX_I_M. This defines are unity_OobjectToWorld and unity_WorldToObject matrices for procedural instanced rendering.

1 Like