Transform.glsl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #ifdef COMPILEVS
  2. // Silence GLSL 150 deprecation warnings
  3. #ifdef GL3
  4. #define attribute in
  5. #define varying out
  6. #endif
  7. attribute vec4 iPos;
  8. attribute vec3 iNormal;
  9. attribute vec4 iColor;
  10. attribute vec2 iTexCoord;
  11. attribute vec2 iTexCoord2;
  12. attribute vec4 iTangent;
  13. attribute vec4 iBlendWeights;
  14. attribute vec4 iBlendIndices;
  15. attribute vec3 iCubeTexCoord;
  16. attribute vec4 iCubeTexCoord2;
  17. #ifndef GL_ES
  18. attribute vec4 iInstanceMatrix1;
  19. attribute vec4 iInstanceMatrix2;
  20. attribute vec4 iInstanceMatrix3;
  21. #endif
  22. #ifdef SKINNED
  23. mat4 GetSkinMatrix(vec4 blendWeights, vec4 blendIndices)
  24. {
  25. ivec4 idx = ivec4(blendIndices) * 3;
  26. const vec4 lastColumn = vec4(0.0, 0.0, 0.0, 1.0);
  27. return mat4(cSkinMatrices[idx.x], cSkinMatrices[idx.x + 1], cSkinMatrices[idx.x + 2], lastColumn) * blendWeights.x +
  28. mat4(cSkinMatrices[idx.y], cSkinMatrices[idx.y + 1], cSkinMatrices[idx.y + 2], lastColumn) * blendWeights.y +
  29. mat4(cSkinMatrices[idx.z], cSkinMatrices[idx.z + 1], cSkinMatrices[idx.z + 2], lastColumn) * blendWeights.z +
  30. mat4(cSkinMatrices[idx.w], cSkinMatrices[idx.w + 1], cSkinMatrices[idx.w + 2], lastColumn) * blendWeights.w;
  31. }
  32. #endif
  33. #ifdef INSTANCED
  34. mat4 GetInstanceMatrix()
  35. {
  36. const vec4 lastColumn = vec4(0.0, 0.0, 0.0, 1.0);
  37. return mat4(iInstanceMatrix1, iInstanceMatrix2, iInstanceMatrix3, lastColumn);
  38. }
  39. #endif
  40. mat3 GetNormalMatrix(mat4 modelMatrix)
  41. {
  42. return mat3(modelMatrix[0].xyz, modelMatrix[1].xyz, modelMatrix[2].xyz);
  43. }
  44. vec2 GetTexCoord(vec2 texCoord)
  45. {
  46. return vec2(dot(texCoord, cUOffset.xy) + cUOffset.w, dot(texCoord, cVOffset.xy) + cVOffset.w);
  47. }
  48. vec4 GetClipPos(vec3 worldPos)
  49. {
  50. vec4 ret = vec4(worldPos, 1.0) * cViewProj;
  51. // While getting the clip coordinate, also automatically set gl_ClipVertex for user clip planes
  52. #if !defined(GL_ES) && !defined(GL3)
  53. gl_ClipVertex = ret;
  54. #elif defined(GL3)
  55. gl_ClipDistance[0] = dot(cClipPlane, ret);
  56. #endif
  57. return ret;
  58. }
  59. float GetZonePos(vec3 worldPos)
  60. {
  61. return clamp((vec4(worldPos, 1.0) * cZone).z, 0.0, 1.0);
  62. }
  63. float GetDepth(vec4 clipPos)
  64. {
  65. return dot(clipPos.zw, cDepthMode.zw);
  66. }
  67. #ifdef BILLBOARD
  68. vec3 GetBillboardPos(vec4 iPos, vec2 iSize, mat4 modelMatrix)
  69. {
  70. return (iPos * modelMatrix).xyz + vec3(iSize.x, iSize.y, 0.0) * cBillboardRot;
  71. }
  72. vec3 GetBillboardNormal()
  73. {
  74. return vec3(-cBillboardRot[0][2], -cBillboardRot[1][2], -cBillboardRot[2][2]);
  75. }
  76. #endif
  77. #if defined(SKINNED)
  78. #define iModelMatrix GetSkinMatrix(iBlendWeights, iBlendIndices)
  79. #elif defined(INSTANCED)
  80. #define iModelMatrix GetInstanceMatrix();
  81. #else
  82. #define iModelMatrix cModel
  83. #endif
  84. vec3 GetWorldPos(mat4 modelMatrix)
  85. {
  86. #if defined(BILLBOARD)
  87. return GetBillboardPos(iPos, iTexCoord2, modelMatrix);
  88. #else
  89. return (iPos * modelMatrix).xyz;
  90. #endif
  91. }
  92. vec3 GetWorldNormal(mat4 modelMatrix)
  93. {
  94. #if defined(BILLBOARD)
  95. return GetBillboardNormal();
  96. #else
  97. return normalize(iNormal * GetNormalMatrix(modelMatrix));
  98. #endif
  99. }
  100. vec3 GetWorldTangent(mat4 modelMatrix)
  101. {
  102. return normalize(iTangent.xyz * GetNormalMatrix(modelMatrix));
  103. }
  104. #else
  105. // Silence GLSL 150 deprecation warnings
  106. #ifdef GL3
  107. #define varying in
  108. // \todo: should not hardcode the number of MRT outputs according to defines
  109. #if defined(DEFERRED)
  110. out vec4 fragData[4];
  111. #elif defined(PREPASS)
  112. out vec4 fragData[2];
  113. #else
  114. out vec4 fragData[1];
  115. #endif
  116. #define gl_FragColor fragData[0]
  117. #define gl_FragData fragData
  118. #endif
  119. #endif