diff --git a/src/lib.rs b/src/lib.rs index 6242f39..ec4f940 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -180,12 +180,15 @@ mod tests { let model = &scene.models[0]; assert!(model.has_normals()); assert!(model.has_tex_coords()); - assert!(!model.has_tangents()); + assert!(model.has_tangents()); for t in model.triangles().unwrap().iter().flatten() { let pos = t.position; assert!(pos.x > -0.01 && pos.x < 1.01); assert!(pos.y > -0.01 && pos.y < 1.01); assert!(pos.z > -0.01 && pos.z < 1.01); + + // Check that the tangent w component is 1 or -1 + assert_eq!(t.tangent.w.abs(), 1.); } } diff --git a/src/scene/model/mod.rs b/src/scene/model/mod.rs index b1d1c4b..d59579e 100644 --- a/src/scene/model/mod.rs +++ b/src/scene/model/mod.rs @@ -312,7 +312,8 @@ impl Model { // Fill tangents let has_tangents = if let Some(tangents) = reader.read_tangents() { for (i, tangent) in tangents.enumerate() { - vertices[i].tangent = Self::apply_transform_tangent(tangent, transform).normalize(); + let tangent = Self::apply_transform_tangent(tangent, transform); + vertices[i].tangent = tangent.truncate().normalize().extend(tangent.w); } true } else { diff --git a/src/scene/model/vertex.rs b/src/scene/model/vertex.rs index 1c85a96..9df025d 100644 --- a/src/scene/model/vertex.rs +++ b/src/scene/model/vertex.rs @@ -15,6 +15,7 @@ pub struct Vertex { /// Normalized normal pub normal: Vector3, /// Tangent normal + /// The w component is the handedness of the tangent basis (can be -1 or 1) pub tangent: Vector4, /// Texture coordinates pub tex_coords: Vector2, diff --git a/tests/cube.glb b/tests/cube.glb index 20b9377..01d07f4 100644 Binary files a/tests/cube.glb and b/tests/cube.glb differ