Search Unity

Question Animation Instancing with BoneScale help...

Discussion in 'Shaders' started by JC-unity43, Apr 7, 2022.

  1. JC-unity43

    JC-unity43

    Joined:
    Apr 5, 2022
    Posts:
    1
    Hi guys . I need some help about Animation Instancing with Bonescale.

    I found animation instancing code from this
    https://blog.unity.com/technology/animation-instancing-instancing-for-skinnedmeshrenderer

    and it is perfectly working. but I want more so I started to research , how to make all Instance got diffrent BoneScale.

    I thought it would be simple.
    first. decompose the bone matrix from texture to scale,rotation,translation.
    second. switch scalematrix to MyNewScaleMatrix made by My Character's bonescale.
    third. compose MyNewScaleMatrix,rotation,translation.
    Finish!.
    so I want this
    View attachment 1034597
    I made left arm double sized so it is what i want.
    but the result is

    View attachment 1034588
    this is the problem..

    I don't know why this happen.
    It looks like my scale affect other matrix. cause if i doubled all bone scale, that left arm position is same as above picture.

    any advice would be nice to me
    so please give some advice.
    thanks


    here is my shader code from unity animation instancing.


    Code (CSharp):
    1.  
    2. float4x4 loadMatFromTexture(uint frameIndex, uint boneIndex)
    3. {
    4.     uint blockCount = _boneTextureWidth / _boneTextureBlockWidth;
    5.     int2 uv;
    6.     uv.y = frameIndex / blockCount * _boneTextureBlockHeight;
    7.     uv.x = _boneTextureBlockWidth * (frameIndex - _boneTextureWidth / _boneTextureBlockWidth * uv.y);
    8.  
    9.     int matCount = _boneTextureBlockWidth / 4;
    10.     uv.x = uv.x + (boneIndex % matCount) * 4;
    11.     uv.y = uv.y + boneIndex / matCount;
    12.  
    13.     float2 uvFrame;
    14.     uvFrame.x = uv.x / (float)_boneTextureWidth;
    15.     uvFrame.y = uv.y / (float)_boneTextureHeight;
    16.     float4 uvf = half4(uvFrame, 0, 0);
    17.  
    18.     float offset = 1.0f / (float)_boneTextureWidth;
    19.     float4 c1 = tex2Dlod(_boneTexture, uvf);
    20.     uvf.x = uvf.x + offset;
    21.     float4 c2 = tex2Dlod(_boneTexture, uvf);
    22.     uvf.x = uvf.x + offset;
    23.     float4 c3 = tex2Dlod(_boneTexture, uvf);
    24.     uvf.x = uvf.x + offset;
    25.     //half4 c4 = tex2Dlod(_boneTexture, uvf);
    26.     float4 c4 = float4(0, 0, 0, 1);
    27.     //float4x4 m = float4x4(c1, c2, c3, c4);
    28.  
    29.     float4x4 m;
    30.     m._11_21_31_41 = c1;
    31.     m._12_22_32_42 = c2;
    32.     m._13_23_33_43 = c3;
    33.     m._14_24_34_44 = c4;
    34.  
    35.  
    36.     half4x4 scaleMatrix;
    37.     half4 sx = half4(m._11, m._12, m._13, 0);
    38.     half4 sy = half4(m._21, m._22, m._23, 0);
    39.     half4 sz = half4(m._31, m._32, m._33, 0);
    40.     float scaleX = length(sx);
    41.     float scaleY = length(sy);
    42.     float scaleZ = length(sz);
    43.     scaleMatrix[0] = half4(scaleX, 0, 0, 0);
    44.     scaleMatrix[1] = half4(0, scaleY, 0, 0);
    45.     scaleMatrix[2] = half4(0, 0, scaleZ, 0);
    46.     scaleMatrix[3] = half4(0, 0, 0, 1);
    47.  
    48.     scaleMatrix *= _boneScaleArray[boneIndex].x; //My character bone scale. xyz is all same.
    49.  
    50.     float4x4 rotationMatrix;
    51.  
    52.     rotationMatrix._11_21_31_41 = float4(c1.x / scaleX, c1.y / scaleY, c1.z / scaleZ, 0);
    53.     rotationMatrix._12_22_32_42 = float4(c2.x / scaleX, c2.y / scaleY, c2.z / scaleZ, 0);
    54.     rotationMatrix._13_23_33_43 = float4(c3.x / scaleX, c3.y / scaleY, c3.z / scaleZ, 0);
    55.     rotationMatrix._14_24_34_44 = float4(0, 0, 0, 1);
    56.  
    57.  
    58.     float4x4 moveMatrix;
    59.     moveMatrix._11_21_31_41 = float4(1, 0, 0, m._41);
    60.     moveMatrix._12_22_32_42 = float4(0, 1, 0, m._42);
    61.     moveMatrix._13_23_33_43 = float4(0, 0, 1, m._43);
    62.     moveMatrix._14_24_34_44 = float4(0, 0, 0, 1);
    63.  
    64.  
    65.     float4x4 transformMatrix = mul(scaleMatrix, mul(rotationMatrix, moveMatrix));
    66.     return transformMatrix;// mul(rotationMatrix, moveMatrix);
    67. }