From 472bbaae265c3bc9e8835c2ee4a5b1ddeb6ea15e Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Sun, 20 Oct 2024 19:55:08 +0100 Subject: [PATCH] Unnecessary division in compute_smooth_normals (#16039) # Objective - Less code - Simpler code ## Solution - Remove unnecessary division: `normalize` already does that ## Testing - Test added in #16038 --- crates/bevy_mesh/src/mesh.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/crates/bevy_mesh/src/mesh.rs b/crates/bevy_mesh/src/mesh.rs index fc31c0e90f..9398b08463 100644 --- a/crates/bevy_mesh/src/mesh.rs +++ b/crates/bevy_mesh/src/mesh.rs @@ -690,7 +690,6 @@ impl Mesh { .expect("`Mesh::ATTRIBUTE_POSITION` vertex attributes should be of type `float3`"); let mut normals = vec![Vec3::ZERO; positions.len()]; - let mut adjacency_counts = vec![0_usize; positions.len()]; self.indices() .unwrap() @@ -702,17 +701,13 @@ impl Mesh { let normal = Vec3::from(face_normal(positions[a], positions[b], positions[c])); [a, b, c].iter().for_each(|pos| { normals[*pos] += normal; - adjacency_counts[*pos] += 1; }); }); // average (smooth) normals for shared vertices... // TODO: support different methods of weighting the average - for i in 0..normals.len() { - let count = adjacency_counts[i]; - if count > 0 { - normals[i] = (normals[i] / (count as f32)).normalize(); - } + for normal in &mut normals { + *normal = normal.try_normalize().unwrap_or(Vec3::ZERO); } self.insert_attribute(Mesh::ATTRIBUTE_NORMAL, normals);