glTF for Go
Read, write, validate, and decode glTF 2.0 / GLB 3D assets in Go.
A dependency-free, standard-library-only Go toolkit for reading, writing, and inspecting glTF 2.0 assets in both the JSON (.gltf) and binary (.glb) container formats. The Document type mirrors the full glTF 2.0 schema — scenes, nodes, meshes, accessors, buffer views, buffers, materials, textures, animations, skins and cameras — as spec-indexed slices, with typed enumerations and vendor Extensions/Extras preserved as raw JSON. Buffers resolve from GLB BIN chunks, base64 data URIs or external files; typed accessor decoders read vertex data honoring byteOffset, byteStride, normalization and sparse substitutions; Validate reports every structural problem with a descriptive path; and a triangle builder emits a minimal valid asset. No cgo, no third-party dependencies.
Install
$ go get github.com/malcolmston/gltfQuick start
import "github.com/malcolmston/gltf"
// OpenGLB resolves the BIN chunk and external buffers automatically.
doc, _ := gltf.OpenGLB("model.glb")
prim := doc.Meshes[0].Primitives[0]
positions, _ := doc.DecodeAccessorVec3(prim.Attributes["POSITION"])
indices, _ := doc.DecodeIndices(&prim)
fmt.Println(len(positions), "vertices,", len(indices), "indices")Features
Documentmodel — the glTF 2.0 root, with every collection (scenes, nodes, meshes, accessors, buffer views, buffers, materials, textures, animations, skins, cameras) a spec-indexed slice;ExtensionsandExtraspreserved asjson.RawMessage- JSON I/O —
Decode/Encodeplus theOpen/Savefile helpers for.gltfdocuments - Binary GLB container —
ReadGLB/WriteGLBandOpenGLB/SaveGLB, handling the JSON + BIN chunks, magic/version checks and 4-byte padding - Buffer resolution from GLB BIN chunks, base64
data:URIs and external files viaDocument.ResolveBuffers, withEncodeDataURIfor embedding - Typed accessor decoders —
DecodeAccessorVec2/Vec3/Vec4,DecodeAccessorFloat32,DecodeAccessorUint32andDecodeIndices, honoringbyteOffset,byteStride,normalizedand sparse substitutions - Typed enumerations —
ComponentType,AccessorType,PrimitiveMode,Filter,WrapMode,Interpolation,CameraTypeandAlphaModecarry the spec-defined constant values - Structural validation —
Document.Validatechecks required fields and index ranges, returningValidationErrorswith paths likemeshes[0].primitives[0].attributes.POSITION(AsValidationErrorsunwraps them) - Triangle builder —
Triangleconstructs a minimal valid document plus its BIN buffer, andWriteTriangleGLB/WriteTriangleGLTFemit it directly