// Shared WGSL structs, math, procedural filtering, Vogel Disk PCF, // Dual Paraboloid sampling, and PBR GGX helpers. struct GlobalUniforms { view_proj: mat4x4, inv_view_proj: mat4x4, sun_view_proj: mat4x4, camera_pos: vec4, // xyz = pos, w = time_secs sun_dir: vec4, // xyz = normalized toward sun, w = cloud_coverage sun_color: vec4, // rgb = sun HDR color, w = wetness cloud_wind: vec4, // xy = wind offset, z = pcf_samples, w = bicubic_enabled dp_origin: vec4, // xyz = Dual Paraboloid probe origin, w = hemi_sign (+1 or -1) screen_params: vec4, // x = width, y = height, z = near(0.3), w = far(950.0) cloud_params: vec4, // xy = cloud_seed_offset, z = cos(angle), w = sin(angle) cloud_shadow_bounds: vec4, // xy = world center XZ, z = span meters, w = 1.0 / span flags: vec4, // x = light_count, y = show_render_pip, z = suppression_x1000, w = damage_flash_x1000 }; fn cloud_weather_uvs(world_xz: vec2, wind_xy: vec2, params: vec4) -> vec4 { let p = world_xz + wind_xy + params.xy; let c1 = params.z; let s1 = params.w; let rot1 = vec2(c1 * p.x - s1 * p.y, s1 * p.x + c1 * p.y); // Incommensurate secondary octave (~1.13 rad offset + subtle domain warp) breaks grid tiling let c2 = c1 * 0.4266 - s1 * 0.9044; let s2 = s1 * 0.4266 + c1 * 0.9044; let rot2 = vec2(c2 * p.x - s2 * p.y, s2 * p.x + c2 * p.y); let warp = vec2(sin(rot2.y * 0.0011), cos(rot1.x * 0.0009)) * 0.085; let uv1 = rot1 * 0.00042 + warp; let uv2 = rot2 * 0.000575 - warp * 0.65 + vec2(0.37, 0.71); return vec4(uv1, uv2); } struct GpuPointLight { pos_radius: vec4, // xyz = world pos, w = radius color_intensity: vec4, // rgb = color, w = intensity }; const PI: f32 = 3.14159265359; const GOLDEN_ANGLE: f32 = 2.39996323; fn interleaved_gradient_noise(pixel_pos: vec2) -> f32 { let magic = vec3(0.06711056, 0.00583715, 52.9829189); return fract(magic.z * fract(dot(pixel_pos, magic.xy))); } fn vogel_disk_sample(sample_idx: u32, sample_count: u32, phi: f32) -> vec2 { let r = sqrt((f32(sample_idx) + 0.5) / f32(sample_count)); let theta = f32(sample_idx) * GOLDEN_ANGLE + phi; return vec2(cos(theta), sin(theta)) * r; } /// 16-tap (configurable 8/16/32) Vogel Disk PCF with IGN rotation & normal-offset slope bias. fn sample_sun_shadow_vogel( shadow_tex: texture_depth_2d, shadow_sampler: sampler_comparison, sun_vp: mat4x4, world_pos: vec3, world_normal: vec3, sun_dir: vec3, pixel_pos: vec2, sample_count: u32, ) -> f32 { let ndl = clamp(dot(world_normal, sun_dir), 0.0, 1.0); let normal_bias = world_normal * mix(0.22, 0.04, ndl); let clip = sun_vp * vec4(world_pos + normal_bias, 1.0); let ndc = clip.xyz / clip.w; let uv = vec2(ndc.x * 0.5 + 0.5, -ndc.y * 0.5 + 0.5); if (uv.x <= 0.005 || uv.x >= 0.995 || uv.y <= 0.005 || uv.y >= 0.995 || ndc.z <= 0.0 || ndc.z >= 1.0) { return 1.0; } let texel_size = 1.0 / 2048.0; let radius = 3.2 * texel_size; let phi = interleaved_gradient_noise(pixel_pos) * 6.2831853; let bias = max(0.0004, 0.0018 * (1.0 - ndl)); let ref_z = ndc.z - bias; var sum = 0.0; let count = clamp(sample_count, 4u, 32u); for (var i = 0u; i < count; i = i + 1u) { let offset = vogel_disk_sample(i, count, phi) * radius; sum += textureSampleCompareLevel(shadow_tex, shadow_sampler, uv + offset, ref_z); } return sum / f32(count); } /// Samples the 2D projected volumetric cloud shadow map covering the map or a camera-snapped window. fn sample_cloud_shadow( cloud_shadow_tex: texture_2d, samp: sampler, world_pos: vec3, bounds: vec4, ) -> f32 { let uv = (world_pos.xz - bounds.xy) * bounds.w + vec2(0.5, 0.5); if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) { return 0.85; } return textureSampleLevel(cloud_shadow_tex, samp, uv, 0.0).r; } /// 4-tap Catmull-Rom / B-Spline bicubic texture filter for heavily filtered low-res shader textures! fn sample_bicubic_array( tex: texture_2d_array, samp: sampler, uv: vec2, layer: i32, ) -> vec4 { let dims = vec2(textureDimensions(tex, 0)); let inv_dims = 1.0 / dims; let tc = uv * dims - 0.5; let f = fract(tc); let tc_floor = tc - f; // B-Spline weights -> 4 bilinear hardware taps let f2 = f * f; let f3 = f2 * f; let w0 = (1.0 / 6.0) * (-f3 + 3.0 * f2 - 3.0 * f + 1.0); let w1 = (1.0 / 6.0) * (3.0 * f3 - 6.0 * f2 + 4.0); let w2 = (1.0 / 6.0) * (-3.0 * f3 + 3.0 * f2 + 3.0 * f + 1.0); let w3 = (1.0 / 6.0) * f3; let s0 = w0 + w1; let s1 = w2 + w3; let o0 = (tc_floor + w1 / max(s0, vec2(1e-4)) - 0.5) * inv_dims; let o1 = (tc_floor + 2.0 + w3 / max(s1, vec2(1e-4)) - 0.5) * inv_dims; let c00 = textureSample(tex, samp, vec2(o0.x, o0.y), layer); let c10 = textureSample(tex, samp, vec2(o1.x, o0.y), layer); let c01 = textureSample(tex, samp, vec2(o0.x, o1.y), layer); let c11 = textureSample(tex, samp, vec2(o1.x, o1.y), layer); return (c00 * s0.x + c10 * s1.x) * s0.y + (c01 * s0.x + c11 * s1.x) * s1.y; } /// Samples the 360-degree Dual Paraboloid reflection map with roughness-driven mip lookup. fn sample_dual_paraboloid( dp_tex: texture_2d_array, samp: sampler, refl_dir: vec3, roughness: f32, ) -> vec3 { let r = normalize(refl_dir); let layer = select(1, 0, r.z >= 0.0); let az = abs(r.z); let uv = (r.xy / (az + 1.0)) * vec2(0.5, -0.5) + vec2(0.5, 0.5); let lod = clamp(roughness * 5.0, 0.0, 5.0); return textureSampleLevel(dp_tex, samp, clamp(uv, vec2(0.002), vec2(0.998)), layer, lod).rgb; } /// Procedural Caucasus overcast winter sky & sun halo (used in main backdrop & DPR sky dome). fn eval_winter_sky(dir: vec3, sun_dir: vec3, cloud_cov: f32) -> vec3 { let d = normalize(dir); let up = clamp(d.y, 0.0, 1.0); let horizon = vec3(0.42, 0.48, 0.55); let zenith = vec3(0.14, 0.22, 0.34); var sky = mix(horizon, zenith, pow(up, 0.55)); // Overcast winter chill desaturation sky = mix(sky, vec3(0.36, 0.40, 0.44), cloud_cov * 0.45); let sun_dot = max(dot(d, sun_dir), 0.0); sky += vec3(1.15, 0.95, 0.72) * pow(sun_dot, 16.0) * (0.55 - cloud_cov * 0.3); sky += vec3(3.2, 2.7, 2.0) * pow(sun_dot, 320.0); return sky; } /// GGX / Cook-Torrance PBR helper. fn eval_pbr_direct( N: vec3, V: vec3, L: vec3, albedo: vec3, roughness: f32, metallic: f32, radiance: vec3, ) -> vec3 { let ndl = max(dot(N, L), 0.0); if (ndl <= 0.0) { return vec3(0.0); } let H = normalize(V + L); let ndv = max(dot(N, V), 0.001); let ndh = max(dot(N, H), 0.0); let hdv = max(dot(H, V), 0.0); let r = clamp(roughness, 0.04, 1.0); let a = r * r; let a2 = a * a; let denom = ndh * ndh * (a2 - 1.0) + 1.0; let d_ggx = a2 / (PI * denom * denom + 1e-5); let k = ((r + 1.0) * (r + 1.0)) / 8.0; let g_v = ndv / (ndv * (1.0 - k) + k); let g_l = ndl / (ndl * (1.0 - k) + k); let g_smith = g_v * g_l; let f0 = mix(vec3(0.04), albedo, metallic); let fresnel = f0 + (1.0 - f0) * pow(1.0 - hdv, 5.0); let spec = (d_ggx * g_smith * fresnel) / (4.0 * ndv * ndl + 0.001); let kd = (vec3(1.0) - fresnel) * (1.0 - metallic); let diff = kd * albedo / PI; return (diff + spec) * radiance * ndl; }