@needle-tools/gltf-build-pipeline 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/CHANGELOG.md +140 -0
  2. package/clear-caches.mjs +4 -0
  3. package/defaults.mjs +12 -0
  4. package/dist/cache/cache.d.ts +5 -0
  5. package/dist/cache/cache.js +82 -0
  6. package/dist/cache/index.d.ts +1 -0
  7. package/dist/cache/index.js +1 -0
  8. package/dist/extensions/NEEDLE_compression_texture/NEEDLE_compression_texture.d.ts +24 -0
  9. package/dist/extensions/NEEDLE_compression_texture/NEEDLE_compression_texture.js +54 -0
  10. package/dist/extensions/NEEDLE_compression_texture/index.d.ts +1 -0
  11. package/dist/extensions/NEEDLE_compression_texture/index.js +1 -0
  12. package/dist/extensions/NEEDLE_mesh_compression/NEEDLE_mesh_compression.d.ts +23 -0
  13. package/dist/extensions/NEEDLE_mesh_compression/NEEDLE_mesh_compression.js +43 -0
  14. package/dist/extensions/NEEDLE_mesh_compression/index.d.ts +1 -0
  15. package/dist/extensions/NEEDLE_mesh_compression/index.js +1 -0
  16. package/dist/extensions/NEEDLE_opaque/NEEDLE_opaque.d.ts +20 -0
  17. package/dist/extensions/NEEDLE_opaque/NEEDLE_opaque.js +699 -0
  18. package/dist/extensions/NEEDLE_opaque/index.d.ts +3 -0
  19. package/dist/extensions/NEEDLE_opaque/index.js +18 -0
  20. package/dist/extensions/NEEDLE_progressive/NEEDLE_progressive.d.ts +23 -0
  21. package/dist/extensions/NEEDLE_progressive/NEEDLE_progressive.js +53 -0
  22. package/dist/extensions/NEEDLE_progressive/index.d.ts +1 -0
  23. package/dist/extensions/NEEDLE_progressive/index.js +1 -0
  24. package/dist/extensions/NEEDLE_progressive_texture_settings/index.d.ts +21 -0
  25. package/dist/extensions/NEEDLE_progressive_texture_settings/index.js +39 -0
  26. package/dist/extensions/index.d.ts +6 -0
  27. package/dist/extensions/index.js +16 -0
  28. package/dist/extensions/utils.d.ts +6 -0
  29. package/dist/extensions/utils.js +72 -0
  30. package/dist/functions/list-texture-slots.d.ts +1 -0
  31. package/dist/functions/list-texture-slots.js +35 -0
  32. package/dist/index.d.ts +3 -0
  33. package/dist/index.js +3 -0
  34. package/dist/transforms/index.d.ts +9 -0
  35. package/dist/transforms/index.js +9 -0
  36. package/dist/transforms/needle_animation_transform.d.ts +2 -0
  37. package/dist/transforms/needle_animation_transform.js +5 -0
  38. package/dist/transforms/needle_asset.d.ts +2 -0
  39. package/dist/transforms/needle_asset.js +28 -0
  40. package/dist/transforms/needle_common.d.ts +11 -0
  41. package/dist/transforms/needle_common.js +1 -0
  42. package/dist/transforms/needle_mesh_transform.d.ts +3 -0
  43. package/dist/transforms/needle_mesh_transform.js +83 -0
  44. package/dist/transforms/needle_progressive.d.ts +34 -0
  45. package/dist/transforms/needle_progressive.js +264 -0
  46. package/dist/transforms/needle_simplify.d.ts +3 -0
  47. package/dist/transforms/needle_simplify.js +76 -0
  48. package/dist/transforms/needle_texture_transform.d.ts +5 -0
  49. package/dist/transforms/needle_texture_transform.js +123 -0
  50. package/dist/transforms/needle_toktx.d.ts +24 -0
  51. package/dist/transforms/needle_toktx.js +144 -0
  52. package/dist/transforms/needle_webp.d.ts +13 -0
  53. package/dist/transforms/needle_webp.js +53 -0
  54. package/dist/transforms/toktx.d.ts +77 -0
  55. package/dist/transforms/toktx.js +230 -0
  56. package/dist/transforms/types/squoosh.d.ts +31 -0
  57. package/dist/transforms/types/squoosh.js +33 -0
  58. package/dist/transforms/util.d.ts +21 -0
  59. package/dist/transforms/util.js +90 -0
  60. package/dist/utils/fileutils.d.ts +2 -0
  61. package/dist/utils/fileutils.js +95 -0
  62. package/dist/utils/index.d.ts +1 -0
  63. package/dist/utils/index.js +1 -0
  64. package/fileutils.mjs +6 -0
  65. package/index.mjs +33 -0
  66. package/make-progressive.mjs +11 -0
  67. package/pack-gltf.mjs +119 -0
  68. package/package.json +60 -0
  69. package/tsconfig.json +15 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,140 @@
