Merge pull request #13 from eliasstepanik/codex/verify-chunk-mesh-visibility-logic

Fix empty chunk meshes
This commit is contained in:
Elias Stepanik 2025-06-09 13:53:49 +02:00 committed by GitHub
commit 208da2dffa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 8 deletions

View File

@ -304,7 +304,7 @@ pub(crate) fn mesh_chunk(
origin: Vec3, origin: Vec3,
step: f32, step: f32,
tree: &SparseVoxelOctree, tree: &SparseVoxelOctree,
) -> Mesh { ) -> Option<Mesh> {
// ──────────────────────────────────────────────────────────────────────────── // ────────────────────────────────────────────────────────────────────────────
// Helpers // Helpers
// ──────────────────────────────────────────────────────────────────────────── // ────────────────────────────────────────────────────────────────────────────
@ -462,6 +462,10 @@ pub(crate) fn mesh_chunk(
// ──────────────────────────────────────────────────────────────────────────── // ────────────────────────────────────────────────────────────────────────────
// Final mesh assembly // Final mesh assembly
// ──────────────────────────────────────────────────────────────────────────── // ────────────────────────────────────────────────────────────────────────────
if indices.is_empty() {
return None;
}
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList, RenderAssetUsages::default()); let mut mesh = Mesh::new(PrimitiveTopology::TriangleList, RenderAssetUsages::default());
mesh.insert_attribute( mesh.insert_attribute(
Mesh::ATTRIBUTE_POSITION, Mesh::ATTRIBUTE_POSITION,
@ -476,5 +480,5 @@ pub(crate) fn mesh_chunk(
VertexAttributeValues::Float32x2(uvs), VertexAttributeValues::Float32x2(uvs),
); );
mesh.insert_indices(Indices::U32(indices)); 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 { for (key, buf, origin, step, lod) in bufs {
if let Some((ent, mesh_h, _mat_h, _)) = existing.get(&key).cloned() { if let Some((ent, mesh_h, _mat_h, _)) = existing.get(&key).cloned() {
// update mesh in-place; keeps old asset id // update mesh in-place; keeps old asset id
if let Some(mesh) = meshes.get_mut(&mesh_h) { match mesh_chunk(&buf, origin, step, &tree) {
*mesh = 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 if let Some(mesh) = mesh_chunk(&buf, origin, step, &tree) {
} else { // spawn brand-new chunk only if mesh has faces
// spawn brand-new chunk let mesh_h = meshes.add(mesh);
let mesh_h = meshes.add(mesh_chunk(&buf, origin, step, &tree));
let mat_h = materials.add(StandardMaterial::default()); let mat_h = materials.add(StandardMaterial::default());
commands.entity(root.0).with_children(|p| { commands.entity(root.0).with_children(|p| {