using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
namespace Brutal.GltfApi;
///
/// Helper functions for loading and parsing glTF models
///
public static class GltfUtility
{
const uint GLTFHEADER = 0x46546C67;
const uint GLTFVERSION2 = 2;
const uint CHUNKJSON = 0x4E4F534A;
const uint CHUNKBIN = 0x004E4942;
static string EmbeddedBase64 => ";base64,";
static readonly string EmbeddedImagePrefix = "data:image/";
static readonly string EmbeddedOctetStream = "data:application/octet-stream;base64,";
static readonly string EmbeddedGltfBuffer = "data:application/gltf-buffer;base64,";
// Core glTF 2.0 images are PNG or JPEG; KTX2 is sanctioned via the KHR_texture_basisu extension.
// Formats with no sanctioned glTF path (gif, bmp, dds, ...) are deliberately not supported.
static readonly string[] EmbeddedImageExtensions = ["png", "jpg", "jpeg", "ktx2"];
private static readonly string[] EmbeddedImageExtensionPrefixes = Array.ConvertAll(
EmbeddedImageExtensions,
ext => $"{EmbeddedImagePrefix}{ext}{EmbeddedBase64}"
);
private static readonly Encoding DefaultStreamWriterEncoding = new UTF8Encoding(
encoderShouldEmitUTF8Identifier: false,
throwOnInvalidBytes: true
);
private const int DefaultStreamWriterBufferSize = 1024;
///
/// Loads a Schema.Gltf model from a file
///
/// Source file path to a gltf/glb model
/// Schema.Gltf model
public static Gltf LoadModel(string filePath)
{
using (Stream stream = File.OpenRead(filePath))
{
return LoadModel(stream);
}
}
///
/// Reads a Schema.Gltf model from a stream
///
/// Readable stream to a gltf/glb model
/// Schema.Gltf model
public static Gltf LoadModel(Stream stream)
{
var binaryFile = false;
uint magic = 0;
magic |= (uint)stream.ReadByte();
magic |= (uint)stream.ReadByte() << 8;
magic |= (uint)stream.ReadByte() << 16;
magic |= (uint)stream.ReadByte() << 24;
if (magic == GLTFHEADER)
binaryFile = true;
stream.Position = 0; // restart read position
string fileData;
if (binaryFile)
{
fileData = ParseBinary(stream);
}
else
{
fileData = ParseText(stream);
}
return DeserializeModelJson(fileData);
}
private static string ParseText(Stream stream)
{
using (var streamReader = new StreamReader(stream))
{
return streamReader.ReadToEnd();
}
}
private static string ParseBinary(Stream stream)
{
using (var binaryReader = new BinaryReader(stream))
{
ReadBinaryHeader(binaryReader);
var data = ReadBinaryChunk(binaryReader, CHUNKJSON);
return Encoding.UTF8.GetString(data);
}
}
private static byte[] ReadBinaryChunk(BinaryReader binaryReader, uint format)
{
var stream = binaryReader.BaseStream;
while (stream.Length - stream.Position >= 8) // chunk header is length (4) + format (4)
{
var chunkLength = binaryReader.ReadUInt32();
if ((chunkLength & 3) != 0)
{
throw new InvalidDataException($"The chunk must be padded to 4 bytes: {chunkLength}");
}
if (chunkLength > stream.Length - stream.Position - 4)
{
throw new InvalidDataException($"Chunk length {chunkLength} exceeds the remaining size of the file");
}
var chunkFormat = binaryReader.ReadUInt32();
var data = binaryReader.ReadBytes((int)chunkLength);
if (chunkFormat == format)
return data;
}
throw new InvalidDataException($"GLB chunk 0x{format:X8} not found");
}
private static void ReadBinaryHeader(BinaryReader binaryReader)
{
var magic = binaryReader.ReadUInt32();
if (magic != GLTFHEADER)
{
throw new InvalidDataException($"Unexpected magic number: {magic}");
}
var version = binaryReader.ReadUInt32();
if (version != GLTFVERSION2)
{
throw new InvalidDataException($"Unknown version number: {version}");
}
var length = binaryReader.ReadUInt32();
var fileLength = binaryReader.BaseStream.Length;
if (length != fileLength)
{
throw new InvalidDataException(
$"The specified length of the file ({length}) is not equal to the actual length of the file ({fileLength})."
);
}
}
[UnconditionalSuppressMessage(
"Trimming",
"IL2026",
Justification = "Reflection is only used when JsonSerializer.IsReflectionEnabledByDefault, which is false under trimming and Native AOT."
)]
[UnconditionalSuppressMessage(
"AOT",
"IL3050",
Justification = "Reflection is only used when JsonSerializer.IsReflectionEnabledByDefault, which is false under trimming and Native AOT."
)]
private static JsonSerializerOptions CreateSerializerOptions()
{
// Settings, converters and metadata come from the source-generated GltfJsonContext, which is all
// that is available under trimming and Native AOT. When reflection is enabled, fall back to it for
// types the context doesn't know, such as custom extension classes passed to GetExtension or
// assigned to an Extensions map.
var options = new JsonSerializerOptions(GltfJsonContext.Default.Options);
if (JsonSerializer.IsReflectionEnabledByDefault)
{
options.TypeInfoResolver = JsonTypeInfoResolver.Combine(
GltfJsonContext.Default,
new DefaultJsonTypeInfoResolver()
);
}
return options;
}
internal static readonly JsonSerializerOptions JsonOptions = CreateSerializerOptions();
private static readonly JsonTypeInfo GltfTypeInfo = (JsonTypeInfo)JsonOptions.GetTypeInfo(typeof(Gltf));
///
/// Parses a JSON formatted text content
///
/// JSON text content
/// Schema.Gltf model
public static Gltf DeserializeModelJson(string fileData)
{
return JsonSerializer.Deserialize(fileData, GltfTypeInfo) ?? throw new NullReferenceException();
}
///
/// Serializes a Schema.Gltf model to text
///
/// Schema.Gltf model
/// JSON formatted text
public static string SerializeModelJson(this Gltf model)
{
return JsonSerializer.Serialize(model, GltfTypeInfo);
}
private static byte[]? TryLoadBase64BinaryBufferUnchecked(Buffer buffer, string prefix)
{
return buffer.Uri == null || !buffer.Uri.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)
? null
: Convert.FromBase64String(buffer.Uri.Substring(prefix.Length));
}
private static byte[] LoadBinaryBufferUnchecked(Buffer buffer, Func externalReferenceSolver)
{
return TryLoadBase64BinaryBufferUnchecked(buffer, EmbeddedGltfBuffer)
?? TryLoadBase64BinaryBufferUnchecked(buffer, EmbeddedOctetStream)
?? externalReferenceSolver(buffer.Uri);
}
private static byte[] LoadExternalFileReference(string baseFilePath, string uri)
{
// Load embedded glb
if (string.IsNullOrEmpty(uri))
{
using (Stream stream = File.OpenRead(baseFilePath))
using (BinaryReader binaryReader = new BinaryReader(stream))
{
ReadBinaryHeader(binaryReader);
return ReadBinaryChunk(binaryReader, CHUNKBIN);
}
}
// Load external reference
return File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(baseFilePath) ?? "", uri));
}
private static byte[] LoadBinaryBufferUnchecked(Buffer buffer, string baseFilePath)
{
return TryLoadBase64BinaryBufferUnchecked(buffer, EmbeddedGltfBuffer)
?? TryLoadBase64BinaryBufferUnchecked(buffer, EmbeddedOctetStream)
?? LoadExternalFileReference(baseFilePath, buffer.Uri);
}
///
/// Attempts to parse and load an embedded image from a glTF model.
///
/// Image with embedded data
/// Extension format of embedded image file
/// Byte array of image data
/// Invalid image format
public static byte[] OpenEmbeddedImage(this Image image, out string? extension)
{
string? s = null;
extension = null;
for (var i = 0; i < EmbeddedImageExtensionPrefixes.Length; ++i)
{
if (image.Uri.StartsWith(EmbeddedImageExtensionPrefixes[i], StringComparison.OrdinalIgnoreCase))
{
s = image.Uri.Substring(EmbeddedImageExtensionPrefixes[i].Length);
extension = EmbeddedImageExtensions[i];
break;
}
}
if (s is null)
throw new ArgumentException($"Image URI '{image.Uri}' is not a valid embedded image format.");
return Convert.FromBase64String(s);
}
///
/// Load a buffer from a glTF model by its index.
/// This method will read the buffer data from the model's binary data or through an external reference solver.
///
/// glTF model
/// Index of buffer in glTF
/// Function to handle a reference for an external buffer
/// Buffer loaded as byte array
///
///
public static byte[] LoadBinaryBuffer(
this Gltf model,
int bufferIndex,
Func externalReferenceSolver
)
{
var buffer = model.Buffers[bufferIndex];
if (buffer == null)
throw new ArgumentException($"Buffer with index {bufferIndex} does not exist in the model.");
var numArray = LoadBinaryBufferUnchecked(buffer, externalReferenceSolver);
if (numArray.Length < buffer.ByteLength || numArray.Length - buffer.ByteLength > 3)
throw new InvalidDataException($"The buffer length is {numArray.Length}, expected {buffer.ByteLength}");
return numArray;
}
///
/// Load a buffer from a glTF model by its index.
/// This method reads the buffer data from the model's binary data or external file reference.
///
/// glTF Model
/// Index of buffer
/// Path of source gltf file itself, or directory URIs are assumed to be relative to
/// Buffer loaded as byte array
///
///
public static byte[] LoadBinaryBuffer(this Gltf model, int bufferIndex, string baseFilePath)
{
var buffer = model.Buffers[bufferIndex];
if (buffer == null)
throw new ArgumentException($"Buffer with index {bufferIndex} does not exist in the model.");
var numArray = LoadBinaryBufferUnchecked(buffer, baseFilePath);
if (numArray.Length < buffer.ByteLength || numArray.Length - buffer.ByteLength > 3)
throw new InvalidDataException($"The buffer length is {numArray.Length}, expected {buffer.ByteLength}");
return numArray;
}
///
/// Open an image file from a glTF model.
///
/// glTF model
/// Index of image to load
/// Function to handle an external reference
/// Stream for image file
///
/// Stateless by design: for images stored in a buffer view, each call reloads the entire
/// underlying buffer. Use for cached buffer/image access.
///
public static Stream OpenImageFile(this Gltf model, int imageIndex, Func externalReferenceSolver)
{
var image = model.Images[imageIndex];
if (image.BufferView.HasValue)
{
var bufferView = model.BufferViews[image.BufferView.Value];
return new MemoryStream(
model.LoadBinaryBuffer(bufferView.Buffer, externalReferenceSolver),
bufferView.ByteOffset,
bufferView.ByteLength
);
}
return image.IsEmbedded()
? new MemoryStream(image.OpenEmbeddedImage(out _))
: new MemoryStream(externalReferenceSolver(image.Uri));
}
///
/// Open an image file from a glTF model.
///
/// glTF model
/// Index of image to load
/// Function to handle an external reference
/// Stream for image file
///
/// Stateless by design: for images stored in a buffer view, each call reloads the entire
/// underlying buffer. Use for cached buffer/image access.
///
public static Stream OpenImageFile(this Gltf model, int imageIndex, string baseFilePath)
{
var image = model.Images[imageIndex];
if (image.BufferView.HasValue)
{
var bufferView = model.BufferViews[image.BufferView.Value];
return new MemoryStream(
model.LoadBinaryBuffer(bufferView.Buffer, baseFilePath),
bufferView.ByteOffset,
bufferView.ByteLength
);
}
return image.IsEmbedded()
? new MemoryStream(image.OpenEmbeddedImage(out _))
: new MemoryStream(LoadExternalFileReference(baseFilePath, image.Uri));
}
///
/// Checks if an image contains embedded data in it's URI
///
/// Image to check for embedded data
/// True if embedded data present
public static bool IsEmbedded(this Image image)
{
return image.Uri.StartsWith(EmbeddedImagePrefix);
}
#region saving
///
/// Saves a Schema.Gltf model to a gltf file
///
/// Schema.Gltf model
/// Destination file path
public static void SaveModel(this Gltf model, string path)
{
using (Stream stream = File.Create(path))
{
SaveModel(model, stream);
}
}
///
/// Writes a Schema.Gltf model to a writable stream
///
/// Schema.Gltf model
/// Writable stream
public static void SaveModel(this Gltf model, Stream stream)
{
string fileData = SerializeModelJson(model);
using (
var ts = new StreamWriter(
stream,
DefaultStreamWriterEncoding,
DefaultStreamWriterBufferSize,
leaveOpen: true
)
)
{
ts.Write(fileData);
}
}
///
/// Saves a Schema.Gltf model to a glb file
///
/// Schema.Gltf model
/// Binary buffer to embed in the file, or null
/// Destination file path
public static void SaveBinaryModel(this Gltf model, byte[] buffer, string filePath)
{
using (Stream stream = File.Create(filePath))
{
SaveBinaryModel(model, buffer, stream);
}
}
///
/// Writes a Schema.Gltf model to a writable stream
///
/// Schema.Gltf model
/// Binary buffer to embed in the file, or null
/// Writable stream
public static void SaveBinaryModel(this Gltf model, byte[] buffer, Stream stream)
{
using (var wb = new BinaryWriter(stream))
{
SaveBinaryModel(model, buffer, wb);
}
}
///
/// Writes a Schema.Gltf model to a writable binary writer
///
/// Schema.Gltf model
/// Binary buffer to embed in the file, or null
/// Binary Writer
public static void SaveBinaryModel(this Gltf model, byte[]? buffer, BinaryWriter binaryWriter)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
var brefcount = model.Buffers?.Count(item => item.Uri == null) ?? 0;
if (brefcount > 1)
throw new ArgumentException(
"Model has multiple buffers with a null uri; at most one GLB-stored buffer is allowed",
nameof(model)
);
if (brefcount == 1)
{
if (buffer == null)
throw new ArgumentNullException(nameof(buffer), "Model has a GLB-stored buffer (null uri)");
// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#glb-stored-buffer
// The GLB-stored buffer must have a null uri and be the first element of the buffers array.
var b = model.Buffers![0];
if (b.Uri != null)
throw new ArgumentException(
"The GLB-stored buffer (null uri) must be the first element of the buffers array",
nameof(model)
);
if (b.ByteLength > buffer.Length)
throw new ArgumentException("Buffer byte size is smaller than declared", nameof(buffer));
if ((buffer.Length - b.ByteLength) > 3)
throw new ArgumentException("Buffer byte size is larger than declared", nameof(buffer));
}
if (brefcount == 0 && buffer != null)
throw new ArgumentException(
"Buffer must be null when the model has no GLB-stored buffer (null uri)",
nameof(buffer)
);
var jsonText = SerializeModelJson(model);
var jsonChunk = Encoding.UTF8.GetBytes(jsonText);
var jsonPadding = jsonChunk.Length & 3;
if (jsonPadding != 0)
jsonPadding = 4 - jsonPadding;
if (buffer != null && buffer.Length == 0)
buffer = null;
var binPadding = buffer == null ? 0 : buffer.Length & 3;
if (binPadding != 0)
binPadding = 4 - binPadding;
int fullLength = 4 + 4 + 4;
fullLength += 8 + jsonChunk.Length + jsonPadding;
if (buffer != null)
fullLength += 8 + buffer.Length + binPadding;
binaryWriter.Write(GLTFHEADER);
binaryWriter.Write(GLTFVERSION2);
binaryWriter.Write(fullLength);
binaryWriter.Write(jsonChunk.Length + jsonPadding);
binaryWriter.Write(CHUNKJSON);
binaryWriter.Write(jsonChunk);
for (int i = 0; i < jsonPadding; ++i)
binaryWriter.Write((Byte)0x20);
if (buffer != null)
{
binaryWriter.Write(buffer.Length + binPadding);
binaryWriter.Write(CHUNKBIN);
binaryWriter.Write(buffer);
for (int i = 0; i < binPadding; ++i)
binaryWriter.Write((Byte)0);
}
}
///
/// Combines multiple buffers into a single buffer, remapping all BufferView byte offsets in the model.
///
/// glTF model whose BufferViews will be remapped to the merged buffer
/// Buffer data arrays, one entry per element in model.Buffers
/// Combined buffer data with all BufferViews remapped to buffer index 0
public static byte[] MergeBuffers(this Gltf model, byte[][] buffers)
{
if (buffers == null)
throw new ArgumentNullException(nameof(buffers));
if (model.Buffers == null || model.Buffers.Length == 0)
return Array.Empty();
if (buffers.Length != model.Buffers.Length)
throw new ArgumentException(
$"Expected {model.Buffers.Length} buffer(s), got {buffers.Length}.",
nameof(buffers)
);
if (buffers.Length == 1)
{
model.Buffers = new[] { new Buffer { ByteLength = buffers[0].Length } };
return buffers[0];
}
using var ms = new MemoryStream();
var startOffsets = new int[buffers.Length];
for (int i = 0; i < buffers.Length; i++)
{
ms.Align(4);
startOffsets[i] = (int)ms.Position;
ms.Write(buffers[i], 0, buffers[i].Length);
}
if (model.BufferViews != null)
{
foreach (var bv in model.BufferViews)
{
bv.ByteOffset += startOffsets[bv.Buffer];
bv.Buffer = 0;
}
}
var combined = ms.ToArray();
model.Buffers = new[] { new Buffer { ByteLength = combined.Length } };
return combined;
}
///
/// Writes a Schema.Gltf model to a writable binary writer and pack all data into the model
///
/// Schema.Gltf model
/// GLB output file path
/// Source file path used to load the model
/// optional GLB-stored Buffer (BIN data file). If null, then the first buffer Uri must point to a BIN file.
public static void SaveBinaryModelPacked(
this Gltf model,
string outputFile,
string gltfFilePath,
byte[]? glbBinChunk = null
)
{
using (Stream stream = File.Create(outputFile))
using (var wb = new BinaryWriter(stream))
{
SaveBinaryModelPacked(model, wb, gltfFilePath, glbBinChunk);
}
}
///
/// Writes a Schema.Gltf model to a file as a self-contained GLB, packing all resources
/// from the provided pre-loaded buffer data. External image URIs are resolved relative to
/// when supplied; otherwise only buffer-view and embedded images are supported.
///
/// Schema.Gltf model
/// GLB output file path
/// Pre-loaded buffer data, one entry per element in model.Buffers
/// Optional source file path used to resolve external image URIs
public static void SaveBinaryModelPacked(
this Gltf model,
string outputFile,
byte[][] buffers,
string? gltfFilePath = null
)
{
using (Stream stream = File.Create(outputFile))
using (var wb = new BinaryWriter(stream))
{
SaveBinaryModelPacked(model, wb, buffers, gltfFilePath);
}
}
///
/// Writes a Schema.Gltf model to a writable binary writer and pack all data into the model
///
/// Schema.Gltf model
/// Binary Writer
/// Source file path used to load the model
/// optional GLB-stored Buffer (BIN data file). If null, then the first buffer Uri must point to a BIN file.
public static void SaveBinaryModelPacked(
this Gltf model,
BinaryWriter binaryWriter,
string gltfFilePath,
byte[]? glbBinChunk = null
)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
// https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#glb-stored-buffer
// "glTF Buffer referring to GLB-stored BIN chunk, must have buffer.uri property undefined,
// and it must be the first element of buffers array"
var binData = PackBufferData(
model,
bufferIndex =>
bufferIndex == 0 && glbBinChunk != null
? glbBinChunk
: model.LoadBinaryBuffer(bufferIndex, gltfFilePath),
imageIndex => model.OpenImageFile(imageIndex, gltfFilePath)
);
SaveBinaryModel(model, binData, binaryWriter);
}
///
/// Writes a Schema.Gltf model to a writable binary writer as a self-contained GLB, packing
/// all resources from the provided pre-loaded buffer data. External image URIs are resolved relative to
/// when supplied; otherwise only buffer-view and embedded images are supported.
///
/// Schema.Gltf model
/// Binary Writer
/// Pre-loaded buffer data, one entry per element in model.Buffers
/// Optional source file path used to resolve external image URIs
public static void SaveBinaryModelPacked(
this Gltf model,
BinaryWriter binaryWriter,
byte[][] buffers,
string? gltfFilePath = null
)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
if (buffers == null)
throw new ArgumentNullException(nameof(buffers));
if (model.Buffers != null && buffers.Length != model.Buffers.Length)
throw new ArgumentException(
$"Expected {model.Buffers.Length} buffer(s), got {buffers.Length}.",
nameof(buffers)
);
var binData = PackBufferData(
model,
bufferIndex => buffers[bufferIndex],
imageIndex =>
{
var image = model.Images[imageIndex];
if (image.IsEmbedded())
return new MemoryStream(image.OpenEmbeddedImage(out _));
if (gltfFilePath != null)
return model.OpenImageFile(imageIndex, gltfFilePath);
throw new InvalidOperationException(
$"Cannot load external image '{image.Uri}' without a gltfFilePath."
);
}
);
SaveBinaryModel(model, binData, binaryWriter);
}
///
/// Writes a Schema.Gltf model to a file as a self-contained GLB, packing all resources
/// from the provided pre-loaded buffer data, using a custom function to resolve external image URIs.
///
/// Schema.Gltf model
/// GLB output file path
/// Pre-loaded buffer data, one entry per element in model.Buffers
/// Function to load raw image bytes by URI for non-embedded images
public static void SaveBinaryModelPacked(
this Gltf model,
string outputFile,
byte[][] buffers,
Func externalResolveImage
)
{
using (Stream stream = File.Create(outputFile))
using (var wb = new BinaryWriter(stream))
{
SaveBinaryModelPacked(model, wb, buffers, externalResolveImage);
}
}
///
/// Writes a Schema.Gltf model to a writable binary writer as a self-contained GLB, packing
/// all resources from the provided pre-loaded buffer data, using a custom function to resolve external image URIs.
///
/// Schema.Gltf model
/// Binary Writer
/// Pre-loaded buffer data, one entry per element in model.Buffers
/// Function to load raw image bytes by URI for non-embedded images
public static void SaveBinaryModelPacked(
this Gltf model,
BinaryWriter binaryWriter,
byte[][] buffers,
Func externalResolveImage
)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
if (buffers == null)
throw new ArgumentNullException(nameof(buffers));
if (externalResolveImage == null)
throw new ArgumentNullException(nameof(externalResolveImage));
if (model.Buffers != null && buffers.Length != model.Buffers.Length)
throw new ArgumentException(
$"Expected {model.Buffers.Length} buffer(s), got {buffers.Length}.",
nameof(buffers)
);
var binData = PackBufferData(
model,
bufferIndex => buffers[bufferIndex],
imageIndex =>
{
var image = model.Images[imageIndex];
if (image.IsEmbedded())
return new MemoryStream(image.OpenEmbeddedImage(out _));
return new MemoryStream(externalResolveImage(image.Uri));
}
);
SaveBinaryModel(model, binData, binaryWriter);
}
private static byte[]? PackBufferData(Gltf model, Func bufferLoader, Func imageLoader)
{
using var ms = new MemoryStream();
var bufferViews = new List();
var loadedBuffers = new Dictionary();
if (model.BufferViews != null)
{
foreach (var bv in model.BufferViews)
{
ms.Align(4);
var byteOffset = ms.Position;
if (!loadedBuffers.TryGetValue(bv.Buffer, out var data))
{
data = bufferLoader(bv.Buffer);
loadedBuffers[bv.Buffer] = data;
}
ms.Write(data, bv.ByteOffset, bv.ByteLength);
bv.Buffer = 0;
bv.ByteOffset = (int)byteOffset;
bufferViews.Add(bv);
}
}
if (model.Images != null)
{
for (int i = 0; i < model.Images.Length; i++)
{
var image = model.Images[i];
// Images already in a BufferView were packed above; skip them.
if (image.BufferView.HasValue)
continue;
ms.Align(4);
var byteOffset = ms.Position;
using (var imageStream = imageLoader(i))
imageStream.CopyTo(ms);
// Preserve existing MimeType for images that have one (e.g. previously buffer-view images);
// otherwise derive it from the URI.
image.MimeType = GetMimeType(image.Uri) ?? image.MimeType;
image.BufferView = bufferViews.Count;
image.Uri = null;
bufferViews.Add(
new BufferView
{
Buffer = 0,
ByteOffset = (int)byteOffset,
ByteLength = (int)(ms.Position - byteOffset),
}
);
}
}
if (!bufferViews.Any())
return null;
model.BufferViews = bufferViews.ToArray();
model.Buffers = new[] { new Buffer { ByteLength = (int)ms.Length } };
return ms.ToArray();
}
///
/// Converts self contained GLB to glTF file and associated textures and data
///
/// glTF binary file (.glb) to unpack
/// Directory where the files will be extracted
public static void Unpack(string inputFilePath, string outputDirectoryPath)
{
ArgumentNullException.ThrowIfNull(outputDirectoryPath);
ArgumentNullException.ThrowIfNull(inputFilePath);
if (!File.Exists(inputFilePath))
throw new ArgumentException("Input file does not exists");
if (!Directory.Exists(outputDirectoryPath))
throw new ArgumentException("Output directory does not exists");
string inputFileName = Path.GetFileNameWithoutExtension(inputFilePath);
string inputDirectoryPath = Path.GetDirectoryName(inputFilePath) ?? throw new InvalidOperationException();
var model = LoadModel(inputFilePath);
Buffer? binBuffer = null;
byte[]? binBufferData = null;
if (model.Buffers != null && string.IsNullOrEmpty(model.Buffers[0].Uri))
{
binBuffer = model.Buffers[0];
binBufferData = model.LoadBinaryBuffer(0, inputFilePath);
}
var imageBufferViewIndices = new List();
if (model.Images != null)
{
for (var index = 0; index < model.Images.Length; index++)
{
var image = model.Images[index];
if (!string.IsNullOrEmpty(image.Uri))
{
if (!image.Uri.StartsWith("data:"))
{
var sourceFilePath = Path.Combine(inputDirectoryPath, image.Uri);
var fileName = $"{inputFilePath}_image{index}.bin";
if (File.Exists(sourceFilePath))
{
var destinationFilePath = Path.Combine(outputDirectoryPath, fileName);
File.Copy(sourceFilePath, destinationFilePath, true);
}
image.Uri = fileName;
}
}
else if (image.BufferView.HasValue)
{
var bufferView = model.BufferViews[image.BufferView.Value];
if (bufferView.Buffer == 0)
{
imageBufferViewIndices.Add(image.BufferView.Value);
var fileExtension = image.MimeType switch
{
Image.MimeTypeJpeg => "jpg",
Image.MimeTypeKtx2 => "ktx2",
_ => "png",
};
var fileName = $"{inputFileName}_image{index}.{fileExtension}";
using (var fileStream = File.Create(Path.Combine(outputDirectoryPath, fileName)))
{
fileStream.Write(binBufferData!, bufferView.ByteOffset, bufferView.ByteLength);
}
image.BufferView = null;
image.MimeType = null;
image.Uri = fileName;
}
}
}
}
if (model.BufferViews != null)
{
var binFileName = $"{inputFileName}.bin";
var binFilePath = Path.Combine(outputDirectoryPath, binFileName);
var binByteLength = 0;
var indexMap = new Dictionary();
var bufferViews = new List();
using (var fileStream = File.Create(binFilePath))
{
for (var index = 0; index < model.BufferViews.Length; index++)
{
if (!imageBufferViewIndices.Any(imageIndex => imageIndex == index))
{
var bufferView = model.BufferViews[index];
if (bufferView.Buffer == 0)
{
fileStream.Align(4);
var fileStreamPosition = fileStream.Position;
fileStream.Write(binBufferData!, bufferView.ByteOffset, bufferView.ByteLength);
bufferView.ByteOffset = (int)fileStreamPosition;
}
var newIndex = bufferViews.Count;
if (index != newIndex)
{
indexMap.Add(index, newIndex);
}
bufferViews.Add(bufferView);
}
}
binByteLength = (int)fileStream.Length;
}
model.BufferViews = bufferViews.ToArray();
if (binByteLength == 0)
{
File.Delete(binFilePath);
if (binBuffer != null)
{
model.Buffers = model.Buffers!.Skip(1).ToArray();
foreach (var bufferView in model.BufferViews)
{
bufferView.Buffer--;
}
}
}
else
{
binBuffer?.Uri = binFileName;
binBuffer?.ByteLength = binByteLength;
}
// Remap every structure that references buffer views by index. Buffer view references
// inside opaque extension objects (e.g. KHR_draco_mesh_compression, EXT_meshopt_compression)
// cannot be remapped here and are a known limitation.
if (model.Accessors != null)
{
foreach (var accessor in model.Accessors)
{
if (accessor.BufferView.HasValue)
{
if (indexMap.TryGetValue(accessor.BufferView.Value, out int newIndex))
{
accessor.BufferView = newIndex;
}
}
if (accessor.Sparse != null)
{
if (indexMap.TryGetValue(accessor.Sparse.Indices.BufferView, out int newSparseIndices))
{
accessor.Sparse.Indices.BufferView = newSparseIndices;
}
if (indexMap.TryGetValue(accessor.Sparse.Values.BufferView, out int newSparseValues))
{
accessor.Sparse.Values.BufferView = newSparseValues;
}
}
}
}
if (model.Images != null)
{
// Images extracted to files above already had BufferView set to null; any image still
// referencing a buffer view (e.g. on a non-GLB buffer) must be remapped like accessors.
foreach (var image in model.Images)
{
if (image.BufferView.HasValue)
{
if (indexMap.TryGetValue(image.BufferView.Value, out int newIndex))
{
image.BufferView = newIndex;
}
}
}
}
}
if (model.Buffers != null)
{
for (var index = 1; index < model.Buffers.Length; index++)
{
var buffer = model.Buffers[index];
if (!buffer.Uri.StartsWith("data:"))
{
var sourceFilePath = Path.Combine(inputDirectoryPath, buffer.Uri);
var fileName = $"{inputFileName}{index}.bin";
if (File.Exists(sourceFilePath))
{
var destinationFilePath = Path.Combine(outputDirectoryPath, fileName);
File.Copy(sourceFilePath, destinationFilePath, true);
}
buffer.Uri = fileName;
}
}
}
SaveModel(model, Path.Combine(outputDirectoryPath, $"{inputFileName}.gltf"));
}
///
/// Converts a glTF file and its associated resources to a packed GLB binary file
///
/// glTF file (.gltf) to pack
/// Path where the GLB file should be generated
public static void Pack(string inputGltfFilePath, string outputGlbFile)
{
if (!File.Exists(inputGltfFilePath))
throw new ArgumentException("glTF file does not exists.", nameof(inputGltfFilePath));
Gltf model = LoadModel(inputGltfFilePath);
SaveBinaryModelPacked(model, outputGlbFile, inputGltfFilePath);
}
private static void Align(this Stream stream, int size, byte fillByte = 0)
{
var mod = stream.Position % size;
if (mod == 0)
{
return;
}
for (var i = 0; i < size - mod; i++)
{
stream.WriteByte(fillByte);
}
}
private static string? GetMimeType(string uri)
{
if (String.IsNullOrEmpty(uri))
{
return null;
}
if (uri.StartsWith("data:image/png;base64,") || uri.EndsWith(".png", StringComparison.OrdinalIgnoreCase))
{
return Image.MimeTypePng;
}
if (
uri.StartsWith("data:image/jpeg;base64,")
|| uri.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase)
|| uri.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase)
)
{
return Image.MimeTypeJpeg;
}
// Note: a packed KTX2 image is only spec-conformant if the asset declares KHR_texture_basisu.
if (uri.StartsWith("data:image/ktx2;base64,") || uri.EndsWith(".ktx2", StringComparison.OrdinalIgnoreCase))
{
return Image.MimeTypeKtx2;
}
throw new NotSupportedException(
$"Unable to determine image MIME type from uri '{uri}'. Core glTF 2.0 allows only PNG and JPEG "
+ "images; KTX2 is supported via the KHR_texture_basisu extension. Other formats have no "
+ "sanctioned glTF path and cannot be packed."
);
}
#endregion
}