scene_forward_lights_inc.glsl 33 KB

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