Skip to content

Commit a83750b

Browse files
more detailed warning when computing normals
also panic in compute_flat_normals if indices are set. That way there won't be no unexpected vertex buffer size increases.
1 parent c7898b0 commit a83750b

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

crates/bevy_gltf/src/loader.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,15 @@ async fn load_gltf<'a, 'b>(
147147
};
148148

149149
if mesh.attribute(Mesh::ATTRIBUTE_NORMAL).is_none() {
150-
bevy_log::info!("missing normals, computing them as flat");
150+
let vertex_count_before = mesh.count_vertices();
151+
151152
mesh.duplicate_vertices();
152153
mesh.compute_flat_normals();
154+
let vertex_count_after = mesh.count_vertices();
155+
156+
if mesh.indices().is_some() {
157+
bevy_log::warn!("Missing vertex normals in indexed geometry, computing them as flat. Vertex count increased from {} to {}", vertex_count_before, vertex_count_after);
158+
}
153159
}
154160

155161
let mesh = load_context.set_labeled_asset(&primitive_label, LoadedAsset::new(mesh));

crates/bevy_render/src/mesh/mesh.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ impl Mesh {
402402

403403
/// Duplicates the vertex attributes so that no vertices are shared.
404404
///
405+
/// This can dramatically increase the vertex count, so make sure this is what you want.
405406
/// Does nothing if no [Indices] are set.
406407
pub fn duplicate_vertices(&mut self) {
407408
fn duplicate<T: Copy>(values: &[T], indices: impl Iterator<Item = usize>) -> Vec<T> {
@@ -438,12 +439,12 @@ impl Mesh {
438439
}
439440

440441
/// Calculates the [`Mesh::ATTRIBUTE_NORMAL`] of a mesh.
441-
/// This [duplicates the vertices](Mesh::duplicate_vertices), so any [`Indices`] will be gone if set.
442+
///
443+
/// Panics if [`Indices`] are set.
444+
/// Consider calling [Mesh::duplicate_vertices] or export your mesh with normal attributes.
442445
pub fn compute_flat_normals(&mut self) {
443446
if self.indices().is_some() {
444-
self.duplicate_vertices();
445-
self.compute_flat_normals();
446-
return;
447+
panic!("`compute_flat_normals` can't work on indexed geometry. Consider calling `Mesh::duplicate_vertices`.");
447448
}
448449

449450
let positions = self

0 commit comments

Comments
 (0)