From a1e847d7296ef5bd188a99d45358da0ba3f1ad17 Mon Sep 17 00:00:00 2001 From: Joacim Magnusson Date: Sat, 4 Nov 2023 18:19:31 +0100 Subject: [PATCH] Fix issue where filled underground chunks would never get meshed, even if they were modified --- src/voxel.rs | 4 ++++ src/voxel_world.rs | 35 ++++++++++++++++++----------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/voxel.rs b/src/voxel.rs index db5969a..baced8c 100644 --- a/src/voxel.rs +++ b/src/voxel.rs @@ -11,6 +11,10 @@ impl WorldVoxel { pub fn is_unset(&self) -> bool { *self == WorldVoxel::Unset } + + pub fn is_air(&self) -> bool { + *self == WorldVoxel::Air + } } impl Voxel for WorldVoxel { diff --git a/src/voxel_world.rs b/src/voxel_world.rs index d35462f..86c0b2f 100644 --- a/src/voxel_world.rs +++ b/src/voxel_world.rs @@ -383,6 +383,7 @@ impl<'w, 's> VoxelWorldInternal<'w, 's> { modified_voxels: self.modified_voxels.clone(), mesh: None, is_empty: true, + is_full: false, }; let thread = thread_pool.spawn(async move { @@ -446,21 +447,20 @@ impl<'w, 's> VoxelWorldMeshSpawner<'w, 's> { if let Some(chunk_task) = thread_result { if !chunk_task.is_empty { - self.commands - .entity(chunk.entity) - .insert(MaterialMeshBundle { - mesh: self.mesh_assets.add(chunk_task.mesh.unwrap()), - material: self.material_handle.0.clone(), - transform: *transform, - ..default() - }) - .remove::(); - - { - let mut chunk_map_write = (*self.chunk_map).write().unwrap(); - let chunk_data_mut = chunk_map_write.get_mut(&chunk.position).unwrap(); - chunk_data_mut.voxels = chunk_task.voxels; + if !chunk_task.is_full { + self.commands + .entity(chunk.entity) + .insert(MaterialMeshBundle { + mesh: self.mesh_assets.add(chunk_task.mesh.unwrap()), + material: self.material_handle.0.clone(), + transform: *transform, + ..default() + }) + .remove::(); } + let mut chunk_map_write = (*self.chunk_map).write().unwrap(); + let chunk_data_mut = chunk_map_write.get_mut(&chunk.position).unwrap(); + chunk_data_mut.voxels = chunk_task.voxels; } } @@ -501,6 +501,7 @@ pub(crate) struct ChunkTask { voxels: Arc<[WorldVoxel; PaddedChunkShape::SIZE as usize]>, modified_voxels: ModifiedVoxels, is_empty: bool, + is_full: bool, mesh: Option, } @@ -524,7 +525,7 @@ impl ChunkTask { if let Some(voxel) = modified_voxels.get(&block_pos) { voxels[i as usize] = *voxel; - if !voxel.is_unset() { + if !voxel.is_unset() && !voxel.is_air() { filled_count += 1; } continue; @@ -541,8 +542,8 @@ impl ChunkTask { self.voxels = Arc::new(voxels); - // If the chunk is empty or full, we don't need to mesh it. - self.is_empty = filled_count == PaddedChunkShape::SIZE || filled_count == 0; + self.is_empty = filled_count == 0; + self.is_full = filled_count == PaddedChunkShape::SIZE; } pub fn mesh(&mut self, texture_index_mapper: Arc [u32; 3] + Send + Sync>) {