Lighting.glsl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. lightDir = cLightDirPS;
  119. return clamp(dot(normal, lightDir), 0.0, 1.0);
  120. }
  121. float GetAttenPoint(vec3 normal, vec3 worldPos, out vec3 lightDir)
  122. {
  123. vec3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
  124. float lightDist = length(lightVec);
  125. float falloff = pow(clamp(1.0 - pow(lightDist / 1.0, 4.0), 0.0, 1.0), 2.0) * 3.14159265358979323846 / (4.0 * 3.14159265358979323846)*(pow(lightDist, 2.0) + 1.0);
  126. lightDir = lightVec / lightDist;
  127. return clamp(dot(normal, lightDir), 0.0, 1.0) * falloff;
  128. }
  129. float GetAttenSpot(vec3 normal, vec3 worldPos, out vec3 lightDir)
  130. {
  131. vec3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
  132. float lightDist = length(lightVec);
  133. float falloff = pow(clamp(1.0 - pow(lightDist / 1.0, 4.0), 0.0, 1.0), 2.0) / (pow(lightDist, 2.0) + 1.0);
  134. lightDir = lightVec / lightDist;
  135. return clamp(dot(normal, lightDir), 0.0, 1.0) * falloff;
  136. }
  137. float GetDiffuseVolumetric(vec3 worldPos)
  138. {
  139. #ifdef DIRLIGHT
  140. return 1.0;
  141. #else
  142. vec3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
  143. float lightDist = length(lightVec);
  144. return texture2D(sLightRampMap, vec2(lightDist, 0.0)).r;
  145. #endif
  146. }
  147. float GetSpecular(vec3 normal, vec3 eyeVec, vec3 lightDir, float specularPower)
  148. {
  149. vec3 halfVec = normalize(normalize(eyeVec) + lightDir);
  150. return pow(max(dot(normal, halfVec), 0.0), specularPower);
  151. }
  152. float GetIntensity(vec3 color)
  153. {
  154. return dot(color, vec3(0.299, 0.587, 0.114));
  155. }
  156. #ifdef SHADOW
  157. #if defined(DIRLIGHT) && (!defined(GL_ES) || defined(WEBGL))
  158. #define NUMCASCADES 4
  159. #else
  160. #define NUMCASCADES 1
  161. #endif
  162. #ifdef VSM_SHADOW
  163. float ReduceLightBleeding(float min, float p_max)
  164. {
  165. return clamp((p_max - min) / (1.0 - min), 0.0, 1.0);
  166. }
  167. float Chebyshev(vec2 Moments, float depth)
  168. {
  169. //One-tailed inequality valid if depth > Moments.x
  170. float p = float(depth <= Moments.x);
  171. //Compute variance.
  172. float Variance = Moments.y - (Moments.x * Moments.x);
  173. float minVariance = cVSMShadowParams.x;
  174. Variance = max(Variance, minVariance);
  175. //Compute probabilistic upper bound.
  176. float d = depth - Moments.x;
  177. float p_max = Variance / (Variance + d*d);
  178. // Prevent light bleeding
  179. p_max = ReduceLightBleeding(cVSMShadowParams.y, p_max);
  180. return max(p, p_max);
  181. }
  182. #endif
  183. #ifndef GL_ES
  184. float GetShadow(vec4 shadowPos)
  185. {
  186. #if defined(SIMPLE_SHADOW)
  187. // Take one sample
  188. #ifndef GL3
  189. float inLight = shadow2DProj(sShadowMap, shadowPos).r;
  190. #else
  191. float inLight = textureProj(sShadowMap, shadowPos);
  192. #endif
  193. return cShadowIntensity.y + cShadowIntensity.x * inLight;
  194. #elif defined(PCF_SHADOW)
  195. // Take four samples and average them
  196. // Note: in case of sampling a point light cube shadow, we optimize out the w divide as it has already been performed
  197. #ifndef POINTLIGHT
  198. vec2 offsets = cShadowMapInvSize * shadowPos.w;
  199. #else
  200. vec2 offsets = cShadowMapInvSize;
  201. #endif
  202. #ifndef GL3
  203. return cShadowIntensity.y + cShadowIntensity.x * (shadow2DProj(sShadowMap, shadowPos).r +
  204. shadow2DProj(sShadowMap, vec4(shadowPos.x + offsets.x, shadowPos.yzw)).r +
  205. shadow2DProj(sShadowMap, vec4(shadowPos.x, shadowPos.y + offsets.y, shadowPos.zw)).r +
  206. shadow2DProj(sShadowMap, vec4(shadowPos.xy + offsets.xy, shadowPos.zw)).r);
  207. #else
  208. return cShadowIntensity.y + cShadowIntensity.x * (textureProj(sShadowMap, shadowPos) +
  209. textureProj(sShadowMap, vec4(shadowPos.x + offsets.x, shadowPos.yzw)) +
  210. textureProj(sShadowMap, vec4(shadowPos.x, shadowPos.y + offsets.y, shadowPos.zw)) +
  211. textureProj(sShadowMap, vec4(shadowPos.xy + offsets.xy, shadowPos.zw)));
  212. #endif
  213. #elif defined(VSM_SHADOW)
  214. vec2 samples = texture2D(sShadowMap, shadowPos.xy / shadowPos.w).rg;
  215. return cShadowIntensity.y + cShadowIntensity.x * Chebyshev(samples, shadowPos.z / shadowPos.w);
  216. #endif
  217. }
  218. #else
  219. float GetShadow(highp vec4 shadowPos)
  220. {
  221. #if defined(SIMPLE_SHADOW)
  222. // Take one sample
  223. return cShadowIntensity.y + (texture2DProj(sShadowMap, shadowPos).r * shadowPos.w > shadowPos.z ? cShadowIntensity.x : 0.0);
  224. #elif defined(PCF_SHADOW)
  225. // Take four samples and average them
  226. vec2 offsets = cShadowMapInvSize * shadowPos.w;
  227. vec4 inLight = vec4(
  228. texture2DProj(sShadowMap, shadowPos).r * shadowPos.w > shadowPos.z,
  229. texture2DProj(sShadowMap, vec4(shadowPos.x + offsets.x, shadowPos.yzw)).r * shadowPos.w > shadowPos.z,
  230. texture2DProj(sShadowMap, vec4(shadowPos.x, shadowPos.y + offsets.y, shadowPos.zw)).r * shadowPos.w > shadowPos.z,
  231. texture2DProj(sShadowMap, vec4(shadowPos.xy + offsets.xy, shadowPos.zw)).r * shadowPos.w > shadowPos.z
  232. );
  233. return cShadowIntensity.y + dot(inLight, vec4(cShadowIntensity.x));
  234. #elif defined(VSM_SHADOW)
  235. vec2 samples = texture2D(sShadowMap, shadowPos.xy / shadowPos.w).rg;
  236. return cShadowIntensity.y + cShadowIntensity.x * Chebyshev(samples, shadowPos.z / shadowPos.w);
  237. #endif
  238. }
  239. #endif
  240. #ifdef POINTLIGHT
  241. float GetPointShadow(vec3 lightVec)
  242. {
  243. vec3 axis = textureCube(sFaceSelectCubeMap, lightVec).rgb;
  244. float depth = abs(dot(lightVec, axis));
  245. // Expand the maximum component of the light vector to get full 0.0 - 1.0 UV range from the cube map,
  246. // and to avoid sampling across faces. Some GPU's filter across faces, while others do not, and in this
  247. // case filtering across faces is wrong
  248. const vec3 factor = vec3(1.0 / 256.0);
  249. lightVec += factor * axis * lightVec;
  250. // Read the 2D UV coordinates, adjust according to shadow map size and add face offset
  251. vec4 indirectPos = textureCube(sIndirectionCubeMap, lightVec);
  252. indirectPos.xy *= cShadowCubeAdjust.xy;
  253. indirectPos.xy += vec2(cShadowCubeAdjust.z + indirectPos.z * 0.5, cShadowCubeAdjust.w + indirectPos.w);
  254. vec4 shadowPos = vec4(indirectPos.xy, cShadowDepthFade.x + cShadowDepthFade.y / depth, 1.0);
  255. return GetShadow(shadowPos);
  256. }
  257. #endif
  258. #ifdef DIRLIGHT
  259. float GetDirShadowFade(float inLight, float depth)
  260. {
  261. return min(inLight + max((depth - cShadowDepthFade.z) * cShadowDepthFade.w, 0.0), 1.0);
  262. }
  263. #if !defined(GL_ES) || defined(WEBGL)
  264. float GetDirShadow(const vec4 iShadowPos[NUMCASCADES], float depth)
  265. {
  266. vec4 shadowPos;
  267. if (depth < cShadowSplits.x)
  268. shadowPos = iShadowPos[0];
  269. else if (depth < cShadowSplits.y)
  270. shadowPos = iShadowPos[1];
  271. else if (depth < cShadowSplits.z)
  272. shadowPos = iShadowPos[2];
  273. else
  274. shadowPos = iShadowPos[3];
  275. return GetDirShadowFade(GetShadow(shadowPos), depth);
  276. }
  277. #else
  278. float GetDirShadow(const highp vec4 iShadowPos[NUMCASCADES], float depth)
  279. {
  280. return GetDirShadowFade(GetShadow(iShadowPos[0]), depth);
  281. }
  282. #endif
  283. #ifndef GL_ES
  284. float GetDirShadowDeferred(vec4 projWorldPos, vec3 normal, float depth)
  285. {
  286. vec4 shadowPos;
  287. #ifdef NORMALOFFSET
  288. float cosAngle = clamp(1.0 - dot(normal, cLightDirPS), 0.0, 1.0);
  289. if (depth < cShadowSplits.x)
  290. shadowPos = vec4(projWorldPos.xyz + cosAngle * cNormalOffsetScalePS.x * normal, 1.0) * cLightMatricesPS[0];
  291. else if (depth < cShadowSplits.y)
  292. shadowPos = vec4(projWorldPos.xyz + cosAngle * cNormalOffsetScalePS.y * normal, 1.0) * cLightMatricesPS[1];
  293. else if (depth < cShadowSplits.z)
  294. shadowPos = vec4(projWorldPos.xyz + cosAngle * cNormalOffsetScalePS.z * normal, 1.0) * cLightMatricesPS[2];
  295. else
  296. shadowPos = vec4(projWorldPos.xyz + cosAngle * cNormalOffsetScalePS.w * normal, 1.0) * cLightMatricesPS[3];
  297. #else
  298. if (depth < cShadowSplits.x)
  299. shadowPos = projWorldPos * cLightMatricesPS[0];
  300. else if (depth < cShadowSplits.y)
  301. shadowPos = projWorldPos * cLightMatricesPS[1];
  302. else if (depth < cShadowSplits.z)
  303. shadowPos = projWorldPos * cLightMatricesPS[2];
  304. else
  305. shadowPos = projWorldPos * cLightMatricesPS[3];
  306. #endif
  307. return GetDirShadowFade(GetShadow(shadowPos), depth);
  308. }
  309. #endif
  310. #endif
  311. #ifndef GL_ES
  312. float GetShadow(const vec4 iShadowPos[NUMCASCADES], float depth)
  313. #else
  314. float GetShadow(const highp vec4 iShadowPos[NUMCASCADES], float depth)
  315. #endif
  316. {
  317. #if defined(DIRLIGHT)
  318. return GetDirShadow(iShadowPos, depth);
  319. #elif defined(SPOTLIGHT)
  320. return GetShadow(iShadowPos[0]);
  321. #else
  322. return GetPointShadow(iShadowPos[0].xyz);
  323. #endif
  324. }
  325. #ifndef GL_ES
  326. float GetShadowDeferred(vec4 projWorldPos, vec3 normal, float depth)
  327. {
  328. #ifdef DIRLIGHT
  329. return GetDirShadowDeferred(projWorldPos, normal, depth);
  330. #else
  331. #ifdef NORMALOFFSET
  332. float cosAngle = clamp(1.0 - dot(normal, normalize(cLightPosPS.xyz - projWorldPos.xyz)), 0.0, 1.0);
  333. projWorldPos.xyz += cosAngle * cNormalOffsetScalePS.x * normal;
  334. #endif
  335. #ifdef SPOTLIGHT
  336. vec4 shadowPos = projWorldPos * cLightMatricesPS[1];
  337. return GetShadow(shadowPos);
  338. #else
  339. vec3 shadowPos = projWorldPos.xyz - cLightPosPS.xyz;
  340. return GetPointShadow(shadowPos);
  341. #endif
  342. #endif
  343. }
  344. #endif
  345. #endif
  346. #endif