Merge pull request #274 from ludovic111/fix/mesh-out-of-range

Mesh columns loaded past the render distance once they come in range (#228)
This commit is contained in:
Ludovic Marie
2026-10-15 06:48:24 +02:00
committed by GitHub
2 changed files with 137 additions and 2 deletions
+35 -2
View File
@@ -120,6 +120,10 @@ pub struct Streaming {
lit: FxHashSet<(i32, i32)>,
/// Columns whose sections have been meshed at least once.
meshed: FxHashSet<(i32, i32)>,
/// Dirty sections of loaded columns past the render distance, meshed once
/// the player comes closer, and the area they were set aside for.
waiting: FxHashSet<(i32, i32, i32)>,
waiting_area: ((i32, i32), i32, usize),
sequence: u64,
pub center: (i32, i32),
pub radius: i32,
@@ -249,6 +253,8 @@ impl Streaming {
meshing: Default::default(),
lit: Default::default(),
meshed: Default::default(),
waiting: Default::default(),
waiting_area: ((0, 0), 0, 0),
sequence: 0,
center: (0, 0),
radius: 8,
@@ -303,6 +309,7 @@ impl Streaming {
self.meshed.remove(&(x, z));
self.lighting.remove(&(x, z));
self.meshing.retain(|k, _| (k.0, k.2) != (x, z));
self.waiting.retain(|k| (k.0, k.2) != (x, z));
world.dirty_sections.retain(|k| (k.0, k.2) != (x, z));
world.chunks.remove(&(x, z))
}
@@ -484,6 +491,7 @@ impl Streaming {
if !far.is_empty() {
let far: HashSet<(i32, i32)> = far.iter().copied().collect();
self.meshing.retain(|k, _| !far.contains(&(k.0, k.2)));
self.waiting.retain(|k| !far.contains(&(k.0, k.2)));
world.dirty_sections.retain(|k| !far.contains(&(k.0, k.2)));
}
// Drop far proto chunks that are not checked out.
@@ -706,15 +714,40 @@ impl Streaming {
Job::Light(x, z, region, self.sequence, self.sky_light),
);
}
// Meshing once the column and its neighbours are lit.
// Meshing once the column and its neighbours are lit, and the column
// is within the render distance. 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());
if self.waiting_area != area {
self.waiting_area = area;
let ranges = Ranges {
center: self.center,
radius: self.radius,
extra: &self.extra,
forced: &self.forced,
};
self.waiting.retain(|&(x, sy, z)| {
let back = ranges.in_main_range(x, z, 0);
if back {
world.dirty_sections.insert((x, sy, z));
}
!back
});
}
let dirty: Vec<_> = world.dirty_sections.iter().copied().collect();
let mut columns: FxHashMap<(i32, i32), Option<Box<Region>>> = FxHashMap::default();
let mut ready_columns: FxHashMap<(i32, i32), bool> = FxHashMap::default();
for (x, sy, z) in dirty {
if !world.chunks.contains_key(&(x, z)) || !self.in_main_range(x, z, 0) {
if !world.chunks.contains_key(&(x, z)) {
world.dirty_sections.remove(&(x, sy, z));
continue;
}
if !self.in_main_range(x, z, 0) {
world.dirty_sections.remove(&(x, sy, z));
self.waiting.insert((x, sy, z));
continue;
}
// Once per column: the 3x3 lit check would otherwise repeat for
// each of its sections every frame.
let lit = &self.lit;
@@ -0,0 +1,102 @@
//! Every column the player comes close to gets meshed (#228): columns that
//! load with their light, past the render distance, used to drop their
//! sections' mesh requests there, and nothing asked again once the player
//! came closer, so they stayed invisible until a block in them changed.
use horizon_client::{
assets::Assets,
biome::Biomes,
blocks::BlockFacts,
chunk::{Chunk, HEIGHT},
light::LightTables,
mesher::MeshTables,
models,
streaming::{ChunkGenerator, Proto, Streaming, WorkerContext},
world::World,
};
use horizon_world::registry::BlockStateId;
use std::{
collections::HashSet,
path::Path,
sync::Arc,
thread,
time::{Duration, Instant},
};
fn resources() -> Option<std::path::PathBuf> {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3");
root.join("items.json").exists().then_some(root)
}
/// A saved world: every column loads with its light, as saved chunks do.
struct Saved;
impl ChunkGenerator for Saved {
type Worker = ();
fn worker(&self) {}
fn load(&self, x: i32, z: i32) -> Option<Chunk> {
let mut chunk = Chunk::empty(x, z);
chunk.lit = true;
chunk.light_loaded = true;
Some(chunk)
}
fn proto(&self, _: &mut (), _: i32, _: i32) -> Proto {
unreachable!("every column is saved")
}
fn finish(&self, _: Proto) -> Chunk {
unreachable!("every column is saved")
}
}
#[test]
fn columns_loaded_past_the_render_distance_mesh_when_they_come_in_range() {
let Some(root) = resources() else {
return;
};
let assets = Arc::new(Assets::load(&root).unwrap());
let facts = BlockFacts::load(&root, assets.registry).unwrap();
let visibility = models::load_visibility(&root, assets.registry).unwrap();
let context = Arc::new(WorkerContext {
tables: Arc::new(MeshTables::new(&assets, &facts, visibility).unwrap()),
light: Arc::new(LightTables::load(&root, assets.registry, &facts).unwrap()),
biomes: Arc::new(Biomes::load(&root).unwrap()),
assets,
});
let mut streaming = Streaming::new(Arc::new(Saved), context, 3);
streaming.radius = 4;
let mut world = World::new(BlockStateId(0), 0);
let mut meshed = HashSet::new();
let mut settle_at = |streaming: &mut Streaming, center: (i32, i32)| {
streaming.center = center;
let deadline = Instant::now() + Duration::from_secs(60);
let mut quiet = 0;
while quiet < 40 {
assert!(Instant::now() < deadline, "streaming never settled");
let before = (world.chunks.len(), meshed.len());
let (uploads, _) = streaming.update(&mut world);
meshed.extend(uploads.into_iter().map(|(key, _)| key));
thread::sleep(Duration::from_millis(5));
let idle = streaming.pending() == 0 && before == (world.chunks.len(), meshed.len());
quiet = if idle { quiet + 1 } else { 0 };
}
};
// Columns up to three chunks past the render distance load around the
// first position; the second brings some of them within it.
settle_at(&mut streaming, (0, 0));
settle_at(&mut streaming, (3, 0));
let (cx, cz): (i32, i32) = (3, 0);
let mut checked = 0;
for x in cx - 4..=cx + 4 {
for z in cz - 4..=cz + 4 {
if (x - cx).pow(2) + (z - cz).pow(2) > 16 {
continue;
}
for sy in 0..HEIGHT / 16 {
assert!(
meshed.contains(&(x, sy, z)),
"section {x} {sy} {z} is in range but was never meshed"
);
}
checked += 1;
}
}
assert_eq!(checked, 49);
}