Transform.glsl 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #ifdef COMPILEVS
  2. // Silence WARNING: Shader ... does not use the define NOUV
  3. #ifdef NOUV
  4. #endif
  5. // Silence GLSL 150 deprecation warnings
  6. #ifdef GL3
  7. #define attribute in
  8. #define varying out
  9. #endif
  10. attribute vec4 iPos;
  11. attribute vec3 iNormal;
  12. attribute vec4 iColor;
  13. attribute vec2 iTexCoord;
  14. attribute vec2 iTexCoord1;
  15. attribute vec4 iTangent;
  16. attribute vec4 iBlendWeights;
  17. attribute vec4 iBlendIndices;
  18. attribute vec3 iCubeTexCoord;
  19. attribute vec4 iCubeTexCoord1;
  20. #ifdef INSTANCED
  21. attribute vec4 iTexCoord4;
  22. attribute vec4 iTexCoord5;
  23. attribute vec4 iTexCoord6;
  24. #endif
  25. attribute float iObjectIndex;
  26. #ifdef SKINNED
  27. mat4 GetSkinMatrix(vec4 blendWeights, vec4 blendIndices)
  28. {
  29. ivec4 idx = ivec4(blendIndices) * 3;
  30. const vec4 lastColumn = vec4(0.0, 0.0, 0.0, 1.0);
  31. return mat4(cSkinMatrices[idx.x], cSkinMatrices[idx.x + 1], cSkinMatrices[idx.x + 2], lastColumn) * blendWeights.x +
  32. mat4(cSkinMatrices[idx.y], cSkinMatrices[idx.y + 1], cSkinMatrices[idx.y + 2], lastColumn) * blendWeights.y +
  33. mat4(cSkinMatrices[idx.z], cSkinMatrices[idx.z + 1], cSkinMatrices[idx.z + 2], lastColumn) * blendWeights.z +
  34. mat4(cSkinMatrices[idx.w], cSkinMatrices[idx.w + 1], cSkinMatrices[idx.w + 2], lastColumn) * blendWeights.w;
  35. }
  36. #endif
  37. #ifdef INSTANCED
  38. mat4 GetInstanceMatrix()
  39. {
  40. const vec4 lastColumn = vec4(0.0, 0.0, 0.0, 1.0);
  41. return mat4(iTexCoord4, iTexCoord5, iTexCoord6, lastColumn);
  42. }
  43. #endif
  44. mat3 GetNormalMatrix(mat4 modelMatrix)
  45. {
  46. return mat3(modelMatrix[0].xyz, modelMatrix[1].xyz, modelMatrix[2].xyz);
  47. }
  48. vec2 GetTexCoord(vec2 texCoord)
  49. {
  50. return vec2(dot(texCoord, cUOffset.xy) + cUOffset.w, dot(texCoord, cVOffset.xy) + cVOffset.w);
  51. }
  52. vec4 GetClipPos(vec3 worldPos)
  53. {
  54. vec4 ret = vec4(worldPos, 1.0) * cViewProj;
  55. // While getting the clip coordinate, also automatically set gl_ClipVertex for user clip planes
  56. #if !defined(GL_ES) && !defined(GL3)
  57. gl_ClipVertex = ret;
  58. #elif defined(GL3)
  59. gl_ClipDistance[0] = dot(cClipPlane, ret);
  60. #endif
  61. return ret;
  62. }
  63. float GetZonePos(vec3 worldPos)
  64. {
  65. return clamp((vec4(worldPos, 1.0) * cZone).z, 0.0, 1.0);
  66. }
  67. float GetDepth(vec4 clipPos)
  68. {
  69. return dot(clipPos.zw, cDepthMode.zw);
  70. }
  71. #ifdef BILLBOARD
  72. vec3 GetBillboardPos(vec4 iPos, vec2 iSize, mat4 modelMatrix)
  73. {
  74. return (iPos * modelMatrix).xyz + vec3(iSize.x, iSize.y, 0.0) * cBillboardRot;
  75. }
  76. vec3 GetBillboardNormal()
  77. {
  78. return vec3(-cBillboardRot[0][2], -cBillboardRot[1][2], -cBillboardRot[2][2]);
  79. }
  80. #endif
  81. #ifdef DIRBILLBOARD
  82. mat3 GetFaceCameraRotation(vec3 position, vec3 direction)
  83. {
  84. vec3 cameraDir = normalize(position - cCameraPos);
  85. vec3 front = normalize(direction);
  86. vec3 right = normalize(cross(front, cameraDir));
  87. vec3 up = normalize(cross(front, right));
  88. return mat3(
  89. right.x, up.x, front.x,
  90. right.y, up.y, front.y,
  91. right.z, up.z, front.z
  92. );
  93. }
  94. vec3 GetBillboardPos(vec4 iPos, vec3 iDirection, mat4 modelMatrix)
  95. {
  96. vec3 worldPos = (iPos * modelMatrix).xyz;
  97. return worldPos + vec3(iTexCoord1.x, 0.0, iTexCoord1.y) * GetFaceCameraRotation(worldPos, iDirection);
  98. }
  99. vec3 GetBillboardNormal(vec4 iPos, vec3 iDirection, mat4 modelMatrix)
  100. {
  101. vec3 worldPos = (iPos * modelMatrix).xyz;
  102. return vec3(0.0, 1.0, 0.0) * GetFaceCameraRotation(worldPos, iDirection);
  103. }
  104. #endif
  105. #ifdef TRAILFACECAM
  106. vec3 GetTrailPos(vec4 iPos, vec3 iFront, float iScale, mat4 modelMatrix)
  107. {
  108. vec3 up = normalize(cCameraPos - iPos.xyz);
  109. vec3 right = normalize(cross(iFront, up));
  110. return (vec4((iPos.xyz + right * iScale), 1.0) * modelMatrix).xyz;
  111. }
  112. vec3 GetTrailNormal(vec4 iPos)
  113. {
  114. return normalize(cCameraPos - iPos.xyz);
  115. }
  116. #endif
  117. #ifdef TRAILBONE
  118. vec3 GetTrailPos(vec4 iPos, vec3 iParentPos, float iScale, mat4 modelMatrix)
  119. {
  120. vec3 right = iParentPos - iPos.xyz;
  121. return (vec4((iPos.xyz + right * iScale), 1.0) * modelMatrix).xyz;
  122. }
  123. vec3 GetTrailNormal(vec4 iPos, vec3 iParentPos, vec3 iForward)
  124. {
  125. vec3 left = normalize(iPos.xyz - iParentPos);
  126. vec3 up = normalize(cross(normalize(iForward), left));
  127. return up;
  128. }
  129. #endif
  130. #if defined(SKINNED)
  131. #define iModelMatrix GetSkinMatrix(iBlendWeights, iBlendIndices)
  132. #elif defined(INSTANCED)
  133. #define iModelMatrix GetInstanceMatrix()
  134. #else
  135. #define iModelMatrix cModel
  136. #endif
  137. vec3 GetWorldPos(mat4 modelMatrix)
  138. {
  139. #if defined(BILLBOARD)
  140. return GetBillboardPos(iPos, iTexCoord1, modelMatrix);
  141. #elif defined(DIRBILLBOARD)
  142. return GetBillboardPos(iPos, iNormal, modelMatrix);
  143. #elif defined(TRAILFACECAM)
  144. return GetTrailPos(iPos, iTangent.xyz, iTangent.w, modelMatrix);
  145. #elif defined(TRAILBONE)
  146. return GetTrailPos(iPos, iTangent.xyz, iTangent.w, modelMatrix);
  147. #else
  148. return (iPos * modelMatrix).xyz;
  149. #endif
  150. }
  151. vec3 GetWorldNormal(mat4 modelMatrix)
  152. {
  153. #if defined(BILLBOARD)
  154. return GetBillboardNormal();
  155. #elif defined(DIRBILLBOARD)
  156. return GetBillboardNormal(iPos, iNormal, modelMatrix);
  157. #elif defined(TRAILFACECAM)
  158. return GetTrailNormal(iPos);
  159. #elif defined(TRAILBONE)
  160. return GetTrailNormal(iPos, iTangent.xyz, iNormal);
  161. #else
  162. return normalize(iNormal * GetNormalMatrix(modelMatrix));
  163. #endif
  164. }
  165. vec4 GetWorldTangent(mat4 modelMatrix)
  166. {
  167. #if defined(BILLBOARD)
  168. return vec4(normalize(vec3(1.0, 0.0, 0.0) * cBillboardRot), 1.0);
  169. #elif defined(DIRBILLBOARD)
  170. return vec4(normalize(vec3(1.0, 0.0, 0.0) * GetNormalMatrix(modelMatrix)), 1.0);
  171. #else
  172. return vec4(normalize(iTangent.xyz * GetNormalMatrix(modelMatrix)), iTangent.w);
  173. #endif
  174. }
  175. #else
  176. // Silence GLSL 150 deprecation warnings
  177. #ifdef GL3
  178. #define varying in
  179. #ifndef MRT_COUNT
  180. #if defined(DEFERRED)
  181. #define MRT_COUNT 4
  182. #elif defined(PREPASS)
  183. #define MRT_COUNT 2
  184. #else
  185. #define MRT_COUNT 1
  186. #endif
  187. #endif
  188. out vec4 fragData[MRT_COUNT];
  189. #define gl_FragColor fragData[0]
  190. #define gl_FragData fragData
  191. #endif
  192. #endif