Lighting.glsl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. #ifdef TRANSLUCENT
  18. float NdotL = abs(dot(normal, lightDir));
  19. #else
  20. float NdotL = max(dot(normal, lightDir), 0.0);
  21. #endif
  22. return NdotL;
  23. }
  24. // Point/spot light
  25. else
  26. {
  27. vec3 lightVec = (lightPos - worldPos) * invRange;
  28. float lightDist = length(lightVec);
  29. vec3 localDir = lightVec / lightDist;
  30. #ifdef TRANSLUCENT
  31. float NdotL = abs(dot(normal, localDir));
  32. #else
  33. float NdotL = max(dot(normal, localDir), 0.0);
  34. #endif
  35. float atten = clamp(1.0 - lightDist * lightDist, 0.0, 1.0);
  36. float spotEffect = dot(localDir, lightDir);
  37. float spotAtten = clamp((spotEffect - cutoff) * invCutoff, 0.0, 1.0);
  38. return NdotL * atten * spotAtten;
  39. }
  40. }
  41. float GetVertexLightVolumetric(int index, vec3 worldPos)
  42. {
  43. vec3 lightDir = cVertexLights[index * 3 + 1].xyz;
  44. vec3 lightPos = cVertexLights[index * 3 + 2].xyz;
  45. float invRange = cVertexLights[index * 3].w;
  46. float cutoff = cVertexLights[index * 3 + 1].w;
  47. float invCutoff = cVertexLights[index * 3 + 2].w;
  48. // Directional light
  49. if (invRange == 0.0)
  50. return 1.0;
  51. // Point/spot light
  52. else
  53. {
  54. vec3 lightVec = (lightPos - worldPos) * invRange;
  55. float lightDist = length(lightVec);
  56. vec3 localDir = lightVec / lightDist;
  57. float atten = clamp(1.0 - lightDist * lightDist, 0.0, 1.0);
  58. float spotEffect = dot(localDir, lightDir);
  59. float spotAtten = clamp((spotEffect - cutoff) * invCutoff, 0.0, 1.0);
  60. return atten * spotAtten;
  61. }
  62. }
  63. #endif
  64. #ifdef SHADOW
  65. #if defined(DIRLIGHT) && (!defined(GL_ES) || defined(WEBGL))
  66. #define NUMCASCADES 4
  67. #else
  68. #define NUMCASCADES 1
  69. #endif
  70. vec4 GetShadowPos(int index, vec3 normal, vec4 projWorldPos)
  71. {
  72. #ifdef NORMALOFFSET
  73. float normalOffsetScale[4];
  74. normalOffsetScale[0] = cNormalOffsetScale.x;
  75. normalOffsetScale[1] = cNormalOffsetScale.y;
  76. normalOffsetScale[2] = cNormalOffsetScale.z;
  77. normalOffsetScale[3] = cNormalOffsetScale.w;
  78. #ifdef DIRLIGHT
  79. float cosAngle = clamp(1.0 - dot(normal, cLightDir), 0.0, 1.0);
  80. #else
  81. float cosAngle = clamp(1.0 - dot(normal, normalize(cLightPos.xyz - projWorldPos.xyz)), 0.0, 1.0);
  82. #endif
  83. projWorldPos.xyz += cosAngle * normalOffsetScale[index] * normal;
  84. #endif
  85. #if defined(DIRLIGHT)
  86. return projWorldPos * cLightMatrices[index];
  87. #elif defined(SPOTLIGHT)
  88. return projWorldPos * cLightMatrices[1];
  89. #else
  90. return vec4(projWorldPos.xyz - cLightPos.xyz, 1.0);
  91. #endif
  92. }
  93. #endif
  94. #endif
  95. #ifdef COMPILEPS
  96. float GetDiffuse(vec3 normal, vec3 worldPos, out vec3 lightDir)
  97. {
  98. #ifdef DIRLIGHT
  99. lightDir = cLightDirPS;
  100. #ifdef TRANSLUCENT
  101. return abs(dot(normal, lightDir));
  102. #else
  103. return max(dot(normal, lightDir), 0.0);
  104. #endif
  105. #else
  106. vec3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
  107. float lightDist = length(lightVec);
  108. lightDir = lightVec / lightDist;
  109. #ifdef TRANSLUCENT
  110. return abs(dot(normal, lightDir)) * texture2D(sLightRampMap, vec2(lightDist, 0.0)).r;
  111. #else
  112. return max(dot(normal, lightDir), 0.0) * texture2D(sLightRampMap, vec2(lightDist, 0.0)).r;
  113. #endif
  114. #endif
  115. }
  116. float GetAtten(vec3 normal, vec3 worldPos, out vec3 lightDir)
  117. {
  118. #ifdef DIRLIGHT
  119. lightDir = cLightDirPS;
  120. return clamp(dot(normal, lightDir), 0.0, 1.0);
  121. #else
  122. vec3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
  123. float lightDist = length(lightVec);
  124. float falloff = pow(clamp(1.0 - pow(lightDist / 1.0, 4.0), 0.0, 1.0), 2.0) / (pow(lightDist, 2.0) + 1.0);
  125. lightDir = lightVec / vec3(lightDist, lightDist, lightDist);
  126. return clamp(dot(normal, lightDir), 0.0, 1.0) * falloff;
  127. #endif
  128. }
  129. float GetDiffuseVolumetric(vec3 worldPos)
  130. {
  131. #ifdef DIRLIGHT
  132. return 1.0;
  133. #else
  134. vec3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
  135. float lightDist = length(lightVec);
  136. return texture2D(sLightRampMap, vec2(lightDist, 0.0)).r;
  137. #endif
  138. }
  139. float GetSpecular(vec3 normal, vec3 eyeVec, vec3 lightDir, float specularPower)
  140. {
  141. vec3 halfVec = normalize(normalize(eyeVec) + lightDir);
  142. return pow(max(dot(normal, halfVec), 0.0), specularPower);
  143. }
  144. float GetIntensity(vec3 color)
  145. {
  146. return dot(color, vec3(0.299, 0.587, 0.114));
  147. }
  148. #ifdef SHADOW
  149. #if defined(DIRLIGHT) && (!defined(GL_ES) || defined(WEBGL))
  150. #define NUMCASCADES 4
  151. #else
  152. #define NUMCASCADES 1
  153. #endif
  154. #ifdef VSM_SHADOW
  155. float ReduceLightBleeding(float min, float p_max)
  156. {
  157. return clamp((p_max - min) / (1.0 - min), 0.0, 1.0);
  158. }
  159. float Chebyshev(vec2 Moments, float depth)
  160. {
  161. //One-tailed inequality valid if depth > Moments.x
  162. float p = float(depth <= Moments.x);
  163. //Compute variance.
  164. float Variance = Moments.y - (Moments.x * Moments.x);
  165. float minVariance = cVSMShadowParams.x;
  166. Variance = max(Variance, minVariance);
  167. //Compute probabilistic upper bound.
  168. float d = depth - Moments.x;
  169. float p_max = Variance / (Variance + d*d);
  170. // Prevent light bleeding
  171. p_max = ReduceLightBleeding(cVSMShadowParams.y, p_max);
  172. return max(p, p_max);
  173. }
  174. #endif
  175. #ifndef GL_ES
  176. float GetShadow(vec4 shadowPos)
  177. {
  178. #if defined(SIMPLE_SHADOW)
  179. // Take one sample
  180. #ifndef GL3
  181. float inLight = shadow2DProj(sShadowMap, shadowPos).r;
  182. #else
  183. float inLight = textureProj(sShadowMap, shadowPos);
  184. #endif
  185. return cShadowIntensity.y + cShadowIntensity.x * inLight;
  186. #elif defined(PCF_SHADOW)
  187. // Take four samples and average them
  188. // Note: in case of sampling a point light cube shadow, we optimize out the w divide as it has already been performed
  189. #ifndef POINTLIGHT
  190. vec2 offsets = cShadowMapInvSize * shadowPos.w;
  191. #else
  192. vec2 offsets = cShadowMapInvSize;
  193. #endif
  194. #ifndef GL3
  195. return cShadowIntensity.y + cShadowIntensity.x * (shadow2DProj(sShadowMap, shadowPos).r +
  196. shadow2DProj(sShadowMap, vec4(shadowPos.x + offsets.x, shadowPos.yzw)).r +
  197. shadow2DProj(sShadowMap, vec4(shadowPos.x, shadowPos.y + offsets.y, shadowPos.zw)).r +
  198. shadow2DProj(sShadowMap, vec4(shadowPos.xy + offsets.xy, shadowPos.zw)).r);
  199. #else
  200. return cShadowIntensity.y + cShadowIntensity.x * (textureProj(sShadowMap, shadowPos) +
  201. textureProj(sShadowMap, vec4(shadowPos.x + offsets.x, shadowPos.yzw)) +
  202. textureProj(sShadowMap, vec4(shadowPos.x, shadowPos.y + offsets.y, shadowPos.zw)) +
  203. textureProj(sShadowMap, vec4(shadowPos.xy + offsets.xy, shadowPos.zw)));
  204. #endif
  205. #elif defined(VSM_SHADOW)
  206. vec2 samples = texture2D(sShadowMap, shadowPos.xy / shadowPos.w).rg;
  207. return cShadowIntensity.y + cShadowIntensity.x * Chebyshev(samples, shadowPos.z / shadowPos.w);
  208. #endif
  209. }
  210. #else
  211. float GetShadow(highp vec4 shadowPos)
  212. {
  213. #if defined(SIMPLE_SHADOW)
  214. // Take one sample
  215. return cShadowIntensity.y + (texture2DProj(sShadowMap, shadowPos).r * shadowPos.w > shadowPos.z ? cShadowIntensity.x : 0.0);
  216. #elif defined(PCF_SHADOW)
  217. // Take four samples and average them
  218. vec2 offsets = cShadowMapInvSize * shadowPos.w;
  219. vec4 inLight = vec4(
  220. texture2DProj(sShadowMap, shadowPos).r * shadowPos.w > shadowPos.z,
  221. texture2DProj(sShadowMap, vec4(shadowPos.x + offsets.x, shadowPos.yzw)).r * shadowPos.w > shadowPos.z,
  222. texture2DProj(sShadowMap, vec4(shadowPos.x, shadowPos.y + offsets.y, shadowPos.zw)).r * shadowPos.w > shadowPos.z,
  223. texture2DProj(sShadowMap, vec4(shadowPos.xy + offsets.xy, shadowPos.zw)).r * shadowPos.w > shadowPos.z
  224. );
  225. return cShadowIntensity.y + dot(inLight, vec4(cShadowIntensity.x));
  226. #elif defined(VSM_SHADOW)
  227. vec2 samples = texture2D(sShadowMap, shadowPos.xy / shadowPos.w).rg;
  228. return cShadowIntensity.y + cShadowIntensity.x * Chebyshev(samples, shadowPos.z / shadowPos.w);
  229. #endif
  230. }
  231. #endif
  232. #ifdef POINTLIGHT
  233. float GetPointShadow(vec3 lightVec)
  234. {
  235. vec3 axis = textureCube(sFaceSelectCubeMap, lightVec).rgb;
  236. float depth = abs(dot(lightVec, axis));
  237. // Expand the maximum component of the light vector to get full 0.0 - 1.0 UV range from the cube map,
  238. // and to avoid sampling across faces. Some GPU's filter across faces, while others do not, and in this
  239. // case filtering across faces is wrong
  240. const vec3 factor = vec3(1.0 / 256.0);
  241. lightVec += factor * axis * lightVec;
  242. // Read the 2D UV coordinates, adjust according to shadow map size and add face offset
  243. vec4 indirectPos = textureCube(sIndirectionCubeMap, lightVec);
  244. indirectPos.xy *= cShadowCubeAdjust.xy;
  245. indirectPos.xy += vec2(cShadowCubeAdjust.z + indirectPos.z * 0.5, cShadowCubeAdjust.w + indirectPos.w);
  246. vec4 shadowPos = vec4(indirectPos.xy, cShadowDepthFade.x + cShadowDepthFade.y / depth, 1.0);
  247. return GetShadow(shadowPos);
  248. }
  249. #endif
  250. #ifdef DIRLIGHT
  251. float GetDirShadowFade(float inLight, float depth)
  252. {
  253. return min(inLight + max((depth - cShadowDepthFade.z) * cShadowDepthFade.w, 0.0), 1.0);
  254. }
  255. #if !defined(GL_ES) || defined(WEBGL)
  256. float GetDirShadow(const vec4 iShadowPos[NUMCASCADES], float depth)
  257. {
  258. vec4 shadowPos;
  259. if (depth < cShadowSplits.x)
  260. shadowPos = iShadowPos[0];
  261. else if (depth < cShadowSplits.y)
  262. shadowPos = iShadowPos[1];
  263. else if (depth < cShadowSplits.z)
  264. shadowPos = iShadowPos[2];
  265. else
  266. shadowPos = iShadowPos[3];
  267. return GetDirShadowFade(GetShadow(shadowPos), depth);
  268. }
  269. #else
  270. float GetDirShadow(const highp vec4 iShadowPos[NUMCASCADES], float depth)
  271. {
  272. return GetDirShadowFade(GetShadow(iShadowPos[0]), depth);
  273. }
  274. #endif
  275. #ifndef GL_ES
  276. float GetDirShadowDeferred(vec4 projWorldPos, vec3 normal, float depth)
  277. {
  278. vec4 shadowPos;
  279. #ifdef NORMALOFFSET
  280. float cosAngle = clamp(1.0 - dot(normal, cLightDirPS), 0.0, 1.0);
  281. if (depth < cShadowSplits.x)
  282. shadowPos = vec4(projWorldPos.xyz + cosAngle * cNormalOffsetScalePS.x * normal, 1.0) * cLightMatricesPS[0];
  283. else if (depth < cShadowSplits.y)
  284. shadowPos = vec4(projWorldPos.xyz + cosAngle * cNormalOffsetScalePS.y * normal, 1.0) * cLightMatricesPS[1];
  285. else if (depth < cShadowSplits.z)
  286. shadowPos = vec4(projWorldPos.xyz + cosAngle * cNormalOffsetScalePS.z * normal, 1.0) * cLightMatricesPS[2];
  287. else
  288. shadowPos = vec4(projWorldPos.xyz + cosAngle * cNormalOffsetScalePS.w * normal, 1.0) * cLightMatricesPS[3];
  289. #else
  290. if (depth < cShadowSplits.x)
  291. shadowPos = projWorldPos * cLightMatricesPS[0];
  292. else if (depth < cShadowSplits.y)
  293. shadowPos = projWorldPos * cLightMatricesPS[1];
  294. else if (depth < cShadowSplits.z)
  295. shadowPos = projWorldPos * cLightMatricesPS[2];
  296. else
  297. shadowPos = projWorldPos * cLightMatricesPS[3];
  298. #endif
  299. return GetDirShadowFade(GetShadow(shadowPos), depth);
  300. }
  301. #endif
  302. #endif
  303. #ifndef GL_ES
  304. float GetShadow(const vec4 iShadowPos[NUMCASCADES], float depth)
  305. #else
  306. float GetShadow(const highp vec4 iShadowPos[NUMCASCADES], float depth)
  307. #endif
  308. {
  309. #if defined(DIRLIGHT)
  310. return GetDirShadow(iShadowPos, depth);
  311. #elif defined(SPOTLIGHT)
  312. return GetShadow(iShadowPos[0]);
  313. #else
  314. return GetPointShadow(iShadowPos[0].xyz);
  315. #endif
  316. }
  317. #ifndef GL_ES
  318. float GetShadowDeferred(vec4 projWorldPos, vec3 normal, float depth)
  319. {
  320. #ifdef DIRLIGHT
  321. return GetDirShadowDeferred(projWorldPos, normal, depth);
  322. #else
  323. #ifdef NORMALOFFSET
  324. float cosAngle = clamp(1.0 - dot(normal, normalize(cLightPosPS.xyz - projWorldPos.xyz)), 0.0, 1.0);
  325. projWorldPos.xyz += cosAngle * cNormalOffsetScalePS.x * normal;
  326. #endif
  327. #ifdef SPOTLIGHT
  328. vec4 shadowPos = projWorldPos * cLightMatricesPS[1];
  329. return GetShadow(shadowPos);
  330. #else
  331. vec3 shadowPos = projWorldPos.xyz - cLightPosPS.xyz;
  332. return GetPointShadow(shadowPos);
  333. #endif
  334. #endif
  335. }
  336. #endif
  337. #endif
  338. #endif