Przeglądaj źródła

fixed node name
more vertex skin constructors

Vicente Penades 6 lat temu
rodzic
commit
6701aecef6

+ 2 - 2
src/SharpGLTF.Core/Schema2/gltf.Node.cs

@@ -9,7 +9,7 @@ namespace SharpGLTF.Schema2
     {
         IEnumerable<Node> VisualChildren { get; }
 
-        Node CreateNode(string name);
+        Node CreateNode(string name = null);
     }
 
     [System.Diagnostics.DebuggerDisplay("Node[{LogicalIndex}] {Name} SkinJoint:{IsSkinJoint} T:{LocalTransform.Translation.X} {LocalTransform.Translation.Y} {LocalTransform.Translation.Z}")]
@@ -169,7 +169,7 @@ namespace SharpGLTF.Schema2
         /// </summary>
         /// <param name="name">The name of the instance.</param>
         /// <returns>A <see cref="Node"/> instance.</returns>
-        public Node CreateNode(string name)
+        public Node CreateNode(string name = null)
         {
             var node = this.LogicalParent._CreateLogicalNode(this._children);
             node.Name = name;

+ 3 - 1
src/SharpGLTF.Core/Schema2/gltf.Scene.cs

@@ -40,7 +40,9 @@ namespace SharpGLTF.Schema2
         /// <returns>A <see cref="Node"/> instance.</returns>
         public Node CreateNode(String name = null)
         {
-            return this.LogicalParent._CreateLogicalNode(this._nodes);
+            var n = this.LogicalParent._CreateLogicalNode(this._nodes);
+            n.Name = name;
+            return n;
         }
 
         internal bool _ContainsVisualNode(Node node, bool recursive)

+ 77 - 6
src/SharpGLTF.Toolkit/Geometry/VertexTypes/VertexSkinning.cs

@@ -11,6 +11,11 @@ namespace SharpGLTF.Geometry.VertexTypes
     [System.Diagnostics.DebuggerDisplay("{Joint} = {Weight}")]
     public struct JointWeightPair : IComparable<JointWeightPair>
     {
+        public static implicit operator JointWeightPair((int, float) jw)
+        {
+            return new JointWeightPair(jw.Item1, jw.Item2);
+        }
+
         public JointWeightPair(int joint, float weight)
         {
             this.Joint = joint;
@@ -103,10 +108,28 @@ namespace SharpGLTF.Geometry.VertexTypes
             Weights = Vector4.UnitX;
         }
 
-        public VertexJoints8x4(int jointIndex1, int jointIndex2)
+        public VertexJoints8x4(JointWeightPair a, JointWeightPair b)
         {
-            Joints = new Vector4(jointIndex1, jointIndex2, 0, 0);
-            Weights = new Vector4(0.5f, 0.5f, 0, 0);
+            Joints = new Vector4(a.Joint, b.Joint, 0, 0);
+            Weights = new Vector4(a.Weight, b.Weight, 0, 0);
+
+            InPlaceSort();
+        }
+
+        public VertexJoints8x4(JointWeightPair a, JointWeightPair b, JointWeightPair c)
+        {
+            Joints = new Vector4(a.Joint, b.Joint, c.Joint, 0);
+            Weights = new Vector4(a.Weight, b.Weight, c.Weight, 0);
+
+            InPlaceSort();
+        }
+
+        public VertexJoints8x4(JointWeightPair a, JointWeightPair b, JointWeightPair c, JointWeightPair d)
+        {
+            Joints = new Vector4(a.Joint, b.Joint, c.Joint, d.Joint);
+            Weights = new Vector4(a.Weight, b.Weight, c.Weight, d.Weight);
+
+            InPlaceSort();
         }
 
         #endregion
@@ -157,6 +180,21 @@ namespace SharpGLTF.Geometry.VertexTypes
             }
         }
 
+        public void InPlaceSort()
+        {
+            Span<JointWeightPair> pairs = stackalloc JointWeightPair[4];
+
+            pairs[0] = new JointWeightPair((int)Joints.X, Weights.X);
+            pairs[1] = new JointWeightPair((int)Joints.Y, Weights.Y);
+            pairs[2] = new JointWeightPair((int)Joints.Z, Weights.Z);
+            pairs[3] = new JointWeightPair((int)Joints.W, Weights.W);
+
+            JointWeightPair.InPlaceReverseBubbleSort(pairs);
+
+            Joints = new Vector4(pairs[0].Joint, pairs[1].Joint, pairs[2].Joint, pairs[3].Joint);
+            Weights = new Vector4(pairs[0].Weight, pairs[1].Weight, pairs[2].Weight, pairs[3].Weight);
+        }
+
         #endregion
     }
 
@@ -173,10 +211,28 @@ namespace SharpGLTF.Geometry.VertexTypes
             Weights = Vector4.UnitX;
         }
 
