Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up --formats and --slots inputs for texture compression #1150

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
PaletteOptions,
PALETTE_DEFAULTS,
MESHOPT_DEFAULTS,
TEXTURE_COMPRESS_SUPPORTED_FORMATS,
} from '@gltf-transform/functions';
import { inspect } from './inspect.js';
import {
Expand All @@ -66,7 +67,7 @@ import {
XMPOptions,
xmp,
} from './transforms/index.js';
import { formatBytes, MICROMATCH_OPTIONS, underline, TableFormat, dim } from './util.js';
import { formatBytes, MICROMATCH_OPTIONS, underline, TableFormat, dim, regexFromArray } from './util.js';
import { Session } from './session.js';
import { ValidateOptions, validate } from './validate.js';
import { getConfig, loadConfig } from './config.js';
Expand Down Expand Up @@ -352,7 +353,10 @@ commands or using the scripting API.

// Texture compression.
if (opts.textureCompress === 'ktx2') {
const slotsUASTC = '{normalTexture,occlusionTexture,metallicRoughnessTexture}';
const slotsUASTC = micromatch.makeRe(
'{normalTexture,occlusionTexture,metallicRoughnessTexture}',
MICROMATCH_OPTIONS,
);
transforms.push(
toktx({ mode: Mode.UASTC, slots: slotsUASTC, level: 4, rdo: 4, zstd: 18 }),
toktx({ mode: Mode.ETC1S, quality: 255 }),
Expand Down Expand Up @@ -1417,8 +1421,8 @@ program
.option('--pattern <pattern>', 'Pattern (glob) to match textures, by name or URI.', {
validator: Validator.STRING,
})
.option('--formats <formats>', 'Texture formats to include (glob)', {
validator: ['image/png', 'image/jpeg', '*'],
.option('--formats <formats>', 'Texture formats to include', {
validator: [...TEXTURE_COMPRESS_SUPPORTED_FORMATS, '*'],
default: '*',
})
.option('--slots <slots>', 'Texture slots to include (glob)', { validator: Validator.STRING, default: '*' })
Expand All @@ -1427,7 +1431,7 @@ program
.option('--lossless <lossless>', 'Use lossless compression mode', { validator: Validator.BOOLEAN, default: false })
.action(async ({ args, options, logger }) => {
const pattern = options.pattern ? micromatch.makeRe(String(options.pattern), MICROMATCH_OPTIONS) : null;
const formats = micromatch.makeRe(String(options.formats), MICROMATCH_OPTIONS);
const formats = regexFromArray([options.formats] as string[]);
const slots = micromatch.makeRe(String(options.slots), MICROMATCH_OPTIONS);
const { default: encoder } = await import('sharp');
return Session.create(io, logger, args.input, args.output).transform(
Expand All @@ -1454,8 +1458,8 @@ program
.option('--pattern <pattern>', 'Pattern (glob) to match textures, by name or URI.', {
validator: Validator.STRING,
})
.option('--formats <formats>', 'Texture formats to include (glob)', {
validator: ['image/png', 'image/jpeg', '*'],
.option('--formats <formats>', 'Texture formats to include', {
validator: [...TEXTURE_COMPRESS_SUPPORTED_FORMATS, '*'],
default: '*',
})
.option('--slots <slots>', 'Texture slots to include (glob)', { validator: Validator.STRING, default: '*' })
Expand All @@ -1468,7 +1472,7 @@ program
})
.action(async ({ args, options, logger }) => {
const pattern = options.pattern ? micromatch.makeRe(String(options.pattern), MICROMATCH_OPTIONS) : null;
const formats = micromatch.makeRe(String(options.formats), MICROMATCH_OPTIONS);
const formats = regexFromArray([options.formats] as string[]);
const slots = micromatch.makeRe(String(options.slots), MICROMATCH_OPTIONS);
const { default: encoder } = await import('sharp');
return Session.create(io, logger, args.input, args.output).transform(
Expand Down Expand Up @@ -1496,16 +1500,16 @@ program
.option('--pattern <pattern>', 'Pattern (glob) to match textures, by name or URI.', {
validator: Validator.STRING,
})
.option('--formats <formats>', 'Texture formats to include (glob)', {
validator: ['image/png', 'image/jpeg', '*'],
default: 'image/png',
.option('--formats <formats>', 'Texture formats to include', {
validator: [...TEXTURE_COMPRESS_SUPPORTED_FORMATS, '*'],
default: 'png',
})
.option('--slots <slots>', 'Texture slots to include (glob)', { validator: Validator.STRING, default: '*' })
.option('--quality <quality>', 'Quality, 1-100', { validator: Validator.NUMBER })
.option('--effort <effort>', 'Level of CPU effort to reduce file size, 0-100', { validator: Validator.NUMBER })
.action(async ({ args, options, logger }) => {
const pattern = options.pattern ? micromatch.makeRe(String(options.pattern), MICROMATCH_OPTIONS) : null;
const formats = micromatch.makeRe(String(options.formats), MICROMATCH_OPTIONS);
const formats = regexFromArray([options.formats] as string[]);
const slots = micromatch.makeRe(String(options.slots), MICROMATCH_OPTIONS);
const { default: encoder } = await import('sharp');
return Session.create(io, logger, args.input, args.output).transform(
Expand All @@ -1531,15 +1535,15 @@ program
.option('--pattern <pattern>', 'Pattern (glob) to match textures, by name or URI.', {
validator: Validator.STRING,
})
.option('--formats <formats>', 'Texture formats to include (glob)', {
validator: ['image/png', 'image/jpeg', '*'],
default: 'image/jpeg',
.option('--formats <formats>', 'Texture formats to include', {
validator: [...TEXTURE_COMPRESS_SUPPORTED_FORMATS, '*'],
default: 'jpeg',
})
.option('--slots <slots>', 'Texture slots to include (glob)', { validator: Validator.STRING, default: '*' })
.option('--quality <quality>', 'Quality, 1-100', { validator: Validator.NUMBER })
.action(async ({ args, options, logger }) => {
const pattern = options.pattern ? micromatch.makeRe(String(options.pattern), MICROMATCH_OPTIONS) : null;
const formats = micromatch.makeRe(String(options.formats), MICROMATCH_OPTIONS);
const formats = regexFromArray([options.formats] as string[]);
const slots = micromatch.makeRe(String(options.slots), MICROMATCH_OPTIONS);
const { default: encoder } = await import('sharp');
return Session.create(io, logger, args.input, args.output).transform(
Expand Down
7 changes: 7 additions & 0 deletions packages/cli/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import _commandExists from 'command-exists';
import CLITable from 'cli-table3';
import { stringify } from 'csv-stringify';
import stripAnsi from 'strip-ansi';
import micromatch from 'micromatch';

// Constants.

Expand All @@ -19,6 +20,12 @@ export const XMPContext: Record<string, string> = {
// minimatch. Need to ensure that '*' matches patterns like 'image/png'.
export const MICROMATCH_OPTIONS = { nocase: true, contains: true };

// See: https://github.com/micromatch/micromatch/issues/224
export function regexFromArray(values: string[]): RegExp {
const pattern = values.map((s) => `(${s})`).join('|');
return micromatch.makeRe(pattern, MICROMATCH_OPTIONS);
}

// Mocks for tests.

export let spawn = _spawn;
Expand Down
6 changes: 3 additions & 3 deletions packages/functions/src/texture-compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { lanczos2, lanczos3 } from 'ndarray-lanczos';

const NAME = 'textureCompress';

type Format = (typeof FORMATS)[number];
const FORMATS = ['jpeg', 'png', 'webp', 'avif'] as const;
type Format = (typeof TEXTURE_COMPRESS_SUPPORTED_FORMATS)[number];
export const TEXTURE_COMPRESS_SUPPORTED_FORMATS = ['jpeg', 'png', 'webp', 'avif'] as const;
const SUPPORTED_MIME_TYPES = ['image/jpeg', 'image/png', 'image/webp', 'image/avif'];

export interface TextureCompressOptions {
Expand Down Expand Up @@ -339,7 +339,7 @@ function getFormat(texture: Texture): Format {

function getFormatFromMimeType(mimeType: string): Format {
const format = mimeType.split('/').pop() as Format | undefined;
if (!format || !FORMATS.includes(format)) {
if (!format || !TEXTURE_COMPRESS_SUPPORTED_FORMATS.includes(format)) {
throw new Error(`Unknown MIME type "${mimeType}".`);
}
return format;
Expand Down