Bladeren bron

Fixed image write settings for GLB files.

Vicente Penades 3 jaren geleden
bovenliggende
commit
05101e1a3a

+ 13 - 2
src/SharpGLTF.Core/Schema2/Serialization.WriteContext.cs

@@ -71,7 +71,7 @@ namespace SharpGLTF.Schema2
             }
 
             var context = Create(_saveFile);
-            context.ImageWriting = ResourceWriteMode.SatelliteFile;
+            context.ImageWriting = ResourceWriteMode.Default;
             context.JsonIndented = true;
             context.CurrentDirectory = dinfo;
             return context;
@@ -95,13 +95,24 @@ namespace SharpGLTF.Schema2
             Guard.IsTrue(stream.CanWrite, nameof(stream));
 
             var context = Create((fn, d) => stream.Write(d.Array, d.Offset, d.Count));
-            context.ImageWriting = ResourceWriteMode.Embedded;
+            context.ImageWriting = ResourceWriteMode.Default;
             context.MergeBuffers = true;
             context.JsonIndented = false;
 
             return context.WithBinarySettings();
         }
 
+        public WriteContext WithTextSettings()
+        {
+            if (ImageWriting == ResourceWriteMode.Default) ImageWriting = ResourceWriteMode.SatelliteFile;
+            if (ImageWriting == ResourceWriteMode.BufferView) ImageWriting = ResourceWriteMode.SatelliteFile;
+
+            MergeBuffers = true;
+            JsonIndented = false;
+
+            return this;
+        }
+
         public WriteContext WithBinarySettings()
         {
             // Binary settings should allow BufferView and SatelliteFile ImageWriting modes:

+ 3 - 6
src/SharpGLTF.Core/Schema2/Serialization.WriteSettings.cs

@@ -199,6 +199,8 @@ namespace SharpGLTF.Schema2
                     .WithSettingsFrom(settings);
             }
 
+            context.WithTextSettings();
+
             var name = Path.GetFileNameWithoutExtension(filePath);
 
             context.WriteTextSchema2(name, this);
@@ -274,12 +276,7 @@ namespace SharpGLTF.Schema2
                 .CreateFromStream(stream)
                 .WithSettingsFrom(settings);
 
-            if (settings != null)
-            {
-                // override settings with required values for GLB writing.
-                context.MergeBuffers = true;
-                context.ImageWriting = ResourceWriteMode.Default;
-            }
+            context.WithBinarySettings();
 
             context.WriteBinarySchema2("model", this);
         }

+ 2 - 2
src/SharpGLTF.Toolkit/IO/Zip.cs

@@ -158,8 +158,8 @@ namespace SharpGLTF.IO
             bool isGltfExtension = filePath.EndsWith(".GLTF", StringComparison.OrdinalIgnoreCase);
 
             var context = WriteContext.Create(_WriteAsset);
-
-            if (!isGltfExtension) context.WithBinarySettings();
+            if (isGltfExtension) context.WithTextSettings();
+            else context.WithBinarySettings();
 
             settings?.CopyTo(context);
 

+ 27 - 0
tests/SharpGLTF.Core.Tests/Schema2/LoadAndSave/RegressionTests.cs

@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+using NUnit.Framework;
+
+using SharpGLTF.Validation;
+
+namespace SharpGLTF.Schema2.LoadAndSave
+{
+    internal class RegressionTests
+    {
+        [Test]
+        public void LoadSuzanneTest()
+        {
+            var path = TestFiles.GetSampleModelsPaths().First(item => item.EndsWith("Suzanne.gltf"));
+
+            var suzanne = ModelRoot.Load(path, ValidationMode.TryFix);
+
+            path = suzanne.AttachToCurrentTest("suzanne.glb");
+
+            Assert.Less(1024*1024, new System.IO.FileInfo(path).Length);
+        }
+    }
+}