skinning-animation-dqs.chunk 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
  2. #include <builtin/uniforms/cc-skinning>
  3. #if CC_USE_BAKED_ANIMATION
  4. #if CC_DEVICE_SUPPORT_FLOAT_TEXTURE
  5. void getJointDQ (float i, out vec4 Qr, out vec4 Qt, out vec3 S) {
  6. highp float x, y, invSize;
  7. CCGetJointTextureCoords(3.0, i, x, y, invSize);
  8. Qr = texture(cc_jointTexture, vec2((x + 0.5) * invSize, y));
  9. Qt = texture(cc_jointTexture, vec2((x + 1.5) * invSize, y));
  10. S = texture(cc_jointTexture, vec2((x + 2.5) * invSize, y)).xyz;
  11. }
  12. #else
  13. void getJointDQ (float i, out vec4 Qr, out vec4 Qt, out vec3 S) {
  14. highp float x, y, invSize;
  15. CCGetJointTextureCoords(12.0, i, x, y, invSize);
  16. Qr = vec4(
  17. decode32(texture(cc_jointTexture, vec2((x + 0.5) * invSize, y))),
  18. decode32(texture(cc_jointTexture, vec2((x + 1.5) * invSize, y))),
  19. decode32(texture(cc_jointTexture, vec2((x + 2.5) * invSize, y))),
  20. decode32(texture(cc_jointTexture, vec2((x + 3.5) * invSize, y)))
  21. );
  22. Qt = vec4(
  23. decode32(texture(cc_jointTexture, vec2((x + 4.5) * invSize, y))),
  24. decode32(texture(cc_jointTexture, vec2((x + 5.5) * invSize, y))),
  25. decode32(texture(cc_jointTexture, vec2((x + 6.5) * invSize, y))),
  26. decode32(texture(cc_jointTexture, vec2((x + 7.5) * invSize, y)))
  27. );
  28. S = vec3(
  29. decode32(texture(cc_jointTexture, vec2((x + 8.5) * invSize, y))),
  30. decode32(texture(cc_jointTexture, vec2((x + 9.5) * invSize, y))),
  31. decode32(texture(cc_jointTexture, vec2((x + 10.5) * invSize, y)))
  32. );
  33. }
  34. #endif
  35. #else
  36. #if CC_USE_REAL_TIME_JOINT_TEXTURE
  37. #if CC_DEVICE_SUPPORT_FLOAT_TEXTURE
  38. void getJointDQ (float i, out vec4 Qr, out vec4 Qt, out vec3 S) {
  39. float x = i;
  40. Qr = texture(cc_realtimeJoint, vec2( x / 256.0, 0.5 / 3.0));
  41. Qt = texture(cc_realtimeJoint, vec2( x / 256.0, 1.5 / 3.0));
  42. S = texture(cc_realtimeJoint, vec2( x / 256.0, 2.5 / 3.0)).xyz;
  43. }
  44. #else
  45. void getJointDQ (float i, out vec4 Qr, out vec4 Qt, out vec3 S) {
  46. float x = 4.0 * i;
  47. vec4 v1 = vec4(
  48. decode32(texture(cc_realtimeJoint, vec2((x + 0.5)/ 1024.0, 0.5 / 3.0))),
  49. decode32(texture(cc_realtimeJoint, vec2((x + 1.5)/ 1024.0, 0.5 / 3.0))),
  50. decode32(texture(cc_realtimeJoint, vec2((x + 2.5)/ 1024.0, 0.5 / 3.0))),
  51. decode32(texture(cc_realtimeJoint, vec2((x + 3.5)/ 1024.0, 0.5 / 3.0)))
  52. );
  53. vec4 v2 = vec4(
  54. decode32(texture(cc_realtimeJoint, vec2((x + 0.5)/ 1024.0, 1.5 / 3.0))),
  55. decode32(texture(cc_realtimeJoint, vec2((x + 1.5)/ 1024.0, 1.5 / 3.0))),
  56. decode32(texture(cc_realtimeJoint, vec2((x + 2.5)/ 1024.0, 1.5 / 3.0))),
  57. decode32(texture(cc_realtimeJoint, vec2((x + 3.5)/ 1024.0, 1.5 / 3.0)))
  58. );
  59. vec4 v3 = vec4(
  60. decode32(texture(cc_realtimeJoint, vec2((x + 0.5)/ 1024.0, 2.5 / 3.0))),
  61. decode32(texture(cc_realtimeJoint, vec2((x + 1.5)/ 1024.0, 2.5 / 3.0))),
  62. decode32(texture(cc_realtimeJoint, vec2((x + 2.5)/ 1024.0, 2.5 / 3.0))),
  63. decode32(texture(cc_realtimeJoint, vec2((x + 3.5)/ 1024.0, 2.5 / 3.0)))
  64. );
  65. Qr = v1;
  66. Qt = v2;
  67. S = v3.xyz;
  68. }
  69. #endif
  70. #else
  71. void getJointDQ (float i, out vec4 Qr, out vec4 Qt, out vec3 S) {
  72. int idx = int(i);
  73. Qr = cc_joints[idx * 3];
  74. Qt = cc_joints[idx * 3 + 1];
  75. S = cc_joints[idx * 3 + 2].xyz;
  76. }
  77. #endif
  78. #endif
  79. // [Kavan07] Skinning with Dual Quaternions
  80. void skinRTS (out vec4 R, out vec3 T, out vec3 S) {
  81. vec4 r, t, Qt = vec4(0.0); vec3 s;
  82. R = vec4(0.0); S = vec3(0.0);
  83. for (int i = 0; i < 4; i++) {
  84. float w = a_weights[i];
  85. getJointDQ(a_joints[i], r, t, s);
  86. S += s * w; R += r * w; Qt += t * w;
  87. }
  88. float invNorm = 1.0 / length(R); R *= invNorm; Qt *= invNorm;
  89. T = 2.0 * (R.w * Qt.xyz - Qt.w * R.xyz + cross(R.xyz, Qt.xyz)); // 2 * Qt * conjugate(R)
  90. }
  91. vec3 VectorTransformQuat (vec3 v, vec4 Q) {
  92. return v + 2.0 * cross(Q.xyz, cross(Q.xyz, v) + Q.w * v);
  93. }
  94. void CCSkin (inout vec4 position) {
  95. vec4 R; vec3 T, S;
  96. skinRTS(R, T, S);
  97. position.xyz = VectorTransformQuat(position.xyz * S, R) + T;
  98. }
  99. void CCSkin (inout vec4 position, inout vec3 normal, inout vec4 tangent) {
  100. vec4 R; vec3 T, S;
  101. skinRTS(R, T, S);
  102. position.xyz = VectorTransformQuat(position.xyz * S, R) + T;
  103. normal = VectorTransformQuat(normal, R);
  104. tangent.xyz = VectorTransformQuat(tangent.xyz, R);
  105. }