diff --git a/crates/moonwell-gfx/src/frame.rs b/crates/moonwell-gfx/src/frame.rs index d126e0f43..c914e9b0a 100644 --- a/crates/moonwell-gfx/src/frame.rs +++ b/crates/moonwell-gfx/src/frame.rs @@ -46,7 +46,7 @@ pub(crate) mod row { pub(crate) const WATER_WAVES: usize = 10; /// The mirrored scene: `x` on, `y` its plane (Bevy y), `z` the height band trusted whole. pub(crate) const WATER_MIRROR: usize = 11; - /// The sky's zenith stop, `rgb`, the reflected sky's top. + /// The sky's zenith stop (`rgb`) and shallow-bed caustics strength (`w`). pub(crate) const WATER_SKY: usize = 12; /// The white moon: `xyz` toward it, `w` its weight on the glitter. pub(crate) const WATER_MOON: usize = 13; diff --git a/crates/moonwell-gfx/src/options.rs b/crates/moonwell-gfx/src/options.rs index a256a161c..50f46df6d 100644 --- a/crates/moonwell-gfx/src/options.rs +++ b/crates/moonwell-gfx/src/options.rs @@ -335,6 +335,14 @@ const ITEMS: &[Item] = &[ ), slider("water.clarity", 0.02, 3.0, 0.01, "Мутность", "Murkiness"), slider("water.foam", 0.0, 1.0, 0.05, "Пена у берега", "Shore foam"), + slider( + "water.caustics", + 0.0, + 1.0, + 0.05, + "Блики на дне", + "Bed caustics", + ), switch("water.ripples", "Волны от персонажей", "Wakes"), slider( "water.ripple_strength", diff --git a/crates/moonwell-gfx/src/settings.rs b/crates/moonwell-gfx/src/settings.rs index a02842e1d..0597b7c46 100644 --- a/crates/moonwell-gfx/src/settings.rs +++ b/crates/moonwell-gfx/src/settings.rs @@ -176,6 +176,8 @@ pub struct WaterSettings { pub clarity: f64, /// The shore foam, 0..1. pub foam: f64, + /// Sunlight focused on a shallow bed, 0..1; off for existing settings. + pub caustics: f64, /// Ripples where units wade and swim: a wave field about the viewer. pub ripples: bool, /// How hard a unit pushes the water, 0..3. @@ -209,6 +211,7 @@ impl Default for WaterSettings { refraction: 1.0, clarity: 0.35, foam: 0.5, + caustics: 0.0, ripples: true, ripple_strength: 0.3, ripple_normal: 1.0, @@ -234,6 +237,7 @@ impl WaterSettings { refraction: self.refraction.clamp(0.0, 3.0), clarity: self.clarity.clamp(0.02, 3.0), foam: self.foam.clamp(0.0, 1.0), + caustics: self.caustics.clamp(0.0, 1.0), ripples: self.ripples, ripple_strength: self.ripple_strength.clamp(0.0, 3.0), ripple_normal: self.ripple_normal.clamp(0.0, 3.0), @@ -723,6 +727,10 @@ mod tests { assert!(!GfxSettings::default().post.enabled); assert!(!GfxSettings::default().wind.enabled); assert!(!GfxSettings::default().water.enabled); + assert_eq!(GfxSettings::default().water.caustics, 0.0); + let legacy: GfxSettings = toml::from_str("[water]\nenabled = true\n").unwrap(); + assert_eq!(legacy.water.caustics, 0.0); + assert_eq!(GfxSettings::forever().water.caustics, 0.0); } #[test] diff --git a/crates/moonwell-gfx/src/water.rs b/crates/moonwell-gfx/src/water.rs index d32a48936..e12f2c9e5 100644 --- a/crates/moonwell-gfx/src/water.rs +++ b/crates/moonwell-gfx/src/water.rs @@ -75,7 +75,7 @@ pub(crate) fn publish( ]; let (zenith, moon) = lighting.map_or(([0.0; 3], Vec3::ZERO), |l| (l.sky_stops()[0], l.moon_dir())); - frame.rows[row::WATER_SKY] = [zenith[0], zenith[1], zenith[2], 0.0]; + frame.rows[row::WATER_SKY] = [zenith[0], zenith[1], zenith[2], w.caustics as f32]; let moon = moon.normalize_or_zero(); frame.rows[row::WATER_MOON] = [ moon.x, @@ -393,6 +393,12 @@ fn mw_water(stock: vec3, shallow: vec4, deep: vec4, depth: f32, a var thick = -1.0; var bed_depth = -1.0; var behind = vec3(0.0); + var bed_xz = xz; + var caustic_valid = false; + var caustic_depth_match = 0.0; + var caustic_bed_depth = 0.0; + var bed_footprint = 0.0; + let caustic_strength = clamp(mw_lane(12u).w, 0.0, 1.0) * mw_lane(8u).x; let scene_on = q.w > 0.5 && textureDimensions(mw_scene).x > 1u; if (scene_on) { let uv = frag.xy / vec2(textureDimensions(mw_scene)); @@ -403,9 +409,25 @@ fn mw_water(stock: vec3, shallow: vec4, deep: vec4, depth: f32, a let eye = view.world_position.xyz; let bed = eye + (world - eye) * (straight.a / max(water_z, 1e-4)); bed_depth = select(100.0, max(world.y - bed.y, 0.0), straight.a > 0.0); + // The waves bend what is seen through them, the wakes' rings most of all. let bend = n.xz * (q.x * 0.01 * near) - wake * (q.x * 0.012); var s = textureSampleLevel(mw_scene, mw_scene_samp, uv + bend, 0.0); + if (caustic_strength > 0.0) { + let bent_uv = uv + bend; + caustic_valid = straight.a > water_z && s.a > water_z + && all(bent_uv > vec2(0.002)) && all(bent_uv < vec2(0.998)); + caustic_depth_match = 1.0 - smoothstep(0.05, 0.3, abs(s.a - straight.a)); + // Reconstruct the same bent scene sample that supplies the transmitted colour. + let ray = view.view_from_clip * vec4(bent_uv.x * 2.0 - 1.0, 1.0 - bent_uv.y * 2.0, 1.0, 1.0); + caustic_valid = caustic_valid && ray.z < -1e-4; + let bent_bed = (view.world_from_view * vec4(ray.xyz * (s.a / max(-ray.z, 1e-4)), 1.0)).xyz; + caustic_valid = caustic_valid && bent_bed.y < world.y; + bed_xz = bent_bed.xz; + caustic_bed_depth = max(world.y - bent_bed.y, 0.0); + let ray_scale = clamp(s.a / max(water_z, 1e-4), 1.0, 4.0); + bed_footprint = length(abs(d_xz_dx) + abs(d_xz_dy)) * ray_scale; + } // A bent tap that lands in front of the water reads the straight one instead. if (s.a > 0.0 && s.a < water_z) { s = straight; @@ -416,7 +438,8 @@ fn mw_water(stock: vec3, shallow: vec4, deep: vec4, depth: f32, a body_rgb = mix(shallow.rgb, deep.rgb, absorbed * MW_BODY_DEPTH_MAX); } // The foam line's anti-aliasing, taken here, where the flow is still uniform. - let aa = clamp(fwidth(bed_depth), 0.004, 0.05); + let bed_width = fwidth(bed_depth); + let aa = clamp(bed_width, 0.004, 0.05); let sun = mw_lane(2u).xyz; let sun_xz = normalize(sun.xz + vec2(1e-4)); let body = body_rgb * lit * (1.0 + MW_BODY_RIPPLE_SHADE * dot(n.xz, sun_xz)); @@ -425,6 +448,22 @@ fn mw_water(stock: vec3, shallow: vec4, deep: vec4, depth: f32, a let opacity = mix(shallow.w, deep.w, sqrt(clamp(depth, 0.0, 1.0))); var under = body; if (scene_on) { + if (caustic_strength > 0.0 && caustic_valid && view.world_position.y > world.y) { + // Original procedural focusing on the bed; stock 1.12 uses a swatch and sheet + // (benilla-re terrain/watershade, 0x6851b0), without this extension. + let pos = bed_xz * 1.7; + let a = sin(pos.x + sin(pos.y * 0.8 + t * 0.7) + t * 0.35); + let b = sin(pos.y - sin(pos.x * 0.7 - t * 0.6) - t * 0.3); + let focus = pow(clamp(1.0 - abs(a + b) * 0.5, 0.0, 1.0), 8.0); + let fade = (1.0 - smoothstep(0.2, 3.0, caustic_bed_depth)) + * exp(-q.y * caustic_bed_depth * 0.5) + * (1.0 - smoothstep(0.1, 0.5, bed_width)) + * (1.0 - smoothstep(0.2, 0.8, bed_footprint)) + * (1.0 - smoothstep(MW_SLOPE_TRUSTED, MW_SLOPE_LIMIT, tilt)) + * smoothstep(0.12, 0.4, normalize(view.world_position.xyz - world).y) + * caustic_depth_match; + behind = behind * (1.0 + focus * fade * caustic_strength * 0.45); + } under = mix(body, behind, exp(-MW_EXTINCTION * q.y * min(thick, 24.0))); } @@ -472,7 +511,9 @@ fn mw_water(stock: vec3, shallow: vec4, deep: vec4, depth: f32, a // themselves catch a thin sheen of the sky, as Cataclysm's do. var foam = wake_foam * 0.14; rgb = rgb + sky * lit * (smoothstep(0.12, 0.55, length(wake)) * 0.16); - if (bed_depth >= 0.0) { + let foam_edge_max = (MW_FOAM_LINE + MW_FOAM_RUNUP) * (1.0 + MW_FOAM_WARP); + if (bed_depth >= 0.0 && q.z > 0.0 + && (bed_depth < foam_edge_max + aa || bed_depth - foam_edge_max < MW_FOAM_BUBBLES)) { // The line: its edge wavers a little and runs up and back with a slow swell whose phase // drifts along the coast; behind it bubbles, cut from the noise at a threshold that // climbs with depth, so they thin out to nothing.