Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Forked from donmccurdy/generate-points.ts
Created September 4, 2024 19:36
Show Gist options
  • Save unitycoder/ccd0ed50020f8c8b77687d12bc19f9ab to your computer and use it in GitHub Desktop.
Save unitycoder/ccd0ed50020f8c8b77687d12bc19f9ab 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