diff --git a/crates/horizon-client/examples/probe/marker_particles_probe.rs b/crates/horizon-client/examples/probe/marker_particles_probe.rs index 190751d79..6b05af356 100644 --- a/crates/horizon-client/examples/probe/marker_particles_probe.rs +++ b/crates/horizon-client/examples/probe/marker_particles_probe.rs @@ -49,7 +49,7 @@ pub fn main() -> Result<()> { } } } - let mut engine = ParticleEngine::new(root, &HashMap::new()); + let mut engine = ParticleEngine::new(root, &HashMap::new(), Default::default()); let mut out = BufWriter::new(File::create(&args[2])?); for line in BufReader::new(File::open(&args[1])?).lines() { let case: Value = serde_json::from_str(&line?)?; diff --git a/crates/horizon-client/examples/probe/particle_providers_probe.rs b/crates/horizon-client/examples/probe/particle_providers_probe.rs index c8cd9d88d..067648e95 100644 --- a/crates/horizon-client/examples/probe/particle_providers_probe.rs +++ b/crates/horizon-client/examples/probe/particle_providers_probe.rs @@ -152,7 +152,7 @@ pub fn main() -> Result<()> { world(&assets, &facts, false, true)?, world(&assets, &facts, true, true)?, ]; - let mut engine = ParticleEngine::new(root, &HashMap::new()); + let mut engine = ParticleEngine::new(root, &HashMap::new(), Default::default()); for (ty, item) in [ (ParticleType::ItemSlime, "item_slime_ball"), (ParticleType::ItemSnowball, "item_snowball"), diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 653a1d038..cfb3ad433 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -11690,7 +11690,11 @@ impl<'a> Game<'a> { let inventory = vec![None; 36]; let now = Instant::now(); let look = options.look; - let particles = horizon_client::particles::ParticleEngine::new(&root, &gui.atlas.sprites); + let particles = horizon_client::particles::ParticleEngine::new( + &root, + &gui.atlas.sprites, + gui.atlas.translucent.clone(), + ); Ok(Game { res: res.clone(), rest_loader: None, diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index a848d36a5..6af750b62 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -35,7 +35,7 @@ fn entity_shader() -> String { mod entity_batch; mod weather; use entity_batch::EntityBatch; -pub use entity_batch::{SHADOW_OVERLAY, WATER_MASK_OVERLAY}; +pub use entity_batch::{SHADOW_OVERLAY, TRANSLUCENT_PARTICLE_OVERLAY, WATER_MASK_OVERLAY}; /// Window.getAppropriateLineWidth, in framebuffer pixels. pub fn outline_width(width: u32) -> f32 { @@ -3362,6 +3362,7 @@ impl Renderer { translucent_crumbling_quads, water_mask_quads, shadow_quads, + translucent_particle_quads, ] = self.entity_batch.update( frame.entities, frame.additive, @@ -3373,7 +3374,8 @@ impl Renderer { + crumbling_quads + translucent_crumbling_quads + water_mask_quads - + shadow_quads; + + shadow_quads + + translucent_particle_quads; let cloud_quads = if let Some(input) = &frame.clouds { let (rebuilt, uniform) = crate::spans::time("clouds", || self.cloud_mesh.prepare(input)); @@ -3827,6 +3829,22 @@ impl Renderer { ); } } + // LevelRenderer.executeTranslucentAfterTerrain: vanilla's + // translucent particle layer draws after translucent terrain — + // the quads write depth, so before it they would hide the glass + // or water behind them. + if translucent_particle_quads > 0 { + pass.set_pipeline(&self.entity_pipeline); + pass.set_bind_group(0, &self.entity_group, &[]); + pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); + pass.set_vertex_buffer(0, self.entity_buffer.slice(..)); + pass.draw_indexed( + 0..translucent_particle_quads * 6, + ((world_quads - translucent_particle_quads) * 4) as i32, + 0..1, + ); + pass.set_bind_group(0, &self.group, &[]); + } // LevelRenderer.executeClassicTransparency: translucent targets' // outlines follow terrain, so its blending cannot wash them out. if outline_edges > 0 && frame.outline_after_terrain { @@ -4316,14 +4334,23 @@ mod tests { normal: [0; 4], }; 4] }; - let entities: Vec<_> = [(0, 0), (14, 9), (4, 1), (11, 2), (7, 3), (4, 4), (12, 7)] - .into_iter() - .flat_map(|(layer, id)| quad(layer, id)) - .collect(); + let entities: Vec<_> = [ + (0, 0), + (14, 9), + (4, 1), + (11, 2), + (15, 10), + (7, 3), + (4, 4), + (12, 7), + ] + .into_iter() + .flat_map(|(layer, id)| quad(layer, id)) + .collect(); let mut batch = super::EntityBatch::default(); let counts = batch.update(&entities, &quad(3, 5), &quad(0, 6), &quad(0, 8)); let vertices = &batch.combined; - assert_eq!(counts, [4, 1, 1, 0, 1]); + assert_eq!(counts, [4, 1, 1, 0, 1, 1]); assert_eq!( vertices .as_chunks::<4>() @@ -4331,7 +4358,7 @@ mod tests { .iter() .map(|q| q[0].light) .collect::>(), - [0, 1, 3, 4, 2, 7, 9, 5, 6, 8] + [0, 1, 3, 4, 2, 7, 9, 10, 5, 6, 8] ); } #[test] diff --git a/crates/horizon-client/src/gfx/entity_batch.rs b/crates/horizon-client/src/gfx/entity_batch.rs index 56ffb04b9..8153699fc 100644 --- a/crates/horizon-client/src/gfx/entity_batch.rs +++ b/crates/horizon-client/src/gfx/entity_batch.rs @@ -3,13 +3,16 @@ use super::Vertex; /// Entity materials by a quad's overlay (`Vertex::layer`), in draw order: /// ordinary entities, breaking cracks (11), translucent breaking cracks (12), -/// the boat water mask (13, `RenderPipelines.WATER_MASK`) and entity shadows -/// (14, `RenderPipelines.ENTITY_SHADOW`). -const MATERIALS: usize = 5; +/// the boat water mask (13, `RenderPipelines.WATER_MASK`), entity shadows +/// (14, `RenderPipelines.ENTITY_SHADOW`) and translucent-layer particles (15, +/// drawn after translucent terrain: `SubmitNodeCollection.submitQuadParticleGroup`). +const MATERIALS: usize = 6; /// The overlay of depth-only quads that hide water (`RenderTypes.waterMask`). pub const WATER_MASK_OVERLAY: u32 = 13; /// The overlay of entity shadow quads (`RenderTypes.entityShadow`). pub const SHADOW_OVERLAY: u32 = 14; +/// The overlay of translucent-layer particles (`SingleQuadParticle.Layer.translucent`). +pub const TRANSLUCENT_PARTICLE_OVERLAY: u32 = 15; #[derive(Default)] pub(super) struct EntityBatch { @@ -41,6 +44,7 @@ impl EntityBatch { 12 => 2, WATER_MASK_OVERLAY => 3, SHADOW_OVERLAY => 4, + TRANSLUCENT_PARTICLE_OVERLAY => 5, _ => 0, }; self.buckets[material][counts[material]] = index; @@ -92,13 +96,14 @@ mod tests { ) -> (Vec, [u32; MATERIALS]) { let mut combined = Vec::with_capacity(entities.len() + additive.len() + hand.len()); let mut counts = [0; MATERIALS]; - for (i, layer) in [0, 11, 12, 13, 14].into_iter().enumerate() { + for (i, layer) in [0, 11, 12, 13, 14, 15].into_iter().enumerate() { for quad in entities.as_chunks::<4>().0 { let material = match quad[0].layer { 11 => 11, 12 => 12, 13 => 13, 14 => 14, + 15 => 15, _ => 0, }; if material == layer { @@ -118,7 +123,7 @@ mod tests { pos: [index as f32, 2.0, 3.0], uv: [index as f32, 5.0], layer: if mixed { - [7, 11, 14, 12, 4, 13, 0][index / 4 % 7] + [7, 11, 14, 12, 4, 13, 15, 0][index / 4 % 8] } else { 0 }, diff --git a/crates/horizon-client/src/hud.rs b/crates/horizon-client/src/hud.rs index 89d1b1c3c..5626359c8 100644 --- a/crates/horizon-client/src/hud.rs +++ b/crates/horizon-client/src/hud.rs @@ -3,7 +3,7 @@ //! GUI scale is applied when drawing). use crate::Result; use std::{ - collections::HashMap, + collections::{HashMap, HashSet}, fs, path::{Path, PathBuf}, }; @@ -58,6 +58,9 @@ pub struct HudAtlas { pub width: u32, pub height: u32, pub sprites: HashMap, + /// Sprites with a 0 < alpha < 255 texel (`SpriteContents.transparency().hasTranslucent`), + /// which puts their particles on vanilla's translucent layer. + pub translucent: HashSet, /// Sprites whose `.png.mcmeta` sets `blur` or `clamp`; the others /// sample as vanilla's defaults (nearest, repeat). pub flags: HashMap, @@ -459,7 +462,16 @@ impl HudAtlas { let height = (glyph_region + GLYPH_ROWS).next_multiple_of(1024); let mut pixels = vec![0u8; (width * height * 4) as usize]; let mut sprites = HashMap::new(); + let mut translucent = HashSet::new(); for ((name, w, h, data), (px, py)) in images.iter().zip(placements) { + if data + .as_chunks::<4>() + .0 + .iter() + .any(|p| p[3] != 0 && p[3] != 255) + { + translucent.insert(name.clone()); + } for row in 0..*h { let src = &data[(row * w * 4) as usize..((row + 1) * w * 4) as usize]; let dst = (((py + row) * width + px) * 4) as usize; @@ -524,6 +536,7 @@ impl HudAtlas { width, height, sprites, + translucent, flags, fire_animations, font, diff --git a/crates/horizon-client/src/particles.rs b/crates/horizon-client/src/particles.rs index 0d4f4d623..62127808a 100644 --- a/crates/horizon-client/src/particles.rs +++ b/crates/horizon-client/src/particles.rs @@ -18,7 +18,10 @@ use horizon_core::{ random::{LegacyRandom, RandomSource}, }; use horizon_world::block_collision::BlockCollisionRegistry; -use std::{collections::HashMap, path::Path}; +use std::{ + collections::{HashMap, HashSet}, + path::Path, +}; macro_rules! particle_types { ($($variant:ident = $name:literal),* $(,)?) => { @@ -196,6 +199,33 @@ impl ParticleType { | Self::OminousSpawning ) } + /// `SingleQuadParticle.Layer.translucent`: types whose vanilla particle class + /// hardcodes the translucent layer, drawn after translucent terrain. + /// `TerrainParticle`, `BreakingItemParticle` and `BlockMarker` resolve their + /// layer from the sprite instead (`Layer.bySprite`); see the `translucent` + /// sprite set on the engine. + pub fn translucent_layer(self) -> bool { + match self { + // CampfireSmokeParticle. + Self::CampfireCosySmoke | Self::CampfireSignalSmoke + // PlayerCloudParticle. + | Self::Cloud | Self::Sneeze + // SpellParticle. + | Self::Effect | Self::InstantEffect | Self::EntityEffect + | Self::Witch | Self::Infested | Self::TrialOmen | Self::RaidOmen + // EmissiveRisingParticle. + | Self::Soul | Self::SculkSoul + // SculkChargeParticle, SculkChargePopParticle, ShriekParticle, + // VibrationSignalParticle, FireflyParticle, OverlayParticle. + | Self::SculkCharge | Self::SculkChargePop | Self::Shriek + | Self::Vibration | Self::Firefly | Self::Flash + // SimpleAnimatedParticle and its subclasses: EndRodParticle, + // SparkParticle (firework), SquidInkParticle, TotemParticle. + | Self::EndRod | Self::Firework | Self::SquidInk | Self::GlowSquidInk + | Self::TotemOfUndying => true, + _ => false, + } + } } /// `ParticleOptions` with the parameters of the option classes used here. @@ -1148,6 +1178,9 @@ pub struct ParticleEngine { names: Vec, rects: Vec>, atlas: HashMap, + /// `SingleQuadParticle.Layer.bySprite`: named sprites whose texels include + /// 0 < alpha < 255 render on vanilla's translucent particle layer. + translucent: HashSet, sets: HashMap, set_sprites: Vec>, /// `TrackingEmitter`s: particle type, entity feet position, width, height, ticks left. @@ -1167,7 +1200,7 @@ pub struct ParticleEngine { /// `ParticleGroup.MAX_PARTICLES` (per render group; one group here). const MAX_PARTICLES: usize = 16384; impl ParticleEngine { - pub fn new(root: &Path, atlas: &HashMap) -> Self { + pub fn new(root: &Path, atlas: &HashMap, translucent: HashSet) -> Self { let mut engine = Self { particles: Vec::new(), pending: Vec::new(), @@ -1179,6 +1212,7 @@ impl ParticleEngine { names: Vec::new(), rects: Vec::new(), atlas: atlas.clone(), + translucent, sets: HashMap::new(), set_sprites: Vec::new(), emitters: Vec::new(), @@ -3765,6 +3799,13 @@ impl ParticleEngine { Sprite::Named(name) => self.atlas.get(name).copied(), }; let Some(r) = rect else { continue }; + // `SubmitNodeCollection.submitQuadParticleGroup`: the translucent + // layer draws after translucent terrain, so a crumb of stained + // glass cannot hide the glass behind it (`getLayer`). + let layer = match &p.sprite { + Sprite::Index(_) => p.ty.translucent_layer(), + Sprite::Named(name) => self.translucent.contains(name), + }; let a = f64::from(partial); let pos = DVec3::new( p.previous[0] + (p.position[0] - p.previous[0]) * a, @@ -3867,7 +3908,11 @@ impl ParticleEngine { normal: [0; 4], pos: v.to_array(), uv, - layer: 0, + layer: if layer { + crate::gfx::TRANSLUCENT_PARTICLE_OVERLAY + } else { + 0 + }, color, light, }); @@ -3891,7 +3936,7 @@ mod tests { #[test] fn egg_teleport_unpacks_offsets_and_preserves_seeded_draw_order() { - let mut engine = ParticleEngine::new(Path::new(""), &HashMap::new()); + let mut engine = ParticleEngine::new(Path::new(""), &HashMap::new(), HashSet::new()); engine.seed_randoms(0, 0, 0); // Destination offset (-15, 7, 15); the unused top byte is ignored. engine.block_teleportation([4, 12, -5], 0xff01_0f1fu32 as i32, [16, 8, 16], 0); @@ -3916,7 +3961,7 @@ mod tests { #[test] fn consume_teleport_unpacks_offsets_within_127() { - let mut engine = ParticleEngine::new(Path::new(""), &HashMap::new()); + let mut engine = ParticleEngine::new(Path::new(""), &HashMap::new(), HashSet::new()); engine.seed_randoms(0, 0, 0); // TeleportRandomlyConsumeEffect: BlockUtil.clampedPackDifferenceInPosition // of the move (-7, -3, 9) with radii 127, from (4, 12, -5) to (-3, 9, 4). @@ -3941,7 +3986,8 @@ mod tests { (2, 0, 99_413_408_083_437), ] { for distant in [false, true] { - let mut engine = ParticleEngine::new(Path::new(""), &HashMap::new()); + let mut engine = + ParticleEngine::new(Path::new(""), &HashMap::new(), HashSet::new()); engine.seed_randoms(0, 0, 0); if distant { engine.camera = [1000.0; 3];