The GLTFScenegraph
class provides an API for accessing and modifying glTF data.
Caveat: Modification of binary data chunks has limitations, and this class is currently not intended to be a generic utility for modifying glTF data.
import {GLTFLoader, GLTFScenegraph} from '@loaders.gl/gltf';
import {load} from '@loaders.gl/core';
// Load and parse a file
const gltfData = await parse(fetch(GLTF_URL), GLTFLoader);
// Create a parser
const gltf = new GLTFScenegraph(gltfData);
// Get the complete glTF JSON structure
const gltfJson = gltf.getJSON();
// Get specific top-level fields from the glTF JSON chunk
const appData = gltf.getApplicationData('customData');
// Get a top level extension from the glTF JSON chunk
const topLevelExtension = gltf.getExtension('ORGNAME_extensionName');
if (topLevelExtension) {
...
}
// Get images from the binary chunk (together with metadata)
const imageIndex = 0;
const image = gltf.getImage(imageIndex);
// Get default glTF scenegraph
const scenegraph = gltf.getScene();
// Get specific glTF scenegraph
const scenegraph = gltf.getScene(2);
import {load, encode} from '@loaders.gl/core';
import {ImageWriter} from '@loaders.gl/images';
import {GLTFLoader, GLTFWriter, GLTFScenegraph} from '@loaders.gl/gltf';
// Load and parse a file
const gltfData = await load(GLTF_URL, GLTFLoader);
const meshIndex = gltfBuilder.addMesh(gltfData.meshes[0].primitives[0].attributes);
const nodeIndex = gltfBuilder.addNode(meshIndex);
const sceneIndex = gltfBuilder.addScene([nodeIndex]);
gltfBuilder.setDefaultScene(sceneIndex);
const imageBuffer = await encode(gltfData.images[0].image, ImageWriter);
const imageIndex = gltfBuilder.addImage(imageBuffer, 'image/jpeg');
const textureIndex = gltfBuilder.addTexture(imageIndex);
const pbrMaterialInfo = {
pbrMetallicRoughness: {
baseColorTexture: textureIndex
}
};
gltfBuilder.addMaterial(pbrMaterialInfo);
gltfBuilder.createBinaryChunk();
const gltfBuffer = encodeSync(gltfBuilder.gltf, GLTFWriter);
Contains all data of gltf including the json part and the binary chunk. The binary chunk is generated by createBinChunk()
method
Accumulates buffers of resources added by addMesh()
, addImage()
etc. These buffers should be compiled before encoding using createBinaryChunk()
Total byteLength of the binary part of gltf
Creates a new GLTFScenegraph
instance from a pure JavaScript object.
Returns the given data field in the top-level glTF JSON object.
Returns a key in the top-level glTF extras
JSON object.
Returns the top-level extension by name
, if present.
Returns an array of extension names (covering all extensions used at any level of the glTF hierarchy).
Returns an array of extensions at any level of the glTF hierarchy that are required to properly display this file (covering all extensions used at any level of the glTF hierarchy).
Returns the scene (scenegraph) with the given index, or the default scene if no index is specified.
Returns the image with specified index
Accepts buffer view index or buffer view object
Accepts accessor index or accessor object.
Returns a typed array with type that matches the types
accepts accessor index or accessor object
Add an extra application-defined key to the top-level data structure
extras
- Standard GLTF field for storing application specific data
Add to GLTF top level extension object, mark as used
Add GLTF top level extension object, mark as used and required
Add extensionName to list of used extensions
Add extensionName to list of required extensions
Removes an extension from the top-level list
Set the default scene which is to be displayed at load time
Add a scene to the json part
Add a node to the json part
Add one untyped source buffer, create a matching glTF bufferView
, and return its index
The binary data will not be added to the gltf buffer until
createBinChunk()
is called.
Add a texture to the json part
Add a material to the json part
Adds an accessor to a bufferView
The binary data will not be added to the gltf buffer until
createBinChunk()
is called.
Adds a binary image. Builds glTF "JSON metadata" and saves buffer reference Buffer will be copied into BIN chunk during "pack"
The binary data will not be added to the gltf buffer until
createBinChunk()
is called.
Packs any pending binary data into the first binary glTF buffer.
Note: Overwrites the existing first buffer if present.