256 vec2 dx = dFdx(texCoords);
257 vec2 dy = dFdy(texCoords);
258 const float minLayers = 2.0;
259 const float maxLayers = 16.0;
261 float tx_width = float(textureSize(normal_map, 0).x);
263 vec2 dx2 = max(dx * dx * tx_width * tx_width, dy * dy * tx_width * tx_width);
264 float mip_level = 0.5 * log2(max(dx2.
x, dx2.
y));
266 float fade =
clamp(1.0 - (mip_level / 3.0), 0.0, 1.0);
269 float current_max_layers = mix(minLayers, maxLayers, fade);
272 float numLayers = mix(current_max_layers, minLayers, abs(dot(
vec3(0.0, 0.0, 1.0), viewDir)));
274 float layerDepth = 1.0 / numLayers;
275 float currentLayerDepth = 0.0;
278 float uv_scale = length(fwidth(texCoords));
280 vec2 deltaTexCoords =
vec2(P.
x, -P.
y) / numLayers;
282 vec2 currentTexCoords = texCoords;
284 float currentDepthMapValue = textureGrad(normal_map, currentTexCoords, dx, dy).a;
288 while(currentLayerDepth < currentDepthMapValue && safety > cur)
290 currentTexCoords -= deltaTexCoords;
293 currentDepthMapValue = textureGrad(normal_map, currentTexCoords, dx, dy).a;
294 currentLayerDepth += layerDepth;
298 vec2 prevTexCoords = currentTexCoords + deltaTexCoords;
299 float afterDepth = currentLayerDepth - currentDepthMapValue;
300 float current_depth = textureGrad(normal_map, currentTexCoords, dx, dy).a;
301 float prev_depth = textureGrad(normal_map, prevTexCoords, dx, dy).a;
302 float beforeDepth = (prev_depth) - currentLayerDepth + layerDepth;
304 float weight = afterDepth / (afterDepth + beforeDepth);
305 vec2 finalTexCoords = mix(currentTexCoords, prevTexCoords, weight);
306 final_depth = mix(current_depth, prev_depth, weight);
308 return finalTexCoords;
577 vec2 screenUV = gl_FragCoord.xy * texelSize;
609 map_normal.xyz = normalize(map_normal.xyz);
610 vec3 N = normalize(
TBN * map_normal.xyz);
622 #ifdef ORIGINAL_NORMAL
628 vec3 sun_dir =
vec3(-0.5, 0.2, 0.9);
629 vec3 L = normalize(sun_dir);
631 vec3 view_dir = normalize(cam_pos.xyz -
Pos);
633 float cosTheta = max(dot(N, view_dir), 0.0);
635 float ao = arm.r * texture(
uSSAO, screenUV).r;
639 float metallic = arm.b * mat.
metallic;
648 Cluster cluster = clusters[cluster_index];
665 for (
int i = 0; i < cluster.
count; i++)
667 uint index = global_light_indices[cluster.
offset + i];
668 Light l = lights[index];
670 vec3 dir = normalize(diff);
672 float nbias = max(0.1 * (1.0 - dot(original_normal, dir)), 0.05);
673 float d2 = dot(diff, diff);
679 vec3 opp = (
Pos + original_normal * nbias) - l.
pos;
680 float current_depth = length(opp) / 100;
682 opp =
vec3(opp.
x, opp.
z, -opp.
y);
683 float bias = max(0.005 * (1.0 - dot(N, dir)), 0.0005);
685 float s = texture(
uPLSCMA,
vec4(opp, index), current_depth - bias).r;
694 for (
int i = 0; i < directional_light_count; i++)
696 Light l = lights[light_indices[directional_light_offset + i]];
704 for (
int i = 0; i < spot_light_count; i++)
707 Light l = lights[light_indices[spot_light_offset + i]];
709 vec3 dir = normalize(diff);
710 float d2 = dot(diff, diff);
714 float theta = dot(-dir, l.
dir);
717 lighting /= max(d2, 0.001);
718 lighting *= intensity;
722 vec3 direct_diffuse = albedo.rgb * acc;
733 float closest = 10000000;
736 for (
int i = 0; i < 6; i++)
739 if (pvs[i].loaded == 0)
continue;
742 float dist = length(clamped_local - local);
760 vec3 coord0 =
vec3(tex_local.
x, tex_local.
y, local_z + grid_size_div * 0);
761 vec3 coord1 =
vec3(tex_local.
x, tex_local.
y, local_z + grid_size_div * 1);
762 vec3 coord2 =
vec3(tex_local.
x, tex_local.
y, local_z + grid_size_div * 2);
763 vec3 coord3 =
vec3(tex_local.
x, tex_local.
y, local_z + grid_size_div * 3);
764 vec3 coord4 =
vec3(tex_local.
x, tex_local.
y, local_z + grid_size_div * 4);
765 vec3 coord5 =
vec3(tex_local.
x, tex_local.
y, local_z + grid_size_div * 5);
766 vec3 coord6 =
vec3(tex_local.
x, tex_local.
y, local_z + grid_size_div * 6);
767 vec3 coord7 =
vec3(tex_local.
x, tex_local.
y, local_z + grid_size_div * 7);
768 vec3 coord8 =
vec3(tex_local.
x, tex_local.
y, local_z + grid_size_div * 8);
780 vec3 pc0 = p0 * 0.282095;
781 vec3 pc1 = p1 * 0.488603 * N.
y;
782 vec3 pc2 = p2 * 0.488603 * N.
z;
783 vec3 pc3 = p3 * 0.488603 * N.
x;
784 vec3 pc4 = p4 * 1.092548 * N.
x * N.
y;
785 vec3 pc5 = p5 * 1.092548 * N.
y * N.
z;
786 vec3 pc6 = p6 * 0.315392 * (3 * (N.
z * N.
z) - 1);
787 vec3 pc7 = p7 * 1.092548 * (N.
x * N.
z);
788 vec3 pc8 = p8 * 0.546274 * (N.
x * N.
x - N.
y * N.
y);
791 irradiance = max(pc0 + pc1 + pc2 + pc3 + pc4 + pc5 + pc6 + pc7 + pc8,
vec3(0.0));
799 float focus_factor = dir_data.a;
801 vec3 dominant_dir = normalize(dir_data.xyz * 2.0f - 1.0f);
803 float ndotl = max(0.0, dot(N, dominant_dir));
805 float lam = dot(N, dominant_dir) * 0.5f + 0.5f;
807 ff = (lam / max(focus_factor, 0.00001f));
808 #ifdef LM_DIRECTIONAL
834 const float PI = 3.14159265359;
838 F0 = mix(F0, albedo.rgb, metallic);
841 vec3 kD_indirect = (
vec3(1.0) - kS) * (1.0 - metallic);
842 vec3 indirect_diffuse = kD_indirect * irradiance * albedo.rgb;
848 const float MAX_REFLECTION_LOD = 4.0f;
850 float normal_blend = filtered_roughness;
851 vec3 parallax_N = normalize(mix(original_normal, N,
clamp(normal_blend + 0.1, 0.2, 1.0)));
868 specular = rprobe_color * (F0 * brdf.
x + brdf.
y);
876 rprobe_color =
vec3(0,0,0);
881 vec3 final_indirect = (indirect_diffuse + specular) * ao;
883 vec3 ambient =
vec3(0.005, 0.005, 0.005);
885 vec3 ambient_light = ambient * albedo.rgb;
886 float z_scale = z_tile / 24.0f;
887 vec3 tiles =
vec3(x_tile / 16.0f, y_tile /9.0f, depth /
uFar);