Skip to content

Commit f57f3bd

Browse files
committed
Ensure the normal is is normalized in Gram-Schmidt tangent computation
Also added a note about why the accumulated tangent is normalized prior to applying Gram-Schmidt orthogonalization. Normalizing the accumulated tangent may not be mathematically necessary, but it may prevent numerical stability issues in some cases.
1 parent 39d4a08 commit f57f3bd

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

crates/bevy_mesh/src/gramschmidt.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ pub(crate) fn generate_tangents_for_mesh(
105105

106106
for i in 0..vertex_count {
107107
let normal = Vec3A::from_array(normals[i]);
108+
// Assume that the normal has not been pre-normalized
109+
let normal = normal.normalize_or_zero();
110+
// Although it is not mathematically necessary to normalize the accumulated
111+
// tangent, doing so may provide better numerical stability in extreme cases.
108112
let tangent = tangents[i].normalize();
109113
// Gram-Schmidt orthogonalization
110114
let tangent = (tangent - normal * normal.dot(tangent)).normalize();

0 commit comments

Comments
 (0)