diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 1b60f8f0a..c45744bcc 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -9912,6 +9912,9 @@ impl Game<'_> { for key in lod.removed() { renderer.remove_lod(key); } + if let Some(cutout) = lod.take_cutout() { + renderer.set_lod_cutout(cutout); + } } } } diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index e7fbd2d91..c34532c93 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -373,6 +373,12 @@ pub struct Renderer { depth: wgpu::TextureView, sections: resident::Resident, lods: HashMap, + /// The chunk columns the resident tiles leave out (`Lod::take_cutout`): + /// their sections are drawn even past the render distance, so terrain + /// leaving it never opens a hole before the tiles fill it in. + lod_cutout: crate::fast_hash::FxHashSet<(i32, i32)>, + /// Removed sections of `lod_cutout` columns, released once tiles cover them. + lod_retiring: crate::fast_hash::FxHashSet, detail_columns: HashMap<(i32, i32), u32>, arena: arena::Arena, sorted_indices: arena::Arena, @@ -2041,6 +2047,8 @@ impl Renderer { depth, sections: resident::Resident::default(), lods: HashMap::new(), + lod_cutout: Default::default(), + lod_retiring: Default::default(), detail_columns: HashMap::new(), arena, sorted_indices, @@ -2255,6 +2263,9 @@ impl Renderer { if changed { self.block_entity_revision = next_block_entity_revision(); } + if !self.lod_retiring.is_empty() { + self.lod_retiring.remove(&key); + } self.release_section(key); let section = self.upload_mesh(mesh); if section.is_some() || (mesh.solid.is_empty() && mesh.translucent.is_empty()) { @@ -2415,6 +2426,8 @@ impl Renderer { self.block_entity_revision = next_block_entity_revision(); self.sections.clear(); self.lods.clear(); + self.lod_cutout.clear(); + self.lod_retiring.clear(); self.detail_columns.clear(); self.arena.clear(); if let Some(flat) = &mut self.flat { @@ -2441,7 +2454,12 @@ impl Renderer { self.block_entity_revision = next_block_entity_revision(); } self.occlusion.remove(key); - self.release_section(key); + if !self.lod_cutout.is_empty() && self.lod_cutout.contains(&(key.0, key.2)) { + // Distant tiles still leave its column out: drawn until they don't. + self.lod_retiring.insert(key); + } else { + self.release_section(key); + } } /// Run `pack`'s stages over the world from now on (`None`: no pack, the /// vanilla frame). On error the previous pack stays. @@ -2829,6 +2847,7 @@ impl Renderer { }); } let reachable = &self.occlusion; + let cutout = &self.lod_cutout; let mut visible = std::mem::take(&mut self.visible); visible.clear(); crate::spans::time("visibility", || { @@ -2837,6 +2856,7 @@ impl Renderer { (frame.detail_distance / 16.0) as i32, &view, |k| !culling || reachable.contains(k), + |x, z| !cutout.is_empty() && cutout.contains(&(x, z)), &mut visible, ) }); diff --git a/crates/horizon-client/src/gfx/lod.rs b/crates/horizon-client/src/gfx/lod.rs index 6699a62a3..4622c43c0 100644 --- a/crates/horizon-client/src/gfx/lod.rs +++ b/crates/horizon-client/src/gfx/lod.rs @@ -104,6 +104,23 @@ impl Renderer { for (_, tile) in std::mem::take(&mut self.lods) { self.release_mesh(tile.mesh); } + self.set_lod_cutout(Default::default()); + } + + /// The chunk columns the resident tiles now leave out (`Lod::take_cutout`). + /// Removed sections they no longer leave out are released. + pub fn set_lod_cutout(&mut self, cutout: crate::fast_hash::FxHashSet<(i32, i32)>) { + self.lod_cutout = cutout; + let released: Vec<_> = self + .lod_retiring + .iter() + .copied() + .filter(|key| !self.lod_cutout.contains(&(key.0, key.2))) + .collect(); + for key in released { + self.lod_retiring.remove(&key); + self.release_section(key); + } } } diff --git a/crates/horizon-client/src/gfx/resident.rs b/crates/horizon-client/src/gfx/resident.rs index b7455b765..49407e77a 100644 --- a/crates/horizon-client/src/gfx/resident.rs +++ b/crates/horizon-client/src/gfx/resident.rs @@ -163,11 +163,17 @@ fn camera_column(camera: DVec3) -> (i32, i32) { /// 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. +/// distance and origin. `Resident::visible` walks columns instead. +#[cfg(test)] 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; } + in_frustum(key, camera, frustum) +} + +/// `in_view` without the distance test. +fn in_frustum(key: SectionKey, camera: DVec3, frustum: &Frustum) -> Option<(f64, Vec3)> { let origin = origin(key, camera); let min = origin.as_vec3(); if !frustum.intersects(min, min + Vec3::splat(16.0)) { @@ -178,7 +184,10 @@ fn in_view(key: SectionKey, camera: DVec3, radius: i32, frustum: &Frustum) -> Op impl Resident { /// Appends the sections `in_view` and `reachable` to `out`, nearest - /// first (ties by key, so the order never depends on storage). + /// first (ties by key, so the order never depends on storage), and the + /// sections in the frustum of columns past the render distance that + /// `beyond` keeps (distant terrain still leaves them out; the occlusion + /// graph never reaches them). /// /// 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 @@ -191,11 +200,13 @@ impl Resident { radius: i32, frustum: &Frustum, reachable: impl Fn(SectionKey) -> bool, + beyond: impl Fn(i32, i32) -> bool, out: &mut Vec, ) { let from = camera_column(camera); for column in &self.columns { - if !in_view_distance(from, radius, column.x, column.z) { + let near = in_view_distance(from, radius, column.x, column.z); + if !near && !beyond(column.x, column.z) { continue; } let min = origin((column.x, column.low, column.z), camera).as_vec3(); @@ -205,10 +216,10 @@ impl Resident { } for &(y, slot) in &column.sections { let key = (column.x, y, column.z); - if !reachable(key) { + if near && !reachable(key) { continue; } - if let Some((d, o)) = in_view(key, camera, radius, frustum) { + if let Some((d, o)) = in_frustum(key, camera, frustum) { out.push(Visible { distance: d.to_bits(), key, @@ -255,13 +266,22 @@ mod tests { planes: super::super::frustum(projection * view), }; let mut fast = Vec::new(); - r.visible(camera, radius, &frustum, reachable, &mut fast); + // Columns past the distance kept for distant terrain. + let beyond = |x: i32, z: i32| (x + z).rem_euclid(3) == 0; + r.visible(camera, radius, &frustum, reachable, beyond, &mut fast); let mut slow: Vec<(SectionKey, f64, [f32; 3])> = r .items .iter() - .filter(|(k, _)| reachable(*k)) - .filter_map(|(k, _)| { - in_view(*k, camera, radius, &frustum).map(|(d, o)| (*k, d, o.to_array())) + .filter_map(|&(k, _)| { + let near = in_view_distance(camera_column(camera), radius, k.0, k.2); + if near { + in_view(k, camera, radius, &frustum).filter(|_| reachable(k)) + } else if beyond(k.0, k.2) { + in_frustum(k, camera, &frustum) + } else { + None + } + .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))); @@ -370,7 +390,7 @@ mod tests { old = old.min(t.elapsed()); let t = Instant::now(); list.clear(); - r.visible(camera, 32, &frustum, |_| true, &mut list); + r.visible(camera, 32, &frustum, |_| true, |_, _| false, &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/lod.rs b/crates/horizon-client/src/lod.rs index c2e69a291..6bb8e1cd4 100644 --- a/crates/horizon-client/src/lod.rs +++ b/crates/horizon-client/src/lod.rs @@ -422,6 +422,7 @@ enum Done { Mesh { active: FxHashSet, meshes: Vec<(TileKey, SectionMesh)>, + covered: FxHashSet<(i32, i32)>, }, } struct Shared { @@ -452,6 +453,9 @@ pub struct Lod { removed: Vec, covered: FxHashSet<(i32, i32)>, covered_bounds: Option<[i32; 4]>, + /// The covered columns the active meshes leave out, when a mesh job + /// has finished since the last `take_cutout`. + cutout: Option>, saved: FxHashMap<(i32, i32), Arc<[Column; 256]>>, reduced: FxHashMap<(i32, i32), ReducedColumns>, saved_order: VecDeque<(i32, i32)>, @@ -588,7 +592,11 @@ impl Lod { ); meshes.push((input.key, mesh)); } - Done::Mesh { active, meshes } + Done::Mesh { + active, + meshes, + covered, + } } }; if tx.send(done).is_err() { @@ -623,6 +631,7 @@ impl Lod { removed: Vec::new(), covered: FxHashSet::default(), covered_bounds: None, + cutout: None, saved: FxHashMap::default(), reduced: FxHashMap::default(), saved_order: VecDeque::new(), @@ -653,6 +662,13 @@ impl Lod { pub fn removed(&mut self) -> Vec { std::mem::take(&mut self.removed) } + /// The full-detail columns the active meshes leave out, once per finished + /// mesh job. Until a column leaves this set, nothing distant stands in for + /// it: its full-detail terrain must still be drawn, in or past the render + /// distance, or a hole opens there. + pub fn take_cutout(&mut self) -> Option> { + self.cutout.take() + } pub fn invalidate_chunk(&mut self, x: i32, z: i32) { if self.meshing { self.invalidated.insert((x, z)); @@ -882,7 +898,9 @@ impl Lod { Done::Mesh { active, meshes: completed, + covered, } => { + self.cutout = Some(covered); // Retire parents only when the entire replacement is ready. self.removed .extend(self.active.difference(&active).copied()); diff --git a/crates/horizon-client/src/lod/mesh.rs b/crates/horizon-client/src/lod/mesh.rs index a991a4a8e..50082fc3c 100644 --- a/crates/horizon-client/src/lod/mesh.rs +++ b/crates/horizon-client/src/lod/mesh.rs @@ -377,6 +377,7 @@ fn mesh_columns_with_edges( } } let column = &prepared[z * CELLS + x]; + let own_sky = column.last().map_or(0, |top| top.span.light & 0xff); for dz in (0..stride).step_by(step as usize) { for dx in (0..stride).step_by(step as usize) { if is_covered(wx + dx, wz + dz) { @@ -462,10 +463,21 @@ fn mesh_columns_with_edges( // Near meshes may have culled against the real neighbor. // Keep a finite seal where their full-detail area begins. if is_covered(nw[0], nw[1]) { - add_side(&mut faces, direction, plane, u0, u1, segment, &[], side); + add_side( + &mut faces, + direction, + plane, + u0, + u1, + segment, + &[], + side, + own_sky, + ); } else if !edge { add_side( &mut faces, direction, plane, u0, u1, segment, column, side, + own_sky, ); } else if (0..CELLS as i32).contains(&nx) && (0..CELLS as i32).contains(&nz) @@ -479,6 +491,7 @@ fn mesh_columns_with_edges( segment, &prepared[nz as usize * CELLS + nx as usize], side, + own_sky, ); } else { let edge_side = match direction { @@ -498,6 +511,7 @@ fn mesh_columns_with_edges( segment, &[], side, + own_sky, ); } // A coarse face can meet several finer columns. @@ -515,6 +529,7 @@ fn mesh_columns_with_edges( segment, &prepared[edge_offsets[edge_side] + i], side, + own_sky, ); } } @@ -621,6 +636,11 @@ mod refinement_tests { } } +/// Adds the parts of `segment`'s side not hidden by `neighbor`, the column +/// it faces. Parts above the neighbour's top face open air: reduced columns +/// and the seals at full-detail edges expose sides that were buried (their +/// recorded light is a cave's or none), so those take at least the sky light +/// over the top of the neighbour (or of this column, with no neighbour). fn add_side( faces: &mut Faces, direction: Direction, @@ -630,7 +650,23 @@ fn add_side( segment: &Segment<'_>, neighbor: &[Segment<'_>], side: Material, + own_sky: u16, ) { + let (open, sky) = neighbor.last().map_or((f32::NEG_INFINITY, own_sky), |top| { + (f32::from(top.span.max), top.span.light & 0xff) + }); + let mut add = |v0: f32, v1: f32| { + let mut lit = side; + for paint in lit.faces.iter_mut().flatten() { + paint.light = (paint.light & 0xff00) | (paint.light & 0xff).max(sky); + } + let split = open.clamp(v0, v1); + for (v0, v1, material) in [(v0, split, side), (split, v1, lit)] { + if v0 < v1 { + add_face(faces, direction, plane, Rect { u0, v0, u1, v1 }, material); + } + } + }; let mut floor = segment.min; for next in neighbor { if next.max <= floor @@ -640,18 +676,7 @@ fn add_side( continue; } if floor < next.min { - add_face( - faces, - direction, - plane, - Rect { - u0, - v0: floor, - u1, - v1: next.min.min(segment.max), - }, - side, - ); + add(floor, next.min.min(segment.max)); } floor = floor.max(next.max); if floor >= segment.max { @@ -659,18 +684,7 @@ fn add_side( } } if floor < segment.max { - add_face( - faces, - direction, - plane, - Rect { - u0, - v0: floor, - u1, - v1: segment.max, - }, - side, - ); + add(floor, segment.max); } } @@ -960,6 +974,44 @@ mod tests { ); } #[test] + fn exposed_buried_sides_take_the_sky_light_over_their_neighbor() { + let mut columns = columns(); + let dark = |min, max| Span { + light: 0, + ..span(min, max, 1) + }; + // A reduced column whose lower blocks were buried (no light), next to + // a lower column, and to one with a cave below its top. + columns[0].spans = vec![dark(0, 9), span(9, 10, 1)]; + columns[1].spans = vec![span(0, 4, 1)]; + columns[CELLS].spans = vec![span(0, 2, 1), span(6, 12, 1)]; + let mesh = mesh(&columns); + let quads = mesh.solid.as_chunks::<4>().0; + let bounds = |q: &[TerrainVertex; 4]| { + let ys = q.iter().map(|v| v.pos[1]); + ( + ys.clone().fold(f32::INFINITY, f32::min), + ys.fold(0.0, f32::max), + ) + }; + // Over the lower neighbour: open air, lit like its top. + let open: Vec<_> = quads + .iter() + .filter(|q| q.iter().all(|v| v.pos[0] == 1.0 && v.pos[2] <= 1.0) && normal(q).x > 0.0) + .collect(); + assert_eq!(open.len(), 1); + assert_eq!(bounds(open[0]), (4.0, 10.0)); + assert!(open[0].iter().all(|v| v.light == 240)); + // Into the cave: its recorded light. + let cave: Vec<_> = quads + .iter() + .filter(|q| q.iter().all(|v| v.pos[2] == 1.0 && v.pos[0] <= 1.0) && normal(q).z > 0.0) + .collect(); + assert_eq!(cave.len(), 1); + assert_eq!(bounds(cave[0]), (2.0, 6.0)); + assert!(cave[0].iter().all(|v| v.light == 0)); + } + #[test] fn greedy_faces_preserve_material_and_light_boundaries() { let mut columns = columns(); columns[0].spans = vec![span(0, 1, 1)];