Created
March 18, 2024 18:53
-
-
Save donmccurdy/34c4072a9bf598fb60b62c8d95abea02 to your computer and use it in GitHub Desktop.
Generate a point cloud in glTF format, using glTF Transform.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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