Skip to content

Commit

Permalink
feat(extensions,functions): Allow encoding meshopt-compressed float32…
Browse files Browse the repository at this point in the history
… accessors
  • Loading branch information
donmccurdy committed Oct 11, 2024
1 parent f5c681b commit 8aa84c8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
6 changes: 3 additions & 3 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ commands or using the scripting API.
default: INSTANCE_DEFAULTS.min,
})
.option('--meshopt-level <level>', 'Meshopt compression level.', {
validator: ['medium', 'high'],
validator: ['low', 'medium', 'high'],
default: 'high',
})
.option('--palette <bool>', 'Creates palette textures and merges materials.', {
Expand Down Expand Up @@ -344,7 +344,7 @@ commands or using the scripting API.
const opts = options as {
instance: boolean;
instanceMin: number;
meshoptLevel: 'medium' | 'high';
meshoptLevel: 'low' | 'medium' | 'high';
palette: boolean;
paletteMin: number;
simplify: boolean;
Expand Down Expand Up @@ -859,7 +859,7 @@ ${underline('References')}
.argument('<input>', INPUT_DESC)
.argument('<output>', OUTPUT_DESC)
.option('--level <level>', 'Compression level.', {
validator: ['medium', 'high'],
validator: ['low', 'medium', 'high'],
default: 'high',
})
.option('--quantize-position <bits>', 'Precision for POSITION attributes.', {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { GLTF, TypedArray } from '@gltf-transform/core';

/** @deprecated Use 'filter' boolean instead. */
export enum EncoderMethod {
QUANTIZE = 'quantize',
FILTER = 'filter',
Expand Down
6 changes: 6 additions & 0 deletions packages/extensions/src/ext-meshopt-compression/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ export function getMeshoptFilter(accessor: Accessor, doc: Document): { filter: M
.listParentEdges(accessor)
.filter((edge) => !(edge.getParent() instanceof Root));

// If filtering is enabled but the attribute is not quantized, do not filter,
// which would change the attribute's component type.
if (accessor.getComponentSize() > 2) {
return { filter: MeshoptFilter.NONE };
}

for (const ref of refs) {
const refName = ref.getName();
const refKey = (ref.getAttributes().key || '') as string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ import type { MeshoptEncoder, MeshoptDecoder } from 'meshoptimizer';
const NAME = EXT_MESHOPT_COMPRESSION;

interface EncoderOptions {
/** Whether to use meshopt filters, for more aggressive compression. Overrides 'method'. */
filter?: boolean;
/** @deprecated Use 'filter' instead. */
method?: EncoderMethod;
}

const DEFAULT_ENCODER_OPTIONS: Required<EncoderOptions> = {
filter: false,
method: EncoderMethod.QUANTIZE,
};

Expand Down Expand Up @@ -313,7 +317,7 @@ export class EXTMeshoptCompression extends Extension {
const parentID = context.accessorUsageGroupedByParent.has(usage) ? getParentID(accessor) : null;
const mode = getMeshoptMode(accessor, usage);
const filter =
options.method === EncoderMethod.FILTER
(options.filter || options.method === EncoderMethod.FILTER)
? getMeshoptFilter(accessor, this.document)
: { filter: MeshoptFilter.NONE };
const preparedAccessor = prepareAccessor(accessor, encoder, mode, filter);
Expand Down
26 changes: 13 additions & 13 deletions packages/functions/src/meshopt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { assignDefaults, createTransform } from './utils.js';

export interface MeshoptOptions extends Omit<QuantizeOptions, 'pattern' | 'patternTargets'> {
encoder: unknown;
level?: 'medium' | 'high';
level?: 'low' | 'medium' | 'high';
}

export const MESHOPT_DEFAULTS: Required<Omit<MeshoptOptions, 'encoder'>> = {
Expand Down Expand Up @@ -78,22 +78,22 @@ export function meshopt(_options: MeshoptOptions): Transform {
encoder: encoder,
target: 'size',
}),
quantize({
...options,
pattern,
patternTargets,
quantizeNormal,
}),
);

if (options.level !== 'low') {
await document.transform(
quantize({
...options,
pattern,
patternTargets,
quantizeNormal,
}),
);
}

document
.createExtension(EXTMeshoptCompression)
.setRequired(true)
.setEncoderOptions({
method:
options.level === 'medium'
? EXTMeshoptCompression.EncoderMethod.QUANTIZE
: EXTMeshoptCompression.EncoderMethod.FILTER,
});
.setEncoderOptions({filter: options.level === 'high'});
});
}

0 comments on commit 8aa84c8

Please sign in to comment.