Ver Fonte

Added basic quaternion normalization

Vicente Penades há 6 anos atrás
pai
commit
203d23704d

+ 1 - 1
src/SharpGLTF.DOM/Schema2/gltf.Animations.cs

@@ -332,7 +332,7 @@ namespace SharpGLTF.Schema2
             var keys = new Single[sorted.Count];
             var vals = new TValue[sorted.Count];
 
-            for (int i=0; i < keys.Length; ++i)
+            for (int i = 0; i < keys.Length; ++i)
             {
                 keys[i] = sorted[i].Key;
                 vals[i] = sorted[i].Value;

+ 2 - 2
src/SharpGLTF.DOM/Schema2/gltf.Scene.cs

@@ -69,7 +69,7 @@ namespace SharpGLTF.Schema2
         public Node WithLocalRotation(Quaternion rotation)
         {
             var xform = this.LocalTransform;
-            xform.Rotation = rotation;
+            xform.Rotation = rotation.Sanitized();
             this.LocalTransform = xform;
 
             return this;
@@ -111,7 +111,7 @@ namespace SharpGLTF.Schema2
             {
                 _matrix = null;
                 _scale = value.Scale;
-                _rotation = value.Rotation;
+                _rotation = value.Rotation.Sanitized();
                 _translation = value.Translation;
             }
         }

+ 12 - 0
src/SharpGLTF.DOM/_Extensions.cs

@@ -49,6 +49,18 @@ namespace SharpGLTF
             return new Quaternion(v.X, v.Y, v.Z, v.W);
         }
 
+        internal static Boolean IsNormalized(this Quaternion q)
+        {
+            // As per: https://github.com/KhronosGroup/glTF-Validator/issues/33 , quaternions need to be normalized.
+
+            return Math.Abs(1.0 - q.Length()) > 0.000005;
+        }
+
+        internal static Quaternion Sanitized(this Quaternion q)
+        {
+            return q.IsNormalized() ? q : Quaternion.Normalize(q);
+        }
+
         #endregion
 
         #region base64