### CLI - Command-line interface for gltf-pipeline Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt The gltf-pipeline binary exposes all library functions through command-line flags. Install globally with `npm install -g gltf-pipeline` and invoke as `gltf-pipeline -i [options]`. ```APIDOC ## CLI - Command-line interface for gltf-pipeline ### Description The `gltf-pipeline` binary exposes all library functions through command-line flags. Install globally with `npm install -g gltf-pipeline` and invoke as `gltf-pipeline -i [options]`. The output path defaults to `-processed.` in the same directory as the input. ### Usage Examples **Convert .gltf → .glb (binary pack)** ```bash gltf-pipeline -i model.gltf -o model.glb ``` **Convert .glb → .gltf (embedded JSON)** ```bash gltf-pipeline -i model.glb -j -o model.gltf ``` **Apply Draco compression with custom quantization** ```bash gltf-pipeline -i model.gltf -d \ --draco.compressionLevel 10 \ --draco.quantizePositionBits 14 \ --draco.unifiedQuantization \ --draco.uncompressedFallback \ -o model-draco.gltf ``` **Separate textures (write .png/.jpg alongside .gltf)** ```bash gltf-pipeline -i model.gltf -t -o out/model.gltf ``` **Separate all resources (buffers + shaders + textures)** ```bash gltf-pipeline -i model.gltf -s -o out/model.gltf ``` **Print statistics, keep unused nodes/meshes, keep legacy extensions** ```bash gltf-pipeline -i model.gltf --stats --keepUnusedElements --keepLegacyExtensions ``` **Allow absolute buffer URIs (use with caution)** ```bash gltf-pipeline -i model.gltf -a -o model-out.gltf ``` **glTF 1.0 → 2.0 PBR upgrade with custom uniform name hints** ```bash gltf-pipeline -i legacy-model.gltf \ --baseColorTextureNames u_diffuse u_tex \ --baseColorFactorNames u_diffuse_mat \ -o model-pbr.gltf ``` **Full flag reference** ```bash gltf-pipeline --help ``` ``` -------------------------------- ### Install glTF Pipeline CLI Source: https://github.com/cesiumgs/gltf-pipeline/blob/main/README.md Installs the glTF Pipeline command-line interface globally using npm. ```bash npm install -g gltf-pipeline ``` -------------------------------- ### Generate Project Documentation Source: https://github.com/cesiumgs/gltf-pipeline/blob/main/README.md Create project documentation using JSDoc. The generated documentation will be saved in the `doc` folder. ```bash npm run jsdoc ``` -------------------------------- ### Full flag reference Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Run gltf-pipeline --help to see the full list of available command-line flags and options. ```bash gltf-pipeline --help ``` -------------------------------- ### Convert .gltf to .glb (binary pack) Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Use the -i and -o flags to specify input and output files for conversion. ```bash gltf-pipeline -i model.gltf -o model.glb ``` -------------------------------- ### Separate all resources (buffers + shaders + textures) Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Use the -s flag to separate all resources, including buffers, shaders, and textures. Specify the output path with -o. ```bash gltf-pipeline -i model.gltf -s -o out/model.gltf ``` -------------------------------- ### Run Project Tests Source: https://github.com/cesiumgs/gltf-pipeline/blob/main/README.md Execute all project tests using npm. This command verifies the functionality of the Node.js module. ```bash npm run test ``` -------------------------------- ### glTF 1.0 to 2.0 PBR upgrade with custom uniform name hints Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Upgrade glTF 1.0 to 2.0 PBR by specifying custom uniform name hints for base color textures and factors. ```bash gltf-pipeline -i legacy-model.gltf \ --baseColorTextureNames u_diffuse u_tex \ --baseColorFactorNames u_diffuse_mat \ -o model-pbr.gltf ``` -------------------------------- ### Apply Draco compression with custom quantization Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Use the -d flag for Draco compression and specify quantization options like compressionLevel, quantizePositionBits, unifiedQuantization, and uncompressedFallback. ```bash gltf-pipeline -i model.gltf -d \ --draco.compressionLevel 10 \ --draco.quantizePositionBits 14 \ --draco.unifiedQuantization \ --draco.uncompressedFallback \ -o model-draco.gltf ``` -------------------------------- ### gltfToGlb(gltf, options) Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Converts a glTF JSON object into a binary GLB buffer. It processes the glTF asset and packs all resources into a single binary chunk within the GLB file. Accepts options similar to `processGltf`. ```APIDOC ## gltfToGlb(gltf, options) ### Description Converts a glTF JSON object to a binary GLB buffer. All buffers are merged into the GLB binary chunk. Accepts the same `options` as `processGltf`. ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - **gltf** (object) - The glTF JSON object to convert. - **options** (object) - Optional. Configuration options, including `resourceDirectory` and `dracoOptions`. - **resourceDirectory** (string) - Path to the directory containing external resources. - **dracoOptions** (object) - Options for Draco compression. - **compressionLevel** (number) - The Draco compression level. ### Request Example ```javascript const { gltfToGlb } = require("gltf-pipeline"); const fsExtra = require("fs-extra"); const gltf = fsExtra.readJsonSync("./input/model.gltf"); gltfToGlb(gltf, { resourceDirectory: "./input/" }) .then(({ glb }) => { fsExtra.writeFileSync("./output/model.glb", glb); console.log(`GLB written: ${glb.byteLength} bytes`); }) .catch((err) => console.error(err)); // With Draco compression: gltfToGlb(gltf, { resourceDirectory: "./input/", dracoOptions: { compressionLevel: 10 }, }) .then(({ glb }) => fsExtra.writeFileSync("./output/model-draco.glb", glb)); ``` ### Response #### Success Response (200) - **glb** (Buffer) - The resulting binary GLB buffer. - **separateResources** (object) - An object containing any resources that were separated during processing. #### Response Example ```json { "glb": Buffer, "separateResources": {} } ``` ``` -------------------------------- ### Separate textures (write .png/.jpg alongside .gltf) Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Use the -t flag to separate textures into external files. Specify the output path with -o. ```bash gltf-pipeline -i model.gltf -t -o out/model.gltf ``` -------------------------------- ### Build for CesiumJS Integration Source: https://github.com/cesiumgs/gltf-pipeline/blob/main/README.md Generate necessary files for CesiumJS integration. The output is placed in the `dist/cesium` folder. ```bash npm run build-cesium ``` -------------------------------- ### Convert .glb to .gltf (embedded JSON) Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Use the -i flag for input and the -j flag to embed JSON. Specify the output file with -o. ```bash gltf-pipeline -i model.glb -j -o model.gltf ``` -------------------------------- ### Run ESLint for Code Linting Source: https://github.com/cesiumgs/gltf-pipeline/blob/main/README.md Apply ESLint to the entire codebase for code quality checks. This helps maintain consistent coding standards. ```bash npm run eslint ``` -------------------------------- ### Enable absolute paths in glTF processing Source: https://github.com/cesiumgs/gltf-pipeline/blob/main/README.md Demonstrates how to set the `allowAbsolute: true` option when processing glTF files to permit absolute paths, avoiding warnings when no `resourceDirectory` is specified. ```javascript const options = { allowAbsolute: true, /*... */ }; const results = await processGltf(gltf, options); ``` -------------------------------- ### Convert glb to glTF CLI Source: https://github.com/cesiumgs/gltf-pipeline/blob/main/README.md Converts a glb file to a glTF file using the command-line tool. Use the -j flag to unpack embedded resources. ```bash gltf-pipeline -i model.glb -o model.gltf ``` ```bash gltf-pipeline -i model.glb -j ``` -------------------------------- ### Convert glTF to glb CLI Source: https://github.com/cesiumgs/gltf-pipeline/blob/main/README.md Converts a glTF file to a glb file using the command-line tool. Use the -b flag to embed resources. ```bash gltf-pipeline -i model.gltf -o model.glb ``` ```bash gltf-pipeline -i model.gltf -b ``` -------------------------------- ### Print statistics, keep unused elements, and keep legacy extensions Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Use the --stats flag to print statistics. Use --keepUnusedElements and --keepLegacyExtensions to retain specific data. ```bash gltf-pipeline -i model.gltf --stats --keepUnusedElements --keepLegacyExtensions ``` -------------------------------- ### Allow absolute buffer URIs Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Use the -a flag to allow absolute buffer URIs. Use with caution. Specify the output file with -o. ```bash gltf-pipeline -i model.gltf -a -o model-out.gltf ``` -------------------------------- ### processGlb(glb, options) Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Processes an input GLB buffer and returns an optimized GLB buffer. This function applies the full gltf-pipeline, including optional Draco compression, and re-packs the result into a new GLB buffer. ```APIDOC ## processGlb(glb, options) ### Description Processes a GLB buffer and returns an optimized GLB buffer. Parses the input GLB, applies the full gltf-pipeline (including optional Draco compression), and re-packs the result into a new GLB binary buffer. This is the binary-in / binary-out convenience function. Accepts the same `options` as `processGltf`. Returns a Promise resolving to `{ glb: Buffer, separateResources }`. ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - **glb** (Buffer) - The input binary GLB buffer. - **options** (object) - Optional. Configuration options, including `dracoOptions` and `stats`. - **dracoOptions** (object) - Options for Draco compression. - **compressionLevel** (number) - The Draco compression level. - **quantizePositionBits** (number) - Number of bits for position quantization. - **unifiedQuantization** (boolean) - Whether to use unified quantization. - **uncompressedFallback** (boolean) - Whether to include an uncompressed fallback. - **stats** (boolean) - If true, statistics about the processing will be included. ### Request Example ```javascript const { processGlb } = require("gltf-pipeline"); const fsExtra = require("fs-extra"); const inputGlb = fsExtra.readFileSync("./model.glb"); processGlb(inputGlb, { dracoOptions: { compressionLevel: 7, quantizePositionBits: 14, unifiedQuantization: true, uncompressedFallback: true, }, stats: true, }) .then(({ glb }) => { fsExtra.writeFileSync("./model-optimized.glb", glb); console.log(`Optimized GLB: ${glb.byteLength} bytes`); }) .catch((err) => { console.error("processGlb failed:", err); process.exitCode = 1; }); ``` ### Response #### Success Response (200) - **glb** (Buffer) - The optimized binary GLB buffer. - **separateResources** (object) - An object containing any resources that were separated during processing. #### Response Example ```json { "glb": Buffer, "separateResources": {} } ``` ``` -------------------------------- ### glbToGltf(glb, options) Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Converts a binary GLB buffer into a glTF JSON object. It parses the GLB, extracts embedded resources, and processes them. Options can control whether resources are kept embedded or separated. ```APIDOC ## glbToGltf(glb, options) ### Description Converts a binary GLB buffer to a glTF JSON object. Parses a binary GLB `Buffer`, extracts the embedded binary chunk, and processes the result through `processGltf`. Returns a Promise resolving to `{ gltf, separateResources }`. Use `options.separate` or `options.separateTextures` to control whether embedded resources stay embedded or are written out. ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - **glb** (Buffer) - The binary GLB buffer to convert. - **options** (object) - Optional. Configuration options. - **separate** (boolean) - If true, embedded resources are written out to separate files. - **separateTextures** (boolean) - If true, embedded textures are written out to separate files. ### Request Example ```javascript const { glbToGltf } = require("gltf-pipeline"); const fsExtra = require("fs-extra"); const glb = fsExtra.readFileSync("./model.glb"); // Embedded glTF (all resources as data URIs) glbToGltf(glb) .then(({ gltf }) => { fsExtra.writeJsonSync("./model-embedded.gltf", gltf, { spaces: 2 }); }); // Separate textures and buffers written alongside the .gltf glbToGltf(glb, { separate: true }) .then(({ gltf, separateResources }) => { fsExtra.writeJsonSync("./out/model.gltf", gltf, { spaces: 2 }); for (const [rel, data] of Object.entries(separateResources)) { fsExtra.outputFileSync(`./out/${rel}`, data); } }) .catch((err) => console.error("Conversion failed:", err.message)); ``` ### Response #### Success Response (200) - **gltf** (object) - The resulting glTF JSON object. - **separateResources** (object) - An object containing any resources that were separated during processing. #### Response Example ```json { "gltf": { ... }, "separateResources": { "model0.bin": Buffer, "texture0.png": Buffer, ... } } ``` ``` -------------------------------- ### Convert glTF to Draco glTF CLI Source: https://github.com/cesiumgs/gltf-pipeline/blob/main/README.md Converts a glTF file to a Draco-compressed glTF file using the command-line tool. ```bash gltf-pipeline -i model.gltf -o modelDraco.gltf -d ``` -------------------------------- ### Watch for ESLint Changes Source: https://github.com/cesiumgs/gltf-pipeline/blob/main/README.md Continuously run ESLint and automatically check files as they are saved. Leave this command running in a console window. ```bash npm run eslint-watch ``` -------------------------------- ### Run Test Coverage with nyc Source: https://github.com/cesiumgs/gltf-pipeline/blob/main/README.md Generate code coverage reports using nyc. This command helps identify areas of the code that are not adequately tested. ```bash npm run coverage ``` -------------------------------- ### processGltf(gltf, options) Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Processes a glTF JSON object through the full gltf-pipeline. This includes reading external resources, upgrading the glTF version, adding defaults, applying optional stages like Draco compression, writing resources, and stripping internal pipeline metadata. The function returns a Promise that resolves to an object containing the processed glTF object and any separate resources. The original glTF object is modified in place. ```APIDOC ## processGltf(gltf, options) ### Description Runs a glTF JavaScript object through the complete gltf-pipeline: reads external resources, upgrades the glTF version, adds defaults, applies optional stages (Draco compression, unused element removal), writes resources, and strips internal pipeline metadata. Returns a Promise resolving to `{ gltf, separateResources }`. The original object is modified in place. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **gltf** (object) - Required - The glTF JavaScript object to process. - **options** (object) - Optional - Configuration options for the pipeline. - **resourceDirectory** (string) - Directory to resolve external .bin and texture files. - **separate** (boolean) - Embed all buffers, shaders, textures in glTF JSON. Defaults to false. - **separateTextures** (boolean) - Write textures as external files. Defaults to true. - **stats** (boolean) - Print before/after statistics to console. Defaults to false. - **keepUnusedElements** (boolean) - Strip unused nodes, meshes, materials. Defaults to false. - **keepLegacyExtensions** (boolean) - Convert KHR_techniques_webgl / KHR_materials_common to PBR. Defaults to false. - **dracoOptions** (object) - Options for Draco mesh compression. - **compressionLevel** (number) - 0 (fastest/least) to 10 (slowest/most). - **quantizePositionBits** (number) - Precision bits for vertex positions. - **quantizeNormalBits** (number) - Precision bits for vertex normals. - **quantizeTexcoordBits** (number) - Precision bits for vertex texture coordinates. - **quantizeColorBits** (number) - Precision bits for vertex colors. - **quantizeGenericBits** (number) - Precision bits for joints, weights, custom attributes. - **uncompressedFallback** (boolean) - Include uncompressed mesh alongside Draco mesh. - **unifiedQuantization** (boolean) - Use a shared quantization grid across all primitives. - **customStages** (array) - An array of custom stage functions to inject into the pipeline. - **logger** (function) - A custom function to handle log messages. - **baseColorTextureNames** (array) - Hint for 1.0→2.0 PBR upgrade for base color texture names. - **baseColorFactorNames** (array) - Hint for 1.0→2.0 PBR upgrade for base color factor names. ### Request Example ```javascript const gltfPipeline = require("gltf-pipeline"); const fsExtra = require("fs-extra"); const gltf = fsExtra.readJsonSync("./model.gltf"); const options = { resourceDirectory: "./", separate: false, separateTextures: true, stats: true, keepUnusedElements: false, keepLegacyExtensions: false, dracoOptions: { compressionLevel: 7, quantizePositionBits: 11, quantizeNormalBits: 8, quantizeTexcoordBits: 10, quantizeColorBits: 8, quantizeGenericBits: 8, uncompressedFallback: false, unifiedQuantization: false, }, customStages: [ function myCustomStage(gltf, options) { gltf.asset.copyright = "My Company 2024"; }, ], logger: (msg) => process.stderr.write(msg + "\n"), baseColorTextureNames: ["u_diffuse", "u_tex"], baseColorFactorNames: ["u_diffuse_mat"], }; processGltf(gltf, options) .then(({ gltf: outGltf, separateResources }) => { fsExtra.writeJsonSync("./out/model.gltf", outGltf, { spaces: 2 }); for (const [relativePath, data] of Object.entries(separateResources)) { fsExtra.outputFileSync(`./out/${relativePath}`, data); } console.log("Done."); }) .catch((err) => { console.error("Pipeline error:", err.message); process.exitCode = 1; }); ``` ### Response #### Success Response (200) - **gltf** (object) - The processed glTF JSON object. - **separateResources** (object) - An object where keys are relative paths and values are the content of external resources (buffers, textures) that were separated. #### Response Example ```json { "gltf": { "asset": { "version": "2.0", "copyright": "My Company 2024" }, "buffers": [ { "uri": "model.bin", "byteLength": 48312 } ], "textures": [ { "source": 0, "sampler": 0 } ], "images": [ { "uri": "texture.png" } ], "meshes": [ { "primitives": [ { "attributes": { "POSITION": 0, "NORMAL": 1, "TEXCOORD_0": 2 }, "indices": 3, "material": 0 } ] } ], "nodes": [ { "mesh": 0 } ], "materials": [ { "pbrMetallicRoughness": { "baseColorTexture": { "index": 0 } } } ], "accessors": [ { "bufferView": 0, "byteOffset": 0, "componentType": 5126, "count": 1024, "type": "VEC3" }, { "bufferView": 1, "byteOffset": 0, "componentType": 5126, "count": 1024, "type": "VEC3" }, { "bufferView": 2, "byteOffset": 0, "componentType": 5126, "count": 1024, "type": "VEC2" }, { "bufferView": 3, "byteOffset": 0, "componentType": 5123, "count": 3072, "type": "SCALAR" } ], "bufferViews": [ { "buffer": 0, "byteOffset": 0, "byteLength": 12288 }, { "buffer": 0, "byteOffset": 12288, "byteLength": 12288 }, { "buffer": 0, "byteOffset": 24576, "byteLength": 8192 }, { "buffer": 0, "byteOffset": 32768, "byteLength": 6144 } ], "samplers": [ { "magFilter": 9729, "minFilter": 9906, "wrapS": 10497, "wrapT": 10497 } ] }, "separateResources": { "model.bin": "...binary data...", "texture.png": "...image data..." } } ``` ``` -------------------------------- ### Save separate textures CLI Source: https://github.com/cesiumgs/gltf-pipeline/blob/main/README.md Processes a glTF file and saves its textures as separate files using the command-line tool. ```bash gltf-pipeline -i model.gltf -t ``` -------------------------------- ### Convert GLB Buffer to glTF JSON Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Use `glbToGltf` to parse a binary GLB buffer into a glTF JSON object. Set `options.separate` to true to write embedded resources to separate files. ```javascript const { glbToGltf } = require("gltf-pipeline"); const fsExtra = require("fs-extra"); const glb = fsExtra.readFileSync("./model.glb"); // Embedded glTF (all resources as data URIs) glbToGltf(glb) .then(({ gltf }) => { fsExtra.writeJsonSync("./model-embedded.gltf", gltf, { spaces: 2 }); }); // Separate textures and buffers written alongside the .gltf glbToGltf(glb, { separate: true }) .then(({ gltf, separateResources }) => { fsExtra.writeJsonSync("./out/model.gltf", gltf, { spaces: 2 }); for (const [rel, data] of Object.entries(separateResources)) { fsExtra.outputFileSync(`./out/${rel}`, data); } // writes: out/model.gltf, out/model0.bin, out/texture0.png, ... }) .catch((err) => console.error("Conversion failed:", err.message)); ``` -------------------------------- ### Process GLB Buffer to Optimized GLB Buffer Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt The `processGlb` function optimizes a GLB buffer by applying the full gltf-pipeline, including optional Draco compression, and returns a new optimized GLB buffer. It accepts the same options as `processGltf`. ```javascript const { processGlb } = require("gltf-pipeline"); const fsExtra = require("fs-extra"); const inputGlb = fsExtra.readFileSync("./model.glb"); processGlb(inputGlb, { dracoOptions: { compressionLevel: 7, quantizePositionBits: 14, unifiedQuantization: true, // consistent grid across all meshes uncompressedFallback: true, // keeps original mesh for renderers without Draco support }, stats: true, }) .then(({ glb }) => { fsExtra.writeFileSync("./model-optimized.glb", glb); console.log(`Optimized GLB: ${glb.byteLength} bytes`); }) .catch((err) => { console.error("processGlb failed:", err); process.exitCode = 1; }); ``` -------------------------------- ### Convert glTF to glb Library Source: https://github.com/cesiumgs/gltf-pipeline/blob/main/README.md Converts a glTF JSON object to a glb binary buffer using the gltf-pipeline Node.js module. Ensure resource files are accessible via the specified resourceDirectory. ```javascript const gltfPipeline = require("gltf-pipeline"); const fsExtra = require("fs-extra"); const gltfToGlb = gltfPipeline.gltfToGlb; const gltf = fsExtra.readJsonSync("./input/model.gltf"); const options = { resourceDirectory: "./input/" }; gltfToGlb(gltf, options).then(function (results) { fsExtra.writeFileSync("model.glb", results.glb); }); ``` -------------------------------- ### Retrieve Mesh and Resource Statistics for a glTF Asset Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Use getStatistics to retrieve asset complexity. Omit nodeId for full asset statistics, or provide a nodeId for subtree statistics. The Statistics object has a .toString() method for human-readable summaries. ```javascript const { processGltf, getStatistics } = require("gltf-pipeline"); const fsExtra = require("fs-extra"); const gltf = fsExtra.readJsonSync("./model.gltf"); // Get statistics before processing const statsBefore = getStatistics(gltf); console.log("=== Before ==="); console.log(statsBefore.toString()); // Total byte length of all buffers: 102400 bytes // Images: 3 // Draw calls: 8 // Rendered primitives (e.g., triangles): 4096 // Nodes: 12 // Meshes: 5 // Materials: 4 // Animations: 2 // External requests (not data uris): 4 // Per-node statistics (draw calls and primitives only) const nodeStats = getStatistics(gltf, 0); console.log("Node 0 draw calls:", nodeStats.numberOfDrawCalls); // e.g. 2 console.log("Node 0 primitives:", nodeStats.numberOfRenderedPrimitives); // e.g. 512 // Programmatic access to individual fields console.log("Buffer size (bytes):", statsBefore.buffersByteLength); console.log("Number of animations:", statsBefore.numberOfAnimations); console.log("External requests:", statsBefore.numberOfExternalRequests); ``` -------------------------------- ### Convert glb to embedded glTF Library Source: https://github.com/cesiumgs/gltf-pipeline/blob/main/README.md Converts a glb binary buffer to a glTF JSON object using the gltf-pipeline Node.js module. ```javascript const gltfPipeline = require("gltf-pipeline"); const fsExtra = require("fs-extra"); const glbToGltf = gltfPipeline.glbToGltf; const glb = fsExtra.readFileSync("model.glb"); glbToGltf(glb).then(function (results) { fsExtra.writeJsonSync("model.gltf", results.gltf); }); ``` -------------------------------- ### getStatistics(gltf, nodeId?) Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Retrieve mesh and resource statistics for a glTF asset. When nodeId is omitted, full asset-wide statistics are computed. When nodeId is provided, only draw call and rendered primitive counts are computed for the subtree rooted at that node. ```APIDOC ## getStatistics(gltf, nodeId?) ### Description Retrieve mesh and resource statistics for a glTF asset. When `nodeId` is omitted, full asset-wide statistics are computed. When `nodeId` is provided, only draw call and rendered primitive counts are computed for the subtree rooted at that node. The `Statistics` object has a `.toString()` method that produces a human-readable summary. ### Parameters #### Path Parameters - **gltf** (object) - Required - The glTF asset object. - **nodeId** (number) - Optional - The ID of the node for which to compute statistics. ### Request Example ```javascript const { getStatistics } = require("gltf-pipeline"); const fsExtra = require("fs-extra"); const gltf = fsExtra.readJsonSync("./model.gltf"); // Get statistics before processing const statsBefore = getStatistics(gltf); console.log(statsBefore.toString()); // Per-node statistics (draw calls and primitives only) const nodeStats = getStatistics(gltf, 0); console.log("Node 0 draw calls:", nodeStats.numberOfDrawCalls); console.log("Node 0 primitives:", nodeStats.numberOfRenderedPrimitives); // Programmatic access to individual fields console.log("Buffer size (bytes):", statsBefore.buffersByteLength); console.log("Number of animations:", statsBefore.numberOfAnimations); console.log("External requests:", statsBefore.numberOfExternalRequests); ``` ### Response #### Success Response - **Statistics object** - An object containing various statistics about the glTF asset. It has properties like `buffersByteLength`, `numberOfImages`, `numberOfDrawCalls`, `numberOfRenderedPrimitives`, `numberOfNodes`, `numberOfMeshes`, `numberOfMaterials`, `numberOfAnimations`, `numberOfExternalRequests`, and methods like `toString()`. ``` -------------------------------- ### Process glTF JSON with gltf-pipeline Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Use `processGltf` to run a glTF JavaScript object through the complete pipeline. Configure options such as resource directory, embedding/separation of resources, Draco compression settings, and custom pipeline stages. The function returns a Promise that resolves to the processed glTF object and any separate resources. ```javascript const gltfPipeline = require("gltf-pipeline"); const fsExtra = require("fs-extra"); const gltf = fsExtra.readJsonSync("./model.gltf"); const options = { resourceDirectory: "./", // directory to resolve external .bin and texture files separate: false, // embed all buffers, shaders, textures in glTF JSON separateTextures: true, // write textures as external files stats: true, // print before/after statistics to console keepUnusedElements: false, // strip unused nodes, meshes, materials keepLegacyExtensions: false, // convert KHR_techniques_webgl / KHR_materials_common to PBR dracoOptions: { compressionLevel: 7, // 0 (fastest/least) to 10 (slowest/most) quantizePositionBits: 11, // precision bits for vertex positions quantizeNormalBits: 8, quantizeTexcoordBits: 10, quantizeColorBits: 8, quantizeGenericBits: 8, // joints, weights, custom attributes uncompressedFallback: false, // include uncompressed mesh alongside Draco mesh unifiedQuantization: false, // shared quantization grid across all primitives }, // Inject a custom stage function into the pipeline customStages: [ function myCustomStage(gltf, options) { // manipulate gltf in-place; may return a Promise gltf.asset.copyright = "My Company 2024"; }, ], logger: (msg) => process.stderr.write(msg + "\n"), // custom log target baseColorTextureNames: ["u_diffuse", "u_tex"], // hint for 1.0→2.0 PBR upgrade baseColorFactorNames: ["u_diffuse_mat"], }; processGltf(gltf, options) .then(({ gltf: outGltf, separateResources }) => { fsExtra.writeJsonSync("./out/model.gltf", outGltf, { spaces: 2 }); // save each separate texture / buffer file for (const [relativePath, data] of Object.entries(separateResources)) { fsExtra.outputFileSync(`./out/${relativePath}`, data); } console.log("Done."); }) .catch((err) => { console.error("Pipeline error:", err.message); process.exitCode = 1; }); // Console output (stats: true): // Statistics after: // Total byte length of all buffers: 48312 bytes // Images: 2 // Draw calls: 4 // Rendered primitives (e.g., triangles): 1024 // Nodes: 6 // Meshes: 3 // Materials: 2 // Animations: 1 // External requests (not data uris): 2 ``` -------------------------------- ### Convert glTF to Draco glTF Library Source: https://github.com/cesiumgs/gltf-pipeline/blob/main/README.md Processes a glTF object to apply Draco mesh compression using the gltf-pipeline Node.js module. Compression level can be adjusted. ```javascript const gltfPipeline = require("gltf-pipeline"); const fsExtra = require("fs-extra"); const processGltf = gltfPipeline.processGltf; const gltf = fsExtra.readJsonSync("model.gltf"); const options = { dracoOptions: { compressionLevel: 10, }, }; processGltf(gltf, options).then(function (results) { fsExtra.writeJsonSync("model-draco.gltf", results.gltf); }); ``` -------------------------------- ### Convert glTF JSON to GLB Buffer Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Use `gltfToGlb` to convert a glTF JSON object into a binary GLB buffer. Specify `resourceDirectory` for external resources. Draco compression can be enabled via `dracoOptions`. ```javascript const { gltfToGlb } = require("gltf-pipeline"); const fsExtra = require("fs-extra"); const gltf = fsExtra.readJsonSync("./input/model.gltf"); gltfToGlb(gltf, { resourceDirectory: "./input/" }) .then(({ glb }) => { fsExtra.writeFileSync("./output/model.glb", glb); console.log(`GLB written: ${glb.byteLength} bytes`); // GLB written: 204800 bytes }) .catch((err) => console.error(err)); // With Draco compression: gltfToGlb(gltf, { resourceDirectory: "./input/", dracoOptions: { compressionLevel: 10 }, }) .then(({ glb }) => fsExtra.writeFileSync("./output/model-draco.glb", glb)); ``` -------------------------------- ### Save separate textures Library Source: https://github.com/cesiumgs/gltf-pipeline/blob/main/README.md Processes a glTF object to save its textures as separate files using the gltf-pipeline Node.js module. The resulting separate resources are written to disk. ```javascript const gltfPipeline = require("gltf-pipeline"); const fsExtra = require("fs-extra"); const processGltf = gltfPipeline.processGltf; const gltf = fsExtra.readJsonSync("model.gltf"); const options = { separateTextures: true, }; processGltf(gltf, options).then(function (results) { fsExtra.writeJsonSync("model-separate.gltf", results.gltf); // Save separate resources const separateResources = results.separateResources; for (const relativePath in separateResources) { if (separateResources.hasOwnProperty(relativePath)) { const resource = separateResources[relativePath]; fsExtra.writeFileSync(relativePath, resource); } } }); ``` -------------------------------- ### removeExtension(gltf, extension) Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Removes a specified glTF extension from all objects within a glTF asset. It traverses the asset and removes the extension from `extensions`, `extensionsUsed`, and `extensionsRequired` blocks. It also handles special cases like `CESIUM_RTC`. ```APIDOC ## removeExtension(gltf, extension) ### Description Removes a glTF extension from all objects in the asset. Traverses the entire glTF object tree and removes all occurrences of the named extension from `extensions` blocks, `extensionsUsed`, and `extensionsRequired`. Handles `CESIUM_RTC` specially by rewriting affected technique uniform semantics before removal. ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - **gltf** (object) - The glTF JSON object. - **extension** (string) - The name of the extension to remove (e.g., 'KHR_draco_mesh_compression'). ### Request Example ```javascript const { glbToGltf, removeExtension } = require("gltf-pipeline"); const fsExtra = require("fs-extra"); const glb = fsExtra.readFileSync("./model-with-draco.glb"); glbToGltf(glb) .then(({ gltf }) => { console.log("Before:", gltf.extensionsRequired); const removedData = removeExtension(gltf, "KHR_draco_mesh_compression"); console.log("Removed data:", removedData); console.log("After:", gltf.extensionsRequired); removeExtension(gltf, "CESIUM_RTC"); fsExtra.writeJsonSync("./model-clean.gltf", gltf, { spaces: 2 }); }); ``` ### Response #### Success Response (200) - Returns the extension data previously stored at `gltf.extensions[extension]`, or `undefined` if the extension was not found. #### Response Example ```json { "extensionData": { ... } // The data associated with the removed extension } ``` ``` -------------------------------- ### Remove glTF Extension Source: https://context7.com/cesiumgs/gltf-pipeline/llms.txt Use `removeExtension` to strip a specified extension from a glTF object. This function modifies the glTF in place and handles special cases like `CESIUM_RTC` by rewriting semantics. ```javascript const { glbToGltf, removeExtension } = require("gltf-pipeline"); const fsExtra = require("fs-extra"); const glb = fsExtra.readFileSync("./model-with-draco.glb"); glbToGltf(glb) .then(({ gltf }) => { // Inspect which extensions are present console.log("Before:", gltf.extensionsRequired); // Before: [ 'KHR_draco_mesh_compression' ] // Strip the Draco extension (decompressed accessors remain in gltf) const removedData = removeExtension(gltf, "KHR_draco_mesh_compression"); console.log("Removed data:", removedData); // extension object or undefined console.log("After:", gltf.extensionsRequired); // After: [] // Also works for CESIUM_RTC — rewrites CESIUM_RTC_MODELVIEW → MODELVIEW removeExtension(gltf, "CESIUM_RTC"); fsExtra.writeJsonSync("./model-clean.gltf", gltf, { spaces: 2 }); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.