From b986165c22142a098bf7d4a3e10e0deaa9451a7e Mon Sep 17 00:00:00 2001 From: sxv20_ Date: Sun, 12 May 2019 21:58:37 +0100 Subject: [PATCH] start fixing chunk rendering, noticed stack overflow crash Former-commit-id: 334904276580cd78f5d5e3bf010a86fcd822cfdf --- common/src/volumes/vol_map.rs | 15 ++++++++----- voxygen/src/mesh/terrain.rs | 41 +++++++++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/common/src/volumes/vol_map.rs b/common/src/volumes/vol_map.rs index f1d908a6c0..d73ede7ee1 100644 --- a/common/src/volumes/vol_map.rs +++ b/common/src/volumes/vol_map.rs @@ -36,7 +36,7 @@ impl VolMap { } #[inline(always)] - fn chunk_offs(pos: Vec3) -> Vec3 { + pub fn chunk_offs(pos: Vec3) -> Vec3 { pos.map2(S::SIZE, |e, sz| e.rem_euclid(sz as i32)) } } @@ -121,12 +121,11 @@ impl SampleVol for VolMap for z in chunk_min.z..=chunk_max.z { let chunk_key = Vec3::new(x, y, z); - let chunk = self - .get_key(chunk_key) - .map(|v| v.clone()) - .ok_or(VolMapErr::NoSuchChunk)?; + let chunk = self.get_key_arc(chunk_key).map(|v| v.clone()); - sample.insert(chunk_key, Arc::new(chunk)); + if let Some(chunk) = chunk { + sample.insert(chunk_key, chunk); + } } } } @@ -177,6 +176,10 @@ impl VolMap { } } + pub fn get_key_arc(&self, key: Vec3) -> Option<&Arc>> { + self.chunks.get(&key) + } + pub fn remove(&mut self, key: Vec3) -> Option>> { self.chunks.remove(&key) } diff --git a/voxygen/src/mesh/terrain.rs b/voxygen/src/mesh/terrain.rs index 50c4ea7667..5e670d513a 100644 --- a/voxygen/src/mesh/terrain.rs +++ b/voxygen/src/mesh/terrain.rs @@ -55,9 +55,9 @@ impl Meshable for VolMap { let mut last_chunk = self.get_key(last_chunk_pos); let size = range.max - range.min; - for x in 1..(size.x - 1) { - for y in 1..(size.y - 1) { - for z in 1..(size.z - 1) { + for x in 0..size.x { + for y in 0..size.y { + for z in 0..size.z { let pos = Vec3::new(x, y, z); let new_chunk_pos = self.pos_key(range.min + pos); @@ -66,15 +66,42 @@ impl Meshable for VolMap { last_chunk_pos = new_chunk_pos; } let offs = pos.map(|e| e as f32 - 1.0); - if let Some(col) = self.get(pos).ok().and_then(|vox| vox.get_color()) { - let col = col.map(|e| e as f32 / 255.0); + if let Some(chunk) = last_chunk { + let chunk_pos = Self::chunk_offs(range.min + pos); + if let Some(col) = chunk.get(chunk_pos).ok().and_then(|vox| vox.get_color()) + { + let col = col.map(|e| e as f32 / 255.0); - vol::push_vox_verts(&mut mesh, self, pos, offs, col, TerrainVertex::new); + vol::push_vox_verts( + &mut mesh, + self, + pos, + offs, + col, + TerrainVertex::new, + ); + } + } else { + if let Some(col) = self + .get(range.min + pos) + .ok() + .and_then(|vox| vox.get_color()) + { + let col = col.map(|e| e as f32 / 255.0); + + vol::push_vox_verts( + &mut mesh, + self, + pos, + offs, + col, + TerrainVertex::new, + ); + } } } } } - mesh } }