Lighting.glsl 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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)
  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 cLightMatrices[index] * projWorldPos;
  66. #elif defined(SPOTLIGHT)
  67. return cLightMatrices[1] * projWorldPos;
  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.333));
  105. }
  106. #ifdef SHADOW
  107. #if defined(DIRLIGHT) && !defined(GL_ES)
  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. vec4 inLight = vec4(
  124. 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. );
  129. return cShadowIntensity.y + dot(inLight, vec4(cShadowIntensity.x));
  130. #else
  131. // Take one sample
  132. float inLight = shadow2DProj(sShadowMap, shadowPos).r;
  133. return cShadowIntensity.y + cShadowIntensity.x * inLight;
  134. #endif
  135. #else
  136. #ifndef LQSHADOW
  137. // Take four samples and average them
  138. vec2 offsets = cShadowMapInvSize * shadowPos.w;
  139. vec4 inLight = vec4(
  140. texture2DProj(sShadowMap, shadowPos).r * shadowPos.w > shadowPos.z,
  141. texture2DProj(sShadowMap, vec4(shadowPos.x + offsets.x, shadowPos.yzw)).r * shadowPos.w > shadowPos.z,
  142. texture2DProj(sShadowMap, vec4(shadowPos.x, shadowPos.y + offsets.y, shadowPos.zw)).r * shadowPos.w > shadowPos.z,
  143. texture2DProj(sShadowMap, vec4(shadowPos.xy + offsets.xy, shadowPos.zw)).r * shadowPos.w > shadowPos.z
  144. );
  145. return cShadowIntensity.y + dot(inLight, vec4(cShadowIntensity.x));
  146. #else
  147. // Take one sample
  148. return cShadowIntensity.y + (texture2DProj(sShadowMap, shadowPos).r * shadowPos.w > shadowPos.z ? cShadowIntensity.x : 0.0);
  149. #endif
  150. #endif
  151. }
  152. #ifdef POINTLIGHT
  153. float GetPointShadow(vec3 lightVec)
  154. {
  155. vec3 axis = textureCube(sFaceSelectCubeMap, lightVec).rgb;
  156. float depth = abs(dot(lightVec, axis));
  157. // Expand the maximum component of the light vector to get full 0.0 - 1.0 UV range from the cube map,
  158. // and to avoid sampling across faces. Some GPU's filter across faces, while others do not, and in this
  159. // case filtering across faces is wrong
  160. const vec3 factor = vec3(1.0 / 256.0);
  161. lightVec += factor * axis * lightVec;
  162. // Read the 2D UV coordinates, adjust according to shadow map size and add face offset
  163. vec4 indirectPos = textureCube(sIndirectionCubeMap, lightVec);
  164. indirectPos.xy *= cShadowCubeAdjust.xy;
  165. indirectPos.xy += vec2(cShadowCubeAdjust.z + indirectPos.z * 0.5, cShadowCubeAdjust.w + indirectPos.w);
  166. vec4 shadowPos = vec4(indirectPos.xy, cShadowDepthFade.x + cShadowDepthFade.y / depth, 1.0);
  167. return GetShadow(shadowPos);
  168. }
  169. #endif
  170. #ifdef DIRLIGHT
  171. float GetDirShadowFade(float inLight, float depth)
  172. {
  173. return min(inLight + max((depth - cShadowDepthFade.z) * cShadowDepthFade.w, 0.0), 1.0);
  174. }
  175. #ifndef GL_ES
  176. float GetDirShadow(const vec4 iShadowPos[NUMCASCADES], float depth)
  177. {
  178. vec4 shadowPos;
  179. if (depth < cShadowSplits.x)
  180. shadowPos = iShadowPos[0];
  181. else if (depth < cShadowSplits.y)
  182. shadowPos = iShadowPos[1];
  183. else if (depth < cShadowSplits.z)
  184. shadowPos = iShadowPos[2];
  185. else
  186. shadowPos = iShadowPos[3];
  187. return GetDirShadowFade(GetShadow(shadowPos), depth);
  188. }
  189. float GetDirShadowDeferred(vec4 projWorldPos, float depth)
  190. {
  191. vec4 shadowPos;
  192. if (depth < cShadowSplits.x)
  193. shadowPos = cLightMatricesPS[0] * projWorldPos;
  194. else if (depth < cShadowSplits.y)
  195. shadowPos = cLightMatricesPS[1] * projWorldPos;
  196. else if (depth < cShadowSplits.z)
  197. shadowPos = cLightMatricesPS[2] * projWorldPos;
  198. else
  199. shadowPos = cLightMatricesPS[3] * projWorldPos;
  200. return GetDirShadowFade(GetShadow(shadowPos), depth);
  201. }
  202. #else
  203. float GetDirShadow(const vec4 iShadowPos[NUMCASCADES], float depth)
  204. {
  205. return GetDirShadowFade(GetShadow(iShadowPos[0]), depth);
  206. }
  207. #endif
  208. #endif
  209. float GetShadow(vec4 iShadowPos[NUMCASCADES], float depth)
  210. {
  211. #if defined(DIRLIGHT)
  212. return GetDirShadow(iShadowPos, depth);
  213. #elif defined(SPOTLIGHT)
  214. return GetShadow(iShadowPos[0]);
  215. #else
  216. return GetPointShadow(iShadowPos[0].xyz);
  217. #endif
  218. }
  219. #ifndef GL_ES
  220. float GetShadowDeferred(vec4 projWorldPos, float depth)
  221. {
  222. #if defined(DIRLIGHT)
  223. return GetDirShadowDeferred(projWorldPos, depth);
  224. #elif defined(SPOTLIGHT)
  225. vec4 shadowPos = cLightMatricesPS[1] * projWorldPos;
  226. return GetShadow(shadowPos);
  227. #else
  228. vec3 shadowPos = projWorldPos.xyz - cLightPosPS.xyz;
  229. return GetPointShadow(shadowPos);
  230. #endif
  231. }
  232. #endif
  233. #endif
  234. #endif