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

Allow transform stages to detect later stages #417

Merged
merged 2 commits into from
Nov 8, 2021
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
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
packages/functions/src/**
packages/cli/src/**
**/dist
**/in
**/out
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @module core */

export { Document, Transform } from './document';
export { Document, Transform, TransformContext } from './document';
export { JSONDocument } from './json-document';
export { Extension } from './extension';
export {
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import {
} from './properties';
import { Logger } from './utils';

export type Transform = (doc: Document) => void;
export interface TransformContext {
stack: string[];
}

export type Transform = (doc: Document, context?: TransformContext) => void;

/**
* # Document
Expand Down Expand Up @@ -190,8 +194,9 @@ export class Document {
* @param transforms List of synchronous transformation functions to apply.
*/
public async transform(...transforms: Transform[]): Promise<this> {
const stack = transforms.map((fn) => fn.name);
for (const transform of transforms) {
await transform(this);
await transform(this, { stack });
}
return this;
}
Expand Down
7 changes: 4 additions & 3 deletions packages/functions/src/center.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Document, Transform, vec3 } from '@gltf-transform/core';
import { bounds } from '@gltf-transform/core';
import { createTransform } from './utils';

const NAME = 'center';

Expand All @@ -24,7 +25,7 @@ const CENTER_DEFAULTS: Required<CenterOptions> = {pivot: 'center'};
export function center (_options: CenterOptions = CENTER_DEFAULTS): Transform {
const options = {...CENTER_DEFAULTS, ..._options} as Required<CenterOptions>;

return (doc: Document): void => {
return createTransform(NAME, (doc: Document): void => {
const logger = doc.getLogger();
const root = doc.getRoot();
const isAnimated = root.listAnimations().length > 0 || root.listSkins().length > 0;
Expand Down Expand Up @@ -69,6 +70,6 @@ export function center (_options: CenterOptions = CENTER_DEFAULTS): Transform {
});

logger.debug(`${NAME}: Complete.`);
};
});

}
};
7 changes: 4 additions & 3 deletions packages/functions/src/colorspace.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Accessor, Document, Primitive, Transform, vec3 } from '@gltf-transform/core';
import { createTransform } from './utils';

const NAME = 'colorspace';

Expand All @@ -15,7 +16,7 @@ export interface ColorspaceOptions {
*/
export function colorspace (options: ColorspaceOptions): Transform {

return (doc: Document): void => {
return createTransform(NAME, (doc: Document): void => {

const logger = doc.getLogger();

Expand Down Expand Up @@ -67,6 +68,6 @@ export function colorspace (options: ColorspaceOptions): Transform {

logger.debug(`${NAME}: Complete.`);

};
});

}
};
5 changes: 3 additions & 2 deletions packages/functions/src/dedup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Accessor, BufferUtils, Document, Logger, Material, Mesh, Primitive, PrimitiveTarget, PropertyType, Root, Texture, Transform } from '@gltf-transform/core';
import { createTransform } from './utils';

const NAME = 'dedup';

Expand Down Expand Up @@ -42,7 +43,7 @@ export const dedup = function (_options: DedupOptions = DEDUP_DEFAULTS): Transfo
}
}

return (doc: Document): void => {
return createTransform(NAME, (doc: Document): void => {
const logger = doc.getLogger();

if (propertyTypes.has(PropertyType.ACCESSOR)) dedupAccessors(logger, doc);
Expand All @@ -51,7 +52,7 @@ export const dedup = function (_options: DedupOptions = DEDUP_DEFAULTS): Transfo
if (propertyTypes.has(PropertyType.MATERIAL)) dedupMaterials(logger, doc);

logger.debug(`${NAME}: Complete.`);
};
});

};

Expand Down
5 changes: 3 additions & 2 deletions packages/functions/src/instance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Document, Logger, MathUtils, Mesh, Node, Transform, vec3, vec4 } from '@gltf-transform/core';
import { InstancedMesh, MeshGPUInstancing } from '@gltf-transform/extensions';
import { createTransform } from './utils';

