Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Interpolating Animations using SkinMatrix

Discussion in 'Graphics for ECS' started by LeFlop2001, May 14, 2023.

  1. LeFlop2001

    LeFlop2001

    Joined:
    Jan 30, 2020
    Posts:
    11
    Ive been using unity Graphics Skin Matrix and Blob Array to achieve mesh deformation at runtime. However when trying to blend to poses i couldnt figure out how to interpolate two matricies without causing artifacts.

    Ive read that the way to go is to destruct the matricies and lerp the translation and slerp the rotation components and then reform however this doesnt work.

    Code (CSharp):
    1.  
    2.                 for (int j = 0; j < bones; j++)
    3.                 {
    4.                     var trans0 = m0[j];
    5.                     var trans1 = m1[j];
    6.                    
    7.                     var rotMat = new float3x3(Quaternion.Slerp(trans0.rotation, trans1.rotation, animation.weight));
    8.  
    9.                     renderer.ElementAt(j).Value = new float3x4(
    10.                         rotMat.c0,
    11.                         rotMat.c1,
    12.                         rotMat.c2,
    13.                         math.lerp(trans0.position, trans1.position, animation.weight));
    14.                 }
    15.  
    this ofcourse works at weights 0 and 1 but fails otherwise.

    I didnt find much info on the skinMatrix topic and i wanted to be sure this i a bad idea before moving on to other animation options.
     
  2. Rukhanka

    Rukhanka

    Joined:
    Dec 14, 2022
    Posts:
    209
    Do not store matrices in BlobAssets. Store rotatation quaternion and position separately and make skin matrix after interpolation of those values.
     
  3. LeFlop2001

    LeFlop2001

    Joined:
    Jan 30, 2020
    Posts:
    11
    Thanks for the reply.
    From my understanding thats exactly what im doing, as shown in the code snippet
     
  4. Rukhanka

    Rukhanka

    Joined:
    Dec 14, 2022
    Posts:
    209
    Sorry, I was careless. I thought that trans0/1 has SkinMatrix type and you are doing double quaternion->matrix->quaternion conversion. It is hard to give you any practical advice by looking on your code, because Rukhanka Animation System is working by similar way (and blending is ok).
     
  5. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,292
    Here's what you need:
    1) You need to blend your local space transforms. That is weighted average for position and scale, and weighted nlerp for rotations.
    2) You need to transform your local transforms into root space. That requires knowing the hierarchy.
    3) You need to multiply the root-space matrix with the corresponding bone's bindpose which is stored in the Mesh. That's what then becomes the Skin Matrix.
     
  6. LeFlop2001

    LeFlop2001

    Joined:
    Jan 30, 2020
    Posts:
    11
    Not having the bindpose baked in and instead applying it at runtime after blending worked. Thanks big help