scene_forward_lights_inc.glsl 34 KB

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