const NAME = 'instance';

Expand All @@ -16,7 +17,7 @@ export function instance (_options: InstanceOptions = INSTANCE_DEFAULTS): Transf
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const options = {...INSTANCE_DEFAULTS, ..._options} as Required<InstanceOptions>;

return (doc: Document): void => {
return createTransform(NAME, (doc: Document): void => {
const logger = doc.getLogger();
const root = doc.getRoot();
const batchExtension = doc.createExtension(MeshGPUInstancing);
Expand Down Expand Up @@ -97,7 +98,7 @@ export function instance (_options: InstanceOptions = INSTANCE_DEFAULTS): Transf
}

logger.debug(`${NAME}: Complete.`);
};
});

}

Expand Down
6 changes: 3 additions & 3 deletions packages/functions/src/metal-rough.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Document, Texture, Transform } from '@gltf-transform/core';
import { MaterialsIOR, MaterialsPBRSpecularGlossiness, MaterialsSpecular, PBRSpecularGlossiness } from '@gltf-transform/extensions';
import { rewriteTexture } from './utils';
import { createTransform, rewriteTexture } from './utils';

const NAME = 'metalRough';

Expand All @@ -21,7 +21,7 @@ export function metalRough (_options: MetalRoughOptions = METALROUGH_DEFAULTS):
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const options = {...METALROUGH_DEFAULTS, ..._options} as Required<MetalRoughOptions>;

return async (doc: Document): Promise<void> => {
return createTransform(NAME, async (doc: Document): Promise<void> => {

const logger = doc.getLogger();

Expand Down Expand Up @@ -117,6 +117,6 @@ export function metalRough (_options: MetalRoughOptions = METALROUGH_DEFAULTS):

logger.debug(`${NAME}: Complete.`);

};
});

}
5 changes: 3 additions & 2 deletions packages/functions/src/partition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Document, Logger, PropertyType, Transform } from '@gltf-transform/core';
import { prune } from './prune';
import { createTransform } from './utils';

const NAME = 'partition';

Expand Down Expand Up @@ -32,7 +33,7 @@ const partition = (_options: PartitionOptions = PARTITION_DEFAULTS): Transform =

const options = {...PARTITION_DEFAULTS, ..._options} as Required<PartitionOptions>;

return async (doc: Document): Promise<void> => {
return createTransform(NAME, async (doc: Document): Promise<void> => {
const logger = doc.getLogger();

if (options.meshes !== false) partitionMeshes(doc, logger, options);
Expand All @@ -45,7 +46,7 @@ const partition = (_options: PartitionOptions = PARTITION_DEFAULTS): Transform =
await doc.transform(prune({propertyTypes: [PropertyType.BUFFER]}));

logger.debug(`${NAME}: Complete.`);
};
});

};

Expand Down
5 changes: 3 additions & 2 deletions packages/functions/src/prune.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AnimationChannel, Document, Graph, Property, PropertyType, Root, Transform } from '@gltf-transform/core';
import { createTransform } from './utils';

const NAME = 'prune';

Expand Down Expand Up @@ -44,7 +45,7 @@ export const prune = function (_options: PruneOptions = PRUNE_DEFAULTS): Transfo
const options = {...PRUNE_DEFAULTS, ..._options} as Required<PruneOptions>;
const propertyTypes = options.propertyTypes;

