@needle-tools/gltf-build-pipeline 2.9.4 → 2.9.5-experimental.0dfa267
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.
- package/CHANGELOG.md +486 -485
- package/README.md +35 -35
- package/package.json +78 -78
- package/scripts/clear-caches.mjs +3 -3
- package/scripts/defaults.mjs +11 -11
- package/scripts/fileutils.mjs +5 -5
- package/scripts/index.mjs +41 -41
- package/scripts/limit-caches.mjs +3 -3
- package/scripts/make-progressive.mjs +16 -16
- package/scripts/pack-gltf.mjs +106 -106
- package/tsconfig.json +19 -19
- package/dist/cache/cache.d.ts +0 -55
- package/dist/cache/cache.js +0 -289
- package/dist/cache/index.d.ts +0 -1
- package/dist/cache/index.js +0 -1
- package/dist/cli/index.d.ts +0 -2
- package/dist/cli/index.js +0 -189
- package/dist/config/index.d.ts +0 -53
- package/dist/config/index.js +0 -152
- package/dist/constants.d.ts +0 -7
- package/dist/constants.js +0 -7
- package/dist/extensions/NEEDLE_compression_texture/NEEDLE_compression_texture.d.ts +0 -24
- package/dist/extensions/NEEDLE_compression_texture/NEEDLE_compression_texture.js +0 -54
- package/dist/extensions/NEEDLE_compression_texture/index.d.ts +0 -1
- package/dist/extensions/NEEDLE_compression_texture/index.js +0 -1
- package/dist/extensions/NEEDLE_mesh_compression/NEEDLE_mesh_compression.d.ts +0 -27
- package/dist/extensions/NEEDLE_mesh_compression/NEEDLE_mesh_compression.js +0 -88
- package/dist/extensions/NEEDLE_mesh_compression/index.d.ts +0 -1
- package/dist/extensions/NEEDLE_mesh_compression/index.js +0 -1
- package/dist/extensions/NEEDLE_opaque/NEEDLE_opaque.d.ts +0 -27
- package/dist/extensions/NEEDLE_opaque/NEEDLE_opaque.js +0 -971
- package/dist/extensions/NEEDLE_opaque/index.d.ts +0 -3
- package/dist/extensions/NEEDLE_opaque/index.js +0 -28
- package/dist/extensions/NEEDLE_progressive/NEEDLE_progressive.d.ts +0 -48
- package/dist/extensions/NEEDLE_progressive/NEEDLE_progressive.js +0 -69
- package/dist/extensions/NEEDLE_progressive/index.d.ts +0 -1
- package/dist/extensions/NEEDLE_progressive/index.js +0 -1
- package/dist/extensions/NEEDLE_progressive_mesh_settings/index.d.ts +0 -23
- package/dist/extensions/NEEDLE_progressive_mesh_settings/index.js +0 -41
- package/dist/extensions/NEEDLE_progressive_texture_settings/index.d.ts +0 -25
- package/dist/extensions/NEEDLE_progressive_texture_settings/index.js +0 -67
- package/dist/extensions/index.d.ts +0 -7
- package/dist/extensions/index.js +0 -17
- package/dist/extensions/utils.d.ts +0 -6
- package/dist/extensions/utils.js +0 -72
- package/dist/functions/weld.3.d.ts +0 -76
- package/dist/functions/weld.3.js +0 -414
- package/dist/index.d.ts +0 -8
- package/dist/index.js +0 -8
- package/dist/scripts/index.d.ts +0 -2
- package/dist/scripts/index.js +0 -2
- package/dist/scripts/pack-gltf.d.ts +0 -15
- package/dist/scripts/pack-gltf.js +0 -129
- package/dist/scripts/stats.d.ts +0 -2
- package/dist/scripts/stats.js +0 -19
- package/dist/transforms/index.d.ts +0 -9
- package/dist/transforms/index.js +0 -9
- package/dist/transforms/needle_animation_transform.d.ts +0 -2
- package/dist/transforms/needle_animation_transform.js +0 -5
- package/dist/transforms/needle_asset.d.ts +0 -3
- package/dist/transforms/needle_asset.js +0 -50
- package/dist/transforms/needle_common.d.ts +0 -17
- package/dist/transforms/needle_common.js +0 -1
- package/dist/transforms/needle_mesh_transform.d.ts +0 -15
- package/dist/transforms/needle_mesh_transform.js +0 -218
- package/dist/transforms/needle_progressive.d.ts +0 -44
- package/dist/transforms/needle_progressive.js +0 -1005
- package/dist/transforms/needle_texture_transform.d.ts +0 -13
- package/dist/transforms/needle_texture_transform.js +0 -257
- package/dist/transforms/needle_toktx.d.ts +0 -26
- package/dist/transforms/needle_toktx.js +0 -239
- package/dist/transforms/needle_webp.d.ts +0 -15
- package/dist/transforms/needle_webp.js +0 -53
- package/dist/transforms/toktx.d.ts +0 -52
- package/dist/transforms/toktx.js +0 -221
- package/dist/transforms/toktx_types.d.ts +0 -78
- package/dist/transforms/toktx_types.js +0 -62
- package/dist/transforms/util.d.ts +0 -21
- package/dist/transforms/util.js +0 -90
- package/dist/utils/compression_utils.d.ts +0 -2
- package/dist/utils/compression_utils.js +0 -6
- package/dist/utils/fileutils.d.ts +0 -19
- package/dist/utils/fileutils.js +0 -177
- package/dist/utils/guid.d.ts +0 -1
- package/dist/utils/guid.js +0 -5
- package/dist/utils/index.d.ts +0 -10
- package/dist/utils/index.js +0 -10
- package/dist/utils/merge.d.ts +0 -12
- package/dist/utils/merge.js +0 -31
- package/dist/utils/mesh.d.ts +0 -13
- package/dist/utils/mesh.js +0 -124
- package/dist/utils/nodeio.d.ts +0 -6
- package/dist/utils/nodeio.js +0 -86
- package/dist/utils/stats.d.ts +0 -70
- package/dist/utils/stats.js +0 -258
- package/dist/utils/texture.d.ts +0 -5
- package/dist/utils/texture.js +0 -14
- package/dist/utils/validate.d.ts +0 -5
- package/dist/utils/validate.js +0 -17
- package/dist/utils/version.d.ts +0 -1
- package/dist/utils/version.gen.d.ts +0 -1
- package/dist/utils/version.gen.js +0 -1
- package/dist/utils/version.js +0 -4
package/dist/functions/weld.3.js
DELETED
|
@@ -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";
|
package/dist/scripts/index.d.ts
DELETED
package/dist/scripts/index.js
DELETED
|
@@ -1,15 +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
|
-
};
|
|
13
|
-
/** Compress a glTF file
|
|
14
|
-
*/
|
|
15
|
-
export declare function packGLTF(inputFile: string, outputFile: string, options: PackGLTFOptions): Promise<boolean>;
|
|
@@ -1,129 +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, flatten, join, prune, resample } from '@gltf-transform/functions';
|
|
8
|
-
import { ALL_EXTENSIONS as NEEDLE_EXTENSIONS, NEEDLE_compression_texture, NEEDLE_mesh_compression, } from '../extensions/index.js';
|
|
9
|
-
import { isLOD, isMeshLOD, needle_animation_transform, needle_asset, needle_mesh_transform, needle_texture_transform, } from '../transforms/index.js';
|
|
10
|
-
import { getOutputPath, ioTryReadWithMissingResources, writeNodeIO } from "../utils/index.js";
|
|
11
|
-
import { addToCache, getHash, tryGetFromCache, } from "../cache/index.js";
|
|
12
|
-
/** Compress a glTF file
|
|
13
|
-
*/
|
|
14
|
-
export async function packGLTF(inputFile, outputFile, options) {
|
|
15
|
-
if (!options.logger) {
|
|
16
|
-
options.logger = new Logger(Verbosity.INFO);
|
|
17
|
-
}
|
|
18
|
-
const { debug, stats, packedFiles, useCache, logger } = options;
|
|
19
|
-
if (!existsSync(inputFile)) {
|
|
20
|
-
logger.error(`WARN: can not pack, file does not exist: \"${inputFile}\"`);
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
// check if it's in the cache.
|
|
24
|
-
// We only need to check mesh LOD files because image LODs are cache via our toktx transform
|
|
25
|
-
// We can not cache the main glTF files because the hash always changes for them
|
|
26
|
-
let cacheKey = undefined;
|
|
27
|
-
if (useCache && isMeshLOD(inputFile)) {
|
|
28
|
-
const existing = readFileSync(inputFile);
|
|
29
|
-
const hash = getHash(existing);
|
|
30
|
-
cacheKey = "packed-" + hash;
|
|
31
|
-
const cached = tryGetFromCache(cacheKey);
|
|
32
|
-
if (cached) {
|
|
33
|
-
logger.info(`→ Loaded compressed file from cache: \"${path.basename(inputFile)}\"`);
|
|
34
|
-
writeFileSync(inputFile, cached);
|
|
35
|
-
return true;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
if (packedFiles) {
|
|
39
|
-
if (packedFiles.has(inputFile))
|
|
40
|
-
return true;
|
|
41
|
-
packedFiles.add(inputFile);
|
|
42
|
-
}
|
|
43
|
-
try {
|
|
44
|
-
const config = options.config;
|
|
45
|
-
if (!outputFile)
|
|
46
|
-
outputFile = inputFile;
|
|
47
|
-
else {
|
|
48
|
-
outputFile = getOutputPath(inputFile, outputFile);
|
|
49
|
-
}
|
|
50
|
-
const prevSizeInMB = statSync(inputFile).size / 1024 / 1024;
|
|
51
|
-
logger.info(`→ Compressing ${prevSizeInMB.toFixed(1)} MB: ${inputFile}`);
|
|
52
|
-
const startTime = Date.now();
|
|
53
|
-
const io = new NodeIO()
|
|
54
|
-
.registerExtensions(ALL_EXTENSIONS)
|
|
55
|
-
.registerExtensions(NEEDLE_EXTENSIONS)
|
|
56
|
-
.registerExtensions([
|
|
57
|
-
NEEDLE_compression_texture,
|
|
58
|
-
NEEDLE_mesh_compression,
|
|
59
|
-
])
|
|
60
|
-
.registerDependencies({
|
|
61
|
-
'draco3d.decoder': await draco3d.createDecoderModule(),
|
|
62
|
-
'draco3d.encoder': await draco3d.createEncoderModule(),
|
|
63
|
-
'meshopt.decoder': MeshoptDecoder,
|
|
64
|
-
'meshopt.encoder': MeshoptEncoder,
|
|
65
|
-
});
|
|
66
|
-
const document = await ioTryReadWithMissingResources(io, inputFile); // await io.read(inputFile);
|
|
67
|
-
/** @ts-ignore */
|
|
68
|
-
document.getLogger().verbosity = options.verbose ? Verbosity.DEBUG : options.debug ? Verbosity.INFO : Verbosity.WARN;
|
|
69
|
-
let totalGPUTextureMemory = 0;
|
|
70
|
-
/** @type {Array<import('@gltf-transform/core').Transform>} */
|
|
71
|
-
const transforms = [
|
|
72
|
-
needle_asset(),
|
|
73
|
-
needle_texture_transform({
|
|
74
|
-
file: inputFile,
|
|
75
|
-
outfile: outputFile,
|
|
76
|
-
useCache,
|
|
77
|
-
noToktx: false,
|
|
78
|
-
debug: debug,
|
|
79
|
-
reportGPUMemory: val => {
|
|
80
|
-
totalGPUTextureMemory += val;
|
|
81
|
-
},
|
|
82
|
-
config,
|
|
83
|
-
}),
|
|
84
|
-
needle_mesh_transform({ file: inputFile, config }),
|
|
85
|
-
needle_animation_transform({}),
|
|
86
|
-
resample(),
|
|
87
|
-
// Can not dedup "materials" because progressive textures will then assigned to the wrong material instance
|
|
88
|
-
// HACK: keepUniqueNames is enabled to fix https://linear.app/needle/issue/NE-6288
|
|
89
|
-
dedup({ propertyTypes: [PropertyType.MESH, PropertyType.MATERIAL] }),
|
|
90
|
-
// prune(),
|
|
91
|
-
];
|
|
92
|
-
const isProgressiveAsset = isLOD(inputFile);
|
|
93
|
-
// 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)
|
|
94
|
-
if (!isProgressiveAsset) {
|
|
95
|
-
transforms.push(prune({
|
|
96
|
-
// Pruning animations is not supported yet (e.g. if the animation is only referenced by a component)
|
|
97
|
-
propertyTypes: ['MESH', 'MATERIAL', 'TEXTURE']
|
|
98
|
-
}));
|
|
99
|
-
}
|
|
100
|
-
const isVRM = inputFile.toLowerCase().endsWith(".vrm");
|
|
101
|
-
if (!isVRM && !isProgressiveAsset && config?.usecase === "product") {
|
|
102
|
-
transforms.push(flatten());
|
|
103
|
-
transforms.push(join({ keepNamed: false }));
|
|
104
|
-
}
|
|
105
|
-
await document.transform(...transforms);
|
|
106
|
-
logger.info(`← Writing to ${outputFile}`);
|
|
107
|
-
await writeNodeIO(io, outputFile, document);
|
|
108
|
-
if (useCache && cacheKey != undefined && existsSync(outputFile))
|
|
109
|
-
addToCache(cacheKey, readFileSync(outputFile));
|
|
110
|
-
const duration = Date.now() - startTime;
|
|
111
|
-
const sizeInMB = statSync(outputFile).size / 1024 / 1024;
|
|
112
|
-
const filename = path.basename(outputFile);
|
|
113
|
-
const gpuMemoryInMB = totalGPUTextureMemory / 1024 / 1024;
|
|
114
|
-
logger.info(`← Compressing done in ${(duration / 1000).toFixed(1)} sec, ${prevSizeInMB.toFixed(1)} MB → ${sizeInMB.toFixed(1)} MB (GPU: ${gpuMemoryInMB.toFixed(1)} MB textures): ${filename} at ${outputFile}`);
|
|
115
|
-
if (stats) {
|
|
116
|
-
stats.totalFilesProcessed++;
|
|
117
|
-
stats.totalFileSizeInMB += sizeInMB;
|
|
118
|
-
stats.totalFileSizeInMBBefore += prevSizeInMB;
|
|
119
|
-
stats.totalGPUMemoryInMB += gpuMemoryInMB;
|
|
120
|
-
}
|
|
121
|
-
return true;
|
|
122
|
-
}
|
|
123
|
-
catch (err) {
|
|
124
|
-
logger.error(`Packing \"${path.basename(inputFile)}\" failed. Error: ${err.message}`);
|
|
125
|
-
if (debug)
|
|
126
|
-
throw err;
|
|
127
|
-
return false;
|
|
128
|
-
}
|
|
129
|
-
}
|
package/dist/scripts/stats.d.ts
DELETED
package/dist/scripts/stats.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { NodeIO } 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) {
|
|
7
|
-
const io = new NodeIO()
|
|
8
|
-
.registerExtensions(ALL_EXTENSIONS)
|
|
9
|
-
.registerDependencies({
|
|
10
|
-
'draco3d.decoder': await draco3d.createDecoderModule(),
|
|
11
|
-
'draco3d.encoder': await draco3d.createEncoderModule(),
|
|
12
|
-
'meshopt.decoder': MeshoptDecoder,
|
|
13
|
-
'meshopt.encoder': MeshoptEncoder,
|
|
14
|
-
});
|
|
15
|
-
const document = await io.read(inputfile);
|
|
16
|
-
const json = await io.readAsJSON(inputfile);
|
|
17
|
-
const stats = createNewFilestats(inputfile, document, json);
|
|
18
|
-
return stats;
|
|
19
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export * from "./needle_asset.js";
|
|
2
|
-
export * from "./toktx.js";
|
|
3
|
-
export * from "./needle_toktx.js";
|
|
4
|
-
export * from "./needle_progressive.js";
|
|
5
|
-
export * from "./needle_webp.js";
|
|
6
|
-
export * from "./needle_mesh_transform.js";
|
|
7
|
-
export * from "./needle_common.js";
|
|
8
|
-
export * from "./needle_texture_transform.js";
|
|
9
|
-
export * from "./needle_animation_transform.js";
|
package/dist/transforms/index.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export * from "./needle_asset.js";
|
|
2
|
-
export * from "./toktx.js";
|
|
3
|
-
export * from "./needle_toktx.js";
|
|
4
|
-
export * from "./needle_progressive.js";
|
|
5
|
-
export * from "./needle_webp.js";
|
|
6
|
-
export * from "./needle_mesh_transform.js";
|
|
7
|
-
export * from "./needle_common.js";
|
|
8
|
-
export * from "./needle_texture_transform.js";
|
|
9
|
-
export * from "./needle_animation_transform.js";
|