scene_forward_lights_inc.glsl 35 KB

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