GPUSkinning.vert 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #define MAX_JOINT_COUNT 64
  2. uniform mat4 skeletonMatrix[MAX_JOINT_COUNT];
  3. attribute vec4 vBoneIndices;
  4. attribute vec4 vBoneWeights;
  5. varying vec3 normal;
  6. varying vec4 pos;
  7. varying vec4 rawpos;
  8. varying vec4 vertexColor;
  9. mat3 m3( mat4 m )
  10. {
  11. mat3 result;
  12. result[0][0] = m[0][0];
  13. result[0][1] = m[0][1];
  14. result[0][2] = m[0][2];
  15. result[1][0] = m[1][0];
  16. result[1][1] = m[1][1];
  17. result[1][2] = m[1][2];
  18. result[2][0] = m[2][0];
  19. result[2][1] = m[2][1];
  20. result[2][2] = m[2][2];
  21. return result;
  22. }
  23. void jointInfluence(in mat4 joint_matrix, in float weight, in vec4 position, inout vec4 outPosition, in vec3 normal, inout vec3 outNormal)
  24. {
  25. outPosition += weight * (joint_matrix * position);
  26. mat3 normalMatrix = m3(joint_matrix);
  27. outNormal += weight * (normalMatrix * normal);
  28. }
  29. void main() {
  30. vec4 inVert = gl_Vertex;
  31. vec4 outVert = vec4(0.0, 0.0, 0.0, 0.0);
  32. vec3 inNormal = gl_Normal;
  33. vec3 outNormal = vec3(0.0, 0.0, 0.0);
  34. jointInfluence(skeletonMatrix[int(vBoneIndices.x)], vBoneWeights.x, inVert, outVert, inNormal, outNormal);
  35. jointInfluence(skeletonMatrix[int(vBoneIndices.y)], vBoneWeights.y, inVert, outVert, inNormal, outNormal);
  36. jointInfluence(skeletonMatrix[int(vBoneIndices.z)], vBoneWeights.z, inVert, outVert, inNormal, outNormal);
  37. jointInfluence(skeletonMatrix[int(vBoneIndices.w)], vBoneWeights.w, inVert, outVert, inNormal, outNormal);
  38. outVert.w = 1.0;
  39. normal = gl_NormalMatrix * normalize(outNormal);
  40. gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * outVert;
  41. pos = gl_ModelViewMatrix * outVert;
  42. rawpos = outVert;
  43. vertexColor = gl_Color;
  44. gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
  45. }