1
+ # Changelog
2
+ All notable changes to this package will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
+ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [1.1.3-alpha] - 2023-04-04
8
+ - change: file utils use needle.config.json to find files if no directory was specified
9
+
10
+ ## [1.1.2-alpha] - 2023-04-04
11
+ - change: add generator info
12
+ - change: try catch toktx exception
13
+ - change: improve relative path handling
14
+
15
+ ## [1.1.1-alpha] - 2023-04-04
16
+ - change: use env variable set by npm to resolve relative paths
17
+
18
+ ## [1.1.0-alpha.1] - 2023-04-04
19
+ - add: option to disallow toktx using `no-toktx` commandline parameter
20
+
21
+ ## [1.0.0-alpha] - 2023-04-03
22
+ - update to gltf-transform 3.1.4
23
+
24
+ ## [0.11.2-pre] - 2023-01-30
25
+ - fix: clean up logic for finding toktx to only use one approach
26
+
27
+ ## [0.11.1-pre] - 2023-01-27
28
+ - fix: getting correct gltf transform texture for texture definition
29
+ - fix: opaque extension now also uses getTexture util method
30
+
31
+ ## [0.11.0-pre] - 2023-01-25
32
+ - add: script `transform` does run progressive and gltf-pack
33
+ - change: script `transform:progressive` can now process all files in passed in directory
34
+
35
+ ## [0.10.10-pre] - 2023-01-25
36
+ - add: `NEEDLE_TOKTX` environment variable
37
+ - fix: env variable handling
38
+
39
+ ## [0.10.9-pre] - 2023-01-20
40
+ - fix: set mime-type to KTX2 when using cached texture data that was processed with default toktx settings
41
+
42
+ ## [0.10.8-pre] - 2023-01-18
43
+ - fix: pack-gltf should run toktx with default settings when no compression extension was found in texture
44
+
45
+ ## [0.10.7-pre] - 2023-01-18
46
+ - fix: pack-gltf when invoked with single glb path
47
+
48
+ ## [0.10.6-pre.1] - 2023-01-13
49
+ - fix: support for glbs using old texture pointer format ``textures/`` (new format is ``/textures/``)
50
+
51
+ ## [0.10.5-pre] - 2023-01-12
52
+ - fix: pack-gltf running in sequence again to avoid wasm memory error with many glbs
53
+
54
+ ## [0.10.4-pre] - 2023-01-11
55
+ - change: simplify mesh logs `lockBorder` setting
56
+ - change: pack-gltf now searches glb files in subdirectories
57
+
58
+ ## [0.10.3-pre] - 2023-01-09
59
+ - fix: issue with pack-gltf running out of memory when invoked for directory with many glbs
60
+ - change: await squoosh pool close
61
+ - change: disable extension read-write logs by default
62
+
63
+ ## [0.10.2-pre] - 2023-01-07
64
+ - add: mesh simplification now welds vertices with tiny threshold to be able to simplify disjointed meshes
65
+ - change: progressive loading handling of unknown slot, dont throw for unknown texture slot but instead log warning and proceed without making the texture progressively loaded
66
+ - change: update gltf-transform dependency to 2.5.1
67
+
68
+ ## [0.10.1-pre] - 2023-01-04
69
+ - fix: error in progressive texture resize where `maxSize` could be 0
70
+
71
+ ## [0.10.0-pre] - 2023-01-04
72
+ - add: caching for texture transforms
73
+ - change: use one needle_transform function to handle compressions of textures in one loop instead of mulitple toktx and squoosh transform methods
74
+
75
+ ## [0.9.1-pre] - 2023-01-03
76
+ - add: opaque extension support for primitive extensions to support mesh compression settings passed through when using progressive transformations
77
+
78
+ ## [0.9.0-pre] - 2023-01-03
79
+ - add: ``NEEDLE_mesh_compression`` support for running gltf-transform simplify per primitive
80
+
81
+ ## [0.8.1-pre.1] - 2022-12-14
82
+ - add: quality option to ``NEEDLE_compression_texture`` for webp compression
83
+ - fix: progressive glb should always use name from guid and index
84
+
85
+ ## [0.8.0-pre] - 2022-12-13
86
+ - add: webp compression mode
87
+ - change: progressive texture glbs use guid as filename
88
+ - change: pack-gltf can now take directory as argument
89
+
90
+ ## [0.7.1-pre] - 2022-12-08
91
+ - fix: processing gltf multiple times did cause texture json pointers being resolved wrongly including loosing previously set extensions
92
+
93
+ ## [0.7.0-pre] - 2022-12-05
94
+ - add: make-progressive tool to transform an existing glbs and resize images with high res images being loaded from external glbs
95
+
96
+ ## [0.6.4-pre] - 2022-12-03
97
+ - add: compression mode ``none`` to skip toktx compression
98
+ - fix: issue with texture pointer not finding previously assigned sampler when writing
99
+ - fix: if commandline parsing fails take path containing ``KTX-Software`` as additional toktx search path
100
+
101
+ ## [0.6.3-pre] - 2022-10-27
102
+ - fix: options.file nullreference error
103
+
104
+ ## [0.6.2-pre] - 2022-10-20
105
+ - change: toktx temp folder adjusted, use uuid for shorter names, generate uuid once per file [PR](https://github.com/donmccurdy/glTF-Transform/pull/708)
106
+
107
+ ## [0.6.1-pre] - 2022-10-20
108
+ - fix: NEEDLE_compression_settings havent been passed to toktx anymore
109
+
110
+ ## [0.6.0-pre] - 2022-10-19
111
+ - add: generate toktx textures in ``glTF-transform`` subdirectory, input and output textures having the same random name and are prefixed with the texture index
112
+
113
+ ## [0.5.1-pre] - 2022-10-12
114
+ - fix texture extensions surviving multiple compressions (e.g. compression same glb multiple times)
115
+ - fix cli args when invoked via npm run
116
+
117
+ ## [0.5.0-pre] - 2022-10-11
118
+ - add draco support again
119
+
120
+ ## [0.4.1-pre] - 2022-10-11
121
+ - fix texture extension pass-through
122
+
123
+ ## [0.4.0-pre] - 2022-10-11
124
+ - update toktx to gltf-Transform v2.4.4
125
+
126
+ ## [0.3.0-pre] - 2022-10-06
127
+ - add: handle NEEDLE_compression_texture extension and API to toktx to control cli arguments per texture
128
+
129
+ ## [0.2.2-pre] - 2022-10-03
130
+ - add: allow setting ``--toktxPath`` from commandline on windows
131
+
132
+ ## [0.2.1-pre] - 2022-10-03
133
+ - fix bug where EXT_texture_exr extension would be inserted in all textures
134
+
135
+ ## [0.2.0-pre] - 2022-10-02
136
+ - handle KHR_animation_pointer
137
+ - handle texture json pointers
138
+
139
+ ## [0.1.3-pre] - 2022-09-26
140
+ - initial release
@@ -0,0 +1,4 @@
1
+
2
+ import { clearCache } from './dist/cache/index.js';
3
+
4
+ clearCache();
package/defaults.mjs ADDED
@@ -0,0 +1,12 @@
1
+ import { ALL_EXTENSIONS } from '@gltf-transform/extensions';
2
+ import { ALL_EXTENSIONS as NEEDLE_EXTENSIONS } from "./dist/extensions/NEEDLE_opaque/index.js";
3
+ import { NEEDLE_progressive_texture } from './dist/extensions/NEEDLE_progressive_texture/index.js';
4
+
5
+
6
+ export function registerExtensions(node) {
7
+ node.registerExtensions(ALL_EXTENSIONS);
8
+ node.registerExtensions(NEEDLE_EXTENSIONS);
9
+ node.registerExtensions([
10
+ NEEDLE_progressive_texture
11
+ ])
12
+ }
@@ -0,0 +1,5 @@
1
+ import { ExtensibleProperty } from "@gltf-transform/core";
2
+ export declare function clearCache(): void;
3
+ export declare function addToCache(key: string, bytes: Uint8Array): void;
4
+ export declare function tryGetFromCache(key: string): Uint8Array | null;
5
+ export declare function getKey(property: ExtensibleProperty): string;
@@ -0,0 +1,82 @@
1
+ import { Texture, Primitive } from "@gltf-transform/core";
2
+ import tmp from 'tmp';
3
+ import fs from 'fs';
4
+ import path from 'path';
5
+ export function clearCache() {
6
+ const dir = getCacheDirectory();
7
+ fs.rmSync(dir, { recursive: true });
8
+ console.log(`Cleared cache at ${dir}`);
9
+ }
10
+ export function addToCache(key, bytes) {
11
+ const dir = getCacheDirectory();
12
+ const path = `${dir}/${key}.bin`;
13
+ console.log(`Added ${(bytes.byteLength / 1024).toFixed(1)} KB to cache at ${path}`);
14
+ fs.writeFileSync(path, bytes);
15
+ }
16
+ export function tryGetFromCache(key) {
17
+ const dir = getCacheDirectory();
18
+ const path = `${dir}/${key}.bin`;
19
+ if (fs.existsSync(path)) {
20
+ const bytes = fs.readFileSync(path);
21
+ return bytes;
22
+ }
23
+ return null;
24
+ }
25
+ export function getKey(property) {
26
+ let hash = 0;
27
+ let prefix = "prop";
28
+ const extensions = property.listExtensions();
29
+ const extras = property.getExtras();
30
+ // const slots = listTextureSlots(doc, texture);
31
+ // const channels = getTextureChannelMask(doc, texture);
32
+ hash += hashObject({
33
+ extensions,
34
+ extras,
35
+ });
36
+ if (property instanceof Texture) {
37
+ prefix = "image";
38
+ const image = property.getImage();
39
+ const bytes = image?.buffer;
40
+ if (bytes)
41
+ hash += hashBuffer(bytes, image.byteOffset, image.byteLength);
42
+ }
43
+ else if (property instanceof Primitive) {
44
+ prefix = "prim";
45
+ const indices = property.getIndices();
46
+ const attributes = property.listAttributes();
47
+ const targets = property.listTargets();
48
+ hash += hashObject({
49
+ indices,
50
+ attributes,
51
+ targets
52
+ });
53
+ }
54
+ let key = hash.toString();
55
+ key = `${prefix}-${key}`;
56
+ return key;
57
+ }
58
+ function getCacheDirectory() {
59
+ const tmpDir = path.join(tmp.tmpdir, "Needle", "build-pipeline-cache");
60
+ if (!fs.existsSync(tmpDir)) {
61
+ fs.mkdirSync(tmpDir, { recursive: true });
62
+ }
63
+ return tmpDir;
64
+ }
65
+ function hashObject(obj) {
66
+ const str = JSON.stringify(obj);
67
+ let hash = 0;
68
+ for (let i = 0; i < str.length; i++) {
69
+ hash = ((hash << 5) - hash) + str.charCodeAt(i);
70
+ hash |= 0;
71
+ }
72
+ return hash;
73
+ }
74
+ function hashBuffer(buffer, offset, length) {
75
+ let hash = 0;
76
+ const view = new Uint8Array(buffer, offset, length);
77
+ for (let i = 0; i < view.length; i++) {
78
+ hash = ((hash << 5) - hash) + view[i];
79
+ hash |= 0;
80
+ }
81
+ return hash;
82
+ }
@@ -0,0 +1 @@
1
+ export * from "./cache.js";
@@ -0,0 +1 @@
1
+ export * from "./cache.js";
@@ -0,0 +1,24 @@
1
+ import { Extension, ExtensionProperty, PropertyType, ReaderContext, WriterContext } from "@gltf-transform/core";
2
+ export declare const EXTENSION_NAME = "NEEDLE_compression_texture";
3
+ export declare interface NEEDLE_compression_texture_schema {
4
+ mode: string;
5
+ slot: string;
6
+ /** quality in range of 0 - 1 */
7
+ quality: number;
8
+ maxSize: number;
9
+ }
10
+ export declare class NEEDLE_compression_texture extends Extension {
11
+ static EXTENSION_NAME: string;
12
+ extensionName: string;
13
+ create(toktxInfo: NEEDLE_compression_texture_schema): NEEDLE_compression_texture_property;
14
+ read(context: ReaderContext): this;
15
+ write(context: WriterContext): this;
16
+ }
17
+ export declare class NEEDLE_compression_texture_property extends ExtensionProperty {
18
+ static EXTENSION_NAME: string;
19
+ extensionName: string;
20
+ parentTypes: PropertyType[];
21
+ propertyType: any;
22
+ init(): void;
23
+ model: NEEDLE_compression_texture_schema;
24
+ }
@@ -0,0 +1,54 @@
1
+ import { Extension, ExtensionProperty, PropertyType } from "@gltf-transform/core";
2
+ import { getTextureDefinition } from "../utils.js";
3
+ export const EXTENSION_NAME = "NEEDLE_compression_texture";
4
+ const debugLog = false;
5
+ export class NEEDLE_compression_texture extends Extension {
6
+ static EXTENSION_NAME = EXTENSION_NAME;
7
+ extensionName = EXTENSION_NAME;
8
+ create(toktxInfo) {
9
+ const ext = new NEEDLE_compression_texture_property(this.document.getGraph(), "");
10
+ ext.model = toktxInfo;
11
+ return ext;
12
+ }
13
+ read(context) {
14
+ if (!context.jsonDoc.json.textures)
15
+ return this;
16
+ if (debugLog)
17
+ console.log("READ " + EXTENSION_NAME);
18
+ context.textures?.forEach((tex, _) => {
19
+ const textureDefinition = getTextureDefinition(context, tex);
20
+ if (textureDefinition?.extensions && textureDefinition.extensions[EXTENSION_NAME]) {
21
+ const prop = new NEEDLE_compression_texture_property(this.document.getGraph(), EXTENSION_NAME);
22
+ prop.model = textureDefinition.extensions[EXTENSION_NAME];
23
+ tex.setExtension(EXTENSION_NAME, prop);
24
+ }
25
+ });
26
+ return this;
27
+ }
28
+ write(context) {
29
+ if (debugLog)
30
+ console.log("WRITE " + EXTENSION_NAME);
31
+ const root = this.document.getRoot();
32
+ const textures = root.listTextures();
33
+ for (const tex of textures) {
34
+ const ext = tex.getExtension(EXTENSION_NAME);
35
+ if (ext) {
36
+ const def = getTextureDefinition(context, tex);
37
+ if (def) {
38
+ def.extensions = def.extensions || {};
39
+ def.extensions[EXTENSION_NAME] = ext.model;
40
+ }
41
+ }
42
+ }
43
+ return this;
44
+ }
45
+ }
46
+ export class NEEDLE_compression_texture_property extends ExtensionProperty {
47
+ static EXTENSION_NAME = EXTENSION_NAME;
48
+ init() {
49
+ this.extensionName = EXTENSION_NAME;
50
+ this.parentTypes = [PropertyType.TEXTURE];
51
+ this.propertyType = EXTENSION_NAME;
52
+ }
53
+ model;
54
+ }
@@ -0,0 +1 @@
1
+ export * from "./NEEDLE_compression_texture.js";
@@ -0,0 +1 @@
1
+ export * from "./NEEDLE_compression_texture.js";
@@ -0,0 +1,23 @@
1
+ import { Extension, ExtensionProperty, PropertyType, ReaderContext } from "@gltf-transform/core";
2
+ declare interface NEEDLE_mesh_compression_model {
3
+ useSimplifier: boolean;
4
+ error: number;
5
+ ratio: number;
6
+ lockBorder: boolean;
7
+ }
8
+ export declare class NEEDLE_mesh_compression extends Extension {
9
+ static EXTENSION_NAME: string;
10
+ extensionName: string;
11
+ create(model: NEEDLE_mesh_compression_model): NEEDLE_mesh_compression_property;
12
+ read(context: ReaderContext): this;
13
+ write(ctx: any): this;
14
+ }
15
+ export declare class NEEDLE_mesh_compression_property extends ExtensionProperty {
16
+ static EXTENSION_NAME: string;
17
+ readonly extensionName: string;
18
+ parentTypes: PropertyType[];
19
+ propertyType: string;
20
+ init(): void;
21
+ model: NEEDLE_mesh_compression_model;
22
+ }
23
+ export {};
@@ -0,0 +1,43 @@
1
+ import { Extension, ExtensionProperty, PropertyType } from "@gltf-transform/core";
2
+ const EXTENSION_NAME = "NEEDLE_mesh_compression";
3
+ const debugLog = false;
4
+ export class NEEDLE_mesh_compression extends Extension {
5
+ static EXTENSION_NAME = EXTENSION_NAME;
6
+ extensionName = EXTENSION_NAME;
7
+ create(model) {
8
+ const ext = new NEEDLE_mesh_compression_property(this.document.getGraph(), "");
9
+ ext.model = model;
10
+ return ext;
11
+ }
12
+ read(context) {
13
+ if (!context.jsonDoc.json.meshes)
14
+ return;
15
+ if (debugLog)
16
+ console.log("READ " + EXTENSION_NAME);
17
+ context.meshes?.forEach((mesh, index) => {
18
+ const meshDefinition = context.jsonDoc.json.meshes[index];
19
+ if (!meshDefinition)
20
+ return;
21
+ const primitives = mesh.listPrimitives();
22
+ for (let i = 0; i < primitives.length; i++) {
23
+ const primitive = meshDefinition.primitives[i];
24
+ if (primitive.extensions && primitive.extensions[EXTENSION_NAME]) {
25
+ const model = primitive.extensions[EXTENSION_NAME];
26
+ primitives[i].setExtension(EXTENSION_NAME, this.create(model));
27
+ }
28
+ }
29
+ });
30
+ return this;
31
+ }
32
+ write(ctx) {
33
+ return this;
34
+ }
35
+ }
36
+ export class NEEDLE_mesh_compression_property extends ExtensionProperty {
37
+ static EXTENSION_NAME = EXTENSION_NAME;
38
+ extensionName = EXTENSION_NAME;
39
+ parentTypes = [PropertyType.PRIMITIVE];
40
+ propertyType = EXTENSION_NAME;
41
+ init() { }
42
+ model;
43
+ }
@@ -0,0 +1 @@
1
+ export * from "./NEEDLE_mesh_compression.js";
@@ -0,0 +1 @@
1
+ export * from "./NEEDLE_mesh_compression.js";
@@ -0,0 +1,20 @@
1
+ import { WriterContext, PropertyType } from '@gltf-transform/core';
2
+ declare interface IExtensibleProperty {
3
+ }
4
+ export type Options = {
5
+ debug?: boolean;
6
+ write?: boolean;
7
+ };
8
+ export interface IOpaqueExtension {
9
+ }
10
+ export interface IOpaqueProperty {
11
+ /** used by progressive texture extension to pass an extension between documents */
12
+ getExtensionDefinition(): object;
13
+ }
14
+ export interface IExtensionWriter {
15
+ /** used by progressive texture extension to pass an extension between documents */
16
+ assignTo?(obj: object): any;
17
+ write(context: WriterContext, prop: IExtensibleProperty): void;
18
+ }
19
+ export declare function createOpaqueExtension(name: string, types: PropertyType | PropertyType[], opts?: Options): IOpaqueExtension;
20
+ export {};