From 9cb03b932e1ab6abd6af3c147412d2fd0411d0d1 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 8 Oct 2026 15:43:25 +0200 Subject: [PATCH] Entity shadows for mobs, dropped items and experience orbs shadows.rs ports EntityRenderer.extractShadow and ShadowFeatureRenderer with the vanilla per-renderer shadow radii and strengths. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 14 ++++ crates/horizon-client/src/lib.rs | 1 + crates/horizon-client/src/shadows.rs | 116 +++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 crates/horizon-client/src/shadows.rs diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 838f47f7f..956fde96c 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2113,6 +2113,20 @@ impl Game<'_> { } } } + // Entity shadows (EntityRenderer.extractShadow). + if let Some(sprite) = self.res.atlas.sprites.get("misc/shadow").copied() { + let ambient = if self.dim == Dim::Nether { 0.1 } else { 0.0 }; + let darken = self.sky_darken(); + let a = alpha; + let lerp = |p: Vec3, q: Vec3| glam::DVec3::new(p.x + (q.x - p.x) * a, p.y + (q.y - p.y) * a, p.z + (q.z - p.z) * a); + let mut casters: Vec<(glam::DVec3, &str)> = self.mobs.iter().map(|m| (lerp(m.previous, m.position), m.kind.id.as_str())).collect(); + casters.extend(self.dropped.iter().map(|i| (lerp(i.previous, i.position), "minecraft:item"))); + casters.extend(self.orbs.iter().map(|o| (lerp(o.previous, o.position), "minecraft:experience_orb"))); + for (pos, id) in casters { + let (radius, strength) = horizon_client::shadows::shadow_params(id); + horizon_client::shadows::shadow(&self.world, &self.res.facts, sprite, camera, pos, radius, strength, ambient, darken, &mut out); + } + } // Block entities (BlockEntityRenderers): chests, shulker boxes, banners, skulls... if let Some(r) = &self.renderer { let lid = |p: [i32; 3]| { diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index 9f3438827..61d6648b4 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -35,6 +35,7 @@ pub mod pose; pub mod save; pub mod screen_effects; pub mod settings; +pub mod shadows; pub mod sprint; pub mod sprites; pub mod streaming; diff --git a/crates/horizon-client/src/shadows.rs b/crates/horizon-client/src/shadows.rs new file mode 100644 index 000000000..e971687da --- /dev/null +++ b/crates/horizon-client/src/shadows.rs @@ -0,0 +1,116 @@ +//! Entity shadows (`EntityRenderer.extractShadow` + `ShadowFeatureRenderer`): +//! `misc/shadow.png` projected onto the tops of full-collision blocks under +//! an entity, faded by camera distance, depth below the entity and the light +//! at each block. +use crate::{blocks::BlockFacts, hud::Rect, mesher::Vertex, world::World}; +use glam::DVec3; + +/// Vanilla renderer shadow radius and strength for an entity type id. +pub fn shadow_params(entity: &str) -> (f32, f32) { + let id = entity.strip_prefix("minecraft:").unwrap_or(entity); + let radius = match id { + "item" | "experience_orb" | "tropical_fish" => 0.15, + "tadpole" => 0.14, + "pufferfish" => 0.2, + "slime" | "magma_cube" | "bat" => 0.25, + "chicken" | "cod" | "parrot" | "rabbit" | "frog" | "silverfish" | "endermite" | "vex" => 0.3, + "cat" | "ocelot" | "fox" | "bee" | "allay" | "salmon" => 0.4, + "zombie" | "husk" | "drowned" | "zombie_villager" | "zombified_piglin" | "skeleton" | "stray" | "wither_skeleton" | "bogged" + | "creeper" | "enderman" | "villager" | "wandering_trader" | "witch" | "pillager" | "vindicator" | "evoker" | "illusioner" + | "blaze" | "breeze" | "snow_golem" | "copper_golem" | "strider" | "wolf" | "axolotl" | "tnt" | "falling_block" | "end_crystal" => { + 0.5 + } + "cave_spider" => 0.56, + "creaking" => 0.6, + "pig" | "cow" | "mooshroom" | "sheep" | "goat" | "llama" | "trader_llama" | "iron_golem" | "camel" | "camel_husk" | "squid" + | "glow_squid" | "dolphin" | "turtle" | "hoglin" | "zoglin" | "nautilus" | "zombie_nautilus" | "minecart" => 0.7, + "horse" | "donkey" | "mule" | "skeleton_horse" | "zombie_horse" | "phantom" => 0.75, + "spider" | "boat" => 0.8, + "polar_bear" | "panda" | "warden" => 0.9, + "wither" => 1.0, + "sniffer" | "ravager" => 1.1, + "ghast" => 1.5, + _ => 0.5, + }; + let strength = if matches!(id, "item" | "experience_orb") { 0.75 } else { 1.0 }; + (radius, strength) +} + +/// Shadow quads for an entity at `pos` (interpolated feet position). +#[allow(clippy::too_many_arguments)] +pub fn shadow( + world: &World, + facts: &BlockFacts, + sprite: Rect, + camera: DVec3, + pos: DVec3, + radius: f32, + strength: f32, + ambient_light: f32, + sky_darken: i32, + out: &mut Vec, +) { + let radius = radius.min(32.0); + if radius <= 0.0 { + return; + } + let dist_sq = pos.distance_squared(camera); + let pow = ((1.0 - dist_sq / 256.0) * f64::from(strength)) as f32; + if pow <= 0.0 { + return; + } + let r = f64::from(radius); + let (x0, x1) = ((pos.x - r).floor() as i32, (pos.x + r).floor() as i32); + let (z0, z1) = ((pos.z - r).floor() as i32, (pos.z + r).floor() as i32); + let depth = (pow / 0.5 - 1.0).min(radius); + let (y0, y1) = ((pos.y - f64::from(depth)).floor() as i32, pos.y.floor() as i32); + for z in z0..=z1 { + for x in x0..=x1 { + for y in y0..=y1 { + let below = facts.get(world.block(x, y - 1, z)); + if below.render_shape == crate::blocks::RenderShape::Invisible || !below.full_collision { + continue; + } + let packed = world.light(x, y, z); + let raw = (i32::from(packed >> 4) - sky_darken).max(i32::from(packed & 15)); + if raw <= 3 { + continue; + } + let power_at_depth = pow - (pos.y - f64::from(y)) as f32 * 0.5; + let alpha = (power_at_depth * 0.5 * crate::screen_effects::lightmap_brightness(ambient_light, raw as u8)).clamp(0.0, 1.0); + // The block top, clipped to the shadow square so the UVs stay inside the sprite. + let (bx0, bx1) = (f64::from(x).max(pos.x - r), f64::from(x + 1).min(pos.x + r)); + let (bz0, bz1) = (f64::from(z).max(pos.z - r), f64::from(z + 1).min(pos.z + r)); + if bx1 <= bx0 || bz1 <= bz0 { + continue; + } + let u = |wx: f64| (-((wx - pos.x) as f32) / 2.0 / radius + 0.5).clamp(0.0, 1.0); + 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; + for (wx, wz) in [(bx0, bz0), (bx0, bz1), (bx1, bz1), (bx1, bz0)] { + let p = DVec3::new(wx, top, wz) - camera; + out.push(Vertex { + pos: p.as_vec3().to_array(), + uv: [sprite.u0 + (sprite.u1 - sprite.u0) * u(wx), sprite.v0 + (sprite.v1 - sprite.v0) * v(wz)], + layer: 0, + color, + light: 240 | 240 << 8, + }); + } + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn vanilla_radii() { + assert_eq!(shadow_params("minecraft:zombie"), (0.5, 1.0)); + assert_eq!(shadow_params("minecraft:item"), (0.15, 0.75)); + assert_eq!(shadow_params("minecraft:spider"), (0.8, 1.0)); + } +}