Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to export files and buffer views #73

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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
78 changes: 72 additions & 6 deletions UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/GLTFSceneExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,19 +248,34 @@ private struct ImageInfo
public bool canBeExportedFromDisk;
}

private struct FileInfo
{
public Stream stream;
public string uniqueFileName;
}

public struct ExportFileResult
{
public string uri;
public string mimeType;
public BufferViewId bufferView;
}

public IReadOnlyList<Transform> RootTransforms => _rootTransforms;

private Transform[] _rootTransforms;
private GLTFRoot _root;
private BufferId _bufferId;
private GLTFBuffer _buffer;
private List<ImageInfo> _imageInfos;
private List<FileInfo> _fileInfos;
private HashSet<string> _fileNames;
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 @@ -511,6 +526,8 @@ public GLTFSceneExporter(Transform[] rootTransforms, ExportOptions options)
};

_imageInfos = new List<ImageInfo>();
_fileInfos = new List<FileInfo>();
_fileNames = new HashSet<string>();
_exportedMaterials = new Dictionary<int, int>();
_textures = new List<UniqueTexture>();
#if ANIMATION_SUPPORTED
Expand Down Expand Up @@ -546,16 +563,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 @@ -566,7 +584,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 @@ -586,7 +604,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 @@ -679,7 +697,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 @@ -746,6 +764,7 @@ public void SaveGLTFandBin(string path, string fileName)
binFile.Close();
#endif
ExportImages(path);
ExportFiles(path);
gltfWriteOutMarker.End();

exportGltfMarker.End();
Expand Down Expand Up @@ -1051,6 +1070,53 @@ private void FilterPrimitives(Transform transform, out GameObject[] primitives,
// && ContainsValidRenderer(gameObject);
// }

public ExportFileResult ExportFile(string fileName, string mimeType, Stream stream) {
if (shouldUseInternalBuffer) {
byte[] data = new byte[stream.Length];
stream.Read(data, 0, (int)stream.Length);
stream.Close();

return new ExportFileResult {
bufferView = this.ExportBufferView(data),
mimeType = mimeType,
};
} else {
var uniqueFileName = GetUniqueName(_fileNames, fileName);

_fileNames.Add(uniqueFileName);

_fileInfos.Add(
new FileInfo {
stream = stream,
uniqueFileName = uniqueFileName,
}
);

return new ExportFileResult {
uri = uniqueFileName,
};
}
}

private void ExportFiles(string outputPath)
{
for (int i = 0; i < _fileInfos.Count; ++i)
{
var fileInfo = _fileInfos[i];

var fileOutputPath = Path.Combine(outputPath, fileInfo.uniqueFileName);

var dir = Path.GetDirectoryName(fileOutputPath);
if (!Directory.Exists(dir) && dir != null)
Directory.CreateDirectory(dir);

var outputStream = File.Create(fileOutputPath);
fileInfo.stream.Seek(0, SeekOrigin.Begin);
fileInfo.stream.CopyTo(outputStream);
outputStream.Close();
}
}

private void ExportAnimation()
{
for (int i = 0; i < _animatedNodes.Count; ++i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,14 @@ private AccessorId ExportAccessor(Color[] arr, bool exportAlphaChannel)
return id;
}

public BufferViewId ExportBufferView(byte[] bytes) {
AlignToBoundary(_bufferWriter.BaseStream, 0x00);
uint byteOffset = CalculateAlignment((uint)_bufferWriter.BaseStream.Position, 4);
_bufferWriter.Write(bytes);
uint byteLength = CalculateAlignment((uint)_bufferWriter.BaseStream.Position - byteOffset, 4);
return ExportBufferView((uint)byteOffset, (uint)byteLength);
}

private BufferViewId ExportBufferView(uint byteOffset, uint byteLength, uint byteStride = 0)
{
var bufferView = new BufferView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public TextureId ExportTexture(Texture textureObj, string textureSlot, TextureEx
texture.Name = textureObj.name;
}

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