Transform.vert 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. attribute vec4 iPos;
  2. attribute vec3 iNormal;
  3. attribute vec4 iColor;
  4. attribute vec2 iTexCoord;
  5. attribute vec2 iTexCoord2;
  6. attribute vec3 iCubeTexCoord;
  7. attribute vec4 iCubeTexCoord2;
  8. attribute vec4 iTangent;
  9. attribute vec4 iBlendWeights;
  10. attribute vec4 iBlendIndices;
  11. mat4 GetSkinMatrix(vec4 blendWeights, vec4 blendIndices)
  12. {
  13. ivec4 idx = ivec4(blendIndices) * 3;
  14. const vec4 lastColumn = vec4(0.0, 0.0, 0.0, 1.0);
  15. return mat4(cSkinMatrices[idx.x], cSkinMatrices[idx.x + 1], cSkinMatrices[idx.x + 2], lastColumn) * blendWeights.x +
  16. mat4(cSkinMatrices[idx.y], cSkinMatrices[idx.y + 1], cSkinMatrices[idx.y + 2], lastColumn) * blendWeights.y +
  17. mat4(cSkinMatrices[idx.z], cSkinMatrices[idx.z + 1], cSkinMatrices[idx.z + 2], lastColumn) * blendWeights.z +
  18. mat4(cSkinMatrices[idx.w], cSkinMatrices[idx.w + 1], cSkinMatrices[idx.w + 2], lastColumn) * blendWeights.w;
  19. }
  20. mat3 GetNormalMatrix(mat4 modelMatrix)
  21. {
  22. return mat3(modelMatrix[0].xyz, modelMatrix[1].xyz, modelMatrix[2].xyz);
  23. }
  24. vec2 GetTexCoord(vec2 texCoord)
  25. {
  26. return vec2(dot(texCoord, cUOffset.xy) + cUOffset.w, dot(texCoord, cVOffset.xy) + cVOffset.w);
  27. }
  28. vec4 GetClipPos(vec3 worldPos)
  29. {
  30. return cViewProj * vec4(worldPos, 1.0);
  31. }
  32. float GetZonePos(vec3 worldPos)
  33. {
  34. return clamp((cZone * vec4(worldPos, 1.0)).z, 0.0, 1.0);
  35. }
  36. float GetDepth(vec4 clipPos)
  37. {
  38. return dot(clipPos.zw, cDepthMode.zw);
  39. }
  40. vec3 GetBillboardPos(vec4 iPos, vec2 iSize)
  41. {
  42. return vec3(iPos.xyz + iSize.x * cViewRightVector + iSize.y * cViewUpVector);
  43. }
  44. vec3 GetBillboardNormal()
  45. {
  46. return vec3(-cCameraRot[2][0], -cCameraRot[2][1], -cCameraRot[2][2]);
  47. }
  48. #ifdef SKINNED
  49. #define iModelMatrix GetSkinMatrix(iBlendWeights, iBlendIndices)
  50. #else
  51. #define iModelMatrix cModel
  52. #endif
  53. vec3 GetWorldPos(mat4 modelMatrix)
  54. {
  55. #if defined(BILLBOARD)
  56. return GetBillboardPos(iPos, iTexCoord2);
  57. #elif defined(SKINNED)
  58. return (iPos * modelMatrix).xyz;
  59. #else
  60. return (modelMatrix * iPos).xyz;
  61. #endif
  62. }
  63. vec3 GetWorldNormal(mat4 modelMatrix)
  64. {
  65. #if defined(BILLBOARD)
  66. return GetBillboardNormal();
  67. #elif defined(SKINNED)
  68. return normalize(iNormal * GetNormalMatrix(modelMatrix));
  69. #else
  70. return normalize(GetNormalMatrix(modelMatrix) * iNormal);
  71. #endif
  72. }
  73. vec3 GetWorldTangent(mat4 modelMatrix)
  74. {
  75. mat3 normalMatrix = GetNormalMatrix(modelMatrix);
  76. #ifdef SKINNED
  77. return normalize(iTangent.xyz * normalMatrix);
  78. #else
  79. return normalize(normalMatrix * iTangent.xyz);
  80. #endif
  81. }