Skip to content

Commit 71ca407

Browse files
committed
fix tangent normalization
1 parent c92599f commit 71ca407

File tree

4 files changed

+7
-2
lines changed

4 files changed

+7
-2
lines changed

src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,15 @@ mod tests {
180180
let model = &scene.models[0];
181181
assert!(model.has_normals());
182182
assert!(model.has_tex_coords());
183-
assert!(!model.has_tangents());
183+
assert!(model.has_tangents());
184184
for t in model.triangles().unwrap().iter().flatten() {
185185
let pos = t.position;
186186
assert!(pos.x > -0.01 && pos.x < 1.01);
187187
assert!(pos.y > -0.01 && pos.y < 1.01);
188188
assert!(pos.z > -0.01 && pos.z < 1.01);
189+
190+
// Check that the tangent w component is 1 or -1
191+
assert_eq!(t.tangent.w.abs(), 1.);
189192
}
190193
}
191194

src/scene/model/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@ impl Model {
312312
// Fill tangents
313313
let has_tangents = if let Some(tangents) = reader.read_tangents() {
314314
for (i, tangent) in tangents.enumerate() {
315-
vertices[i].tangent = Self::apply_transform_tangent(tangent, transform).normalize();
315+
let tangent = Self::apply_transform_tangent(tangent, transform);
316+
vertices[i].tangent = tangent.truncate().normalize().extend(tangent.w);
316317
}
317318
true
318319
} else {

src/scene/model/vertex.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct Vertex {
1515
/// Normalized normal
1616
pub normal: Vector3<f32>,
1717
/// Tangent normal
18+
/// The w component is the handedness of the tangent basis (can be -1 or 1)
1819
pub tangent: Vector4<f32>,
1920
/// Texture coordinates
2021
pub tex_coords: Vector2<f32>,

tests/cube.glb

620 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)