| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- // Cold Mountain River Water Shader
- // Uses Dual Paraboloid 360 Reflections + Animated Flow Ripple Normals + Schlick Fresnel
- // + Vogel Disk PCF Sun Shadows + 2D Projected Cloud Shadows + Shoreline Foam.
- @group(0) @binding(0) var<uniform> u: GlobalUniforms;
- @group(0) @binding(1) var<storage, read> lights: array<GpuPointLight>;
- @group(0) @binding(6) var shadow_tex: texture_depth_2d;
- @group(0) @binding(7) var shadow_sampler: sampler_comparison;
- @group(0) @binding(8) var cloud_shadow_tex: texture_2d<f32>;
- @group(0) @binding(9) var linear_clamp_sampler: sampler;
- @group(0) @binding(10) var dp_tex: texture_2d_array<f32>;
- struct WaterIn {
- @location(0) position: vec3<f32>,
- @location(1) flow_foam_depth: vec4<f32>,
- };
- struct WaterOut {
- @builtin(position) position: vec4<f32>,
- @location(0) world_pos: vec3<f32>,
- @location(1) flow_foam_depth: vec4<f32>,
- };
- @vertex
- fn vs_main(v: WaterIn) -> WaterOut {
- var out: WaterOut;
- let t = u.camera_pos.w;
- let wave = 0.04 * sin(v.position.x * 0.65 + t * 2.2) * cos(v.position.z * 0.55 - t * 1.8)
- * clamp(v.flow_foam_depth.w, 0.0, 1.0);
- let wp = vec3<f32>(v.position.x, v.position.y + wave, v.position.z);
- out.position = u.view_proj * vec4<f32>(wp, 1.0);
- out.world_pos = wp;
- out.flow_foam_depth = v.flow_foam_depth;
- return out;
- }
- @fragment
- fn fs_main(in: WaterOut) -> @location(0) vec4<f32> {
- let t = u.camera_pos.w;
- let flow = normalize(in.flow_foam_depth.xy + vec2<f32>(0.001, 0.001));
- let p = in.world_pos.xz;
- // Multi-directional organic river ripples (avoids regular grid striping)
- let q1 = p * 0.65 - flow * (t * 1.9);
- let q2 = p * 1.35 - flow * (t * 3.1) + vec2<f32>(7.3, -3.1);
- let d1 = vec2<f32>(0.73, 0.68);
- let d2 = vec2<f32>(-0.61, 0.79);
- let d3 = vec2<f32>(0.91, -0.41);
- let d4 = vec2<f32>(-0.34, -0.94);
- let ph1 = dot(q1, d1) * 2.3;
- let ph2 = dot(q1, d2) * 3.1 + 1.4;
- let ph3 = dot(q2, d3) * 2.7 - 0.8;
- let ph4 = dot(q2, d4) * 3.9 + 2.1;
- let grad = d1 * cos(ph1) * 0.018
- + d2 * cos(ph2) * 0.015
- + d3 * cos(ph3) * 0.013
- + d4 * cos(ph4) * 0.010;
- let N = normalize(vec3<f32>(-grad.x, 1.0, -grad.y));
- let V = normalize(u.camera_pos.xyz - in.world_pos);
- let sun_d = normalize(u.sun_dir.xyz);
- let R = reflect(-V, N);
- // Sharp Dual Paraboloid reflection of sky, clouds, bridge & ruined skyline!
- let refl = sample_dual_paraboloid(dp_tex, linear_clamp_sampler, R, 0.04);
- let ndv = max(dot(N, V), 0.001);
- let fresnel = 0.04 + 0.96 * pow(1.0 - ndv, 4.5);
- let sun_vis = sample_sun_shadow_vogel(
- shadow_tex, shadow_sampler, u.sun_view_proj,
- in.world_pos, Vec3_Y(), sun_d, in.position.xy, 12u
- );
- let cloud_vis = sample_cloud_shadow(cloud_shadow_tex, linear_clamp_sampler, in.world_pos, u.cloud_shadow_bounds);
- // Deep icy Caucasus river teal-slate base water + crisp sun glitter
- let deep_col = vec3<f32>(0.022, 0.052, 0.066) * (0.45 + 0.55 * cloud_vis);
- let H = normalize(V + sun_d);
- let spec = pow(max(dot(N, H), 0.0), 260.0) * 2.2 * sun_vis * cloud_vis;
- var color = mix(deep_col, refl, clamp(fresnel * 1.15, 0.14, 0.96)) + u.sun_color.rgb * spec;
- // Non-periodic multi-octave shoreline cold-water foam confined to shallow edges
- let depth_foam = 1.0 - smoothstep(0.0, 0.42, in.flow_foam_depth.w);
- let shore_mask = in.flow_foam_depth.z * depth_foam;
- let fp = p * 0.42 - flow * (t * 0.35);
- let fn1 = water_vnoise(fp);
- let fn2 = water_vnoise(fp * 2.15 + vec2<f32>(11.7, -5.3) - flow * (t * 0.55));
- let fn3 = water_vnoise(fp * 4.6 + vec2<f32>(-4.1, 8.9));
- let foam_fbm = fn1 * 0.52 + fn2 * 0.33 + fn3 * 0.15;
- let foam = clamp(shore_mask * smoothstep(0.46, 0.78, foam_fbm), 0.0, 0.55);
- color = mix(color, vec3<f32>(0.68, 0.74, 0.78) * (0.5 + 0.5 * cloud_vis), foam * 0.52);
- // Valley fog match
- let cam_dist = length(in.world_pos - u.camera_pos.xyz);
- let fog_scale = select(1.0, 0.20, u.screen_params.w > 1500.0);
- let fog_amount = 1.0 - exp(-cam_dist * 0.0024 * fog_scale);
- let fog_col = eval_winter_sky(normalize(in.world_pos - u.camera_pos.xyz), sun_d, u.sun_dir.w);
- color = mix(color, fog_col, clamp(fog_amount, 0.0, 0.90));
- return vec4<f32>(color, 1.0);
- }
- fn water_hash21(p: vec2<f32>) -> f32 {
- let p3 = fract(vec3<f32>(p.x, p.y, p.x) * 0.1031);
- let d = p3 + dot(p3, p3.yzx + 33.33);
- return fract((d.x + d.y) * d.z);
- }
- fn water_vnoise(p: vec2<f32>) -> f32 {
- let i = floor(p);
- let f = fract(p);
- let u2 = f * f * (3.0 - 2.0 * f);
- let a = water_hash21(i);
- let b = water_hash21(i + vec2<f32>(1.0, 0.0));
- let c = water_hash21(i + vec2<f32>(0.0, 1.0));
- let d = water_hash21(i + vec2<f32>(1.0, 1.0));
- return mix(mix(a, b, u2.x), mix(c, d, u2.x), u2.y);
- }
- fn Vec3_Y() -> vec3<f32> {
- return vec3<f32>(0.0, 1.0, 0.0);
- }
|