@3ditles/glb
v1.0.1
Published
A TypeScript library for parsing and processing GLB 3D model files
Maintainers
Readme
@3dtiles/glb
A TypeScript library for parsing and processing GLB 3D model files.
Features
- Parse GLB files into structured data
- Validate GLB file integrity and structure
- Serialize GLB data back into binary format
- Support for GLB version 2
- Handle both JSON and binary chunks
Installation
npm install @3dtiles/glbUsage
Parsing a GLB file
import { parseGLB, validateGLB } from '@3dtiles/glb';
import { readFileSync } from 'fs';
// Read GLB file as binary
const data = readFileSync('model.glb');
const glbFile = parseGLB(new Uint8Array(data.buffer));
// Validate the parsed file
if (validateGLB(glbFile)) {
console.log('Valid GLB file');
console.log('JSON content:', glbFile.jsonContent);
console.log('Has binary chunk:', !!glbFile.binaryChunk);
} else {
console.error('Invalid GLB file');
}Serializing a GLB file
import { serializeGLB, GLBFile } from '@3dtiles/glb';
import { writeFileSync } from 'fs';
// Create or modify a GLBFile object
const glbFile: GLBFile = {
header: {
magic: 0x46546C67,
version: 2,
length: 0 // Will be calculated automatically
},
jsonContent: {
scenes: [{ nodes: [] }],
nodes: []
},
// ... add other chunks as needed
};
// Serialize to binary
const binaryData = serializeGLB(glbFile);
// Write to file
writeFileSync('new-model.glb', binaryData);API
parseGLB(data: Uint8Array): GLBFile
Parses binary data into a GLBFile structure.
validateGLB(file: GLBFile): boolean
Validates the structure and integrity of a parsed GLBFile.
serializeGLB(file: GLBFile): Uint8Array
Serializes a GLBFile back into binary format.
GLBParser class
Provides the core functionality with methods:
parse(data: Uint8Array): GLBFilevalidate(file: GLBFile): booleanserialize(file: GLBFile): Uint8Array
License
MIT
