light.glsl 5.2 KB

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