@margoengine/loaders 0.1.0 → 1.0.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.
- package/LICENSE +201 -201
- package/NOTICE +4 -4
- package/README.md +1666 -1198
- package/dist/crease.d.ts +7 -0
- package/dist/crease.js +67 -0
- package/dist/dae-common.js +1 -1
- package/dist/dae-kinematics.d.ts +65 -0
- package/dist/dae-kinematics.js +461 -0
- package/dist/dae.d.ts +9 -4
- package/dist/dae.js +16 -5
- package/dist/dxf-hatch.d.ts +8 -0
- package/dist/dxf-hatch.js +346 -0
- package/dist/dxf-leaders.d.ts +59 -0
- package/dist/dxf-leaders.js +258 -0
- package/dist/dxf.d.ts +42 -1
- package/dist/dxf.js +226 -15
- package/dist/engine.d.ts +5 -0
- package/dist/engine.js +3 -0
- package/dist/gcode.d.ts +37 -0
- package/dist/gcode.js +91 -0
- package/dist/geometry.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/mtl.d.ts +8 -3
- package/dist/mtl.js +91 -10
- package/dist/nrrd.d.ts +12 -5
- package/dist/nrrd.js +68 -26
- package/dist/obj.d.ts +3 -1
- package/dist/obj.js +7 -1
- package/dist/render.d.ts +9 -4
- package/dist/render.js +31 -3
- package/dist/shared/asset-url.js +82 -82
- package/dist/shared/crc32.d.ts +2 -2
- package/dist/shared/crc32.js +47 -47
- package/dist/shared/errors.js +36 -36
- package/dist/shared/inflate.js +284 -284
- package/dist/shared/normals.js +26 -26
- package/dist/shared/scene-scope.js +54 -54
- package/dist/shared/trailing-data.js +45 -42
- package/dist/threemf-beam-reader.d.ts +19 -0
- package/dist/threemf-beam-reader.js +165 -0
- package/dist/threemf-beams.d.ts +52 -0
- package/dist/threemf-beams.js +268 -0
- package/dist/threemf-boolean-read.d.ts +10 -0
- package/dist/threemf-boolean-read.js +113 -0
- package/dist/threemf-boolean.d.ts +59 -0
- package/dist/threemf-boolean.js +20 -0
- package/dist/threemf-csg.d.ts +14 -0
- package/dist/threemf-csg.js +193 -0
- package/dist/threemf-displacement-read.d.ts +8 -0
- package/dist/threemf-displacement-read.js +213 -0
- package/dist/threemf-displacement.d.ts +71 -0
- package/dist/threemf-displacement.js +18 -0
- package/dist/threemf-evaluate.d.ts +36 -0
- package/dist/threemf-evaluate.js +86 -0
- package/dist/threemf-functions.d.ts +33 -0
- package/dist/threemf-functions.js +359 -0
- package/dist/threemf-lattice-mesh.d.ts +9 -0
- package/dist/threemf-lattice-mesh.js +115 -0
- package/dist/threemf-lattice.js +10 -145
- package/dist/threemf-levelset.d.ts +23 -0
- package/dist/threemf-levelset.js +105 -0
- package/dist/threemf-materials.d.ts +89 -2
- package/dist/threemf-materials.js +194 -11
- package/dist/threemf-mesh.d.ts +21 -6
- package/dist/threemf-mesh.js +266 -45
- package/dist/threemf-secure.d.ts +50 -0
- package/dist/threemf-secure.js +153 -0
- package/dist/threemf-tessellate.d.ts +37 -0
- package/dist/threemf-tessellate.js +277 -0
- package/dist/threemf-volume-colour.d.ts +25 -0
- package/dist/threemf-volume-colour.js +233 -0
- package/dist/threemf-volume-sample.d.ts +8 -0
- package/dist/threemf-volume-sample.js +65 -0
- package/dist/threemf-volumetric-read.d.ts +15 -0
- package/dist/threemf-volumetric-read.js +539 -0
- package/dist/threemf-volumetric.d.ts +177 -0
- package/dist/threemf-volumetric.js +28 -0
- package/dist/threemf.d.ts +73 -4
- package/dist/threemf.js +235 -41
- package/dist/types.d.ts +18 -0
- package/dist/workers/usd.js +1 -1
- package/package.json +291 -246
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
// The reader of the 3MF Volumetric and Implicit Extensions as hooks of the core reader, so that
|
|
2
|
+
// `parse3MFVolumetric` and `parse3MFEvaluated` share it.
|
|
3
|
+
import { beamReader, finishLattice } from './threemf-beam-reader.js';
|
|
4
|
+
import { threeMFBeamDistance } from './threemf-beams.js';
|
|
5
|
+
import { MAX_CELLS, meshLevelSet } from './threemf-levelset.js';
|
|
6
|
+
import { colourMesh, MAX_COLOUR_SAMPLES } from './threemf-volume-colour.js';
|
|
7
|
+
import { LoaderError } from './types.js';
|
|
8
|
+
import { limits, throwIfAborted } from './util.js';
|
|
9
|
+
const F = '3MF';
|
|
10
|
+
const MAX_SHEETS = 65_536;
|
|
11
|
+
const MAX_BEAMS = 1_000_000;
|
|
12
|
+
const bad = (message) => {
|
|
13
|
+
throw new LoaderError(F, 'syntax', message);
|
|
14
|
+
};
|
|
15
|
+
const required = (a, key, element) => a[key] ?? bad(`<${element}> without ${key}`);
|
|
16
|
+
const number = (value, fallback, what) => {
|
|
17
|
+
if (value === undefined)
|
|
18
|
+
return fallback;
|
|
19
|
+
const n = Number(value);
|
|
20
|
+
return value.trim() && Number.isFinite(n) ? n : bad(`invalid ${what}`);
|
|
21
|
+
};
|
|
22
|
+
/** An attribute under any prefix, as an extension qualifies those it adds to core elements. */
|
|
23
|
+
const prefixed = (a, name) => {
|
|
24
|
+
for (const key in a)
|
|
25
|
+
if (key === name || key.endsWith(`:${name}`))
|
|
26
|
+
return a[key];
|
|
27
|
+
return undefined;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* What one part's functions read: its image stacks, decoded by `@margoengine/decoders`, and distances to its meshes
|
|
31
|
+
* from `@margoengine/bvh`, both loaded only for a package that has a level set.
|
|
32
|
+
*/
|
|
33
|
+
async function fieldScope(model, part, surfaces) {
|
|
34
|
+
const [{ decodePNG }, { MeshBVH }, { pointInside }] = await Promise.all([
|
|
35
|
+
import('@margoengine/decoders/png'),
|
|
36
|
+
import('@margoengine/bvh'),
|
|
37
|
+
import('@margoengine/bvh/inside'),
|
|
38
|
+
]), voxels = new Map();
|
|
39
|
+
for (const f of part.functions.values()) {
|
|
40
|
+
if (f.kind !== 'image' || voxels.has(f.imageId))
|
|
41
|
+
continue;
|
|
42
|
+
const { rows, columns, sheets, id } = part.images.get(f.imageId);
|
|
43
|
+
if (rows * columns * sheets.length > 2 ** 27)
|
|
44
|
+
throw new LoaderError(F, 'limit', `image3d ${id.slice(0, 32)} holds more than ${2 ** 27} voxels`);
|
|
45
|
+
const data = new Uint16Array(rows * columns * sheets.length * 4);
|
|
46
|
+
sheets.forEach((sheet, i) => {
|
|
47
|
+
let picture;
|
|
48
|
+
try {
|
|
49
|
+
picture = decodePNG(sheet.data, { bitDepth: 16, maxPixels: rows * columns });
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
bad(`${sheet.path.slice(0, 128)}: ${error.message}`);
|
|
53
|
+
}
|
|
54
|
+
if (picture.width !== columns || picture.height !== rows)
|
|
55
|
+
bad(`${sheet.path.slice(0, 128)} is not ${columns} x ${rows} pixels`);
|
|
56
|
+
data.set(picture.data, i * rows * columns * 4);
|
|
57
|
+
});
|
|
58
|
+
voxels.set(f.imageId, { rows, columns, sheets: sheets.length, data });
|
|
59
|
+
}
|
|
60
|
+
const distances = new Map();
|
|
61
|
+
const lattices = new Map();
|
|
62
|
+
const scope = {
|
|
63
|
+
functions: part.functions,
|
|
64
|
+
voxels: (imageId) => voxels.get(imageId),
|
|
65
|
+
lattice(objectId) {
|
|
66
|
+
let found = lattices.get(objectId);
|
|
67
|
+
if (!found) {
|
|
68
|
+
const lattice = part.lattices.get(objectId) ?? bad(`a beamlattice node needs a lattice object ${objectId.slice(0, 32)}`), own = surfaces.get(objectId), beams = threeMFBeamDistance(own.vertices.result(), lattice), { clippingMode, clippingMesh } = lattice;
|
|
69
|
+
if (clippingMode === 'none')
|
|
70
|
+
found = beams;
|
|
71
|
+
else {
|
|
72
|
+
const clip = scope.distance(clippingMesh, true), side = clippingMode === 'inside' ? 1 : -1;
|
|
73
|
+
found = (x, y, z) => Math.max(beams(x, y, z), side * clip(x, y, z));
|
|
74
|
+
}
|
|
75
|
+
lattices.set(objectId, found);
|
|
76
|
+
}
|
|
77
|
+
return found;
|
|
78
|
+
},
|
|
79
|
+
distance(objectId, signed) {
|
|
80
|
+
const key = `${signed} ${objectId}`;
|
|
81
|
+
let found = distances.get(key);
|
|
82
|
+
if (!found) {
|
|
83
|
+
const mesh = surfaces.get(objectId);
|
|
84
|
+
if (!mesh?.triangles.length)
|
|
85
|
+
bad(`a distance needs a mesh object ${objectId.slice(0, 32)}`);
|
|
86
|
+
const bvh = MeshBVH.build({ positions: mesh.vertices.result(), stride: 3, indices: mesh.triangles.result() }), at = [0, 0, 0];
|
|
87
|
+
found = (x, y, z) => {
|
|
88
|
+
at[0] = x;
|
|
89
|
+
at[1] = y;
|
|
90
|
+
at[2] = z;
|
|
91
|
+
const { distance } = bvh.closestPointToPoint(at);
|
|
92
|
+
return signed && pointInside(bvh, at) ? -distance : distance;
|
|
93
|
+
};
|
|
94
|
+
distances.set(key, found);
|
|
95
|
+
}
|
|
96
|
+
return found;
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
return scope;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The volumetric reader's hooks. `addLattices` runs for each model part once its level sets are meshed and before its
|
|
103
|
+
* colours are sampled; `surfaces` holds the part's meshes without lattice triangles.
|
|
104
|
+
*/
|
|
105
|
+
export function volumetricStage(options, addLattices) {
|
|
106
|
+
const { levelSetResolution = 64, colorResolution = 32 } = options;
|
|
107
|
+
if (!(Number.isInteger(levelSetResolution) && levelSetResolution >= 2 && levelSetResolution <= MAX_CELLS))
|
|
108
|
+
throw new LoaderError(F, 'range', `levelSetResolution must be an integer from 2 to ${MAX_CELLS}`);
|
|
109
|
+
if (!(Number.isInteger(colorResolution) && colorResolution >= 1 && colorResolution <= MAX_COLOUR_SAMPLES))
|
|
110
|
+
throw new LoaderError(F, 'range', `colorResolution must be an integer from 1 to ${MAX_COLOUR_SAMPLES}`);
|
|
111
|
+
const parts = new Map(),
|
|
112
|
+
/** Each object the result lists, by its model part and id. */
|
|
113
|
+
listed = new Map(), result = { images: [], functions: [], volumes: [], objects: [] };
|
|
114
|
+
let sheets = 0, levelSets = 0;
|
|
115
|
+
const beams = beamReader(MAX_BEAMS);
|
|
116
|
+
const part = (model) => {
|
|
117
|
+
let found = parts.get(model);
|
|
118
|
+
if (!found)
|
|
119
|
+
parts.set(model, (found = {
|
|
120
|
+
namespaces: new Map(),
|
|
121
|
+
images: new Map(),
|
|
122
|
+
functions: new Map(),
|
|
123
|
+
volumes: new Map(),
|
|
124
|
+
objects: new Map(),
|
|
125
|
+
bases: new Set(),
|
|
126
|
+
lattices: new Map(),
|
|
127
|
+
}));
|
|
128
|
+
return found;
|
|
129
|
+
};
|
|
130
|
+
const resource = (p, a, element) => {
|
|
131
|
+
const id = required(a, 'id', element);
|
|
132
|
+
if (p.images.has(id) || p.functions.has(id) || p.volumes.has(id))
|
|
133
|
+
bad(`duplicate resource id ${id.slice(0, 32)}`);
|
|
134
|
+
return id;
|
|
135
|
+
};
|
|
136
|
+
const sampled = (a, element) => {
|
|
137
|
+
const field = {
|
|
138
|
+
functionId: required(a, 'functionid', element),
|
|
139
|
+
channel: required(a, 'channel', element),
|
|
140
|
+
minFeatureSize: number(a.minfeaturesize, 0, 'minfeaturesize'),
|
|
141
|
+
fallback: number(a.fallbackvalue, 0, 'fallbackvalue'),
|
|
142
|
+
}, { transform } = a;
|
|
143
|
+
if (transform !== undefined) {
|
|
144
|
+
const values = transform.trim().split(/\s+/).map(Number);
|
|
145
|
+
if (values.length !== 12 || !values.every(Number.isFinite))
|
|
146
|
+
bad(`invalid ${element} transform`);
|
|
147
|
+
field.transform = values;
|
|
148
|
+
}
|
|
149
|
+
return field;
|
|
150
|
+
};
|
|
151
|
+
const open = (model, name, a) => {
|
|
152
|
+
const p = part(model), { implicit, volume } = p;
|
|
153
|
+
if (implicit) {
|
|
154
|
+
if (name === 'in' || name === 'out') {
|
|
155
|
+
p.list = name;
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (p.list) {
|
|
159
|
+
const reference = name.endsWith('ref'), type = (reference ? name.slice(0, -3) : name), port = {
|
|
160
|
+
name: required(a, 'identifier', name),
|
|
161
|
+
type: (type === 'resource' ? 'resourceid' : type),
|
|
162
|
+
};
|
|
163
|
+
if (!['scalar', 'vector', 'matrix', 'resourceid'].includes(port.type))
|
|
164
|
+
bad(`unknown port <${name}>`);
|
|
165
|
+
if (reference)
|
|
166
|
+
port.ref = required(a, 'ref', name);
|
|
167
|
+
// A node's inputs and the function's outputs read values; the function's inputs and a node's outputs name them.
|
|
168
|
+
if (reference !== (p.node ? p.list === 'in' : p.list === 'out'))
|
|
169
|
+
bad(`<${name}> in the wrong list`);
|
|
170
|
+
const owner = p.node ?? implicit;
|
|
171
|
+
owner[p.list === 'in' ? 'inputs' : 'outputs'].push(port);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (p.node)
|
|
175
|
+
bad(`<${name}> inside a node`);
|
|
176
|
+
const { identifier, displayname, tag: _tag, ...attributes } = a;
|
|
177
|
+
p.node = {
|
|
178
|
+
id: identifier ?? bad(`<${name}> without identifier`),
|
|
179
|
+
op: name,
|
|
180
|
+
...(displayname === undefined ? {} : { name: displayname }),
|
|
181
|
+
attributes,
|
|
182
|
+
inputs: [],
|
|
183
|
+
outputs: [],
|
|
184
|
+
};
|
|
185
|
+
implicit.nodes.push(p.node);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
if (volume) {
|
|
189
|
+
if (name === 'color') {
|
|
190
|
+
if (volume.color)
|
|
191
|
+
bad('<volumedata> has two colors');
|
|
192
|
+
volume.color = sampled(a, name);
|
|
193
|
+
}
|
|
194
|
+
else if (name === 'composite') {
|
|
195
|
+
if (volume.composite)
|
|
196
|
+
bad('<volumedata> has two composites');
|
|
197
|
+
volume.composite = { baseMaterialId: required(a, 'basematerialid', name), mappings: [] };
|
|
198
|
+
}
|
|
199
|
+
else if (name === 'materialmapping') {
|
|
200
|
+
(volume.composite ?? bad('<materialmapping> outside <composite>')).mappings.push(sampled(a, name));
|
|
201
|
+
}
|
|
202
|
+
else if (name === 'property') {
|
|
203
|
+
const full = required(a, 'name', name), colon = full.indexOf(':'), namespace = p.namespaces.get(full.slice(0, colon));
|
|
204
|
+
if (colon < 1 || namespace === undefined)
|
|
205
|
+
bad(`property ${full.slice(0, 64)} needs a declared namespace prefix`);
|
|
206
|
+
if (volume.properties.some((q) => q.namespace === namespace && q.name === full.slice(colon + 1)))
|
|
207
|
+
bad(`property ${full.slice(0, 64)} is given twice`);
|
|
208
|
+
volume.properties.push({
|
|
209
|
+
...sampled(a, name),
|
|
210
|
+
name: full.slice(colon + 1),
|
|
211
|
+
namespace: namespace,
|
|
212
|
+
required: a.required === 'true' || a.required === '1',
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (p.image) {
|
|
218
|
+
if (name === 'imagestack') {
|
|
219
|
+
p.image.rows = number(a.rowcount, 0, 'rowcount');
|
|
220
|
+
p.image.columns = number(a.columncount, 0, 'columncount');
|
|
221
|
+
p.image.count = number(a.sheetcount, 0, 'sheetcount');
|
|
222
|
+
}
|
|
223
|
+
else if (name === 'imagesheet') {
|
|
224
|
+
if (++sheets > MAX_SHEETS)
|
|
225
|
+
throw new LoaderError(F, 'limit', `image sheets exceed ${MAX_SHEETS}`);
|
|
226
|
+
p.image.sheets.push({ path: required(a, 'path', name), data: new Uint8Array(0) });
|
|
227
|
+
}
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (name === 'image3d') {
|
|
231
|
+
const id = resource(p, a, name);
|
|
232
|
+
p.image = { id, ...(a.name === undefined ? {} : { name: a.name }), rows: 0, columns: 0, count: 0, sheets: [] };
|
|
233
|
+
p.images.set(id, p.image);
|
|
234
|
+
}
|
|
235
|
+
else if (name === 'functionfromimage3d') {
|
|
236
|
+
const id = resource(p, a, name), imageId = required(a, 'image3did', name);
|
|
237
|
+
if (!p.images.has(imageId))
|
|
238
|
+
bad(`image3d ${imageId.slice(0, 32)} is not defined before function ${id}`);
|
|
239
|
+
p.functions.set(id, {
|
|
240
|
+
id,
|
|
241
|
+
kind: 'image',
|
|
242
|
+
...(a.displayname === undefined ? {} : { name: a.displayname }),
|
|
243
|
+
imageId,
|
|
244
|
+
filter: a.filter === 'nearest' ? 'nearest' : 'linear',
|
|
245
|
+
tile: [a.tilestyleu ?? 'wrap', a.tilestylev ?? 'wrap', a.tilestylew ?? 'wrap'],
|
|
246
|
+
offset: number(a.valueoffset, 0, 'valueoffset'),
|
|
247
|
+
scale: number(a.valuescale, 1, 'valuescale'),
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
else if (name === 'implicitfunction') {
|
|
251
|
+
const id = resource(p, a, name);
|
|
252
|
+
p.implicit = {
|
|
253
|
+
id,
|
|
254
|
+
kind: 'implicit',
|
|
255
|
+
...(a.displayname === undefined ? {} : { name: a.displayname }),
|
|
256
|
+
inputs: [],
|
|
257
|
+
nodes: [],
|
|
258
|
+
outputs: [],
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
else if (name === 'volumedata') {
|
|
262
|
+
const id = resource(p, a, name);
|
|
263
|
+
p.volume = { id, properties: [] };
|
|
264
|
+
p.volumes.set(id, p.volume);
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
/** A finished graph reads only what came before, in matching types, and names no function that is not yet defined. */
|
|
268
|
+
const checkGraph = (p, f) => {
|
|
269
|
+
const known = new Map(f.inputs.map((i) => [`inputs.${i.name}`, i.type])), reads = (ports) => {
|
|
270
|
+
for (const { ref, type } of ports)
|
|
271
|
+
if (ref !== undefined && known.get(ref) !== type)
|
|
272
|
+
bad(`function ${f.id}: ${ref.slice(0, 64)} is not an earlier ${type} output`);
|
|
273
|
+
}, nodes = new Set();
|
|
274
|
+
for (const node of f.nodes) {
|
|
275
|
+
if (node.id === 'inputs' || node.id === 'outputs' || nodes.has(node.id))
|
|
276
|
+
bad(`function ${f.id}: node identifier ${node.id.slice(0, 64)} is reserved or used twice`);
|
|
277
|
+
nodes.add(node.id);
|
|
278
|
+
reads(node.inputs);
|
|
279
|
+
if (node.op === 'constresourceid' && node.attributes.value === f.id)
|
|
280
|
+
bad(`function ${f.id} references itself`);
|
|
281
|
+
for (const { name, type } of node.outputs)
|
|
282
|
+
known.set(`${node.id}.${name}`, type);
|
|
283
|
+
}
|
|
284
|
+
reads(f.outputs);
|
|
285
|
+
};
|
|
286
|
+
const close = (model, name) => {
|
|
287
|
+
const p = part(model);
|
|
288
|
+
if (p.implicit) {
|
|
289
|
+
if (name === 'implicitfunction') {
|
|
290
|
+
checkGraph(p, p.implicit);
|
|
291
|
+
p.functions.set(p.implicit.id, p.implicit);
|
|
292
|
+
p.implicit = undefined;
|
|
293
|
+
}
|
|
294
|
+
else if (name === 'in' || name === 'out')
|
|
295
|
+
p.list = undefined;
|
|
296
|
+
else if (name === p.node?.op && !p.list)
|
|
297
|
+
p.node = undefined;
|
|
298
|
+
}
|
|
299
|
+
else if (name === 'volumedata')
|
|
300
|
+
p.volume = undefined;
|
|
301
|
+
else if (name === 'image3d' && p.image) {
|
|
302
|
+
const { rows, columns, count, sheets: listed, id } = p.image;
|
|
303
|
+
if (!(rows > 0 && columns > 0 && count > 0) || count !== listed.length)
|
|
304
|
+
bad(`image3d ${id.slice(0, 32)} needs one imagestack with sheetcount sheets of a positive size`);
|
|
305
|
+
p.image = undefined;
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
const hooks = {
|
|
309
|
+
levelSets: true,
|
|
310
|
+
welded: true,
|
|
311
|
+
field: { open, close },
|
|
312
|
+
close(model, name) {
|
|
313
|
+
beams.close(part(model), name);
|
|
314
|
+
},
|
|
315
|
+
element(model, name, a) {
|
|
316
|
+
const p = part(model);
|
|
317
|
+
if (name === 'model') {
|
|
318
|
+
for (const key in a)
|
|
319
|
+
if (key.startsWith('xmlns:'))
|
|
320
|
+
p.namespaces.set(key.slice(6), a[key]);
|
|
321
|
+
}
|
|
322
|
+
else if (name === 'object')
|
|
323
|
+
p.object = a.id;
|
|
324
|
+
else if (name === 'basematerials' && a.id)
|
|
325
|
+
p.bases.add(a.id);
|
|
326
|
+
else if (name === 'beamlattice' && p.object !== undefined) {
|
|
327
|
+
if (p.lattices.has(p.object))
|
|
328
|
+
bad(`object ${p.object.slice(0, 32)} has two beam lattices`);
|
|
329
|
+
const object = model.objects.get(p.object);
|
|
330
|
+
p.lattices.set(p.object, beams.begin(p, a, object.pid, object.pindex));
|
|
331
|
+
}
|
|
332
|
+
else if (['beam', 'ball', 'beamset', 'ref', 'ballref'].includes(name))
|
|
333
|
+
beams.element(p, name, a);
|
|
334
|
+
else if (name === 'displacementmesh' && prefixed(a, 'volumeid') !== undefined)
|
|
335
|
+
// Volumetric extension 5.2: only a mesh or a level set, unless a shape's own extension allows more.
|
|
336
|
+
bad(`object ${p.object?.slice(0, 32)} is a displacement mesh, which takes no volume data`);
|
|
337
|
+
else if (name === 'mesh' && p.object !== undefined) {
|
|
338
|
+
const volumeId = prefixed(a, 'volumeid');
|
|
339
|
+
if (volumeId !== undefined) {
|
|
340
|
+
if (!p.volumes.has(volumeId))
|
|
341
|
+
bad(`volumedata ${volumeId.slice(0, 32)} is not defined before its mesh`);
|
|
342
|
+
p.objects.set(p.object, { volumeId });
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
else if (name === 'levelset' && p.object !== undefined) {
|
|
346
|
+
const volumeId = a.volumeid;
|
|
347
|
+
if (volumeId !== undefined && !p.volumes.has(volumeId))
|
|
348
|
+
bad(`volumedata ${volumeId.slice(0, 32)} is not defined before its level set`);
|
|
349
|
+
p.objects.set(p.object, {
|
|
350
|
+
volumeId,
|
|
351
|
+
levelSet: {
|
|
352
|
+
...sampled(a, name),
|
|
353
|
+
meshId: required(a, 'meshid', name),
|
|
354
|
+
boundingBoxOnly: a.meshbboxonly === 'true' || a.meshbboxonly === '1',
|
|
355
|
+
},
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
},
|
|
359
|
+
instance(model, object, m, scale) {
|
|
360
|
+
const found = listed.get(model)?.get(object.id);
|
|
361
|
+
if (!found)
|
|
362
|
+
return;
|
|
363
|
+
found.instances.push(m.map((v) => v * scale));
|
|
364
|
+
if (object.levelSet)
|
|
365
|
+
levelSets++;
|
|
366
|
+
},
|
|
367
|
+
hasGeometry: () => levelSets > 0,
|
|
368
|
+
async ready(bytes, { models, partPath }) {
|
|
369
|
+
const files = new Map();
|
|
370
|
+
for (const model of models.values()) {
|
|
371
|
+
const p = parts.get(model), modelPath = partPath(model);
|
|
372
|
+
if (!p)
|
|
373
|
+
continue;
|
|
374
|
+
const where = modelPath === undefined ? {} : { modelPath }, own = new Map(), surfaces = new Map();
|
|
375
|
+
let scope;
|
|
376
|
+
/** Built once, on the first level set, color or sample that needs it. */
|
|
377
|
+
const evaluator = () => (scope ??= fieldScope(model, p, surfaces));
|
|
378
|
+
listed.set(model, own);
|
|
379
|
+
for (const [objectId, lattice] of p.lattices) {
|
|
380
|
+
const mesh = model.objects.get(objectId).mesh;
|
|
381
|
+
if (!mesh)
|
|
382
|
+
bad(`lattice object ${objectId.slice(0, 32)} requires a mesh`);
|
|
383
|
+
finishLattice(lattice, mesh.vertices.result(), () => { });
|
|
384
|
+
}
|
|
385
|
+
for (const { count: _count, ...image } of p.images.values()) {
|
|
386
|
+
for (const sheet of image.sheets) {
|
|
387
|
+
let data = files.get(sheet.path);
|
|
388
|
+
if (!data)
|
|
389
|
+
files.set(sheet.path, (data = await bytes(sheet.path)));
|
|
390
|
+
sheet.data = data;
|
|
391
|
+
}
|
|
392
|
+
result.images.push({ ...image, ...where });
|
|
393
|
+
}
|
|
394
|
+
for (const f of p.functions.values())
|
|
395
|
+
result.functions.push({ ...f, ...where });
|
|
396
|
+
const check = ({ functionId, channel }, type) => {
|
|
397
|
+
const f = p.functions.get(functionId);
|
|
398
|
+
if (!f)
|
|
399
|
+
bad(`function ${functionId.slice(0, 32)} is not defined`);
|
|
400
|
+
const output = f.kind === 'implicit'
|
|
401
|
+
? f.outputs.find((o) => o.name === channel)?.type
|
|
402
|
+
: channel === 'color'
|
|
403
|
+
? 'vector'
|
|
404
|
+
: ['red', 'green', 'blue', 'alpha'].includes(channel)
|
|
405
|
+
? 'scalar'
|
|
406
|
+
: undefined;
|
|
407
|
+
if (!output || (type && output !== type))
|
|
408
|
+
bad(`function ${functionId.slice(0, 32)} has no ${type ?? ''} output ${channel.slice(0, 64)}`);
|
|
409
|
+
};
|
|
410
|
+
for (const volume of p.volumes.values()) {
|
|
411
|
+
if (volume.color)
|
|
412
|
+
check(volume.color, 'vector');
|
|
413
|
+
if (volume.composite) {
|
|
414
|
+
if (!p.bases.has(volume.composite.baseMaterialId) || !volume.composite.mappings.length)
|
|
415
|
+
bad(`volumedata ${volume.id} composite needs a basematerials group and a materialmapping`);
|
|
416
|
+
for (const mapping of volume.composite.mappings)
|
|
417
|
+
check(mapping, 'scalar');
|
|
418
|
+
const bases = model.properties.get(volume.composite.baseMaterialId)?.length;
|
|
419
|
+
if (volume.composite.mappings.length !== bases)
|
|
420
|
+
bad(`volumedata ${volume.id} composite needs one materialmapping for each of ${bases} base materials`);
|
|
421
|
+
}
|
|
422
|
+
for (const property of volume.properties)
|
|
423
|
+
check(property);
|
|
424
|
+
result.volumes.push({ ...volume, ...where });
|
|
425
|
+
}
|
|
426
|
+
for (const [objectId, { volumeId, levelSet }] of p.objects) {
|
|
427
|
+
let shape;
|
|
428
|
+
if (levelSet) {
|
|
429
|
+
check(levelSet, 'scalar');
|
|
430
|
+
const mesh = model.objects.get(levelSet.meshId)?.mesh;
|
|
431
|
+
if (!mesh)
|
|
432
|
+
bad(`level set ${objectId.slice(0, 32)} needs a mesh object ${levelSet.meshId.slice(0, 32)}`);
|
|
433
|
+
shape = {
|
|
434
|
+
...levelSet,
|
|
435
|
+
domain: {
|
|
436
|
+
positions: mesh.vertices.result(),
|
|
437
|
+
indices: mesh.triangles.result(),
|
|
438
|
+
},
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
const object = {
|
|
442
|
+
objectId,
|
|
443
|
+
...where,
|
|
444
|
+
...(volumeId === undefined ? {} : { volumeId }),
|
|
445
|
+
...(shape ? { levelSet: shape } : {}),
|
|
446
|
+
instances: [],
|
|
447
|
+
};
|
|
448
|
+
if (volumeId !== undefined) {
|
|
449
|
+
const volume = p.volumes.get(volumeId);
|
|
450
|
+
object.sample = async (positions) => {
|
|
451
|
+
const { sampleVolume } = await import('./threemf-volume-sample.js');
|
|
452
|
+
return sampleVolume(await evaluator(), volume, positions);
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
own.set(objectId, object);
|
|
456
|
+
result.objects.push(object);
|
|
457
|
+
}
|
|
458
|
+
// Distances and level set domains read each mesh as the file wrote it, before lattices and colour samples.
|
|
459
|
+
for (const [objectId, object] of model.objects)
|
|
460
|
+
if (object.mesh)
|
|
461
|
+
surfaces.set(objectId, object.mesh);
|
|
462
|
+
if (options.meshLevelSets !== false)
|
|
463
|
+
for (const object of own.values()) {
|
|
464
|
+
const { levelSet, objectId } = object;
|
|
465
|
+
if (!levelSet)
|
|
466
|
+
continue;
|
|
467
|
+
const { mesh, cell, coarse } = await meshLevelSet(await evaluator(), levelSet, levelSet.domain.positions, {
|
|
468
|
+
resolution: levelSetResolution,
|
|
469
|
+
maxTriangles: limits(options, F).maxTriangles,
|
|
470
|
+
signal: options.signal,
|
|
471
|
+
});
|
|
472
|
+
levelSet.cellSize = cell;
|
|
473
|
+
levelSet.unresolved = coarse;
|
|
474
|
+
model.objects.get(objectId).mesh = mesh;
|
|
475
|
+
surfaces.set(objectId, mesh);
|
|
476
|
+
}
|
|
477
|
+
await addLattices?.(model, p.lattices, surfaces, modelPath);
|
|
478
|
+
if (options.sampleColors === false)
|
|
479
|
+
continue;
|
|
480
|
+
for (const object of own.values()) {
|
|
481
|
+
const { color, composite } = (object.volumeId === undefined ? undefined : p.volumes.get(object.volumeId)) ?? {}, mesh = model.objects.get(object.objectId).mesh;
|
|
482
|
+
if (!mesh)
|
|
483
|
+
continue;
|
|
484
|
+
if (color)
|
|
485
|
+
mesh.fill = { scope: await evaluator(), fields: [color] };
|
|
486
|
+
else if (composite) {
|
|
487
|
+
const bases = model.properties.get(composite.baseMaterialId);
|
|
488
|
+
mesh.fill = { scope: await evaluator(), fields: composite.mappings, bases };
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
},
|
|
493
|
+
bucket: () => '',
|
|
494
|
+
corner() { },
|
|
495
|
+
group() { },
|
|
496
|
+
};
|
|
497
|
+
/**
|
|
498
|
+
* Samples volume colour onto every mesh the build reaches. It runs after every step that changes a surface, and
|
|
499
|
+
* before the core reader writes the triangles.
|
|
500
|
+
*/
|
|
501
|
+
const paint = (models) => {
|
|
502
|
+
const root = models.values().next().value, seen = new Set();
|
|
503
|
+
const visit = (model, id, path, depth) => {
|
|
504
|
+
const owner = path ? models.get(path.replace(/\\/g, '/').replace(/^\/+/, '').toLowerCase()) : model, object = owner?.objects.get(id);
|
|
505
|
+
// The core reader reports a missing object or a loop.
|
|
506
|
+
if (!owner || !object || seen.has(object) || depth > 64)
|
|
507
|
+
return;
|
|
508
|
+
seen.add(object);
|
|
509
|
+
for (const next of [...object.components, ...object.alternatives])
|
|
510
|
+
visit(owner, next.objectId, next.path, depth + 1);
|
|
511
|
+
const mesh = object.mesh;
|
|
512
|
+
if (!mesh?.triangles.length || !(mesh.fill || mesh.fills?.some(Boolean)))
|
|
513
|
+
return;
|
|
514
|
+
throwIfAborted(options.signal, F);
|
|
515
|
+
const sampled = colourMesh(owner, object, mesh, {
|
|
516
|
+
resolution: colorResolution,
|
|
517
|
+
maxTriangles: limits(options, F).maxTriangles,
|
|
518
|
+
}), found = listed.get(owner)?.get(id);
|
|
519
|
+
if (found) {
|
|
520
|
+
found.colorSpacing = sampled.spacing;
|
|
521
|
+
found.colorUnresolved = sampled.coarse;
|
|
522
|
+
}
|
|
523
|
+
object.mesh = sampled.mesh;
|
|
524
|
+
};
|
|
525
|
+
for (const item of root.items)
|
|
526
|
+
visit(root, item.objectId, item.path, 0);
|
|
527
|
+
};
|
|
528
|
+
return {
|
|
529
|
+
hooks,
|
|
530
|
+
paint,
|
|
531
|
+
/** The `sample` of an object with volume data, built or only read by another shape. */
|
|
532
|
+
sampleOf: (objectId, modelPath) => result.objects.find((o) => o.objectId === objectId && o.modelPath === modelPath)?.sample,
|
|
533
|
+
/** What the reader adds to the core result, once the build has been walked. */
|
|
534
|
+
result() {
|
|
535
|
+
// The build walk has filled the instances; an object nothing builds, such as a level set's domain, is left out.
|
|
536
|
+
return { ...result, objects: result.objects.filter((o) => o.instances.length) };
|
|
537
|
+
},
|
|
538
|
+
};
|
|
539
|
+
}
|