Skip to content

Instantly share code, notes, and snippets.

@donmccurdy
Created March 18, 2024 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donmccurdy/34c4072a9bf598fb60b62c8d95abea02 to your computer and use it in GitHub Desktop.
Save donmccurdy/34c4072a9bf598fb60b62c8d95abea02 to your computer and use it in GitHub Desktop.
Generate a point cloud in glTF format, using glTF Transform.
import { Document, NodeIO, Primitive } from '@gltf-transform/core';
const document = new Document();
const buffer = document.createBuffer();
const position = document.createAccessor()
.setType('VEC3')
.setBuffer(buffer)
.setArray(new Float32Array([
0, 0, 0, // ax,ay,az
0, 0, 1, // bx,by,bz
0, 1, 0, // ...
1, 0, 0,
]));
const color = document.createAccessor()
.setType('VEC4')
.setBuffer(buffer)
.setNormalized(true)
.setArray(new Uint8Array([
0, 0, 0, 255,
0, 0, 255, 255,
0, 255, 0, 255,
255, 0, 0, 255,
]));
const prim = document
.createPrimitive()
.setMode(Primitive.Mode.POINTS)
.setAttribute('POSITION', position)
.setAttribute('COLOR_0', color);
const mesh = document.createMesh().addPrimitive(prim);
const node = document.createNode().setMesh(mesh);
const scene = document.createScene().addChild(node);
document.getRoot().setDefaultScene(scene);
const io = new NodeIO();
await io.write('./points.glb', document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment