skinning.vs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #version 100
  2. #define MAX_BONE_NUM 64
  3. // Input vertex attributes
  4. attribute vec3 vertexPosition;
  5. attribute vec2 vertexTexCoord;
  6. attribute vec4 vertexColor;
  7. attribute vec4 vertexBoneIds;
  8. attribute vec4 vertexBoneWeights;
  9. // Input uniform values
  10. uniform mat4 mvp;
  11. uniform mat4 boneMatrices[MAX_BONE_NUM];
  12. // Output vertex attributes (to fragment shader)
  13. varying vec2 fragTexCoord;
  14. varying vec4 fragColor;
  15. void main()
  16. {
  17. int boneIndex0 = int(vertexBoneIds.x);
  18. int boneIndex1 = int(vertexBoneIds.y);
  19. int boneIndex2 = int(vertexBoneIds.z);
  20. int boneIndex3 = int(vertexBoneIds.w);
  21. // WARNING: OpenGL ES 2.0 does not support automatic matrix transposing, neither transpose() function
  22. mat4 boneMatrixTransposed0 = mat4(
  23. vec4(boneMatrices[boneIndex0][0].x, boneMatrices[boneIndex0][1].x, boneMatrices[boneIndex0][2].x, boneMatrices[boneIndex0][3].x),
  24. vec4(boneMatrices[boneIndex0][0].y, boneMatrices[boneIndex0][1].y, boneMatrices[boneIndex0][2].y, boneMatrices[boneIndex0][3].y),
  25. vec4(boneMatrices[boneIndex0][0].z, boneMatrices[boneIndex0][1].z, boneMatrices[boneIndex0][2].z, boneMatrices[boneIndex0][3].z),
  26. vec4(boneMatrices[boneIndex0][0].w, boneMatrices[boneIndex0][1].w, boneMatrices[boneIndex0][2].w, boneMatrices[boneIndex0][3].w));
  27. mat4 boneMatrixTransposed1 = mat4(
  28. vec4(boneMatrices[boneIndex1][0].x, boneMatrices[boneIndex1][1].x, boneMatrices[boneIndex1][2].x, boneMatrices[boneIndex1][3].x),
  29. vec4(boneMatrices[boneIndex1][0].y, boneMatrices[boneIndex1][1].y, boneMatrices[boneIndex1][2].y, boneMatrices[boneIndex1][3].y),
  30. vec4(boneMatrices[boneIndex1][0].z, boneMatrices[boneIndex1][1].z, boneMatrices[boneIndex1][2].z, boneMatrices[boneIndex1][3].z),
  31. vec4(boneMatrices[boneIndex1][0].w, boneMatrices[boneIndex1][1].w, boneMatrices[boneIndex1][2].w, boneMatrices[boneIndex1][3].w));
  32. mat4 boneMatrixTransposed2 = mat4(
  33. vec4(boneMatrices[boneIndex2][0].x, boneMatrices[boneIndex2][1].x, boneMatrices[boneIndex2][2].x, boneMatrices[boneIndex2][3].x),
  34. vec4(boneMatrices[boneIndex2][0].y, boneMatrices[boneIndex2][1].y, boneMatrices[boneIndex2][2].y, boneMatrices[boneIndex2][3].y),
  35. vec4(boneMatrices[boneIndex2][0].z, boneMatrices[boneIndex2][1].z, boneMatrices[boneIndex2][2].z, boneMatrices[boneIndex2][3].z),
  36. vec4(boneMatrices[boneIndex2][0].w, boneMatrices[boneIndex2][1].w, boneMatrices[boneIndex2][2].w, boneMatrices[boneIndex2][3].w));
  37. mat4 boneMatrixTransposed3 = mat4(
  38. vec4(boneMatrices[boneIndex3][0].x, boneMatrices[boneIndex3][1].x, boneMatrices[boneIndex3][2].x, boneMatrices[boneIndex3][3].x),
  39. vec4(boneMatrices[boneIndex3][0].y, boneMatrices[boneIndex3][1].y, boneMatrices[boneIndex3][2].y, boneMatrices[boneIndex3][3].y),
  40. vec4(boneMatrices[boneIndex3][0].z, boneMatrices[boneIndex3][1].z, boneMatrices[boneIndex3][2].z, boneMatrices[boneIndex3][3].z),
  41. vec4(boneMatrices[boneIndex3][0].w, boneMatrices[boneIndex3][1].w, boneMatrices[boneIndex3][2].w, boneMatrices[boneIndex3][3].w));
  42. vec4 skinnedPosition =
  43. vertexBoneWeights.x*(boneMatrixTransposed0*vec4(vertexPosition, 1.0)) +
  44. vertexBoneWeights.y*(boneMatrixTransposed1*vec4(vertexPosition, 1.0)) +
  45. vertexBoneWeights.z*(boneMatrixTransposed2*vec4(vertexPosition, 1.0)) +
  46. vertexBoneWeights.w*(boneMatrixTransposed3*vec4(vertexPosition, 1.0));
  47. fragTexCoord = vertexTexCoord;
  48. fragColor = vertexColor;
  49. gl_Position = mvp*skinnedPosition;
  50. }