diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 25896253e..fa0f732dd 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2543,6 +2543,8 @@ impl Game<'_> { distance: (self.streaming.radius * 16) as f32, underwater, title, + // The camera inside an opaque block sees nothing through it: disable. + cave_culling: !self.res.facts.get(self.world.block(block[0], block[1], block[2])).solid_render, water_fog, outline, hud: &hud, diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 81cff662b..0d3233e46 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -40,6 +40,9 @@ pub struct Frame<'a> { /// Render distance in blocks (fog end). pub distance: f32, pub underwater: bool, + /// Skip sections the camera cannot see through open faces (vanilla's + /// occlusion graph; off in spectator-like views inside blocks). + pub cave_culling: bool, /// Title screen: only the panorama (in `celestial`) and the HUD. pub title: bool, pub water_fog: [f32; 3], @@ -82,6 +85,11 @@ pub struct Renderer { outline: wgpu::Buffer, depth: wgpu::TextureView, sections: HashMap, + /// VisGraph face connectivity per meshed section (cave culling). + visibility: HashMap, + reachable: std::collections::HashSet, + reachable_from: Option, + visibility_dirty: bool, pub hud: HudRenderer, pub info: wgpu::AdapterInfo, pub suspended: bool, @@ -662,6 +670,10 @@ impl Renderer { outline, depth, sections: HashMap::new(), + visibility: HashMap::new(), + reachable: std::collections::HashSet::new(), + reachable_from: None, + visibility_dirty: true, hud, info, suspended: size.width == 0 || size.height == 0, @@ -715,6 +727,9 @@ impl Renderer { self.sections.len() } pub fn upload(&mut self, key: SectionKey, mesh: &SectionMesh) { + if self.visibility.insert(key, mesh.visibility) != Some(mesh.visibility) { + self.visibility_dirty = true; + } let solid = (mesh.solid.len() / 4) as u32; let translucent = (mesh.translucent.len() / 4) as u32; if solid + translucent == 0 { @@ -749,8 +764,46 @@ impl Renderer { } pub fn clear_sections(&mut self) { self.sections.clear(); + self.visibility.clear(); + self.visibility_dirty = true; + } + /// SectionOcclusionGraph: breadth-first from the camera section through + /// faces its VisGraph connects, never stepping back towards the camera. + fn update_reachable(&mut self, from: SectionKey, radius: i32) { + const STEPS: [(i32, i32, i32); 6] = [(0, -1, 0), (0, 1, 0), (0, 0, -1), (0, 0, 1), (-1, 0, 0), (1, 0, 0)]; + let opposite = |d: usize| d ^ 1; + self.reachable.clear(); + let mut queue = std::collections::VecDeque::new(); + // (section, entered face, directions travelled) + queue.push_back((from, None::, 0u8)); + self.reachable.insert(from); + let sections = (crate::chunk::HEIGHT / 16) as i32; + while let Some((key, entered, travelled)) = queue.pop_front() { + let vis = self.visibility.get(&key).copied().unwrap_or(crate::mesher::ALL_VISIBLE); + for (d, step) in STEPS.iter().enumerate() { + if travelled & (1 << opposite(d)) != 0 { + continue; + } + if let Some(e) = entered + && vis & (1 << (e * 6 + d)) == 0 + { + continue; + } + let next = (key.0 + step.0, key.1 + step.1, key.2 + step.2); + if next.1 < -1 || next.1 > sections || (next.0 - from.0).abs() > radius || (next.2 - from.2).abs() > radius { + continue; + } + if self.reachable.insert(next) { + queue.push_back((next, Some(opposite(d)), travelled | (1 << d))); + } + } + } + self.reachable_from = Some(from); + self.visibility_dirty = false; } pub fn remove(&mut self, key: SectionKey) { + self.visibility.remove(&key); + self.visibility_dirty = true; self.sections.remove(&key); } pub fn view_projection(&self, frame: &Frame) -> Mat4 { @@ -916,9 +969,21 @@ impl Renderer { // Frustum culling and camera-relative origins. let planes = frustum(view_proj); let camera = frame.camera; + let camera_section = ( + (camera.x.floor() as i32) >> 4, + (camera.y.floor() as i32 - crate::chunk::MIN_Y) >> 4, + (camera.z.floor() as i32) >> 4, + ); + let culling = !frame.title && frame.cave_culling; + if culling && (self.visibility_dirty || self.reachable_from != Some(camera_section)) { + let radius = (frame.distance / 16.0) as i32 + 2; + self.update_reachable(camera_section, radius); + } + let reachable = &self.reachable; let mut visible: Vec<(SectionKey, f64, [f32; 3])> = self .sections .keys() + .filter(|k| !culling || reachable.contains(k)) .filter_map(|&(sx, sy, sz)| { let origin = DVec3::new( f64::from(sx * 16), diff --git a/crates/horizon-client/src/mesher.rs b/crates/horizon-client/src/mesher.rs index 04421b208..d149483e1 100644 --- a/crates/horizon-client/src/mesher.rs +++ b/crates/horizon-client/src/mesher.rs @@ -26,10 +26,64 @@ pub struct Vertex { /// Smooth light, 16x fixed point: sky in bits 0..8, block in bits 8..16. pub light: u32, } -#[derive(Default)] pub struct SectionMesh { pub solid: Vec, pub translucent: Vec, + /// VisGraph: bit `a * 6 + b` is set when faces `a` and `b` (Direction + /// order) are connected through non-opaque blocks. + pub visibility: u64, +} +impl Default for SectionMesh { + fn default() -> Self { + Self { solid: Vec::new(), translucent: Vec::new(), visibility: ALL_VISIBLE } + } +} +pub const ALL_VISIBLE: u64 = (1 << 36) - 1; +/// VisGraph.resolve: flood fill non-opaque cells and connect the faces each region touches. +fn visibility(pad: &Padded, info: &[Info]) -> u64 { + let mut seen = vec![false; 4096]; + let mut result = 0u64; + let opaque = |x: i32, y: i32, z: i32| info[pad.blocks[pi(x, y, z)] as usize].solid; + let mut stack = Vec::new(); + for start in 0..4096usize { + let (x, y, z) = ((start & 15) as i32, (start >> 8) as i32, ((start >> 4) & 15) as i32); + if seen[start] || opaque(x, y, z) { + continue; + } + let mut faces = 0u8; + seen[start] = true; + stack.push(start); + while let Some(c) = stack.pop() { + let (x, y, z) = ((c & 15) as i32, (c >> 8) as i32, ((c >> 4) & 15) as i32); + if y == 0 { faces |= 1 << 0; } + if y == 15 { faces |= 1 << 1; } + if z == 0 { faces |= 1 << 2; } + if z == 15 { faces |= 1 << 3; } + if x == 0 { faces |= 1 << 4; } + if x == 15 { faces |= 1 << 5; } + for (dx, dy, dz) in [(1, 0, 0), (-1, 0, 0), (0, 1, 0), (0, -1, 0), (0, 0, 1), (0, 0, -1)] { + let (nx, ny, nz) = (x + dx, y + dy, z + dz); + if !(0..16).contains(&nx) || !(0..16).contains(&ny) || !(0..16).contains(&nz) { + continue; + } + let n = (ny << 8 | nz << 4 | nx) as usize; + if !seen[n] && !opaque(nx, ny, nz) { + seen[n] = true; + stack.push(n); + } + } + } + for a in 0..6 { + if faces & (1 << a) != 0 { + for b in 0..6 { + if faces & (1 << b) != 0 { + result |= 1 << (a * 6 + b); + } + } + } + } + } + result } #[derive(Clone, Copy, PartialEq, Eq)] enum Tint { @@ -268,6 +322,7 @@ impl Mesher<'_> { return out; } let pad = Padded::new(region, sy); + out.visibility = visibility(&pad, &self.tables.info); let base_y = MIN_Y + sy * 16; let colors = blend(region, self.biomes, center.x, center.z, base_y + 8); let info = &self.tables.info;