Common glTF modifications, written using the core API.

Most of these functions are Transforms, applying a modification to the Document, used with Document.transform. This project includes many common transforms already, and others can be quickly implemented using the same APIs. Other functions, like bounds, provide non-mutating functionality on top of the existing glTF-Transform property types.

Installation

Install:

npm install --save @gltf-transform/functions

Import:

import { NodeIO } from '@gltf-transform/core';
import { dedup, quantize, weld } from '@gltf-transform/functions';

const io = new NodeIO();
const document = io.read('input.glb');

await document.transform(
    weld(),
    quantize(),
    dedup()
);

io.write('output.glb', document);

Functions

  • bounds(node: Node | Scene): bbox
  • Computes bounding box (AABB) in world space for the given Node or Scene.

    Example:

    const {min, max} = bounds(scene);
    
  • Centers the Scene at the origin, or above/below it. Transformations from animation, skinning, and morph targets are not taken into account.

    Example:

    await document.transform(center({pivot: 'below'}));
    
  • Vertex color colorspace correction. The glTF format requires vertex colors to be stored as linear values, and this function provides a way to correct vertex colors that are (incorrectly) sRGB.

  • dedup(_options?: DedupOptions): Transform
  • inspect(doc: Document): InspectReport
  • instance(_options?: InstanceOptions): Transform
  • metalRough(_options?: MetalRoughOptions): Transform
  • Convert Materials from spec/gloss PBR workflow to metal/rough PBR workflow, removing KHR_materials_pbrSpecularGlossiness and adding KHR_materials_ior and KHR_materials_specular. The metal/rough PBR workflow is preferred for most use cases, and is a prerequisite for other advanced PBR extensions provided by glTF.

    No options are currently implemented for this function.

  • partition(_options?: PartitionOptions): Transform
  • Partitions the binary payload of a glTF file so separate mesh or animation data is in separate .bin Buffers. This technique may be useful for engines that support lazy-loading specific binary resources as needed over the application lifecycle.

    Example:

    document.getRoot().listBuffers(); // → [Buffer]
    
    await document.transform(partition({meshes: true}));
    
    document.getRoot().listBuffers(); // → [Buffer, Buffer, ...]
    
  • prune(_options?: PruneOptions): Transform
  • Removes properties from the file if they are not referenced by a Scene. Commonly helpful for cleaning up after other operations, e.g. allowing a node to be detached and any unused meshes, materials, or other resources to be removed automatically.

    Example:

    document.getRoot().listMaterials(); // → [Material, Material]
    
    await document.transform(prune());
    
    document.getRoot().listMaterials(); // → [Material]
    

    No options are currently implemented for this function.

  • Optimizes Mesh Primitives for locality of reference. Choose whether the order should be optimal for transmission size (recommended for Web) or for GPU rendering performance. Requires a MeshoptEncoder instance from the Meshoptimizer library.

    Example:

    import { MeshoptEncoder } from 'meshoptimizer';
    import { reorder } from '@gltf-transform/functions';
    
    await document.transform(
        reorder({encoder: MeshoptEncoder})
    );
    
  • resample(_options?: ResampleOptions): Transform
  • Resample Animations, losslessly deduplicating keyframes to reduce file size. Duplicate keyframes are commonly present in animation 'baked' by the authoring software to apply IK constraints or other software-specific features. Based on THREE.KeyframeTrack.optimize().

    Example: (0,0,0,0,1,1,1,0,0,0,0,0,0,0) --> (0,0,1,1,0,0)

  • sequence(_options?: SequenceOptions): Transform
  • Generates MikkTSpace vertex tangents for mesh primitives, which may fix rendering issues occuring with some baked normal maps. Requires access to the mikktspace WASM package, or equivalent.

    Example:

    import { generateTangents } from 'mikktspace';
    import { tangents } from '@gltf-transform/functions';
    
    await document.transform(
        tangents({generateTangents})
    );
    
  • De-index Primitives, disconnecting any shared vertices. This operation will generally increase the number of vertices in a mesh, but may be helpful for some geometry operations or for creating hard edges.

    No options are currently implemented for this function.

Function symbol, f(📦) → 📦, where the argument and output are a box labeled 'glTF'.

Made by Don McCurdy TypeDoc documentation Copyright 2021 under MIT license