From edba54adac6c14d6b6f8199bb87c3dcef85a7989 Mon Sep 17 00:00:00 2001 From: aloucks Date: Fri, 7 Mar 2025 12:39:42 -0500 Subject: [PATCH] Fix mesh tangent attribute matching in mesh transform operations (#17992) Fixes #17170 # Objective Tangents are not currently transformed as described in #17170. I came across this while working on #17989 and it seemed like low hanging fruit. --- crates/bevy_mesh/src/mesh.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/crates/bevy_mesh/src/mesh.rs b/crates/bevy_mesh/src/mesh.rs index 26363d50ed..eec67d6664 100644 --- a/crates/bevy_mesh/src/mesh.rs +++ b/crates/bevy_mesh/src/mesh.rs @@ -909,13 +909,16 @@ impl Mesh { }); } - if let Some(VertexAttributeValues::Float32x3(tangents)) = + if let Some(VertexAttributeValues::Float32x4(tangents)) = self.attribute_mut(Mesh::ATTRIBUTE_TANGENT) { // Transform tangents, taking into account non-uniform scaling and rotation tangents.iter_mut().for_each(|tangent| { + let handedness = tangent[3]; let scaled_tangent = Vec3::from_slice(tangent) * transform.scale; - *tangent = (transform.rotation * scaled_tangent.normalize_or_zero()).to_array(); + *tangent = (transform.rotation * scaled_tangent.normalize_or_zero()) + .extend(handedness) + .to_array(); }); } } @@ -981,12 +984,15 @@ impl Mesh { }); } - if let Some(VertexAttributeValues::Float32x3(tangents)) = + if let Some(VertexAttributeValues::Float32x4(tangents)) = self.attribute_mut(Mesh::ATTRIBUTE_TANGENT) { // Transform tangents tangents.iter_mut().for_each(|tangent| { - *tangent = (rotation * Vec3::from_slice(tangent).normalize_or_zero()).to_array(); + let handedness = tangent[3]; + *tangent = (rotation * Vec3::from_slice(tangent).normalize_or_zero()) + .extend(handedness) + .to_array(); }); } } @@ -1033,13 +1039,17 @@ impl Mesh { }); } - if let Some(VertexAttributeValues::Float32x3(tangents)) = + if let Some(VertexAttributeValues::Float32x4(tangents)) = self.attribute_mut(Mesh::ATTRIBUTE_TANGENT) { // Transform tangents, taking into account non-uniform scaling tangents.iter_mut().for_each(|tangent| { + let handedness = tangent[3]; let scaled_tangent = Vec3::from_slice(tangent) * scale; - *tangent = scaled_tangent.normalize_or_zero().to_array(); + *tangent = scaled_tangent + .normalize_or_zero() + .extend(handedness) + .to_array(); }); } }