parseGlb static method
Parses a GLB binary buffer.
Implementation
static GltfResult parseGlb(Uint8List glbData) {
// GLB header: magic(4) + version(4) + length(4) = 12 bytes
final magic = ByteData.sublistView(
glbData,
0,
4,
).getUint32(0, Endian.little);
if (magic != 0x46546C67) {
throw const FormatException('Invalid GLB magic number');
}
// Chunk 0: JSON
final jsonLength = ByteData.sublistView(
glbData,
12,
16,
).getUint32(0, Endian.little);
final jsonString = utf8.decode(glbData.sublist(20, 20 + jsonLength));
// Chunk 1: Binary (optional)
Uint8List? binBuffer;
if (glbData.length > 20 + jsonLength + 8) {
final binOffset = 20 + jsonLength;
final binLength = ByteData.sublistView(
glbData,
binOffset,
binOffset + 4,
).getUint32(0, Endian.little);
binBuffer = glbData.sublist(binOffset + 8, binOffset + 8 + binLength);
}
final json = jsonDecode(jsonString) as Map<String, dynamic>;
return _parse(json, binBuffer);
}