Create 3D models using JS.
gltf-builder provides a means to programatically construct 3D assets in the glTF file format. It aims to make procedural generation easier by letting you build up a model piece-by-piece without having to worry about the structure of the glTF file format itself.
npm install gltf-builder
The gltf-builder package contains builders for various glTF components used to construct a 3D asset. Each component allows for all of its properties to be set in a chaining style syntax, and can be then composed together to make the final product.
import {
Asset,
Scene,
Mesh,
Node,
Primitive,
buildVec3Accessor
} from 'gltf-builder';
const triangleAsset = new Asset().addScene(
new Scene().addNode(
new Node().mesh(
new Mesh().addPrimitive(
new Primitive().position(
buildVec3Accessor([[0, 0, 0], [1, 0, 0], [0, 1, 0]])
)
)
)
)
);
// Calling .build on the asset produces the final product as an object
const gltf = triangleAsset.build();
// Saving to a file
import { writeFileSync } from 'fs';
fs.writeFileSync('triangle.gltf', JSON.stringify(gltf), 'utf8');
The created model can be viewed online using gltf-viewer or added to a scene using a framework like A-Frame.