@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
package/dist/threemf-lattice.js
CHANGED
|
@@ -2,34 +2,8 @@ import { LoaderError } from './types.js';
|
|
|
2
2
|
import { limits, resolvePath, throwIfAborted } from './util.js';
|
|
3
3
|
import { read3MFMaterials } from './threemf-materials.js';
|
|
4
4
|
import { readXMLTree, decodeXML } from './xml.js';
|
|
5
|
+
import { attribute, beamReader, choice, finishLattice, index, number } from './threemf-beam-reader.js';
|
|
5
6
|
const F = '3MF';
|
|
6
|
-
const attribute = (a, name) => a[name] ?? Object.entries(a).find(([key]) => key.endsWith(':' + name))?.[1];
|
|
7
|
-
function number(value, name, fallback) {
|
|
8
|
-
const n = value === undefined ? fallback : Number(value);
|
|
9
|
-
if (n === undefined || !Number.isFinite(n))
|
|
10
|
-
throw new LoaderError(F, 'syntax', `${name} must be a finite number`);
|
|
11
|
-
return n;
|
|
12
|
-
}
|
|
13
|
-
function index(value, name) {
|
|
14
|
-
const n = number(value, name);
|
|
15
|
-
if (!Number.isSafeInteger(n) || n < 0 || n >= 2147483648)
|
|
16
|
-
throw new LoaderError(F, 'range', `${name} must be a nonnegative 31-bit index`);
|
|
17
|
-
return n;
|
|
18
|
-
}
|
|
19
|
-
function positive(value, name, fallback) {
|
|
20
|
-
const n = number(value, name, fallback);
|
|
21
|
-
if (n <= 0)
|
|
22
|
-
throw new LoaderError(F, 'range', `${name} must be positive`);
|
|
23
|
-
return n;
|
|
24
|
-
}
|
|
25
|
-
function choice(value, values, fallback) {
|
|
26
|
-
if (value === undefined)
|
|
27
|
-
return fallback;
|
|
28
|
-
if (!values.includes(value))
|
|
29
|
-
throw new LoaderError(F, 'range', `expected ${values.join(', ')}; got ${value}`);
|
|
30
|
-
return value;
|
|
31
|
-
}
|
|
32
|
-
const cap = (value) => choice(value, ['sphere', 'hemisphere', 'butt'], 'sphere');
|
|
33
7
|
const own = (a) => Object.assign(Object.create(null), a);
|
|
34
8
|
const normalize = (path) => path.replaceAll('\\', '/').replace(/^\/+/, '').toLowerCase();
|
|
35
9
|
const list = (value, name) => {
|
|
@@ -48,8 +22,9 @@ export async function parse3MFLattice(input, options = {}) {
|
|
|
48
22
|
throwIfAborted(options.signal, F);
|
|
49
23
|
const states = new Map(), instances = [], sliceInstances = [];
|
|
50
24
|
const ancestors = [];
|
|
51
|
-
let objectCount = 0,
|
|
52
|
-
|
|
25
|
+
let objectCount = 0, expandedLayers = 0;
|
|
26
|
+
const lattices = beamReader(maxBeams);
|
|
27
|
+
let slices = 0, vertices = 0, segments = 0;
|
|
53
28
|
const state = (m) => {
|
|
54
29
|
let s = states.get(m);
|
|
55
30
|
if (!s) {
|
|
@@ -58,10 +33,6 @@ export async function parse3MFLattice(input, options = {}) {
|
|
|
58
33
|
}
|
|
59
34
|
return s;
|
|
60
35
|
};
|
|
61
|
-
const propertyRef = (a, pid, p) => ({
|
|
62
|
-
pid: a.pid ?? pid,
|
|
63
|
-
property: a.pindex === undefined ? p : index(a.pindex, 'pindex'),
|
|
64
|
-
});
|
|
65
36
|
const hooks = {
|
|
66
37
|
element(m, name, a) {
|
|
67
38
|
const s = state(m), obj = s.object;
|
|
@@ -109,78 +80,11 @@ export async function parse3MFLattice(input, options = {}) {
|
|
|
109
80
|
else if (name === 'beamlattice') {
|
|
110
81
|
if (!obj || obj.lattice)
|
|
111
82
|
throw new LoaderError(F, 'syntax', 'beamlattice needs a distinct mesh object');
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
radius: positive(a.radius, 'radius'),
|
|
115
|
-
minLength: positive(a.minlength, 'minlength'),
|
|
116
|
-
cap: cap(a.cap),
|
|
117
|
-
ballMode: choice(attribute(a, 'ballmode'), ['none', 'mixed', 'all'], 'none'),
|
|
118
|
-
clippingMode: choice(a.clippingmode, ['none', 'inside', 'outside'], 'none'),
|
|
119
|
-
clippingMesh: a.clippingmesh,
|
|
120
|
-
representationMesh: a.representationmesh,
|
|
121
|
-
...r,
|
|
122
|
-
beams: [],
|
|
123
|
-
balls: [],
|
|
124
|
-
sets: [],
|
|
125
|
-
activeBeams: new Uint32Array(),
|
|
126
|
-
effectiveBalls: [],
|
|
127
|
-
};
|
|
128
|
-
const br = attribute(a, 'ballradius');
|
|
129
|
-
if (br !== undefined)
|
|
130
|
-
lattice.ballRadius = positive(br, 'ballradius');
|
|
131
|
-
if (lattice.ballMode !== 'none' && lattice.ballRadius === undefined)
|
|
132
|
-
throw new LoaderError(F, 'syntax', 'ballmode needs ballradius');
|
|
133
|
-
obj.lattice = lattice;
|
|
134
|
-
s.lattice = lattice;
|
|
135
|
-
}
|
|
136
|
-
else if (name === 'beam') {
|
|
137
|
-
const l = s.lattice;
|
|
138
|
-
if (!l)
|
|
139
|
-
throw new LoaderError(F, 'syntax', 'beam outside beamlattice');
|
|
140
|
-
if (++beams > maxBeams)
|
|
141
|
-
throw new LoaderError(F, 'limit', 'beam count exceeds maxBeams');
|
|
142
|
-
if (a.r2 !== undefined && a.r1 === undefined)
|
|
143
|
-
throw new LoaderError(F, 'syntax', 'r2 requires r1');
|
|
144
|
-
const r1 = positive(a.r1, 'r1', l.radius), beam = {
|
|
145
|
-
v1: index(a.v1, 'v1'),
|
|
146
|
-
v2: index(a.v2, 'v2'),
|
|
147
|
-
r1,
|
|
148
|
-
r2: positive(a.r2, 'r2', r1),
|
|
149
|
-
cap1: a.cap1 === undefined ? l.cap : cap(a.cap1),
|
|
150
|
-
cap2: a.cap2 === undefined ? l.cap : cap(a.cap2),
|
|
151
|
-
pid: a.pid ?? l.pid,
|
|
152
|
-
p1: a.p1 === undefined ? l.property : index(a.p1, 'p1'),
|
|
153
|
-
p2: a.p2 === undefined ? l.property : index(a.p2, 'p2'),
|
|
154
|
-
};
|
|
155
|
-
l.beams.push(beam);
|
|
156
|
-
}
|
|
157
|
-
else if (name === 'ball') {
|
|
158
|
-
const l = s.lattice;
|
|
159
|
-
if (!l)
|
|
160
|
-
throw new LoaderError(F, 'syntax', 'ball outside beamlattice');
|
|
161
|
-
if (++balls > maxBeams)
|
|
162
|
-
throw new LoaderError(F, 'limit', 'ball count exceeds maxBeams');
|
|
163
|
-
l.balls.push({
|
|
164
|
-
vertex: index(a.vindex, 'vindex'),
|
|
165
|
-
radius: positive(a.r, 'ball radius', l.ballRadius),
|
|
166
|
-
pid: a.pid ?? l.pid,
|
|
167
|
-
property: a.p === undefined ? l.property : index(a.p, 'ball property'),
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
else if (name === 'beamset') {
|
|
171
|
-
if (!s.lattice || s.beamSet)
|
|
172
|
-
throw new LoaderError(F, 'syntax', 'invalid beamset nesting');
|
|
173
|
-
s.beamSet = { name: a.name, identifier: a.identifier, beams: [], balls: [] };
|
|
83
|
+
const core = m.objects.get(obj.id);
|
|
84
|
+
obj.lattice = lattices.begin(s, a, core?.pid, core?.pindex);
|
|
174
85
|
}
|
|
175
|
-
else if (
|
|
176
|
-
|
|
177
|
-
throw new LoaderError(F, 'limit', 'beamset references exceed the native primitive budget');
|
|
178
|
-
s.beamSet.beams.push(index(a.index, 'beam ref'));
|
|
179
|
-
}
|
|
180
|
-
else if (name === 'ballref' && s.beamSet) {
|
|
181
|
-
if (++references > maxBeams * 2)
|
|
182
|
-
throw new LoaderError(F, 'limit', 'beamset references exceed the native primitive budget');
|
|
183
|
-
s.beamSet.balls.push(index(a.index, 'ball ref'));
|
|
86
|
+
else if (['beam', 'ball', 'beamset', 'ref', 'ballref'].includes(name)) {
|
|
87
|
+
lattices.element(s, name, a);
|
|
184
88
|
}
|
|
185
89
|
else if (name === 'slicestack') {
|
|
186
90
|
if (s.stack || !a.id || s.stacks.has(a.id))
|
|
@@ -239,22 +143,13 @@ export async function parse3MFLattice(input, options = {}) {
|
|
|
239
143
|
},
|
|
240
144
|
close(m, name) {
|
|
241
145
|
const s = state(m);
|
|
146
|
+
lattices.close(s, name);
|
|
242
147
|
if (name === 'object') {
|
|
243
148
|
if (s.object)
|
|
244
149
|
s.object.vertices = Float64Array.from(s.xyz);
|
|
245
150
|
s.object = undefined;
|
|
246
151
|
s.xyz = [];
|
|
247
152
|
}
|
|
248
|
-
else if (name === 'beamlattice')
|
|
249
|
-
s.lattice = undefined;
|
|
250
|
-
else if (name === 'beamset' && s.beamSet && s.lattice) {
|
|
251
|
-
s.lattice.sets.push({
|
|
252
|
-
...s.beamSet,
|
|
253
|
-
beams: Uint32Array.from(s.beamSet.beams),
|
|
254
|
-
balls: Uint32Array.from(s.beamSet.balls),
|
|
255
|
-
});
|
|
256
|
-
s.beamSet = undefined;
|
|
257
|
-
}
|
|
258
153
|
else if (name === 'slice' && s.slice) {
|
|
259
154
|
s.slice.vertices = Float64Array.from(s.xy);
|
|
260
155
|
s.slice = undefined;
|
|
@@ -359,39 +254,9 @@ export async function parse3MFLattice(input, options = {}) {
|
|
|
359
254
|
const l = o.lattice;
|
|
360
255
|
if (!l)
|
|
361
256
|
continue;
|
|
362
|
-
if (!l.beams.length)
|
|
363
|
-
throw new LoaderError(F, 'syntax', 'beamlattice requires at least one beam');
|
|
364
257
|
if (!m.objects.get(o.id)?.mesh || m.objects.get(o.id).components.length)
|
|
365
258
|
throw new LoaderError(F, 'syntax', 'lattice object requires a mesh');
|
|
366
|
-
|
|
367
|
-
l.beams.forEach((beam, i) => {
|
|
368
|
-
if (beam.v1 === beam.v2 || beam.v1 >= pointCount || beam.v2 >= pointCount)
|
|
369
|
-
throw new LoaderError(F, 'range', 'invalid beam endpoints');
|
|
370
|
-
checkProperty(s, beam.pid, beam.p1);
|
|
371
|
-
checkProperty(s, beam.pid, beam.p2);
|
|
372
|
-
const a = beam.v1 * 3, b = beam.v2 * 3;
|
|
373
|
-
if (Math.hypot(o.vertices[a] - o.vertices[b], o.vertices[a + 1] - o.vertices[b + 1], o.vertices[a + 2] - o.vertices[b + 2]) >= l.minLength) {
|
|
374
|
-
active.push(i);
|
|
375
|
-
ends.add(beam.v1);
|
|
376
|
-
ends.add(beam.v2);
|
|
377
|
-
}
|
|
378
|
-
});
|
|
379
|
-
l.activeBeams = Uint32Array.from(active);
|
|
380
|
-
const explicit = new Map();
|
|
381
|
-
for (const ball of l.balls) {
|
|
382
|
-
if (ball.vertex >= pointCount || explicit.has(ball.vertex))
|
|
383
|
-
throw new LoaderError(F, 'range', 'invalid or repeated ball vertex');
|
|
384
|
-
checkProperty(s, ball.pid, ball.property);
|
|
385
|
-
explicit.set(ball.vertex, ball);
|
|
386
|
-
}
|
|
387
|
-
if (l.ballMode !== 'none')
|
|
388
|
-
for (const vertex of ends) {
|
|
389
|
-
const ball = explicit.get(vertex);
|
|
390
|
-
if (ball)
|
|
391
|
-
l.effectiveBalls.push({ ...ball });
|
|
392
|
-
else if (l.ballMode === 'all')
|
|
393
|
-
l.effectiveBalls.push({ vertex, radius: l.ballRadius, pid: l.pid, property: l.property });
|
|
394
|
-
}
|
|
259
|
+
finishLattice(l, o.vertices, (pid, p) => checkProperty(s, pid, p));
|
|
395
260
|
for (const set of l.sets)
|
|
396
261
|
if (set.beams.some((i) => i >= l.beams.length) || set.balls.some((i) => i >= l.balls.length))
|
|
397
262
|
throw new LoaderError(F, 'range', 'beamset index outside source arrays');
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type FieldScope } from './threemf-functions.js';
|
|
2
|
+
import type { ThreeMFSampledField } from './threemf-volumetric.js';
|
|
3
|
+
import type { ThreeMFMesh } from './threemf.js';
|
|
4
|
+
/** The most cells along the longest side. */
|
|
5
|
+
export declare const MAX_CELLS = 512;
|
|
6
|
+
export interface LevelSetOptions {
|
|
7
|
+
/** Cells wanted along the longest side of the domain. */
|
|
8
|
+
resolution: number;
|
|
9
|
+
maxTriangles: number;
|
|
10
|
+
signal?: AbortSignal;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The triangles of a level set in its object's coordinates, and whether its grid is too coarse for the
|
|
14
|
+
* `minfeaturesize` the file asks for (two cells to a feature).
|
|
15
|
+
*/
|
|
16
|
+
export declare function meshLevelSet(scope: FieldScope, levelSet: ThreeMFSampledField & {
|
|
17
|
+
meshId: string;
|
|
18
|
+
boundingBoxOnly: boolean;
|
|
19
|
+
}, domain: ArrayLike<number>, { resolution, maxTriangles, signal }: LevelSetOptions): Promise<{
|
|
20
|
+
mesh: ThreeMFMesh;
|
|
21
|
+
cell: number;
|
|
22
|
+
coarse: boolean;
|
|
23
|
+
}>;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Meshes a 3MF level set (volumetric extension chapter 4): the part of the domain mesh, or of its box, where the
|
|
2
|
+
// function is below zero, polygonized by `@margoengine/procedural`'s Marching Cubes on max(function, domain distance).
|
|
3
|
+
// The grid's points are multiples of a power of two, so blocks of it meshed one after another meet in the same
|
|
4
|
+
// vertices.
|
|
5
|
+
import { compileField } from './threemf-functions.js';
|
|
6
|
+
import { LoaderError } from './types.js';
|
|
7
|
+
import { floats, throwIfAborted, uints } from './util.js';
|
|
8
|
+
const F = '3MF';
|
|
9
|
+
/** Cells along the side of a block: the most points `marchingCubes` takes on one axis is 128. */
|
|
10
|
+
const BLOCK = 32;
|
|
11
|
+
/** The most cells along the longest side. */
|
|
12
|
+
export const MAX_CELLS = 512;
|
|
13
|
+
const NONE = 0xffffffff;
|
|
14
|
+
/** The signed distance to an axis-aligned box, negative inside. */
|
|
15
|
+
const boxDistance = (low, high) => (x, y, z) => {
|
|
16
|
+
const a = Math.max(low[0] - x, x - high[0]), b = Math.max(low[1] - y, y - high[1]), c = Math.max(low[2] - z, z - high[2]), inner = Math.max(a, b, c);
|
|
17
|
+
return inner <= 0 ? inner : Math.hypot(Math.max(a, 0), Math.max(b, 0), Math.max(c, 0));
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* The triangles of a level set in its object's coordinates, and whether its grid is too coarse for the
|
|
21
|
+
* `minfeaturesize` the file asks for (two cells to a feature).
|
|
22
|
+
*/
|
|
23
|
+
export async function meshLevelSet(scope, levelSet, domain, { resolution, maxTriangles, signal }) {
|
|
24
|
+
const { marchingCubes } = await import('@margoengine/procedural/marching-cubes');
|
|
25
|
+
const low = [Infinity, Infinity, Infinity], high = [-Infinity, -Infinity, -Infinity];
|
|
26
|
+
for (let i = 0; i < domain.length; i++) {
|
|
27
|
+
low[i % 3] = Math.min(low[i % 3], domain[i]);
|
|
28
|
+
high[i % 3] = Math.max(high[i % 3], domain[i]);
|
|
29
|
+
}
|
|
30
|
+
const longest = Math.max(...high.map((v, k) => v - low[k]));
|
|
31
|
+
if (!(longest > 0))
|
|
32
|
+
throw new LoaderError(F, 'syntax', `the domain mesh ${levelSet.meshId.slice(0, 32)} is empty`);
|
|
33
|
+
const { minFeatureSize, fallback, transform } = levelSet, wanted = Math.max(longest / MAX_CELLS, Math.min(longest / resolution, minFeatureSize > 0 ? minFeatureSize / 2 : Infinity)), cell = 2 ** Math.floor(Math.log2(wanted));
|
|
34
|
+
const inside = levelSet.boundingBoxOnly ? boxDistance(low, high) : scope.distance(levelSet.meshId, true);
|
|
35
|
+
const pos = new Float64Array(3), program = compileField(scope, levelSet.functionId, new Map([['pos', pos]])), output = program.outputs.get(levelSet.channel);
|
|
36
|
+
const m = transform ?? [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0];
|
|
37
|
+
const first = low.map((v) => Math.floor(v / cell) - 1), last = high.map((v) => Math.ceil(v / cell) + 1);
|
|
38
|
+
// The domain distance at the centres of cubes of 8 cells: a distance changes no faster than its point moves, so a
|
|
39
|
+
// cube whose centre is farther from the domain's surface than its corners lies wholly inside or outside it.
|
|
40
|
+
const coarseCell = 8 * cell, corner = (Math.sqrt(3) / 2) * coarseCell, centres = new Map();
|
|
41
|
+
const bound = (x, y, z) => {
|
|
42
|
+
const i = Math.floor(x / coarseCell), j = Math.floor(y / coarseCell), k = Math.floor(z / coarseCell), key = `${i} ${j} ${k}`;
|
|
43
|
+
let centre = centres.get(key);
|
|
44
|
+
if (centre === undefined)
|
|
45
|
+
centres.set(key, (centre = inside((i + 0.5) * coarseCell, (j + 0.5) * coarseCell, (k + 0.5) * coarseCell)));
|
|
46
|
+
return centre < -corner ? -Infinity : centre > corner ? centre - corner : inside(x, y, z);
|
|
47
|
+
};
|
|
48
|
+
// max(function, domain distance) in sign; the domain is asked only where the function is not already outside.
|
|
49
|
+
const field = (x, y, z) => {
|
|
50
|
+
for (let k = 0; k < 3; k++)
|
|
51
|
+
pos[k] = x * m[k] + y * m[3 + k] + z * m[6 + k] + m[9 + k];
|
|
52
|
+
program.run();
|
|
53
|
+
const value = Number.isFinite(output[0]) ? output[0] : fallback;
|
|
54
|
+
return value > 0 ? value : Math.max(value, bound(x, y, z));
|
|
55
|
+
};
|
|
56
|
+
const vertices = floats(), triangles = uints(), welded = new Map();
|
|
57
|
+
for (let z = first[2]; z < last[2]; z += BLOCK)
|
|
58
|
+
for (let y = first[1]; y < last[1]; y += BLOCK)
|
|
59
|
+
for (let x = first[0]; x < last[0]; x += BLOCK) {
|
|
60
|
+
throwIfAborted(signal, F);
|
|
61
|
+
const from = [x, y, z], cells = from.map((v, k) => Math.max(2, Math.min(BLOCK, last[k] - v))), centre = from.map((v, k) => (v + cells[k] / 2) * cell), reach = (Math.hypot(...cells) / 2) * cell;
|
|
62
|
+
// A distance changes no faster than its point moves: a block this far outside the domain holds no surface.
|
|
63
|
+
if (inside(centre[0], centre[1], centre[2]) > reach)
|
|
64
|
+
continue;
|
|
65
|
+
const surface = marchingCubes(field, {
|
|
66
|
+
resolution: [cells[0] + 1, cells[1] + 1, cells[2] + 1],
|
|
67
|
+
bounds: [
|
|
68
|
+
[from[0] * cell, from[1] * cell, from[2] * cell],
|
|
69
|
+
[(from[0] + cells[0]) * cell, (from[1] + cells[1]) * cell, (from[2] + cells[2]) * cell],
|
|
70
|
+
],
|
|
71
|
+
});
|
|
72
|
+
if (!surface)
|
|
73
|
+
continue;
|
|
74
|
+
const points = surface.vertices, stride = 6, ids = [];
|
|
75
|
+
for (let i = 0; i < points.length; i += stride) {
|
|
76
|
+
const key = `${points[i]},${points[i + 1]},${points[i + 2]}`;
|
|
77
|
+
let id = welded.get(key);
|
|
78
|
+
if (id === undefined) {
|
|
79
|
+
id = vertices.length / 3;
|
|
80
|
+
welded.set(key, id);
|
|
81
|
+
vertices.push3(points[i], points[i + 1], points[i + 2]);
|
|
82
|
+
}
|
|
83
|
+
ids.push(id);
|
|
84
|
+
}
|
|
85
|
+
const index = surface.indices;
|
|
86
|
+
for (let t = 0; t < index.length; t += 3) {
|
|
87
|
+
const a = ids[index[t]], b = ids[index[t + 1]], c = ids[index[t + 2]];
|
|
88
|
+
if (a === b || b === c || c === a)
|
|
89
|
+
continue;
|
|
90
|
+
if (triangles.length / 3 >= maxTriangles)
|
|
91
|
+
throw new LoaderError(F, 'limit', `a level set exceeds ${maxTriangles} triangles; lower levelSetResolution or raise maxTriangles`);
|
|
92
|
+
triangles.push3(a, b, c);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const count = triangles.length / 3, pid = uints(), p = uints();
|
|
96
|
+
for (let t = 0; t < count; t++) {
|
|
97
|
+
pid.push(NONE);
|
|
98
|
+
p.push3(NONE, NONE, NONE);
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
mesh: { vertices, triangles, pid, p },
|
|
102
|
+
cell,
|
|
103
|
+
coarse: minFeatureSize > 0 && cell > minFeatureSize / 2,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
@@ -34,22 +34,106 @@ export interface ThreeMFLayer {
|
|
|
34
34
|
*/
|
|
35
35
|
blend: 'mix' | 'multiply';
|
|
36
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* The metalness and roughness a group's triangles are shown with: the materials extension's `pbmetallic`, or its
|
|
39
|
+
* `pbmetallictexturedisplayproperties` (chapter 7 of the extension).
|
|
40
|
+
*/
|
|
41
|
+
export interface ThreeMFMetallic {
|
|
42
|
+
/** The element's `name`, usually the material's. */
|
|
43
|
+
name: string;
|
|
44
|
+
/** The metalness, 0 to 1. With `metalnessTexture` it is the factor over the sampled value (default 1). */
|
|
45
|
+
metalness: number;
|
|
46
|
+
/** The roughness, 0 to 1. With `roughnessTexture` it is the factor over the sampled value (default 1). */
|
|
47
|
+
roughness: number;
|
|
48
|
+
/**
|
|
49
|
+
* Index into `textures` of the linear map metalness is sampled from, at the group's own coordinates and with the tile
|
|
50
|
+
* style and filter of its colour texture: the red channel, or the green one when `roughnessTexture` is the same
|
|
51
|
+
* resource.
|
|
52
|
+
*/
|
|
53
|
+
metalnessTexture?: number;
|
|
54
|
+
/** Index into `textures` of the linear map roughness is sampled from, in its red channel. */
|
|
55
|
+
roughnessTexture?: number;
|
|
56
|
+
/** `basecolorfactor`, sRGB red, green, blue and linear alpha over the colour texture, when it is not white. */
|
|
57
|
+
baseColorFactor?: [number, number, number, number];
|
|
58
|
+
/** In a `multiproperties` stack, the index of the layer these properties come from. */
|
|
59
|
+
layer?: number;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The specular colour and glossiness a group's triangles are shown with: the materials extension's `pbspecular`, or its
|
|
63
|
+
* `pbspeculartexturedisplayproperties` (chapter 7 of the extension). The diffuse colour is the group's own colour or
|
|
64
|
+
* colour texture.
|
|
65
|
+
*/
|
|
66
|
+
export interface ThreeMFSpecular {
|
|
67
|
+
/** The element's `name`, usually the material's. */
|
|
68
|
+
name: string;
|
|
69
|
+
/**
|
|
70
|
+
* Reflectance at normal incidence, sRGB red, green and blue (default `#383838`, 4 % in linear light, as plastics have).
|
|
71
|
+
* With `specularTexture` it is the factor over the sampled colour (default white).
|
|
72
|
+
*/
|
|
73
|
+
specularColor: [number, number, number];
|
|
74
|
+
/** Smoothness, 0 (rough, the default) to 1 (mirror). With `glossinessTexture` it is the factor (default 1). */
|
|
75
|
+
glossiness: number;
|
|
76
|
+
/** Index into `textures` of the sRGB map the specular colour is sampled from, at the group's own coordinates. */
|
|
77
|
+
specularTexture?: number;
|
|
78
|
+
/**
|
|
79
|
+
* Index into `textures` of the linear map glossiness is sampled from: the alpha channel when it is the same resource
|
|
80
|
+
* as `specularTexture`, else the red one.
|
|
81
|
+
*/
|
|
82
|
+
glossinessTexture?: number;
|
|
83
|
+
/** `diffusefactor`, sRGB red, green, blue and linear alpha over the colour texture, when it is not white. */
|
|
84
|
+
diffuseFactor?: [number, number, number, number];
|
|
85
|
+
/** In a `multiproperties` stack, the index of the layer these properties come from. */
|
|
86
|
+
layer?: number;
|
|
87
|
+
}
|
|
88
|
+
/** The volume a translucent group's triangles enclose: the materials extension's `translucent` (section 7.5). */
|
|
89
|
+
export interface ThreeMFTranslucent {
|
|
90
|
+
/** The element's `name`, usually the print material's. */
|
|
91
|
+
name: string;
|
|
92
|
+
/**
|
|
93
|
+
* Linear attenuation of red, green and blue per metre, whatever the model's unit: light keeps `exp(-a * t)` of itself
|
|
94
|
+
* after `t` metres inside (Beer-Lambert). 0 is clear.
|
|
95
|
+
*/
|
|
96
|
+
attenuation: [number, number, number];
|
|
97
|
+
/** Index of refraction of red, green and blue. Default 1, 1, 1: light passes straight through. */
|
|
98
|
+
refractiveIndex: [number, number, number];
|
|
99
|
+
/** Roughness of the surface, 0 (the default) to 1. */
|
|
100
|
+
roughness: number;
|
|
101
|
+
/** In a `multiproperties` stack, the index of the layer these properties come from. */
|
|
102
|
+
layer?: number;
|
|
103
|
+
}
|
|
37
104
|
/** A group of {@link ThreeMFMaterials}: {@link LoadedGroup} with the texture its triangles sample. */
|
|
38
105
|
export interface ThreeMFMaterialGroup extends LoadedGroup {
|
|
39
106
|
/** Index into `textures`: the group's only texture, or the first texture layer of its `layers`. */
|
|
40
107
|
texture?: number;
|
|
41
108
|
/** The whole layer stack, when the group's triangles use a `multiproperties`; `create3MFMesh` draws it. */
|
|
42
109
|
layers?: ThreeMFLayer[];
|
|
110
|
+
/**
|
|
111
|
+
* The metallic display properties of the group's triangles (their first corner's, as the spec says). A group has at
|
|
112
|
+
* most one of `metallic`, `specular` and `translucent`, and none when the file gives no display properties. In a stack
|
|
113
|
+
* they are those of the lowest layer that has any. `create3MFMesh` draws them.
|
|
114
|
+
*/
|
|
115
|
+
metallic?: ThreeMFMetallic;
|
|
116
|
+
/** The specular display properties of the group's triangles, chosen as `metallic` is. */
|
|
117
|
+
specular?: ThreeMFSpecular;
|
|
118
|
+
/** The translucent display properties of the group's triangles, chosen as `metallic` is. */
|
|
119
|
+
translucent?: ThreeMFTranslucent;
|
|
43
120
|
}
|
|
44
121
|
/** What {@link parse3MFMaterials} returns: {@link ThreeMFResult} with textures, and one vertex per triangle corner. */
|
|
45
122
|
export interface ThreeMFMaterials extends ThreeMFResult {
|
|
46
123
|
groups: ThreeMFMaterialGroup[];
|
|
47
124
|
/** Every texture the package declares. */
|
|
48
125
|
textures: ThreeMFTexture[];
|
|
126
|
+
/**
|
|
127
|
+
* Metres in one unit of `positions`, which the reader sets: 1, or the length of the model's unit with
|
|
128
|
+
* `units: 'file'`. Default 1.
|
|
129
|
+
*/
|
|
130
|
+
metresPerUnit?: number;
|
|
49
131
|
}
|
|
50
132
|
/**
|
|
51
133
|
* Reads a 3MF package like `parse3MF` and adds the materials extension: `texture2d` images, `texture2dgroup`
|
|
52
|
-
* coordinates, `compositematerials` as mixed colours,
|
|
134
|
+
* coordinates, `compositematerials` as mixed colours, `multiproperties` layers, and the display properties as each
|
|
135
|
+
* group's `metallic` (`pbmetallic` and `pbmetallictexturedisplayproperties`), `specular` (`pbspecular` and
|
|
136
|
+
* `pbspeculartexturedisplayproperties`) or `translucent`. Every triangle corner gets its own
|
|
53
137
|
* vertex, so `uvs` and `colors` can differ across a seam; `uvs` use Margo's convention, v = 1 - the file's v. `colors` is
|
|
54
138
|
* present when corners of a triangle differ or a layer sets them. A group's `texture` indexes `textures`. In
|
|
55
139
|
* `multiproperties` colour layers mix by their alpha, and the first texture layer sets the group's texture: draw
|
|
@@ -58,7 +142,10 @@ export interface ThreeMFMaterials extends ThreeMFResult {
|
|
|
58
142
|
* @param input The package's bytes.
|
|
59
143
|
* @param options The same options as `parse3MF`.
|
|
60
144
|
* @returns The merged mesh with its coordinates, colours and images.
|
|
61
|
-
* @throws LoaderError with the codes of `parse3MF`, with `range` for a property index outside its group
|
|
145
|
+
* @throws LoaderError with the codes of `parse3MF`, with `range` for a property index outside its group or its display
|
|
146
|
+
* properties, for a metalness, roughness or glossiness outside 0 to 1 (a texture's factor: below 0), a negative
|
|
147
|
+
* attenuation or a refractive index of 0 or less, and `syntax` for display properties that are not defined or are of a
|
|
148
|
+
* kind their group cannot take, and for a colour or a number triplet that is not one.
|
|
62
149
|
*
|
|
63
150
|
* @example
|
|
64
151
|
* ```ts
|