diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index fc217bf5c..dbd3d9fd6 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::WATER_MASK_OVERLAY; +pub use entity_batch::{SHADOW_OVERLAY, WATER_MASK_OVERLAY}; /// Window.getAppropriateLineWidth, in framebuffer pixels. pub fn outline_width(width: u32) -> f32 { @@ -375,6 +375,7 @@ pub struct Renderer { entity_pipeline: wgpu::RenderPipeline, weather: weather::Weather, crumbling_pipeline: wgpu::RenderPipeline, + shadow_blob_pipeline: wgpu::RenderPipeline, /// `RenderPipelines.WATER_MASK`: depth writes only, no colour. water_mask_pipeline: wgpu::RenderPipeline, cloud_pipeline: wgpu::RenderPipeline, @@ -1815,6 +1816,26 @@ impl Renderer { CRUMBLING_BLEND, false, ); + // RenderPipelines.ENTITY_SHADOW: translucent, depth test without + // writes. The quads lie on the block tops like vanilla's, but the + // terrain under them snaps to the subpixel grid at other vertices, so + // at a slant their depths differ by up to 1/256 of a pixel's depth + // slope and whole shadows failed the test. The bias covers that and + // the rounding, and stays below the 0.001 a model's feet sit higher + // (1/27 of a pixel's slope at the 16-block shadow range in 720p). + let shadow_blob_pipeline = entity_pipeline( + "Entity shadows", + None, + false, + wgpu::CompareFunction::GreaterEqual, + wgpu::DepthBiasState { + constant: 16, + slope_scale: 0.015, + clamp: 0.0, + }, + alpha_blend, + false, + ); let cloud_pipeline = entity_pipeline( "Clouds", Some(wgpu::Face::Back), @@ -2332,6 +2353,7 @@ impl Renderer { entity_pipeline, weather, crumbling_pipeline, + shadow_blob_pipeline, water_mask_pipeline, cloud_pipeline, cloud_mesh: Default::default(), @@ -3510,6 +3532,7 @@ impl Renderer { crumbling_quads, translucent_crumbling_quads, water_mask_quads, + shadow_quads, ] = self.entity_batch.update( frame.entities, frame.additive, @@ -3517,8 +3540,11 @@ impl Renderer { frame.see_through, ); let weather_quads = frame.weather.len() as u32 / 4; - let world_quads = - entity_quads + crumbling_quads + translucent_crumbling_quads + water_mask_quads; + let world_quads = entity_quads + + crumbling_quads + + translucent_crumbling_quads + + water_mask_quads + + shadow_quads; let cloud_quads = if let Some(input) = &frame.clouds { let (rebuilt, uniform) = crate::spans::time("clouds", || self.cloud_mesh.prepare(input)); @@ -3972,6 +3998,17 @@ impl Renderer { pass.draw_indexed(0..crumbling_quads * 6, (entity_quads * 4) as i32, 0..1); pass.set_bind_group(0, &self.group, &[]); } + if shadow_quads > 0 { + pass.set_pipeline(&self.shadow_blob_pipeline); + pass.set_bind_group(0, &self.entity_group, &[]); + pass.set_vertex_buffer(0, self.entity_buffer.slice(..)); + pass.draw_indexed( + 0..shadow_quads * 6, + ((world_quads - shadow_quads) * 4) as i32, + 0..1, + ); + pass.set_bind_group(0, &self.group, &[]); + } // BoatRenderer's water patch (RenderTypes.waterMask): depth only, // before translucent terrain, so no water shows inside the boat. if water_mask_quads > 0 { @@ -4625,14 +4662,14 @@ mod tests { normal: [0; 4], }; 4] }; - let entities: Vec<_> = [(0, 0), (4, 1), (11, 2), (7, 3), (4, 4), (12, 7)] + 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 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]); + assert_eq!(counts, [4, 1, 1, 0, 1]); assert_eq!( vertices .as_chunks::<4>() @@ -4640,7 +4677,7 @@ mod tests { .iter() .map(|q| q[0].light) .collect::>(), - [0, 1, 3, 4, 2, 7, 5, 6, 8] + [0, 1, 3, 4, 2, 7, 9, 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 999113761..56ffb04b9 100644 --- a/crates/horizon-client/src/gfx/entity_batch.rs +++ b/crates/horizon-client/src/gfx/entity_batch.rs @@ -2,11 +2,14 @@ use super::Vertex; /// Entity materials by a quad's overlay (`Vertex::layer`), in draw order: -/// ordinary entities, breaking cracks (11), translucent breaking cracks (12) -/// and the boat water mask (13, `RenderPipelines.WATER_MASK`). -const MATERIALS: usize = 4; +/// 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 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; #[derive(Default)] pub(super) struct EntityBatch { @@ -37,6 +40,7 @@ impl EntityBatch { 11 => 1, 12 => 2, WATER_MASK_OVERLAY => 3, + SHADOW_OVERLAY => 4, _ => 0, }; self.buckets[material][counts[material]] = index; @@ -88,12 +92,13 @@ 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].into_iter().enumerate() { + for (i, layer) in [0, 11, 12, 13, 14].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, _ => 0, }; if material == layer { @@ -113,7 +118,7 @@ mod tests { pos: [index as f32, 2.0, 3.0], uv: [index as f32, 5.0], layer: if mixed { - [7, 11, 12, 4, 13, 0][index / 4 % 6] + [7, 11, 14, 12, 4, 13, 0][index / 4 % 7] } else { 0 }, diff --git a/crates/horizon-client/src/shadows.rs b/crates/horizon-client/src/shadows.rs index 66e4f272b..dece7f726 100644 --- a/crates/horizon-client/src/shadows.rs +++ b/crates/horizon-client/src/shadows.rs @@ -103,7 +103,10 @@ pub fn shadow( let v = |wz: f64| (-((wz - pos.z) as f32) / 2.0 / radius + 0.5).clamp(0.0, 1.0); let a = (alpha * 255.0) as u32; let color = a << 24 | 0x00FF_FFFF; - let top = f64::from(y) + 0.001; + // ShadowFeatureRenderer: relativeY + shapeBelow.minY, the block top + // itself. A model's feet sit 0.001 above it (LivingEntityRenderer's + // translate(0, -1.501, 0)), so a lifted shadow fights with them. + let top = f64::from(y); for (wx, wz) in [(bx0, bz0), (bx0, bz1), (bx1, bz1), (bx1, bz0)] { let p = DVec3::new(wx, top, wz) - camera; out.push(Vertex { @@ -113,7 +116,7 @@ pub fn shadow( sprite.u0 + (sprite.u1 - sprite.u0) * u(wx), sprite.v0 + (sprite.v1 - sprite.v0) * v(wz), ], - layer: 0, + layer: crate::gfx::SHADOW_OVERLAY, color, light: 240 | 240 << 8, }); @@ -127,6 +130,52 @@ pub fn shadow( mod tests { use super::*; #[test] + fn shadows_lie_on_the_block_top_under_model_feet() { + use crate::chunk::Chunk; + use horizon_world::level::data; + use std::{path::PathBuf, sync::Arc}; + let root = std::env::var_os("HORIZON_RESOURCES") + .map(PathBuf::from) + .unwrap_or_else(|| { + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../../.local/horizon-client") + .join(horizon_core::VANILLA_VERSION) + }); + if !root.join("gameplay-facts.json").exists() { + eprintln!("skipping resource-backed shadow check: {}", root.display()); + return; + } + let registry = data::load_registry(&root).unwrap(); + let facts = BlockFacts::load(&root, ®istry).unwrap(); + let stone = registry.default_state("minecraft:stone").unwrap(); + let mut world = World::new(facts.air, 0); + world.chunks.insert((0, 0), Arc::new(Chunk::empty(0, 0))); + for x in 0..16 { + for z in 0..16 { + world.set(x, 63, z, stone).unwrap(); + } + } + let (camera, pos) = (DVec3::new(8.5, 66.0, 4.0), DVec3::new(8.5, 64.0, 8.5)); + let mut out = Vec::new(); + let sprite = Rect { + u0: 0.0, + v0: 0.0, + u1: 1.0, + v1: 1.0, + w: 16, + h: 16, + }; + shadow( + &world, &facts, sprite, camera, pos, 0.3, 1.0, 0.0, 0, &mut out, + ); + assert_eq!(out.len(), 4); + let feet = (pos.y + (1.501 - 24.0 / 16.0) - camera.y) as f32; + for v in &out { + assert_eq!(v.pos[1], (pos.y - camera.y) as f32); + assert!(v.pos[1] < feet); + } + } + #[test] fn vanilla_radii() { assert_eq!(shadow_params("minecraft:zombie"), (0.5, 1.0)); assert_eq!(shadow_params("minecraft:item"), (0.15, 0.75));