Skip to content

Commit cbe1e02

Browse files
authored
Fix glTF coordinate conversion not converting tangents (#20573)
# Objective Fix glTF coordinate conversion not converting tangents. <img width="995" height="565" alt="image" src="https://github.com/user-attachments/assets/20aada7a-39fe-4527-b257-c5efb4555aaf" /> Report: https://discord.com/channels/691052431525675048/692572690833473578/1405362252617355335 Thread: https://discord.com/channels/691052431525675048/1405451520836898848 ## Solution Fixed by removing a redundant copy of the tangent attributes. In #5370 attribute copying was moved to a more generic function (see `convert_attribute`). But one code path was left behind, so the tangents are actually copied twice. This is technically a bug, but a harmless one since both copies are the same. Later on, #19633 added the option to convert attribute coordinates. But only the first tangent copy (in `convert_attribute`) applies the conversion - the second copy will overwrite them with unconverted values. This PR removes the second copy. There's also some minor code quality tweaks - prefer `contains_attribute()` to `attribute().is_some()`, and fixed some bad indentation in log macros. I can revert these if we want to play it safe. ## Testing Tested a modified version of example `load_gltf` with and without feature `gltf_convert_coordinates_default`. Also tested after hacking the loader to use the code path where tangents are recalculated.
1 parent b094540 commit cbe1e02

File tree

1 file changed

+8
-12
lines changed
  • crates/bevy_gltf/src/loader

1 file changed

+8
-12
lines changed

crates/bevy_gltf/src/loader/mod.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use bevy_math::{Mat4, Vec3};
3535
use bevy_mesh::{
3636
morph::{MeshMorphWeights, MorphAttributes, MorphTargetImage, MorphWeights},
3737
skinning::{SkinnedMesh, SkinnedMeshInverseBindposes},
38-
Indices, Mesh, Mesh3d, MeshVertexAttribute, PrimitiveTopology, VertexAttributeValues,
38+
Indices, Mesh, Mesh3d, MeshVertexAttribute, PrimitiveTopology,
3939
};
4040
#[cfg(feature = "pbr_transmission_textures")]
4141
use bevy_pbr::UvChannel;
@@ -771,26 +771,22 @@ impl GltfLoader {
771771
}
772772
}
773773

774-
if let Some(vertex_attribute) = reader
775-
.read_tangents()
776-
.map(|v| VertexAttributeValues::Float32x4(v.collect()))
777-
{
778-
mesh.insert_attribute(Mesh::ATTRIBUTE_TANGENT, vertex_attribute);
779-
} else if mesh.attribute(Mesh::ATTRIBUTE_NORMAL).is_some()
774+
if !mesh.contains_attribute(Mesh::ATTRIBUTE_TANGENT)
775+
&& mesh.contains_attribute(Mesh::ATTRIBUTE_NORMAL)
780776
&& needs_tangents(&primitive.material())
781777
{
782778
tracing::debug!(
783-
"Missing vertex tangents for {}, computing them using the mikktspace algorithm. Consider using a tool such as Blender to pre-compute the tangents.", file_name
784-
);
779+
"Missing vertex tangents for {}, computing them using the mikktspace algorithm. Consider using a tool such as Blender to pre-compute the tangents.", file_name
780+
);
785781

786782
let generate_tangents_span = info_span!("generate_tangents", name = file_name);
787783

788784
generate_tangents_span.in_scope(|| {
789785
if let Err(err) = mesh.generate_tangents() {
790786
warn!(
791-
"Failed to generate vertex tangents using the mikktspace algorithm: {}",
792-
err
793-
);
787+
"Failed to generate vertex tangents using the mikktspace algorithm: {}",
788+
err
789+
);
794790
}
795791
});
796792
}

0 commit comments

Comments
 (0)