Skip to content

Instantly share code, notes, and snippets.

@donmccurdy
Created February 27, 2022 21:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save donmccurdy/07f89a045446d943d06c3162d67ed8ac to your computer and use it in GitHub Desktop.
Save donmccurdy/07f89a045446d943d06c3162d67ed8ac to your computer and use it in GitHub Desktop.
Create a basic GLB with a single mesh.
import { Accessor, Document, Primitive, NodeIO } from '@gltf-transform/core';
const document = new Document();
const prim = document.createPrimitive();
const buffer = document.createBuffer();
const material = document.createMaterial()
.setBaseColorFactor([1.0, 0.0, 0.0, 1.0])
.setMetallicFactor(0.0)
.setRoughnessFactor(1.0);
prim.setMode(Primitive.Mode.TRIANGLES)
.setMaterial(material)
.setAttribute(
'POSITION',
document
.createAccessor()
.setArray(new Float32Array([0, 0, 0]))
.setType(Accessor.Type.VEC3)
.setBuffer(buffer)
)
.setAttribute(
'COLOR_0',
document
.createAccessor()
.setArray(new Uint8Array([128, 128, 128]))
.setType(Accessor.Type.VEC3)
.setBuffer(buffer)
);
const mesh = document.createMesh().addPrimitive(prim);
const node = document.createNode().setMesh(mesh);
const scene = document.createScene().addChild(node);
const io = new NodeIO();
await io.write('path/to/output.glb', document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment