Browse Source

More valuetuples cleanup
fixed spline animation conversion (untested)

Vicente Penades 6 years ago
parent
commit
b83df0ac02

+ 16 - 16
src/SharpGLTF.Core/Animations/CubicSamplers.cs

@@ -32,13 +32,13 @@ namespace SharpGLTF.Animations
 
         public Vector3 GetPoint(float offset)
         {
-            var segment = SamplerFactory.FindPairContainingOffset(_Sequence, offset);
+            var (valA, valB, amount) = SamplerFactory.FindPairContainingOffset(_Sequence, offset);
 
             return SamplerFactory.InterpolateCubic
                 (
-                segment.A.Item2, segment.A.Item3,   // start, startTangentOut
-                segment.B.Item2, segment.B.Item1,   // end, endTangentIn
-                segment.Amount                               // amount
+                valA.Item2, valA.Item3,   // start, startTangentOut
+                valB.Item2, valB.Item1,   // end, endTangentIn
+                amount                               // amount
                 );
         }
 
@@ -96,13 +96,13 @@ namespace SharpGLTF.Animations
 
         public Quaternion GetPoint(float offset)
         {
-            var segment = SamplerFactory.FindPairContainingOffset(_Sequence, offset);
+            var (valA, valB, amount) = SamplerFactory.FindPairContainingOffset(_Sequence, offset);
 
             return SamplerFactory.InterpolateCubic
                 (
-                segment.A.Item2, segment.A.Item3,   // start, startTangentOut
-                segment.B.Item2, segment.B.Item1,   // end, endTangentIn
-                segment.Amount                               // amount
+                valA.Item2, valA.Item3,   // start, startTangentOut
+                valB.Item2, valB.Item1,   // end, endTangentIn
+                amount                               // amount
                 );
         }
 
@@ -160,13 +160,13 @@ namespace SharpGLTF.Animations
 
         public Transforms.SparseWeight8 GetPoint(float offset)
         {
-            var segment = SamplerFactory.FindPairContainingOffset(_Sequence, offset);
+            var (valA, valB, amount) = SamplerFactory.FindPairContainingOffset(_Sequence, offset);
 
             return Transforms.SparseWeight8.InterpolateCubic
                 (
-                segment.A.Item2, segment.A.Item3,   // start, startTangentOut
-                segment.B.Item2, segment.B.Item1,   // end, endTangentIn
-                segment.Amount                               // amount
+                valA.Item2, valA.Item3,   // start, startTangentOut
+                valB.Item2, valB.Item1,   // end, endTangentIn
+                amount                               // amount
                 );
         }
 
@@ -224,13 +224,13 @@ namespace SharpGLTF.Animations
 
         public float[] GetPoint(float offset)
         {
-            var segment = SamplerFactory.FindPairContainingOffset(_Sequence, offset);
+            var (valA, valB, amount) = SamplerFactory.FindPairContainingOffset(_Sequence, offset);
 
             return SamplerFactory.InterpolateCubic
                 (
-                segment.A.Item2, segment.A.Item3,   // start, startTangentOut
-                segment.B.Item2, segment.B.Item1,   // end, endTangentIn
-                segment.Amount                               // amount
+                valA.Item2, valA.Item3,   // start, startTangentOut
+                valB.Item2, valB.Item1,   // end, endTangentIn
+                amount                               // amount
                 );
         }
 

+ 12 - 12
src/SharpGLTF.Core/Animations/LinearSamplers.cs

@@ -34,11 +34,11 @@ namespace SharpGLTF.Animations
 
         public Vector3 GetPoint(float offset)
         {
-            var segment = SamplerFactory.FindPairContainingOffset(_Sequence, offset);
+            var (valA, valB, amount) = SamplerFactory.FindPairContainingOffset(_Sequence, offset);
 
-            if (!_Linear) return segment.A;
+            if (!_Linear) return valA;
 
-            return Vector3.Lerp(segment.A, segment.B, segment.Amount);
+            return Vector3.Lerp(valA, valB, amount);
         }
 
         public IReadOnlyDictionary<float, Vector3> ToStepCurve()
@@ -100,11 +100,11 @@ namespace SharpGLTF.Animations
 
         public Quaternion GetPoint(float offset)
         {
-            var segment = SamplerFactory.FindPairContainingOffset(_Sequence, offset);
+            var (valA, valB, amount) = SamplerFactory.FindPairContainingOffset(_Sequence, offset);
 
-            if (!_Linear) return segment.A;
+            if (!_Linear) return valA;
 
-            return Quaternion.Slerp(segment.A, segment.B, segment.Amount);
+            return Quaternion.Slerp(valA, valB, amount);
         }
 
         public IReadOnlyDictionary<float, Quaternion> ToStepCurve()
@@ -166,11 +166,11 @@ namespace SharpGLTF.Animations
 
         public Transforms.SparseWeight8 GetPoint(float offset)
         {
-            var segment = SamplerFactory.FindPairContainingOffset(_Sequence, offset);
+            var (valA, valB, amount) = SamplerFactory.FindPairContainingOffset(_Sequence, offset);
 
-            if (!_Linear) return segment.A;
+            if (!_Linear) return valA;
 
-            var weights = Transforms.SparseWeight8.InterpolateLinear(segment.A, segment.B, segment.Amount);
+            var weights = Transforms.SparseWeight8.InterpolateLinear(valA, valB, amount);
 
             return weights;
         }
@@ -233,11 +233,11 @@ namespace SharpGLTF.Animations
 
         public float[] GetPoint(float offset)
         {
-            var segment = SamplerFactory.FindPairContainingOffset(_Sequence, offset);
+            var (valA, valB, amount) = SamplerFactory.FindPairContainingOffset(_Sequence, offset);
 
-            if (!_Linear) return segment.A;
+            if (!_Linear) return valA;
 
-            return SamplerFactory.InterpolateLinear(segment.A, segment.B, segment.Amount);
+            return SamplerFactory.InterpolateLinear(valA, valB, amount);
         }
 
         public IReadOnlyDictionary<float, float[]> ToStepCurve()

+ 3 - 3
src/SharpGLTF.Core/Runtime/SceneTemplate.cs

@@ -264,10 +264,10 @@ namespace SharpGLTF.Runtime
 
             for (int i = 0; i < _JointsNodeIndices.Length; ++i)
             {
-                var jm = skin.GetJoint(i);
+                var (j, ibm) = skin.GetJoint(i);
 
-                _JointsNodeIndices[i] = indexFunc(jm.Joint);
-                _BindMatrices[i] = jm.InverseBindMatrix;
+                _JointsNodeIndices[i] = indexFunc(j);
+                _BindMatrices[i] = ibm;
             }
         }
 

+ 3 - 3
src/SharpGLTF.Core/Schema2/gltf.Skin.cs

@@ -163,10 +163,10 @@ namespace SharpGLTF.Schema2
             for (int i = 0; i < this._joints.Count; ++i)
             {
                 var src = joints[i];
-                var dst = GetJoint(i);
+                var (j, ibm) = GetJoint(i);
 
-                if (!ReferenceEquals(src.Key, dst.Joint)) return false;
-                if (src.Value != dst.InverseBindMatrix) return false;
+                if (!ReferenceEquals(src.Key, j)) return false;
+                if (src.Value != ibm) return false;
             }
 
             return true;

+ 24 - 16
src/SharpGLTF.Toolkit/Animations/CurveBuilder.cs

@@ -102,18 +102,19 @@ namespace SharpGLTF.Animations
 
             offset -= float.Epsilon;
 
-            var offsets = SamplerFactory.FindPairContainingOffset(_Keys.Keys, offset);
+            var (keyA, keyB, _) = SamplerFactory.FindPairContainingOffset(_Keys.Keys, offset);
 
-            var a = _Keys[offsets.A];
-            var b = _Keys[offsets.B];
+            var a = _Keys[keyA];
+            var b = _Keys[keyB];
 
             if (a.Degree == 1) a.OutgoingTangent = GetTangent(a.Point, b.Point);
 
             a.Degree = 3;
+
             b.IncomingTangent = tangent;
 
-            _Keys[offsets.A] = a;
-            _Keys[offsets.B] = b;
+            _Keys[keyA] = a;
+            _Keys[keyB] = b;
         }
 
         /// <summary>
@@ -125,30 +126,31 @@ namespace SharpGLTF.Animations
         {
             Guard.IsTrue(_Keys.ContainsKey(offset), nameof(offset));
 
-            var offsets = SamplerFactory.FindPairContainingOffset(_Keys.Keys, offset);
+            var (keyA, keyB, _) = SamplerFactory.FindPairContainingOffset(_Keys.Keys, offset);
 
-            var a = _Keys[offsets.A];
-            var b = _Keys[offsets.B];
+            var a = _Keys[keyA];
+            var b = _Keys[keyB];
 
-            if (offsets.A != offsets.B)
+            if (keyA != keyB)
             {
                 if (a.Degree == 1) b.IncomingTangent = GetTangent(a.Point, b.Point);
-                _Keys[offsets.B] = b;
+                _Keys[keyB] = b;
             }
 
             a.Degree = 3;
+
             a.OutgoingTangent = tangent;
 
-            _Keys[offsets.A] = a;
+            _Keys[keyA] = a;
         }
 
         private protected (_CurveNode<T> A, _CurveNode<T> B, float Amount) FindSample(float offset)
         {
             if (_Keys.Count == 0) return (default(_CurveNode<T>), default(_CurveNode<T>), 0);
 
-            var offsets = SamplerFactory.FindPairContainingOffset(_Keys.Keys, offset);
+            var (keyA, keyB, amount) = SamplerFactory.FindPairContainingOffset(_Keys.Keys, offset);
 
-            return (_Keys[offsets.A], _Keys[offsets.B], offsets.Amount);
+            return (_Keys[keyA], _Keys[keyB], amount);
         }
 
         public void SetCurve(ICurveSampler<T> curve)
