From 01f4c56dc10baa1ddaf442b4542c29207a193d65 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 00:21:10 +0200 Subject: [PATCH] Cull boats, paintings, item frames, armor stands, TNT and tridents like vanilla These entities were drawn every frame wherever they were in the loaded world: every armor stand, item frame and painting of every loaded village or base went through its model, item and per-vertex occlusion test even far behind the camera. EntityRenderer.shouldRender now decides, as for mobs and minecarts: - Entity.shouldRenderAtSqrDistance from the current position: 64 times the bounding box's mean edge times the entity distance option (boats about 71 blocks, primed TNT 63, paintings by size); item frames 1024 blocks; armor stands 256 times the box's mean edge whatever the option (4 for a marker's empty box); tridents ten times the default (AbstractArrow); firework rockets 64 blocks. - the interpolated culling box grown by 0.5 in the view; tridents grow it by 1.5 more (ThrownTridentRenderer), armor stands with a dragon head by 0.5 (LivingEntityRenderer), item frames holding a map use the block-wide box. The fishing bobber is unchanged: vanilla never frustum-culls it (FishingHookRenderer.affectedByCulling) and Horizon only draws the player's own, always within its 64 blocks. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/src/game/armor_stands.rs | 26 ++++++++ crates/horizon-client/src/game/boats.rs | 13 +++- .../horizon-client/src/game/entity_culling.rs | 61 +++++++++++++++++++ crates/horizon-client/src/game/fireworks.rs | 14 +++++ crates/horizon-client/src/game/frames.rs | 18 ++++++ crates/horizon-client/src/game/paintings.rs | 9 +++ crates/horizon-client/src/game/tnt.rs | 17 ++++++ crates/horizon-client/src/game/trident.rs | 19 ++++++ 8 files changed, 176 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/game/armor_stands.rs b/crates/horizon-client/src/game/armor_stands.rs index 80e42169d..4ea8df359 100644 --- a/crates/horizon-client/src/game/armor_stands.rs +++ b/crates/horizon-client/src/game/armor_stands.rs @@ -413,6 +413,32 @@ impl Game<'_> { if a.invisible { continue; } + // ArmorStand.shouldRenderAtSqrDistance: 256 times the box's mean + // edge (4 for a marker's empty box), whatever the entity distance. + // LivingEntityRenderer grows the culling box by 0.5 for a dragon head. + let (width, height) = match (a.marker, a.small) { + (true, _) => (0.0, 0.0), + (false, true) => (0.25, 0.9875), + (false, false) => (0.5, 1.975), + }; + let edge = (width + height + width) / 3.0 * 4.0; + let range = if edge == 0.0 { 4.0 } else { edge } * 64.0; + let dragon = a.equipment[5] + .as_ref() + .is_some_and(|i| &*i.item == "minecraft:dragon_head"); + let position = super::entity_culling::at(a.position); + if !self.standing_entity_in_view( + camera, + position, + position, + alpha, + width, + height, + range, + if dragon { 0.5 } else { 0.0 }, + ) { + continue; + } let Some(model) = (if a.small { &assets.small } else { &assets.main }) else { continue; }; diff --git a/crates/horizon-client/src/game/boats.rs b/crates/horizon-client/src/game/boats.rs index a5a609cac..8f2cad5f5 100644 --- a/crates/horizon-client/src/game/boats.rs +++ b/crates/horizon-client/src/game/boats.rs @@ -2,7 +2,7 @@ //! paddling controls, collisions, riding (`positionRider`, `clampRotation`, //! dismounting), `BoatItem` placement, `VehicleEntity.hurtServer` breaking, //! saving and `AbstractBoatRenderer` with animated paddles. -use super::{Game, Keys, view_vector}; +use super::{Game, Keys, entity_culling::at, view_vector}; use horizon_client::{ blocks::Fluid, collisions::SceneCollisions, @@ -700,8 +700,19 @@ impl Game<'_> { /// AbstractBoatRenderer. pub(super) fn boat_geometry(&self, camera: glam::DVec3, alpha: f64, out: &mut Vec) { let a = alpha as f32; + let range = super::entity_culling::default_range( + glam::DVec3::new(WIDTH, HEIGHT, WIDTH), + self.settings.entity_distance, + ); for b in &self.boats { let entity_start = out.len(); + // EntityRenderer.shouldRender. + let (previous, position) = (at(b.previous), at(b.position)); + if !self.standing_entity_in_view( + camera, previous, position, alpha, WIDTH, HEIGHT, range, 0.0, + ) { + continue; + } let models = if b.chest { &self.boat_models.chest_models } else { diff --git a/crates/horizon-client/src/game/entity_culling.rs b/crates/horizon-client/src/game/entity_culling.rs index 71ab13378..b710f4e73 100644 --- a/crates/horizon-client/src/game/entity_culling.rs +++ b/crates/horizon-client/src/game/entity_culling.rs @@ -49,3 +49,64 @@ impl Game<'_> { } } } + +/// Entity.shouldRenderAtSqrDistance's default range: 64 times the bounding +/// box's mean edge, scaled by the entity distance option. +pub(super) fn default_range(size: glam::DVec3, view_scale: f32) -> f64 { + let edge = (size.x + size.y + size.z) / 3.0; + let edge = if edge.is_nan() { 1.0 } else { edge }; + edge * 64.0 * f64::from(view_scale) +} + +impl Game<'_> { + /// EntityRenderer.shouldRender: the entity within `range` blocks of the + /// camera, measured from its current `position`, and its culling box + /// (`lo`..`hi`, interpolated, in world space) grown by 0.5 in the view. + pub(super) fn entity_in_view( + &self, + camera: glam::DVec3, + position: glam::DVec3, + range: f64, + lo: glam::DVec3, + hi: glam::DVec3, + ) -> bool { + if (position - camera).length_squared() >= range * range { + return false; + } + self.view_frustum.as_ref().is_none_or(|view| { + view.intersects( + (lo - camera - glam::DVec3::splat(0.5)).as_vec3(), + (hi - camera + glam::DVec3::splat(0.5)).as_vec3(), + ) + }) + } +} + +/// An entity's position as a `DVec3`. +pub(super) fn at(p: horizon_core::geometry::Vec3) -> glam::DVec3 { + glam::DVec3::new(p.x, p.y, p.z) +} + +impl Game<'_> { + /// `entity_in_view` for an entity whose box stands on its position, + /// `width` wide and `height` tall, interpolated by `alpha` and grown by + /// `grow` (a renderer's getBoundingBoxForCulling). + #[allow(clippy::too_many_arguments)] + pub(super) fn standing_entity_in_view( + &self, + camera: glam::DVec3, + previous: glam::DVec3, + position: glam::DVec3, + alpha: f64, + width: f64, + height: f64, + range: f64, + grow: f64, + ) -> bool { + let p = previous.lerp(position, alpha); + let half = glam::DVec3::new(width / 2.0 + grow, grow, width / 2.0 + grow); + let lo = p - half; + let hi = p + glam::DVec3::new(half.x, height + grow, half.z); + self.entity_in_view(camera, position, range, lo, hi) + } +} diff --git a/crates/horizon-client/src/game/fireworks.rs b/crates/horizon-client/src/game/fireworks.rs index 0e074025f..0d8dd0b12 100644 --- a/crates/horizon-client/src/game/fireworks.rs +++ b/crates/horizon-client/src/game/fireworks.rs @@ -253,6 +253,20 @@ impl Game<'_> { pub(super) fn rocket_geometry(&self, camera: glam::DVec3, alpha: f64, out: &mut Vec) { let orientation = glam::Mat4::from_quat(self.camera_orientation()); for r in self.rockets.iter().filter(|r| !r.attached) { + // FireworkRocketEntity.shouldRenderAtSqrDistance: within 64 blocks; + // EntityRenderer.shouldRender: its 0.25 box in the view. + if !self.standing_entity_in_view( + camera, + super::entity_culling::at(r.previous), + super::entity_culling::at(r.position), + alpha, + 0.25, + 0.25, + 64.0, + 0.0, + ) { + continue; + } let p = glam::DVec3::new( r.previous.x + (r.position.x - r.previous.x) * alpha, r.previous.y + (r.position.y - r.previous.y) * alpha, diff --git a/crates/horizon-client/src/game/frames.rs b/crates/horizon-client/src/game/frames.rs index 3ffa0e67e..2977ad483 100644 --- a/crates/horizon-client/src/game/frames.rs +++ b/crates/horizon-client/src/game/frames.rs @@ -304,9 +304,27 @@ impl Game<'_> { let sprites = &self.atlas.sprites; let mut maps: Vec<(i32, glam::Mat4, u32)> = Vec::new(); let mut items: Vec<(ItemStack, u32, glam::Mat4, u8)> = Vec::new(); + // ItemFrame.shouldRenderAtSqrDistance: 1024 blocks at 100%. + let range = 16.0 * 64.0 * f64::from(self.settings.entity_distance); for f in &self.item_frames { let entity_start = out.len(); let map = f.item.as_ref().and_then(super::maps::map_id); + // EntityRenderer.shouldRender: its box (a block wide with a map), + // measured from the box's centre. + let (lo, hi) = f.bounds(); + let (mut lo, mut hi) = (glam::DVec3::from(lo), glam::DVec3::from(hi)); + if map.is_some() { + let s = f.step(); + for i in 0..3 { + if s[i] == 0 { + let c = (lo[i] + hi[i]) / 2.0; + (lo[i], hi[i]) = (c - 0.5, c + 0.5); + } + } + } + if !self.entity_in_view(camera, (lo + hi) / 2.0, range, lo, hi) { + continue; + } let s = f.step(); // Pose: to the block centre, then X/Y rotation by facing. let (x_rot, y_rot) = if s[1] == 0 { diff --git a/crates/horizon-client/src/game/paintings.rs b/crates/horizon-client/src/game/paintings.rs index 2e5542ccc..c65dbbb89 100644 --- a/crates/horizon-client/src/game/paintings.rs +++ b/crates/horizon-client/src/game/paintings.rs @@ -255,6 +255,15 @@ impl Game<'_> { }; for p in &self.paintings { let entity_start = out.len(); + // EntityRenderer.shouldRender: the default range and its box, + // measured from the box's centre (HangingEntity's position). + let (lo, hi) = p.bounds(); + let (lo, hi) = (glam::DVec3::from(lo), glam::DVec3::from(hi)); + let range = + super::entity_culling::default_range(hi - lo, self.settings.entity_distance); + if !self.entity_in_view(camera, (lo + hi) / 2.0, range, lo, hi) { + continue; + } let Some(v) = self.painting_variants.get(&p.variant) else { continue; }; diff --git a/crates/horizon-client/src/game/tnt.rs b/crates/horizon-client/src/game/tnt.rs index 78f107599..ad77f67c1 100644 --- a/crates/horizon-client/src/game/tnt.rs +++ b/crates/horizon-client/src/game/tnt.rs @@ -241,7 +241,24 @@ impl Game<'_> { .and_then(|name| self.atlas.sprites.get(name).copied()); } } + let range = super::entity_culling::default_range( + glam::DVec3::splat(PrimedTnt::SIZE), + self.settings.entity_distance, + ); for t in &self.tnts { + // EntityRenderer.shouldRender. + if !self.standing_entity_in_view( + camera, + super::entity_culling::at(t.previous), + super::entity_culling::at(t.position), + alpha, + PrimedTnt::SIZE, + PrimedTnt::SIZE, + range, + 0.0, + ) { + continue; + } let fuse = t.fuse as f32 - alpha as f32 + 1.0; let scale = if fuse < 10.0 { let g = (1.0 - fuse / 10.0).clamp(0.0, 1.0); diff --git a/crates/horizon-client/src/game/trident.rs b/crates/horizon-client/src/game/trident.rs index 8c372fa46..2a5a89e1a 100644 --- a/crates/horizon-client/src/game/trident.rs +++ b/crates/horizon-client/src/game/trident.rs @@ -350,7 +350,26 @@ impl Game<'_> { else { return; }; + // AbstractArrow.shouldRenderAtSqrDistance: ten times the default. + let range = 10.0 + * super::entity_culling::default_range( + glam::DVec3::splat(2.0 * HALF), + self.settings.entity_distance, + ); for t in &self.tridents { + // EntityRenderer.shouldRender; ThrownTridentRenderer grows the box by 1.5. + if !self.standing_entity_in_view( + camera, + super::entity_culling::at(t.previous), + super::entity_culling::at(t.position), + alpha, + 2.0 * HALF, + 2.0 * HALF, + range, + 1.5, + ) { + continue; + } let p = glam::DVec3::new( t.previous.x + (t.position.x - t.previous.x) * alpha, t.previous.y + (t.position.y - t.previous.y) * alpha,