return (doc: Document): void => {
return createTransform(NAME, (doc: Document): void => {
const logger = doc.getLogger();
const root = doc.getRoot();
const graph = doc.getGraph();
Expand Down Expand Up @@ -139,6 +140,6 @@ export const prune = function (_options: PruneOptions = PRUNE_DEFAULTS): Transfo
disposed[prop.propertyType]++;
}

};
});

};
5 changes: 3 additions & 2 deletions packages/functions/src/quantize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { fromRotationTranslationScale, fromScaling, invert, multiply as multiply
import { max, min, scale, transformMat4 } from 'gl-matrix/vec3';
import { MeshQuantization } from '@gltf-transform/extensions';
import { prune } from './prune';
import { createTransform } from './utils';

const NAME = 'quantize';

Expand Down Expand Up @@ -80,7 +81,7 @@ export const QUANTIZE_DEFAULTS: Required<QuantizeOptions> = {
const quantize = (_options: QuantizeOptions = QUANTIZE_DEFAULTS): Transform => {
const options = { ...QUANTIZE_DEFAULTS, ..._options } as Required<QuantizeOptions>;

return async (doc: Document): Promise<void> => {
return createTransform(NAME, async (doc: Document): Promise<void> => {
const logger = doc.getLogger();
const root = doc.getRoot();

Expand Down Expand Up @@ -116,7 +117,7 @@ const quantize = (_options: QuantizeOptions = QUANTIZE_DEFAULTS): Transform => {
);

logger.debug(`${NAME}: Complete.`);
};
});
};

function quantizePrimitive(
Expand Down
6 changes: 3 additions & 3 deletions packages/functions/src/reorder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Accessor, Document, GLTF, Primitive, PropertyType, Transform } from '@gltf-transform/core';
import { prune } from './prune';
import { SetMap } from './utils';
import { createTransform, SetMap } from './utils';
import type { MeshoptEncoder } from 'meshoptimizer';

const NAME = 'reorder';
Expand Down Expand Up @@ -46,7 +46,7 @@ export function reorder (_options: ReorderOptions = REORDER_DEFAULTS): Transform
const options = {...REORDER_DEFAULTS, ..._options} as Required<ReorderOptions>;
const encoder = options.encoder;

return async (doc: Document): Promise<void> => {
return createTransform(NAME, async (doc: Document): Promise<void> => {
const logger = doc.getLogger();

await encoder.ready;
Expand Down Expand Up @@ -95,7 +95,7 @@ export function reorder (_options: ReorderOptions = REORDER_DEFAULTS): Transform
} else {
logger.debug(`${NAME}: Complete.`);
}
};
});
}

function remapAttribute(attribute: Accessor, remap: Uint32Array, dstCount: number) {
Expand Down
9 changes: 5 additions & 4 deletions packages/functions/src/resample.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Accessor, AnimationSampler, Document, Root, Transform } from '@gltf-transform/core';
import { Accessor, AnimationSampler, Document, Root, Transform, TransformContext } from '@gltf-transform/core';
import { createTransform, isTransformPending } from './utils';

const NAME = 'resample';

Expand All @@ -17,7 +18,7 @@ export const resample = (_options: ResampleOptions = RESAMPLE_DEFAULTS): Transfo

const options = {...RESAMPLE_DEFAULTS, ..._options} as Required<ResampleOptions>;

return (doc: Document): void => {
return createTransform(NAME, (doc: Document, context?: TransformContext): void => {
const accessorsVisited = new Set<Accessor>();
const accessorsCountPrev = doc.getRoot().listAccessors().length;
const logger = doc.getLogger();
Expand Down Expand Up @@ -52,7 +53,7 @@ export const resample = (_options: ResampleOptions = RESAMPLE_DEFAULTS): Transfo
if (!used) accessor.dispose();
}

if (doc.getRoot().listAccessors().length > accessorsCountPrev) {
if (doc.getRoot().listAccessors().length > accessorsCountPrev && !isTransformPending(context, NAME, 'dedup')) {
logger.warn(
`${NAME}: Resampling required copying accessors, some of which may be duplicates.`
+ ' Consider using "dedup" to consolidate any duplicates.'
Expand All @@ -64,7 +65,7 @@ export const resample = (_options: ResampleOptions = RESAMPLE_DEFAULTS): Transfo
}

logger.debug(`${NAME}: Complete.`);
};
});

};

Expand Down
5 changes: 3 additions & 2 deletions packages/functions/src/sequence.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Accessor, AnimationChannel, AnimationSampler, Document, Transform } from '@gltf-transform/core';
import { createTransform } from './utils';

