Browse Source

clarified embedding flag

Vicente Penades 3 years ago
parent
commit
1bfe321a34

+ 11 - 5
src/SharpGLTF.Core/Schema2/Serialization.WriteContext.cs

@@ -118,7 +118,7 @@ namespace SharpGLTF.Schema2
             // Binary settings should allow BufferView and SatelliteFile ImageWriting modes:
             // Binary settings should allow BufferView and SatelliteFile ImageWriting modes:
 
 
             if (ImageWriting == ResourceWriteMode.Default) ImageWriting = ResourceWriteMode.BufferView;
             if (ImageWriting == ResourceWriteMode.Default) ImageWriting = ResourceWriteMode.BufferView;
-            if (ImageWriting == ResourceWriteMode.Embedded) ImageWriting = ResourceWriteMode.BufferView;
+            if (ImageWriting == ResourceWriteMode.EmbeddedAsBase64) ImageWriting = ResourceWriteMode.BufferView;
 
 
             MergeBuffers = true;
             MergeBuffers = true;
             JsonIndented = false;
             JsonIndented = false;
@@ -205,12 +205,15 @@ namespace SharpGLTF.Schema2
             Guard.NotNullOrEmpty(baseName, nameof(baseName));
             Guard.NotNullOrEmpty(baseName, nameof(baseName));
             Guard.NotNull(model, nameof(model));
             Guard.NotNull(model, nameof(model));
 
 
-            model = this._PreprocessSchema2(model, this.ImageWriting == ResourceWriteMode.BufferView, this.MergeBuffers, this.BuffersMaxSize);
+            // merge images when explicitly requested.
+            var mergeImages = this.ImageWriting == ResourceWriteMode.BufferView;
+
+            model = this._PreprocessSchema2(model, mergeImages, this.MergeBuffers, this.BuffersMaxSize);
             Guard.NotNull(model, nameof(model));
             Guard.NotNull(model, nameof(model));
 
 
             model._PrepareBuffersForSatelliteWriting(this, baseName);
             model._PrepareBuffersForSatelliteWriting(this, baseName);
 
 
-            model._PrepareImagesForWriting(this, baseName, ResourceWriteMode.SatelliteFile);
+            model._PrepareImagesForWriting(this, baseName, false, ResourceWriteMode.SatelliteFile);
 
 
             _ValidateBeforeWriting(model);
             _ValidateBeforeWriting(model);
 
 
@@ -234,7 +237,10 @@ namespace SharpGLTF.Schema2
             Guard.NotNullOrEmpty(baseName, nameof(baseName));
             Guard.NotNullOrEmpty(baseName, nameof(baseName));
             Guard.NotNull(model, nameof(model));
             Guard.NotNull(model, nameof(model));
 
 
-            model = this._PreprocessSchema2(model, this.ImageWriting == ResourceWriteMode.BufferView, true, int.MaxValue);
+            // merge images for all cases except for satellite files
+            var mergeImages = this.ImageWriting != ResourceWriteMode.SatelliteFile;
+
+            model = this._PreprocessSchema2(model, mergeImages, true, int.MaxValue);
             Guard.NotNull(model, nameof(model));
             Guard.NotNull(model, nameof(model));
 
 
             var ex = _BinarySerialization.IsBinaryCompatible(model);
             var ex = _BinarySerialization.IsBinaryCompatible(model);
@@ -242,7 +248,7 @@ namespace SharpGLTF.Schema2
 
 
             model._PrepareBuffersForInternalWriting();
             model._PrepareBuffersForInternalWriting();
 
 
-            model._PrepareImagesForWriting(this, baseName, ResourceWriteMode.Embedded);
+            model._PrepareImagesForWriting(this, baseName, true, ResourceWriteMode.BufferView);
 
 
             _ValidateBeforeWriting(model);
             _ValidateBeforeWriting(model);
 
 

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

@@ -23,9 +23,13 @@ namespace SharpGLTF.Schema2
         SatelliteFile,
         SatelliteFile,
 
 
         /// <summary>
         /// <summary>
-        /// Resources will be embedded into the JSON encoded in MIME64.
+        /// Resources will be embedded into the JSON encoded in Base64.
         /// </summary>
         /// </summary>
-        Embedded,
+        /// <remarks>
+        /// When writing to GLB this does not have any effect.<br/>
+        /// This flag only has effect with images, not with binary blobs.
+        /// </remarks>
+        EmbeddedAsBase64,
 
 
         /// <summary>
         /// <summary>
         /// Resources will be stored as internal binary buffers. Valid only for <see cref="Image"/>
         /// Resources will be stored as internal binary buffers. Valid only for <see cref="Image"/>
@@ -342,10 +346,21 @@ namespace SharpGLTF.Schema2
             }
             }
         }
         }
 
 
-        internal void _PrepareImagesForWriting(WriteContext context, string baseName, ResourceWriteMode rmode)
+        internal void _PrepareImagesForWriting(WriteContext context, string baseName, bool isBinary, ResourceWriteMode rmode)
         {
         {
             if (context.ImageWriting != ResourceWriteMode.Default) rmode = context.ImageWriting;
             if (context.ImageWriting != ResourceWriteMode.Default) rmode = context.ImageWriting;
 
 
+            if (rmode == ResourceWriteMode.Default) throw new InvalidOperationException(nameof(ResourceWriteMode) + " is not set");
+
+            if (isBinary)
+            {
+                if (rmode == ResourceWriteMode.EmbeddedAsBase64) throw new InvalidOperationException(nameof(ResourceWriteMode.EmbeddedAsBase64) + " not supported when writing binary GLB.");
+            }
+            else
+            {
+                if (rmode == ResourceWriteMode.BufferView) throw new InvalidOperationException(nameof(ResourceWriteMode.BufferView) + " not supported when writing text glTF.");
+            }
+
             // setup all images to be written to the appropiate location.
             // setup all images to be written to the appropiate location.
             for (int i = 0; i < this._images.Count; ++i)
             for (int i = 0; i < this._images.Count; ++i)
             {
             {