LightFunctions.glsl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright (C) 2009-2018, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. // Contains functions for light calculations
  6. #pragma once
  7. #include <shaders/Functions.glsl>
  8. #include <shaders/Pack.glsl>
  9. const F32 LIGHT_FRUSTUM_NEAR_PLANE = 0.1 / 4.0;
  10. const U32 SHADOW_SAMPLE_COUNT = 16;
  11. #if !defined(ESM_CONSTANT)
  12. const F32 ESM_CONSTANT = 40.0;
  13. #endif
  14. // Fresnel term unreal
  15. Vec3 F_Unreal(Vec3 specular, F32 VoH)
  16. {
  17. return specular + (1.0 - specular) * pow(2.0, (-5.55473 * VoH - 6.98316) * VoH);
  18. }
  19. // Fresnel Schlick: "An Inexpensive BRDF Model for Physically-Based Rendering"
  20. // It has lower VGRPs than F_Unreal
  21. Vec3 F_Schlick(Vec3 specular, F32 VoH)
  22. {
  23. F32 a = 1.0 - VoH;
  24. F32 a2 = a * a;
  25. F32 a5 = a2 * a2 * a; // a5 = a^5
  26. return /*saturate(50.0 * specular.g) */ a5 + (1.0 - a5) * specular;
  27. }
  28. // D(n,h) aka NDF: GGX Trowbridge-Reitz
  29. F32 D_GGX(F32 roughness, F32 NoH)
  30. {
  31. F32 a = roughness * roughness;
  32. F32 a2 = a * a;
  33. F32 D = (NoH * a2 - NoH) * NoH + 1.0;
  34. return a2 / (PI * D * D);
  35. }
  36. // Visibility term: Geometric shadowing divided by BRDF denominator
  37. F32 V_Schlick(F32 roughness, F32 NoV, F32 NoL)
  38. {
  39. F32 k = (roughness * roughness) * 0.5;
  40. F32 Vis_SchlickV = NoV * (1.0 - k) + k;
  41. F32 Vis_SchlickL = NoL * (1.0 - k) + k;
  42. return 0.25 / (Vis_SchlickV * Vis_SchlickL);
  43. }
  44. Vec3 envBRDF(Vec3 specular, F32 roughness, sampler2D integrationLut, F32 NoV)
  45. {
  46. F32 a = roughness * roughness;
  47. F32 a2 = a * a;
  48. Vec2 envBRDF = textureLod(integrationLut, Vec2(a2, NoV), 0.0).xy;
  49. return specular * envBRDF.x + /*min(1.0, 50.0 * specular.g) */ envBRDF.y;
  50. }
  51. Vec3 diffuseLambert(Vec3 diffuse)
  52. {
  53. return diffuse * (1.0 / PI);
  54. }
  55. // Performs BRDF specular lighting
  56. Vec3 computeSpecularColorBrdf(GbufferInfo gbuffer, Vec3 viewDir, Vec3 frag2Light)
  57. {
  58. Vec3 H = normalize(frag2Light + viewDir);
  59. F32 NoL = max(EPSILON, dot(gbuffer.m_normal, frag2Light));
  60. F32 VoH = max(EPSILON, dot(viewDir, H));
  61. F32 NoH = max(EPSILON, dot(gbuffer.m_normal, H));
  62. F32 NoV = max(EPSILON, dot(gbuffer.m_normal, viewDir));
  63. // F
  64. #if 0
  65. Vec3 F = F_Unreal(gbuffer.m_specular, VoH);
  66. #else
  67. Vec3 F = F_Schlick(gbuffer.m_specular, VoH);
  68. #endif
  69. // D
  70. F32 D = D_GGX(gbuffer.m_roughness, NoH);
  71. // Vis
  72. F32 V = V_Schlick(gbuffer.m_roughness, NoV, NoL);
  73. return F * (V * D);
  74. }
  75. F32 computeSpotFactor(Vec3 l, F32 outerCos, F32 innerCos, Vec3 spotDir)
  76. {
  77. F32 costheta = -dot(l, spotDir);
  78. F32 spotFactor = smoothstep(outerCos, innerCos, costheta);
  79. return spotFactor;
  80. }
  81. U32 computeShadowSampleCount(const U32 COUNT, F32 zVSpace)
  82. {
  83. const F32 MAX_DISTANCE = 5.0;
  84. F32 z = max(zVSpace, -MAX_DISTANCE);
  85. F32 sampleCountf = F32(COUNT) + z * (F32(COUNT) / MAX_DISTANCE);
  86. sampleCountf = max(sampleCountf, 1.0);
  87. U32 sampleCount = U32(sampleCountf);
  88. return sampleCount;
  89. }
  90. F32 computeShadowFactorSpot(Mat4 lightProjectionMat, Vec3 worldPos, F32 distance, sampler2D spotMapArr)
  91. {
  92. Vec4 texCoords4 = lightProjectionMat * Vec4(worldPos, 1.0);
  93. Vec3 texCoords3 = texCoords4.xyz / texCoords4.w;
  94. const F32 near = LIGHT_FRUSTUM_NEAR_PLANE;
  95. const F32 far = distance;
  96. F32 linearDepth = linearizeDepth(texCoords3.z, near, far);
  97. F32 shadowFactor = textureLod(spotMapArr, texCoords3.xy, 0.0).r;
  98. return saturate(exp(ESM_CONSTANT * (shadowFactor - linearDepth)));
  99. }
  100. F32 computeShadowFactorOmni(Vec3 frag2Light, F32 radius, UVec2 atlasTiles, F32 tileSize, sampler2D shadowMap)
  101. {
  102. Vec3 dir = -frag2Light;
  103. Vec3 dirabs = abs(dir);
  104. F32 dist = max(dirabs.x, max(dirabs.y, dirabs.z));
  105. const F32 near = LIGHT_FRUSTUM_NEAR_PLANE;
  106. const F32 far = radius;
  107. F32 linearDepth = (dist - near) / (far - near);
  108. // Read tex
  109. F32 shadowFactor;
  110. {
  111. // Convert cube coords
  112. U32 faceIdxu;
  113. Vec2 uv = convertCubeUvsu(dir, faceIdxu);
  114. // Clamp uv to a small value to avoid reading from other tiles due to bilinear filtering. It's not a perfect
  115. // solution but it works
  116. uv = clamp(uv, Vec2(0.001), Vec2(1.0 - 0.001));
  117. // Compute atlas tile
  118. atlasTiles >>= UVec2(faceIdxu * 5u);
  119. atlasTiles &= UVec2(31u);
  120. // Compute UV
  121. uv = (uv + Vec2(atlasTiles)) * tileSize;
  122. // Sample
  123. shadowFactor = textureLod(shadowMap, uv, 0.0).r;
  124. }
  125. return saturate(exp(ESM_CONSTANT * (shadowFactor - linearDepth)));
  126. }
  127. // Compute the cubemap texture lookup vector given the reflection vector (r) the radius squared of the probe (R2) and
  128. // the frag pos in sphere space (f)
  129. Vec3 computeCubemapVecAccurate(in Vec3 r, in F32 R2, in Vec3 f)
  130. {
  131. // Compute the collision of the r to the inner part of the sphere
  132. // From now on we work on the sphere's space
  133. // Project the center of the sphere (it's zero now since we are in sphere space) in ray "f,r"
  134. Vec3 p = f - r * dot(f, r);
  135. // The collision to the sphere is point x where x = p + T * r
  136. // Because of the pythagorean theorem: R^2 = dot(p, p) + dot(T * r, T * r)
  137. // solving for T, T = R / |p|
  138. // then x becomes x = sqrt(R^2 - dot(p, p)) * r + p;
  139. F32 pp = dot(p, p);
  140. pp = min(pp, R2);
  141. F32 sq = sqrt(R2 - pp);
  142. Vec3 x = p + sq * r;
  143. return x;
  144. }
  145. // Cheap version of computeCubemapVecAccurate
  146. Vec3 computeCubemapVecCheap(in Vec3 r, in F32 R2, in Vec3 f)
  147. {
  148. return r;
  149. }
  150. F32 computeAttenuationFactor(F32 lightRadius, Vec3 frag2Light)
  151. {
  152. F32 fragLightDist = dot(frag2Light, frag2Light);
  153. F32 att = 1.0 - fragLightDist * lightRadius;
  154. att = max(0.0, att);
  155. return att * att;
  156. }
  157. // Given the probe properties trace a ray inside the probe and find the cube tex coordinates to sample
  158. Vec3 intersectProbe(Vec3 fragPos, // Ray origin
  159. Vec3 rayDir, // Ray direction
  160. Vec3 probeAabbMin,
  161. Vec3 probeAabbMax,
  162. Vec3 probeOrigin // Cubemap origin
  163. )
  164. {
  165. // Compute the intersection point
  166. F32 intresectionDist = rayAabbIntersectionInside(fragPos, rayDir, probeAabbMin, probeAabbMax);
  167. Vec3 intersectionPoint = fragPos + intresectionDist * rayDir;
  168. // Compute the cubemap vector
  169. return intersectionPoint - probeOrigin;
  170. }
  171. // Compute a weight (factor) of fragPos against some probe's bounds. The weight will be zero when fragPos is close to
  172. // AABB bounds and 1.0 at fadeDistance and less.
  173. F32 computeProbeBlendWeight(Vec3 fragPos, // Doesn't need to be inside the AABB
  174. Vec3 probeAabbMin,
  175. Vec3 probeAabbMax,
  176. F32 fadeDistance)
  177. {
  178. // Compute the min distance of fragPos from the edges of the AABB
  179. Vec3 distFromMin = fragPos - probeAabbMin;
  180. Vec3 distFromMax = probeAabbMax - fragPos;
  181. Vec3 minDistVec = min(distFromMin, distFromMax);
  182. F32 minDist = min(minDistVec.x, min(minDistVec.y, minDistVec.z));
  183. // Use saturate because minDist might be negative.
  184. return saturate(minDist / fadeDistance);
  185. }