Sfoglia il codice sorgente

Improving Skin API
more documentation

Vicente Penades 6 anni fa
parent
commit
54b244bc71

+ 50 - 7
src/SharpGLTF.DOM/Schema2/gltf.Scene.cs

@@ -51,7 +51,7 @@ namespace SharpGLTF.Schema2
 
         public IEnumerable<Node> VisualChildren => GetVisualChildren();
 
-        public Boolean IsSkinJoint => Skin.FindSkinsUsing(this).Any();
+        public Boolean IsSkinJoint => Skin.FindSkinsUsingJoint(this).Any();
 
         #endregion
 
@@ -65,12 +65,15 @@ namespace SharpGLTF.Schema2
 
         internal Vector3? RawScale  { get => _scale; set => _scale = value; }
 
+        /// <summary>
+        /// Gets or sets the local transform <see cref="Matrix4x4"/> of this <see cref="Node"/>.
+        /// </summary>
         public Matrix4x4 LocalMatrix
         {
             get => Transforms.AffineTransform.Evaluate(_matrix, _scale, _rotation, _translation);
             set
             {
-                if (value == System.Numerics.Matrix4x4.Identity) _matrix = null;
+                if (value == Matrix4x4.Identity) _matrix = null;
                 else _matrix = value;
 
                 _scale = null;
@@ -79,6 +82,9 @@ namespace SharpGLTF.Schema2
             }
         }
 
+        /// <summary>
+        /// Gets or sets the local Scale, Rotation and Translation of this <see cref="Node"/>.
+        /// </summary>
         public Transforms.AffineTransform LocalTransform
         {
             get => new Transforms.AffineTransform(_matrix, _scale, _rotation, _translation);
@@ -91,6 +97,9 @@ namespace SharpGLTF.Schema2
             }
         }
 
+        /// <summary>
+        /// Gets or sets the world transform <see cref="Matrix4x4"/> of this <see cref="Node"/>.
+        /// </summary>
         public Matrix4x4 WorldMatrix
         {
             get
@@ -109,6 +118,9 @@ namespace SharpGLTF.Schema2
 
         #region properties - content
 
+        /// <summary>
+        /// Gets or sets the <see cref="Mesh"/> of this <see cref="Node"/>.
+        /// </summary>
         public Mesh Mesh
         {
             get => this._mesh.HasValue ? this.LogicalParent.LogicalMeshes[this._mesh.Value] : null;
@@ -120,6 +132,9 @@ namespace SharpGLTF.Schema2
             }
         }
 
+        /// <summary>
+        /// Gets or sets the <see cref="Skin"/> of this <see cref="Node"/>.
+        /// </summary>
         public Skin Skin
         {
             get => this._skin.HasValue ? this.LogicalParent.LogicalSkins[this._skin.Value] : null;
@@ -131,7 +146,18 @@ namespace SharpGLTF.Schema2
             }
         }
 
-        public IReadOnlyList<float> MorphWeights => _weights == null ? Mesh?.MorphWeights : _weights.Select(item => (float)item).ToArray();
+        /// <summary>
+        /// Gets or sets the Morph Weights of this <see cref="Node"/>.
+        /// </summary>
+        public IReadOnlyList<Single> MorphWeights
+        {
+            get => _weights == null ? Mesh?.MorphWeights : _weights.Select(item => (float)item).ToArray();
+            set
+            {
+                _weights.Clear();
+                _weights.AddRange(value.Select(item => (Double)item));
+            }
+        }
 
         public Transforms.BoundingBox3? WorldBounds3 => Transforms.BoundingBox3.Create(this);
 
@@ -177,14 +203,21 @@ namespace SharpGLTF.Schema2
             return node;
         }
 
+        /// <summary>
+        /// Finds a node with the given <paramref name="name"/>
+        /// </summary>
+        /// <param name="name">A name.</param>
+        /// <returns>A <see cref="Node"/> instance.</returns>
         public Node FindNode(string name)
         {
             return this.VisualChildren.FirstOrDefault(item => item.Name == name);
         }
 
-        // TODO: AddVisualChild must return a "NodeBuilder"
-        // public Node AddVisualChild() { return LogicalParent._AddLogicalNode(_children); }
-
+        /// <summary>
+        /// Returns all the nodes of a visual hierarchy as a flattened list.
+        /// </summary>
+        /// <param name="container">A <see cref="Scene"/> instance or a <see cref="Node"/> instance.</param>
+        /// <returns>A collection of <see cref="Node"/> instances.</returns>
         public static IEnumerable<Node> Flatten(IVisualNodeContainer container)
         {
             if (container is Node n) yield return n;
@@ -197,6 +230,11 @@ namespace SharpGLTF.Schema2
             }
         }
 
+        /// <summary>
+        /// Gets a collection of <see cref="Node"/> instances using <paramref name="mesh"/>.
+        /// </summary>
+        /// <param name="mesh">A <see cref="Mesh"/> instance.</param>
+        /// <returns>A collection of <see cref="Node"/> instances.</returns>
         public static IEnumerable<Node> FindNodesUsingMesh(Mesh mesh)
         {
             if (mesh == null) return Enumerable.Empty<Node>();
@@ -208,6 +246,11 @@ namespace SharpGLTF.Schema2
                 .Where(item => item._mesh.AsValue(int.MinValue) == meshIdx);
         }
 
