Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 62 additions & 7 deletions UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/GLTFSceneExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -101,12 +114,13 @@ private struct ImageInfo
private BufferId _bufferId;
private GLTFBuffer _buffer;
private List<ImageInfo> _imageInfos;
private List<FileInfo> _fileInfos;
private List<UniqueTexture> _textures;
private Dictionary<int, int> _exportedMaterials;
#if ANIMATION_SUPPORTED
private List<(Transform tr, AnimationClip clip)> _animationClips;
#endif
private bool _shouldUseInternalBufferForImages;
private bool _shouldUseInternalBuffer;
private Dictionary<int, int> _exportedTransforms;
private List<Transform> _animatedNodes;

Expand Down Expand Up @@ -341,6 +355,7 @@ public GLTFSceneExporter(Transform[] rootTransforms, ExportOptions options)
};

_imageInfos = new List<ImageInfo>();
_fileInfos = new List<FileInfo>();
_exportedMaterials = new Dictionary<int, int>();
_textures = new List<UniqueTexture>();
#if ANIMATION_SUPPORTED
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -396,7 +412,7 @@ public void SaveGLB(string path, string fileName)
/// <returns></returns>
public byte[] SaveGLBToByteArray(string sceneName)
{
_shouldUseInternalBufferForImages = true;
_shouldUseInternalBuffer = true;
using (var stream = new MemoryStream())
{
SaveGLBToStream(stream, sceneName);
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -576,6 +592,7 @@ public void SaveGLTFandBin(string path, string fileName)
binFile.Close();
#endif
ExportImages(path);
ExportFiles(path);
gltfWriteOutMarker.End();

exportGltfMarker.End();
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public TextureId ExportTexture(Texture textureObj, TextureMapType textureMapType
texture.Name = textureObj.name;
}

if (_shouldUseInternalBufferForImages)
if (_shouldUseInternalBuffer)
{
texture.Source = ExportImageInternalBuffer(uniqueTexture, textureMapType);
}
Expand Down