Skip to content

Commit

Permalink
Move unlit(), meshopt(), and draco() to /functions package (donmccurd…
Browse files Browse the repository at this point in the history
…y#544)

* [functions] Move unlit() to /functions
* [functions] Move meshopt() to /functions
* [functions] Move draco() to /functions
  • Loading branch information
donmccurdy authored Apr 7, 2022
1 parent 7ac7aa6 commit 1b32fe0
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 35 deletions.
8 changes: 4 additions & 4 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { gzip } from 'node-gzip';
import { program } from '@caporal/core';
import { Logger, NodeIO, PropertyType, VertexLayout, vec2 } from '@gltf-transform/core';
import { ALL_EXTENSIONS } from '@gltf-transform/extensions';
import { CenterOptions, InstanceOptions, PartitionOptions, PruneOptions, QUANTIZE_DEFAULTS, ResampleOptions, SequenceOptions, TEXTURE_RESIZE_DEFAULTS, TextureResizeFilter, UnweldOptions, WeldOptions, center, dedup, instance, metalRough, partition, prune, quantize, resample, sequence, tangents, textureResize, unweld, weld, reorder, dequantize, oxipng, mozjpeg, webp } from '@gltf-transform/functions';
import { CenterOptions, InstanceOptions, PartitionOptions, PruneOptions, QUANTIZE_DEFAULTS, ResampleOptions, SequenceOptions, TEXTURE_RESIZE_DEFAULTS, TextureResizeFilter, UnweldOptions, WeldOptions, center, dedup, instance, metalRough, partition, prune, quantize, resample, sequence, tangents, textureResize, unweld, weld, reorder, dequantize, oxipng, mozjpeg, webp, unlit, meshopt, MeshoptOptions, DRACO_DEFAULTS, draco, DracoOptions } from '@gltf-transform/functions';
import { InspectFormat, inspect } from './inspect';
import { DRACO_DEFAULTS, DracoCLIOptions, ETC1S_DEFAULTS, Filter, Mode, UASTC_DEFAULTS, draco, ktxfix, merge, toktx, unlit, meshopt, MeshoptCLIOptions, XMPOptions, xmp } from './transforms';
import { ETC1S_DEFAULTS, Filter, Mode, UASTC_DEFAULTS, ktxfix, merge, toktx, XMPOptions, xmp } from './transforms';
import { formatBytes, MICROMATCH_OPTIONS, underline } from './util';
import { Session } from './session';
import { ValidateOptions, validate } from './validate';
Expand Down Expand Up @@ -421,7 +421,7 @@ ${underline('References')}
.action(({args, options, logger}) =>
// Include a lossless weld — Draco requires indices.
Session.create(io, logger, args.input, args.output)
.transform(weld({tolerance: 0}), draco(options as unknown as DracoCLIOptions))
.transform(weld({tolerance: 0}), draco(options as unknown as DracoOptions))
);

// MESHOPT
Expand Down Expand Up @@ -452,7 +452,7 @@ ${underline('References')}
})
.action(({args, options, logger}) =>
Session.create(io, logger, args.input, args.output)
.transform(meshopt(options as unknown as MeshoptCLIOptions))
.transform(meshopt(options as unknown as MeshoptOptions))
);

// QUANTIZE
Expand Down
3 changes: 0 additions & 3 deletions packages/cli/src/transforms/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
export * from './draco';
export * from './ktxfix';
export * from './merge';
export * from './meshopt';
export * from './toktx';
export * from './unlit';
export * from './xmp';
20 changes: 1 addition & 19 deletions packages/cli/test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fs from 'fs';
import test from 'tape';
import tmp from 'tmp';
import { Document, FileUtils, NodeIO } from '@gltf-transform/core';
import { draco, program, programReady, unlit } from '../';
import { program, programReady } from '../';

tmp.setGracefulCleanup();

Expand Down Expand Up @@ -100,21 +100,3 @@ test('@gltf-transform/cli::merge', async (t) => {
})
);
});

test('@gltf-transform/cli::draco', async (t) => {
const doc = new Document();
await doc.transform(draco({ method: 'edgebreaker' }));
await doc.transform(draco({ method: 'sequential' }));
const dracoExtension = doc.getRoot().listExtensionsUsed()[0];
t.equals(dracoExtension.extensionName, 'KHR_draco_mesh_compression', 'adds extension');
t.end();
});

