skip empty chunk meshes

This commit is contained in:
Elias Stepanik 2025-06-09 13:46:48 +02:00
parent 84b2f8f420
commit c03bf8b622
2 changed files with 21 additions and 8 deletions

View File

@ -304,7 +304,7 @@ pub(crate) fn mesh_chunk(
origin: Vec3,
step: f32,
tree: &SparseVoxelOctree,
) -> Mesh {
) -> Option<Mesh> {
// ────────────────────────────────────────────────────────────────────────────
// 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)
}

View File

@ -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| {