Gltf.Glb (Gltf v0.1.0)

Copy Markdown View Source

The GLB binary container.

A .glb is a 12-byte header followed by length-prefixed chunks: a JSON chunk holding the glTF document, and usually a BIN chunk holding the buffer data that document refers to. Bundling them avoids the base64 inflation of embedding binary in JSON, which is why almost every real asset ships as GLB rather than .gltf.

Layout

magic    4 bytes   "glTF"
version  uint32    2
length   uint32    total file length, including this header

chunk:
  length uint32    payload length, excluding this 8-byte prefix
  type   uint32    0x4E4F534A "JSON", or 0x004E4942 "BIN\0"
  data   length bytes, then padding to a 4-byte boundary

Everything is little-endian regardless of the host.

Summary

Functions

Whether a binary looks like a GLB container.

Splits a GLB into its JSON document and its binary chunk.

Functions

glb?(arg1)

@spec glb?(binary()) :: boolean()

Whether a binary looks like a GLB container.

iex> Gltf.Glb.glb?(<<"glTF", 2::little-32, 12::little-32>>)
true

iex> Gltf.Glb.glb?("{\"asset\": {}}")
false

parse(arg1)

@spec parse(binary()) :: {:ok, String.t(), binary() | nil} | {:error, Gltf.Error.t()}

Splits a GLB into its JSON document and its binary chunk.

The BIN chunk is optional — a GLB may reference external buffers instead — so it comes back as nil when absent rather than as an error.