From 7404c2b94eb88c1b64e76f4a31a8abf1efc42ef3 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Wed, 21 Oct 2026 18:20:26 +0200 Subject: [PATCH] Sky, fog and water fog colours blend across biomes (GaussianSampler) The camera's biome alone gave the sky, fog and water fog colours, so they jumped at every biome boundary (most visibly the Nether fog). Vanilla's EnvironmentAttributeProbe weights the noise biomes of a 6x6x6 quart neighbourhood around the camera with GaussianSampler's kernel and lerps the spatially interpolated attributes between them (SpatialAttributeInterpolator, ARGB.srgbLerp); unloaded chunks count as plains. Fixes #797 Co-Authored-By: Claude Opus 5.5 --- .../examples/environment_fixture.rs | 6 +- crates/horizon-client/src/biome.rs | 99 +++++++++++++++++++ crates/horizon-client/src/environment.rs | 24 +++-- crates/horizon-client/src/game.rs | 41 +++++--- crates/horizon-client/src/game/render_tick.rs | 8 +- 5 files changed, 150 insertions(+), 28 deletions(-) diff --git a/crates/horizon-client/examples/environment_fixture.rs b/crates/horizon-client/examples/environment_fixture.rs index d92123a2c..7f331962a 100644 --- a/crates/horizon-client/examples/environment_fixture.rs +++ b/crates/horizon-client/examples/environment_fixture.rs @@ -30,7 +30,11 @@ fn main() -> horizon_client::Result<()> { ), }; let info = biomes.get(biomes.id(&format!("minecraft:{}", f[4]))); - let mut sky = environment.sample(f[1].parse()?, info.sky, info.fog); + let mut sky = environment.sample( + f[1].parse()?, + info.sky.map(horizon_client::biome::rgb), + info.fog.map(horizon_client::biome::rgb), + ); if dimension == "overworld" { apply_weather(&mut sky, f[2].parse()?, f[3].parse()?); } diff --git a/crates/horizon-client/src/biome.rs b/crates/horizon-client/src/biome.rs index e0c6900cf..63ad004d4 100644 --- a/crates/horizon-client/src/biome.rs +++ b/crates/horizon-client/src/biome.rs @@ -182,3 +182,102 @@ impl Biomes { } } } + +/// A 24-bit colour as sRGB components in 0..1. +pub fn rgb(c: u32) -> [f32; 3] { + [16, 8, 0].map(|shift| (c >> shift & 255) as f32 / 255.0) +} + +/// `GaussianSampler.GAUSSIAN_SAMPLE_KERNEL`. +const GAUSSIAN_KERNEL: [f64; 7] = [0.0, 1.0, 4.0, 6.0, 4.0, 1.0, 0.0]; + +/// `EnvironmentAttributeProbe.tick`: `GaussianSampler.sample` over the noise +/// biomes (quart cells) around `position`, in blocks. Each biome comes once +/// with its summed weight, in the order it was first sampled, as +/// `SpatialAttributeInterpolator` keeps them (zero weights included). +pub fn gaussian_weights( + position: [f64; 3], + mut biome_at_quart: impl FnMut(i32, i32, i32) -> u16, +) -> Vec<(u16, f64)> { + let p = position.map(|c| c * 0.25 - 0.5); + let integral = p.map(|c| c.floor() as i32); + let relative: [f64; 3] = std::array::from_fn(|i| p[i] - f64::from(integral[i])); + let weight = |axis: usize, i: usize| { + let (start, end) = (GAUSSIAN_KERNEL[i + 1], GAUSSIAN_KERNEL[i]); + start + relative[axis] * (end - start) + }; + let mut weights: Vec<(u16, f64)> = Vec::new(); + for z in 0..6 { + let wz = weight(2, z); + for x in 0..6 { + let wx = weight(0, x); + for y in 0..6 { + let wy = weight(1, y); + let biome = biome_at_quart( + integral[0] - 2 + x as i32, + integral[1] - 2 + y as i32, + integral[2] - 2 + z as i32, + ); + let w = wx * wy * wz; + match weights.iter_mut().find(|(b, _)| *b == biome) { + Some((_, total)) => *total += w, + None => weights.push((biome, w)), + } + } + } + } + weights +} + +/// `SpatialAttributeInterpolator.applyAttributeLayer` for an RGB colour +/// (`ARGB.srgbLerp`: a component-wise lerp of the sRGB values). +pub fn blend_rgb(weights: &[(u16, f64)], mut value: impl FnMut(u16) -> [f32; 3]) -> [f32; 3] { + let mut result: Option<[f32; 3]> = None; + let mut accumulated = 0.0; + for &(biome, weight) in weights { + let v = value(biome); + accumulated += weight; + result = Some(match result { + None => v, + Some(r) => { + let t = (weight / accumulated) as f32; + std::array::from_fn(|i| r[i] + (v[i] - r[i]) * t) + } + }); + } + result.unwrap_or([0.0; 3]) +} + +#[cfg(test)] +mod blend_tests { + use super::*; + + #[test] + fn one_biome_keeps_its_colour() { + let weights = gaussian_weights([10.3, 64.0, -7.9], |_, _, _| 4); + assert_eq!(weights.len(), 1); + // The kernel sums to 16 per axis whatever the offset. + assert!((weights[0].1 - 16f64.powi(3)).abs() < 1e-9); + assert_eq!(blend_rgb(&weights, |_| [0.2, 0.4, 0.6]), [0.2, 0.4, 0.6]); + } + + #[test] + fn colour_changes_gradually_across_a_boundary() { + // Biome 1 for quart x >= 0 (block x >= 0), biome 0 below. + let at = |x: f64| { + let w = gaussian_weights([x, 40.0, 0.0], |qx, _, _| u16::from(qx >= 0)); + blend_rgb(&w, |b| if b == 1 { [1.0, 0.0, 0.0] } else { [0.0; 3] })[0] + }; + assert_eq!(at(-12.0), 0.0); + assert_eq!(at(12.0), 1.0); + // Halfway at the boundary, rising steadily through it. + assert!((at(0.0) - 0.5).abs() < 1e-5, "{}", at(0.0)); + let mut last = 0.0; + for i in -16..=16 { + let v = at(f64::from(i) * 0.5); + assert!(v >= last - 1e-6, "{v} < {last} at {i}"); + last = v; + } + assert!(at(-2.0) > 0.0 && at(-2.0) < 0.5); + } +} diff --git a/crates/horizon-client/src/environment.rs b/crates/horizon-client/src/environment.rs index ee2c921d0..20855b749 100644 --- a/crates/horizon-client/src/environment.rs +++ b/crates/horizon-client/src/environment.rs @@ -230,16 +230,20 @@ impl Environment { fn base(&self, name: &str) -> [f32; 4] { self.base.get(name).copied().unwrap_or([0.0, 0.0, 0.0, 1.0]) } - /// Sky state for a time of day, with the biome's sky and fog colours as base values. - pub fn sample(&self, ticks: i64, biome_sky: Option, biome_fog: Option) -> Sky { - let rgb = |c: u32| { - [ - (c >> 16 & 255) as f32 / 255.0, - (c >> 8 & 255) as f32 / 255.0, - (c & 255) as f32 / 255.0, - 1.0, - ] - }; + /// The dimension's value of an RGB attribute (what biomes without it keep). + pub fn base_rgb(&self, name: &str) -> [f32; 3] { + let b = self.base(name); + [b[0], b[1], b[2]] + } + /// Sky state for a time of day, with the biomes' (blended) sky and fog + /// colours as base values. + pub fn sample( + &self, + ticks: i64, + biome_sky: Option<[f32; 3]>, + biome_fog: Option<[f32; 3]>, + ) -> Sky { + let rgb = |c: [f32; 3]| [c[0], c[1], c[2], 1.0]; let v = |name: &str, base: [f32; 4]| self.value(name, base, ticks); let three = |a: [f32; 4]| [a[0], a[1], a[2]]; let scalar = |name: &str| { diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 4013bb04b..0a9827532 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -9262,9 +9262,36 @@ impl Game<'_> { .get(&(block[0] >> 4, block[2] >> 4)) .map(|c| c.biome(block[0], block[1], block[2])) .unwrap_or(0); - let info = self.res.biomes().get(biome).clone(); let environment = self.dim_environment(); - let mut sky = environment.sample(self.world.day_time, info.sky, info.fog); + // EnvironmentAttributeProbe: the spatially interpolated biome colours, + // Gaussian-weighted over the noise biomes around the camera (plains + // where no chunk is loaded, as ClientLevel.getUncachedNoiseBiome). + let biomes = self.res.biomes(); + let weights = { + let chunks = &self.world.chunks; + let plains = biomes.id("minecraft:plains"); + horizon_client::biome::gaussian_weights(camera.to_array(), |qx, qy, qz| { + chunks + .get(&(qx >> 2, qz >> 2)) + .map_or(plains, |c| c.biome(qx * 4, qy * 4, qz * 4)) + }) + }; + let blend = |pick: fn(&horizon_client::biome::BiomeInfo) -> Option, base: [f32; 3]| { + horizon_client::biome::blend_rgb(&weights, |b| { + pick(biomes.get(b)).map_or(base, horizon_client::biome::rgb) + }) + }; + let biome_sky = blend( + |b| b.sky, + environment.base_rgb("minecraft:visual/sky_color"), + ); + let biome_fog = blend( + |b| b.fog, + environment.base_rgb("minecraft:visual/fog_color"), + ); + // visual/water_fog_color: no dimension sets it, its default is #050533. + let water_fog = blend(|b| Some(b.water_fog), [0.0; 3]); + let mut sky = environment.sample(self.world.day_time, Some(biome_sky), Some(biome_fog)); let (rain, thunder) = if self.dim == Dim::Overworld { self.world.weather.levels(alpha as f32) } else { @@ -9435,14 +9462,6 @@ impl Game<'_> { self.weather_geometry(camera, alpha, &mut entities) }); } - let water_fog = { - let c = info.water_fog; - [ - (c >> 16 & 255) as f32 / 255.0, - (c >> 8 & 255) as f32 / 255.0, - (c & 255) as f32 / 255.0, - ] - }; let border = (!title) .then(|| self.border_draw(camera.to_array(), alpha as f32)) .flatten(); @@ -9467,7 +9486,7 @@ impl Game<'_> { cave_culling, water_fog, fog: Some({ - let mut fog = self.frame_fog(camera, &sky, biome); + let mut fog = self.frame_fog(camera, &sky, biome, water_fog); // Horizon's fog setting: no distance fog (fluids keep theirs). if !self.settings.horizon.fog && !underwater diff --git a/crates/horizon-client/src/game/render_tick.rs b/crates/horizon-client/src/game/render_tick.rs index cdc7a68c6..a938e80f3 100644 --- a/crates/horizon-client/src/game/render_tick.rs +++ b/crates/horizon-client/src/game/render_tick.rs @@ -63,11 +63,11 @@ impl Game<'_> { camera: glam::DVec3, sky: &horizon_client::environment::Sky, biome: u16, + water_fog_color: [f32; 3], ) -> FogData { let forward = self.forward(); let at = [camera.x, camera.y, camera.z]; let fog_type = fog_type(&self.world, self.res.facts(), at, forward); - let water = self.res.biomes().get(biome).water_fog; let (rain, thunder) = if self.dim == Dim::Overworld { self.world.weather.levels(self.clock.fraction() as f32) } else { @@ -85,11 +85,7 @@ impl Game<'_> { &FogInputs { fog_type, sky, - water_fog_color: [ - (water >> 16 & 255) as f32 / 255.0, - (water >> 8 & 255) as f32 / 255.0, - (water & 255) as f32 / 255.0, - ], + water_fog_color, biome, render_distance_chunks: self.terrain_distance(), distant_terrain: self.lod.is_some(),