scene_forward_lights_inc.glsl 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  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) / (1.0 - 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 = 1.0 - 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) / (1.0 - 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. depth = 1.0 - depth;
  426. 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);
  427. }
  428. return shadow;
  429. }
  430. #endif
  431. return 1.0;
  432. }
  433. 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,
  434. #ifdef LIGHT_BACKLIGHT_USED
  435. vec3 backlight,
  436. #endif
  437. #ifdef LIGHT_TRANSMITTANCE_USED
  438. vec4 transmittance_color,
  439. float transmittance_depth,
  440. float transmittance_boost,
  441. #endif
  442. #ifdef LIGHT_RIM_USED
  443. float rim, float rim_tint,
  444. #endif
  445. #ifdef LIGHT_CLEARCOAT_USED
  446. float clearcoat, float clearcoat_roughness, vec3 vertex_normal,
  447. #endif
  448. #ifdef LIGHT_ANISOTROPY_USED
  449. vec3 binormal, vec3 tangent, float anisotropy,
  450. #endif
  451. inout vec3 diffuse_light, inout vec3 specular_light) {
  452. vec3 light_rel_vec = omni_lights.data[idx].position - vertex;
  453. float light_length = length(light_rel_vec);
  454. float omni_attenuation = get_omni_attenuation(light_length, omni_lights.data[idx].inv_radius, omni_lights.data[idx].attenuation);
  455. float light_attenuation = omni_attenuation;
  456. vec3 color = omni_lights.data[idx].color;
  457. float size_A = 0.0;
  458. if (sc_use_light_soft_shadows && omni_lights.data[idx].size > 0.0) {
  459. float t = omni_lights.data[idx].size / max(0.001, light_length);
  460. size_A = max(0.0, 1.0 - 1 / sqrt(1 + t * t));
  461. }
  462. #ifdef LIGHT_TRANSMITTANCE_USED
  463. float transmittance_z = transmittance_depth; //no transmittance by default
  464. transmittance_color.a *= light_attenuation;
  465. #ifndef SHADOWS_DISABLED
  466. if (omni_lights.data[idx].shadow_opacity > 0.001) {
  467. // Redo shadowmapping, but shrink the model a bit to avoid artifacts.
  468. vec2 texel_size = scene_data_block.data.shadow_atlas_pixel_size;
  469. vec4 uv_rect = omni_lights.data[idx].atlas_rect;
  470. uv_rect.xy += texel_size;
  471. uv_rect.zw -= texel_size * 2.0;
  472. // Omni lights use direction.xy to store to store the offset between the two paraboloid regions
  473. vec2 flip_offset = omni_lights.data[idx].direction.xy;
  474. vec3 local_vert = (omni_lights.data[idx].shadow_matrix * vec4(vertex - normalize(normal) * omni_lights.data[idx].transmittance_bias, 1.0)).xyz;
  475. float shadow_len = length(local_vert); //need to remember shadow len from here
  476. vec3 shadow_sample = normalize(local_vert);
  477. if (shadow_sample.z >= 0.0) {
  478. uv_rect.xy += flip_offset;
  479. flip_offset *= -1.0;
  480. }
  481. shadow_sample.z = 1.0 + abs(shadow_sample.z);
  482. vec2 pos = shadow_sample.xy / shadow_sample.z;
  483. float depth = shadow_len * omni_lights.data[idx].inv_radius;
  484. depth = 1.0 - depth;
  485. pos = pos * 0.5 + 0.5;
  486. pos = uv_rect.xy + pos * uv_rect.zw;
  487. float shadow_z = textureLod(sampler2D(shadow_atlas, SAMPLER_LINEAR_CLAMP), pos, 0.0).r;
  488. transmittance_z = (depth - shadow_z) / omni_lights.data[idx].inv_radius;
  489. }
  490. #endif // !SHADOWS_DISABLED
  491. #endif // LIGHT_TRANSMITTANCE_USED
  492. if (sc_use_light_projector && omni_lights.data[idx].projector_rect != vec4(0.0)) {
  493. vec3 local_v = (omni_lights.data[idx].shadow_matrix * vec4(vertex, 1.0)).xyz;
  494. local_v = normalize(local_v);
  495. vec4 atlas_rect = omni_lights.data[idx].projector_rect;
  496. if (local_v.z >= 0.0) {
  497. atlas_rect.y += atlas_rect.w;
  498. }
  499. local_v.z = 1.0 + abs(local_v.z);
  500. local_v.xy /= local_v.z;
  501. local_v.xy = local_v.xy * 0.5 + 0.5;
  502. vec2 proj_uv = local_v.xy * atlas_rect.zw;
  503. if (sc_projector_use_mipmaps) {
  504. vec2 proj_uv_ddx;
  505. vec2 proj_uv_ddy;
  506. {
  507. vec3 local_v_ddx = (omni_lights.data[idx].shadow_matrix * vec4(vertex + vertex_ddx, 1.0)).xyz;
  508. local_v_ddx = normalize(local_v_ddx);
  509. if (local_v_ddx.z >= 0.0) {
  510. local_v_ddx.z += 1.0;
  511. } else {
  512. local_v_ddx.z = 1.0 - local_v_ddx.z;
  513. }
  514. local_v_ddx.xy /= local_v_ddx.z;
  515. local_v_ddx.xy = local_v_ddx.xy * 0.5 + 0.5;
  516. proj_uv_ddx = local_v_ddx.xy * atlas_rect.zw - proj_uv;
  517. vec3 local_v_ddy = (omni_lights.data[idx].shadow_matrix * vec4(vertex + vertex_ddy, 1.0)).xyz;
  518. local_v_ddy = normalize(local_v_ddy);
  519. if (local_v_ddy.z >= 0.0) {
  520. local_v_ddy.z += 1.0;
  521. } else {
  522. local_v_ddy.z = 1.0 - local_v_ddy.z;
  523. }
  524. local_v_ddy.xy /= local_v_ddy.z;
  525. local_v_ddy.xy = local_v_ddy.xy * 0.5 + 0.5;
  526. proj_uv_ddy = local_v_ddy.xy * atlas_rect.zw - proj_uv;
  527. }
  528. vec4 proj = textureGrad(sampler2D(decal_atlas_srgb, light_projector_sampler), proj_uv + atlas_rect.xy, proj_uv_ddx, proj_uv_ddy);
  529. color *= proj.rgb * proj.a;
  530. } else {
  531. vec4 proj = textureLod(sampler2D(decal_atlas_srgb, light_projector_sampler), proj_uv + atlas_rect.xy, 0.0);
  532. color *= proj.rgb * proj.a;
  533. }
  534. }
  535. light_attenuation *= shadow;
  536. 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,
  537. #ifdef LIGHT_BACKLIGHT_USED
  538. backlight,
  539. #endif
  540. #ifdef LIGHT_TRANSMITTANCE_USED
  541. transmittance_color,
  542. transmittance_depth,
  543. transmittance_boost,
  544. transmittance_z,
  545. #endif
  546. #ifdef LIGHT_RIM_USED
  547. rim * omni_attenuation, rim_tint,
  548. #endif
  549. #ifdef LIGHT_CLEARCOAT_USED
  550. clearcoat, clearcoat_roughness, vertex_normal,
  551. #endif
  552. #ifdef LIGHT_ANISOTROPY_USED
  553. binormal, tangent, anisotropy,
  554. #endif
  555. diffuse_light,
  556. specular_light);
  557. }
  558. float light_process_spot_shadow(uint idx, vec3 vertex, vec3 normal) {
  559. #ifndef SHADOWS_DISABLED
  560. if (spot_lights.data[idx].shadow_opacity > 0.001) {
  561. vec3 light_rel_vec = spot_lights.data[idx].position - vertex;
  562. float light_length = length(light_rel_vec);
  563. vec3 spot_dir = spot_lights.data[idx].direction;
  564. vec3 shadow_dir = light_rel_vec / light_length;
  565. vec3 normal_bias = normal * light_length * spot_lights.data[idx].shadow_normal_bias * (1.0 - abs(dot(normal, shadow_dir)));
  566. //there is a shadowmap
  567. vec4 v = vec4(vertex + normal_bias, 1.0);
  568. vec4 splane = (spot_lights.data[idx].shadow_matrix * v);
  569. splane.z += spot_lights.data[idx].shadow_bias / (light_length * spot_lights.data[idx].inv_radius);
  570. splane /= splane.w;
  571. float shadow;
  572. if (sc_use_light_soft_shadows && spot_lights.data[idx].soft_shadow_size > 0.0) {
  573. //soft shadow
  574. //find blocker
  575. float z_norm = dot(spot_dir, -light_rel_vec) * spot_lights.data[idx].inv_radius;
  576. vec2 shadow_uv = splane.xy * spot_lights.data[idx].atlas_rect.zw + spot_lights.data[idx].atlas_rect.xy;
  577. float blocker_count = 0.0;
  578. float blocker_average = 0.0;
  579. mat2 disk_rotation;
  580. {
  581. float r = quick_hash(gl_FragCoord.xy) * 2.0 * M_PI;
  582. float sr = sin(r);
  583. float cr = cos(r);
  584. disk_rotation = mat2(vec2(cr, -sr), vec2(sr, cr));
  585. }
  586. float uv_size = spot_lights.data[idx].soft_shadow_size * z_norm * spot_lights.data[idx].soft_shadow_scale;
  587. vec2 clamp_max = spot_lights.data[idx].atlas_rect.xy + spot_lights.data[idx].atlas_rect.zw;
  588. for (uint i = 0; i < sc_penumbra_shadow_samples; i++) {
  589. vec2 suv = shadow_uv + (disk_rotation * scene_data_block.data.penumbra_shadow_kernel[i].xy) * uv_size;
  590. suv = clamp(suv, spot_lights.data[idx].atlas_rect.xy, clamp_max);
  591. float d = textureLod(sampler2D(shadow_atlas, SAMPLER_LINEAR_CLAMP), suv, 0.0).r;
  592. if (d > splane.z) {
  593. blocker_average += d;
  594. blocker_count += 1.0;
  595. }
  596. }
  597. if (blocker_count > 0.0) {
  598. //blockers found, do soft shadow
  599. blocker_average /= blocker_count;
  600. float penumbra = (-z_norm + blocker_average) / (1.0 - blocker_average);
  601. uv_size *= penumbra;
  602. shadow = 0.0;
  603. for (uint i = 0; i < sc_penumbra_shadow_samples; i++) {
  604. vec2 suv = shadow_uv + (disk_rotation * scene_data_block.data.penumbra_shadow_kernel[i].xy) * uv_size;
  605. suv = clamp(suv, spot_lights.data[idx].atlas_rect.xy, clamp_max);
  606. shadow += textureProj(sampler2DShadow(shadow_atlas, shadow_sampler), vec4(suv, splane.z, 1.0));
  607. }
  608. shadow /= float(sc_penumbra_shadow_samples);
  609. shadow = mix(1.0, shadow, spot_lights.data[idx].shadow_opacity);
  610. } else {
  611. //no blockers found, so no shadow
  612. shadow = 1.0;
  613. }
  614. } else {
  615. //hard shadow
  616. vec3 shadow_uv = vec3(splane.xy * spot_lights.data[idx].atlas_rect.zw + spot_lights.data[idx].atlas_rect.xy, splane.z);
  617. 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);
  618. }
  619. return shadow;
  620. }
  621. #endif // SHADOWS_DISABLED
  622. return 1.0;
  623. }
  624. vec2 normal_to_panorama(vec3 n) {
  625. n = normalize(n);
  626. vec2 panorama_coords = vec2(atan(n.x, n.z), acos(-n.y));
  627. if (panorama_coords.x < 0.0) {
  628. panorama_coords.x += M_PI * 2.0;
  629. }
  630. panorama_coords /= vec2(M_PI * 2.0, M_PI);
  631. return panorama_coords;
  632. }
  633. 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,
  634. #ifdef LIGHT_BACKLIGHT_USED
  635. vec3 backlight,
  636. #endif
  637. #ifdef LIGHT_TRANSMITTANCE_USED
  638. vec4 transmittance_color,
  639. float transmittance_depth,
  640. float transmittance_boost,
  641. #endif
  642. #ifdef LIGHT_RIM_USED
  643. float rim, float rim_tint,
  644. #endif
  645. #ifdef LIGHT_CLEARCOAT_USED
  646. float clearcoat, float clearcoat_roughness, vec3 vertex_normal,
  647. #endif
  648. #ifdef LIGHT_ANISOTROPY_USED
  649. vec3 binormal, vec3 tangent, float anisotropy,
  650. #endif
  651. inout vec3 diffuse_light,
  652. inout vec3 specular_light) {
  653. vec3 light_rel_vec = spot_lights.data[idx].position - vertex;
  654. float light_length = length(light_rel_vec);
  655. float spot_attenuation = get_omni_attenuation(light_length, spot_lights.data[idx].inv_radius, spot_lights.data[idx].attenuation);
  656. vec3 spot_dir = spot_lights.data[idx].direction;
  657. // This conversion to a highp float is crucial to prevent light leaking
  658. // due to precision errors in the following calculations (cone angle is mediump).
  659. highp float cone_angle = spot_lights.data[idx].cone_angle;
  660. float scos = max(dot(-normalize(light_rel_vec), spot_dir), cone_angle);
  661. float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - cone_angle));
  662. spot_attenuation *= 1.0 - pow(spot_rim, spot_lights.data[idx].cone_attenuation);
  663. float light_attenuation = spot_attenuation;
  664. vec3 color = spot_lights.data[idx].color;
  665. float specular_amount = spot_lights.data[idx].specular_amount;
  666. float size_A = 0.0;
  667. if (sc_use_light_soft_shadows && spot_lights.data[idx].size > 0.0) {
  668. float t = spot_lights.data[idx].size / max(0.001, light_length);
  669. size_A = max(0.0, 1.0 - 1 / sqrt(1 + t * t));
  670. }
  671. #ifdef LIGHT_TRANSMITTANCE_USED
  672. float transmittance_z = transmittance_depth;
  673. transmittance_color.a *= light_attenuation;
  674. #ifndef SHADOWS_DISABLED
  675. if (spot_lights.data[idx].shadow_opacity > 0.001) {
  676. vec4 splane = (spot_lights.data[idx].shadow_matrix * vec4(vertex - normalize(normal) * spot_lights.data[idx].transmittance_bias, 1.0));
  677. splane /= splane.w;
  678. vec3 shadow_uv = vec3(splane.xy * spot_lights.data[idx].atlas_rect.zw + spot_lights.data[idx].atlas_rect.xy, splane.z);
  679. float shadow_z = textureLod(sampler2D(shadow_atlas, SAMPLER_LINEAR_CLAMP), shadow_uv.xy, 0.0).r;
  680. shadow_z = shadow_z * 2.0 - 1.0;
  681. float z_far = 1.0 / spot_lights.data[idx].inv_radius;
  682. float z_near = 0.01;
  683. shadow_z = 2.0 * z_near * z_far / (z_far + z_near - shadow_z * (z_far - z_near));
  684. //distance to light plane
  685. float z = dot(spot_dir, -light_rel_vec);
  686. transmittance_z = z - shadow_z;
  687. }
  688. #endif // !SHADOWS_DISABLED
  689. #endif // LIGHT_TRANSMITTANCE_USED
  690. if (sc_use_light_projector && spot_lights.data[idx].projector_rect != vec4(0.0)) {
  691. vec4 splane = (spot_lights.data[idx].shadow_matrix * vec4(vertex, 1.0));
  692. splane /= splane.w;
  693. vec2 proj_uv = splane.xy * spot_lights.data[idx].projector_rect.zw;
  694. if (sc_projector_use_mipmaps) {
  695. //ensure we have proper mipmaps
  696. vec4 splane_ddx = (spot_lights.data[idx].shadow_matrix * vec4(vertex + vertex_ddx, 1.0));
  697. splane_ddx /= splane_ddx.w;
  698. vec2 proj_uv_ddx = splane_ddx.xy * spot_lights.data[idx].projector_rect.zw - proj_uv;
  699. vec4 splane_ddy = (spot_lights.data[idx].shadow_matrix * vec4(vertex + vertex_ddy, 1.0));
  700. splane_ddy /= splane_ddy.w;
  701. vec2 proj_uv_ddy = splane_ddy.xy * spot_lights.data[idx].projector_rect.zw - proj_uv;
  702. 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);
  703. color *= proj.rgb * proj.a;
  704. } else {
  705. vec4 proj = textureLod(sampler2D(decal_atlas_srgb, light_projector_sampler), proj_uv + spot_lights.data[idx].projector_rect.xy, 0.0);
  706. color *= proj.rgb * proj.a;
  707. }
  708. }
  709. light_attenuation *= shadow;
  710. 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,
  711. #ifdef LIGHT_BACKLIGHT_USED
  712. backlight,
  713. #endif
  714. #ifdef LIGHT_TRANSMITTANCE_USED
  715. transmittance_color,
  716. transmittance_depth,
  717. transmittance_boost,
  718. transmittance_z,
  719. #endif
  720. #ifdef LIGHT_RIM_USED
  721. rim * spot_attenuation, rim_tint,
  722. #endif
  723. #ifdef LIGHT_CLEARCOAT_USED
  724. clearcoat, clearcoat_roughness, vertex_normal,
  725. #endif
  726. #ifdef LIGHT_ANISOTROPY_USED
  727. binormal, tangent, anisotropy,
  728. #endif
  729. diffuse_light, specular_light);
  730. }
  731. 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) {
  732. vec3 box_extents = reflections.data[ref_index].box_extents;
  733. vec3 local_pos = (reflections.data[ref_index].local_matrix * vec4(vertex, 1.0)).xyz;
  734. if (any(greaterThan(abs(local_pos), box_extents))) { //out of the reflection box
  735. return;
  736. }
  737. vec3 inner_pos = abs(local_pos / box_extents);
  738. float blend = max(inner_pos.x, max(inner_pos.y, inner_pos.z));
  739. //make blend more rounded
  740. blend = mix(length(inner_pos), blend, blend);
  741. blend *= blend;
  742. blend = max(0.0, 1.0 - blend);
  743. if (reflections.data[ref_index].intensity > 0.0) { // compute reflection
  744. vec3 local_ref_vec = (reflections.data[ref_index].local_matrix * vec4(ref_vec, 0.0)).xyz;
  745. if (reflections.data[ref_index].box_project) { //box project
  746. vec3 nrdir = normalize(local_ref_vec);
  747. vec3 rbmax = (box_extents - local_pos) / nrdir;
  748. vec3 rbmin = (-box_extents - local_pos) / nrdir;
  749. vec3 rbminmax = mix(rbmin, rbmax, greaterThan(nrdir, vec3(0.0, 0.0, 0.0)));
  750. float fa = min(min(rbminmax.x, rbminmax.y), rbminmax.z);
  751. vec3 posonbox = local_pos + nrdir * fa;
  752. local_ref_vec = posonbox - reflections.data[ref_index].box_offset;
  753. }
  754. vec4 reflection;
  755. reflection.rgb = textureLod(samplerCubeArray(reflection_atlas, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec4(local_ref_vec, reflections.data[ref_index].index), roughness * MAX_ROUGHNESS_LOD).rgb * sc_luminance_multiplier;
  756. reflection.rgb *= reflections.data[ref_index].exposure_normalization;
  757. if (reflections.data[ref_index].exterior) {
  758. reflection.rgb = mix(specular_light, reflection.rgb, blend);
  759. }
  760. reflection.rgb *= reflections.data[ref_index].intensity; //intensity
  761. reflection.a = blend;
  762. reflection.rgb *= reflection.a;
  763. reflection_accum += reflection;
  764. }
  765. switch (reflections.data[ref_index].ambient_mode) {
  766. case REFLECTION_AMBIENT_DISABLED: {
  767. //do nothing
  768. } break;
  769. case REFLECTION_AMBIENT_ENVIRONMENT: {
  770. //do nothing
  771. vec3 local_amb_vec = (reflections.data[ref_index].local_matrix * vec4(normal, 0.0)).xyz;
  772. vec4 ambient_out;
  773. ambient_out.rgb = textureLod(samplerCubeArray(reflection_atlas, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec4(local_amb_vec, reflections.data[ref_index].index), MAX_ROUGHNESS_LOD).rgb;
  774. ambient_out.rgb *= reflections.data[ref_index].exposure_normalization;
  775. ambient_out.a = blend;
  776. if (reflections.data[ref_index].exterior) {
  777. ambient_out.rgb = mix(ambient_light, ambient_out.rgb, blend);
  778. }
  779. ambient_out.rgb *= ambient_out.a;
  780. ambient_accum += ambient_out;
  781. } break;
  782. case REFLECTION_AMBIENT_COLOR: {
  783. vec4 ambient_out;
  784. ambient_out.a = blend;
  785. ambient_out.rgb = reflections.data[ref_index].ambient;
  786. if (reflections.data[ref_index].exterior) {
  787. ambient_out.rgb = mix(ambient_light, ambient_out.rgb, blend);
  788. }
  789. ambient_out.rgb *= ambient_out.a;
  790. ambient_accum += ambient_out;
  791. } break;
  792. }
  793. }
  794. float blur_shadow(float shadow) {
  795. return shadow;
  796. #if 0
  797. //disabling for now, will investigate later
  798. float interp_shadow = shadow;
  799. if (gl_HelperInvocation) {
  800. interp_shadow = -4.0; // technically anything below -4 will do but just to make sure
  801. }
  802. uvec2 fc2 = uvec2(gl_FragCoord.xy);
  803. interp_shadow -= dFdx(interp_shadow) * (float(fc2.x & 1) - 0.5);
  804. interp_shadow -= dFdy(interp_shadow) * (float(fc2.y & 1) - 0.5);
  805. if (interp_shadow >= 0.0) {
  806. shadow = interp_shadow;
  807. }
  808. return shadow;
  809. #endif
  810. }