DebuggerDisplay.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using SharpGLTF.Schema2;
  6. namespace SharpGLTF.Debug
  7. {
  8. static class DebuggerDisplay
  9. {
  10. internal static string GetAttributeShortName(string attributeName)
  11. {
  12. if (attributeName == "POSITION") return "𝐏";
  13. if (attributeName == "NORMAL") return "𝚴";
  14. if (attributeName == "TANGENT") return "𝚻";
  15. if (attributeName == "COLOR_0") return "𝐂₀";
  16. if (attributeName == "COLOR_1") return "𝐂₁";
  17. if (attributeName == "TEXCOORD_0") return "𝐔𝐕₀";
  18. if (attributeName == "TEXCOORD_1") return "𝐔𝐕₁";
  19. if (attributeName == "JOINTS_0") return "𝐉₀";
  20. if (attributeName == "JOINTS_1") return "𝐉₁";
  21. if (attributeName == "WEIGHTS_0") return "𝐖₀";
  22. if (attributeName == "WEIGHTS_1") return "𝐖₁";
  23. return attributeName;
  24. }
  25. public static String ToReport(this Memory.MemoryAccessInfo minfo)
  26. {
  27. var txt = GetAttributeShortName(minfo.Name);
  28. if (minfo.ByteOffset != 0) txt += $" Offs:{minfo.ByteOffset}ᴮʸᵗᵉˢ";
  29. if (minfo.ByteStride != 0) txt += $" Strd:{minfo.ByteStride}ᴮʸᵗᵉˢ";
  30. txt += $" {minfo.Encoding.ToDebugString(minfo.Dimensions, minfo.Normalized)}[{minfo.ItemsCount}]";
  31. return txt;
  32. }
  33. public static string ToReport(this BufferView bv)
  34. {
  35. var path = string.Empty;
  36. if (bv.DeviceBufferTarget == BufferMode.ARRAY_BUFFER) path += " VertexView";
  37. else if (bv.DeviceBufferTarget == BufferMode.ELEMENT_ARRAY_BUFFER) path += " IndexView";
  38. else path += " BufferView";
  39. var content = bv.Content;
  40. path += $"[{bv.LogicalIndex}ᴵᵈˣ]";
  41. path += $"[{content.Count}ᴮʸᵗᵉˢ]";
  42. if (bv.ByteStride > 0) path += $" Stride:{bv.ByteStride}ᴮʸᵗᵉˢ";
  43. return path;
  44. }
  45. public static string ToReportShort(this Accessor accessor)
  46. {
  47. return $"{accessor.Encoding.ToDebugString(accessor.Dimensions, accessor.Normalized)}[{accessor.Count}ᴵᵗᵉᵐˢ]";
  48. }
  49. public static string ToReportLong(this Accessor accessor)
  50. {
  51. var path = string.Empty;
  52. var bv = accessor.SourceBufferView;
  53. if (bv.DeviceBufferTarget == BufferMode.ARRAY_BUFFER) path += "VertexBuffer";
  54. else if (bv.DeviceBufferTarget == BufferMode.ELEMENT_ARRAY_BUFFER) path += "IndexBuffer";
  55. else path += "BufferView";
  56. path += $"[{bv.LogicalIndex}ᴵᵈˣ] ⇨";
  57. path += $" Accessor[{accessor.LogicalIndex}ᴵᵈˣ] Offset:{accessor.ByteOffset}ᴮʸᵗᵉˢ ⇨";
  58. path += $" {accessor.Encoding.ToDebugString(accessor.Dimensions, accessor.Normalized)}[{accessor.Count}ᴵᵗᵉᵐˢ]";
  59. if (accessor.IsSparse) path += " SPARSE";
  60. return path;
  61. }
  62. public static string ToReport(this MeshPrimitive prim, string txt)
  63. {
  64. // gather vertex attribute information
  65. var vcounts = prim.VertexAccessors.Values
  66. .Select(item => item.Count)
  67. .Distinct();
  68. var vcount = vcounts.First();
  69. if (vcounts.Count() > 1)
  70. {
  71. var vAccessors = prim.VertexAccessors
  72. .OrderBy(item => item.Key, Memory.MemoryAccessInfo.NameComparer)
  73. .Select(item => $"{GetAttributeShortName(item.Key)}={item.Value.ToReportShort()}")
  74. .ToList();
  75. txt += $" Vrts: {String.Join(" ", vAccessors)} ⚠️Vertex Count mismatch⚠️";
  76. }
  77. else
  78. {
  79. string toShort(string name, Accessor accessor)
  80. {
  81. name = GetAttributeShortName(name);
  82. var t = accessor.Encoding.ToDebugString(accessor.Dimensions, accessor.Normalized);
  83. return $"{name}.{t}";
  84. }
  85. var vAccessors = prim.VertexAccessors
  86. .OrderBy(item => item.Key, Memory.MemoryAccessInfo.NameComparer)
  87. .Select(item => toShort(item.Key, item.Value))
  88. .ToList();
  89. txt += $" Vrts: ( {String.Join(" ", vAccessors)} )[{vcount}]";
  90. }
  91. // gather index attribute information
  92. var indices = prim.IndexAccessor?.AsIndicesArray();
  93. var pcount = 0;
  94. switch (prim.DrawPrimitiveType)
  95. {
  96. case PrimitiveType.POINTS:
  97. pcount = vcount;
  98. break;
  99. case PrimitiveType.LINES:
  100. case PrimitiveType.LINE_LOOP:
  101. case PrimitiveType.LINE_STRIP:
  102. pcount = indices.HasValue ? prim.DrawPrimitiveType.GetLinesIndices(indices.Value).Count() : prim.DrawPrimitiveType.GetLinesIndices(vcount).Count();
  103. break;
  104. case PrimitiveType.TRIANGLES:
  105. case PrimitiveType.TRIANGLE_FAN:
  106. case PrimitiveType.TRIANGLE_STRIP:
  107. pcount = indices.HasValue ? prim.DrawPrimitiveType.GetTrianglesIndices(indices.Value).Count() : prim.DrawPrimitiveType.GetTrianglesIndices(vcount).Count();
  108. break;
  109. }
  110. txt += $" {prim.DrawPrimitiveType}[{pcount}]";
  111. // materials
  112. if (prim.Material != null)
  113. {
  114. if (string.IsNullOrWhiteSpace(prim.Material.Name)) txt += $" Material[{prim.Material.LogicalIndex}]";
  115. else txt += $" Material {prim.Material.Name}";
  116. }
  117. return txt;
  118. }
  119. }
  120. }