@needle-tools/gltf-build-pipeline 2.16.0-next.eb5a640 → 3.0.0-alpha

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 (121) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.md +13 -1
  3. package/dist/cli/index.js +4154 -258
  4. package/package.json +9 -14
  5. package/tools/pmrem/pkg/pmrem_wasm.d.ts +25 -0
  6. package/tools/pmrem/pkg/pmrem_wasm.js +110 -0
  7. package/tools/pmrem/pkg/pmrem_wasm_bg.wasm +0 -0
  8. package/dist/cache/cache.d.ts +0 -59
  9. package/dist/cache/cache.js +0 -350
  10. package/dist/cache/index.d.ts +0 -2
  11. package/dist/cache/index.js +0 -2
  12. package/dist/cache/job-cache.d.ts +0 -28
  13. package/dist/cache/job-cache.js +0 -86
  14. package/dist/cli/index.d.ts +0 -2
  15. package/dist/config/index.d.ts +0 -64
  16. package/dist/config/index.js +0 -161
  17. package/dist/constants.d.ts +0 -7
  18. package/dist/constants.js +0 -7
  19. package/dist/extensions/NEEDLE_compression_texture/NEEDLE_compression_texture.d.ts +0 -24
  20. package/dist/extensions/NEEDLE_compression_texture/NEEDLE_compression_texture.js +0 -54
  21. package/dist/extensions/NEEDLE_compression_texture/index.d.ts +0 -1
  22. package/dist/extensions/NEEDLE_compression_texture/index.js +0 -1
  23. package/dist/extensions/NEEDLE_lightmaps/index.d.ts +0 -39
  24. package/dist/extensions/NEEDLE_lightmaps/index.js +0 -257
  25. package/dist/extensions/NEEDLE_mesh_compression/NEEDLE_mesh_compression.d.ts +0 -27
  26. package/dist/extensions/NEEDLE_mesh_compression/NEEDLE_mesh_compression.js +0 -88
  27. package/dist/extensions/NEEDLE_mesh_compression/index.d.ts +0 -1
  28. package/dist/extensions/NEEDLE_mesh_compression/index.js +0 -1
  29. package/dist/extensions/NEEDLE_opaque/NEEDLE_opaque.d.ts +0 -27
  30. package/dist/extensions/NEEDLE_opaque/NEEDLE_opaque.js +0 -1078
  31. package/dist/extensions/NEEDLE_opaque/index.d.ts +0 -2
  32. package/dist/extensions/NEEDLE_opaque/index.js +0 -30
  33. package/dist/extensions/NEEDLE_pmrem/index.d.ts +0 -21
  34. package/dist/extensions/NEEDLE_pmrem/index.js +0 -95
  35. package/dist/extensions/NEEDLE_progressive/NEEDLE_progressive.d.ts +0 -48
  36. package/dist/extensions/NEEDLE_progressive/NEEDLE_progressive.js +0 -69
  37. package/dist/extensions/NEEDLE_progressive/index.d.ts +0 -1
  38. package/dist/extensions/NEEDLE_progressive/index.js +0 -1
  39. package/dist/extensions/NEEDLE_progressive_mesh_settings/index.d.ts +0 -23
  40. package/dist/extensions/NEEDLE_progressive_mesh_settings/index.js +0 -41
  41. package/dist/extensions/NEEDLE_progressive_texture_settings/index.d.ts +0 -25
  42. package/dist/extensions/NEEDLE_progressive_texture_settings/index.js +0 -38
  43. package/dist/extensions/index.d.ts +0 -15
  44. package/dist/extensions/index.js +0 -34
  45. package/dist/extensions/utils.d.ts +0 -9
  46. package/dist/extensions/utils.js +0 -79
  47. package/dist/functions/weld.3.d.ts +0 -76
  48. package/dist/functions/weld.3.js +0 -414
  49. package/dist/index.d.ts +0 -8
  50. package/dist/index.js +0 -8
  51. package/dist/scripts/index.d.ts +0 -2
  52. package/dist/scripts/index.js +0 -2
  53. package/dist/scripts/pack-gltf.d.ts +0 -19
  54. package/dist/scripts/pack-gltf.js +0 -187
  55. package/dist/scripts/stats.d.ts +0 -5
  56. package/dist/scripts/stats.js +0 -20
  57. package/dist/transforms/index.d.ts +0 -12
  58. package/dist/transforms/index.js +0 -12
  59. package/dist/transforms/needle_animation_transform.d.ts +0 -2
  60. package/dist/transforms/needle_animation_transform.js +0 -5
  61. package/dist/transforms/needle_asset.d.ts +0 -3
  62. package/dist/transforms/needle_asset.js +0 -50
  63. package/dist/transforms/needle_audio.d.ts +0 -12
  64. package/dist/transforms/needle_audio.js +0 -104
  65. package/dist/transforms/needle_audio_ffmpeg.d.ts +0 -14
  66. package/dist/transforms/needle_audio_ffmpeg.js +0 -51
  67. package/dist/transforms/needle_audio_registry.d.ts +0 -38
  68. package/dist/transforms/needle_audio_registry.js +0 -47
  69. package/dist/transforms/needle_common.d.ts +0 -17
  70. package/dist/transforms/needle_common.js +0 -1
  71. package/dist/transforms/needle_mesh_transform.d.ts +0 -15
  72. package/dist/transforms/needle_mesh_transform.js +0 -223
  73. package/dist/transforms/needle_pmrem.d.ts +0 -21
  74. package/dist/transforms/needle_pmrem.js +0 -238
  75. package/dist/transforms/needle_progressive.d.ts +0 -53
  76. package/dist/transforms/needle_progressive.js +0 -1117
  77. package/dist/transforms/needle_texture_transform.d.ts +0 -15
  78. package/dist/transforms/needle_texture_transform.js +0 -271
  79. package/dist/transforms/needle_toktx.d.ts +0 -26
  80. package/dist/transforms/needle_toktx.js +0 -250
  81. package/dist/transforms/needle_webp.d.ts +0 -15
  82. package/dist/transforms/needle_webp.js +0 -53
  83. package/dist/transforms/toktx.d.ts +0 -52
  84. package/dist/transforms/toktx.js +0 -208
  85. package/dist/transforms/toktx_types.d.ts +0 -78
  86. package/dist/transforms/toktx_types.js +0 -62
  87. package/dist/transforms/util.d.ts +0 -21
  88. package/dist/transforms/util.js +0 -90
  89. package/dist/utils/analytics.d.ts +0 -24
  90. package/dist/utils/analytics.js +0 -128
  91. package/dist/utils/compression_utils.d.ts +0 -2
  92. package/dist/utils/compression_utils.js +0 -6
  93. package/dist/utils/fileutils.d.ts +0 -22
  94. package/dist/utils/fileutils.js +0 -195
  95. package/dist/utils/guid.d.ts +0 -1
  96. package/dist/utils/guid.js +0 -5
  97. package/dist/utils/index.d.ts +0 -9
  98. package/dist/utils/index.js +0 -9
  99. package/dist/utils/mesh.d.ts +0 -13
  100. package/dist/utils/mesh.js +0 -124
  101. package/dist/utils/nodeio.d.ts +0 -11
  102. package/dist/utils/nodeio.js +0 -116
  103. package/dist/utils/stats.d.ts +0 -70
  104. package/dist/utils/stats.js +0 -273
  105. package/dist/utils/test-assert.d.ts +0 -8
  106. package/dist/utils/test-assert.js +0 -18
  107. package/dist/utils/texture.d.ts +0 -5
  108. package/dist/utils/texture.js +0 -14
  109. package/dist/utils/validate.d.ts +0 -9
  110. package/dist/utils/validate.js +0 -24
  111. package/dist/utils/version.d.ts +0 -1
  112. package/dist/utils/version.gen.d.ts +0 -1
  113. package/dist/utils/version.gen.js +0 -1
  114. package/dist/utils/version.js +0 -4
  115. package/scripts/clear-caches.mjs +0 -4
  116. package/scripts/defaults.mjs +0 -12
  117. package/scripts/fileutils.mjs +0 -6
  118. package/scripts/index.mjs +0 -41
  119. package/scripts/limit-caches.mjs +0 -4
  120. package/scripts/make-progressive.mjs +0 -16
  121. package/scripts/pack-gltf.mjs +0 -107
