LightFunctions.glsl 5.3 KB

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