GpuEcsAnimator.cginc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //UNITY_SHADER_NO_UPGRADE
  2. #ifndef MYHLSLINCLUDE_INCLUDED
  3. #define MYHLSLINCLUDE_INCLUDED
  4. half3 doTransform(half4x4 transform, half3 pt)
  5. {
  6. return mul(transform, half4(pt.x, pt.y, pt.z, 1)).xyz;
  7. }
  8. half4 loadBoneMatrixTexture(Texture2D<float4> animatedBoneMatrices, int frameIndex, int boneIndex, int i)
  9. {
  10. return animatedBoneMatrices.Load(int3((boneIndex * 3) + i, frameIndex, 0));
  11. }
  12. half4x4 extractRotationMatrix(half4x4 m)
  13. {
  14. half sx = length(half3(m[0][0], m[0][1], m[0][2]));
  15. half sy = length(half3(m[1][0], m[1][1], m[1][2]));
  16. half sz = length(half3(m[2][0], m[2][1], m[2][2]));
  17. // if determine is negative, we need to invert one scale
  18. half det = determinant(m);
  19. if (det < 0) {
  20. sx = -sx;
  21. }
  22. half invSX = 1.0 / sx;
  23. half invSY = 1.0 / sy;
  24. half invSZ = 1.0 / sz;
  25. m[0][0] *= invSX;
  26. m[0][1] *= invSX;
  27. m[0][2] *= invSX;
  28. m[0][3] = 0;
  29. m[1][0] *= invSY;
  30. m[1][1] *= invSY;
  31. m[1][2] *= invSY;
  32. m[1][3] = 0;
  33. m[2][0] *= invSZ;
  34. m[2][1] *= invSZ;
  35. m[2][2] *= invSZ;
  36. m[2][3] = 0;
  37. m[3][0] = 0;
  38. m[3][1] = 0;
  39. m[3][2] = 0;
  40. m[3][3] = 1;
  41. return m;
  42. }
  43. void calculateFrameValues(half3 position, half3 normal, half3 tangent,
  44. Texture2D<float4> animatedBoneMatrices,
  45. half2 boneWeights[6], int frameIndex,
  46. out half3 positionOut, out half3 normalOut, out half3 tangentOut)
  47. {
  48. positionOut = normalOut = tangentOut = half3(0, 0, 0);
  49. for(int i = 0; i < 6; i++)
  50. {
  51. half boneWeight = boneWeights[i].y;
  52. if(boneWeight != 0)
  53. {
  54. half boneIndex = boneWeights[i].x;
  55. half4 m0 = loadBoneMatrixTexture(animatedBoneMatrices, frameIndex, boneIndex, 0);
  56. half4 m1 = loadBoneMatrixTexture(animatedBoneMatrices, frameIndex, boneIndex, 1);
  57. half4 m2 = loadBoneMatrixTexture(animatedBoneMatrices, frameIndex, boneIndex, 2);
  58. half4 m3 = half4(0,0,0,1);
  59. half4x4 animatedBoneMatrix = half4x4(m0, m1, m2, m3);
  60. half4x4 rotationMatrix = extractRotationMatrix(animatedBoneMatrix);
  61. positionOut += boneWeight * doTransform(animatedBoneMatrix, position);
  62. normalOut += boneWeight * doTransform(rotationMatrix, normal);
  63. tangentOut += boneWeight * doTransform(rotationMatrix, tangent);
  64. }
  65. }
  66. }
  67. void AnimateBlend_half(half3 position, half3 normal, half3 tangent,
  68. half3x4 uvs, Texture2D<float4> animatedBoneMatrices, half4x3 animationState,
  69. out half3 positionOut, out half3 normalOut, out half3 tangentOut)
  70. {
  71. positionOut = half3(0, 0, 0);
  72. normalOut = half3(0, 0, 0);
  73. tangentOut = half3(0, 0, 0);
  74. half2 boneWeights[6] = {
  75. half2(uvs._m00, uvs._m01), half2(uvs._m02, uvs._m03),
  76. half2(uvs._m10, uvs._m11), half2(uvs._m12, uvs._m13),
  77. half2(uvs._m20, uvs._m21), half2(uvs._m22, uvs._m23) };
  78. for(int blendIndex = 0; blendIndex < 4; blendIndex++)
  79. {
  80. half blendFactor = animationState[blendIndex][0];
  81. if(blendFactor > 0)
  82. {
  83. half transitionNextFrame = animationState[blendIndex][1];
  84. half prevFrameFrac = 1.0 - transitionNextFrame;
  85. half frameIndex = animationState[blendIndex][2];
  86. half3 posOutBefore, posOutAfter, normalOutBefore, normalOutAfter, tangentOutBefore, tangentOutAfter;
  87. calculateFrameValues(position, normal, tangent, animatedBoneMatrices, boneWeights, frameIndex,
  88. posOutBefore, normalOutBefore, tangentOutBefore);
  89. calculateFrameValues(position, normal, tangent, animatedBoneMatrices, boneWeights, frameIndex + 1,
  90. posOutAfter, normalOutAfter, tangentOutAfter);
  91. positionOut += blendFactor * (prevFrameFrac * posOutBefore + transitionNextFrame * posOutAfter);
  92. normalOut += blendFactor * (prevFrameFrac * normalOutBefore + transitionNextFrame * normalOutAfter);
  93. tangentOut += blendFactor * (prevFrameFrac * tangentOutBefore + transitionNextFrame * tangentOutAfter);
  94. }
  95. }
  96. }
  97. #endif //MYHLSLINCLUDE_INCLUDED