glTF for Go

github.com/malcolmston/gltf

Read, write, validate, and decode glTF 2.0 / GLB 3D assets in Go.

 GitHubports KhronosGroup/glTF

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

shell
$ go get github.com/malcolmston/gltf

Quick start

main.go
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

  • Document model — the glTF 2.0 root, with every collection (scenes, nodes, meshes, accessors, buffer views, buffers, materials, textures, animations, skins, cameras) a spec-indexed slice; Extensions and Extras preserved as json.RawMessage
  • JSON I/O — Decode / Encode plus the Open / Save file helpers for .gltf documents
  • Binary GLB container — ReadGLB / WriteGLB and OpenGLB / 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 via Document.ResolveBuffers, with EncodeDataURI for embedding
  • Typed accessor decoders — DecodeAccessorVec2/Vec3/Vec4, DecodeAccessorFloat32, DecodeAccessorUint32 and DecodeIndices, honoring byteOffset, byteStride, normalized and sparse substitutions
  • Typed enumerations — ComponentType, AccessorType, PrimitiveMode, Filter, WrapMode, Interpolation, CameraType and AlphaMode carry the spec-defined constant values
  • Structural validation — Document.Validate checks required fields and index ranges, returning ValidationErrors with paths like meshes[0].primitives[0].attributes.POSITION (AsValidationErrors unwraps them)
  • Triangle builder — Triangle constructs a minimal valid document plus its BIN buffer, and WriteTriangleGLB / WriteTriangleGLTF emit it directly
glTF