Skip to content

Commit

Permalink
Fix issue where filled underground chunks would never get meshed, eve…
Browse files Browse the repository at this point in the history
…n if they were modified
  • Loading branch information
splashdust committed Nov 4, 2023
1 parent 3c53dce commit a1e847d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
4 changes: 4 additions & 0 deletions src/voxel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
35 changes: 18 additions & 17 deletions src/voxel_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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::<bevy::render::primitives::Aabb>();

{
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::<bevy::render::primitives::Aabb>();
}
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;
}
}

Expand Down Expand Up @@ -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<Mesh>,
}

Expand All @@ -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;
Expand All @@ -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<dyn Fn(u8) -> [u32; 3] + Send + Sync>) {
Expand Down

0 comments on commit a1e847d

Please sign in to comment.