Lighting.glsl 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #ifdef COMPILEVS
  2. vec3 GetAmbient(float zonePos)
  3. {
  4. return cAmbientStartColor + zonePos * cAmbientEndColor;
  5. }
  6. #ifdef NUMVERTEXLIGHTS
  7. float GetVertexLight(int index, vec3 worldPos, vec3 normal)
  8. {
  9. vec3 lightDir = cVertexLights[index * 3 + 1].xyz;
  10. vec3 lightPos = cVertexLights[index * 3 + 2].xyz;
  11. float invRange = cVertexLights[index * 3].w;
  12. float cutoff = cVertexLights[index * 3 + 1].w;
  13. float invCutoff = cVertexLights[index * 3 + 2].w;
  14. // Directional light
  15. if (invRange == 0.0)
  16. {
  17. float NdotL = max(dot(normal, lightDir), 0.0);
  18. return NdotL;
  19. }
  20. // Point/spot light
  21. else
  22. {
  23. vec3 lightVec = (lightPos - worldPos) * invRange;
  24. float lightDist = length(lightVec);
  25. vec3 localDir = lightVec / lightDist;
  26. float NdotL = max(dot(normal, localDir), 0.0);
  27. float atten = clamp(1.0 - lightDist * lightDist, 0.0, 1.0);
  28. float spotEffect = dot(localDir, lightDir);
  29. float spotAtten = clamp((spotEffect - cutoff) * invCutoff, 0.0, 1.0);
  30. return NdotL * atten * spotAtten;
  31. }
  32. }
  33. float GetVertexLightVolumetric(int index, vec3 worldPos)
  34. {
  35. vec3 lightDir = cVertexLights[index * 3 + 1].xyz;
  36. vec3 lightPos = cVertexLights[index * 3 + 2].xyz;
  37. float invRange = cVertexLights[index * 3].w;
  38. float cutoff = cVertexLights[index * 3 + 1].w;
  39. float invCutoff = cVertexLights[index * 3 + 2].w;
  40. // Directional light
  41. if (invRange == 0.0)
  42. return 1.0;
  43. // Point/spot light
  44. else
  45. {
  46. vec3 lightVec = (lightPos - worldPos) * invRange;
  47. float lightDist = length(lightVec);
  48. vec3 localDir = lightVec / lightDist;
  49. float atten = clamp(1.0 - lightDist * lightDist, 0.0, 1.0);
  50. float spotEffect = dot(localDir, lightDir);
  51. float spotAtten = clamp((spotEffect - cutoff) * invCutoff, 0.0, 1.0);
  52. return atten * spotAtten;
  53. }
  54. }
  55. #endif
  56. #ifdef SHADOW
  57. #if defined(DIRLIGHT) && (!defined(GL_ES) || defined(WEBGL))
  58. #define NUMCASCADES 4
  59. #else
  60. #define NUMCASCADES 1
  61. #endif
  62. vec4 GetShadowPos(int index, vec4 projWorldPos)
  63. {
  64. #if defined(DIRLIGHT)
  65. return projWorldPos * cLightMatrices[index];
  66. #elif defined(SPOTLIGHT)
  67. return projWorldPos * cLightMatrices[1];
  68. #else
  69. return vec4(projWorldPos.xyz - cLightPos.xyz, 1.0);
  70. #endif
  71. }
  72. #endif
  73. #endif
  74. #ifdef COMPILEPS
  75. float GetDiffuse(vec3 normal, vec3 worldPos, out vec3 lightDir)
  76. {
  77. #ifdef DIRLIGHT
  78. lightDir = cLightDirPS;
  79. return max(dot(normal, lightDir), 0.0);
  80. #else
  81. vec3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
  82. float lightDist = length(lightVec);
  83. lightDir = lightVec / lightDist;
  84. return max(dot(normal, lightDir), 0.0) * texture2D(sLightRampMap, vec2(lightDist, 0.0)).r;
  85. #endif
  86. }
  87. float GetDiffuseVolumetric(vec3 worldPos)
  88. {
  89. #ifdef DIRLIGHT
  90. return 1.0;
  91. #else
  92. vec3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
  93. float lightDist = length(lightVec);
  94. return texture2D(sLightRampMap, vec2(lightDist, 0.0)).r;
  95. #endif
  96. }
  97. float GetSpecular(vec3 normal, vec3 eyeVec, vec3 lightDir, float specularPower)
  98. {
  99. vec3 halfVec = normalize(normalize(eyeVec) + lightDir);
  100. return pow(max(dot(normal, halfVec), 0.0), specularPower);
  101. }
  102. float GetIntensity(vec3 color)
  103. {
  104. return dot(color, vec3(0.299, 0.587, 0.114));
  105. }
  106. #ifdef SHADOW
  107. #if defined(DIRLIGHT) && (!defined(GL_ES) || defined(WEBGL))
  108. #define NUMCASCADES 4
  109. #else
  110. #define NUMCASCADES 1
  111. #endif
  112. float GetShadow(vec4 shadowPos)
  113. {
  114. #ifndef GL_ES
  115. #ifndef LQSHADOW
  116. // Take four samples and average them
  117. // Note: in case of sampling a point light cube shadow, we optimize out the w divide as it has already been performed
  118. #ifndef POINTLIGHT
  119. vec2 offsets = cShadowMapInvSize * shadowPos.w;
  120. #else
  121. vec2 offsets = cShadowMapInvSize;
  122. #endif
  123. #ifndef GL3
  124. return cShadowIntensity.y + cShadowIntensity.x * (shadow2DProj(sShadowMap, shadowPos).r +
  125. shadow2DProj(sShadowMap, vec4(shadowPos.x + offsets.x, shadowPos.yzw)).r +
  126. shadow2DProj(sShadowMap, vec4(shadowPos.x, shadowPos.y + offsets.y, shadowPos.zw)).r +
  127. shadow2DProj(sShadowMap, vec4(shadowPos.xy + offsets.xy, shadowPos.zw)).r);
  128. #else
  129. return cShadowIntensity.y + cShadowIntensity.x * (textureProj(sShadowMap, shadowPos) +
  130. textureProj(sShadowMap, vec4(shadowPos.x + offsets.x, shadowPos.yzw)) +
  131. textureProj(sShadowMap, vec4(shadowPos.x, shadowPos.y + offsets.y, shadowPos.zw)) +
  132. textureProj(sShadowMap, vec4(shadowPos.xy + offsets.xy, shadowPos.zw)));
  133. #endif
  134. #else
  135. // Take one sample
  136. #ifndef GL3
  137. float inLight = shadow2DProj(sShadowMap, shadowPos).r;
  138. #else
  139. float inLight = textureProj(sShadowMap, shadowPos);
  140. #endif
  141. return cShadowIntensity.y + cShadowIntensity.x * inLight;
  142. #endif
  143. #else
  144. #ifndef LQSHADOW
  145. // Take four samples and average them
  146. vec2 offsets = cShadowMapInvSize * shadowPos.w;
  147. vec4 inLight = vec4(
  148. texture2DProj(sShadowMap, shadowPos).r * shadowPos.w > shadowPos.z,
  149. texture2DProj(sShadowMap, vec4(shadowPos.x + offsets.x, shadowPos.yzw)).r * shadowPos.w > shadowPos.z,
  150. texture2DProj(sShadowMap, vec4(shadowPos.x, shadowPos.y + offsets.y, shadowPos.zw)).r * shadowPos.w > shadowPos.z,
  151. texture2DProj(sShadowMap, vec4(shadowPos.xy + offsets.xy, shadowPos.zw)).r * shadowPos.w > shadowPos.z
  152. );
  153. return cShadowIntensity.y + dot(inLight, vec4(cShadowIntensity.x));
  154. #else
  155. // Take one sample
  156. return cShadowIntensity.y + (texture2DProj(sShadowMap, shadowPos).r * shadowPos.w > shadowPos.z ? cShadowIntensity.x : 0.0);
  157. #endif
  158. #endif
  159. }
  160. #ifdef POINTLIGHT
  161. float GetPointShadow(vec3 lightVec)
  162. {
  163. vec3 axis = textureCube(sFaceSelectCubeMap, lightVec).rgb;
  164. float depth = abs(dot(lightVec, axis));
  165. // Expand the maximum component of the light vector to get full 0.0 - 1.0 UV range from the cube map,
  166. // and to avoid sampling across faces. Some GPU's filter across faces, while others do not, and in this
  167. // case filtering across faces is wrong
  168. const vec3 factor = vec3(1.0 / 256.0);
  169. lightVec += factor * axis * lightVec;
  170. // Read the 2D UV coordinates, adjust according to shadow map size and add face offset
  171. vec4 indirectPos = textureCube(sIndirectionCubeMap, lightVec);
  172. indirectPos.xy *= cShadowCubeAdjust.xy;
  173. indirectPos.xy += vec2(cShadowCubeAdjust.z + indirectPos.z * 0.5, cShadowCubeAdjust.w + indirectPos.w);
  174. vec4 shadowPos = vec4(indirectPos.xy, cShadowDepthFade.x + cShadowDepthFade.y / depth, 1.0);
  175. return GetShadow(shadowPos);
  176. }
  177. #endif
  178. #ifdef DIRLIGHT
  179. float GetDirShadowFade(float inLight, float depth)
  180. {
  181. return min(inLight + max((depth - cShadowDepthFade.z) * cShadowDepthFade.w, 0.0), 1.0);
  182. }
  183. #if !defined(GL_ES) || defined(WEBGL)
  184. float GetDirShadow(const vec4 iShadowPos[NUMCASCADES], float depth)
  185. {
  186. vec4 shadowPos;
  187. if (depth < cShadowSplits.x)
  188. shadowPos = iShadowPos[0];
  189. else if (depth < cShadowSplits.y)
  190. shadowPos = iShadowPos[1];
  191. else if (depth < cShadowSplits.z)
  192. shadowPos = iShadowPos[2];
  193. else
  194. shadowPos = iShadowPos[3];
  195. return GetDirShadowFade(GetShadow(shadowPos), depth);
  196. }
  197. #else
  198. float GetDirShadow(const vec4 iShadowPos[NUMCASCADES], float depth)
  199. {
  200. return GetDirShadowFade(GetShadow(iShadowPos[0]), depth);
  201. }
  202. #endif
  203. #ifndef GL_ES
  204. float GetDirShadowDeferred(vec4 projWorldPos, float depth)
  205. {
  206. vec4 shadowPos;
  207. if (depth < cShadowSplits.x)
  208. shadowPos = projWorldPos * cLightMatricesPS[0];
  209. else if (depth < cShadowSplits.y)
  210. shadowPos = projWorldPos * cLightMatricesPS[1];
  211. else if (depth < cShadowSplits.z)
  212. shadowPos = projWorldPos * cLightMatricesPS[2];
  213. else
  214. shadowPos = projWorldPos * cLightMatricesPS[3];
  215. return GetDirShadowFade(GetShadow(shadowPos), depth);
  216. }
  217. #endif
  218. #endif
  219. float GetShadow(vec4 iShadowPos[NUMCASCADES], float depth)
  220. {
  221. #if defined(DIRLIGHT)
  222. return GetDirShadow(iShadowPos, depth);
  223. #elif defined(SPOTLIGHT)
  224. return GetShadow(iShadowPos[0]);
  225. #else
  226. return GetPointShadow(iShadowPos[0].xyz);
  227. #endif
  228. }
  229. #ifndef GL_ES
  230. float GetShadowDeferred(vec4 projWorldPos, float depth)
  231. {
  232. #if defined(DIRLIGHT)
  233. return GetDirShadowDeferred(projWorldPos, depth);
  234. #elif defined(SPOTLIGHT)
  235. vec4 shadowPos = projWorldPos * cLightMatricesPS[1];
  236. return GetShadow(shadowPos);
  237. #else
  238. vec3 shadowPos = projWorldPos.xyz - cLightPosPS.xyz;
  239. return GetPointShadow(shadowPos);
  240. #endif
  241. }
  242. #endif
  243. #endif
  244. #endif