const NAME = 'sequence';

Expand Down Expand Up @@ -26,7 +27,7 @@ const SEQUENCE_DEFAULTS: Required<SequenceOptions> = {
export function sequence (_options: SequenceOptions = SEQUENCE_DEFAULTS): Transform {
const options = {...SEQUENCE_DEFAULTS, ..._options} as Required<SequenceOptions>;

return (doc: Document): void => {
return createTransform(NAME, (doc: Document): void => {

const logger = doc.getLogger();
const root = doc.getRoot();
Expand Down Expand Up @@ -80,6 +81,6 @@ export function sequence (_options: SequenceOptions = SEQUENCE_DEFAULTS): Transf

logger.debug(`${NAME}: Complete.`);

};
});

}
5 changes: 3 additions & 2 deletions packages/functions/src/tangents.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Accessor, Document, Logger, Primitive, Transform, TypedArray, uuid } from '@gltf-transform/core';
import { createTransform } from './utils';

const NAME = 'tangents';

Expand Down Expand Up @@ -42,7 +43,7 @@ export function tangents (_options: TangentsOptions = TANGENTS_DEFAULTS): Transf

const options = {...TANGENTS_DEFAULTS, ..._options} as Required<TangentsOptions>;

return (doc: Document): void => {
return createTransform(NAME, (doc: Document): void => {
const logger = doc.getLogger();
const attributeIDs = new Map<TypedArray, string>();
const tangentCache = new Map<string, Accessor>();
Expand Down Expand Up @@ -117,7 +118,7 @@ export function tangents (_options: TangentsOptions = TANGENTS_DEFAULTS): Transf
} else {
logger.debug(`${NAME}: Complete.`);
}
};
});
}

function getNormalTexcoord(prim: Primitive): string {
Expand Down
5 changes: 3 additions & 2 deletions packages/functions/src/texture-resize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ndarray from 'ndarray';
import { lanczos2, lanczos3 } from 'ndarray-lanczos';
import { getPixels, savePixels } from 'ndarray-pixels';
import { Document, Transform, vec2 } from '@gltf-transform/core';
import { createTransform } from './utils';

const NAME = 'textureResize';

Expand Down Expand Up @@ -40,7 +41,7 @@ export const TEXTURE_RESIZE_DEFAULTS: TextureResizeOptions = {
export function textureResize(_options: TextureResizeOptions = TEXTURE_RESIZE_DEFAULTS): Transform {
const options = {...TEXTURE_RESIZE_DEFAULTS, ..._options} as Required<TextureResizeOptions>;

return async (doc: Document): Promise<void> => {
return createTransform(NAME, async (doc: Document): Promise<void> => {

const logger = doc.getLogger();

Expand Down Expand Up @@ -105,6 +106,6 @@ export function textureResize(_options: TextureResizeOptions = TEXTURE_RESIZE_DE

logger.debug(`${NAME}: Complete.`);

};
});

}
5 changes: 3 additions & 2 deletions packages/functions/src/unweld.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Accessor, Document, Logger, Transform, TypedArray } from '@gltf-transform/core';
import { createTransform } from './utils';

const NAME = 'unweld';

Expand All @@ -19,7 +20,7 @@ export function unweld (_options: UnweldOptions = UNWELD_DEFAULTS): Transform {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const options = {...UNWELD_DEFAULTS, ..._options} as Required<UnweldOptions>;

return (doc: Document): void => {
return createTransform(NAME, (doc: Document): void => {

const logger = doc.getLogger();
const visited = new Map<Accessor, Map<Accessor, Accessor>>();
Expand Down Expand Up @@ -60,7 +61,7 @@ export function unweld (_options: UnweldOptions = UNWELD_DEFAULTS): Transform {
}

logger.debug(`${NAME}: Complete.`);
};
});
}

function unweldAttribute(
Expand Down
Loading