Skip to content

Commit

Permalink
more detailed warning when computing normals
Browse files Browse the repository at this point in the history
also panic in compute_flat_normals if indices are set.
That way there won't be no unexpected vertex buffer size increases.
  • Loading branch information
jakobhellermann committed Apr 3, 2021
1 parent c7898b0 commit 5ca2020
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
7 changes: 6 additions & 1 deletion crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,14 @@ async fn load_gltf<'a, 'b>(
};

if mesh.attribute(Mesh::ATTRIBUTE_NORMAL).is_none() {
bevy_log::info!("missing normals, computing them as flat");
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_log::warn!("Missing vertex normals in indexed geometry, computing them as flat. Vertex count increased from {} to {}", vertex_count_before, vertex_count_after);
}
}

let mesh = load_context.set_labeled_asset(&primitive_label, LoadedAsset::new(mesh));
Expand Down
9 changes: 5 additions & 4 deletions crates/bevy_render/src/mesh/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ impl Mesh {

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

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

let positions = self
Expand Down

0 comments on commit 5ca2020

Please sign in to comment.