Transform.glsl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifdef COMPILEVS
  2. attribute vec4 iPos;
  3. attribute vec3 iNormal;
  4. attribute vec4 iColor;
  5. attribute vec2 iTexCoord;
  6. attribute vec2 iTexCoord2;
  7. attribute vec4 iTangent;
  8. attribute vec4 iBlendWeights;
  9. attribute vec4 iBlendIndices;
  10. attribute vec3 iCubeTexCoord;
  11. attribute vec4 iCubeTexCoord2;
  12. #ifndef GL_ES
  13. attribute vec4 iInstanceMatrix1;
  14. attribute vec4 iInstanceMatrix2;
  15. attribute vec4 iInstanceMatrix3;
  16. #endif
  17. #ifdef SKINNED
  18. mat4 GetSkinMatrix(vec4 blendWeights, vec4 blendIndices)
  19. {
  20. ivec4 idx = ivec4(blendIndices) * 3;
  21. const vec4 lastColumn = vec4(0.0, 0.0, 0.0, 1.0);
  22. return mat4(cSkinMatrices[idx.x], cSkinMatrices[idx.x + 1], cSkinMatrices[idx.x + 2], lastColumn) * blendWeights.x +
  23. mat4(cSkinMatrices[idx.y], cSkinMatrices[idx.y + 1], cSkinMatrices[idx.y + 2], lastColumn) * blendWeights.y +
  24. mat4(cSkinMatrices[idx.z], cSkinMatrices[idx.z + 1], cSkinMatrices[idx.z + 2], lastColumn) * blendWeights.z +
  25. mat4(cSkinMatrices[idx.w], cSkinMatrices[idx.w + 1], cSkinMatrices[idx.w + 2], lastColumn) * blendWeights.w;
  26. }
  27. #endif
  28. #ifdef INSTANCED
  29. mat4 GetInstanceMatrix()
  30. {
  31. const vec4 lastColumn = vec4(0.0, 0.0, 0.0, 1.0);
  32. return mat4(iInstanceMatrix1, iInstanceMatrix2, iInstanceMatrix3, lastColumn);
  33. }
  34. #endif
  35. mat3 GetNormalMatrix(mat4 modelMatrix)
  36. {
  37. return mat3(modelMatrix[0].xyz, modelMatrix[1].xyz, modelMatrix[2].xyz);
  38. }
  39. vec2 GetTexCoord(vec2 texCoord)
  40. {
  41. return vec2(dot(texCoord, cUOffset.xy) + cUOffset.w, dot(texCoord, cVOffset.xy) + cVOffset.w);
  42. }
  43. vec4 GetClipPos(vec3 worldPos)
  44. {
  45. vec4 ret = cViewProj * vec4(worldPos, 1.0);
  46. // While getting the clip coordinate, also automatically set gl_ClipVertex for user clip planes
  47. #ifndef GL_ES
  48. gl_ClipVertex = ret;
  49. #endif
  50. return ret;
  51. }
  52. float GetZonePos(vec3 worldPos)
  53. {
  54. return clamp((cZone * vec4(worldPos, 1.0)).z, 0.0, 1.0);
  55. }
  56. float GetDepth(vec4 clipPos)
  57. {
  58. return dot(clipPos.zw, cDepthMode.zw);
  59. }
  60. vec3 GetBillboardPos(vec4 iPos, vec2 iSize, mat4 modelMatrix)
  61. {
  62. return (modelMatrix * iPos).xyz + cBillboardRot * vec3(iSize.x, iSize.y, 0.0);
  63. }
  64. vec3 GetBillboardNormal()
  65. {
  66. return vec3(-cBillboardRot[2][0], -cBillboardRot[2][1], -cBillboardRot[2][2]);
  67. }
  68. // Note: the skinning/instancing model matrix is a transpose, so the matrix multiply order must be swapped
  69. // (see GetWorldPos(), GetWorldNormal() and GetWorldTangent() below)
  70. #if defined(SKINNED)
  71. #define iModelMatrix GetSkinMatrix(iBlendWeights, iBlendIndices)
  72. #elif defined(INSTANCED)
  73. #define iModelMatrix GetInstanceMatrix();
  74. #else
  75. #define iModelMatrix cModel
  76. #endif
  77. vec3 GetWorldPos(mat4 modelMatrix)
  78. {
  79. #if defined(SKINNED) || defined(INSTANCED)
  80. return (iPos * modelMatrix).xyz;
  81. #elif defined(BILLBOARD)
  82. return GetBillboardPos(iPos, iTexCoord2, modelMatrix);
  83. #else
  84. return (modelMatrix * iPos).xyz;
  85. #endif
  86. }
  87. vec3 GetWorldNormal(mat4 modelMatrix)
  88. {
  89. #if defined(SKINNED) || defined(INSTANCED)
  90. return normalize(iNormal * GetNormalMatrix(modelMatrix));
  91. #elif defined(BILLBOARD)
  92. return GetBillboardNormal();
  93. #else
  94. return normalize(GetNormalMatrix(modelMatrix) * iNormal);
  95. #endif
  96. }
  97. vec3 GetWorldTangent(mat4 modelMatrix)
  98. {
  99. mat3 normalMatrix = GetNormalMatrix(modelMatrix);
  100. #if defined(SKINNED) || defined(INSTANCED)
  101. return normalize(iTangent.xyz * normalMatrix);
  102. #else
  103. return normalize(normalMatrix * iTangent.xyz);
  104. #endif
  105. }
  106. #endif