skinningDefine.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. export default
  2. /* glsl */
  3. `
  4. #ifdef WX_SKINNED
  5. attribute highp vec4 a_boneIndices;
  6. attribute highp vec4 a_weights;
  7. #ifdef WX_USE_BONE_MATRIX_TEXTURE
  8. // #ifndef WX_BONE_SAMPLER_HELPER
  9. // 请不要这样写,在部分安卓机上会出错,比如MTK、麒麟的设备很容易出现
  10. // #define WX_BONE_SAMPLER_HELPER(row, col) \
  11. // texture2D(u_boneMatrixTexture, vec2((col) / 4.0, (row) / float(WX_BONE_NUM)))
  12. // #endif
  13. uniform sampler2D u_boneMatrixTexture;
  14. mat4 sampleFromBoneMatrixTexture(float index) {
  15. float row = index + 0.5;
  16. float col = 0.5;
  17. return mat4(
  18. texture2D(u_boneMatrixTexture, vec2((col + .0) / 4.0, (row) / float(WX_BONE_NUM))),
  19. texture2D(u_boneMatrixTexture, vec2((col + 1.0) / 4.0, (row) / float(WX_BONE_NUM))),
  20. texture2D(u_boneMatrixTexture, vec2((col + 2.0) / 4.0, (row) / float(WX_BONE_NUM))),
  21. texture2D(u_boneMatrixTexture, vec2((col + 3.0) / 4.0, (row) / float(WX_BONE_NUM)))
  22. );
  23. }
  24. #else
  25. uniform mat4 u_boneOffsetMatrix[WX_BONE_NUM];
  26. #endif // WX_USE_BONE_MATRIX_TEXTURE
  27. #endif // WX_SKINNED
  28. #ifdef WX_SKINNED
  29. mat4 getBoneMat() {
  30. #ifdef WX_USE_BONE_MATRIX_TEXTURE
  31. mat4 boneMatX = sampleFromBoneMatrixTexture(a_boneIndices.x);
  32. mat4 boneMatY = sampleFromBoneMatrixTexture(a_boneIndices.y);
  33. mat4 boneMatZ = sampleFromBoneMatrixTexture(a_boneIndices.z);
  34. mat4 boneMatW = sampleFromBoneMatrixTexture(a_boneIndices.w);
  35. #else
  36. mat4 boneMatX = u_boneOffsetMatrix[int(a_boneIndices.x)];
  37. mat4 boneMatY = u_boneOffsetMatrix[int(a_boneIndices.y)];
  38. mat4 boneMatZ = u_boneOffsetMatrix[int(a_boneIndices.z)];
  39. mat4 boneMatW = u_boneOffsetMatrix[int(a_boneIndices.w)];
  40. #endif // WX_USE_BONE_MATRIX_TEXTURE
  41. mat4 boneMat = a_weights.x * boneMatX + a_weights.y * boneMatY + a_weights.z * boneMatZ + a_weights.w * boneMatW;
  42. return boneMat;
  43. }
  44. #endif // WX_SKINNED
  45. vec4 getSkinningWorldPosition(vec4 origin) {
  46. #ifdef WX_SKINNED
  47. return getBoneMat() * origin;
  48. #else
  49. return u_world * origin;
  50. #endif // WX_SKINNED
  51. }
  52. `;