Jelajahi Sumber

improving DebuggerDisplay

Vicente Penades 6 tahun lalu
induk
melakukan
1efce27863

+ 7 - 7
src/Shared/_Extensions.cs

@@ -518,9 +518,14 @@ namespace SharpGLTF
             }
         }
 
-        public static IEnumerable<(int, int)> GetLinesIndices(this PrimitiveType ptype, int count)
+        public static IEnumerable<(int, int)> GetLinesIndices(this PrimitiveType ptype, int vertexCount)
         {
-            return ptype.GetLinesIndices(Enumerable.Range(0, count).Select(item => (UInt32)item));
+            return ptype.GetLinesIndices(Enumerable.Range(0, vertexCount).Select(item => (UInt32)item));
+        }
+
+        public static IEnumerable<(int, int, int)> GetTrianglesIndices(this PrimitiveType ptype, int vertexCount)
+        {
+            return ptype.GetTrianglesIndices(Enumerable.Range(0, vertexCount).Select(item => (UInt32)item));
         }
 
         public static IEnumerable<(int, int)> GetLinesIndices(this PrimitiveType ptype, IEnumerable<UInt32> sourceIndices)
@@ -549,11 +554,6 @@ namespace SharpGLTF
             }
         }
 
-        public static IEnumerable<(int, int, int)> GetTrianglesIndices(this PrimitiveType ptype, int count)
-        {
-            return ptype.GetTrianglesIndices(Enumerable.Range(0, count).Select(item => (UInt32)item));
-        }
-
         public static IEnumerable<(int, int, int)> GetTrianglesIndices(this PrimitiveType ptype, IEnumerable<UInt32> sourceIndices)
         {
             switch (ptype)

+ 154 - 0
src/SharpGLTF.Core/Debug/DebuggerDisplay.cs

@@ -0,0 +1,154 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+using SharpGLTF.Schema2;
+
+namespace SharpGLTF.Debug
+{
+    static class DebuggerDisplay
+    {
+        internal static string GetAttributeShortName(string attributeName)
+        {
+            if (attributeName == "POSITION") return "𝐏";
+            if (attributeName == "NORMAL") return "𝚴";
+            if (attributeName == "TANGENT") return "𝚻";
+            if (attributeName == "COLOR_0") return "𝐂₀";
+            if (attributeName == "COLOR_1") return "𝐂₁";
+            if (attributeName == "TEXCOORD_0") return "𝐔𝐕₀";
+            if (attributeName == "TEXCOORD_1") return "𝐔𝐕₁";
+
+            if (attributeName == "JOINTS_0") return "𝐉₀";
+            if (attributeName == "JOINTS_1") return "𝐉₁";
+
+            if (attributeName == "WEIGHTS_0") return "𝐖₀";
+            if (attributeName == "WEIGHTS_1") return "𝐖₁";
+            return attributeName;
+        }
+
+        public static String ToReport(this Memory.MemoryAccessInfo minfo)
+        {
+            var txt = GetAttributeShortName(minfo.Name);
+            if (minfo.ByteOffset != 0) txt += $" Offs:{minfo.ByteOffset}ᴮʸᵗᵉˢ";
+            if (minfo.ByteStride != 0) txt += $" Strd:{minfo.ByteStride}ᴮʸᵗᵉˢ";
+            txt += $" {minfo.Encoding.ToDebugString(minfo.Dimensions, minfo.Normalized)}[{minfo.ItemsCount}]";
+
+            return txt;
+        }
+
+        public static string ToReport(this BufferView bv)
+        {
+            var path = string.Empty;
+
+            if (bv.DeviceBufferTarget == BufferMode.ARRAY_BUFFER) path += " VertexView";
+            else if (bv.DeviceBufferTarget == BufferMode.ELEMENT_ARRAY_BUFFER) path += " IndexView";
+            else path += " BufferView";
+
+            var content = bv.Content;
+
+            path += $"[{bv.LogicalIndex}ᴵᵈˣ]";
+            path += $"[{content.Count}ᴮʸᵗᵉˢ]";
+
+            if (bv.ByteStride > 0) path += $" Stride:{bv.ByteStride}ᴮʸᵗᵉˢ";
+
+            return path;
+        }
+
+        public static string ToReportShort(this Accessor accessor)
+        {
+            return $"{accessor.Encoding.ToDebugString(accessor.Dimensions, accessor.Normalized)}[{accessor.Count}ᴵᵗᵉᵐˢ]";
+        }
+
+        public static string ToReportLong(this Accessor accessor)
+        {
+            var path = string.Empty;
+
+            var bv = accessor.SourceBufferView;
+
+            if (bv.DeviceBufferTarget == BufferMode.ARRAY_BUFFER) path += "VertexBuffer";
+            else if (bv.DeviceBufferTarget == BufferMode.ELEMENT_ARRAY_BUFFER) path += "IndexBuffer";
+            else path += "BufferView";
+            path += $"[{bv.LogicalIndex}ᴵᵈˣ] ⇨";
+
+            path += $" Accessor[{accessor.LogicalIndex}ᴵᵈˣ] Offset:{accessor.ByteOffset}ᴮʸᵗᵉˢ ⇨";
+
+            path += $" {accessor.Encoding.ToDebugString(accessor.Dimensions, accessor.Normalized)}[{accessor.Count}ᴵᵗᵉᵐˢ]";
+
+            if (accessor.IsSparse) path += " SPARSE";
+
+            return path;
+        }
+
+        public static string ToReport(this MeshPrimitive prim, string txt)
+        {
+            // gather vertex attribute information
+
+            var vcounts = prim.VertexAccessors.Values
+                .Select(item => item.Count)
+                .Distinct();
+
+            var vcount = vcounts.First();
+
+            if (vcounts.Count() > 1)
+            {
+                var vAccessors = prim.VertexAccessors
+                    .OrderBy(item => item.Key, Memory.MemoryAccessInfo.NameComparer)
+                    .Select(item => $"{GetAttributeShortName(item.Key)}={item.Value.ToReportShort()}")
+                    .ToList();
+
+                txt += $" Vrts: {String.Join(" ", vAccessors)} ⚠️Vertex Count mismatch⚠️";
+            }
+            else
+            {
+                string toShort(string name, Accessor accessor)
+                {
+                    name = GetAttributeShortName(name);
+                    var t = accessor.Encoding.ToDebugString(accessor.Dimensions, accessor.Normalized);
+                    return $"{name}.{t}";
+                }
+
+                var vAccessors = prim.VertexAccessors
+                    .OrderBy(item => item.Key, Memory.MemoryAccessInfo.NameComparer)
+                    .Select(item => toShort(item.Key, item.Value))
+                    .ToList();
+
+                txt += $" Vrts: ( {String.Join(" ", vAccessors)} )[{vcount}]";
+            }
+
+            // gather index attribute information
+
+            var indices = prim.IndexAccessor?.AsIndicesArray();
+            var pcount = 0;
+
+            switch (prim.DrawPrimitiveType)
+            {
+                case PrimitiveType.POINTS:
+                    pcount = vcount;
+                    break;
+                case PrimitiveType.LINES:
+                case PrimitiveType.LINE_LOOP:
+                case PrimitiveType.LINE_STRIP:
+                    pcount = indices.HasValue ? prim.DrawPrimitiveType.GetLinesIndices(indices.Value).Count() : prim.DrawPrimitiveType.GetLinesIndices(vcount).Count();
+                    break;
+                case PrimitiveType.TRIANGLES:
+                case PrimitiveType.TRIANGLE_FAN:
+                case PrimitiveType.TRIANGLE_STRIP:
+                    pcount = indices.HasValue ? prim.DrawPrimitiveType.GetTrianglesIndices(indices.Value).Count() : prim.DrawPrimitiveType.GetTrianglesIndices(vcount).Count();
+                    break;
+            }
+
+            txt += $" {prim.DrawPrimitiveType}[{pcount}]";
+
+            // materials
+
+            if (prim.Material != null)
+            {
+                if (string.IsNullOrWhiteSpace(prim.Material.Name)) txt += $" Material[{prim.Material.LogicalIndex}]";
+                else txt += $" Material {prim.Material.Name}";
+            }
+
+            return txt;
+        }
+    }
+}

+ 45 - 25
src/SharpGLTF.Core/Memory/MemoryAccessor.cs

@@ -16,32 +16,9 @@ namespace SharpGLTF.Memory
     {
         #region debug
 
-        internal static string GetAttributeShortName(string attributeName)
+        internal string _GetDebuggerDisplay()
         {
-            if (attributeName == "POSITION") return "𝐏";
-            if (attributeName == "NORMAL") return "𝚴";
-            if (attributeName == "TANGENT") return "𝚻";
-            if (attributeName == "COLOR_0") return "𝐂₀";
-            if (attributeName == "COLOR_1") return "𝐂₁";
-            if (attributeName == "TEXCOORD_0") return "𝐔𝐕₀";
-            if (attributeName == "TEXCOORD_1") return "𝐔𝐕₁";
-
-            if (attributeName == "JOINTS_0") return "𝐉₀";
-            if (attributeName == "JOINTS_1") return "𝐉₁";
-
-            if (attributeName == "WEIGHTS_0") return "𝐖₀";
-            if (attributeName == "WEIGHTS_1") return "𝐖₁";
-            return attributeName;
-        }
-
-        internal String _GetDebuggerDisplay()
-        {
-            var txt = GetAttributeShortName(Name);
-            if (ByteOffset != 0) txt += $" Offs:{ByteOffset}ᴮʸᵗᵉˢ";
-            if (ByteStride != 0) txt += $" Strd:{ByteStride}ᴮʸᵗᵉˢ";
-            txt += $" {Encoding.ToDebugString(Dimensions, Normalized)}[{ItemsCount}]";
-
-            return txt;
+            return Debug.DebuggerDisplay.ToReport(this);
         }
 
         #endregion
@@ -220,6 +197,49 @@ namespace SharpGLTF.Memory
         }
 
         #endregion
+
+        #region types
+
+        private static int _GetSortingScore(string attribute)
+        {
+            switch (attribute)
+            {
+                case "POSITION": return 0;
+                case "NORMAL": return 1;
+                case "TANGENT": return 2;
+
+                case "COLOR_0": return 10;
+                case "COLOR_1": return 11;
+                case "COLOR_2": return 12;
+                case "COLOR_3": return 13;
+
+                case "TEXCOORD_0": return 20;
+                case "TEXCOORD_1": return 21;
+                case "TEXCOORD_2": return 22;
+                case "TEXCOORD_3": return 23;
+
+                case "JOINTS_0": return 50;
+                case "JOINTS_1": return 51;
+                case "WEIGHTS_0": return 50;
+                case "WEIGHTS_1": return 51;
+                default: return 100;
+            }
+        }
+
+        internal static IComparer<string> NameComparer { get; private set; } = new AttributeComparer();
+
+        private class AttributeComparer : IComparer<String>
+        {
+            public int Compare(string x, string y)
+            {
+                var xx = _GetSortingScore(x);
+                var yy = _GetSortingScore(y);
+
+                return xx.CompareTo(yy);
+            }
+        }
+
+        #endregion
     }
 
     /// <summary>