-        public VertexJoints16x4(int jointIndex1, int jointIndex2)
+        public VertexJoints16x4(JointWeightPair a, JointWeightPair b)
+        {
+            Joints = new Vector4(a.Joint, b.Joint, 0, 0);
+            Weights = new Vector4(a.Weight, b.Weight, 0, 0);
+
+            InPlaceSort();
+        }
+
+        public VertexJoints16x4(JointWeightPair a, JointWeightPair b, JointWeightPair c)
+        {
+            Joints = new Vector4(a.Joint, b.Joint, c.Joint, 0);
+            Weights = new Vector4(a.Weight, b.Weight, c.Weight, 0);
+
+            InPlaceSort();
+        }
+
+        public VertexJoints16x4(JointWeightPair a, JointWeightPair b, JointWeightPair c, JointWeightPair d)
         {
-            Joints = new Vector4(jointIndex1, jointIndex2, 0, 0);
-            Weights = new Vector4(0.5f, 0.5f, 0, 0);
+            Joints = new Vector4(a.Joint, b.Joint, c.Joint, d.Joint);
+            Weights = new Vector4(a.Weight, b.Weight, c.Weight, d.Weight);
+
+            InPlaceSort();
         }
 
         #endregion
@@ -227,6 +283,21 @@ namespace SharpGLTF.Geometry.VertexTypes
             }
         }
 
+        public void InPlaceSort()
+        {
+            Span<JointWeightPair> pairs = stackalloc JointWeightPair[4];
+
+            pairs[0] = new JointWeightPair((int)Joints.X, Weights.X);
+            pairs[1] = new JointWeightPair((int)Joints.Y, Weights.Y);
+            pairs[2] = new JointWeightPair((int)Joints.Z, Weights.Z);
+            pairs[3] = new JointWeightPair((int)Joints.W, Weights.W);
+
+            JointWeightPair.InPlaceReverseBubbleSort(pairs);
+
+            Joints = new Vector4(pairs[0].Joint, pairs[1].Joint, pairs[2].Joint, pairs[3].Joint);
+            Weights = new Vector4(pairs[0].Weight, pairs[1].Weight, pairs[2].Weight, pairs[3].Weight);
+        }
+
         #endregion
     }
 

+ 6 - 0
src/SharpGLTF.Toolkit/Schema2/SceneExtensions.cs

@@ -43,6 +43,12 @@ namespace SharpGLTF.Schema2
             return node;
         }
 
+        public static Node WithSkin(this Node node, Skin skin)
+        {
+            node.Skin = skin;
+            return node;
+        }
+
         #endregion
 
         #region evaluation

+ 4 - 4
tests/SharpGLTF.Tests/Schema2/Authoring/MeshBuilderCreationTests.cs

@@ -165,10 +165,10 @@ namespace SharpGLTF.Schema2.Authoring
             var v3 = (new VPOS(+10, 0, -10), new VSKIN4(0));
             var v4 = (new VPOS(-10, 0, -10), new VSKIN4(0));
 
-            var v5 = (new VPOS(-10, 40, +10), new VSKIN4(0, 1));
-            var v6 = (new VPOS(+10, 40, +10), new VSKIN4(0, 1));
-            var v7 = (new VPOS(+10, 40, -10), new VSKIN4(0, 1));
-            var v8 = (new VPOS(-10, 40, -10), new VSKIN4(0, 1));
+            var v5 = (new VPOS(-10, 40, +10), new VSKIN4((0,0.5f), (1, 0.5f)));
+            var v6 = (new VPOS(+10, 40, +10), new VSKIN4((0, 0.5f), (1, 0.5f)));
+            var v7 = (new VPOS(+10, 40, -10), new VSKIN4((0, 0.5f), (1, 0.5f)));
+            var v8 = (new VPOS(-10, 40, -10), new VSKIN4((0, 0.5f), (1, 0.5f)));
 
             var v9  = (new VPOS(-5, 80, +5), new VSKIN4(2));
             var v10 = (new VPOS(+5, 80, +5), new VSKIN4(2));

+ 5 - 3
tests/SharpGLTF.Tests/Schema2/LoadAndSave/LoadSampleTests.cs

@@ -102,9 +102,11 @@ namespace SharpGLTF.Schema2.LoadAndSave
             _AssertAreEqual(model, model.DeepClone());
 
             // evaluate and save all the triangles to a Wavefront Object
-            model.AttachToCurrentTest(System.IO.Path.ChangeExtension(System.IO.Path.GetFileName(filePath), ".obj"));
-            model.AttachToCurrentTest(System.IO.Path.ChangeExtension(System.IO.Path.GetFileName(filePath), ".glb"));
-            model.AttachToCurrentTest(System.IO.Path.ChangeExtension(System.IO.Path.GetFileName(filePath), ".gltf"));
+
+            filePath = System.IO.Path.GetFileNameWithoutExtension(filePath);
+            model.AttachToCurrentTest(filePath + "_wf.obj");
+            model.AttachToCurrentTest(filePath + ".glb");
+            model.AttachToCurrentTest(filePath + ".gltf");
         }
 
         [Test]