@@ -1,414 +0,0 @@
1
- /*
2
-
3
-
4
- COPY OF weld() of gltf-transform 3.10.1
5
- 2aec33bf432a354275fb5066d1ddf5840f2d8bb4
6
-
7
-
8
-
9
- */
10
- import { Accessor, Document, Primitive, PropertyType, } from '@gltf-transform/core';
11
- import { createTransform, prune, dedup } from '@gltf-transform/functions';
12
- /** @hidden */
13
- export function createIndices(count, maxIndex = count) {
14
- const array = maxIndex <= 65534 ? new Uint16Array(count) : new Uint32Array(count);
15
- for (let i = 0; i < array.length; i++)
16
- array[i] = i;
17
- return array;
18
- }
19
- export function formatLong(x) {
20
- return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
21
- }
22
- export function formatDelta(a, b, decimals = 2) {
23
- const prefix = a > b ? '–' : '+';
24
- const suffix = '%';
25
- return prefix + ((Math.abs(a - b) / a) * 100).toFixed(decimals) + suffix;
26
- }
27
- export function formatDeltaOp(a, b) {
28
- return `${formatLong(a)} → ${formatLong(b)} (${formatDelta(a, b)})`;
29
- }
30
- export function cleanPrimitive(prim) {
31
- const indices = prim.getIndices();
32
- if (!indices)
33
- return;
34
- const tmpIndicesArray = [];
35
- let maxIndex = -Infinity;
36
- for (let i = 0, il = indices.getCount(); i < il; i += 3) {
37
- const a = indices.getScalar(i);
38
- const b = indices.getScalar(i + 1);
39
- const c = indices.getScalar(i + 2);
40
- if (a === b || a === c || b === c)
41
- continue;
42
- // @ts-ignore
43
- tmpIndicesArray.push(a, b, c);
44
- maxIndex = Math.max(maxIndex, a, b, c);
45
- }
46
- const dstIndicesArray = createIndices(tmpIndicesArray.length, maxIndex);
47
- dstIndicesArray.set(tmpIndicesArray);
48
- indices.setArray(dstIndicesArray);
49
- }
50
- // DEVELOPER NOTES: Ideally a weld() implementation should be fast, robust,
51
- // and tunable. The writeup below tracks my attempts to solve for these
52
- // constraints.
53
- //
54
- // (Approach #1) Follow the mergeVertices() implementation of three.js,
55
- // hashing vertices with a string concatenation of all vertex attributes.
56
- // The approach does not allow per-attribute tolerance in local units.
57
- //
58
- // (Approach #2) Sort points along the X axis, then make cheaper
59
- // searches up/down the sorted list for merge candidates. While this allows
60
- // simpler comparison based on specified tolerance, it's much slower, even
61
- // for cases where choice of the X vs. Y or Z axes is reasonable.
62
- //
63
- // (Approach #3) Attempted a Delaunay triangulation in three dimensions,
64
- // expecting it would be an n * log(n) algorithm, but the only implementation
65
- // I found (with delaunay-triangulate) appeared to be much slower than that,
66
- // and was notably slower than the sort-based approach, just building the
67
- // Delaunay triangulation alone.
68
- //
69
- // (Approach #4) Hybrid of (1) and (2), assigning vertices to a spatial
70
- // grid, then searching the local neighborhood (27 cells) for weld candidates.
71
- //
72
- // RESULTS: For the "Lovecraftian" sample model, after joining, a primitive
73
- // with 873,000 vertices can be welded down to 230,000 vertices. Results:
74
- // - (1) Not tested, but prior results suggest not robust enough.
75
- // - (2) 30 seconds
76
- // - (3) 660 seconds
77
- // - (4) 5 seconds exhaustive, 1.5s non-exhaustive
78
- const NAME = 'weld';
79
- const Tolerance = {
80
- DEFAULT: 0.0001,
81
- TEXCOORD: 0.0001,
82
- COLOR: 0.01,
83
- NORMAL: 0.05,
84
- JOINTS: 0.0,
85
- WEIGHTS: 0.01, // [0, ∞]
86
- };
87
- export const WELD_DEFAULTS = {
88
- tolerance: Tolerance.DEFAULT,
89
- toleranceNormal: Tolerance.NORMAL,
90
- overwrite: true,
91
- exhaustive: false, // donmccurdy/glTF-Transform#886
92
- };
93
- /**
94
- * Index {@link Primitive Primitives} and (optionally) merge similar vertices. When merged
95
- * and indexed, data is shared more efficiently between vertices. File size can
96
- * be reduced, and the GPU can sometimes use the vertex cache more efficiently.
97
- *
98
- * When welding, the 'tolerance' threshold determines which vertices qualify for
99
- * welding based on distance between the vertices as a fraction of the primitive's
100
- * bounding box (AABB). For example, tolerance=0.01 welds vertices within +/-1%
101
- * of the AABB's longest dimension. Other vertex attributes are also compared
102
- * during welding, with attribute-specific thresholds. For `tolerance=0`, geometry
103
- * is indexed in place, without merging.
104
- *
105
- * To preserve visual appearance consistently, use low `toleranceNormal` thresholds
106
- * around 0.1 (±3º). To pre-processing a scene before simplification or LOD creation,
107
- * use higher thresholds around 0.5 (±30º).
108
- *
109
- * Example:
110
- *
111
- * ```javascript
112
- * import { weld } from '@gltf-transform/functions';
113
- *
114
- * await document.transform(
115
- * weld({ tolerance: 0.001, toleranceNormal: 0.5 })
116
- * );
117
- * ```
118
- *
119
- * @category Transforms
120
- */
121
- export function weld(_options = WELD_DEFAULTS) {
122
- const options = expandWeldOptions(_options);
123
- return createTransform(NAME, async (doc) => {
124
- const logger = doc.getLogger();
125
- for (const mesh of doc.getRoot().listMeshes()) {
126
- for (const prim of mesh.listPrimitives()) {
127
- weldPrimitive(doc, prim, options);
128
- if (isPrimEmpty(prim))
129
- prim.dispose();
130
- }
131
- if (mesh.listPrimitives().length === 0)
132
- mesh.dispose();
133
- }
134
- if (options.tolerance > 0) {
135
- // If tolerance is greater than 0, welding may remove a mesh, so we prune
136
- await doc.transform(prune({
137
- propertyTypes: [PropertyType.ACCESSOR, PropertyType.NODE],
138
- keepAttributes: true,
139
- keepIndices: true,
140
- keepLeaves: false,
141
- }));
142
- }
143
- await doc.transform(dedup({ propertyTypes: [PropertyType.ACCESSOR] }));
144
- logger.debug(`${NAME}: Complete.`);
145
- });
146
- }
147
- /**
148
- * Index a {@link Primitive} and (optionally) weld similar vertices. When merged
149
- * and indexed, data is shared more efficiently between vertices. File size can
150
- * be reduced, and the GPU can sometimes use the vertex cache more efficiently.
151
- *
152
- * When welding, the 'tolerance' threshold determines which vertices qualify for
153
- * welding based on distance between the vertices as a fraction of the primitive's
154
- * bounding box (AABB). For example, tolerance=0.01 welds vertices within +/-1%
155
- * of the AABB's longest dimension. Other vertex attributes are also compared
156
- * during welding, with attribute-specific thresholds. For tolerance=0, geometry
157
- * is indexed in place, without merging.
158
- *
159
- * Example:
160
- *
161
- * ```javascript
162
- * import { weldPrimitive } from '@gltf-transform/functions';
163
- *
164
- * const mesh = document.getRoot().listMeshes()
165
- * .find((mesh) => mesh.getName() === 'Gizmo');
166
- *
167
- * for (const prim of mesh.listPrimitives()) {
168
- * weldPrimitive(prim, {tolerance: 0.0001});
169
- * }
170
- * ```
171
- *
172
- * @privateRemarks TODO(v4): Remove the "Document" parameter.
173
- */
174
- export function weldPrimitive(a, b = WELD_DEFAULTS, c = WELD_DEFAULTS) {
175
- let _document;
176
- let _prim;
177
- let _options;
178
- if (a instanceof Primitive) {
179
- const graph = a.getGraph();
180
- _document = Document.fromGraph(graph);
181
- _prim = a;
182
- _options = expandWeldOptions(b);
183
- }
184
- else {
185
- _document = a;
186
- _prim = b;
187
- _options = expandWeldOptions(c);
188
- }
189
- if (_prim.getIndices() && !_options.overwrite)
190
- return;
191
- if (_prim.getMode() === Primitive.Mode.POINTS)
192
- return;
193
- if (_options.tolerance === 0) {
194
- _indexPrimitive(_document, _prim);
195
- }
196
- else {
197
- _weldPrimitive(_document, _prim, _options);
198
- }
199
- }
200
- /** @internal Adds indices, if missing. Does not merge vertices. */
201
- function _indexPrimitive(doc, prim) {
202
- // No need to overwrite here, even if options.overwrite=true.
203
- if (prim.getIndices())
204
- return;
205
- const attr = prim.listAttributes()[0];
206
- const numVertices = attr.getCount();
207
- const buffer = attr.getBuffer();
208
- const indices = doc
209
- .createAccessor()
210
- .setBuffer(buffer)
211
- .setType(Accessor.Type.SCALAR)
212
- .setArray(createIndices(numVertices));
213
- prim.setIndices(indices);
214
- }
215
- /** @internal Weld and merge, combining vertices that are similar on all vertex attributes. */
216
- function _weldPrimitive(doc, prim, options) {
217
- const logger = doc.getLogger();
218
- const srcPosition = prim.getAttribute('POSITION');
219
- const srcIndices = prim.getIndices() || doc.createAccessor().setArray(createIndices(srcPosition.getCount()));
220
- const uniqueIndices = new Uint32Array(new Set(srcIndices.getArray())).sort();
221
- // (1) Compute per-attribute tolerance and spatial grid for vertices.
222
- const attributeTolerance = {};
223
- for (const semantic of prim.listSemantics()) {
224
- const attribute = prim.getAttribute(semantic);
225
- attributeTolerance[semantic] = getAttributeTolerance(semantic, attribute, options);
226
- }
227
- logger.debug(`${NAME}: Tolerance thresholds: ${formatKV(attributeTolerance)}`);
228
- // (2) Compare and identify vertices to weld.
229
- const posA = [0, 0, 0];
230
- const posB = [0, 0, 0];
231
- const grid = {};
232
- const cellSize = attributeTolerance.POSITION;
233
- for (let i = 0; i < uniqueIndices.length; i++) {
234
- srcPosition.getElement(uniqueIndices[i], posA);
235
- const key = getGridKey(posA, cellSize);
236
- grid[key] = grid[key] || [];
237
- grid[key].push(uniqueIndices[i]);
238
- }
239
- // (2) Compare and identify vertices to weld.
240
- const srcMaxIndex = uniqueIndices[uniqueIndices.length - 1];
241
- const weldMap = createIndices(srcMaxIndex + 1); // oldIndex → oldCommonIndex
242
- const writeMap = new Array(uniqueIndices.length).fill(-1); // oldIndex → newIndex
243
- const srcVertexCount = srcPosition.getCount();
244
- let dstVertexCount = 0;
245
- for (let i = 0; i < uniqueIndices.length; i++) {
246
- const a = uniqueIndices[i];
247
- srcPosition.getElement(a, posA);
248
- const cellKeys = options.exhaustive ? getGridNeighborhoodKeys(posA, cellSize) : [getGridKey(posA, cellSize)];
249
- cells: for (const cellKey of cellKeys) {
250
- if (!grid[cellKey])
251
- continue cells; // May occur in exhaustive search.
252
- neighbors: for (const j of grid[cellKey]) {
253
- const b = weldMap[j];
254
- // Only weld to lower indices, preventing two-way match.
255
- if (a <= b)
256
- continue neighbors;
257
- srcPosition.getElement(b, posB);
258
- // Weld if base attributes and morph target attributes match.
259
- const isBaseMatch = prim.listSemantics().every((semantic) => {
260
- const attribute = prim.getAttribute(semantic);
261
- const tolerance = attributeTolerance[semantic];
262
- return compareAttributes(attribute, a, b, tolerance, semantic);
263
- });
264
- const isTargetMatch = prim.listTargets().every((target) => {
265
- return target.listSemantics().every((semantic) => {
266
- const attribute = target.getAttribute(semantic);
267
- const tolerance = attributeTolerance[semantic];
268
- return compareAttributes(attribute, a, b, tolerance, semantic);
269
- });
270
- });
271
- if (isBaseMatch && isTargetMatch) {
272
- weldMap[a] = b;
273
- break cells;
274
- }
275
- }
276
- }
277
- // Output the vertex if we didn't find a match, else record the index of the match. Because
278
- // we iterate vertices in ascending order, and only match to lower indices, we're
279
- // guaranteed the source vertex for a weld has already been marked for output.
280
- if (weldMap[a] === a) {
281
- writeMap[a] = dstVertexCount++;
282
- }
283
- else {
284
- writeMap[a] = writeMap[weldMap[a]];
285
- }
286
- }
287
- logger.debug(`${NAME}: ${formatDeltaOp(srcVertexCount, dstVertexCount)} vertices.`);
288
- // (3) Update indices.
289
- const dstIndicesCount = srcIndices.getCount(); // # primitives does not change.
290
- const dstIndicesArray = createIndices(dstIndicesCount, uniqueIndices.length);
291
- for (let i = 0; i < dstIndicesCount; i++) {
292
- dstIndicesArray[i] = writeMap[srcIndices.getScalar(i)];
293
- }
294
- prim.setIndices(srcIndices.clone().setArray(dstIndicesArray));
295
- if (srcIndices.listParents().length === 1)
296
- srcIndices.dispose();
297
- // (4) Update vertex attributes.
298
- for (const srcAttr of prim.listAttributes()) {
299
- swapAttributes(prim, srcAttr, writeMap, dstVertexCount);
300
- }
301
- for (const target of prim.listTargets()) {
302
- for (const srcAttr of target.listAttributes()) {
303
- swapAttributes(target, srcAttr, writeMap, dstVertexCount);
304
- }
305
- }
306
- // (5) Clean up degenerate triangles.
307
- cleanPrimitive(prim);
308
- }
309
- /** Creates a new TypedArray of the same type as an original, with a new length. */
310
- function createArrayOfType(array, length) {
311
- const ArrayCtor = array.constructor;
312
- return new ArrayCtor(length);
313
- }
314
- /** Replaces an {@link Attribute}, creating a new one with the given elements. */
315
- function swapAttributes(parent, srcAttr, reorder, dstCount) {
316
- const dstAttrArray = createArrayOfType(srcAttr.getArray(), dstCount * srcAttr.getElementSize());
317
- const dstAttr = srcAttr.clone().setArray(dstAttrArray);
318
- const done = new Uint8Array(dstCount);
319
- for (let i = 0, el = []; i < reorder.length; i++) {
320
- if (!done[reorder[i]]) {
321
- dstAttr.setElement(reorder[i], srcAttr.getElement(i, el));
322
- done[reorder[i]] = 1;
323
- }
324
- }
325
- parent.swap(srcAttr, dstAttr);
326
- // Clean up.
327
- if (srcAttr.listParents().length === 1)
328
- srcAttr.dispose();
329
- }
330
- const _a = [];
331
- const _b = [];
332
- /** Computes a per-attribute tolerance, based on domain and usage of the attribute. */
333
- function getAttributeTolerance(semantic, attribute, options) {
334
- // Attributes like NORMAL and COLOR_# do not vary in range like POSITION,
335
- // so do not apply the given tolerance factor to these attributes.
336
- if (semantic === 'NORMAL' || semantic === 'TANGENT')
337
- return options.toleranceNormal;
338
- if (semantic.startsWith('COLOR_'))
339
- return Tolerance.COLOR;
340
- if (semantic.startsWith('TEXCOORD_'))
341
- return Tolerance.TEXCOORD;
342
- if (semantic.startsWith('JOINTS_'))
343
- return Tolerance.JOINTS;
344
- if (semantic.startsWith('WEIGHTS_'))
345
- return Tolerance.WEIGHTS;
346
- _a.length = _b.length = 0;
347
- attribute.getMinNormalized(_a);
348
- attribute.getMaxNormalized(_b);
349
- const diff = _b.map((bi, i) => bi - _a[i]);
350
- const range = Math.max(...diff);
351
- return options.tolerance * range;
352
- }
353
- /** Compares two vertex attributes against a tolerance threshold. */
354
- function compareAttributes(attribute, a, b, tolerance, _semantic) {
355
- attribute.getElement(a, _a);
356
- attribute.getElement(b, _b);
357
- for (let i = 0, il = attribute.getElementSize(); i < il; i++) {
358
- if (Math.abs(_a[i] - _b[i]) > tolerance) {
359
- return false;
360
- }
361
- }
362
- return true;
363
- }
364
- function formatKV(kv) {
365
- return Object.entries(kv)
366
- .map(([k, v]) => `${k}=${v}`)
367
- .join(', ');
368
- }
369
- // Order to search nearer cells first.
370
- const CELL_OFFSETS = [0, -1, 1];
371
- function getGridNeighborhoodKeys(p, cellSize) {
372
- const keys = [];
373
- const _p = [0, 0, 0];
374
- for (const i of CELL_OFFSETS) {
375
- for (const j of CELL_OFFSETS) {
376
- for (const k of CELL_OFFSETS) {
377
- _p[0] = p[0] + i * cellSize;
378
- _p[1] = p[1] + j * cellSize;
379
- _p[2] = p[2] + k * cellSize;
380
- keys.push(getGridKey(_p, cellSize));
381
- }
382
- }
383
- }
384
- return keys;
385
- }
386
- function getGridKey(p, cellSize) {
387
- const cellX = Math.round(p[0] / cellSize);
388
- const cellY = Math.round(p[1] / cellSize);
389
- const cellZ = Math.round(p[2] / cellSize);
390
- return cellX + ':' + cellY + ':' + cellZ;
391
- }
392
- function expandWeldOptions(_options) {
393
- const options = { ...WELD_DEFAULTS, ..._options };
394
- if (options.tolerance < 0 || options.tolerance > 0.1) {
395
- throw new Error(`${NAME}: Requires 0 <= tolerance <= 0.1`);
396
- }
397
- if (options.toleranceNormal < 0 || options.toleranceNormal > Math.PI / 2) {
398
- throw new Error(`${NAME}: Requires 0 <= toleranceNormal <= ${(Math.PI / 2).toFixed(2)}`);
399
- }
400
- if (options.tolerance > 0) {
401
- options.tolerance = Math.max(options.tolerance, Number.EPSILON);
402
- options.toleranceNormal = Math.max(options.toleranceNormal, Number.EPSILON);
403
- }
404
- return options;
405
- }
406
- /**
407
- * For purposes of welding, we consider a primitive to be 'empty' or degenerate
408
- * if (1) it has an index, and (2) that index is empty. In some cases
409
- * (mode=POINTS) the index may be missing — this is outside the scope of welding.
410
- */
411
- function isPrimEmpty(prim) {
412
- const indices = prim.getIndices();
413
- return !!indices && indices.getCount() === 0;
414
- }
package/dist/index.d.ts DELETED
@@ -1,8 +0,0 @@
1
- export * from "./config/index.js";
2
- export * from "./cli/index.js";
3
- export * from "./utils/index.js";
4
- export * from "./transforms/index.js";
5
- export * from "./extensions/index.js";
6
- export * from "./cache/index.js";
7
- export * from "./scripts/index.js";
8
- export * from "./constants.js";
package/dist/index.js DELETED
@@ -1,8 +0,0 @@
1
- export * from "./config/index.js";
2
- export * from "./cli/index.js";
3
- export * from "./utils/index.js";
4
- export * from "./transforms/index.js";
5
- export * from "./extensions/index.js";
6
- export * from "./cache/index.js";
7
- export * from "./scripts/index.js";
8
- export * from "./constants.js";
@@ -1,2 +0,0 @@
1
- export * from "./pack-gltf.js";
2
- export * from "./stats.js";
@@ -1,2 +0,0 @@
1
- export * from "./pack-gltf.js";
2
- export * from "./stats.js";
@@ -1,19 +0,0 @@
1
- import { ILogger } from '@gltf-transform/core';
2
- import { ProcessStats } from "../utils/index.js";
3
- import { Config } from '../config/index.js';
4
- export type PackGLTFOptions = {
5
- useCache: boolean;
6
- packedFiles?: Set<string>;
7
- stats?: ProcessStats;
8
- config: Config;
9
- logger?: ILogger;
10
- debug: boolean;
11
- verbose?: boolean;
12
- /** Original source file path, used to resolve external assets (e.g. .exr files) when inputFile differs from the source (e.g. after progressive transform) */
13
- sourceFile?: string;
14
- /** Populated by packGLTF with absolute paths to external sidecar files produced during processing (e.g. .pmrem.ktx2) */
15
- externalFiles?: string[];
16
- };
17
- /** Compress a glTF file
18
- */
19
- export declare function packGLTF(inputFile: string, outputFile: string, options: PackGLTFOptions): Promise<boolean>;
@@ -1,187 +0,0 @@
1
- import path from 'path';
2
- import { Logger, NodeIO, PropertyType, Verbosity } from '@gltf-transform/core';
3
- import { ALL_EXTENSIONS } from '@gltf-transform/extensions';
4
- import draco3d from 'draco3dgltf';
5
- import { existsSync, readFileSync, statSync, writeFileSync } from 'fs';
6
- import { MeshoptDecoder, MeshoptEncoder } from 'meshoptimizer';
7
- import { dedup, metalRough, prune, resample } from '@gltf-transform/functions';
8
- import { ALL_EXTENSIONS as NEEDLE_EXTENSIONS, NEEDLE_compression_texture, NEEDLE_mesh_compression, NEEDLE_pmrem, NEEDLE_lightmaps_ext, setExtensionInputFile } from '../extensions/index.js';
9
- import { clearAudioRegistry, isLOD, isMeshLOD, listAudioRefs, needle_animation_transform, needle_asset, needle_audio_transform, needle_mesh_transform, needle_texture_transform, } from '../transforms/index.js';
10
- import { copyExternalResources, getOutputPath, getVersion, isMaterialOnlyGLB, ioTryReadWithMissingResources, writeNodeIO } from "../utils/index.js";
11
- import { addToCache, getHash, tryGetFromCache, } from "../cache/index.js";
12
- import { TestAssertionError } from '../utils/test-assert.js';
13
- /** Compress a glTF file
14
- */
15
- export async function packGLTF(inputFile, outputFile, options) {
16
- if (!options.logger) {
17
- options.logger = new Logger(Verbosity.INFO);
18
- }
19
- const { debug, stats, packedFiles, useCache, logger } = options;
20
- if (!existsSync(inputFile)) {
21
- logger.error(`WARN: can not pack, file does not exist: \"${inputFile}\"`);
22
- return false;
23
- }
24
- // check if it's in the cache.
25
- // We only need to check mesh LOD files because image LODs are cache via our toktx transform
26
- // We can not cache the main glTF files because the hash always changes for them
27
- let cacheKey = undefined;
28
- if (useCache && isMeshLOD(inputFile)) {
29
- const existing = readFileSync(inputFile);
30
- const hash = getHash(existing);
31
- cacheKey = "packed-" + hash;
32
- const cached = tryGetFromCache(cacheKey);
33
- if (cached) {
34
- logger.info(`→ Loaded compressed file from cache: \"${path.basename(inputFile)}\"`);
35
- writeFileSync(inputFile, cached);
36
- return true;
37
- }
38
- }
39
- if (packedFiles) {
40
- if (packedFiles.has(inputFile))
41
- return true;
42
- packedFiles.add(inputFile);
43
- }
44
- try {
45
- const config = options.config;
46
- if (!outputFile)
47
- outputFile = inputFile;
48
- else {
49
- outputFile = getOutputPath(inputFile, outputFile);
50
- }
51
- const prevSizeInMB = statSync(inputFile).size / 1024 / 1024;
52
- logger.debug(`→ Compressing ${prevSizeInMB.toFixed(1)} MB: ${inputFile}`);
53
- const startTime = Date.now();
54
- const io = new NodeIO()
55
- .registerExtensions(ALL_EXTENSIONS)
56
- .registerExtensions(NEEDLE_EXTENSIONS)
57
- .registerExtensions([
58
- NEEDLE_compression_texture,
59
- NEEDLE_mesh_compression,
60
- NEEDLE_pmrem,
61
- NEEDLE_lightmaps_ext,
62
- ])
63
- .registerDependencies({
64
- 'draco3d.decoder': await draco3d.createDecoderModule(),
65
- 'draco3d.encoder': await draco3d.createEncoderModule(),
66
- 'meshopt.decoder': MeshoptDecoder,
67
- 'meshopt.encoder': MeshoptEncoder,
68
- });
69
- // Set input file path so extensions can resolve external EXR files
70
- // Use sourceFile when available (e.g. when inputFile is a progressive output copy)
71
- const assetSourceFile = options.sourceFile ?? inputFile;
72
- setExtensionInputFile(assetSourceFile);
73
- clearAudioRegistry();
74
- const document = await ioTryReadWithMissingResources(io, inputFile);
75
- if (options.logger) {
76
- document.setLogger(options.logger);
77
- }
78
- else {
79
- /** @ts-ignore */
80
- document.getLogger().verbosity = options.verbose ? Verbosity.DEBUG : options.debug ? Verbosity.INFO : Verbosity.WARN;
81
- }
82
- copyExternalResources(document, assetSourceFile, outputFile);
83
- let totalGPUTextureMemory = 0;
84
- /** @type {Array<import('@gltf-transform/core').Transform>} */
85
- const transforms = [
86
- needle_asset(),
87
- metalRough(),
88
- needle_texture_transform({
89
- file: inputFile,
90
- outfile: outputFile,
91
- useCache,
92
- noToktx: false,
93
- debug: debug,
94
- reportGPUMemory: val => {
95
- totalGPUTextureMemory += val;
96
- },
97
- config,
98
- }),
99
- needle_mesh_transform({ file: inputFile, config }),
100
- needle_audio_transform({ outfile: outputFile, useCache, config }),
101
- needle_animation_transform({}),
102
- resample({
103
- tolerance: 1e-6, // NOTE: lowered from 1e-4 to 1e-6 to fix https://linear.app/needle/issue/NE-6872
104
- }),
105
- // Can not dedup "materials" because progressive textures will then assigned to the wrong material instance
106
- // HACK: keepUniqueNames is enabled to fix https://linear.app/needle/issue/NE-6288
107
- dedup({ propertyTypes: [PropertyType.MESH, PropertyType.MATERIAL] }),
108
- ];
109
- const isProgressiveAsset = isLOD(inputFile);
110
- const materialOnly = isMaterialOnlyGLB(document);
111
- // TODO: progressive mesh LODs should not be pruned right now because it will remove e.g. UVs from meshes (and probably blend shapes too) (so we just not do it for any progressive asset)
112
- // Material-only GLBs should not be pruned at all — they have no meshes so
113
- // prune would strip materials, textures, and buffers, producing an empty file.
114
- if (!isProgressiveAsset && !materialOnly) {
115
- transforms.push(prune({
116
- // Pruning animations is not supported yet (e.g. if the animation is only referenced by a component)
117
- propertyTypes: [
118
- PropertyType.MESH,
119
- PropertyType.MATERIAL,
120
- PropertyType.TEXTURE,
121
- PropertyType.ACCESSOR,
122
- PropertyType.BUFFER,
123
- ],
124
- keepAttributes: true,
125
- keepIndices: true,
126
- keepExtras: true,
127
- keepSolidTextures: true,
128
- }));
129
- }
130
- await document.transform(...transforms);
131
- // Collect external sidecar files (e.g. .pmrem.ktx2) produced by transforms
132
- if (options.externalFiles) {
133
- const outDir = path.dirname(outputFile);
134
- const root = document.getRoot();
135
- for (const tex of root.listTextures()) {
136
- const uri = tex.getURI();
137
- if (uri && !uri.startsWith('data:')) {
138
- const absPath = path.resolve(outDir, uri);
139
- if (existsSync(absPath)) {
140
- options.externalFiles.push(absPath);
141
- }
142
- }
143
- }
144
- for (const buf of root.listBuffers()) {
145
- const uri = buf.getURI();
146
- if (uri && !uri.startsWith('data:')) {
147
- const absPath = path.resolve(outDir, uri);
148
- if (existsSync(absPath)) {
149
- options.externalFiles.push(absPath);
150
- }
151
- }
152
- }
153
- for (const ref of listAudioRefs()) {
154
- const uri = ref.newUri ?? ref.sourceUri;
155
- const absPath = path.resolve(outDir, uri);
156
- if (existsSync(absPath)) {
157
- options.externalFiles.push(absPath);
158
- }
159
- }
160
- }
161
- logger.debug(`← Writing to ${outputFile}`);
162
- await writeNodeIO(io, outputFile, document);
163
- if (useCache && cacheKey != undefined && existsSync(outputFile))
164
- addToCache(cacheKey, readFileSync(outputFile));
165
- const duration = Date.now() - startTime;
166
- const sizeInMB = statSync(outputFile).size / 1024 / 1024;
167
- const filename = path.basename(outputFile);
168
- const gpuMemoryInMB = totalGPUTextureMemory / 1024 / 1024;
169
- logger.info(`← Compressed '${filename}' ${prevSizeInMB.toFixed(1)} MB → ${sizeInMB.toFixed(1)} MB (GPU: ${gpuMemoryInMB.toFixed(1)} MB textures) in ${(duration / 1000).toFixed(1)} sec`);
170
- if (stats) {
171
- stats.totalFilesProcessed++;
172
- stats.totalFileSizeInMB += sizeInMB;
173
- stats.totalFileSizeInMBBefore += prevSizeInMB;
174
- stats.totalGPUMemoryInMB += gpuMemoryInMB;
175
- }
176
- return true;
177
- }
178
- catch (err) {
179
- if (err instanceof TestAssertionError)
180
- throw err;
181
- const shortedStackTrace = err instanceof Error && err.stack ? err.stack.split('\n').slice(0, 2).join('\n') : '';
182
- logger.error(`Packing of "${path.basename(inputFile)}" using version ${getVersion()} failed with error:\n${err instanceof Error ? err.message : String(err)}\n${shortedStackTrace}`);
183
- if (debug)
184
- throw err;
185
- return false;
186
- }
187
- }
@@ -1,5 +0,0 @@
1
- import { FileStats } from "../utils/stats.js";
2
- export interface StatsOptions {
3
- verbose?: boolean;
4
- }
5
- export declare function calculateStats(inputfile: string, options?: StatsOptions): Promise<FileStats>;
@@ -1,20 +0,0 @@
1
- import { Logger, NodeIO, Verbosity } from "@gltf-transform/core";
2
- import { ALL_EXTENSIONS } from "@gltf-transform/extensions";
3
- import draco3d from 'draco3dgltf';
4
- import { MeshoptDecoder, MeshoptEncoder } from "meshoptimizer";
5
- import { createNewFilestats } from "../utils/stats.js";
6
- export async function calculateStats(inputfile, options) {
7
- const io = new NodeIO()
8
- .setLogger(new Logger(options?.verbose ? Verbosity.DEBUG : Verbosity.ERROR))
9
- .registerExtensions(ALL_EXTENSIONS)
10
- .registerDependencies({
11
- 'draco3d.decoder': await draco3d.createDecoderModule(),
12
- 'draco3d.encoder': await draco3d.createEncoderModule(),
13
- 'meshopt.decoder': MeshoptDecoder,
14
- 'meshopt.encoder': MeshoptEncoder,
15
- });
16
- const document = await io.read(inputfile);
17
- const json = await io.readAsJSON(inputfile);
18
- const stats = createNewFilestats(inputfile, document, json);
19
- return stats;
20
- }