LightFunctions.glsl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  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. //==============================================================================
  13. float computeAttenuationFactor(float lightRadius, vec3 frag2Light)
  14. {
  15. float fragLightDist = dot(frag2Light, frag2Light);
  16. float att = 1.0 - fragLightDist * lightRadius;
  17. att = max(0.0, att);
  18. return att * att;
  19. }
  20. //==============================================================================
  21. // Performs BRDF specular lighting
  22. vec3 computeSpecularColorBrdf(vec3 v, // view dir
  23. vec3 l, // light dir
  24. vec3 n, // normal
  25. vec3 specCol,
  26. vec3 lightSpecCol,
  27. float a2, // rougness^2
  28. float nol) // N dot L
  29. {
  30. vec3 h = normalize(l + v);
  31. // Fresnel
  32. float voh = max(EPSILON, dot(v, h));
  33. #if 0
  34. // Schlick
  35. vec3 F = specCol + (1.0 - specCol) * pow((1.0 + EPSILON - loh), 5.0);
  36. #else
  37. // Unreal
  38. vec3 F =
  39. specCol + (1.0 - specCol) * pow(2.0, (-5.55473 * voh - 6.98316) * voh);
  40. #endif
  41. // D(n,h) aka NDF: GGX Trowbridge-Reitz
  42. float noh = max(EPSILON, dot(n, h));
  43. float D = noh * noh * (a2 - 1.0) + 1.0;
  44. D = a2 / (PI * D * D);
  45. // G(l,v,h)/(4*dot(n,h)*dot(n,v)) aka Visibility term: Geometric shadowing
  46. // divided by BRDF denominator
  47. #if 0
  48. float nov = max(EPSILON, dot(n, v));
  49. float V_v = nov + sqrt((nov - nov * a2) * nov + a2);
  50. float V_l = nol + sqrt((nol - nol * a2) * nol + a2);
  51. float V = 1.0 / (V_l * V_v);
  52. #else
  53. float k = (a2 + 1.0);
  54. k = k * k / 8.0;
  55. float nov = max(EPSILON, dot(n, v));
  56. float V_v = nov * (1.0 - k) + k;
  57. float V_l = nol * (1.0 - k) + k;
  58. float V = 1.0 / (4.0 * V_l * V_v);
  59. #endif
  60. return F * (V * D) * lightSpecCol;
  61. }
  62. //==============================================================================
  63. vec3 computeDiffuseColor(vec3 diffCol, vec3 lightDiffCol)
  64. {
  65. return diffCol * lightDiffCol;
  66. }
  67. //==============================================================================
  68. float computeSpotFactor(vec3 l, float outerCos, float innerCos, vec3 spotDir)
  69. {
  70. float costheta = -dot(l, spotDir);
  71. float spotFactor = smoothstep(outerCos, innerCos, costheta);
  72. return spotFactor;
  73. }
  74. //==============================================================================
  75. uint computeShadowSampleCount(const uint COUNT, float zVSpace)
  76. {
  77. const float MAX_DISTANCE = 5.0;
  78. float z = max(zVSpace, -MAX_DISTANCE);
  79. float sampleCountf = float(COUNT) + z * (float(COUNT) / MAX_DISTANCE);
  80. sampleCountf = max(sampleCountf, 1.0);
  81. uint sampleCount = uint(sampleCountf);
  82. return sampleCount;
  83. }
  84. //==============================================================================
  85. float computeShadowFactorSpot(
  86. mat4 lightProjectionMat, vec3 fragPos, float layer, uint sampleCount)
  87. {
  88. vec4 texCoords4 = lightProjectionMat * vec4(fragPos, 1.0);
  89. vec3 texCoords3 = texCoords4.xyz / texCoords4.w;
  90. #if POISSON == 1
  91. const vec2 poissonDisk[SHADOW_SAMPLE_COUNT] =
  92. vec2[](vec2(0.751688, 0.619709) * 2.0 - 1.0,
  93. vec2(0.604741, 0.778485) * 2.0 - 1.0,
  94. vec2(0.936216, 0.463094) * 2.0 - 1.0,
  95. vec2(0.808758, 0.284966) * 2.0 - 1.0,
  96. vec2(0.812927, 0.786332) * 2.0 - 1.0,
  97. vec2(0.608651, 0.303919) * 2.0 - 1.0,
  98. vec2(0.482117, 0.573285) * 2.0 - 1.0,
  99. vec2(0.55819, 0.988451) * 2.0 - 1.0,
  100. vec2(0.340001, 0.728732) * 2.0 - 1.0,
  101. vec2(0.681775, 0.119789) * 2.0 - 1.0,
  102. vec2(0.217429, 0.522558) * 2.0 - 1.0,
  103. vec2(0.384257, 0.352163) * 2.0 - 1.0,
  104. vec2(0.143769, 0.738606) * 2.0 - 1.0,
  105. vec2(0.383474, 0.910019) * 2.0 - 1.0,
  106. vec2(0.409305, 0.177022) * 2.0 - 1.0,
  107. vec2(0.158647, 0.239097) * 2.0 - 1.0);
  108. float shadowFactor = 0.0;
  109. vec2 cordpart0 = vec2(layer, texCoords3.z);
  110. for(uint i = 0; i < sampleCount; i++)
  111. {
  112. vec2 cordpart1 = texCoords3.xy + poissonDisk[i] / 512.0;
  113. vec4 tcoord = vec4(cordpart1, cordpart0);
  114. shadowFactor += texture(u_spotMapArr, tcoord);
  115. }
  116. return shadowFactor / float(sampleCount);
  117. #else
  118. vec4 tcoord = vec4(texCoords3.x, texCoords3.y, layer, texCoords3.z);
  119. float shadowFactor = texture(u_spotMapArr, tcoord);
  120. return shadowFactor;
  121. #endif
  122. }
  123. //==============================================================================
  124. float computeShadowFactorOmni(vec3 frag2Light, float layer, float radius)
  125. {
  126. vec3 dir = (u_lightingUniforms.viewMat * vec4(-frag2Light, 1.0)).xyz;
  127. vec3 dirabs = abs(dir);
  128. float dist = -max(dirabs.x, max(dirabs.y, dirabs.z));
  129. dir = normalize(dir);
  130. const float near = OMNI_LIGHT_FRUSTUM_NEAR_PLANE;
  131. const float far = radius;
  132. // Original code:
  133. // float g = near - far;
  134. // float z = (far + near) / g * dist + (2.0 * far * near) / g;
  135. // float w = -dist;
  136. // z /= w;
  137. // z = z * 0.5 + 0.5;
  138. // Optimized:
  139. float z = (far * (dist + near)) / (dist * (far - near));
  140. float shadowFactor = texture(u_omniMapArr, vec4(dir, layer), z).r;
  141. return shadowFactor;
  142. }
  143. #endif