+ 6 - 4
src/SharpGLTF.Core/Runtime/SceneTemplate.cs

@@ -215,7 +215,8 @@ namespace SharpGLTF.Runtime
     {
         #region lifecycle
 
-        internal StaticDrawableReference(Schema2.Node node, Func<Schema2.Node,int> indexFunc) : base(node)
+        internal StaticDrawableReference(Schema2.Node node, Func<Schema2.Node, int> indexFunc)
+            : base(node)
         {
             _NodeIndex = indexFunc(node);
         }
@@ -251,16 +252,17 @@ namespace SharpGLTF.Runtime
     {
         #region lifecycle
 
-        internal SkinnedDrawableReference(Schema2.Node node, Func<Schema2.Node, int> indexFunc) : base(node)
+        internal SkinnedDrawableReference(Schema2.Node node, Func<Schema2.Node, int> indexFunc)
+            : base(node)
         {
             var skin = node.Skin;
 
             _MorphNodeIndex = indexFunc(node);
 
             _JointsNodeIndices = new int[skin.JointsCount];
-            _BindMatrices = new System.Numerics.Matrix4x4[skin.JointsCount];            
+            _BindMatrices = new System.Numerics.Matrix4x4[skin.JointsCount];
 
-            for(int i=0; i < _JointsNodeIndices.Length; ++i)
+            for (int i = 0; i < _JointsNodeIndices.Length; ++i)
             {
                 var jm = skin.GetJoint(i);
 

+ 3 - 23
src/SharpGLTF.Core/Schema2/gltf.Accessors.cs

@@ -8,35 +8,15 @@ namespace SharpGLTF.Schema2
 {
     // https://github.com/KhronosGroup/glTF/issues/827#issuecomment-277537204
 
-    [System.Diagnostics.DebuggerDisplay("{_GetDebuggerDisplayLong(),nq}")]
+    [System.Diagnostics.DebuggerDisplay("{_GetDebuggerDisplay(),nq}")]
     [System.Diagnostics.DebuggerTypeProxy(typeof(Debug._AccessorDebugProxy))]
     public sealed partial class Accessor
     {
         #region debug
 
-        internal string _GetDebuggerDisplayShort()
+        internal string _GetDebuggerDisplay()
         {
-            return $"{Encoding.ToDebugString(Dimensions, Normalized)}[{Count}]";
-        }
-
-        internal string _GetDebuggerDisplayLong()
-        {
-            var path = string.Empty;
-
-            var bv = SourceBufferView;
-
-            if (bv.DeviceBufferTarget == BufferMode.ARRAY_BUFFER) path += "VertexBuffer";
-            else if (bv.DeviceBufferTarget == BufferMode.ELEMENT_ARRAY_BUFFER) path += "IndexBuffer";
-            else path += "BufferView";
-            path += $"[{bv.LogicalIndex}ᴵᵈˣ] ⇨";
-
-            path += $" Accessor[{LogicalIndex}ᴵᵈˣ] Offset:{ByteOffset}ᴮʸᵗᵉˢ ⇨";
-
-            path += $" {Encoding.ToDebugString(Dimensions, Normalized)}[{Count}ᴵᵗᵉᵐˢ]";
-
-            if (IsSparse) path += " SPARSE";
-
-            return path;
+            return Debug.DebuggerDisplay.ToReportLong(this);
         }
 
         #endregion

+ 1 - 24
src/SharpGLTF.Core/Schema2/gltf.BufferView.cs

@@ -14,20 +14,7 @@ namespace SharpGLTF.Schema2
 
         internal string _GetDebuggerDisplay()
         {
-            var path = string.Empty;
-
-            path = $"Buffer[{this._buffer}ᴵᵈˣ] ⇨";
-
-            if (this.DeviceBufferTarget == BufferMode.ARRAY_BUFFER) path += " VertexBuffer";
-            else if (this.DeviceBufferTarget == BufferMode.ELEMENT_ARRAY_BUFFER) path += " IndexBuffer";
-            else path += " BufferView";
-
-            path += $"[{this.LogicalIndex}ᴵᵈˣ]";
-            path += $"[{this._byteLength}ᴮʸᵗᵉˢ]";
-
-            if (ByteStride > 0) path += $" Stride:{ByteStride}ᴮʸᵗᵉˢ";
-
-            return path;
+            return Debug.DebuggerDisplay.ToReport(this);
         }
 
         #endregion
@@ -122,16 +109,6 @@ namespace SharpGLTF.Schema2
             this._byteOffset = targetBuffer.Append(data);
         }
 
-        private string _DebuggerDisplay_TryIdentifyContent()
-        {
-            var accessors = this
-                .FindAccessors()
-                .OrderBy(item => item.ByteOffset)
-                .ToList();
-
-            return String.Join(" ", accessors.Select(item => item._GetDebuggerDisplayShort()));
-        }
-
         /// <summary>
         /// Checks if <paramref name="accessors"/> use this buffer in interleaved arrangement
         /// </summary>

+ 2 - 41
src/SharpGLTF.Core/Schema2/gltf.MeshPrimitive.cs

@@ -13,48 +13,9 @@ namespace SharpGLTF.Schema2
 
         private String _DebuggerDisplay()
         {
-            var txt = $" Primitive[{this.LogicalIndex}]";
+            var txt = $"Primitive[{this.LogicalIndex}]";
 
-            return _DebuggerDisplay(txt);
-        }
-
-        internal string _DebuggerDisplay(string txt)
-        {
-            if (this.Material != null)
-            {
-                if (string.IsNullOrWhiteSpace(this.Material.Name)) txt += $" Material[{this.Material.LogicalIndex}]";
-                else txt += $" Material {this.Material.Name}";
-            }
-
-            var vcount = this.VertexAccessors.Values
-                .Select(item => item.Count)
-                .Distinct();
-
-            if (vcount.Count() > 1)
-            {
-                var vAccessors = this.VertexAccessors
-                .Select(item => $"{Memory.MemoryAccessInfo.GetAttributeShortName(item.Key)}={item.Value._GetDebuggerDisplayShort()}")
-                .ToList();
-
-                txt += $" Vrts: {String.Join(" ", vAccessors)} ⚠️Vertex Count mismatch⚠️";
-            }
-            else
-            {
-                string toShort(string name, Accessor accessor)
-                {
-                    name = Memory.MemoryAccessInfo.GetAttributeShortName(name);
-                    var t = accessor.Encoding.ToDebugString(accessor.Dimensions, accessor.Normalized);
-                    return $"{name}.{t}";
-                }
-
-                var vAccessors = this.VertexAccessors
-                    .Select(item => toShort(item.Key, item.Value))
-                    .ToList();
-
-                txt += $" Vrts: ( {String.Join(" ", vAccessors)} )[{vcount.First()}]";
-            }
-
-            return txt;
+            return Debug.DebuggerDisplay.ToReport(this, txt);
         }
 
         #endregion