From c03bf8b622551e2571ba477754d5e8f42b414b7d Mon Sep 17 00:00:00 2001 From: Elias Stepanik <40958815+eliasstepanik@users.noreply.github.com> Date: Mon, 9 Jun 2025 13:46:48 +0200 Subject: [PATCH] skip empty chunk meshes --- .../environment/systems/voxels/meshing.rs | 8 +++++-- .../systems/voxels/render_chunks.rs | 21 +++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/client/src/plugins/environment/systems/voxels/meshing.rs b/client/src/plugins/environment/systems/voxels/meshing.rs index bfa853e..c0c3e63 100644 --- a/client/src/plugins/environment/systems/voxels/meshing.rs +++ b/client/src/plugins/environment/systems/voxels/meshing.rs @@ -304,7 +304,7 @@ pub(crate) fn mesh_chunk( origin: Vec3, step: f32, tree: &SparseVoxelOctree, -) -> Mesh { +) -> Option { // ──────────────────────────────────────────────────────────────────────────── // Helpers // ──────────────────────────────────────────────────────────────────────────── @@ -462,6 +462,10 @@ pub(crate) fn mesh_chunk( // ──────────────────────────────────────────────────────────────────────────── // Final mesh assembly // ──────────────────────────────────────────────────────────────────────────── + if indices.is_empty() { + return None; + } + let mut mesh = Mesh::new(PrimitiveTopology::TriangleList, RenderAssetUsages::default()); mesh.insert_attribute( Mesh::ATTRIBUTE_POSITION, @@ -476,5 +480,5 @@ pub(crate) fn mesh_chunk( VertexAttributeValues::Float32x2(uvs), ); mesh.insert_indices(Indices::U32(indices)); - mesh + Some(mesh) } diff --git a/client/src/plugins/environment/systems/voxels/render_chunks.rs b/client/src/plugins/environment/systems/voxels/render_chunks.rs index a8edc65..36d1eac 100644 --- a/client/src/plugins/environment/systems/voxels/render_chunks.rs +++ b/client/src/plugins/environment/systems/voxels/render_chunks.rs @@ -85,13 +85,22 @@ pub fn rebuild_dirty_chunks( for (key, buf, origin, step, lod) in bufs { if let Some((ent, mesh_h, _mat_h, _)) = existing.get(&key).cloned() { // update mesh in-place; keeps old asset id - if let Some(mesh) = meshes.get_mut(&mesh_h) { - *mesh = mesh_chunk(&buf, origin, step, &tree); + match mesh_chunk(&buf, origin, step, &tree) { + Some(new_mesh) => { + if let Some(mesh) = meshes.get_mut(&mesh_h) { + *mesh = new_mesh; + } + spawned.0.insert(key, ent); + } + None => { + meshes.remove(&mesh_h); + commands.entity(ent).despawn_recursive(); + spawned.0.remove(&key); + } } - spawned.0.insert(key, ent); - } else { - // spawn brand-new chunk - let mesh_h = meshes.add(mesh_chunk(&buf, origin, step, &tree)); + } else if let Some(mesh) = mesh_chunk(&buf, origin, step, &tree) { + // spawn brand-new chunk only if mesh has faces + let mesh_h = meshes.add(mesh); let mat_h = materials.add(StandardMaterial::default()); commands.entity(root.0).with_children(|p| {