Skip to content

Commit

Permalink
fix(cli): optimize ignores --prune-attributes in palette() (#1445)
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy authored Jun 29, 2024
1 parent 23e499f commit f58354d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
11 changes: 10 additions & 1 deletion packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,16 @@ commands or using the scripting API.
const transforms: Transform[] = [dedup()];

if (opts.instance) transforms.push(instance({ min: opts.instanceMin }));
if (opts.palette) transforms.push(palette({ min: opts.paletteMin }));

if (opts.palette) {
transforms.push(
palette({
min: opts.paletteMin,
keepAttributes: !opts.prune || !opts.pruneAttributes,
}),
);
}

if (opts.flatten) transforms.push(flatten());
if (opts.join) transforms.push(join());
if (opts.weld) transforms.push(weld());
Expand Down
25 changes: 17 additions & 8 deletions packages/functions/src/palette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export interface PaletteOptions {
* material values are found, no palettes will be generated. Default: 5.
*/
min?: number;
/**
* Whether to keep unused vertex attributes, such as UVs without an assigned
* texture. If kept, unused UV coordinates may prevent palette texture
* creation. Default: false.
*/
keepAttributes?: boolean;
/**
* Whether to perform cleanup steps after completing the operation. Recommended, and enabled by
* default. Cleanup removes temporary resources created during the operation, but may also remove
Expand All @@ -40,6 +46,7 @@ export interface PaletteOptions {
export const PALETTE_DEFAULTS: Required<PaletteOptions> = {
blockSize: 4,
min: 5,
keepAttributes: false,
cleanup: true,
};

Expand Down Expand Up @@ -88,14 +95,16 @@ export function palette(_options: PaletteOptions = PALETTE_DEFAULTS): Transform
const root = document.getRoot();

// Find and remove unused TEXCOORD_n attributes.
await document.transform(
prune({
propertyTypes: [PropertyType.ACCESSOR],
keepAttributes: false,
keepIndices: true,
keepLeaves: true,
}),
);
if (!options.keepAttributes) {
await document.transform(
prune({
propertyTypes: [PropertyType.ACCESSOR],
keepAttributes: false,
keepIndices: true,
keepLeaves: true,
}),
);
}

const prims = new Set<Primitive>();
const materials = new Set<Material>();
Expand Down
14 changes: 11 additions & 3 deletions packages/functions/src/prune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,23 @@ export const PRUNE_DEFAULTS: Required<PruneOptions> = {
* Example:
*
* ```javascript
* import { PropertyType } from '@gltf-transform/core';
* import { prune } from '@gltf-transform/functions';
*
* document.getRoot().listMaterials(); // → [Material, Material]
*
* await document.transform(prune());
* await document.transform(
* prune({
* propertyTypes: [PropertyType.MATERIAL],
* keepExtras: true
* })
* );
*
* document.getRoot().listMaterials(); // → [Material]
* ```
*
* Use {@link PruneOptions} to control what content should be pruned. For example, you can preserve
* empty objects in the scene hierarchy using the option `keepLeaves`.
* By default, pruning will aggressively remove most unused resources. Use
* {@link PruneOptions} to limit what is considered for pruning.
*
* @category Transforms
*/
Expand Down

0 comments on commit f58354d

Please sign in to comment.