@gltf-transform/view creates three.js objects from a glTF Transform Document and maintains a real-time link between them. As you modify the Document via the glTF Transform API, the three.js scene graph updates automatically.
This provides a lossless workflow for editor-like web applications. Unlike THREE.GLTFExporter, features not supported by three.js are preserved in the underlying Document even if they are not rendered in the preview.
Key Workflow
- Load: Use
WebIO to read a glTF file into a Document. - Initialize View: Create a
DocumentView from the Document. - Bind to Scene: Use
documentView.view(sceneDef) to convert a glTF Transform Scene (or other elements) into three.js objects (like THREE.Group) and add them to your three.js scene. - Edit: Modify the
Document properties; the three.js scene will update automatically.
Memory Management
DocumentView tracks reference counts and disposes of WebGL resources (textures, geometry, materials) when the underlying glTF Transform properties are disposed.
To manually free up GPU memory by disposing of unused resources, call documentView.gc(). Resources are re-allocated automatically if they are needed again later.
import { Scene, WebGLRenderer, PerspectiveCamera } from 'three';
import { WebIO } from '@gltf-transform/core';
import { KHRONOS_EXTENSIONS } from '@gltf-transform/extensions';
import { DocumentView } from '@gltf-transform/view';
// Set up three.js scene.
const scene = new Scene();
// ...
// Load glTF Document.
const io = new WebIO().registerExtensions(KHRONOS_EXTENSIONS);
const document = await io.read('path/to/input.glb');
const documentView = new DocumentView(document);
// Add glTF content to the scene (just once).
const sceneDef = document.getRoot().getDefaultScene(); // glTF Transform Scene
const group = documentView.view(sceneDef); // THREE.Group
scene.add(group);
// Render.
function animate () {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
// When glTF Document is edited, scene updates automatically.
const materialDef = document.getRoot().listMaterials()
.find((mat) => mat.getName() === 'MyMaterial');
buttonEl.addEventListener('click', () => {
materialDef.setBaseColorHex(0xFF0000);
});