Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tangent normalization #12

Merged
merged 1 commit into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading