light.glsl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #ifndef _LIGHT_GLSL_
  2. #define _LIGHT_GLSL_
  3. #include "compiled.inc"
  4. #include "std/brdf.glsl"
  5. #include "std/math.glsl"
  6. #ifdef _ShadowMap
  7. #include "std/shadows.glsl"
  8. #endif
  9. #ifdef _VoxelAOvar
  10. #include "std/conetrace.glsl"
  11. #endif
  12. #ifdef _LTC
  13. #include "std/ltc.glsl"
  14. #endif
  15. #ifdef _LightIES
  16. #include "std/ies.glsl"
  17. #endif
  18. #ifdef _ShadowMap
  19. #ifdef _SinglePoint
  20. #ifdef _Spot
  21. uniform sampler2DShadow shadowMapSpot[1];
  22. uniform mat4 LWVPSpot0;
  23. #else
  24. uniform samplerCubeShadow shadowMapPoint[1];
  25. uniform vec2 lightProj;
  26. #endif
  27. #endif
  28. #ifdef _Clusters
  29. uniform samplerCubeShadow shadowMapPoint[4];
  30. uniform vec2 lightProj;
  31. #ifdef _Spot
  32. uniform sampler2DShadow shadowMapSpot[4];
  33. uniform mat4 LWVPSpot0;
  34. uniform mat4 LWVPSpot1;
  35. uniform mat4 LWVPSpot2;
  36. uniform mat4 LWVPSpot3;
  37. #endif
  38. #endif
  39. #endif
  40. #ifdef _LTC
  41. uniform vec3 lightArea0;
  42. uniform vec3 lightArea1;
  43. uniform vec3 lightArea2;
  44. uniform vec3 lightArea3;
  45. uniform sampler2D sltcMat;
  46. uniform sampler2D sltcMag;
  47. #ifdef _ShadowMap
  48. #ifndef _Spot
  49. #ifdef _SinglePoint
  50. uniform sampler2DShadow shadowMapSpot[1];
  51. uniform mat4 LWVPSpot0;
  52. #endif
  53. #ifdef _Clusters
  54. uniform sampler2DShadow shadowMapSpot[4];
  55. uniform mat4 LWVPSpot0;
  56. uniform mat4 LWVPSpot1;
  57. uniform mat4 LWVPSpot2;
  58. uniform mat4 LWVPSpot3;
  59. #endif
  60. #endif
  61. #endif
  62. #endif
  63. vec3 sampleLight(const vec3 p, const vec3 n, const vec3 v, const float dotNV, const vec3 lp, const vec3 lightCol,
  64. const vec3 albedo, const float rough, const float spec, const vec3 f0
  65. #ifdef _ShadowMap
  66. , int index, float bias
  67. #endif
  68. #ifdef _Spot
  69. , bool isSpot, float spotA, float spotB, vec3 spotDir
  70. #endif
  71. #ifdef _VoxelAOvar
  72. #ifdef _VoxelShadow
  73. , sampler3D voxels, vec3 voxpos
  74. #endif
  75. #endif
  76. #ifdef _MicroShadowing
  77. , float occ
  78. #endif
  79. ) {
  80. vec3 ld = lp - p;
  81. vec3 l = normalize(ld);
  82. vec3 h = normalize(v + l);
  83. float dotNH = dot(n, h);
  84. float dotVH = dot(v, h);
  85. float dotNL = dot(n, l);
  86. #ifdef _LTC
  87. float theta = acos(dotNV);
  88. vec2 tuv = vec2(rough, theta / (0.5 * PI));
  89. tuv = tuv * LUT_SCALE + LUT_BIAS;
  90. vec4 t = textureLod(sltcMat, tuv, 0.0);
  91. mat3 invM = mat3(
  92. vec3(1.0, 0.0, t.y),
  93. vec3(0.0, t.z, 0.0),
  94. vec3(t.w, 0.0, t.x));
  95. float ltcspec = ltcEvaluate(n, v, dotNV, p, invM, lightArea0, lightArea1, lightArea2, lightArea3);
  96. ltcspec *= textureLod(sltcMag, tuv, 0.0).a;
  97. float ltcdiff = ltcEvaluate(n, v, dotNV, p, mat3(1.0), lightArea0, lightArea1, lightArea2, lightArea3);
  98. vec3 direct = albedo * ltcdiff + ltcspec * spec * 0.05;
  99. #else
  100. vec3 direct = lambertDiffuseBRDF(albedo, dotNL) +
  101. specularBRDF(f0, rough, dotNL, dotNH, dotNV, dotVH) * spec;
  102. #endif
  103. direct *= attenuate(distance(p, lp));
  104. direct *= lightCol;
  105. #ifdef _MicroShadowing
  106. direct *= dotNL + 2.0 * occ * occ - 1.0;
  107. #endif
  108. #ifdef _VoxelAOvar
  109. #ifdef _VoxelShadow
  110. direct *= 1.0 - traceShadow(voxels, voxpos, l);
  111. #endif
  112. #endif
  113. #ifdef _LTC
  114. #ifdef _ShadowMap
  115. #ifdef _SinglePoint
  116. vec4 lPos = LWVPSpot0 * vec4(p + n * bias * 10, 1.0);
  117. direct *= shadowTest(shadowMapSpot[0], lPos.xyz / lPos.w, bias);
  118. #endif
  119. #ifdef _Clusters
  120. if (index == 0) {
  121. vec4 lPos = LWVPSpot0 * vec4(p + n * bias * 10, 1.0);
  122. direct *= shadowTest(shadowMapSpot[0], lPos.xyz / lPos.w, bias);
  123. }
  124. else if (index == 1) {
  125. vec4 lPos = LWVPSpot1 * vec4(p + n * bias * 10, 1.0);
  126. direct *= shadowTest(shadowMapSpot[1], lPos.xyz / lPos.w, bias);
  127. }
  128. else if (index == 2) {
  129. vec4 lPos = LWVPSpot2 * vec4(p + n * bias * 10, 1.0);
  130. direct *= shadowTest(shadowMapSpot[2], lPos.xyz / lPos.w, bias);
  131. }
  132. else if (index == 3) {
  133. vec4 lPos = LWVPSpot3 * vec4(p + n * bias * 10, 1.0);
  134. direct *= shadowTest(shadowMapSpot[3], lPos.xyz / lPos.w, bias);
  135. }
  136. #endif
  137. #endif
  138. return direct;
  139. #endif
  140. #ifdef _Spot
  141. if (isSpot) {
  142. float spotEffect = dot(spotDir, l); // lightDir
  143. // x - cutoff, y - cutoff - exponent
  144. if (spotEffect < spotA) {
  145. direct *= smoothstep(spotB, spotA, spotEffect);
  146. }
  147. #ifdef _ShadowMap
  148. #ifdef _SinglePoint
  149. vec4 lPos = LWVPSpot0 * vec4(p + n * bias * 10, 1.0);
  150. direct *= shadowTest(shadowMapSpot[0], lPos.xyz / lPos.w, bias);
  151. #endif
  152. #ifdef _Clusters
  153. if (index == 0) {
  154. vec4 lPos = LWVPSpot0 * vec4(p + n * bias * 10, 1.0);
  155. direct *= shadowTest(shadowMapSpot[0], lPos.xyz / lPos.w, bias);
  156. }
  157. else if (index == 1) {
  158. vec4 lPos = LWVPSpot1 * vec4(p + n * bias * 10, 1.0);
  159. direct *= shadowTest(shadowMapSpot[1], lPos.xyz / lPos.w, bias);
  160. }
  161. else if (index == 2) {
  162. vec4 lPos = LWVPSpot2 * vec4(p + n * bias * 10, 1.0);
  163. direct *= shadowTest(shadowMapSpot[2], lPos.xyz / lPos.w, bias);
  164. }
  165. else if (index == 3) {
  166. vec4 lPos = LWVPSpot3 * vec4(p + n * bias * 10, 1.0);
  167. direct *= shadowTest(shadowMapSpot[3], lPos.xyz / lPos.w, bias);
  168. }
  169. #endif
  170. #endif
  171. return direct;
  172. }
  173. #endif
  174. #ifdef _LightIES
  175. direct *= iesAttenuation(-l);
  176. #endif
  177. #ifdef _ShadowMap
  178. #ifdef _SinglePoint
  179. #ifndef _Spot
  180. direct *= PCFCube(shadowMapPoint[0], ld, -l, bias, lightProj, n);
  181. #endif
  182. #endif
  183. #ifdef _Clusters
  184. if (index == 0) direct *= PCFCube(shadowMapPoint[0], ld, -l, bias, lightProj, n);
  185. else if (index == 1) direct *= PCFCube(shadowMapPoint[1], ld, -l, bias, lightProj, n);
  186. else if (index == 2) direct *= PCFCube(shadowMapPoint[2], ld, -l, bias, lightProj, n);
  187. else if (index == 3) direct *= PCFCube(shadowMapPoint[3], ld, -l, bias, lightProj, n);
  188. #endif
  189. #endif
  190. return direct;
  191. }
  192. #endif