Browse Source

Improved inverse bind matrix numeric precission.

Vicente Penades 6 years ago
parent
commit
82cf4ea6c0

+ 6 - 4
src/SharpGLTF.Core/Transforms/MeshTransforms.cs

@@ -322,13 +322,15 @@ namespace SharpGLTF.Transforms
         /// <param name="meshWorldTransform">The world space <see cref="TRANSFORM"/> of the mesh at the time of binding (POSE).</param>
         /// <param name="meshWorldTransform">The world space <see cref="TRANSFORM"/> of the mesh at the time of binding (POSE).</param>
         /// <param name="jointWorldTransform">The world space <see cref="TRANSFORM"/> of the given bone joint at the time of binding (POSE).</param>
         /// <param name="jointWorldTransform">The world space <see cref="TRANSFORM"/> of the given bone joint at the time of binding (POSE).</param>
         /// <returns>A <see cref="TRANSFORM"/> representing the inverse bind transform.</returns>
         /// <returns>A <see cref="TRANSFORM"/> representing the inverse bind transform.</returns>
-        public static Matrix4x4 CalculateInverseBinding(Matrix4x4 meshWorldTransform, Matrix4x4 jointWorldTransform)
+        public static TRANSFORM CalculateInverseBinding(TRANSFORM meshWorldTransform, TRANSFORM jointWorldTransform)
         {
         {
-            var xform = meshWorldTransform.Inverse();
+            // var xform = meshWorldTransform.Inverse();
+            // xform = jointWorldTransform * xform;
+            // return xform.Inverse();
 
 
-            xform = jointWorldTransform * xform;
+            var invJoint = jointWorldTransform.Inverse();
 
 
-            return xform.Inverse();
+            return meshWorldTransform * invJoint;
         }
         }
 
 
         #endregion
         #endregion

+ 37 - 0
tests/SharpGLTF.Tests/Transforms/InverseBindMatrixTest.cs

@@ -0,0 +1,37 @@
+using System;
+using System.Collections.Generic;
+using System.Numerics;
+using System.Text;
+
+using NUnit.Framework;
+
+namespace SharpGLTF.Transforms
+{
+    [Category("Core.Transforms")]
+    public class InverseBindMatrixTest
+    {
+        [TestCase(0, 0, 0, 0, 0, 0)]
+        [TestCase(1, 2, 4, 3, 2, 1)]
+        [TestCase(-1, 1, 3, 2, 0, 1)]
+        [TestCase(0, 0, 1, 0, 1, 0)]
+        [TestCase(0, -1, 1, -2, 1, 0)]
+        public void CalculateInverseBindMatrix(float mx, float my, float mz, float jx, float jy, float jz)
+        {
+            var model = Matrix4x4.CreateFromYawPitchRoll(mx, my, mz);            
+            var joint = Matrix4x4.CreateFromYawPitchRoll(jx, jy, jz);
+            joint.Translation = new Vector3(jx, jy, jz);
+
+            var invBindMatrix = SkinTransform.CalculateInverseBinding(model, joint);
+
+            Matrix4x4.Invert(model, out Matrix4x4 xform);            
+            Matrix4x4.Invert(joint * xform, out Matrix4x4 result);
+            NumericsAssert.AreEqual(result, invBindMatrix, 0.000001f);
+
+            Matrix4x4.Invert(joint, out Matrix4x4 invJoint);
+            result = model * invJoint;
+            NumericsAssert.AreEqual(result, invBindMatrix, 0.000001f);
+        }
+
+
+    }
+}