diff --git a/crates/horizon-assets/src/blockstate/choice.rs b/crates/horizon-assets/src/blockstate/choice.rs index 93e9a4118..d871b1c66 100644 --- a/crates/horizon-assets/src/blockstate/choice.rs +++ b/crates/horizon-assets/src/blockstate/choice.rs @@ -82,6 +82,19 @@ impl CompiledChoices { .collect::>()?, }) } + /// The variant `particleMaterial` comes from: the first entry of a weighted + /// list (`WeightedVariants`) and, for a multipart root, of its first + /// selector whether or not that selector applies (`MultiPartModel`). + pub fn particle_variant(&self, selected: &SelectedModels) -> Option<&Variant> { + let choice = match selected { + SelectedModels::Variant(index) => self.variants.get(*index)?, + SelectedModels::Multipart(_) => self.multipart.first()?, + }; + Some(match choice { + Choice::Single(value) => value, + Choice::Weighted { variants, .. } => variants.first()?, + }) + } /// Append selected variants without allocating intermediate part lists. A /// multipart root consumes nextLong even for an empty selected subset, then /// resets the same random source to that seed before each selected part. diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 870891b71..b0939bf7b 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1809,14 +1809,10 @@ impl Game<'_> { } Some(stack) } - fn particle_sprite(&self, state: BlockStateId) -> Option<(String, u32)> { - Self::block_particle_sprite(&self.pack, self.res.facts(), state) - } - fn block_particle_sprite( - pack: &PackResources, - facts: &BlockFacts, - state: BlockStateId, - ) -> Option<(String, u32)> { + /// The block's first up face (or any face) and its fixed tint, for block + /// models drawn as entities (falling blocks, carried blocks). + fn block_face_sprite(&self, state: BlockStateId) -> Option<(String, u32)> { + let pack = &self.pack; let models = pack .render .model_catalog @@ -1826,14 +1822,10 @@ impl Game<'_> { .iter() .flat_map(|(_, m)| m.faces.iter()) .find(|f| f.quad.facing == horizon_assets::model::Direction::Up) - .or_else(|| models.iter().flat_map(|(_, m)| m.faces.iter()).next()); - // Entity-rendered blocks may have a particle texture without any faces. - let layer = face - .map(|face| face.layer) - .or_else(|| models.iter().find_map(|(_, model)| model.particle))?; - let name = pack.layer_names.get(&layer)?.clone(); - let tint = if face.is_some_and(|face| face.quad.tint_index >= 0) { - let n = &facts.get(state).name; + .or_else(|| models.iter().flat_map(|(_, m)| m.faces.iter()).next())?; + let name = pack.layer_names.get(&face.layer)?.clone(); + let tint = if face.quad.tint_index >= 0 { + let n = &self.res.facts().get(state).name; if n.contains("leaves") || n.contains("vine") { 0x48B518 } else { @@ -1844,6 +1836,51 @@ impl Game<'_> { }; Some((name, tint)) } + /// `TerrainParticle` at `pos`: the model's particle sprite and tint source + /// 0's `colorAsTerrainParticle` (white when the block has none). + fn particle_sprite(&self, state: BlockStateId, pos: [i32; 3]) -> Option<(String, u32)> { + let sprite = Self::block_particle_sprite(&self.pack, state)?; + let chunks = &self.world.chunks; + let (cx, cz) = (pos[0] >> 4, pos[2] >> 4); + let region: [[Option<&horizon_client::chunk::Chunk>; 3]; 3] = std::array::from_fn(|dz| { + std::array::from_fn(|dx| { + chunks + .get(&(cx + dx as i32 - 1, cz + dz as i32 - 1)) + .map(|c| &**c) + }) + }); + let tint = self + .pack + .context + .tables + .terrain_particle_tint( + state, + pos, + ®ion, + self.res.biomes(), + self.streaming.biome_zoom, + self.streaming.biome_blend, + ) + .unwrap_or(0xFFFFFF); + Some((sprite, tint)) + } + /// `BlockStateModelSet.getParticleMaterial`: the sprite of the model's + /// `particle` texture, else its first face (entity-rendered blocks may + /// have a particle texture without faces, and faces without one). + fn block_particle_sprite(pack: &PackResources, state: BlockStateId) -> Option { + let catalog = &pack.render.model_catalog; + let layer = catalog.particle(state).or_else(|| { + let models = catalog + .collect(state, horizon_core::position::BlockPos::new(0, 0, 0)) + .ok()?; + models + .iter() + .flat_map(|(_, m)| m.faces.iter()) + .next() + .map(|f| f.layer) + })?; + pack.layer_names.get(&layer).cloned() + } fn particle_shape(&self, pos: [i32; 3], state: BlockStateId) -> Vec<[f64; 6]> { let f = self.res.facts().get(state); let offset = if f.outline_offset { @@ -1862,7 +1899,7 @@ impl Game<'_> { } /// ClientLevel.addDestroyBlockEffect: a burst per outline box. fn destroy_particles(&mut self, pos: [i32; 3], state: BlockStateId) { - let Some((sprite, tint)) = self.particle_sprite(state) else { + let Some((sprite, tint)) = self.particle_sprite(state, pos) else { return; }; let boxes = self.particle_shape(pos, state); @@ -1873,7 +1910,7 @@ impl Game<'_> { } /// ClientLevel.addBreakingParticles: one crumb on the face being mined. fn crack_particle(&mut self, pos: [i32; 3], face: [i32; 3], state: BlockStateId) { - let Some((sprite, tint)) = self.particle_sprite(state) else { + let Some((sprite, tint)) = self.particle_sprite(state, pos) else { return; }; let boxes = self.particle_shape(pos, state); @@ -6513,7 +6550,7 @@ impl Game<'_> { } // Falling blocks use their baked model, including non-cubic shapes. for f in &self.falling { - let Some((_, tint)) = self.particle_sprite(f.state) else { + let Some((_, tint)) = self.block_face_sprite(f.state) else { continue; }; let p = glam::DVec3::new( diff --git a/crates/horizon-client/src/game/breezes.rs b/crates/horizon-client/src/game/breezes.rs index 466469110..1c441aef4 100644 --- a/crates/horizon-client/src/game/breezes.rs +++ b/crates/horizon-client/src/game/breezes.rs @@ -39,11 +39,13 @@ impl Game<'_> { m.position.y.floor() as i32, m.position.z.floor() as i32, ); + let mut at = [x, y, z]; let mut state = self.world.block(x, y, z); if self.res.facts().is_air(state) { - state = self.world.block(x, (m.position.y - 0.2).floor() as i32, z); + at[1] = (m.position.y - 0.2).floor() as i32; + state = self.world.block(x, at[1], z); } - let Some((sprite, tint)) = self.particle_sprite(state) else { + let Some((sprite, tint)) = self.particle_sprite(state, at) else { continue; }; let at = if jump_trail { diff --git a/crates/horizon-client/src/game/commands/world_cmds.rs b/crates/horizon-client/src/game/commands/world_cmds.rs index ef2adf80c..7e02626d2 100644 --- a/crates/horizon-client/src/game/commands/world_cmds.rs +++ b/crates/horizon-client/src/game/commands/world_cmds.rs @@ -470,8 +470,12 @@ impl Game<'_> { match block { Ok(Arg::Block(b)) => { let state = self.block_state(&b)?; + let at = self + .position_arg(ctx, "pos") + .unwrap_or_else(|| self.source_position()) + .map(|c| c.floor() as i32); let (sprite, tint) = self - .particle_sprite(state) + .particle_sprite(state, at) .unwrap_or_else(|| ("block/stone".into(), 0xFFFFFF)); ParticleOptions::block(ty, sprite, tint) } diff --git a/crates/horizon-client/src/game/creaking.rs b/crates/horizon-client/src/game/creaking.rs index e7a1386f9..c75325c2f 100644 --- a/crates/horizon-client/src/game/creaking.rs +++ b/crates/horizon-client/src/game/creaking.rs @@ -191,7 +191,8 @@ impl Game<'_> { let Ok(id) = registry.resolve(&state) else { return; }; - let Some((sprite, tint)) = this.particle_sprite(id) else { + let Some((sprite, tint)) = this.particle_sprite(id, center.map(|c| c.floor() as i32)) + else { return; }; for _ in 0..count { diff --git a/crates/horizon-client/src/game/mace.rs b/crates/horizon-client/src/game/mace.rs index 46091a773..8ad5e76c3 100644 --- a/crates/horizon-client/src/game/mace.rs +++ b/crates/horizon-client/src/game/mace.rs @@ -267,7 +267,7 @@ impl Game<'_> { pos: BlockPos, fall_distance: f64, ) { - let Some((sprite, tint)) = self.particle_sprite(state) else { + let Some((sprite, tint)) = self.particle_sprite(state, [pos.x, pos.y, pos.z]) else { return; }; let count = (50.0 * fall_distance).clamp(0.0, 200.0) as u32; @@ -288,7 +288,7 @@ impl Game<'_> { /// pillars of the block at `pos`, a cluster over it and a ring 3.5 out. pub(super) fn smash_dust(&mut self, pos: BlockPos, count: i32) { let state = self.world.block(pos.x, pos.y, pos.z); - let Some((sprite, tint)) = self.particle_sprite(state) else { + let Some((sprite, tint)) = self.particle_sprite(state, [pos.x, pos.y, pos.z]) else { return; }; let options = ParticleOptions::block(ParticleType::DustPillar, sprite, tint); diff --git a/crates/horizon-client/src/game/mob_blocks.rs b/crates/horizon-client/src/game/mob_blocks.rs index 27a60ca19..a9e854bfb 100644 --- a/crates/horizon-client/src/game/mob_blocks.rs +++ b/crates/horizon-client/src/game/mob_blocks.rs @@ -220,7 +220,7 @@ impl Game<'_> { self.held_item_geometry(mob, camera, alpha, light, true, out); if mob.species() == Species::Enderman && let Some(state) = mob.extra.carried - && let Some((sprite, tint)) = self.particle_sprite(state) + && let Some((sprite, tint)) = self.block_face_sprite(state) { // CarriedBlockLayer: OverlayTexture.NO_OVERLAY, never the hurt tint. let base = render::base_transform(mob, alpha as f32, camera); diff --git a/crates/horizon-client/src/game/render_tick.rs b/crates/horizon-client/src/game/render_tick.rs index a938e80f3..4ad9db548 100644 --- a/crates/horizon-client/src/game/render_tick.rs +++ b/crates/horizon-client/src/game/render_tick.rs @@ -184,7 +184,8 @@ impl Game<'_> { && f.solid_render && f.full_collision { - block_overlay = self.particle_sprite(state).map(|(name, _)| name); + // ScreenEffectRenderer: the block's particle material. + block_overlay = Self::block_particle_sprite(&self.pack, state); break; } } @@ -288,11 +289,12 @@ impl Game<'_> { }; let marker = marker_particle_target(self.mode, self.stack_in(self.selected)); let marker_options = |state| { - let (sprite, tint) = Self::block_particle_sprite(&self.pack, self.res.facts(), state)?; + // BlockMarker: the particle sprite, untinted. + let sprite = Self::block_particle_sprite(&self.pack, state)?; Some(ParticleOptions::block( ParticleType::BlockMarker, sprite, - tint, + 0xFFFFFF, )) }; let world = &self.world; diff --git a/crates/horizon-client/src/game/sculk.rs b/crates/horizon-client/src/game/sculk.rs index a5a24725b..096562608 100644 --- a/crates/horizon-client/src/game/sculk.rs +++ b/crates/horizon-client/src/game/sculk.rs @@ -1433,7 +1433,7 @@ impl Game<'_> { at[2].floor() as i32, ); let state = self.world.block(below.0, below.1, below.2); - let Some((sprite, tint)) = self.particle_sprite(state) else { + let Some((sprite, tint)) = self.particle_sprite(state, [below.0, below.1, below.2]) else { return; }; for _ in 0..30 { diff --git a/crates/horizon-client/src/game/sniffers.rs b/crates/horizon-client/src/game/sniffers.rs index a30c0ba5c..8e851d028 100644 --- a/crates/horizon-client/src/game/sniffers.rs +++ b/crates/horizon-client/src/game/sniffers.rs @@ -79,7 +79,8 @@ impl Game<'_> { } let head = sniffer::head_block(m); let below = self.world.block(head[0], head[1] - 1, head[2]); - let Some((sprite, tint)) = self.particle_sprite(below) else { + let Some((sprite, tint)) = self.particle_sprite(below, [head[0], head[1] - 1, head[2]]) + else { continue; }; let dust = [ diff --git a/crates/horizon-client/src/mesher.rs b/crates/horizon-client/src/mesher.rs index e6cd27f3e..a9e9f6823 100644 --- a/crates/horizon-client/src/mesher.rs +++ b/crates/horizon-client/src/mesher.rs @@ -232,11 +232,15 @@ fn visibility(pad: &Padded, info: &[Info]) -> u64 { #[cfg(test)] mod leaf_culling_tests; #[cfg(test)] +mod particle_tests; +#[cfg(test)] mod visibility_tests; #[derive(Clone, Copy, PartialEq, Eq)] enum Tint { None, Grass, + /// `BlockTintSources.grassBlock`: grass in the world, none on its particles. + GrassBlock, TallGrassUpper, Foliage, DryFoliage, @@ -353,9 +357,10 @@ impl MeshTables { ([Tint::Grass; 2], 1) } } - "fern" | "short_grass" | "potted_fern" | "bush" | "grass_block" | "sugar_cane" => { + "fern" | "short_grass" | "potted_fern" | "bush" | "sugar_cane" => { ([Tint::Grass; 2], 1) } + "grass_block" => ([Tint::GrassBlock; 2], 1), "pink_petals" | "wildflowers" => ([Tint::None, Tint::Grass], 2), "spruce_leaves" => ([Tint::Constant(0x61_9961); 2], 1), "birch_leaves" => ([Tint::Constant(0x80_A755); 2], 1), @@ -467,6 +472,28 @@ impl MeshTables { tint_sources: i.tint_count, }) } + /// `TerrainParticle`'s colour: tint source 0's `colorAsTerrainParticle`, + /// which is the in-world (biome-blended) colour except on grass blocks, + /// whose dirt crumbs stay untinted. `None` when the state has no source. + /// `region[dz][dx]` are the chunks around `pos`'s chunk (centre 1,1). + pub fn terrain_particle_tint( + &self, + state: BlockStateId, + pos: [i32; 3], + region: &[[Option<&Chunk>; 3]; 3], + biomes: &Biomes, + zoom: BiomeZoom, + blend_radius: i32, + ) -> Option { + let info = self.info.get(state.0 as usize)?; + if info.tints[0] == Tint::GrassBlock { + return None; + } + let [x, y, z] = pos; + let origin = [x >> 4, z >> 4, y & !15]; + let mut tints = Tints::new(region, biomes, zoom, blend_radius, origin); + Mesher::tint(info, 0, &mut tints, [x & 15, y & 15, z & 15]) + } /// `BlockState.getOffset` for blocks with a random model offset. pub fn offset(&self, state: BlockStateId, x: i32, z: i32) -> Option<[f64; 3]> { offset(self.info.get(state.0 as usize)?, x, z) @@ -791,7 +818,7 @@ impl Mesher<'_> { } Some(match state.tints[(index as usize).min(1)] { Tint::None => return None, - Tint::Grass => tints.get(Resolver::Grass, x, y, z), + Tint::Grass | Tint::GrassBlock => tints.get(Resolver::Grass, x, y, z), Tint::TallGrassUpper => tints.get(Resolver::Grass, x, y, z), Tint::Foliage => tints.get(Resolver::Foliage, x, y, z), Tint::DryFoliage => tints.get(Resolver::Dry, x, y, z), diff --git a/crates/horizon-client/src/mesher/particle_tests.rs b/crates/horizon-client/src/mesher/particle_tests.rs new file mode 100644 index 000000000..ef0684f1d --- /dev/null +++ b/crates/horizon-client/src/mesher/particle_tests.rs @@ -0,0 +1,56 @@ +use super::*; + +/// `TerrainParticle`: the model's particle sprite and tint source 0's +/// `colorAsTerrainParticle` (#1034: grass blocks crumbled as tinted grass tops). +#[test] +fn terrain_particles_use_the_particle_sprite_and_vanilla_tint_sources() { + let root = + std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3"); + if !root.join("horizon-resources.json").exists() { + return; + } + let assets = Assets::load(&root).unwrap(); + let render = assets.reload(&root).unwrap(); + let facts = BlockFacts::load(&root, assets.registry).unwrap(); + let visibility = crate::models::load_visibility(&root, assets.registry).unwrap(); + let tables = MeshTables::new(&assets, &render, &facts, visibility).unwrap(); + let biomes = Biomes::load(&root).unwrap(); + let state = |name| assets.registry.default_state(name).unwrap(); + let sprite = |name| { + let layer = render.model_catalog.particle(state(name)).unwrap(); + let (sprite, _) = render + .sprite_layers + .iter() + .find(|(_, l)| **l == layer) + .unwrap(); + sprite.clone() + }; + assert_eq!(sprite("minecraft:grass_block"), "minecraft:block/dirt"); + assert_eq!( + sprite("minecraft:short_grass"), + "minecraft:block/short_grass" + ); + assert_eq!(sprite("minecraft:oak_leaves"), "minecraft:block/oak_leaves"); + + let center = Chunk::empty(0, 0); + let mut region = [[None; 3]; 3]; + region[1][1] = Some(¢er); + let tint = |name| { + tables.terrain_particle_tint( + state(name), + [5, 70, 9], + ®ion, + &biomes, + BiomeZoom::new(0), + 2, + ) + }; + // grassBlock() overrides colorAsTerrainParticle to -1: the dirt stays untinted. + assert_eq!(tint("minecraft:grass_block"), None); + assert!(tint("minecraft:short_grass").is_some()); + assert!(tint("minecraft:oak_leaves").is_some()); + assert_eq!(tint("minecraft:spruce_leaves"), Some(0x61_9961)); + // Pink petals' layer 0 is BLANK_LAYER; stone has no tint source. + assert_eq!(tint("minecraft:pink_petals"), None); + assert_eq!(tint("minecraft:stone"), None); +} diff --git a/crates/horizon-client/src/models/catalog.rs b/crates/horizon-client/src/models/catalog.rs index be550d8c6..73207ee7f 100644 --- a/crates/horizon-client/src/models/catalog.rs +++ b/crates/horizon-client/src/models/catalog.rs @@ -257,6 +257,13 @@ impl StateModels { // Blocks use their position seed; contained/displayed blocks can supply one. self.collect_seeded(state, position_seed(position)) } + /// `BlockStateModel.particleMaterial`: the particle sprite layer of the + /// state's first variant (see `CompiledChoices::particle_variant`). + pub fn particle(&self, state: BlockStateId) -> Option { + let (choice, selected) = self.states.get(&state)?; + let variant = self.choices[*choice].particle_variant(selected)?; + self.baked.get(&key(variant))?.particle + } pub fn collect_seeded( &self, state: BlockStateId,