Browse Source

Fix to ensure 4th columns of IBM meets gltf specification and validator

Vicente Penades 3 years ago
parent
commit
04b192456b
2 changed files with 16 additions and 1 deletions
  1. 3 0
      src/Shared/_Extensions.cs
  2. 13 1
      src/SharpGLTF.Core/Schema2/gltf.Skin.cs

+ 3 - 0
src/Shared/_Extensions.cs

@@ -216,6 +216,9 @@ namespace SharpGLTF
             if (check.HasFlag(MatrixCheck.Identity) && matrix != Matrix4x4.Identity) return false;
             if (check.HasFlag(MatrixCheck.IdentityColumn4))
             {
+                // glTF validator requires 4th column to be (0,0,0,1)
+                // so it means the glTF standard does not allow rounding errors.
+                // https://github.com/vpenades/SharpGLTF/issues/130#issuecomment-1086306019
                 if (matrix.M14 != 0) return false;
                 if (matrix.M24 != 0) return false;
                 if (matrix.M34 != 0) return false;

+ 13 - 1
src/SharpGLTF.Core/Schema2/gltf.Skin.cs

@@ -120,7 +120,19 @@ namespace SharpGLTF.Schema2
 
             _FindCommonAncestor(joints.Select(item => item.Joint));
 
-            foreach (var j in joints) { Guard.IsTrue(j.InverseBindMatrix._IsFinite(), nameof(joints)); }
+            // fix 4th column to meet gltf schema specification and gltf Validator.
+            for (int i = 0; i < joints.Length; ++i)
+            {
+                var ibm = joints[i].InverseBindMatrix;
+                ibm.M14 = 0;
+                ibm.M24 = 0;
+                ibm.M34 = 0;
+                ibm.M44 = 1;
+
+                Guard.IsTrue(ibm.IsValid(_Extensions.MatrixCheck.InverseBindMatrix), nameof(joints));
+
+                joints[i] = (joints[i].Joint, ibm);
+            }
 
             // inverse bind matrices accessor