From a55e0e31e8b43baaf9a3f41b5fb9fcf1202b4905 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Sat, 18 May 2024 14:07:27 +0200 Subject: [PATCH] fix normals computation for gltf (#13396) # Objective - some gltf files are broken since #13333 ``` thread 'IO Task Pool (2)' panicked at crates/bevy_render/src/mesh/mesh/mod.rs:581:9: `compute_flat_normals` can't work on indexed geometry. Consider calling either `Mesh::compute_smooth_normals` or `Mesh::duplicate_vertices` followed by `Mesh::compute_flat_normals`. ``` - test with example `custom_gltf_vertex_attribute` or `gltf_skinned_mesh` ## Solution - Call the wrapper function for normals that will either call `compute_flat_normals` or `compute_smooth_normals` as appropriate ## Testing - Ran the two examples mentioned above --- crates/bevy_gltf/src/loader.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index 5f3dd5c321..a16b958870 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -502,7 +502,17 @@ async fn load_gltf<'a, 'b, 'c>( bevy_utils::tracing::debug!( "Automatically calculating missing vertex normals for geometry." ); + let vertex_count_before = mesh.count_vertices(); + mesh.duplicate_vertices(); mesh.compute_flat_normals(); + let vertex_count_after = mesh.count_vertices(); + if vertex_count_before != vertex_count_after { + bevy_utils::tracing::debug!("Missing vertex normals in indexed geometry, computing them as flat. Vertex count increased from {} to {}", vertex_count_before, vertex_count_after); + } else { + bevy_utils::tracing::debug!( + "Missing vertex normals in indexed geometry, computing them as flat." + ); + } } if let Some(vertex_attribute) = reader