@gltf-transform/extensions
Part of the glTF-Transform project.
Quickstart
Install the scripting packages:
npm install --save @gltf-transform/core @gltf-transform/extensions @gltf-transform/functions
Read and write glTF scenes with platform I/O utilities WebIO, NodeIO, or DenoIO:
import { Document, NodeIO } from '@gltf-transform/core';
import { KHRONOS_EXTENSIONS } from '@gltf-transform/extensions';
import draco3d from 'draco3dgltf';
const io = new NodeIO()
.registerExtensions(KHRONOS_EXTENSIONS)
.registerDependencies({
'draco3d.decoder': await draco3d.createDecoderModule(),
'draco3d.encoder': await draco3d.createEncoderModule(),
});
const document = await io.read('path/to/model.glb');
const glb = await io.writeBinary(document);
To perform changes to an existing glTF Document, import off-the-shelf scripts from the Functions package, or write your own using API classes like Material, Primitive, and Texture.
import { resample, prune, dedup, draco, textureCompress } from '@gltf-transform/functions';
import * as sharp from 'sharp';
await document.transform(
resample(),
prune(),
dedup(),
draco(),
textureCompress({
encoder: sharp,
targetFormat: 'webp',
resize: [1024, 2024],
}),
backfaceCulling({cull: true}),
);
function backfaceCulling(options) {
return (document) => {
for (const material of document.getRoot().listMaterials()) {
material.setDoubleSided(!options.cull);
}
};
}
To learn how glTF-Transform works, and the architecture of the scripting API, start with Concepts. To try out the scripting API without installing anything, visit gltf.report/, load a glTF model, and open the Script tab.
Credits
See Credits.
License
Copyright 2023, MIT License.