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
This commit is contained in:
parent
2e2d669406
commit
472bbaae26
@ -690,7 +690,6 @@ impl Mesh {
|
|||||||
.expect("`Mesh::ATTRIBUTE_POSITION` vertex attributes should be of type `float3`");
|
.expect("`Mesh::ATTRIBUTE_POSITION` vertex attributes should be of type `float3`");
|
||||||
|
|
||||||
let mut normals = vec![Vec3::ZERO; positions.len()];
|
let mut normals = vec![Vec3::ZERO; positions.len()];
|
||||||
let mut adjacency_counts = vec![0_usize; positions.len()];
|
|
||||||
|
|
||||||
self.indices()
|
self.indices()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@ -702,17 +701,13 @@ impl Mesh {
|
|||||||
let normal = Vec3::from(face_normal(positions[a], positions[b], positions[c]));
|
let normal = Vec3::from(face_normal(positions[a], positions[b], positions[c]));
|
||||||
[a, b, c].iter().for_each(|pos| {
|
[a, b, c].iter().for_each(|pos| {
|
||||||
normals[*pos] += normal;
|
normals[*pos] += normal;
|
||||||
adjacency_counts[*pos] += 1;
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// average (smooth) normals for shared vertices...
|
// average (smooth) normals for shared vertices...
|
||||||
// TODO: support different methods of weighting the average
|
// TODO: support different methods of weighting the average
|
||||||
for i in 0..normals.len() {
|
for normal in &mut normals {
|
||||||
let count = adjacency_counts[i];
|
*normal = normal.try_normalize().unwrap_or(Vec3::ZERO);
|
||||||
if count > 0 {
|
|
||||||
normals[i] = (normals[i] / (count as f32)).normalize();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.insert_attribute(Mesh::ATTRIBUTE_NORMAL, normals);
|
self.insert_attribute(Mesh::ATTRIBUTE_NORMAL, normals);
|
||||||
|
Loading…
Reference in New Issue
Block a user