Document

Wraps a glTF asset and its resources for easier modification.

Documents manage glTF assets and the relationships among dependencies. The document wrapper allow tools to read and write changes without dealing with array indices or byte offsets, which would otherwise require careful management over the course of a file modification. An internal graph structure allows any property in the glTF file to maintain references to its dependencies, and makes it easy to determine where a particular property dependency is being used. For example, finding a list of materials that use a particular texture is as simple as calling Texture.listParents().

A new resource Property (e.g. a Mesh or Material) is created by calling 'create' methods on the document. Resources are destroyed by calling Property.dispose().

import fs from 'fs/promises';
import { Document } from '@gltf-transform/core';
import { dedup } from '@gltf-transform/functions';

const document = new Document();

const texture1 = document.createTexture('myTexture')
    .setImage(await fs.readFile('path/to/image.png'))
    .setMimeType('image/png');
const texture2 = document.createTexture('myTexture2')
    .setImage(await fs.readFile('path/to/image2.png'))
    .setMimeType('image/png');

// Document containing duplicate copies of the same texture.
document.getRoot().listTextures(); // → [texture x 2]

await document.transform(
    dedup({textures: true}),
    // ...
);

// Document with duplicate textures removed.
document.getRoot().listTextures(); // → [texture x 1]

Reference:

Constructor

Methods

  • createTexture(name?: string): Texture
  • createSkin(name?: string): Skin
  • createScene(name?: string): Scene
  • createNode(name?: string): Node
  • createMesh(name?: string): Mesh
  • createExtension(ctor: new (doc: Document) => T): T
  • Creates a new Extension, for the extension type of the given constructor. If the extension is already enabled for this Document, the previous Extension reference is reused.

  • createCamera(name?: string): Camera
  • createBuffer(name?: string): Buffer
  • Overrides the Logger instance used for any operations performed on this document.

    Usage:

    doc
        .setLogger(new Logger(Logger.Verbosity.SILENT))
        .transform(dedup(), weld());
    
  • transform(transforms?: Transform[]): Promise<this>
  • Applies a series of modifications to this document. Each transformation is asynchronous, takes the Document as input, and returns nothing. Transforms are applied in the order given, which may affect the final result.

    Usage:

    await doc.transform(
        dedup(),
        prune()
    );
    
Function symbol, where the argument and output are a box labeled 'glTF'.

Made by Don McCurdy. Documentation built with greendoc and published under Creative Commons Attribution 3.0.