From 361c994d98b44ae94edb8e8bc130022c8760dc72 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 00:23:01 +0200 Subject: [PATCH] Draw the columns vanilla draws: ChunkTrackingView.isInViewDistance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Vanilla draws a section when its column is within the render distance of the camera's column with a column of slack on each axis (SectionOcclusionGraph.isInViewDistance → ChunkTrackingView): 533 columns at render distance 12. Horizon meshed a circle of radius 12 (441 columns) and drew sections whose centre lay within 16 x distance + 24 blocks, so the edge of the world, mostly in fog, missed columns vanilla shows, and fell short of vanilla's corners. `streaming::in_view_distance` is vanilla's test. Streaming meshes the columns it accepts around the player (and forced ones), and the renderer's column walk draws the ones it accepts around the camera. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 2 +- crates/horizon-client/src/gfx/resident.rs | 58 ++++++++++++----------- crates/horizon-client/src/streaming.rs | 49 ++++++++++++++++--- 3 files changed, 74 insertions(+), 35 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 4401ea22d..f64200e5c 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -2217,7 +2217,7 @@ impl Renderer { visible.clear(); self.sections.visible( camera, - frame.distance, + (frame.distance / 16.0) as i32, &view, |k| !culling || reachable.contains(k), &mut visible, diff --git a/crates/horizon-client/src/gfx/resident.rs b/crates/horizon-client/src/gfx/resident.rs index 58e187090..71e6a5527 100644 --- a/crates/horizon-client/src/gfx/resident.rs +++ b/crates/horizon-client/src/gfx/resident.rs @@ -3,7 +3,7 @@ //! the render distance rejects all of its sections with one test, and the //! sections that remain are reached by index rather than by hashing keys. use super::{Frustum, SectionKey}; -use crate::fast_hash::FxHashMap; +use crate::{fast_hash::FxHashMap, streaming::in_view_distance}; use glam::{DVec3, Vec3}; /// One chunk column's resident sections. @@ -149,26 +149,28 @@ fn origin(key: SectionKey, camera: DVec3) -> DVec3 { ) - camera } -/// Whether the section at `key` is drawn: within `distance` blocks of the -/// camera horizontally (plus a section and a half), and partly inside the -/// frustum. `None` when it is not, else its squared centre distance and origin. -fn in_view( - key: SectionKey, - camera: DVec3, - distance: f32, - frustum: &Frustum, -) -> Option<(f64, Vec3)> { - let origin = origin(key, camera); - let center = origin + DVec3::splat(8.0); - let horizontal = (center.x * center.x + center.z * center.z).sqrt(); - if horizontal > f64::from(distance) + 24.0 { +/// The camera's chunk column. +fn camera_column(camera: DVec3) -> (i32, i32) { + ( + (camera.x.floor() as i32) >> 4, + (camera.z.floor() as i32) >> 4, + ) +} + +/// Whether the section at `key` is drawn: its column within the render +/// distance of the camera's (`SectionOcclusionGraph.isInViewDistance`), and +/// partly inside the frustum. `None` when it is not, else its squared centre +/// distance and origin. +fn in_view(key: SectionKey, camera: DVec3, radius: i32, frustum: &Frustum) -> Option<(f64, Vec3)> { + if !in_view_distance(camera_column(camera), radius, key.0, key.2) { return None; } + let origin = origin(key, camera); let min = origin.as_vec3(); if !frustum.intersects(min, min + Vec3::splat(16.0)) { return None; } - Some((center.length_squared(), min)) + Some(((origin + DVec3::splat(8.0)).length_squared(), min)) } impl Resident { @@ -178,22 +180,22 @@ impl Resident { /// Columns are tested first, with the same distance test (it depends /// only on x and z) and a box spanning their sections: every section box /// lies inside it and the frustum test rounds monotonically, so a column - /// the frustum rejects holds no section it would accept. + /// the frustum rejects holds no section it would accept. `radius` is the + /// render distance in chunks. pub fn visible( &self, camera: DVec3, - distance: f32, + radius: i32, frustum: &Frustum, reachable: impl Fn(SectionKey) -> bool, out: &mut Vec, ) { + let from = camera_column(camera); for column in &self.columns { - let low = origin((column.x, column.low, column.z), camera); - let center = low + DVec3::splat(8.0); - if (center.x * center.x + center.z * center.z).sqrt() > f64::from(distance) + 24.0 { + if !in_view_distance(from, radius, column.x, column.z) { continue; } - let min = low.as_vec3(); + let min = origin((column.x, column.low, column.z), camera).as_vec3(); let top = origin((column.x, column.high, column.z), camera).as_vec3(); if !frustum.intersects(min, top + Vec3::splat(16.0)) { continue; @@ -203,7 +205,7 @@ impl Resident { if !reachable(key) { continue; } - if let Some((d, o)) = in_view(key, camera, distance, frustum) { + if let Some((d, o)) = in_view(key, camera, radius, frustum) { out.push(Visible { distance: d.to_bits(), key, @@ -243,20 +245,20 @@ mod tests { (DVec3::new(8.0, 64.0, 8.0), 45.0, 89.0), ]; for (camera, yaw, pitch) in cameras { - for distance in [32.0, 96.0, 160.0] { + for radius in [2, 6, 10] { let projection = super::super::world_projection(70.0, 16.0 / 9.0, 512.0); let view = crate::camera::view_matrix(yaw, pitch); let frustum = Frustum { planes: super::super::frustum(projection * view), }; let mut fast = Vec::new(); - r.visible(camera, distance, &frustum, reachable, &mut fast); + r.visible(camera, radius, &frustum, reachable, &mut fast); let mut slow: Vec<(SectionKey, f64, [f32; 3])> = r .items .iter() .filter(|(k, _)| reachable(*k)) .filter_map(|(k, _)| { - in_view(*k, camera, distance, &frustum).map(|(d, o)| (*k, d, o.to_array())) + in_view(*k, camera, radius, &frustum).map(|(d, o)| (*k, d, o.to_array())) }) .collect(); slow.sort_unstable_by(|a, b| a.1.total_cmp(&b.1).then(a.0.cmp(&b.0))); @@ -268,7 +270,7 @@ mod tests { }) .collect(); assert!(!slow.is_empty()); - assert_eq!(fast, slow, "{camera} {yaw} {pitch} {distance}"); + assert_eq!(fast, slow, "{camera} {yaw} {pitch} {radius}"); } } } @@ -356,7 +358,7 @@ mod tests { let t = Instant::now(); keys.clear(); keys.extend(map.keys().filter_map(|&k| { - in_view(k, camera, 512.0, &frustum).map(|(d, o)| (k, d, o.to_array())) + in_view(k, camera, 32, &frustum).map(|(d, o)| (k, d, o.to_array())) })); keys.sort_unstable_by(|a, b| a.1.total_cmp(&b.1)); for (k, _, _) in &keys { @@ -365,7 +367,7 @@ mod tests { old = old.min(t.elapsed()); let t = Instant::now(); list.clear(); - r.visible(camera, 512.0, &frustum, |_| true, &mut list); + r.visible(camera, 32, &frustum, |_| true, &mut list); for v in &list { sum += i64::from(r.get(v.slot)[1] + r.get(v.slot)[0]); } diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index df1001640..994316324 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -968,7 +968,8 @@ impl Streaming { return (uploads, removed); } // Meshing once the column and its neighbours are lit, and the column - // is within the render distance. Sections set aside past it come back + // is within the render distance (`in_view_distance`, the columns + // vanilla draws). Sections set aside past it come back // when the player moves: a column that loads with its light gets no // other request (#228). let area = (self.center, self.radius, self.forced.len()); @@ -981,7 +982,7 @@ impl Streaming { forced: &self.forced, }; self.waiting.retain(|&(x, sy, z)| { - let back = ranges.in_main_range(x, z, 0); + let back = ranges.in_view(x, z); if back { world.dirty_sections.insert((x, sy, z)); } @@ -996,7 +997,7 @@ impl Streaming { world.dirty_sections.remove(&(x, sy, z)); continue; } - if !self.in_main_range(x, z, 0) { + if !self.ranges().in_view(x, z) { world.dirty_sections.remove(&(x, sy, z)); self.waiting.insert((x, sy, z)); continue; @@ -1204,9 +1205,15 @@ impl Streaming { fn in_range(&self, x: i32, z: i32, margin: i32) -> bool { self.ranges().in_range(x, z, margin) } - fn in_main_range(&self, x: i32, z: i32, margin: i32) -> bool { - self.ranges().in_main_range(x, z, margin) - } +} +/// `ChunkTrackingView.isInViewDistance`: whether the column (x, z) is drawn +/// at this render distance around `center`, the camera's column +/// (`SectionOcclusionGraph.isInViewDistance`). Each axis gets a column of +/// slack, so this reaches past the circle of `radius` columns. +pub fn in_view_distance(center: (i32, i32), radius: i32, x: i32, z: i32) -> bool { + let gap = |d: i32| i64::from((d.abs() - 1).max(0)); + let (dx, dz) = (gap(x - center.0), gap(z - center.1)); + dx * dx + dz * dz < i64::from(radius) * i64::from(radius) } /// The areas kept loaded: circles around the player and `extra`, and forced chunks. struct Ranges<'a> { @@ -1271,6 +1278,11 @@ impl Ranges<'_> { dx * dx + dz * dz <= r * r }) } + /// The columns meshed for drawing: those vanilla draws around the + /// player (`in_view_distance`), and forced ones. + fn in_view(&self, x: i32, z: i32) -> bool { + in_view_distance(self.center, self.radius, x, z) || self.forced.contains(&(x, z)) + } fn in_main_range(&self, x: i32, z: i32, margin: i32) -> bool { let r = i64::from(self.radius + margin); let (dx, dz) = (i64::from(x - self.center.0), i64::from(z - self.center.1)); @@ -1306,6 +1318,31 @@ impl Drop for Streaming { mod tests { use super::*; + #[test] + fn view_distance_is_vanillas_shape() { + // ChunkTrackingView.isInViewDistance at render distance 12: a column + // of slack on each axis, so 533 columns rather than the 441 of a + // circle of radius 12. + let count = |r: i32| { + (-40..=40) + .flat_map(|x| (-40..=40).map(move |z| (x, z))) + .filter(|&(x, z)| in_view_distance((5, -7), r, x + 5, z - 7)) + .count() + }; + assert_eq!(count(12), 533); + assert_eq!(count(2), 25); + for (x, z, inside) in [(12, 0, true), (13, 0, false), (9, 9, true), (10, 9, false)] { + assert_eq!(in_view_distance((0, 0), 12, x, z), inside, "({x}, {z})"); + assert_eq!( + in_view_distance((0, 0), 12, -z, -x), + inside, + "({}, {})", + -z, + -x + ); + } + } + struct GenerationFixture { world: World, center: (i32, i32),