diff --git a/UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/GLTFSceneExporter.cs b/UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/GLTFSceneExporter.cs index df4bd7ada..8cbd5e2c5 100644 --- a/UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/GLTFSceneExporter.cs +++ b/UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/GLTFSceneExporter.cs @@ -47,6 +47,19 @@ public ExportOptions(GLTFSettings settings) public GLTFSceneExporter.AfterTextureExportDelegate AfterTextureExport; } + public class GLTFFileDescriptor { + public string uri; + public string mimeType; + public BufferViewId bufferView; + } + + public class FileInfo { + public string fileName; + public string path; + public string mimeType; + } + + public partial class GLTFSceneExporter { // Available export callbacks. @@ -101,12 +114,13 @@ private struct ImageInfo private BufferId _bufferId; private GLTFBuffer _buffer; private List _imageInfos; + private List _fileInfos; private List _textures; private Dictionary _exportedMaterials; #if ANIMATION_SUPPORTED private List<(Transform tr, AnimationClip clip)> _animationClips; #endif - private bool _shouldUseInternalBufferForImages; + private bool _shouldUseInternalBuffer; private Dictionary _exportedTransforms; private List _animatedNodes; @@ -341,6 +355,7 @@ public GLTFSceneExporter(Transform[] rootTransforms, ExportOptions options) }; _imageInfos = new List(); + _fileInfos = new List(); _exportedMaterials = new Dictionary(); _textures = new List(); #if ANIMATION_SUPPORTED @@ -376,16 +391,17 @@ public void SaveGLB(string path, string fileName) var dirName = Path.GetDirectoryName(fullPath); if (dirName != null && !Directory.Exists(dirName)) Directory.CreateDirectory(dirName); - _shouldUseInternalBufferForImages = true; + _shouldUseInternalBuffer = true; using (FileStream glbFile = new FileStream(fullPath, FileMode.Create)) { SaveGLBToStream(glbFile, fileName); } - if (!_shouldUseInternalBufferForImages) + if (!_shouldUseInternalBuffer) { ExportImages(path); + ExportFiles(path); } } @@ -396,7 +412,7 @@ public void SaveGLB(string path, string fileName) /// public byte[] SaveGLBToByteArray(string sceneName) { - _shouldUseInternalBufferForImages = true; + _shouldUseInternalBuffer = true; using (var stream = new MemoryStream()) { SaveGLBToStream(stream, sceneName); @@ -416,7 +432,7 @@ public void SaveGLBToStream(Stream stream, string sceneName) exportGltfInitMarker.Begin(); Stream binStream = new MemoryStream(); Stream jsonStream = new MemoryStream(); - _shouldUseInternalBufferForImages = true; + _shouldUseInternalBuffer = true; _bufferWriter = new BinaryWriterWithLessAllocations(binStream); @@ -509,7 +525,7 @@ public void SaveGLTFandBin(string path, string fileName) exportGltfMarker.Begin(); exportGltfInitMarker.Begin(); - _shouldUseInternalBufferForImages = false; + _shouldUseInternalBuffer = false; var toLower = fileName.ToLowerInvariant(); if (toLower.EndsWith(".gltf")) fileName = fileName.Substring(0, fileName.Length - 5); @@ -576,6 +592,7 @@ public void SaveGLTFandBin(string path, string fileName) binFile.Close(); #endif ExportImages(path); + ExportFiles(path); gltfWriteOutMarker.End(); exportGltfMarker.End(); @@ -886,6 +903,19 @@ private void ExportAnimation() } } + private void ExportFiles(string outputPath) { + foreach (var fileInfo in _fileInfos) { + var fileOutputPath = Path.Combine(outputPath, fileInfo.fileName); + + var dir = Path.GetDirectoryName(fileOutputPath); + + if (!Directory.Exists(dir)) + Directory.CreateDirectory(dir); + + File.Copy(fileInfo.path, fileOutputPath, true); + } + } + #region Public API #if ANIMATION_SUPPORTED @@ -996,6 +1026,31 @@ public SamplerId GetSamplerId(GLTFRoot root, Texture textureObj) public Texture GetTexture(int id) => _textures[id].Texture; - #endregion + public GLTFFileDescriptor ExportFile(string path, string mimeType) { + var fileDescriptor = new GLTFFileDescriptor(); + var fileName = Path.GetFileName(path); + + if (_shouldUseInternalBuffer) { + byte[] data = File.ReadAllBytes(path); + AlignToBoundary(_bufferWriter.BaseStream, 0x00); + uint byteOffset = CalculateAlignment((uint)_bufferWriter.BaseStream.Position, 4); + _bufferWriter.Write(data); + uint byteLength = CalculateAlignment((uint)_bufferWriter.BaseStream.Position - byteOffset, 4); + fileDescriptor.bufferView = ExportBufferView((uint)byteOffset, (uint)byteLength); + fileDescriptor.mimeType = mimeType; + } else { + fileDescriptor.uri = fileName; + } + + _fileInfos.Add(new FileInfo() { + fileName = fileName, + path = path, + mimeType = mimeType, + }); + + return fileDescriptor; + } + +#endregion } } diff --git a/UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/SceneExporter/ExporterTextures.cs b/UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/SceneExporter/ExporterTextures.cs index e4ab4ba50..443f4bbe1 100644 --- a/UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/SceneExporter/ExporterTextures.cs +++ b/UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/SceneExporter/ExporterTextures.cs @@ -162,7 +162,7 @@ public TextureId ExportTexture(Texture textureObj, TextureMapType textureMapType texture.Name = textureObj.name; } - if (_shouldUseInternalBufferForImages) + if (_shouldUseInternalBuffer) { texture.Source = ExportImageInternalBuffer(uniqueTexture, textureMapType); }