scene_forward_lights_inc.glsl 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. // Functions related to lighting
  2. float D_GGX(float cos_theta_m, float alpha) {
  3. float a = cos_theta_m * alpha;
  4. float k = alpha / (1.0 - cos_theta_m * cos_theta_m + a * a);
  5. return k * k * (1.0 / M_PI);
  6. }
  7. // From Earl Hammon, Jr. "PBR Diffuse Lighting for GGX+Smith Microsurfaces" https://www.gdcvault.com/play/1024478/PBR-Diffuse-Lighting-for-GGX
  8. float V_GGX(float NdotL, float NdotV, float alpha) {
  9. return 0.5 / mix(2.0 * NdotL * NdotV, NdotL + NdotV, alpha);
  10. }
  11. float D_GGX_anisotropic(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) {
  12. float alpha2 = alpha_x * alpha_y;
  13. highp vec3 v = vec3(alpha_y * cos_phi, alpha_x * sin_phi, alpha2 * cos_theta_m);
  14. highp float v2 = dot(v, v);
  15. float w2 = alpha2 / v2;
  16. float D = alpha2 * w2 * w2 * (1.0 / M_PI);
  17. return D;
  18. }
  19. float V_GGX_anisotropic(float alpha_x, float alpha_y, float TdotV, float TdotL, float BdotV, float BdotL, float NdotV, float NdotL) {
  20. float Lambda_V = NdotL * length(vec3(alpha_x * TdotV, alpha_y * BdotV, NdotV));
  21. float Lambda_L = NdotV * length(vec3(alpha_x * TdotL, alpha_y * BdotL, NdotL));
  22. return 0.5 / (Lambda_V + Lambda_L);
  23. }
  24. float SchlickFresnel(float u) {
  25. float m = 1.0 - u;
  26. float m2 = m * m;
  27. return m2 * m2 * m; // pow(m,5)
  28. }
  29. vec3 F0(float metallic, float specular, vec3 albedo) {
  30. float dielectric = 0.16 * specular * specular;
  31. // use albedo * metallic as colored specular reflectance at 0 angle for metallic materials;
  32. // see https://google.github.io/filament/Filament.md.html
  33. return mix(vec3(dielectric), albedo, vec3(metallic));
  34. }
  35. void light_compute(vec3 N, vec3 L, vec3 V, float A, vec3 light_color, float attenuation, vec3 f0, uint orms, float specular_amount, vec3 albedo, inout float alpha,
  36. #ifdef LIGHT_BACKLIGHT_USED
  37. vec3 backlight,
  38. #endif
  39. #ifdef LIGHT_TRANSMITTANCE_USED
  40. vec4 transmittance_color,
  41. float transmittance_depth,
  42. float transmittance_boost,
  43. float transmittance_z,
  44. #endif
  45. #ifdef LIGHT_RIM_USED
  46. float rim, float rim_tint,
  47. #endif
  48. #ifdef LIGHT_CLEARCOAT_USED
  49. float clearcoat, float clearcoat_roughness, vec3 vertex_normal,
  50. #endif
  51. #ifdef LIGHT_ANISOTROPY_USED
  52. vec3 B, vec3 T, float anisotropy,
  53. #endif
  54. inout vec3 diffuse_light, inout vec3 specular_light) {
  55. vec4 orms_unpacked = unpackUnorm4x8(orms);
  56. float roughness = orms_unpacked.y;
  57. float metallic = orms_unpacked.z;
  58. #if defined(LIGHT_CODE_USED)
  59. // light is written by the light shader
  60. vec3 normal = N;
  61. vec3 light = L;
  62. vec3 view = V;
  63. #CODE : LIGHT
  64. #else
  65. float NdotL = min(A + dot(N, L), 1.0);
  66. float cNdotL = max(NdotL, 0.0); // clamped NdotL
  67. float NdotV = dot(N, V);
  68. float cNdotV = max(NdotV, 1e-4);
  69. #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED)
  70. vec3 H = normalize(V + L);
  71. #endif
  72. #if defined(SPECULAR_SCHLICK_GGX)
  73. float cNdotH = clamp(A + dot(N, H), 0.0, 1.0);
  74. #endif
  75. #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED)
  76. float cLdotH = clamp(A + dot(L, H), 0.0, 1.0);
  77. #endif
  78. if (metallic < 1.0) {
  79. float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance
  80. #if defined(DIFFUSE_LAMBERT_WRAP)
  81. // Energy conserving lambert wrap shader.
  82. // https://web.archive.org/web/20210228210901/http://blog.stevemcauley.com/2011/12/03/energy-conserving-wrapped-diffuse/
  83. diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness))) * (1.0 / M_PI);
  84. #elif defined(DIFFUSE_TOON)
  85. diffuse_brdf_NL = smoothstep(-roughness, max(roughness, 0.01), NdotL) * (1.0 / M_PI);
  86. #elif defined(DIFFUSE_BURLEY)
  87. {
  88. float FD90_minus_1 = 2.0 * cLdotH * cLdotH * roughness - 0.5;
  89. float FdV = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotV);
  90. float FdL = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotL);
  91. diffuse_brdf_NL = (1.0 / M_PI) * FdV * FdL * cNdotL;
  92. /*
  93. float energyBias = mix(roughness, 0.0, 0.5);
  94. float energyFactor = mix(roughness, 1.0, 1.0 / 1.51);
  95. float fd90 = energyBias + 2.0 * VoH * VoH * roughness;
  96. float f0 = 1.0;
  97. float lightScatter = f0 + (fd90 - f0) * pow(1.0 - cNdotL, 5.0);
  98. float viewScatter = f0 + (fd90 - f0) * pow(1.0 - cNdotV, 5.0);
  99. diffuse_brdf_NL = lightScatter * viewScatter * energyFactor;
  100. */
  101. }
  102. #else
  103. // lambert
  104. diffuse_brdf_NL = cNdotL * (1.0 / M_PI);
  105. #endif
  106. diffuse_light += light_color * diffuse_brdf_NL * attenuation;
  107. #if defined(LIGHT_BACKLIGHT_USED)
  108. diffuse_light += light_color * (vec3(1.0 / M_PI) - diffuse_brdf_NL) * backlight * attenuation;
  109. #endif
  110. #if defined(LIGHT_RIM_USED)
  111. // Epsilon min to prevent pow(0, 0) singularity which results in undefined behavior.
  112. float rim_light = pow(max(1e-4, 1.0 - cNdotV), max(0.0, (1.0 - roughness) * 16.0));
  113. diffuse_light += rim_light * rim * mix(vec3(1.0), albedo, rim_tint) * light_color;
  114. #endif
  115. #ifdef LIGHT_TRANSMITTANCE_USED
  116. {
  117. #ifdef SSS_MODE_SKIN
  118. float scale = 8.25 / transmittance_depth;
  119. float d = scale * abs(transmittance_z);
  120. float dd = -d * d;
  121. vec3 profile = vec3(0.233, 0.455, 0.649) * exp(dd / 0.0064) +
  122. vec3(0.1, 0.336, 0.344) * exp(dd / 0.0484) +
  123. vec3(0.118, 0.198, 0.0) * exp(dd / 0.187) +
  124. vec3(0.113, 0.007, 0.007) * exp(dd / 0.567) +
  125. vec3(0.358, 0.004, 0.0) * exp(dd / 1.99) +
  126. vec3(0.078, 0.0, 0.0) * exp(dd / 7.41);
  127. diffuse_light += profile * transmittance_color.a * light_color * clamp(transmittance_boost - NdotL, 0.0, 1.0) * (1.0 / M_PI);
  128. #else
  129. float scale = 8.25 / transmittance_depth;
  130. float d = scale * abs(transmittance_z);
  131. float dd = -d * d;
  132. diffuse_light += exp(dd) * transmittance_color.rgb * transmittance_color.a * light_color * clamp(transmittance_boost - NdotL, 0.0, 1.0) * (1.0 / M_PI);
  133. #endif
  134. }
  135. #else
  136. #endif //LIGHT_TRANSMITTANCE_USED
  137. }
  138. if (roughness > 0.0) { // FIXME: roughness == 0 should not disable specular light entirely
  139. // D
  140. #if defined(SPECULAR_TOON)
  141. vec3 R = normalize(-reflect(L, N));
  142. float RdotV = dot(R, V);
  143. float mid = 1.0 - roughness;
  144. mid *= mid;
  145. float intensity = smoothstep(mid - roughness * 0.5, mid + roughness * 0.5, RdotV) * mid;
  146. diffuse_light += light_color * intensity * attenuation * specular_amount; // write to diffuse_light, as in toon shading you generally want no reflection
  147. #elif defined(SPECULAR_DISABLED)
  148. // none..
  149. #elif defined(SPECULAR_SCHLICK_GGX)
  150. // shlick+ggx as default
  151. float alpha_ggx = roughness * roughness;
  152. #if defined(LIGHT_ANISOTROPY_USED)
  153. float aspect = sqrt(1.0 - anisotropy * 0.9);
  154. float ax = alpha_ggx / aspect;
  155. float ay = alpha_ggx * aspect;
  156. float XdotH = dot(T, H);
  157. float YdotH = dot(B, H);
  158. float D = D_GGX_anisotropic(cNdotH, ax, ay, XdotH, YdotH);
  159. float G = V_GGX_anisotropic(ax, ay, dot(T, V), dot(T, L), dot(B, V), dot(B, L), cNdotV, cNdotL);
  160. #else // LIGHT_ANISOTROPY_USED
  161. float D = D_GGX(cNdotH, alpha_ggx);
  162. float G = V_GGX(cNdotL, cNdotV, alpha_ggx);
  163. #endif // LIGHT_ANISOTROPY_USED
  164. // F
  165. float cLdotH5 = SchlickFresnel(cLdotH);
  166. // Calculate Fresnel using specular occlusion term from Filament:
  167. // https://google.github.io/filament/Filament.html#lighting/occlusion/specularocclusion
  168. float f90 = clamp(dot(f0, vec3(50.0 * 0.33)), metallic, 1.0);
  169. vec3 F = f0 + (f90 - f0) * cLdotH5;
  170. vec3 specular_brdf_NL = cNdotL * D * F * G;
  171. specular_light += specular_brdf_NL * light_color * attenuation * specular_amount;
  172. #endif
  173. #if defined(LIGHT_CLEARCOAT_USED)
  174. // Clearcoat ignores normal_map, use vertex normal instead
  175. float ccNdotL = max(min(A + dot(vertex_normal, L), 1.0), 0.0);
  176. float ccNdotH = clamp(A + dot(vertex_normal, H), 0.0, 1.0);
  177. float ccNdotV = max(dot(vertex_normal, V), 1e-4);
  178. #if !defined(SPECULAR_SCHLICK_GGX)
  179. float cLdotH5 = SchlickFresnel(cLdotH);
  180. #endif
  181. float Dr = D_GGX(ccNdotH, mix(0.001, 0.1, clearcoat_roughness));
  182. float Gr = 0.25 / (cLdotH * cLdotH);
  183. float Fr = mix(.04, 1.0, cLdotH5);
  184. float clearcoat_specular_brdf_NL = clearcoat * Gr * Fr * Dr * cNdotL;
  185. specular_light += clearcoat_specular_brdf_NL * light_color * attenuation * specular_amount;
  186. // TODO: Clearcoat adds light to the scene right now (it is non-energy conserving), both diffuse and specular need to be scaled by (1.0 - FR)
  187. // but to do so we need to rearrange this entire function
  188. #endif // LIGHT_CLEARCOAT_USED
  189. }
  190. #ifdef USE_SHADOW_TO_OPACITY
  191. alpha = min(alpha, clamp(1.0 - attenuation, 0.0, 1.0));
  192. #endif
  193. #endif //defined(LIGHT_CODE_USED)
  194. }
  195. #ifndef SHADOWS_DISABLED
  196. // Interleaved Gradient Noise
  197. // https://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare
  198. float quick_hash(vec2 pos) {
  199. const vec3 magic = vec3(0.06711056f, 0.00583715f, 52.9829189f);
  200. return fract(magic.z * fract(dot(pos, magic.xy)));
  201. }
  202. float sample_directional_pcf_shadow(texture2D shadow, vec2 shadow_pixel_size, vec4 coord) {
  203. vec2 pos = coord.xy;
  204. float depth = coord.z;
  205. //if only one sample is taken, take it from the center
  206. if (sc_directional_soft_shadow_samples == 0) {
  207. return textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(pos, depth, 1.0));
  208. }
  209. mat2 disk_rotation;
  210. {
  211. float r = quick_hash(gl_FragCoord.xy) * 2.0 * M_PI;
  212. float sr = sin(r);
  213. float cr = cos(r);
  214. disk_rotation = mat2(vec2(cr, -sr), vec2(sr, cr));
  215. }
  216. float avg = 0.0;
  217. for (uint i = 0; i < sc_directional_soft_shadow_samples; i++) {
  218. avg += textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(pos + shadow_pixel_size * (disk_rotation * scene_data_block.data.directional_soft_shadow_kernel[i].xy), depth, 1.0));
  219. }
  220. return avg * (1.0 / float(sc_directional_soft_shadow_samples));
  221. }
  222. float sample_pcf_shadow(texture2D shadow, vec2 shadow_pixel_size, vec3 coord) {
  223. vec2 pos = coord.xy;
  224. float depth = coord.z;
  225. //if only one sample is taken, take it from the center
  226. if (sc_soft_shadow_samples == 0) {
  227. return textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(pos, depth, 1.0));
  228. }
  229. mat2 disk_rotation;
  230. {
  231. float r = quick_hash(gl_FragCoord.xy) * 2.0 * M_PI;
  232. float sr = sin(r);
  233. float cr = cos(r);
  234. disk_rotation = mat2(vec2(cr, -sr), vec2(sr, cr));
  235. }
  236. float avg = 0.0;
  237. for (uint i = 0; i < sc_soft_shadow_samples; i++) {
  238. avg += textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(pos + shadow_pixel_size * (disk_rotation * scene_data_block.data.soft_shadow_kernel[i].xy), depth, 1.0));
  239. }
  240. return avg * (1.0 / float(sc_soft_shadow_samples));
  241. }
  242. float sample_omni_pcf_shadow(texture2D shadow, float blur_scale, vec2 coord, vec4 uv_rect, vec2 flip_offset, float depth) {
  243. //if only one sample is taken, take it from the center
  244. if (sc_soft_shadow_samples == 0) {
  245. vec2 pos = coord * 0.5 + 0.5;
  246. pos = uv_rect.xy + pos * uv_rect.zw;
  247. return textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(pos, depth, 1.0));
  248. }
  249. mat2 disk_rotation;
  250. {
  251. float r = quick_hash(gl_FragCoord.xy) * 2.0 * M_PI;
  252. float sr = sin(r);
  253. float cr = cos(r);
  254. disk_rotation = mat2(vec2(cr, -sr), vec2(sr, cr));
  255. }
  256. float avg = 0.0;
  257. vec2 offset_scale = blur_scale * 2.0 * scene_data_block.data.shadow_atlas_pixel_size / uv_rect.zw;
  258. for (uint i = 0; i < sc_soft_shadow_samples; i++) {
  259. vec2 offset = offset_scale * (disk_rotation * scene_data_block.data.soft_shadow_kernel[i].xy);
  260. vec2 sample_coord = coord + offset;
  261. float sample_coord_length_sqaured = dot(sample_coord, sample_coord);
  262. bool do_flip = sample_coord_length_sqaured > 1.0;
  263. if (do_flip) {
  264. float len = sqrt(sample_coord_length_sqaured);
  265. sample_coord = sample_coord * (2.0 / len - 1.0);
  266. }
  267. sample_coord = sample_coord * 0.5 + 0.5;
  268. sample_coord = uv_rect.xy + sample_coord * uv_rect.zw;
  269. if (do_flip) {
  270. sample_coord += flip_offset;
  271. }
  272. avg += textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(sample_coord, depth, 1.0));
  273. }
  274. return avg * (1.0 / float(sc_soft_shadow_samples));
  275. }
  276. float sample_directional_soft_shadow(texture2D shadow, vec3 pssm_coord, vec2 tex_scale) {
  277. //find blocker
  278. float blocker_count = 0.0;
  279. float blocker_average = 0.0;
  280. mat2 disk_rotation;
  281. {
  282. float r = quick_hash(gl_FragCoord.xy) * 2.0 * M_PI;
  283. float sr = sin(r);
  284. float cr = cos(r);
  285. disk_rotation = mat2(vec2(cr, -sr), vec2(sr, cr));
  286. }
  287. for (uint i = 0; i < sc_directional_penumbra_shadow_samples; i++) {
  288. vec2 suv = pssm_coord.xy + (disk_rotation * scene_data_block.data.directional_penumbra_shadow_kernel[i].xy) * tex_scale;
  289. float d = textureLod(sampler2D(shadow, material_samplers[SAMPLER_LINEAR_CLAMP]), suv, 0.0).r;
  290. if (d < pssm_coord.z) {
  291. blocker_average += d;
  292. blocker_count += 1.0;
  293. }
  294. }
  295. if (blocker_count > 0.0) {
  296. //blockers found, do soft shadow
  297. blocker_average /= blocker_count;
  298. float penumbra = (pssm_coord.z - blocker_average) / blocker_average;
  299. tex_scale *= penumbra;
  300. float s = 0.0;
  301. for (uint i = 0; i < sc_directional_penumbra_shadow_samples; i++) {
  302. vec2 suv = pssm_coord.xy + (disk_rotation * scene_data_block.data.directional_penumbra_shadow_kernel[i].xy) * tex_scale;
  303. s += textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(suv, pssm_coord.z, 1.0));
  304. }
  305. return s / float(sc_directional_penumbra_shadow_samples);
  306. } else {
  307. //no blockers found, so no shadow
  308. return 1.0;
  309. }
  310. }
  311. #endif // SHADOWS_DISABLED
  312. float get_omni_attenuation(float distance, float inv_range, float decay) {
  313. float nd = distance * inv_range;
  314. nd *= nd;
  315. nd *= nd; // nd^4
  316. nd = max(1.0 - nd, 0.0);
  317. nd *= nd; // nd^2
  318. return nd * pow(max(distance, 0.0001), -decay);
  319. }
  320. float light_process_omni_shadow(uint idx, vec3 vertex, vec3 normal) {
  321. #ifndef SHADOWS_DISABLED
  322. if (omni_lights.data[idx].shadow_opacity > 0.001) {
  323. // there is a shadowmap
  324. vec2 texel_size = scene_data_block.data.shadow_atlas_pixel_size;
  325. vec4 base_uv_rect = omni_lights.data[idx].atlas_rect;
  326. base_uv_rect.xy += texel_size;
  327. base_uv_rect.zw -= texel_size * 2.0;
  328. // Omni lights use direction.xy to store to store the offset between the two paraboloid regions
  329. vec2 flip_offset = omni_lights.data[idx].direction.xy;
  330. vec3 local_vert = (omni_lights.data[idx].shadow_matrix * vec4(vertex, 1.0)).xyz;
  331. float shadow_len = length(local_vert); //need to remember shadow len from here
  332. vec3 shadow_dir = normalize(local_vert);
  333. vec3 local_normal = normalize(mat3(omni_lights.data[idx].shadow_matrix) * normal);
  334. vec3 normal_bias = local_normal * omni_lights.data[idx].shadow_normal_bias * (1.0 - abs(dot(local_normal, shadow_dir)));
  335. float shadow;
  336. if (sc_use_light_soft_shadows && omni_lights.data[idx].soft_shadow_size > 0.0) {
  337. //soft shadow
  338. //find blocker
  339. float blocker_count = 0.0;
  340. float blocker_average = 0.0;
  341. mat2 disk_rotation;
  342. {
  343. float r = quick_hash(gl_FragCoord.xy) * 2.0 * M_PI;
  344. float sr = sin(r);
  345. float cr = cos(r);
  346. disk_rotation = mat2(vec2(cr, -sr), vec2(sr, cr));
  347. }
  348. vec3 basis_normal = shadow_dir;
  349. vec3 v0 = abs(basis_normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0);
  350. vec3 tangent = normalize(cross(v0, basis_normal));
  351. vec3 bitangent = normalize(cross(tangent, basis_normal));
  352. float z_norm = shadow_len * omni_lights.data[idx].inv_radius;
  353. tangent *= omni_lights.data[idx].soft_shadow_size * omni_lights.data[idx].soft_shadow_scale;
  354. bitangent *= omni_lights.data[idx].soft_shadow_size * omni_lights.data[idx].soft_shadow_scale;
  355. for (uint i = 0; i < sc_penumbra_shadow_samples; i++) {
  356. vec2 disk = disk_rotation * scene_data_block.data.penumbra_shadow_kernel[i].xy;
  357. vec3 pos = local_vert + tangent * disk.x + bitangent * disk.y;
  358. pos = normalize(pos);
  359. vec4 uv_rect = base_uv_rect;
  360. if (pos.z >= 0.0) {
  361. uv_rect.xy += flip_offset;
  362. }
  363. pos.z = 1.0 + abs(pos.z);
  364. pos.xy /= pos.z;
  365. pos.xy = pos.xy * 0.5 + 0.5;
  366. pos.xy = uv_rect.xy + pos.xy * uv_rect.zw;
  367. float d = textureLod(sampler2D(shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), pos.xy, 0.0).r;
  368. if (d < z_norm) {
  369. blocker_average += d;
  370. blocker_count += 1.0;
  371. }
  372. }
  373. if (blocker_count > 0.0) {
  374. //blockers found, do soft shadow
  375. blocker_average /= blocker_count;
  376. float penumbra = (z_norm - blocker_average) / blocker_average;
  377. tangent *= penumbra;
  378. bitangent *= penumbra;
  379. z_norm -= omni_lights.data[idx].inv_radius * omni_lights.data[idx].shadow_bias;
  380. shadow = 0.0;
  381. for (uint i = 0; i < sc_penumbra_shadow_samples; i++) {
  382. vec2 disk = disk_rotation * scene_data_block.data.penumbra_shadow_kernel[i].xy;
  383. vec3 pos = local_vert + tangent * disk.x + bitangent * disk.y;
  384. pos = normalize(pos);
  385. pos = normalize(pos + normal_bias);
  386. vec4 uv_rect = base_uv_rect;
  387. if (pos.z >= 0.0) {
  388. uv_rect.xy += flip_offset;
  389. }
  390. pos.z = 1.0 + abs(pos.z);
  391. pos.xy /= pos.z;
  392. pos.xy = pos.xy * 0.5 + 0.5;
  393. pos.xy = uv_rect.xy + pos.xy * uv_rect.zw;
  394. shadow += textureProj(sampler2DShadow(shadow_atlas, shadow_sampler), vec4(pos.xy, z_norm, 1.0));
  395. }
  396. shadow /= float(sc_penumbra_shadow_samples);
  397. shadow = mix(1.0, shadow, omni_lights.data[idx].shadow_opacity);
  398. } else {
  399. //no blockers found, so no shadow
  400. shadow = 1.0;
  401. }
  402. } else {
  403. vec4 uv_rect = base_uv_rect;
  404. vec3 shadow_sample = normalize(shadow_dir + normal_bias);
  405. if (shadow_sample.z >= 0.0) {
  406. uv_rect.xy += flip_offset;
  407. flip_offset *= -1.0;
  408. }
  409. shadow_sample.z = 1.0 + abs(shadow_sample.z);
  410. vec2 pos = shadow_sample.xy / shadow_sample.z;
  411. float depth = shadow_len - omni_lights.data[idx].shadow_bias;
  412. depth *= omni_lights.data[idx].inv_radius;
  413. shadow = mix(1.0, sample_omni_pcf_shadow(shadow_atlas, omni_lights.data[idx].soft_shadow_scale / shadow_sample.z, pos, uv_rect, flip_offset, depth), omni_lights.data[idx].shadow_opacity);
  414. }
  415. return shadow;
  416. }
  417. #endif
  418. return 1.0;
  419. }
  420. void light_process_omni(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 vertex_ddx, vec3 vertex_ddy, vec3 f0, uint orms, float shadow, vec3 albedo, inout float alpha,
  421. #ifdef LIGHT_BACKLIGHT_USED
  422. vec3 backlight,
  423. #endif
  424. #ifdef LIGHT_TRANSMITTANCE_USED
  425. vec4 transmittance_color,
  426. float transmittance_depth,
  427. float transmittance_boost,
  428. #endif
  429. #ifdef LIGHT_RIM_USED
  430. float rim, float rim_tint,
  431. #endif
  432. #ifdef LIGHT_CLEARCOAT_USED
  433. float clearcoat, float clearcoat_roughness, vec3 vertex_normal,
  434. #endif
  435. #ifdef LIGHT_ANISOTROPY_USED
  436. vec3 binormal, vec3 tangent, float anisotropy,
  437. #endif
  438. inout vec3 diffuse_light, inout vec3 specular_light) {
  439. vec3 light_rel_vec = omni_lights.data[idx].position - vertex;
  440. float light_length = length(light_rel_vec);
  441. float omni_attenuation = get_omni_attenuation(light_length, omni_lights.data[idx].inv_radius, omni_lights.data[idx].attenuation);
  442. float light_attenuation = omni_attenuation;
  443. vec3 color = omni_lights.data[idx].color;
  444. float size_A = 0.0;
  445. if (sc_use_light_soft_shadows && omni_lights.data[idx].size > 0.0) {
  446. float t = omni_lights.data[idx].size / max(0.001, light_length);
  447. size_A = max(0.0, 1.0 - 1 / sqrt(1 + t * t));
  448. }
  449. #ifdef LIGHT_TRANSMITTANCE_USED
  450. float transmittance_z = transmittance_depth; //no transmittance by default
  451. transmittance_color.a *= light_attenuation;
  452. {
  453. vec4 clamp_rect = omni_lights.data[idx].atlas_rect;
  454. //redo shadowmapping, but shrink the model a bit to avoid arctifacts
  455. vec4 splane = (omni_lights.data[idx].shadow_matrix * vec4(vertex - normalize(normal_interp) * omni_lights.data[idx].transmittance_bias, 1.0));
  456. float shadow_len = length(splane.xyz);
  457. splane.xyz = normalize(splane.xyz);
  458. if (splane.z >= 0.0) {
  459. splane.z += 1.0;
  460. clamp_rect.y += clamp_rect.w;
  461. } else {
  462. splane.z = 1.0 - splane.z;
  463. }
  464. splane.xy /= splane.z;
  465. splane.xy = splane.xy * 0.5 + 0.5;
  466. splane.z = shadow_len * omni_lights.data[idx].inv_radius;
  467. splane.xy = clamp_rect.xy + splane.xy * clamp_rect.zw;
  468. // splane.xy = clamp(splane.xy,clamp_rect.xy + scene_data_block.data.shadow_atlas_pixel_size,clamp_rect.xy + clamp_rect.zw - scene_data_block.data.shadow_atlas_pixel_size );
  469. splane.w = 1.0; //needed? i think it should be 1 already
  470. float shadow_z = textureLod(sampler2D(shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), splane.xy, 0.0).r;
  471. transmittance_z = (splane.z - shadow_z) / omni_lights.data[idx].inv_radius;
  472. }
  473. #endif
  474. if (sc_use_light_projector && omni_lights.data[idx].projector_rect != vec4(0.0)) {
  475. vec3 local_v = (omni_lights.data[idx].shadow_matrix * vec4(vertex, 1.0)).xyz;
  476. local_v = normalize(local_v);
  477. vec4 atlas_rect = omni_lights.data[idx].projector_rect;
  478. if (local_v.z >= 0.0) {
  479. atlas_rect.y += atlas_rect.w;
  480. }
  481. local_v.z = 1.0 + abs(local_v.z);
  482. local_v.xy /= local_v.z;
  483. local_v.xy = local_v.xy * 0.5 + 0.5;
  484. vec2 proj_uv = local_v.xy * atlas_rect.zw;
  485. if (sc_projector_use_mipmaps) {
  486. vec2 proj_uv_ddx;
  487. vec2 proj_uv_ddy;
  488. {
  489. vec3 local_v_ddx = (omni_lights.data[idx].shadow_matrix * vec4(vertex + vertex_ddx, 1.0)).xyz;
  490. local_v_ddx = normalize(local_v_ddx);
  491. if (local_v_ddx.z >= 0.0) {
  492. local_v_ddx.z += 1.0;
  493. } else {
  494. local_v_ddx.z = 1.0 - local_v_ddx.z;
  495. }
  496. local_v_ddx.xy /= local_v_ddx.z;
  497. local_v_ddx.xy = local_v_ddx.xy * 0.5 + 0.5;
  498. proj_uv_ddx = local_v_ddx.xy * atlas_rect.zw - proj_uv;
  499. vec3 local_v_ddy = (omni_lights.data[idx].shadow_matrix * vec4(vertex + vertex_ddy, 1.0)).xyz;
  500. local_v_ddy = normalize(local_v_ddy);
  501. if (local_v_ddy.z >= 0.0) {
  502. local_v_ddy.z += 1.0;
  503. } else {
  504. local_v_ddy.z = 1.0 - local_v_ddy.z;
  505. }
  506. local_v_ddy.xy /= local_v_ddy.z;
  507. local_v_ddy.xy = local_v_ddy.xy * 0.5 + 0.5;
  508. proj_uv_ddy = local_v_ddy.xy * atlas_rect.zw - proj_uv;
  509. }
  510. vec4 proj = textureGrad(sampler2D(decal_atlas_srgb, light_projector_sampler), proj_uv + atlas_rect.xy, proj_uv_ddx, proj_uv_ddy);
  511. color *= proj.rgb * proj.a;
  512. } else {
  513. vec4 proj = textureLod(sampler2D(decal_atlas_srgb, light_projector_sampler), proj_uv + atlas_rect.xy, 0.0);
  514. color *= proj.rgb * proj.a;
  515. }
  516. }
  517. light_attenuation *= shadow;
  518. light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, light_attenuation, f0, orms, omni_lights.data[idx].specular_amount, albedo, alpha,
  519. #ifdef LIGHT_BACKLIGHT_USED
  520. backlight,
  521. #endif
  522. #ifdef LIGHT_TRANSMITTANCE_USED
  523. transmittance_color,
  524. transmittance_depth,
  525. transmittance_boost,
  526. transmittance_z,
  527. #endif
  528. #ifdef LIGHT_RIM_USED
  529. rim * omni_attenuation, rim_tint,
  530. #endif
  531. #ifdef LIGHT_CLEARCOAT_USED
  532. clearcoat, clearcoat_roughness, vertex_normal,
  533. #endif
  534. #ifdef LIGHT_ANISOTROPY_USED
  535. binormal, tangent, anisotropy,
  536. #endif
  537. diffuse_light,
  538. specular_light);
  539. }
  540. float light_process_spot_shadow(uint idx, vec3 vertex, vec3 normal) {
  541. #ifndef SHADOWS_DISABLED
  542. if (spot_lights.data[idx].shadow_opacity > 0.001) {
  543. vec3 light_rel_vec = spot_lights.data[idx].position - vertex;
  544. float light_length = length(light_rel_vec);
  545. vec3 spot_dir = spot_lights.data[idx].direction;
  546. vec3 shadow_dir = light_rel_vec / light_length;
  547. vec3 normal_bias = normal * light_length * spot_lights.data[idx].shadow_normal_bias * (1.0 - abs(dot(normal, shadow_dir)));
  548. //there is a shadowmap
  549. vec4 v = vec4(vertex + normal_bias, 1.0);
  550. vec4 splane = (spot_lights.data[idx].shadow_matrix * v);
  551. splane.z -= spot_lights.data[idx].shadow_bias / (light_length * spot_lights.data[idx].inv_radius);
  552. splane /= splane.w;
  553. float shadow;
  554. if (sc_use_light_soft_shadows && spot_lights.data[idx].soft_shadow_size > 0.0) {
  555. //soft shadow
  556. //find blocker
  557. float z_norm = dot(spot_dir, -light_rel_vec) * spot_lights.data[idx].inv_radius;
  558. vec2 shadow_uv = splane.xy * spot_lights.data[idx].atlas_rect.zw + spot_lights.data[idx].atlas_rect.xy;
  559. float blocker_count = 0.0;
  560. float blocker_average = 0.0;
  561. mat2 disk_rotation;
  562. {
  563. float r = quick_hash(gl_FragCoord.xy) * 2.0 * M_PI;
  564. float sr = sin(r);
  565. float cr = cos(r);
  566. disk_rotation = mat2(vec2(cr, -sr), vec2(sr, cr));
  567. }
  568. float uv_size = spot_lights.data[idx].soft_shadow_size * z_norm * spot_lights.data[idx].soft_shadow_scale;
  569. vec2 clamp_max = spot_lights.data[idx].atlas_rect.xy + spot_lights.data[idx].atlas_rect.zw;
  570. for (uint i = 0; i < sc_penumbra_shadow_samples; i++) {
  571. vec2 suv = shadow_uv + (disk_rotation * scene_data_block.data.penumbra_shadow_kernel[i].xy) * uv_size;
  572. suv = clamp(suv, spot_lights.data[idx].atlas_rect.xy, clamp_max);
  573. float d = textureLod(sampler2D(shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), suv, 0.0).r;
  574. if (d < splane.z) {
  575. blocker_average += d;
  576. blocker_count += 1.0;
  577. }
  578. }
  579. if (blocker_count > 0.0) {
  580. //blockers found, do soft shadow
  581. blocker_average /= blocker_count;
  582. float penumbra = (z_norm - blocker_average) / blocker_average;
  583. uv_size *= penumbra;
  584. shadow = 0.0;
  585. for (uint i = 0; i < sc_penumbra_shadow_samples; i++) {
  586. vec2 suv = shadow_uv + (disk_rotation * scene_data_block.data.penumbra_shadow_kernel[i].xy) * uv_size;
  587. suv = clamp(suv, spot_lights.data[idx].atlas_rect.xy, clamp_max);
  588. shadow += textureProj(sampler2DShadow(shadow_atlas, shadow_sampler), vec4(suv, splane.z, 1.0));
  589. }
  590. shadow /= float(sc_penumbra_shadow_samples);
  591. shadow = mix(1.0, shadow, spot_lights.data[idx].shadow_opacity);
  592. } else {
  593. //no blockers found, so no shadow
  594. shadow = 1.0;
  595. }
  596. } else {
  597. //hard shadow
  598. vec3 shadow_uv = vec3(splane.xy * spot_lights.data[idx].atlas_rect.zw + spot_lights.data[idx].atlas_rect.xy, splane.z);
  599. shadow = mix(1.0, sample_pcf_shadow(shadow_atlas, spot_lights.data[idx].soft_shadow_scale * scene_data_block.data.shadow_atlas_pixel_size, shadow_uv), spot_lights.data[idx].shadow_opacity);
  600. }
  601. return shadow;
  602. }
  603. #endif // SHADOWS_DISABLED
  604. return 1.0;
  605. }
  606. vec2 normal_to_panorama(vec3 n) {
  607. n = normalize(n);
  608. vec2 panorama_coords = vec2(atan(n.x, n.z), acos(-n.y));
  609. if (panorama_coords.x < 0.0) {
  610. panorama_coords.x += M_PI * 2.0;
  611. }
  612. panorama_coords /= vec2(M_PI * 2.0, M_PI);
  613. return panorama_coords;
  614. }
  615. void light_process_spot(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 vertex_ddx, vec3 vertex_ddy, vec3 f0, uint orms, float shadow, vec3 albedo, inout float alpha,
  616. #ifdef LIGHT_BACKLIGHT_USED
  617. vec3 backlight,
  618. #endif
  619. #ifdef LIGHT_TRANSMITTANCE_USED
  620. vec4 transmittance_color,
  621. float transmittance_depth,
  622. float transmittance_boost,
  623. #endif
  624. #ifdef LIGHT_RIM_USED
  625. float rim, float rim_tint,
  626. #endif
  627. #ifdef LIGHT_CLEARCOAT_USED
  628. float clearcoat, float clearcoat_roughness, vec3 vertex_normal,
  629. #endif
  630. #ifdef LIGHT_ANISOTROPY_USED
  631. vec3 binormal, vec3 tangent, float anisotropy,
  632. #endif
  633. inout vec3 diffuse_light,
  634. inout vec3 specular_light) {
  635. vec3 light_rel_vec = spot_lights.data[idx].position - vertex;
  636. float light_length = length(light_rel_vec);
  637. float spot_attenuation = get_omni_attenuation(light_length, spot_lights.data[idx].inv_radius, spot_lights.data[idx].attenuation);
  638. vec3 spot_dir = spot_lights.data[idx].direction;
  639. // This conversion to a highp float is crucial to prevent light leaking
  640. // due to precision errors in the following calculations (cone angle is mediump).
  641. highp float cone_angle = spot_lights.data[idx].cone_angle;
  642. float scos = max(dot(-normalize(light_rel_vec), spot_dir), cone_angle);
  643. float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - cone_angle));
  644. spot_attenuation *= 1.0 - pow(spot_rim, spot_lights.data[idx].cone_attenuation);
  645. float light_attenuation = spot_attenuation;
  646. vec3 color = spot_lights.data[idx].color;
  647. float specular_amount = spot_lights.data[idx].specular_amount;
  648. float size_A = 0.0;
  649. if (sc_use_light_soft_shadows && spot_lights.data[idx].size > 0.0) {
  650. float t = spot_lights.data[idx].size / max(0.001, light_length);
  651. size_A = max(0.0, 1.0 - 1 / sqrt(1 + t * t));
  652. }
  653. #ifdef LIGHT_TRANSMITTANCE_USED
  654. float transmittance_z = transmittance_depth;
  655. transmittance_color.a *= light_attenuation;
  656. {
  657. vec4 splane = (spot_lights.data[idx].shadow_matrix * vec4(vertex - normalize(normal_interp) * spot_lights.data[idx].transmittance_bias, 1.0));
  658. splane /= splane.w;
  659. splane.xy = splane.xy * spot_lights.data[idx].atlas_rect.zw + spot_lights.data[idx].atlas_rect.xy;
  660. float shadow_z = textureLod(sampler2D(shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), splane.xy, 0.0).r;
  661. shadow_z = shadow_z * 2.0 - 1.0;
  662. float z_far = 1.0 / spot_lights.data[idx].inv_radius;
  663. float z_near = 0.01;
  664. shadow_z = 2.0 * z_near * z_far / (z_far + z_near - shadow_z * (z_far - z_near));
  665. //distance to light plane
  666. float z = dot(spot_dir, -light_rel_vec);
  667. transmittance_z = z - shadow_z;
  668. }
  669. #endif //LIGHT_TRANSMITTANCE_USED
  670. if (sc_use_light_projector && spot_lights.data[idx].projector_rect != vec4(0.0)) {
  671. vec4 splane = (spot_lights.data[idx].shadow_matrix * vec4(vertex, 1.0));
  672. splane /= splane.w;
  673. vec2 proj_uv = splane.xy * spot_lights.data[idx].projector_rect.zw;
  674. if (sc_projector_use_mipmaps) {
  675. //ensure we have proper mipmaps
  676. vec4 splane_ddx = (spot_lights.data[idx].shadow_matrix * vec4(vertex + vertex_ddx, 1.0));
  677. splane_ddx /= splane_ddx.w;
  678. vec2 proj_uv_ddx = splane_ddx.xy * spot_lights.data[idx].projector_rect.zw - proj_uv;
  679. vec4 splane_ddy = (spot_lights.data[idx].shadow_matrix * vec4(vertex + vertex_ddy, 1.0));
  680. splane_ddy /= splane_ddy.w;
  681. vec2 proj_uv_ddy = splane_ddy.xy * spot_lights.data[idx].projector_rect.zw - proj_uv;
  682. vec4 proj = textureGrad(sampler2D(decal_atlas_srgb, light_projector_sampler), proj_uv + spot_lights.data[idx].projector_rect.xy, proj_uv_ddx, proj_uv_ddy);
  683. color *= proj.rgb * proj.a;
  684. } else {
  685. vec4 proj = textureLod(sampler2D(decal_atlas_srgb, light_projector_sampler), proj_uv + spot_lights.data[idx].projector_rect.xy, 0.0);
  686. color *= proj.rgb * proj.a;
  687. }
  688. }
  689. light_attenuation *= shadow;
  690. light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, light_attenuation, f0, orms, spot_lights.data[idx].specular_amount, albedo, alpha,
  691. #ifdef LIGHT_BACKLIGHT_USED
  692. backlight,
  693. #endif
  694. #ifdef LIGHT_TRANSMITTANCE_USED
  695. transmittance_color,
  696. transmittance_depth,
  697. transmittance_boost,
  698. transmittance_z,
  699. #endif
  700. #ifdef LIGHT_RIM_USED
  701. rim * spot_attenuation, rim_tint,
  702. #endif
  703. #ifdef LIGHT_CLEARCOAT_USED
  704. clearcoat, clearcoat_roughness, vertex_normal,
  705. #endif
  706. #ifdef LIGHT_ANISOTROPY_USED
  707. binormal, tangent, anisotropy,
  708. #endif
  709. diffuse_light, specular_light);
  710. }
  711. void reflection_process(uint ref_index, vec3 vertex, vec3 ref_vec, vec3 normal, float roughness, vec3 ambient_light, vec3 specular_light, inout vec4 ambient_accum, inout vec4 reflection_accum) {
  712. vec3 box_extents = reflections.data[ref_index].box_extents;
  713. vec3 local_pos = (reflections.data[ref_index].local_matrix * vec4(vertex, 1.0)).xyz;
  714. if (any(greaterThan(abs(local_pos), box_extents))) { //out of the reflection box
  715. return;
  716. }
  717. vec3 inner_pos = abs(local_pos / box_extents);
  718. float blend = max(inner_pos.x, max(inner_pos.y, inner_pos.z));
  719. //make blend more rounded
  720. blend = mix(length(inner_pos), blend, blend);
  721. blend *= blend;
  722. blend = max(0.0, 1.0 - blend);
  723. if (reflections.data[ref_index].intensity > 0.0) { // compute reflection
  724. vec3 local_ref_vec = (reflections.data[ref_index].local_matrix * vec4(ref_vec, 0.0)).xyz;
  725. if (reflections.data[ref_index].box_project) { //box project
  726. vec3 nrdir = normalize(local_ref_vec);
  727. vec3 rbmax = (box_extents - local_pos) / nrdir;
  728. vec3 rbmin = (-box_extents - local_pos) / nrdir;
  729. vec3 rbminmax = mix(rbmin, rbmax, greaterThan(nrdir, vec3(0.0, 0.0, 0.0)));
  730. float fa = min(min(rbminmax.x, rbminmax.y), rbminmax.z);
  731. vec3 posonbox = local_pos + nrdir * fa;
  732. local_ref_vec = posonbox - reflections.data[ref_index].box_offset;
  733. }
  734. vec4 reflection;
  735. reflection.rgb = textureLod(samplerCubeArray(reflection_atlas, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(local_ref_vec, reflections.data[ref_index].index), roughness * MAX_ROUGHNESS_LOD).rgb * sc_luminance_multiplier;
  736. reflection.rgb *= reflections.data[ref_index].exposure_normalization;
  737. if (reflections.data[ref_index].exterior) {
  738. reflection.rgb = mix(specular_light, reflection.rgb, blend);
  739. }
  740. reflection.rgb *= reflections.data[ref_index].intensity; //intensity
  741. reflection.a = blend;
  742. reflection.rgb *= reflection.a;
  743. reflection_accum += reflection;
  744. }
  745. switch (reflections.data[ref_index].ambient_mode) {
  746. case REFLECTION_AMBIENT_DISABLED: {
  747. //do nothing
  748. } break;
  749. case REFLECTION_AMBIENT_ENVIRONMENT: {
  750. //do nothing
  751. vec3 local_amb_vec = (reflections.data[ref_index].local_matrix * vec4(normal, 0.0)).xyz;
  752. vec4 ambient_out;
  753. ambient_out.rgb = textureLod(samplerCubeArray(reflection_atlas, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(local_amb_vec, reflections.data[ref_index].index), MAX_ROUGHNESS_LOD).rgb;
  754. ambient_out.rgb *= reflections.data[ref_index].exposure_normalization;
  755. ambient_out.a = blend;
  756. if (reflections.data[ref_index].exterior) {
  757. ambient_out.rgb = mix(ambient_light, ambient_out.rgb, blend);
  758. }
  759. ambient_out.rgb *= ambient_out.a;
  760. ambient_accum += ambient_out;
  761. } break;
  762. case REFLECTION_AMBIENT_COLOR: {
  763. vec4 ambient_out;
  764. ambient_out.a = blend;
  765. ambient_out.rgb = reflections.data[ref_index].ambient;
  766. if (reflections.data[ref_index].exterior) {
  767. ambient_out.rgb = mix(ambient_light, ambient_out.rgb, blend);
  768. }
  769. ambient_out.rgb *= ambient_out.a;
  770. ambient_accum += ambient_out;
  771. } break;
  772. }
  773. }
  774. float blur_shadow(float shadow) {
  775. return shadow;
  776. #if 0
  777. //disabling for now, will investigate later
  778. float interp_shadow = shadow;
  779. if (gl_HelperInvocation) {
  780. interp_shadow = -4.0; // technically anything below -4 will do but just to make sure
  781. }
  782. uvec2 fc2 = uvec2(gl_FragCoord.xy);
  783. interp_shadow -= dFdx(interp_shadow) * (float(fc2.x & 1) - 0.5);
  784. interp_shadow -= dFdy(interp_shadow) * (float(fc2.y & 1) - 0.5);
  785. if (interp_shadow >= 0.0) {
  786. shadow = interp_shadow;
  787. }
  788. return shadow;
  789. #endif
  790. }