skinning-animation-lbs.chunk 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. mat4 getJointMatrix (float i) {
  6. highp float x, y, invSize;
  7. CCGetJointTextureCoords(3.0, i, x, y, invSize);
  8. vec4 v1 = texture(cc_jointTexture, vec2((x + 0.5) * invSize, y));
  9. vec4 v2 = texture(cc_jointTexture, vec2((x + 1.5) * invSize, y));
  10. vec4 v3 = texture(cc_jointTexture, vec2((x + 2.5) * invSize, y));
  11. return mat4(vec4(v1.xyz, 0.0), vec4(v2.xyz, 0.0), vec4(v3.xyz, 0.0), vec4(v1.w, v2.w, v3.w, 1.0));
  12. }
  13. #else
  14. mat4 getJointMatrix (float i) {
  15. highp float x, y, invSize;
  16. CCGetJointTextureCoords(12.0, i, x, y, invSize);
  17. vec4 v1 = vec4(
  18. decode32(texture(cc_jointTexture, vec2((x + 0.5) * invSize, y))),
  19. decode32(texture(cc_jointTexture, vec2((x + 1.5) * invSize, y))),
  20. decode32(texture(cc_jointTexture, vec2((x + 2.5) * invSize, y))),
  21. decode32(texture(cc_jointTexture, vec2((x + 3.5) * invSize, y)))
  22. );
  23. vec4 v2 = vec4(
  24. decode32(texture(cc_jointTexture, vec2((x + 4.5) * invSize, y))),
  25. decode32(texture(cc_jointTexture, vec2((x + 5.5) * invSize, y))),
  26. decode32(texture(cc_jointTexture, vec2((x + 6.5) * invSize, y))),
  27. decode32(texture(cc_jointTexture, vec2((x + 7.5) * invSize, y)))
  28. );
  29. vec4 v3 = vec4(
  30. decode32(texture(cc_jointTexture, vec2((x + 8.5) * invSize, y))),
  31. decode32(texture(cc_jointTexture, vec2((x + 9.5) * invSize, y))),
  32. decode32(texture(cc_jointTexture, vec2((x + 10.5) * invSize, y))),
  33. decode32(texture(cc_jointTexture, vec2((x + 11.5) * invSize, y)))
  34. );
  35. return mat4(vec4(v1.xyz, 0.0), vec4(v2.xyz, 0.0), vec4(v3.xyz, 0.0), vec4(v1.w, v2.w, v3.w, 1.0));
  36. }
  37. #endif
  38. #else
  39. #if CC_USE_REAL_TIME_JOINT_TEXTURE
  40. #if CC_DEVICE_SUPPORT_FLOAT_TEXTURE
  41. mat4 getJointMatrix (float i) {
  42. float x = i;
  43. vec4 v1 = texture(cc_realtimeJoint, vec2( x / 256.0, 0.5 / 3.0));
  44. vec4 v2 = texture(cc_realtimeJoint, vec2( x / 256.0, 1.5 / 3.0));
  45. vec4 v3 = texture(cc_realtimeJoint, vec2( x / 256.0, 2.5 / 3.0));
  46. return mat4(vec4(v1.xyz, 0.0), vec4(v2.xyz, 0.0), vec4(v3.xyz, 0.0), vec4(v1.w, v2.w, v3.w, 1.0));
  47. }
  48. #else
  49. mat4 getJointMatrix (float i) {
  50. float x = 4.0 * i;
  51. vec4 v1 = vec4(
  52. decode32(texture(cc_realtimeJoint, vec2((x + 0.5)/ 1024.0, 0.5 / 3.0))),
  53. decode32(texture(cc_realtimeJoint, vec2((x + 1.5)/ 1024.0, 0.5 / 3.0))),
  54. decode32(texture(cc_realtimeJoint, vec2((x + 2.5)/ 1024.0, 0.5 / 3.0))),
  55. decode32(texture(cc_realtimeJoint, vec2((x + 3.5)/ 1024.0, 0.5 / 3.0)))
  56. );
  57. vec4 v2 = vec4(
  58. decode32(texture(cc_realtimeJoint, vec2((x + 0.5)/ 1024.0, 1.5 / 3.0))),
  59. decode32(texture(cc_realtimeJoint, vec2((x + 1.5)/ 1024.0, 1.5 / 3.0))),
  60. decode32(texture(cc_realtimeJoint, vec2((x + 2.5)/ 1024.0, 1.5 / 3.0))),
  61. decode32(texture(cc_realtimeJoint, vec2((x + 3.5)/ 1024.0, 1.5 / 3.0)))
  62. );
  63. vec4 v3 = vec4(
  64. decode32(texture(cc_realtimeJoint, vec2((x + 0.5)/ 1024.0, 2.5 / 3.0))),
  65. decode32(texture(cc_realtimeJoint, vec2((x + 1.5)/ 1024.0, 2.5 / 3.0))),
  66. decode32(texture(cc_realtimeJoint, vec2((x + 2.5)/ 1024.0, 2.5 / 3.0))),
  67. decode32(texture(cc_realtimeJoint, vec2((x + 3.5)/ 1024.0, 2.5 / 3.0)))
  68. );
  69. return mat4(vec4(v1.xyz, 0.0), vec4(v2.xyz, 0.0), vec4(v3.xyz, 0.0), vec4(v1.w, v2.w, v3.w, 1.0));
  70. }
  71. #endif
  72. #else
  73. mat4 getJointMatrix (float i) {
  74. int idx = int(i);
  75. vec4 v1 = cc_joints[idx * 3];
  76. vec4 v2 = cc_joints[idx * 3 + 1];
  77. vec4 v3 = cc_joints[idx * 3 + 2];
  78. return mat4(vec4(v1.xyz, 0.0), vec4(v2.xyz, 0.0), vec4(v3.xyz, 0.0), vec4(v1.w, v2.w, v3.w, 1.0));
  79. }
  80. #endif
  81. #endif
  82. mat4 skinMatrix () {
  83. vec4 joints = vec4(a_joints); //simple conversion of a_joints from unsigned integer to float to match function parameter type
  84. return getJointMatrix(joints.x) * a_weights.x
  85. + getJointMatrix(joints.y) * a_weights.y
  86. + getJointMatrix(joints.z) * a_weights.z
  87. + getJointMatrix(joints.w) * a_weights.w;
  88. }
  89. void CCSkin (inout vec4 position) {
  90. mat4 m = skinMatrix();
  91. position = m * position;
  92. }
  93. void CCSkin (inout vec4 position, inout vec3 normal, inout vec4 tangent) {
  94. mat4 m = skinMatrix();
  95. position = m * position;
  96. normal = (m * vec4(normal, 0.0)).xyz;
  97. tangent.xyz = (m * vec4(tangent.xyz, 0.0)).xyz;
  98. }