diff --git a/crates/horizon-client/src/game/shader_packs.rs b/crates/horizon-client/src/game/shader_packs.rs index 7fa2ea3f6..f5f7a5fba 100644 --- a/crates/horizon-client/src/game/shader_packs.rs +++ b/crates/horizon-client/src/game/shader_packs.rs @@ -43,7 +43,10 @@ impl Game<'_> { } let folder = dimension_folder(&pack, dimension); let inputs = renderer.pack_inputs(); - let post = load_post(&pack, &folder, &properties, ¯os, &inputs); + let mut post = load_post(&pack, &folder, &properties, ¯os, &inputs); + let biomes = self.res.biomes(); + let names: Vec<&str> = biomes.list.iter().map(|b| b.name.as_str()).collect(); + post.custom.set_biomes(&names); let text = pack.read_text("block.properties").unwrap_or_default(); let ids = BlockIds::parse(&Properties::parse(&text, ¯os)); for note in &ids.notes { @@ -80,6 +83,12 @@ impl Game<'_> { let lerp = |(old, new): (f32, f32)| old + (new - old) * partial; let at = camera.floor().as_ivec3(); let light = self.world.light(at.x, at.y, at.z); + let biome = self + .world + .chunks + .get(&(at.x >> 4, at.z >> 4)) + .map(|c| c.biome(at.x, at.y, at.z)); + let info = biome.map(|b| self.res.biomes().get(b)); PackInputs { day_time: self.world.day_time, rain: lerp(self.world.weather.rain_level), @@ -94,6 +103,13 @@ impl Game<'_> { eye_light: [i32::from(light & 15), i32::from(light >> 4)], hide_gui: self.ui.hide_gui, brightness: self.settings.gamma, + biome: biome.map_or(-1, i32::from), + biome_category: info.map_or(0, |b| horizon_shaders::biome_category(&b.name)), + biome_precipitation: info.map_or(0, |b| { + horizon_shaders::biome_precipitation(b.precipitation, b.temperature) + }), + temperature: info.map_or(0.0, |b| b.temperature), + rainfall: info.map_or(0.0, |b| b.downfall), } } } diff --git a/crates/horizon-client/src/gfx/pack_uniforms.rs b/crates/horizon-client/src/gfx/pack_uniforms.rs index c7c4cfd8b..ad53b509a 100644 --- a/crates/horizon-client/src/gfx/pack_uniforms.rs +++ b/crates/horizon-client/src/gfx/pack_uniforms.rs @@ -23,6 +23,12 @@ pub struct PackInputs { pub hide_gui: bool, /// The brightness option, 0..1. pub brightness: f32, + /// The biome at the camera (3.4): registry id, `CAT_*`, `PPT_*`, temperature, downfall. + pub biome: i32, + pub biome_category: i32, + pub biome_precipitation: i32, + pub temperature: f32, + pub rainfall: f32, } /// One uniform's value: up to 16 floats or 3 integers. @@ -134,6 +140,11 @@ pub const NAMES: &[&str] = &[ "heldBlockLightValue", "heldBlockLightValue2", "renderStage", + "biome", + "biome_category", + "biome_precipitation", + "temperature", + "rainfall", "gbufferModelView", "gbufferModelViewInverse", "gbufferProjection", @@ -290,6 +301,11 @@ impl FrameUniforms { "screenBrightness" => Value::f(&[inputs.brightness]), "hideGUI" => Value::i(&[inputs.hide_gui as i32]), "eyeBrightness" => Value::i(&eye.map(|v| v as i32)), + "biome" => Value::i(&[inputs.biome]), + "biome_category" => Value::i(&[inputs.biome_category]), + "biome_precipitation" => Value::i(&[inputs.biome_precipitation]), + "temperature" => Value::f(&[inputs.temperature]), + "rainfall" => Value::f(&[inputs.rainfall]), "eyeBrightnessSmooth" => Value::i(&self.eye_smooth.map(|v| v.round() as i32)), "gbufferModelView" => m4(view), "gbufferModelViewInverse" => m4(view.inverse()), diff --git a/crates/horizon-shaders/src/biomes.rs b/crates/horizon-shaders/src/biomes.rs new file mode 100644 index 000000000..c9ede57e0 --- /dev/null +++ b/crates/horizon-shaders/src/biomes.rs @@ -0,0 +1,159 @@ +//! The biome parameters and constants of custom uniform expressions (spec 3.4): `BIOME_`, +//! `CAT_` and `PPT_`. +//! +//! Decisions (impl-plan.md, OQ-11): +//! - `biome` is the biome's index in the game's biome registry, which lists vanilla's biomes +//! sorted by identifier; `BIOME_` is the index of `minecraft:`. Names from before +//! the 1.18 renames map to the biome that replaced them. A `BIOME_` name that matches no biome +//! reads -1, so it never equals the player's biome. +//! - `biome_category` comes from the biome's name (categories left the game in 1.19). + +use std::collections::HashMap; + +/// `CAT_*`, in the order of their values. +pub const CATEGORIES: [&str; 17] = [ + "NONE", + "TAIGA", + "EXTREME_HILLS", + "JUNGLE", + "MESA", + "PLAINS", + "SAVANNA", + "ICY", + "THE_END", + "BEACH", + "FOREST", + "OCEAN", + "DESERT", + "RIVER", + "SWAMP", + "MUSHROOM", + "NETHER", +]; + +/// Old biome names and the biomes that replaced them. +const LEGACY: &[(&str, &str)] = &[ + ("EXTREME_HILLS", "windswept_hills"), + ("MOUNTAINS", "windswept_hills"), + ("GRAVELLY_MOUNTAINS", "windswept_gravelly_hills"), + ("WOODED_MOUNTAINS", "windswept_forest"), + ("SHATTERED_SAVANNA", "windswept_savanna"), + ("GIANT_TREE_TAIGA", "old_growth_pine_taiga"), + ("GIANT_SPRUCE_TAIGA", "old_growth_spruce_taiga"), + ("TALL_BIRCH_FOREST", "old_growth_birch_forest"), + ("SNOWY_TUNDRA", "snowy_plains"), + ("JUNGLE_EDGE", "sparse_jungle"), + ("WOODED_BADLANDS_PLATEAU", "wooded_badlands"), + ("STONE_SHORE", "stony_shore"), + ("NETHER", "nether_wastes"), +]; + +/// The constants every pack sees: `CAT_*` and `PPT_*`, plus `BIOME_*` for `biomes` (the +/// registry's identifiers, in id order). +pub fn constants(biomes: &[&str]) -> HashMap { + let mut out: HashMap = CATEGORIES + .iter() + .enumerate() + .map(|(i, c)| (format!("CAT_{c}"), i as f64)) + .collect(); + for (i, p) in ["NONE", "RAIN", "SNOW"].iter().enumerate() { + out.insert(format!("PPT_{p}"), i as f64); + } + let index = |name: &str| { + biomes + .iter() + .position(|b| b.strip_prefix("minecraft:") == Some(name)) + }; + for (i, b) in biomes.iter().enumerate() { + if let Some(name) = b.strip_prefix("minecraft:") { + out.insert(format!("BIOME_{}", name.to_ascii_uppercase()), i as f64); + } + } + for (old, new) in LEGACY { + if let Some(i) = index(new) { + out.entry(format!("BIOME_{old}")).or_insert(i as f64); + } + } + out +} + +/// `biome_category` of the biome `minecraft:`. +pub fn category(name: &str) -> i32 { + let name = name.strip_prefix("minecraft:").unwrap_or(name); + let has = |words: &[&str]| words.iter().any(|w| name.contains(w)); + let cat = if name == "the_void" { + "NONE" + } else if name == "the_end" || name.starts_with("end_") || name == "small_end_islands" { + "THE_END" + } else if has(&["nether", "crimson", "warped", "basalt", "soul_sand"]) { + "NETHER" + } else if has(&["ocean"]) { + "OCEAN" + } else if has(&["river"]) { + "RIVER" + } else if has(&["beach", "shore"]) { + "BEACH" + } else if has(&["swamp"]) { + "SWAMP" + } else if has(&["mushroom"]) { + "MUSHROOM" + } else if has(&["desert"]) { + "DESERT" + } else if has(&["badlands"]) { + "MESA" + } else if has(&["savanna"]) { + "SAVANNA" + } else if has(&["jungle"]) { + "JUNGLE" + } else if has(&["taiga"]) { + "TAIGA" + } else if has(&["windswept", "peaks", "slopes", "meadow", "grove"]) && name != "cherry_grove" { + "EXTREME_HILLS" + } else if has(&["frozen", "snowy", "ice"]) { + "ICY" + } else if has(&["forest", "garden", "cherry"]) { + "FOREST" + } else if has(&["plains"]) { + "PLAINS" + } else { + "NONE" + }; + CATEGORIES.iter().position(|c| *c == cat).unwrap_or(0) as i32 +} + +/// `biome_precipitation`: none where the biome has none, else rain from temperature 0.15 up, +/// snow below it (spec 3.4). +pub fn precipitation(has_precipitation: bool, temperature: f32) -> i32 { + match (has_precipitation, temperature >= 0.15) { + (false, _) => 0, + (true, true) => 1, + (true, false) => 2, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn constants_categories_and_precipitation() { + let c = constants(&[ + "minecraft:badlands", + "minecraft:pale_garden", + "minecraft:windswept_hills", + ]); + assert_eq!(c["BIOME_PALE_GARDEN"], 1.0); + assert_eq!(c["BIOME_EXTREME_HILLS"], 2.0); + assert_eq!(c["CAT_NETHER"], 16.0); + assert_eq!(c["PPT_SNOW"], 2.0); + assert!(!c.contains_key("BIOME_SULFUR_CAVES")); + assert_eq!(category("minecraft:pale_garden"), 10); + assert_eq!(category("minecraft:deep_frozen_ocean"), 11); + assert_eq!(category("minecraft:snowy_taiga"), 1); + assert_eq!(category("minecraft:crimson_forest"), 16); + assert_eq!(category("minecraft:cherry_grove"), 10); + assert_eq!(precipitation(true, 0.0), 2); + assert_eq!(precipitation(true, 0.8), 1); + assert_eq!(precipitation(false, 2.0), 0); + } +} diff --git a/crates/horizon-shaders/src/custom.rs b/crates/horizon-shaders/src/custom.rs index 34510247e..e27472816 100644 --- a/crates/horizon-shaders/src/custom.rs +++ b/crates/horizon-shaders/src/custom.rs @@ -87,6 +87,8 @@ pub struct Custom { smooth: HashMap, seed: u64, unknown: HashSet, + /// `BIOME_*`, `CAT_*` and `PPT_*` (crate::biomes). + constants: HashMap, /// Expressions that did not parse, and unknown names, as `key: message`. pub notes: Vec, calls: usize, @@ -97,6 +99,7 @@ impl Custom { pub fn parse(properties: &Properties) -> Self { let mut out = Custom { seed: 0x9e37_79b9_7f4a_7c15, + constants: crate::biomes::constants(&[]), ..Default::default() }; for e in &properties.entries { @@ -126,6 +129,11 @@ impl Custom { out } + /// Names the game's biomes (registry identifiers, in id order) for `BIOME_*`. + pub fn set_biomes(&mut self, biomes: &[&str]) { + self.constants = crate::biomes::constants(biomes); + } + /// The uniforms' names and declared types, in file order. pub fn uniforms(&self) -> impl Iterator { self.items @@ -189,8 +197,19 @@ impl Custom { "pi" => Some(Val::scalar(std::f64::consts::PI)), "true" => Some(Val::scalar(1.0)), "false" => Some(Val::scalar(0.0)), - n => vars.get(n).copied().or_else(|| builtin(n)), + n => vars + .get(n) + .copied() + .or_else(|| builtin(n)) + .or_else(|| self.constants.get(n).map(|&x| Val::scalar(x))), }; + if v.is_none() && name.starts_with("BIOME_") { + if self.unknown.insert(name.clone()) { + self.notes + .push(format!("`{name}` names no biome; it reads -1")); + } + return Val::scalar(-1.0); + } let Some(v) = v else { if self.unknown.insert(name.clone()) { self.notes.push(format!("`{name}` is unknown; it reads 0")); @@ -523,16 +542,18 @@ mod tests { uniform.vec3.e = vec3(a, cameraPosition.y, 2) * 2\n\ uniform.float.f = if(a < 0, 1, a < 7, 2, 3) + clamp(fmod(-1, 4), 0, 2.5) + min(4, 2, 3)\n\ uniform.float.g = gbufferModelView.1.0 + frac(sunAngle - 0.25) + pi * 0\n\ - uniform.float.h = unknownThing + in(biome, 3, BIOME_DESERT)\n", + uniform.float.h = unknownThing + in(biome, 3, BIOME_DESERT)\n\ + uniform.int.i = biome == BIOME_PALE_GARDEN || biome == BIOME_OLD\n\ + uniform.int.j = CAT_OCEAN + PPT_RAIN\n", ); + c.set_biomes(&["minecraft:beach", "minecraft:desert"]); assert!(c.notes.is_empty(), "{:?}", c.notes); let names: Vec<_> = c.uniforms().map(|u| u.0).collect(); - assert_eq!(names, ["b", "c", "d", "e", "f", "g", "h"]); + assert_eq!(names, ["b", "c", "d", "e", "f", "g", "h", "i", "j"]); let builtin = |n: &str| match n { "cameraPosition" => Some(Val::vector(&[1.0, 64.0, 2.0])), "sunAngle" => Some(Val::scalar(0.125)), - "biome" => Some(Val::scalar(5.0)), - "BIOME_DESERT" => Some(Val::scalar(5.0)), + "biome" => Some(Val::scalar(1.0)), "gbufferModelView" => { let mut m = Val::scalar(0.0); m.v[1] = 9.0; // column 0, row 1 @@ -543,9 +564,29 @@ mod tests { }; let v = c.eval(0.016, &builtin); let x: Vec = v.iter().map(|v| v.v[0]).collect(); - assert_eq!(x, [3.0, 1.0, -3.0, 12.0, 2.0 + 2.5 + 2.0, 9.0 + 0.875, 1.0]); + assert_eq!( + x, + [ + 3.0, + 1.0, + -3.0, + 12.0, + 2.0 + 2.5 + 2.0, + 9.0 + 0.875, + 1.0, + 0.0, + 12.0 + ] + ); assert_eq!(&v[3].v[..3], &[12.0, 128.0, 4.0]); - assert_eq!(c.notes, ["`unknownThing` is unknown; it reads 0"]); + assert_eq!( + c.notes, + [ + "`unknownThing` is unknown; it reads 0", + "`BIOME_PALE_GARDEN` names no biome; it reads -1", + "`BIOME_OLD` names no biome; it reads -1" + ] + ); } #[test] diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index fbad9bee0..8684be867 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -12,6 +12,7 @@ //! - [`load_post`]: a dimension's `prepare`, `deferred`, `composite` and `final` programs, in order (2.1). //! - [`Macros`] and [`eval_condition`]: the macros packs test, and the `#if` arithmetic (1.5). +mod biomes; mod block_ids; mod condition; mod config; @@ -26,6 +27,7 @@ mod preprocess; mod properties; mod translate; +pub use biomes::{category as biome_category, precipitation as biome_precipitation}; pub use block_ids::BlockIds; pub use condition::eval_condition; pub use config::{BUFFERS, Buffer, Config, Format, Kind, buffer_index, draw_buffers};