LightFunctions.glsl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright (C) 2009-2016, 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. #ifndef ANKI_SHADERS_LIGHT_FUNCTIONS_GLSL
  7. #define ANKI_SHADERS_LIGHT_FUNCTIONS_GLSL
  8. #include "shaders/Common.glsl"
  9. const float ATTENUATION_BOOST = 0.05;
  10. const float OMNI_LIGHT_FRUSTUM_NEAR_PLANE = 0.1 / 4.0;
  11. const uint SHADOW_SAMPLE_COUNT = 16;
  12. float computeAttenuationFactor(float lightRadius, vec3 frag2Light)
  13. {
  14. float fragLightDist = dot(frag2Light, frag2Light);
  15. float att = 1.0 - fragLightDist * lightRadius;
  16. att = max(0.0, att);
  17. return att * att;
  18. }
  19. // Performs BRDF specular lighting
  20. vec3 computeSpecularColorBrdf(vec3 v, // view dir
  21. vec3 l, // light dir
  22. vec3 n, // normal
  23. vec3 specCol,
  24. vec3 lightSpecCol,
  25. float a2, // rougness^2
  26. float nol) // N dot L
  27. {
  28. vec3 h = normalize(l + v);
  29. // Fresnel
  30. float voh = dot(v, h);
  31. #if 0
  32. // Schlick
  33. vec3 F = specCol + (1.0 - specCol) * pow((1.0 + EPSILON - loh), 5.0);
  34. #else
  35. // Unreal
  36. vec3 F = specCol + (1.0 - specCol) * pow(2.0, (-5.55473 * voh - 6.98316) * voh);
  37. #endif
  38. // D(n,h) aka NDF: GGX Trowbridge-Reitz
  39. float noh = dot(n, h);
  40. float D = noh * noh * (a2 - 1.0) + 1.0;
  41. D = a2 / (PI * D * D);
  42. D = clamp(D, EPSILON, 100.0); // Limit that because it may grow
  43. // G(l,v,h)/(4*dot(n,h)*dot(n,v)) aka Visibility term: Geometric shadowing divided by BRDF denominator
  44. #if 0
  45. float nov = max(EPSILON, dot(n, v));
  46. float V_v = nov + sqrt((nov - nov * a2) * nov + a2);
  47. float V_l = nol + sqrt((nol - nol * a2) * nol + a2);
  48. float V = 1.0 / (V_l * V_v);
  49. #else
  50. float k = (a2 + 1.0);
  51. k = k * k / 8.0;
  52. float nov = max(EPSILON, dot(n, v));
  53. float V_v = nov * (1.0 - k) + k;
  54. float V_l = nol * (1.0 - k) + k;
  55. float V = 1.0 / (4.0 * V_l * V_v);
  56. #endif
  57. return F * (V * D) * lightSpecCol;
  58. }
  59. vec3 computeDiffuseColor(vec3 diffCol, vec3 lightDiffCol)
  60. {
  61. return diffCol * lightDiffCol;
  62. }
  63. float computeSpotFactor(vec3 l, float outerCos, float innerCos, vec3 spotDir)
  64. {
  65. float costheta = -dot(l, spotDir);
  66. float spotFactor = smoothstep(outerCos, innerCos, costheta);
  67. return spotFactor;
  68. }
  69. uint computeShadowSampleCount(const uint COUNT, float zVSpace)
  70. {
  71. const float MAX_DISTANCE = 5.0;
  72. float z = max(zVSpace, -MAX_DISTANCE);
  73. float sampleCountf = float(COUNT) + z * (float(COUNT) / MAX_DISTANCE);
  74. sampleCountf = max(sampleCountf, 1.0);
  75. uint sampleCount = uint(sampleCountf);
  76. return sampleCount;
  77. }
  78. float computeShadowFactorSpot(
  79. mat4 lightProjectionMat, vec3 fragPos, float layer, uint sampleCount, sampler2DArrayShadow spotMapArr)
  80. {
  81. vec4 texCoords4 = lightProjectionMat * vec4(fragPos, 1.0);
  82. vec3 texCoords3 = texCoords4.xyz / texCoords4.w;
  83. #if POISSON == 1
  84. const vec2 poissonDisk[SHADOW_SAMPLE_COUNT] = vec2[](vec2(0.751688, 0.619709) * 2.0 - 1.0,
  85. vec2(0.604741, 0.778485) * 2.0 - 1.0,
  86. vec2(0.936216, 0.463094) * 2.0 - 1.0,
  87. vec2(0.808758, 0.284966) * 2.0 - 1.0,
  88. vec2(0.812927, 0.786332) * 2.0 - 1.0,
  89. vec2(0.608651, 0.303919) * 2.0 - 1.0,
  90. vec2(0.482117, 0.573285) * 2.0 - 1.0,
  91. vec2(0.55819, 0.988451) * 2.0 - 1.0,
  92. vec2(0.340001, 0.728732) * 2.0 - 1.0,
  93. vec2(0.681775, 0.119789) * 2.0 - 1.0,
  94. vec2(0.217429, 0.522558) * 2.0 - 1.0,
  95. vec2(0.384257, 0.352163) * 2.0 - 1.0,
  96. vec2(0.143769, 0.738606) * 2.0 - 1.0,
  97. vec2(0.383474, 0.910019) * 2.0 - 1.0,
  98. vec2(0.409305, 0.177022) * 2.0 - 1.0,
  99. vec2(0.158647, 0.239097) * 2.0 - 1.0);
  100. float shadowFactor = 0.0;
  101. vec2 cordpart0 = vec2(layer, texCoords3.z);
  102. for(uint i = 0; i < sampleCount; i++)
  103. {
  104. vec2 cordpart1 = texCoords3.xy + poissonDisk[i] / 512.0;
  105. vec4 tcoord = vec4(cordpart1, cordpart0);
  106. shadowFactor += texture(spotMapArr, tcoord);
  107. }
  108. return shadowFactor / float(sampleCount);
  109. #else
  110. vec4 tcoord = vec4(texCoords3.x, texCoords3.y, layer, texCoords3.z);
  111. float shadowFactor = texture(spotMapArr, tcoord);
  112. return shadowFactor;
  113. #endif
  114. }
  115. float computeShadowFactorOmni(
  116. in vec3 frag2Light, in float layer, in float radius, in mat4 viewMat, in samplerCubeArrayShadow omniMapArr)
  117. {
  118. vec3 dir = (viewMat * vec4(-frag2Light, 1.0)).xyz;
  119. vec3 dirabs = abs(dir);
  120. float dist = -max(dirabs.x, max(dirabs.y, dirabs.z));
  121. dir = normalize(dir);
  122. const float near = OMNI_LIGHT_FRUSTUM_NEAR_PLANE;
  123. const float far = radius;
  124. // Original code:
  125. // float g = near - far;
  126. // float z = (far + near) / g * dist + (2.0 * far * near) / g;
  127. // float w = -dist;
  128. // z /= w;
  129. // z = z * 0.5 + 0.5;
  130. // Optimized:
  131. float z = (far * (dist + near)) / (dist * (far - near));
  132. float shadowFactor = texture(omniMapArr, vec4(dir, layer), z).r;
  133. return shadowFactor;
  134. }
  135. // Compute the cubemap texture lookup vector given the reflection vector (r) the radius squared of the probe (R2) and
  136. // the frag pos in sphere space (f)
  137. vec3 computeCubemapVecAccurate(in vec3 r, in float R2, in vec3 f, in mat3 invViewRotation)
  138. {
  139. // Compute the collision of the r to the inner part of the sphere
  140. // From now on we work on the sphere's space
  141. // Project the center of the sphere (it's zero now since we are in sphere
  142. // space) in ray "f,r"
  143. vec3 p = f - r * dot(f, r);
  144. // The collision to the sphere is point x where x = p + T * r
  145. // Because of the pythagorean theorem: R^2 = dot(p, p) + dot(T * r, T * r)
  146. // solving for T, T = R / |p|
  147. // then x becomes x = sqrt(R^2 - dot(p, p)) * r + p;
  148. float pp = dot(p, p);
  149. pp = min(pp, R2);
  150. float sq = sqrt(R2 - pp);
  151. vec3 x = p + sq * r;
  152. // Rotate UV to move it to world space
  153. vec3 uv = invViewRotation * x;
  154. return uv;
  155. }
  156. // Cheap version of computeCubemapVecAccurate
  157. vec3 computeCubemapVecCheap(in vec3 r, in float R2, in vec3 f, in mat3 invViewRotation)
  158. {
  159. return invViewRotation * r;
  160. }
  161. #endif