From b65badb40a9c34dd82ddd1184afa3af32d37078f Mon Sep 17 00:00:00 2001 From: Hichem Fantar Date: Tue, 24 Sep 2024 18:32:00 +0100 Subject: [PATCH] feat: Add options to configure pruning unused vertex attributes and empty leaf nodes The code changes introduce two new options to the CLI tool: - `--pruneKeepAttributes`: Determines whether to keep unused vertex attributes, such as UVs without an assigned texture. - `--pruneKeepLeaves`: Determines whether to keep empty leaf nodes. These options provide more control over the pruning process, allowing users to optimize the resulting glTF files based on their specific requirements. --- cli.js | 4 ++++ src/utils/parser.js | 10 ++++++---- src/utils/transform.js | 6 +++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/cli.js b/cli.js index 3efdc99..69787bd 100755 --- a/cli.js +++ b/cli.js @@ -33,6 +33,8 @@ const cli = meow( --resolution, -R Resolution for texture resizing (default: 1024) --keepmeshes, -j Do not join compatible meshes --keepmaterials, -M Do not palette join materials + --pruneKeepAttributes, Whether to keep unused vertex attributes, such as UVs without an assigned texture + --pruneKeepLeaves, Whether to keep empty leaf nodes --format, -f Texture format (default: "webp") --simplify, -S Mesh simplification (default: false) --ratio Simplifier ratio (default: 0) @@ -63,6 +65,8 @@ const cli = meow( simplify: { type: 'boolean', shortFlag: 'S', default: false }, keepmeshes: { type: 'boolean', shortFlag: 'j', default: false }, keepmaterials: { type: 'boolean', shortFlag: 'M', default: false }, + pruneKeepAttributes: { type: 'boolean', default: false }, + pruneKeepLeaves: { type: 'boolean', default: false }, format: { type: 'string', shortFlag: 'f', default: 'webp' }, exportdefault: { type: 'boolean', shortFlag: 'E' }, ratio: { type: 'number', default: 0.75 }, diff --git a/src/utils/parser.js b/src/utils/parser.js index 4fc66f3..89d2841 100644 --- a/src/utils/parser.js +++ b/src/utils/parser.js @@ -361,7 +361,7 @@ function parse(gltf, { fileName = 'model', ...options } = {}) { if (obj.isInstancedMesh) { const geo = `${node}.geometry` const mat = obj.material.name ? `materials${sanitizeName(obj.material.name)}` : `${node}.material` - type = "instancedMesh" + type = 'instancedMesh' result = `()' : 'React.useRef()'};` : '' } ${ !options.instanceall - ? `const { ${!hasPrimitives ? `nodes, materials` : 'scene'} ${hasAnimations ? ', animations' : ''}} = useGLTF('${url}'${ - options.draco ? `, ${JSON.stringify(options.draco)}` : '' - })${!hasPrimitives && options.types ? ' as GLTFResult' : ''}${ + ? `const { ${!hasPrimitives ? `nodes, materials` : 'scene'} ${ + hasAnimations ? ', animations' : '' + }} = useGLTF('${url}'${options.draco ? `, ${JSON.stringify(options.draco)}` : ''})${ + !hasPrimitives && options.types ? ' as GLTFResult' : '' + }${ hasPrimitives ? `\nconst clone = React.useMemo(() => SkeletonUtils.clone(scene), [scene]) const { nodes, materials } = useGraph(clone) ${options.types ? ' as GLTFResult' : ''}` diff --git a/src/utils/transform.js b/src/utils/transform.js index 14bcb4e..3d290ce 100644 --- a/src/utils/transform.js +++ b/src/utils/transform.js @@ -58,8 +58,8 @@ async function transform(file, output, config = {}) { } functions.push( - // Weld vertices - weld(), + // Weld vertices + weld() ) if (config.simplify) { @@ -71,7 +71,7 @@ async function transform(file, output, config = {}) { functions.push( resample({ ready: resampleReady, resample: resampleWASM }), - prune({ keepAttributes: false, keepLeaves: false }), + prune({ keepAttributes: config.pruneKeepAttributes ?? false, keepLeaves: config.pruneKeepLeaves ?? false }), sparse() )