Skip to content

Commit

Permalink
feat: Add options to configure pruning unused vertex attributes and e…
Browse files Browse the repository at this point in the history
…mpty 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.
  • Loading branch information
hichemfantar committed Sep 24, 2024
1 parent 8da0e26 commit b65badb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
4 changes: 4 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 },
Expand Down
10 changes: 6 additions & 4 deletions src/utils/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<instancedMesh args={[${geo}, ${mat}, ${!obj.count ? `${node}.count` : obj.count}]} `
} else {
// Form the object in JSX syntax
Expand Down Expand Up @@ -503,9 +503,11 @@ ${parseExtras(gltf.parser.json.asset && gltf.parser.json.asset.extras)}*/`
hasAnimations ? `const group = ${options.types ? 'React.useRef<THREE.Group>()' : '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' : ''}`
Expand Down
6 changes: 3 additions & 3 deletions src/utils/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ async function transform(file, output, config = {}) {
}

functions.push(
// Weld vertices
weld(),
// Weld vertices
weld()
)

if (config.simplify) {
Expand All @@ -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()
)

Expand Down

0 comments on commit b65badb

Please sign in to comment.