@@ -169,10 +171,16 @@ namespace SharpGLTF.Animations
                     return;
                 }
 
-                if (convertible.MaxDegree > 1)
+                if (convertible.MaxDegree == 3)
                 {
-                    var linear = convertible.ToLinearCurve();
-                    foreach (var p in linear) this.SetPoint(p.Key, p.Value);
+                    var spline = convertible.ToSplineCurve();
+                    foreach (var ppp in spline)
+                    {
+                        this.SetPoint(ppp.Key, ppp.Value.Item2);
+                        this.SetIncomingTangent(ppp.Key, ppp.Value.Item1);
+                        this.SetOutgoingTangent(ppp.Key, ppp.Value.Item3);
+                    }
+
                     return;
                 }
             }

+ 10 - 10
src/SharpGLTF.Toolkit/Scenes/SceneBuilder.Schema2.cs

@@ -218,7 +218,7 @@ namespace SharpGLTF.Scenes
             foreach (var srcArmature in srcScene.VisualChildren)
             {
                 var dstArmature = new NodeBuilder();
-                CopyToNodeBuilder(dstArmature, srcArmature, dstNodes);
+                _CopyToNodeBuilder(dstArmature, srcArmature, dstNodes);
             }
 
             // TODO: we must also process the armatures of every skin, in case the joints are outside the scene.
@@ -266,7 +266,7 @@ namespace SharpGLTF.Scenes
                     var dstNode = dstNodes[srcInstance];
                     var dstInst = dstScene.AddMesh(dstMesh, dstNode);
 
-                    CopyMorphingAnimation(dstInst, srcInstance);
+                    _CopyMorphingAnimation(dstInst, srcInstance);
                 }
                 else
                 {
@@ -274,13 +274,13 @@ namespace SharpGLTF.Scenes
 
                     for (int i = 0; i < joints.Length; ++i)
                     {
-                        var j = srcInstance.Skin.GetJoint(i);
-                        joints[i] = (dstNodes[j.Joint], j.InverseBindMatrix);
+                        var (j, ibm) = srcInstance.Skin.GetJoint(i);
+                        joints[i] = (dstNodes[j], ibm);
                     }
 
                     var dstInst = dstScene.AddSkinnedMesh(dstMesh, joints);
 
-                    CopyMorphingAnimation(dstInst, srcInstance);
+                    _CopyMorphingAnimation(dstInst, srcInstance);
                 }
             }
         }
@@ -323,7 +323,7 @@ namespace SharpGLTF.Scenes
             }
         }
 
-        private static void CopyToNodeBuilder(NodeBuilder dstNode, Node srcNode, IDictionary<Node, NodeBuilder> nodeMapping)
+        private static void _CopyToNodeBuilder(NodeBuilder dstNode, Node srcNode, IDictionary<Node, NodeBuilder> nodeMapping)
         {
             Guard.NotNull(srcNode, nameof(srcNode));
             Guard.NotNull(dstNode, nameof(dstNode));
@@ -331,7 +331,7 @@ namespace SharpGLTF.Scenes
             dstNode.Name = srcNode.Name;
             dstNode.LocalTransform = srcNode.LocalTransform;
 
-            CopyTransformAnimation(dstNode, srcNode);
+            _CopyTransformAnimation(dstNode, srcNode);
 
             if (nodeMapping == null) return;
 
@@ -340,11 +340,11 @@ namespace SharpGLTF.Scenes
             foreach (var srcChild in srcNode.VisualChildren)
             {
                 var dstChild = dstNode.CreateNode();
-                CopyToNodeBuilder(dstChild, srcChild, nodeMapping);
+                _CopyToNodeBuilder(dstChild, srcChild, nodeMapping);
             }
         }
 
-        private static void CopyTransformAnimation(NodeBuilder dstNode, Node srcNode)
+        private static void _CopyTransformAnimation(NodeBuilder dstNode, Node srcNode)
         {
             foreach (var anim in srcNode.LogicalParent.LogicalAnimations)
             {
@@ -362,7 +362,7 @@ namespace SharpGLTF.Scenes
             }
         }
 
-        private static void CopyMorphingAnimation(InstanceBuilder dstInst, Node srcNode)
+        private static void _CopyMorphingAnimation(InstanceBuilder dstInst, Node srcNode)
         {
             foreach (var anim in srcNode.LogicalParent.LogicalAnimations)
             {