+        /// <summary>
+        /// Gets a collection of <see cref="Node"/> instances using <paramref name="skin"/>.
+        /// </summary>
+        /// <param name="skin">A <see cref="Skin"/> instance.</param>
+        /// <returns>A collection of <see cref="Node"/> instances.</returns>
         public static IEnumerable<Node> FindNodesUsingSkin(Skin skin)
         {
             if (skin == null) return Enumerable.Empty<Node>();
@@ -401,7 +444,7 @@ namespace SharpGLTF.Schema2
         internal Boolean _CheckNodeIsJoint(Node n)
         {
             var idx = n.LogicalIndex;
-            return _skins.Any(s => s._ContainsNode(idx));
+            return _skins.Any(s => s._ContainsJoint(idx));
         }
     }
 }

+ 30 - 11
src/SharpGLTF.DOM/Schema2/gltf.Skin.cs

@@ -37,11 +37,19 @@ namespace SharpGLTF.Schema2
         /// </summary>
         public int LogicalIndex => this.LogicalParent.LogicalSkins.IndexOfReference(this);
 
+        /// <summary>
+        /// Gets a collection of <see cref="Node"/> instances using this <see cref="Skin"/>.
+        /// </summary>
         public IEnumerable<Node> VisualParents => Node.FindNodesUsingSkin(this);
 
+        /// <summary>
+        /// Gets the number of joints
+        /// </summary>
         public int JointsCount => _joints.Count;
 
-        // Skeleton property points to the node that is the root of a joints hierarchy.
+        /// <summary>
+        /// Gets or sets the Skeleton <see cref="Node"/>, which represents the root of a joints hierarchy.
+        /// </summary>
         public Node Skeleton
         {
             get => this._skeleton.HasValue ? this.LogicalParent.LogicalNodes[this._skeleton.Value] : null;
@@ -56,14 +64,19 @@ namespace SharpGLTF.Schema2
 
         #region API
 
-        public static IEnumerable<Skin> FindSkinsUsing(Node n)
+        /// <summary>
+        /// Finds all the skins that are using the given <see cref="Node"/> as a joint.
+        /// </summary>
+        /// <param name="jointNode">A <see cref="Node"/> joint.</param>
+        /// <returns>A collection of <see cref="Skin"/> instances.</returns>
+        public static IEnumerable<Skin> FindSkinsUsingJoint(Node jointNode)
         {
-            var idx = n.LogicalIndex;
+            var idx = jointNode.LogicalIndex;
 
-            return n.LogicalParent.LogicalSkins.Where(s => s._ContainsNode(idx));
+            return jointNode.LogicalParent.LogicalSkins.Where(s => s._ContainsJoint(idx));
         }
 
-        internal bool _ContainsNode(int nodeIdx) { return _joints.Contains(nodeIdx); }
+        internal bool _ContainsJoint(int nodeIdx) { return _joints.Contains(nodeIdx); }
 
         public Accessor GetInverseBindMatricesAccessor()
         {
@@ -102,6 +115,12 @@ namespace SharpGLTF.Schema2
             return exx;
         }
 
+        /// <summary>
+        /// Returns true if this <see cref="Skin"/> matches the input values.
+        /// </summary>
+        /// <param name="skeleton"></param>
+        /// <param name="joints"></param>
+        /// <returns></returns>
         public bool IsMatch(Node skeleton, KeyValuePair<Node, Matrix4x4>[] joints)
         {
             if (!ReferenceEquals(skeleton, this.Skeleton)) return false;
@@ -120,26 +139,26 @@ namespace SharpGLTF.Schema2
             return true;
         }
 
-        /*
         public void BindJoints(KeyValuePair<Node, Matrix4x4>[] joints)
         {
             // inverse bind matrices accessor
 
             var data = new Byte[joints.Length * 16 * 4];
 
-            var indexer = new Runtime.Encoding.Matrix4x4Indexer(data, 16 * 4, 0, Runtime.Encoding.PackedType.F32);
+            var matrices = new Memory.Matrix4x4Array(data.Slice(0), 0, ComponentType.FLOAT, false);
+            Memory.EncodedArrayUtils.FillFrom(matrices, 0, joints.Select(item => item.Value));
 
-            for(int i=0; i < joints.Length; ++i) { indexer[i] = joints[i].Value; }
+            var accessor = LogicalParent
+                .CreateAccessor("Bind Matrices")
+                .WithData( LogicalParent.UseBufferView(data), 0, joints.Length, ElementType.MAT4, ComponentType.FLOAT, false);
 
-            var accessor = LogicalParent._CreateDataAccessor(data, Runtime.Encoding.DimensionType.Matrix4x4, joints.Length);
             this._inverseBindMatrices = accessor.LogicalIndex;
 
             // joints
 
             _joints.Clear();
             _joints.AddRange(joints.Select(item => item.Key.LogicalIndex));
-
-        }*/
+        }
 
         #endregion
     }

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

@@ -66,6 +66,11 @@ namespace SharpGLTF
 
         #region linq
 
+        internal static ArraySegment<T> Slice<T>(this T[] array, int offset)
+        {
+            return new ArraySegment<T>(array, offset, array.Length - offset);
+        }
+
         internal static ArraySegment<T> Slice<T>(this ArraySegment<T> array, int offset)
         {
             return new ArraySegment<T>(array.Array, array.Offset + offset, array.Count - offset);