Просмотр исходного кода

Small code fixes and debug utilities

Vicente Penades 6 лет назад
Родитель
Сommit
0fc5fd9a05

+ 10 - 2
src/SharpGLTF.Core/Debug/DebuggerDisplay.cs

@@ -138,14 +138,22 @@ namespace SharpGLTF.Debug
                     break;
             }
 
-            txt += $" {prim.DrawPrimitiveType}[{pcount}]";
+            var primName = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(prim.DrawPrimitiveType.ToString().ToLower());
+            txt += $" {primName}[{pcount}]";
+
+            // gather morph attributes information
+
+            if (prim.MorphTargetsCount > 0)
+            {
+                txt += $" MorphTargets[{prim.MorphTargetsCount}]";
+            }
 
             // materials
 
             if (prim.Material != null)
             {
                 if (string.IsNullOrWhiteSpace(prim.Material.Name)) txt += $" Material[{prim.Material.LogicalIndex}]";
-                else txt += $" Material {prim.Material.Name}";
+                else txt += "Material " + "\"" + prim.Material.Name + "\"";
             }
 
             return txt;

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

@@ -227,12 +227,30 @@ namespace SharpGLTF.Schema2
 
     partial class ModelRoot
     {
+        /// <summary>
+        /// Writes this <see cref="MODEL"/> to a file in GLTF or GLB based on the extension of <paramref name="filePath"/>.
+        /// </summary>
+        /// <param name="filePath">A valid file path to write to.</param>
+        public void Save(string filePath)
+        {
+            Guard.FilePathMustBeValid(filePath, nameof(filePath));
+
+            bool isGltfExtension = filePath
+                .ToLower(System.Globalization.CultureInfo.InvariantCulture)
+                .EndsWith(".gltf", StringComparison.OrdinalIgnoreCase);
+
+            if (isGltfExtension) SaveGLTF(filePath);
+            else SaveGLB(filePath);
+        }
+
         /// <summary>
         /// Writes this <see cref="MODEL"/> to a file in GLB format.
         /// </summary>
         /// <param name="filePath">A valid file path to write to.</param>
         public void SaveGLB(string filePath)
         {
+            Guard.FilePathMustBeValid(filePath, nameof(filePath));
+
             var settings = WriteSettings.ForBinary(filePath);
 
             var name = Path.GetFileNameWithoutExtension(filePath);
@@ -250,6 +268,8 @@ namespace SharpGLTF.Schema2
         /// </remarks>
         public void SaveGLTF(string filePath, Formatting fmt = Formatting.None)
         {
+            Guard.FilePathMustBeValid(filePath, nameof(filePath));
+
             var settings = WriteSettings.ForText(filePath);
 
             settings.JsonFormatting = fmt;

+ 9 - 9
tests/SharpGLTF.Tests/Geometry/CustomVertices.cs

@@ -7,16 +7,16 @@ using SharpGLTF.Geometry.VertexTypes;
 
 namespace SharpGLTF.Geometry
 {
-    [System.Diagnostics.DebuggerDisplay("𝐂:{Color} 𝐔𝐕:{TexCoord}")]
+    [System.Diagnostics.DebuggerDisplay("𝐂:{Color} 𝐔𝐕:{TexCoord} _CUSTOM_1:{CustomId}")]
     public struct VertexColor1Texture1Custom1 : IVertexMaterial
     {
         #region constructors
 
-        public VertexColor1Texture1Custom1(Vector4 color, Vector2 tex, Single batchId)
+        public VertexColor1Texture1Custom1(Vector4 color, Vector2 tex, Single customId)
         {
             Color = color;
             TexCoord = tex;
-            BatchId = batchId;
+            CustomId = customId;
         }
 
         public VertexColor1Texture1Custom1(IVertexMaterial src)
@@ -26,17 +26,17 @@ namespace SharpGLTF.Geometry
 
             if (src is VertexColor1Texture1Custom1 custom)
             {
-                this.BatchId = custom.BatchId;
+                this.CustomId = custom.CustomId;
             }
             else
             {
-                this.BatchId = 0;
+                this.CustomId = 0;
             }
         }
 
-        public static implicit operator VertexColor1Texture1Custom1((Vector4 color, Vector2 tex, Single batchId) tuple)
+        public static implicit operator VertexColor1Texture1Custom1((Vector4 color, Vector2 tex, Single customId) tuple)
         {
-            return new VertexColor1Texture1Custom1(tuple.color, tuple.tex, tuple.batchId);
+            return new VertexColor1Texture1Custom1(tuple.color, tuple.tex, tuple.customId);
         }
 
         #endregion
@@ -46,7 +46,7 @@ namespace SharpGLTF.Geometry
         public const string CUSTOMATTRIBUTENAME = "_CUSTOM_1";
 
         [VertexAttribute(CUSTOMATTRIBUTENAME, Schema2.EncodingType.FLOAT, false)]
-        public Single BatchId;
+        public Single CustomId;
 
         [VertexAttribute("COLOR_0", Schema2.EncodingType.UNSIGNED_BYTE, true)]
         public Vector4 Color;
@@ -82,7 +82,7 @@ namespace SharpGLTF.Geometry
 
         public object GetCustomAttribute(string attributeName)
         {
-            return attributeName == CUSTOMATTRIBUTENAME ? (Object)BatchId : null;
+            return attributeName == CUSTOMATTRIBUTENAME ? (Object)CustomId : null;
         }
 
         #endregion

+ 73 - 70
tests/SharpGLTF.Tests/Utils.cs

@@ -43,11 +43,81 @@ namespace SharpGLTF
             return path;
         }
 
+        public static void AttachText(this TestContext context, string fileName, string[] lines)
+        {
+            fileName = context.GetAttachmentPath(fileName, true);
+
+            System.IO.File.WriteAllLines(fileName, lines.ToArray());
+
+            TestContext.AddTestAttachment(fileName);
+        }
+
+        public static void AttachShowDirLink(this TestContext context)
+        {
+            context.AttachLink("📂 Show Directory", context.GetAttachmentPath(string.Empty));
+        }
+
+        public static void AttachLink(this TestContext context, string linkPath, string targetPath)
+        {
+            linkPath = context.GetAttachmentPath(linkPath);
+
+            linkPath = ShortcutUtils.CreateLink(linkPath, targetPath);
+
+            TestContext.AddTestAttachment(linkPath);
+        }        
+    }
+
+    static class ShortcutUtils
+    {
+        public static string CreateLink(string localLinkPath, string targetPath)
+        {
+            if (string.IsNullOrWhiteSpace(localLinkPath)) throw new ArgumentNullException(nameof(localLinkPath));
+            if (string.IsNullOrWhiteSpace(targetPath)) throw new ArgumentNullException(nameof(targetPath));
+
+            if (!Uri.TryCreate(targetPath, UriKind.Absolute, out Uri uri)) throw new UriFormatException(nameof(targetPath));
+
+            var sb = new StringBuilder();
+
+            sb.AppendLine("[{000214A0-0000-0000-C000-000000000046}]");
+            sb.AppendLine("Prop3=19,11");
+            sb.AppendLine("[InternetShortcut]");
+            sb.AppendLine("IDList=");
+            sb.AppendLine($"URL={uri.AbsoluteUri}");
+
+            if (uri.IsFile)
+            {
+                sb.AppendLine("IconIndex=1");
+                string icon = targetPath.Replace('\\', '/');
+                sb.AppendLine("IconFile=" + icon);
+            }
+            else
+            {
+                sb.AppendLine("IconIndex=0");
+            }
+
+            localLinkPath = System.IO.Path.ChangeExtension(localLinkPath, ".url");
+
+            System.IO.File.WriteAllText(localLinkPath, sb.ToString());
+
+            return localLinkPath;
+        }
+    }
+
+    static class NUnitGltfUtils
+    {
+        public static void AttachGltfValidatorLinks(this TestContext context)
+        {
+            context.AttachLink("🌍 Khronos Validator", "http://github.khronos.org/glTF-Validator/");
+            context.AttachLink("🌍 BabylonJS Sandbox", "https://sandbox.babylonjs.com/");
+            context.AttachLink("🌍 Don McCurdy Sandbox", "https://gltf-viewer.donmccurdy.com/");
+            context.AttachLink("🌍 VirtualGIS Cesium Sandbox", "https://www.virtualgis.io/gltfviewer/");
+        }
+
         public static void AttachToCurrentTest(this Schema2.ModelRoot model, string fileName)
         {
             // find the output path for the current test
             fileName = TestContext.CurrentContext.GetAttachmentPath(fileName, true);
-            
+
             if (fileName.ToLower().EndsWith(".glb"))
             {
                 model.SaveGLB(fileName);
@@ -79,7 +149,7 @@ namespace SharpGLTF
 
             // find the output path for the current test
             fileName = TestContext.CurrentContext.GetAttachmentPath(fileName, true);
-            
+
             Schema2.Schema2Toolkit.SaveAsWavefront(model, fileName, animation, time);
 
             // Attach the saved file to the current test
@@ -100,37 +170,6 @@ namespace SharpGLTF
 
             gl2model.AttachToCurrentTest(fileName);
         }
-
-        public static void AttachText(this TestContext context, string fileName, string[] lines)
-        {
-            fileName = context.GetAttachmentPath(fileName, true);
-
-            System.IO.File.WriteAllLines(fileName, lines.ToArray());
-
-            TestContext.AddTestAttachment(fileName);
-        }
-
-        public static void AttachShowDirLink(this TestContext context)
-        {
-            context.AttachLink("📂 Show Directory", context.GetAttachmentPath(string.Empty));
-        }
-
-        public static void AttachGltfValidatorLinks(this TestContext context)
-        {
-            context.AttachLink("🌍 Khronos Validator", "http://github.khronos.org/glTF-Validator/");
-            context.AttachLink("🌍 BabylonJS Sandbox", "https://sandbox.babylonjs.com/");
-            context.AttachLink("🌍 Don McCurdy Sandbox", "https://gltf-viewer.donmccurdy.com/");
-            context.AttachLink("🌍 VirtualGIS Cesium Sandbox", "https://www.virtualgis.io/gltfviewer/");
-        }
-
-        public static void AttachLink(this TestContext context, string linkPath, string targetPath)
-        {
-            linkPath = context.GetAttachmentPath(linkPath);
-
-            linkPath = ShortcutUtils.CreateLink(linkPath, targetPath);
-
-            TestContext.AddTestAttachment(linkPath);
-        }        
     }
 
     static class DownloadUtils
@@ -198,43 +237,7 @@ namespace SharpGLTF
                 return localFilePath;
             }
         }
-    }
-
-    static class ShortcutUtils
-    {
-        public static string CreateLink(string localLinkPath, string targetPath)
-        {
-            if (string.IsNullOrWhiteSpace(localLinkPath)) throw new ArgumentNullException(nameof(localLinkPath));
-            if (string.IsNullOrWhiteSpace(targetPath)) throw new ArgumentNullException(nameof(targetPath));
-
-            if (!Uri.TryCreate(targetPath, UriKind.Absolute, out Uri uri)) throw new UriFormatException(nameof(targetPath));
-
-            var sb = new StringBuilder();
-
-            sb.AppendLine("[{000214A0-0000-0000-C000-000000000046}]");
-            sb.AppendLine("Prop3=19,11");
-            sb.AppendLine("[InternetShortcut]");
-            sb.AppendLine("IDList=");
-            sb.AppendLine($"URL={uri.AbsoluteUri}");
-
-            if (uri.IsFile)
-            {                
-                sb.AppendLine("IconIndex=1");
-                string icon = targetPath.Replace('\\', '/');
-                sb.AppendLine("IconFile=" + icon);
-            }
-            else
-            {
-                sb.AppendLine("IconIndex=0");
-            }
-
-            localLinkPath = System.IO.Path.ChangeExtension(localLinkPath, ".url");
-
-            System.IO.File.WriteAllText(localLinkPath, sb.ToString());
-
-            return localLinkPath;
-        }        
-    }
+    }    
 
     static class VectorsUtils
     {