Skip to content

Commit

Permalink
fix tangent normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
flomonster committed Nov 4, 2023
1 parent c92599f commit c14215d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/scene/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/scene/model/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Vertex {
/// Normalized normal
pub normal: Vector3<f32>,
/// Tangent normal
/// The w component is the handedness of the tangent basis (can be -1 or 1)
pub tangent: Vector4<f32>,
/// Texture coordinates
pub tex_coords: Vector2<f32>,
Expand Down
Binary file modified tests/cube.glb
Binary file not shown.

0 comments on commit c14215d

Please sign in to comment.