2026-10-30 13:52:09 -05:00
2026-10-30 13:52:09 -05:00
2026-10-30 13:52:09 -05:00
2026-10-30 13:52:09 -05:00
2026-10-30 13:52:09 -05:00
2026-10-30 13:52:09 -05:00
2026-10-30 13:52:09 -05:00

gltf

Models read from glTF 2.0, as a .gltf file and the files it names or as a .glb file, whole and checked: every part of the file, each index between parts, and each accessor's reach within its buffer, so what's read is read without failing after. What an accessor holds is read as numbers, as floats or as integers. It's Sakura's, built with blossom, reads std's containers, io, json, and math, and places nodes by the vectors, rotations, and matrices of linear, of the physics package.

val tree := gltf::load("models/tree.glb") else (e) { io::println(e.describe().view()); ... };
for &part in tree.meshes[0].primitives {
  val places := tree.floats(part.find("POSITION").expect("..."));    # x, y, z of each vertex
  val indices := tree.integers(part.indices.expect("..."));          # three a triangle
  val look := &tree.materials[part.material.expect("...")];          # base color, textures, alpha
}
val placed := tree.world_matrix(2);                                   # a node, through its parents

gltf::decode(bytes, folder) reads bytes already in memory, the files they name found in the folder. An image is given as the bytes of its file, PNG or JPEG, as image reads them, which gltf doesn't decode itself.

A project reads it by requiring this folder, or its remote, and naming gltf among a target's deps:

requires:
- ../gltf
targets:
- name: game
  type: program
  deps: [gltf]

Layout

gltf/ is the module gltf.

Path Holds
gltf.sakura asset, gltf_error and its failure, the extensions read, and load and decode
parts.sakura The parts of an asset: scenes, nodes, meshes and their primitives, materials and the textures they name, textures, samplers, images, cameras, skins, animations, and lights
accessors.sakura Buffers, their views, and accessors; reading an accessor's numbers, sparse ones among them
nodes.sakura The tree of nodes: each placed by a matrix or a translation, rotation, and scale, each child's parent, and a node's world matrix
parse.sakura Reading the JSON into the parts, in the order they need one another, each checked as it's read
members.sakura Reading the JSON's members: each of its kind, with its default, an index checked against its list
files.sakura A .glb file's chunks, and the bytes a URI names: a data: URI's base64, or a file beside the asset

What's read

  • Files: .gltf, its buffers and images named as files beside it, their URIs percent-decoded, or as data: URIs of base64; and .glb, its JSON and binary chunks, other chunks passed over. A URI naming another place, as https:, isn't fetched, and is unsupported.
  • Every part of glTF 2.0: scenes, and the scene shown at first; nodes, each with its children and parent, its mesh, camera, skin, and light, its morph weights, and where it is, as a matrix, taken apart into a translation, rotation, and scale, x's negative where it mirrors, or those, put together into a matrix; meshes, their primitives' attributes, indices, material, mode, and morph targets; materials of the metallic-rough model, each texture they name with its TEXCOORD set, a normal map's scale and occlusion's strength, emission, and alpha opaque, cut out, or blended, and double sided; textures, samplers, and images; cameras, perspective or orthographic; skins; and animations, their channels and samplers, linear, step, or cubic spline.
  • Accessors of every component type and element, normalized or not, interleaved by a view's stride, a matrix's columns each begun at a multiple of 4 bytes, and sparse, over a view's elements or over zeros. floats makes integers floats, and normalized ones fractions, as glTF says, -1 at the least for a signed one; integers reads indices and joints as they are.
  • Extensions: KHR_lights_punctual's directional, point, and spot lights; KHR_materials_unlit; KHR_materials_emissive_strength, multiplying emission; and KHR_mesh_quantization, whose accessors of integers floats reads anyway. An asset requiring another is unsupported, naming it. One using another without requiring it is read without what it adds, and names it in extensions.
  • What's checked: the version is 2; every index names a part there is; each buffer view lies within its buffer, of a stride from 4 to 252, a multiple of 4; each accessor lies within its view, its stride no less than an element; each sparse index names an element; a primitive's attributes are of one count, and its indices name vertices it has; a skin's inverse binds are a matrix of floats for each joint; an animation's times are floats; each node has one parent at most, following parents up ends, and a scene's nodes are roots. What breaks it is malformed, saying what and where, as meshes[2].primitives[0]: its index 9 is past its 8 vertices.

A gltf_error holds the first 240 bytes of why, so it's plain data, as std's errors are.

Speed

33 of Khronos's sample models, 65 MB of them, from a box of 24 vertices to BrainStem's skin and MetalRoughSpheres' 3.5 million numbers, each read whole and every accessor's numbers read after, take 136 ms on a Ryzen 9 9950X3D, the files' own reading among it.

Testing

blossom test runs tests/, which writes models of its own and reads them back: a quad from JSON with its buffer in a data: URI, from a .glb file, and from files beside it, one named with a space; an image in a buffer view; numbers of every size, interleaved, normalized, in a matrix's padded columns, and sparse; a matrix taken apart and a translation put together, through a parent; cameras, a spot light, a skin, and an animation; and what's refused, each with why.

What's read was checked further against Khronos's glTF sample assets, which aren't part of the repository: 33 models, as .glb, embedded .gltf, and .gltf with files beside it, each read, every accessor its primitives, skins, and animations name read, and every position within the least and the most its accessor says.

Not yet

Writing glTF; URIs fetched from the network; KHR_texture_transform, KHR_draco_mesh_compression, EXT_meshopt_compression, and KHR_texture_basisu; and extensions' extras.

S
Description
A model loading library written in the Sakura programming language for game development.
Readme
223 KiB
0 Stars 1 Watchers 0 Forks