Skip to content

Commit

Permalink
Add ability to export files and buffer views
Browse files Browse the repository at this point in the history
Update to use streams
  • Loading branch information
robertlong authored and hybridherbst committed Dec 13, 2023
1 parent dbd92ee commit 2df4247
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
67 changes: 67 additions & 0 deletions Runtime/Scripts/GLTFSceneExporter.cs
Expand Up @@ -249,13 +249,28 @@ 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;
private bool _shouldUseInternalBufferForImages;
Expand Down Expand Up @@ -509,6 +524,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>();

Expand Down Expand Up @@ -551,6 +568,7 @@ public void SaveGLB(string path, string fileName)
if (!_shouldUseInternalBufferForImages)
{
ExportImages(path);
ExportFiles(path);
}
}

Expand Down Expand Up @@ -754,6 +772,8 @@ public void SaveGLTFandBin(string path, string fileName, bool exportTextures = t

if (exportTextures)
ExportImages(path);

ExportFiles(path);

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

public ExportFileResult ExportFile(string fileName, string mimeType, Stream stream) {
if (_shouldUseInternalBufferForImages) {
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
8 changes: 8 additions & 0 deletions Runtime/Scripts/SceneExporter/ExporterAccessors.cs
Expand Up @@ -1175,6 +1175,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

0 comments on commit 2df4247

Please sign in to comment.