summaryrefslogtreecommitdiffstats
path: root/modules/gltf
diff options
context:
space:
mode:
authorclayjohn <claynjohn@gmail.com>2024-02-23 13:54:20 -0800
committerclayjohn <claynjohn@gmail.com>2024-02-23 16:25:26 -0800
commit781cd27fe432349c36c5363be4f879b1c3c48c10 (patch)
tree3ff3b1572a2fda97d6874b4c6171f2c9329cc892 /modules/gltf
parent16d61427cab3a8e43f0a9a8ee724fc176b6433c6 (diff)
downloadredot-engine-781cd27fe432349c36c5363be4f879b1c3c48c10.tar.gz
Avoid singularity when generated tangents and validate that tangents are good enough when using compression
Diffstat (limited to 'modules/gltf')
-rw-r--r--modules/gltf/gltf_document.cpp15
1 files changed, 14 insertions, 1 deletions
diff --git a/modules/gltf/gltf_document.cpp b/modules/gltf/gltf_document.cpp
index ff2f290e4f..9aed499751 100644
--- a/modules/gltf/gltf_document.cpp
+++ b/modules/gltf/gltf_document.cpp
@@ -2813,7 +2813,7 @@ Error GLTFDocument::_parse_meshes(Ref<GLTFState> p_state) {
Vector<Vector3> normals = array[Mesh::ARRAY_NORMAL];
for (int k = 0; k < vertex_num; k++) {
- Vector3 tan = Vector3(0.0, 1.0, 0.0).cross(normals[k]);
+ Vector3 tan = Vector3(normals[i].z, -normals[i].x, normals[i].y).cross(normals[k].normalized()).normalized();
tangentsw[k * 4 + 0] = tan.x;
tangentsw[k * 4 + 1] = tan.y;
tangentsw[k * 4 + 2] = tan.z;
@@ -2839,6 +2839,19 @@ Error GLTFDocument::_parse_meshes(Ref<GLTFState> p_state) {
}
array = mesh_surface_tool->commit_to_arrays();
+ if ((flags & RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES) && a.has("NORMAL") && (a.has("TANGENT") || generate_tangents)) {
+ // Compression is enabled, so let's validate that the normals and tangents are correct.
+ Vector<Vector3> normals = array[Mesh::ARRAY_NORMAL];
+ Vector<float> tangents = array[Mesh::ARRAY_TANGENT];
+ for (int vert = 0; vert < normals.size(); vert++) {
+ Vector3 tan = Vector3(tangents[vert * 4 + 0], tangents[vert * 4 + 1], tangents[vert * 4 + 2]);
+ if (abs(tan.dot(normals[vert])) > 0.0001) {
+ // Tangent is not perpendicular to the normal, so we can't use compression.
+ flags &= ~RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES;
+ }
+ }
+ }
+
Array morphs;
//blend shapes
if (p.has("targets")) {