scene_forward_lights_inc.glsl 34 KB

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