Transform.vert 3.5 KB

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