test('@gltf-transform/cli::unlit', async (t) => {
const doc = new Document();
doc.createMaterial();
await doc.transform(unlit());
const unlitExtension = doc.getRoot().listExtensionsUsed()[0];
t.equals(unlitExtension.extensionName, 'KHR_materials_unlit', 'adds extension');
t.end();
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Document, Transform } from '@gltf-transform/core';
import { DracoMeshCompression } from '@gltf-transform/extensions';

export interface DracoCLIOptions {
export interface DracoOptions {
method?: 'edgebreaker' | 'sequential';
encodeSpeed?: number;
decodeSpeed?: number;
Expand All @@ -13,7 +13,7 @@ export interface DracoCLIOptions {
quantizationVolume?: 'mesh' | 'scene';
}

export const DRACO_DEFAULTS: DracoCLIOptions = {
export const DRACO_DEFAULTS: DracoOptions = {
method: 'edgebreaker',
encodeSpeed: 5,
decodeSpeed: 5,
Expand All @@ -25,8 +25,14 @@ export const DRACO_DEFAULTS: DracoCLIOptions = {
quantizationVolume: 'mesh',
};

export const draco = (_options: DracoCLIOptions): Transform => {
const options = { ...DRACO_DEFAULTS, ..._options } as Required<DracoCLIOptions>;
/**
* Applies Draco compression using {@link DracoMeshCompression KHR_draco_mesh_compression}.
* This type of compression can reduce the size of triangle geometry.
*
* This function is a thin wrapper around the {@link DracoMeshCompression} extension itself.
*/
export const draco = (_options: DracoOptions): Transform => {
const options = { ...DRACO_DEFAULTS, ..._options } as Required<DracoOptions>;
return (doc: Document): void => {
doc.createExtension(DracoMeshCompression)
.setRequired(true)
Expand Down
3 changes: 3 additions & 0 deletions packages/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ export * from './center';
export * from './colorspace';
export * from './dedup';
export * from './dequantize';
export * from './draco';
export * from './inspect';
export * from './instance';
export * from './meshopt';
export * from './metal-rough';
export * from './normals';
export * from './partition';
Expand All @@ -55,6 +57,7 @@ export * from './sequence';
export * from './squoosh';
export * from './tangents';
export * from './texture-resize';
export * from './unlit';
export * from './unweld';
export * from './weld';
export * from './list-texture-channels';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@ import { MeshoptCompression } from '@gltf-transform/extensions';
import { reorder, quantize } from '@gltf-transform/functions';
import { MeshoptEncoder } from 'meshoptimizer';

export interface MeshoptCLIOptions {
export interface MeshoptOptions {
level?: 'medium' | 'high';
}
export const MESHOPT_DEFAULTS: Required<MeshoptCLIOptions> = { level: 'high' };
export const MESHOPT_DEFAULTS: Required<MeshoptOptions> = { level: 'high' };

export const meshopt = (_options: MeshoptCLIOptions): Transform => {
const options = { ...MESHOPT_DEFAULTS, ..._options } as Required<MeshoptCLIOptions>;
/**
* Applies Meshopt compression using {@link MeshoptCompression EXT_meshopt_compression}.
* This type of compression can reduce the size of point, line, and triangle geometry,
* morph targets, and animation data.
*
* This function is a thin wrapper around {@link reorder}, {@link quantize}, and
* {@link MeshoptCompression}, and exposes relatively few configuration options.
* To access more options (like quantization bits) direct use of the underlying
* functions is recommended.
*/
export const meshopt = (_options: MeshoptOptions): Transform => {
const options = { ...MESHOPT_DEFAULTS, ..._options } as Required<MeshoptOptions>;
return async (document: Document): Promise<void> => {
await document.transform(
reorder({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Document, Transform } from '@gltf-transform/core';
import type { Document, Transform } from '@gltf-transform/core';
import { MaterialsUnlit } from '@gltf-transform/extensions';

export const unlit = (): Transform => {
Expand Down
12 changes: 12 additions & 0 deletions packages/functions/test/draco.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import test from 'tape';
import { Document } from '@gltf-transform/core';
import { draco } from '../';

test('@gltf-transform/functions::draco', async (t) => {
const document = new Document();
await document.transform(draco({ method: 'edgebreaker' }));
await document.transform(draco({ method: 'sequential' }));
const dracoExtension = document.getRoot().listExtensionsUsed()[0];
t.equals(dracoExtension.extensionName, 'KHR_draco_mesh_compression', 'adds extension');
t.end();
});
12 changes: 12 additions & 0 deletions packages/functions/test/unlit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import test from 'tape';
import { Document } from '@gltf-transform/core';
import { unlit } from '../';

test('@gltf-transform/functions::unlit', async (t) => {
const document = new Document();
document.createMaterial();
await document.transform(unlit());
const unlitExtension = document.getRoot().listExtensionsUsed()[0];
t.equals(unlitExtension.extensionName, 'KHR_materials_unlit', 'adds extension');
t.end();
});

0 comments on commit 1b32fe0

Please sign in to comment.