Transform.glsl 6.0 KB

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