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

feat(extensions,functions): Allow encoding meshopt-compressed float32 accessors #1526

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
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 @@ -111,6 +115,8 @@ export class EXTMeshoptCompression extends Extension {
public readonly writeDependencies = ['meshopt.encoder'];

public static readonly EXTENSION_NAME = NAME;

/** @deprecated Use boolean 'filter' option, instead. */
public static readonly EncoderMethod = EncoderMethod;

private _decoder: typeof MeshoptDecoder | null = null;
Expand Down Expand Up @@ -313,7 +319,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'});
});
}
Loading