Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Add Displacement Matrix To Existing Matrix In Compute Shader

Discussion in 'Shaders' started by chadfranklin47, Dec 30, 2021.

  1. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    221
    Hello, I am trying to implement animation layers with GPU Instancing and in order to do so, rather than baking the positions and rotations of each bone into a texture, except the root bone, I want to bake the translation and the rotation (from the parent bone to the current bone) into the texture.

    I believe I have this part down. Rather than:

    boneMatrices[index] = boneTransform.localToWorldMatrix * bindPoses[boneIndex];


    I am doing:

    Code (CSharp):
    1. Matrix4x4 displacement = Matrix4x4.TRS(boneTransform.position - parentBoneTransform.position, Quaternion.Inverse(parentBoneTransform.rotation) * boneTransform.rotation, Vector3.one);
    2.                         boneMatrices[index] = displacement;
    Now, after passing these "displacement" matrices into a compute shader, I would like to "add" the displacement for each bone to the resulting matrix of its parent bone.

    To be more descriptive, I am setting the root bone's matrix normally (position and rotation). But, for each child bone down the hierarchy, I wish to use said child's parent bone matrix (float4x4) as a base, and "add" the displacement of said child bone (float4x4) to calculate the final position and rotation of the child bone.

    I did this simple test in C# that seems to confirm that this can work as the resulting transforms r1-r4 matched the original transforms t1-t4:

    Code (CSharp):
    1. Matrix4x4 t1Matrix = Matrix4x4.TRS(t1.position, t1.rotation, t1.lossyScale);
    2.         Matrix4x4 t2Displacement = Matrix4x4.TRS(t2.position - t1.position, Quaternion.Inverse(t1.rotation) * t2.rotation, Vector3.one);
    3.         Matrix4x4 t3Displacement = Matrix4x4.TRS(t3.position - t2.position, Quaternion.Inverse(t2.rotation) * t3.rotation, Vector3.one);
    4.         Matrix4x4 t4Displacement = Matrix4x4.TRS(t4.position - t3.position, Quaternion.Inverse(t3.rotation) * t4.rotation, Vector3.one);
    5.  
    6.         r1.SetPositionAndRotation(t1Matrix.GetColumn(3), t1Matrix.rotation);
    7.  
    8.         r2.SetPositionAndRotation(r1.position + (Vector3)t2Displacement.GetColumn(3), r1.rotation * t2Displacement.rotation);
    9.  
    10.         r3.SetPositionAndRotation(r2.position + (Vector3)t3Displacement.GetColumn(3), r2.rotation * t3Displacement.rotation);
    11.  
    12.         r4.SetPositionAndRotation(r3.position + (Vector3)t4Displacement.GetColumn(3), r3.rotation * t4Displacement.rotation);
    But I am unsure of how to do this inside a compute shader and with matrices.

    Please let me know if any more info would help. Any help would be appreciated. Thanks.
     
    Last edited: Dec 31, 2021