Browse Source

better code for normals and tangents generation
+docs

Vicente Penades 4 years ago
parent
commit
b9c9d30de3

+ 1 - 0
src/SharpGLTF.Core/Runtime/AnimationTrackInfo.cs

@@ -4,6 +4,7 @@ using System.Text;
 
 
 namespace SharpGLTF.Runtime
 namespace SharpGLTF.Runtime
 {
 {
+    [System.Diagnostics.DebuggerDisplay("{Name} {Duration}s")]
     public class AnimationTrackInfo
     public class AnimationTrackInfo
     {
     {
         internal AnimationTrackInfo(string name, Object extras, float duration)
         internal AnimationTrackInfo(string name, Object extras, float duration)

+ 28 - 17
src/SharpGLTF.Core/Runtime/ArmatureInstance.cs

@@ -9,6 +9,9 @@ using XFORM = System.Numerics.Matrix4x4;
 
 
 namespace SharpGLTF.Runtime
 namespace SharpGLTF.Runtime
 {
 {
+    /// <summary>
+    /// Represents the transform states of a collection of bones.
+    /// </summary>
     public class ArmatureInstance
     public class ArmatureInstance
     {
     {
         #region constructor
         #region constructor
@@ -36,9 +39,13 @@ namespace SharpGLTF.Runtime
 
 
         #region data
         #region data
 
 
+        [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
         private readonly IReadOnlyList<NodeTemplate> _NodeTemplates;
         private readonly IReadOnlyList<NodeTemplate> _NodeTemplates;
+
+        [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
         private readonly IReadOnlyList<NodeInstance> _NodeInstances;
         private readonly IReadOnlyList<NodeInstance> _NodeInstances;
 
 
+        [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
         private readonly IReadOnlyList<AnimationTrackInfo> _AnimationTracks;
         private readonly IReadOnlyList<AnimationTrackInfo> _AnimationTracks;
 
 
         #endregion
         #endregion
@@ -65,39 +72,43 @@ namespace SharpGLTF.Runtime
         #region API
         #region API
 
 
         /// <summary>
         /// <summary>
-        /// Finds the index of a named animation track.
+        /// Sets the matrix of a bone.
         /// </summary>
         /// </summary>
-        /// <param name="name">The animation name</param>
-        /// <returns>The index of the animation track, or -1 if not found.</returns>
-        public int IndexOfTrack(string name)
-        {
-            for (int i = 0; i < _AnimationTracks.Count; ++i)
-            {
-                if (_AnimationTracks[i].Name == name) return i;
-            }
-
-            return -1;
-        }
-
+        /// <param name="name">The name of the node to be set.</param>
+        /// <param name="localMatrix">A matrix relative to its parent bone.</param>
         public void SetLocalMatrix(string name, XFORM localMatrix)
         public void SetLocalMatrix(string name, XFORM localMatrix)
         {
         {
             var n = LogicalNodes.FirstOrDefault(item => item.Name == name);
             var n = LogicalNodes.FirstOrDefault(item => item.Name == name);
-            if (n == null) return;
+            if (n == null) throw new ArgumentException($"{name} not found", nameof(name));
             n.LocalMatrix = localMatrix;
             n.LocalMatrix = localMatrix;
         }
         }
 
 
-        public void SetModelMatrix(string name, XFORM worldMatrix)
+        /// <summary>
+        /// Sets the matrix of a bone.
+        /// </summary>
+        /// <param name="name">The name of the node to be set.</param>
+        /// <param name="modelMatrix">A matrix relative to the model.</param>
+        public void SetModelMatrix(string name, XFORM modelMatrix)
         {
         {
             var n = LogicalNodes.FirstOrDefault(item => item.Name == name);
             var n = LogicalNodes.FirstOrDefault(item => item.Name == name);
-            if (n == null) return;
-            n.ModelMatrix = worldMatrix;
+            if (n == null) throw new ArgumentException($"{name} not found", nameof(name));
+            n.ModelMatrix = modelMatrix;
         }
         }
 
 
+        /// <summary>
+        /// Resets the bone transforms to their default positions.
+        /// </summary>
         public void SetPoseTransforms()
         public void SetPoseTransforms()
         {
         {
             foreach (var n in _NodeInstances) n.SetPoseTransform();
             foreach (var n in _NodeInstances) n.SetPoseTransform();
         }
         }
 
 
+        /// <summary>
+        /// Sets the bone transforms from an animation frame.
+        /// </summary>
+        /// <param name="trackLogicalIndex">The animation track index.</param>
+        /// <param name="time">The animation time frame.</param>
+        /// <param name="looped">True to use the animation as a looped animation.</param>
         public void SetAnimationFrame(int trackLogicalIndex, float time, bool looped = true)
         public void SetAnimationFrame(int trackLogicalIndex, float time, bool looped = true)
         {
         {
             if (looped)
             if (looped)

+ 3 - 0
src/SharpGLTF.Core/Runtime/ArmatureTemplate.cs

@@ -96,7 +96,10 @@ namespace SharpGLTF.Runtime
 
 
         #region data
         #region data
 
 
+        [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
         private readonly NodeTemplate[] _NodeTemplates;
         private readonly NodeTemplate[] _NodeTemplates;
+
+        [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
         private readonly AnimationTrackInfo[] _AnimationTracks;
         private readonly AnimationTrackInfo[] _AnimationTracks;
 
 
         #endregion
         #endregion

+ 24 - 4
src/SharpGLTF.Core/Runtime/MeshDecoder.Schema2.cs

@@ -62,16 +62,30 @@ namespace SharpGLTF.Runtime
             if (_Primitives.Length == 0) return;
             if (_Primitives.Length == 0) return;
 
 
             var geometries = _Primitives.Select(item => item._Geometry);
             var geometries = _Primitives.Select(item => item._Geometry);
-            VertexNormalsFactory.CalculateSmoothNormals(geometries);
-            VertexTangentsFactory.CalculateTangents(geometries);
+
+            // we can only skip normals and tangents calculation if all the
+            // primitives have them. If we get a case in wich some primitives
+            // have normals and other not, we need to calculate the normals
+            // for all of them because there's vertex adyacencies shared
+            // between primitives.
+
+            var hasNormals = geometries.All(item => item.HasNormals);
+            var hasTangents = geometries.All(item => item.HasTangents);
+
+            if (!hasNormals) VertexNormalsFactory.CalculateSmoothNormals(geometries);
+            if (!hasTangents) VertexTangentsFactory.CalculateTangents(geometries);
 
 
             var morphTargetsCount = _Primitives.Min(item => item.MorphTargetsCount);
             var morphTargetsCount = _Primitives.Min(item => item.MorphTargetsCount);
 
 
             for (int i = 0; i < morphTargetsCount; ++i)
             for (int i = 0; i < morphTargetsCount; ++i)
             {
             {
                 var targets = _Primitives.Select(item => item._MorphTargets[i]);
                 var targets = _Primitives.Select(item => item._MorphTargets[i]);
-                VertexNormalsFactory.CalculateSmoothNormals(targets);
-                VertexTangentsFactory.CalculateTangents(targets);
+
+                hasNormals = targets.All(item => item.HasNormals);
+                hasTangents = targets.All(item => item.HasTangents);
+
+                if (!hasNormals) VertexNormalsFactory.CalculateSmoothNormals(targets);
+                if (!hasTangents) VertexTangentsFactory.CalculateTangents(targets);
             }
             }
         }
         }
 
 
@@ -360,6 +374,9 @@ namespace SharpGLTF.Runtime
 
 
         public int VertexCount => _Positions?.Count ?? 0;
         public int VertexCount => _Positions?.Count ?? 0;
 
 
+        public bool HasNormals => _Normals != null;
+        public bool HasTangents => _Tangents != null;
+
         #endregion
         #endregion
 
 
         #region API
         #region API
@@ -450,6 +467,9 @@ namespace SharpGLTF.Runtime
 
 
         public int VertexCount => _PositionsDeltas?.Count ?? 0;
         public int VertexCount => _PositionsDeltas?.Count ?? 0;
 
 
+        public bool HasNormals => _NormalsDeltas != null;
+        public bool HasTangents => _TangentsDeltas != null;
+
         #endregion
         #endregion
 
 
         #region API
         #region API

+ 2 - 0
src/SharpGLTF.Core/Schema2/gltf.Scene.cs

@@ -17,6 +17,8 @@ namespace SharpGLTF.Schema2
         #endregion
         #endregion
 
 
         #region properties
         #region properties
+
+        [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
         internal IReadOnlyList<int> _VisualChildrenIndices => _nodes;
         internal IReadOnlyList<int> _VisualChildrenIndices => _nodes;
 
 
         public IEnumerable<Node> VisualChildren => _nodes.Select(idx => LogicalParent.LogicalNodes[idx]);
         public IEnumerable<Node> VisualChildren => _nodes.Select(idx => LogicalParent.LogicalNodes[idx]);

+ 3 - 3
src/SharpGLTF.Core/Schema2/gltf.Serialization.Read.cs

@@ -13,15 +13,15 @@ namespace SharpGLTF.Schema2
     using MODEL = ModelRoot;
     using MODEL = ModelRoot;
 
 
     /// <summary>
     /// <summary>
-    /// Callback used to intercept the loading of textures so they can be decoded by the client
-    /// engine and uploaded to the GPU if neccesary.
+    /// Callback used to intercept the loading of textures so they can be
+    /// decoded by the client engine and uploaded to the GPU if neccesary.
     /// </summary>
     /// </summary>
     /// <param name="image">The Image containing the texture</param>
     /// <param name="image">The Image containing the texture</param>
     /// <returns>true if we want to keep the image memory data in Image. Otherwise the memory will be cleared.</returns>
     /// <returns>true if we want to keep the image memory data in Image. Otherwise the memory will be cleared.</returns>
     public delegate Boolean ImageDecodeCallback(Image image);
     public delegate Boolean ImageDecodeCallback(Image image);
 
 
     /// <summary>
     /// <summary>
-    /// Settings to customize how <see cref="MODEL"/> files are read.
+    /// Read settings and base class of <see cref="IO.ReadContext"/>
     /// </summary>
     /// </summary>
     public class ReadSettings
     public class ReadSettings
     {
     {

+ 3 - 0
src/SharpGLTF.Core/Schema2/gltf.Serialization.Write.cs

@@ -37,6 +37,9 @@ namespace SharpGLTF.Schema2
         BufferView
         BufferView
     }
     }
 
 
+    /// <summary>
+    /// Write settings and base class of <see cref="IO.WriteContext"/>
+    /// </summary>
     public class WriteSettings
     public class WriteSettings
     {
     {
         #region lifecycle
         #region lifecycle

+ 0 - 4
src/SharpGLTF.Core/SharpGLTF.Core.csproj

@@ -32,10 +32,6 @@
     <None Include="Schema2\Generated\*.cs">
     <None Include="Schema2\Generated\*.cs">
       <ExcludeFromStyleCop>true</ExcludeFromStyleCop>
       <ExcludeFromStyleCop>true</ExcludeFromStyleCop>
     </None>
     </None>
-  </ItemGroup>
-
-  <ItemGroup>
-    <None Include="..\..\.editorconfig" Link=".editorconfig" />
   </ItemGroup>  
   </ItemGroup>  
 
 
 </Project>
 </Project>