@margoengine/loaders 0.0.0-stage → 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 -0
- package/NOTICE +4 -0
- package/README.md +1665 -2
- package/dist/3dm.d.ts +161 -0
- package/dist/3dm.js +289 -0
- package/dist/3ds.d.ts +124 -0
- package/dist/3ds.js +392 -0
- package/dist/acclaim.d.ts +90 -0
- package/dist/acclaim.js +312 -0
- package/dist/amf.d.ts +45 -0
- package/dist/amf.js +256 -0
- package/dist/babylon.d.ts +197 -0
- package/dist/babylon.js +701 -0
- package/dist/common.d.ts +7 -0
- package/dist/common.js +6 -0
- package/dist/crease.d.ts +7 -0
- package/dist/crease.js +67 -0
- package/dist/dae-animation.d.ts +66 -0
- package/dist/dae-animation.js +266 -0
- package/dist/dae-common.d.ts +95 -0
- package/dist/dae-common.js +226 -0
- package/dist/dae-controllers.d.ts +57 -0
- package/dist/dae-controllers.js +164 -0
- package/dist/dae-kinematics.d.ts +65 -0
- package/dist/dae-kinematics.js +461 -0
- package/dist/dae-pose.d.ts +131 -0
- package/dist/dae-pose.js +381 -0
- package/dist/dae-scene.d.ts +203 -0
- package/dist/dae-scene.js +279 -0
- package/dist/dae.d.ts +166 -0
- package/dist/dae.js +644 -0
- package/dist/detect.d.ts +20 -0
- package/dist/detect.js +72 -0
- 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 +122 -0
- package/dist/dxf.js +985 -0
- package/dist/engine.d.ts +41 -0
- package/dist/engine.js +120 -0
- package/dist/font-opentype.d.ts +14 -0
- package/dist/font-opentype.js +33 -0
- package/dist/font-woff.d.ts +23 -0
- package/dist/font-woff.js +293 -0
- package/dist/font.d.ts +41 -0
- package/dist/font.js +26 -0
- package/dist/gcode.d.ts +92 -0
- package/dist/gcode.js +369 -0
- package/dist/geo-model.d.ts +84 -0
- package/dist/geo-model.js +220 -0
- package/dist/geo-mvt.d.ts +15 -0
- package/dist/geo-mvt.js +302 -0
- package/dist/geo-shp.d.ts +15 -0
- package/dist/geo-shp.js +234 -0
- package/dist/geo-xml.d.ts +6 -0
- package/dist/geo-xml.js +165 -0
- package/dist/geo.d.ts +8 -0
- package/dist/geo.js +59 -0
- package/dist/geometry.d.ts +60 -0
- package/dist/geometry.js +273 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +28 -0
- package/dist/kmz.d.ts +78 -0
- package/dist/kmz.js +173 -0
- package/dist/las.d.ts +92 -0
- package/dist/las.js +110 -0
- package/dist/ldraw.d.ts +118 -0
- package/dist/ldraw.js +474 -0
- package/dist/load.d.ts +72 -0
- package/dist/load.js +205 -0
- package/dist/lottie.d.ts +154 -0
- package/dist/lottie.js +893 -0
- package/dist/lwo.d.ts +81 -0
- package/dist/lwo.js +454 -0
- package/dist/lzf.d.ts +13 -0
- package/dist/lzf.js +56 -0
- package/dist/materialx.d.ts +120 -0
- package/dist/materialx.js +563 -0
- package/dist/md2.d.ts +52 -0
- package/dist/md2.js +112 -0
- package/dist/mmcif.d.ts +14 -0
- package/dist/mmcif.js +246 -0
- package/dist/mmd.d.ts +244 -0
- package/dist/mmd.js +958 -0
- package/dist/mocap.d.ts +71 -0
- package/dist/mocap.js +178 -0
- package/dist/molecule-common.d.ts +4 -0
- package/dist/molecule-common.js +54 -0
- package/dist/mtl.d.ts +125 -0
- package/dist/mtl.js +550 -0
- package/dist/normals.d.ts +7 -0
- package/dist/normals.js +31 -0
- package/dist/nrrd.d.ts +70 -0
- package/dist/nrrd.js +785 -0
- package/dist/nurbs.d.ts +118 -0
- package/dist/nurbs.js +265 -0
- package/dist/obj.d.ts +145 -0
- package/dist/obj.js +462 -0
- package/dist/off.d.ts +10 -0
- package/dist/off.js +155 -0
- package/dist/outline-font.d.ts +87 -0
- package/dist/outline-font.js +110 -0
- package/dist/outline.d.ts +20 -0
- package/dist/outline.js +215 -0
- package/dist/pcd.d.ts +34 -0
- package/dist/pcd.js +258 -0
- package/dist/pdb.d.ts +67 -0
- package/dist/pdb.js +126 -0
- package/dist/ply.d.ts +35 -0
- package/dist/ply.js +392 -0
- package/dist/polygon.d.ts +9 -0
- package/dist/polygon.js +49 -0
- package/dist/prwm.d.ts +24 -0
- package/dist/prwm.js +114 -0
- package/dist/render.d.ts +32 -0
- package/dist/render.js +181 -0
- package/dist/sfnt-cff.d.ts +37 -0
- package/dist/sfnt-cff.js +749 -0
- package/dist/sfnt-layout.d.ts +24 -0
- package/dist/sfnt-layout.js +266 -0
- package/dist/sfnt-reader.d.ts +28 -0
- package/dist/sfnt-reader.js +87 -0
- package/dist/sfnt-variation.d.ts +40 -0
- package/dist/sfnt-variation.js +318 -0
- package/dist/shared/asset-url.js +82 -0
- package/dist/shared/crc32.d.ts +2 -0
- package/dist/shared/crc32.js +47 -0
- package/dist/shared/errors.js +36 -0
- package/dist/shared/inflate.js +284 -0
- package/dist/shared/normals.js +26 -0
- package/dist/shared/scene-scope.js +56 -0
- package/dist/shared/trailing-data.js +45 -0
- package/dist/stl.d.ts +64 -0
- package/dist/stl.js +309 -0
- package/dist/svg.d.ts +90 -0
- package/dist/svg.js +1007 -0
- package/dist/tds.d.ts +2 -0
- package/dist/tds.js +2 -0
- package/dist/textures.d.ts +42 -0
- package/dist/textures.js +53 -0
- 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.d.ts +139 -0
- package/dist/threemf-lattice.js +379 -0
- package/dist/threemf-levelset.d.ts +23 -0
- package/dist/threemf-levelset.js +105 -0
- package/dist/threemf-materials.d.ts +162 -0
- package/dist/threemf-materials.js +497 -0
- package/dist/threemf-mesh.d.ts +75 -0
- package/dist/threemf-mesh.js +422 -0
- 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 +170 -0
- package/dist/threemf.js +609 -0
- package/dist/tilt.d.ts +62 -0
- package/dist/tilt.js +186 -0
- package/dist/tmx.d.ts +151 -0
- package/dist/tmx.js +394 -0
- package/dist/ttf.d.ts +78 -0
- package/dist/ttf.js +568 -0
- package/dist/types.d.ts +108 -0
- package/dist/types.js +35 -0
- package/dist/usd.d.ts +2 -0
- package/dist/usd.js +2 -0
- package/dist/usd.worker.d.ts +1 -0
- package/dist/usdz.d.ts +50 -0
- package/dist/usdz.js +646 -0
- package/dist/util.d.ts +75 -0
- package/dist/util.js +241 -0
- package/dist/vdb.d.ts +111 -0
- package/dist/vdb.js +663 -0
- package/dist/vrml.d.ts +53 -0
- package/dist/vrml.js +532 -0
- package/dist/vtk-cells.d.ts +29 -0
- package/dist/vtk-cells.js +211 -0
- package/dist/vtk-legacy.d.ts +57 -0
- package/dist/vtk-legacy.js +439 -0
- package/dist/vtk-reader.d.ts +19 -0
- package/dist/vtk-reader.js +134 -0
- package/dist/vtk-xml.d.ts +96 -0
- package/dist/vtk-xml.js +578 -0
- package/dist/vtk.d.ts +13 -0
- package/dist/vtk.js +234 -0
- package/dist/woff-glyf.d.ts +17 -0
- package/dist/woff-glyf.js +215 -0
- package/dist/worker.d.ts +10 -0
- package/dist/worker.js +93 -0
- package/dist/workers/usd.js +3 -0
- package/dist/xfile.d.ts +99 -0
- package/dist/xfile.js +706 -0
- package/dist/xml.d.ts +107 -0
- package/dist/xml.js +843 -0
- package/dist/xyz.d.ts +16 -0
- package/dist/xyz.js +110 -0
- package/dist/zip.d.ts +38 -0
- package/dist/zip.js +150 -0
- package/package.json +289 -4
package/dist/threemf.js
ADDED
|
@@ -0,0 +1,609 @@
|
|
|
1
|
+
import { LoaderError } from './types.js';
|
|
2
|
+
import { floats, limits, throwIfAborted, toBytes, uints } from './util.js';
|
|
3
|
+
import { readZipDirectory, readZipEntry } from './zip.js';
|
|
4
|
+
import { decodeXML, readXML } from './xml.js';
|
|
5
|
+
import { hexColor } from '@margoengine/math/color';
|
|
6
|
+
const F = '3MF';
|
|
7
|
+
const NONE = 0xffffffff;
|
|
8
|
+
// The per-file part cap shared by every loader that makes one mesh per part (docs/limits.md).
|
|
9
|
+
const MAX_INSTANCES = 100_000;
|
|
10
|
+
/** Meters per 3MF unit. */
|
|
11
|
+
export const THREEMF_UNITS = {
|
|
12
|
+
micron: 1e-6,
|
|
13
|
+
millimeter: 1e-3,
|
|
14
|
+
centimeter: 1e-2,
|
|
15
|
+
inch: 0.0254,
|
|
16
|
+
foot: 0.3048,
|
|
17
|
+
meter: 1,
|
|
18
|
+
};
|
|
19
|
+
/** The namespace of the production extension's `path` and `UUID` attributes. */
|
|
20
|
+
const PRODUCTION = 'http://schemas.microsoft.com/3dmanufacturing/production/2015/06';
|
|
21
|
+
const IDENTITY = new Float64Array([1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0]);
|
|
22
|
+
/** Property resources of the core and materials formats, which share one set of ids. */
|
|
23
|
+
const PROPERTY_RESOURCES = new Set([
|
|
24
|
+
'basematerials',
|
|
25
|
+
'colorgroup',
|
|
26
|
+
'texture2d',
|
|
27
|
+
'texture2dgroup',
|
|
28
|
+
'compositematerials',
|
|
29
|
+
'multiproperties',
|
|
30
|
+
'pbmetallicdisplayproperties',
|
|
31
|
+
'pbspeculardisplayproperties',
|
|
32
|
+
'translucentdisplayproperties',
|
|
33
|
+
'pbmetallictexturedisplayproperties',
|
|
34
|
+
'pbspeculartexturedisplayproperties',
|
|
35
|
+
]);
|
|
36
|
+
/** Resource elements whose content only an extension reads (volumetric, implicit and displacement extensions). */
|
|
37
|
+
const FIELDS = new Set([
|
|
38
|
+
'image3d',
|
|
39
|
+
'functionfromimage3d',
|
|
40
|
+
'implicitfunction',
|
|
41
|
+
'volumedata',
|
|
42
|
+
'displacement2d',
|
|
43
|
+
'normvectorgroup',
|
|
44
|
+
'disp2dgroup',
|
|
45
|
+
]);
|
|
46
|
+
function parseColor(value) {
|
|
47
|
+
const rgba = hexColor(value?.trim() ?? '');
|
|
48
|
+
if (!rgba)
|
|
49
|
+
throw new LoaderError(F, 'syntax', `invalid color "${String(value).slice(0, 16)}"`);
|
|
50
|
+
return rgba;
|
|
51
|
+
}
|
|
52
|
+
function parseTransform(value) {
|
|
53
|
+
if (value === undefined)
|
|
54
|
+
return IDENTITY;
|
|
55
|
+
const parts = value.trim().split(/\s+/).map(Number);
|
|
56
|
+
if (parts.length !== 12 || !parts.every(Number.isFinite))
|
|
57
|
+
throw new LoaderError(F, 'syntax', `invalid transform "${value.slice(0, 64)}"`);
|
|
58
|
+
return new Float64Array(parts);
|
|
59
|
+
}
|
|
60
|
+
/** Row-vector composition: apply `a`, then `b`. */
|
|
61
|
+
function compose(a, b) {
|
|
62
|
+
const out = new Float64Array(12);
|
|
63
|
+
for (let r = 0; r < 4; r++)
|
|
64
|
+
for (let c = 0; c < 3; c++) {
|
|
65
|
+
let sum = r === 3 ? b[9 + c] : 0;
|
|
66
|
+
for (let k = 0; k < 3; k++)
|
|
67
|
+
sum += a[r * 3 + k] * b[k * 3 + c];
|
|
68
|
+
out[r * 3 + c] = sum;
|
|
69
|
+
}
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
const determinant = (m) => m[0] * (m[4] * m[8] - m[5] * m[7]) - m[1] * (m[3] * m[8] - m[5] * m[6]) + m[2] * (m[3] * m[7] - m[4] * m[6]);
|
|
73
|
+
function index(value, what) {
|
|
74
|
+
if (value === undefined)
|
|
75
|
+
return NONE;
|
|
76
|
+
const number = Number(value);
|
|
77
|
+
if (!Number.isSafeInteger(number) || number < 0 || number >= NONE)
|
|
78
|
+
throw new LoaderError(F, 'range', `invalid ${what} "${value.slice(0, 32)}"`);
|
|
79
|
+
return number;
|
|
80
|
+
}
|
|
81
|
+
function parseModel(xml, limit, extension, external) {
|
|
82
|
+
const model = {
|
|
83
|
+
unit: 'millimeter',
|
|
84
|
+
objects: new Map(),
|
|
85
|
+
properties: new Map(),
|
|
86
|
+
propertyIds: [],
|
|
87
|
+
items: [],
|
|
88
|
+
// Null prototype: <metadata name="..."> comes from the file (`__proto__`, `constructor`, `hasOwnProperty`).
|
|
89
|
+
metadata: Object.create(null),
|
|
90
|
+
};
|
|
91
|
+
const propertyIds = model.propertyIds, resourceIds = new Set(),
|
|
92
|
+
/** The namespace each prefix was last declared for. */
|
|
93
|
+
prefixes = new Map();
|
|
94
|
+
/** A production extension attribute, under whatever prefix the part declares for it (`p` when it declares none). */
|
|
95
|
+
const production = (a, local) => {
|
|
96
|
+
for (const key in a) {
|
|
97
|
+
const at = key.indexOf(':');
|
|
98
|
+
if (at < 0 || key.slice(at + 1) !== local)
|
|
99
|
+
continue;
|
|
100
|
+
const prefix = key.slice(0, at);
|
|
101
|
+
if ((prefixes.get(prefix) ?? (prefix === 'p' ? PRODUCTION : '')) === PRODUCTION)
|
|
102
|
+
return a[key];
|
|
103
|
+
}
|
|
104
|
+
return undefined;
|
|
105
|
+
};
|
|
106
|
+
let object, colors, metadataName, metadataText = '',
|
|
107
|
+
// 3MF Core 1.4 section 4.1: <metadata> inside an object's or item's <metadatagroup> describes that element, not
|
|
108
|
+
// the model, so only top-level entries reach the model metadata.
|
|
109
|
+
metadataGroups = 0,
|
|
110
|
+
/** Inside a slice stack, whose vertices are the layers' 2D points and no mesh's. */
|
|
111
|
+
sliceStacks = 0,
|
|
112
|
+
/** The depth inside a resource of `FIELDS`. */
|
|
113
|
+
field = 0;
|
|
114
|
+
let vertexCount = 0, triangleCount = 0, sawModel = false;
|
|
115
|
+
readXML(xml, {
|
|
116
|
+
open(name, a) {
|
|
117
|
+
for (const key in a)
|
|
118
|
+
if (key.startsWith('xmlns:'))
|
|
119
|
+
prefixes.set(key.slice(6), a[key]);
|
|
120
|
+
if (field || FIELDS.has(name)) {
|
|
121
|
+
field++;
|
|
122
|
+
extension?.field?.open(model, name, a);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (extension?.element?.(model, name, a))
|
|
126
|
+
return;
|
|
127
|
+
if (name === 'slicestack')
|
|
128
|
+
sliceStacks++;
|
|
129
|
+
if (sliceStacks)
|
|
130
|
+
return;
|
|
131
|
+
if (PROPERTY_RESOURCES.has(name) && a.id) {
|
|
132
|
+
// A second resource of one id would silently replace the first.
|
|
133
|
+
if (resourceIds.has(a.id))
|
|
134
|
+
throw new LoaderError(F, 'syntax', `duplicate property resource id ${a.id.slice(0, 32)}`);
|
|
135
|
+
resourceIds.add(a.id);
|
|
136
|
+
}
|
|
137
|
+
switch (name) {
|
|
138
|
+
case 'model':
|
|
139
|
+
if (sawModel)
|
|
140
|
+
throw new LoaderError(F, 'syntax', 'nested <model>');
|
|
141
|
+
sawModel = true;
|
|
142
|
+
model.unit = a.unit ?? 'millimeter';
|
|
143
|
+
if (!Object.hasOwn(THREEMF_UNITS, model.unit))
|
|
144
|
+
throw new LoaderError(F, 'unsupported', `unknown unit "${model.unit.slice(0, 32)}"`);
|
|
145
|
+
break;
|
|
146
|
+
case 'metadatagroup':
|
|
147
|
+
metadataGroups++;
|
|
148
|
+
break;
|
|
149
|
+
case 'metadata':
|
|
150
|
+
metadataName = metadataGroups ? undefined : a.name;
|
|
151
|
+
metadataText = '';
|
|
152
|
+
break;
|
|
153
|
+
case 'basematerials':
|
|
154
|
+
case 'colorgroup':
|
|
155
|
+
if (!a.id)
|
|
156
|
+
throw new LoaderError(F, 'syntax', `<${name}> without id`);
|
|
157
|
+
colors = [];
|
|
158
|
+
model.properties.set(a.id, colors);
|
|
159
|
+
propertyIds.push(a.id);
|
|
160
|
+
break;
|
|
161
|
+
case 'base':
|
|
162
|
+
if (colors)
|
|
163
|
+
colors.push(a.displaycolor ? parseColor(a.displaycolor) : [1, 1, 1, 1]);
|
|
164
|
+
break;
|
|
165
|
+
case 'color':
|
|
166
|
+
if (colors)
|
|
167
|
+
colors.push(parseColor(a.color));
|
|
168
|
+
break;
|
|
169
|
+
case 'object': {
|
|
170
|
+
if (!a.id)
|
|
171
|
+
throw new LoaderError(F, 'syntax', '<object> without id');
|
|
172
|
+
if (model.objects.has(a.id))
|
|
173
|
+
throw new LoaderError(F, 'syntax', `duplicate object id ${a.id}`);
|
|
174
|
+
object = {
|
|
175
|
+
id: a.id,
|
|
176
|
+
name: a.name,
|
|
177
|
+
type: a.type ?? 'model',
|
|
178
|
+
partNumber: a.partnumber,
|
|
179
|
+
uuid: production(a, 'UUID'),
|
|
180
|
+
pid: a.pid,
|
|
181
|
+
pindex: a.pindex === undefined ? undefined : index(a.pindex, 'pindex'),
|
|
182
|
+
components: [],
|
|
183
|
+
alternatives: [],
|
|
184
|
+
};
|
|
185
|
+
model.objects.set(a.id, object);
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
case 'mesh':
|
|
189
|
+
case 'displacementmesh':
|
|
190
|
+
if (!object)
|
|
191
|
+
throw new LoaderError(F, 'syntax', `<${name}> outside <object>`);
|
|
192
|
+
object.mesh = { vertices: floats(), triangles: uints(), pid: uints(), p: uints() };
|
|
193
|
+
if (name === 'displacementmesh')
|
|
194
|
+
object.displaced = true;
|
|
195
|
+
break;
|
|
196
|
+
case 'booleanshape':
|
|
197
|
+
case 'boolean': {
|
|
198
|
+
const shape = name === 'boolean' ? object?.boolean : object;
|
|
199
|
+
if (!shape)
|
|
200
|
+
throw new LoaderError(F, 'syntax', `<${name}> outside ${name === 'boolean' ? '<booleanshape>' : '<object>'}`);
|
|
201
|
+
if (!a.objectid)
|
|
202
|
+
throw new LoaderError(F, 'syntax', `<${name}> without objectid`);
|
|
203
|
+
// The extension's own attributes are unqualified, `path` among them.
|
|
204
|
+
const reference = {
|
|
205
|
+
objectId: a.objectid,
|
|
206
|
+
path: a.path ?? production(a, 'path'),
|
|
207
|
+
transform: parseTransform(a.transform),
|
|
208
|
+
};
|
|
209
|
+
if (name === 'boolean')
|
|
210
|
+
object.boolean.operands.push(reference);
|
|
211
|
+
else
|
|
212
|
+
object.boolean = { operation: a.operation ?? 'union', base: reference, operands: [] };
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
case 'beamlattice':
|
|
216
|
+
if (object)
|
|
217
|
+
object.beamLattice = true;
|
|
218
|
+
extension?.open?.(model, name, a);
|
|
219
|
+
break;
|
|
220
|
+
case 'levelset':
|
|
221
|
+
if (!object)
|
|
222
|
+
throw new LoaderError(F, 'syntax', '<levelset> outside <object>');
|
|
223
|
+
object.levelSet = true;
|
|
224
|
+
extension?.open?.(model, name, a);
|
|
225
|
+
break;
|
|
226
|
+
case 'vertex': {
|
|
227
|
+
const mesh = object?.mesh;
|
|
228
|
+
if (!mesh)
|
|
229
|
+
throw new LoaderError(F, 'syntax', '<vertex> outside <mesh>');
|
|
230
|
+
if (++vertexCount > limit.maxVertices)
|
|
231
|
+
throw new LoaderError(F, 'limit', `vertex count exceeds the limit of ${limit.maxVertices}`);
|
|
232
|
+
const x = Number(a.x), y = Number(a.y), z = Number(a.z);
|
|
233
|
+
if (!Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(z))
|
|
234
|
+
throw new LoaderError(F, 'syntax', `vertex ${mesh.vertices.length / 3} of object ${object.id} has an invalid coordinate`);
|
|
235
|
+
mesh.vertices.push3(x, y, z);
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
case 'triangle': {
|
|
239
|
+
const mesh = object?.mesh;
|
|
240
|
+
if (!mesh)
|
|
241
|
+
throw new LoaderError(F, 'syntax', '<triangle> outside <mesh>');
|
|
242
|
+
if (++triangleCount > limit.maxTriangles)
|
|
243
|
+
throw new LoaderError(F, 'limit', `triangle count exceeds the limit of ${limit.maxTriangles}`);
|
|
244
|
+
const v1 = index(a.v1, 'v1'), v2 = index(a.v2, 'v2'), v3 = index(a.v3, 'v3');
|
|
245
|
+
if (v1 === NONE || v2 === NONE || v3 === NONE)
|
|
246
|
+
throw new LoaderError(F, 'syntax', `triangle ${mesh.triangles.length / 3} of object ${object.id} is missing v1, v2 or v3`);
|
|
247
|
+
mesh.triangles.push3(v1, v2, v3);
|
|
248
|
+
let pid = NONE;
|
|
249
|
+
if (a.pid !== undefined) {
|
|
250
|
+
pid = propertyIds.indexOf(a.pid);
|
|
251
|
+
if (pid < 0)
|
|
252
|
+
pid = NONE - 1;
|
|
253
|
+
}
|
|
254
|
+
mesh.pid.push(pid);
|
|
255
|
+
mesh.p.push3(index(a.p1, 'p1'), index(a.p2, 'p2'), index(a.p3, 'p3'));
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
case 'component': {
|
|
259
|
+
if (!object)
|
|
260
|
+
throw new LoaderError(F, 'syntax', '<component> outside <object>');
|
|
261
|
+
if (!a.objectid)
|
|
262
|
+
throw new LoaderError(F, 'syntax', '<component> without objectid');
|
|
263
|
+
object.components.push({
|
|
264
|
+
objectId: a.objectid,
|
|
265
|
+
path: production(a, 'path'),
|
|
266
|
+
uuid: production(a, 'UUID'),
|
|
267
|
+
transform: parseTransform(a.transform),
|
|
268
|
+
});
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
case 'alternative': {
|
|
272
|
+
if (!object)
|
|
273
|
+
throw new LoaderError(F, 'syntax', '<alternative> outside <object>');
|
|
274
|
+
if (!a.objectid)
|
|
275
|
+
throw new LoaderError(F, 'syntax', '<alternative> without objectid');
|
|
276
|
+
// The alternatives namespace's own attributes are unqualified, as its schema and sample write them.
|
|
277
|
+
object.alternatives.push({
|
|
278
|
+
objectId: a.objectid,
|
|
279
|
+
path: a.path ?? production(a, 'path'),
|
|
280
|
+
uuid: a.UUID ?? production(a, 'UUID'),
|
|
281
|
+
resolution: a.modelresolution ?? 'fullres',
|
|
282
|
+
});
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
285
|
+
case 'build':
|
|
286
|
+
model.buildUUID = production(a, 'UUID');
|
|
287
|
+
break;
|
|
288
|
+
case 'item':
|
|
289
|
+
if (!a.objectid)
|
|
290
|
+
throw new LoaderError(F, 'syntax', '<item> without objectid');
|
|
291
|
+
model.items.push({
|
|
292
|
+
objectId: a.objectid,
|
|
293
|
+
path: production(a, 'path'),
|
|
294
|
+
uuid: production(a, 'UUID'),
|
|
295
|
+
transform: parseTransform(a.transform),
|
|
296
|
+
partNumber: a.partnumber,
|
|
297
|
+
});
|
|
298
|
+
break;
|
|
299
|
+
default:
|
|
300
|
+
extension?.open?.(model, name, a);
|
|
301
|
+
}
|
|
302
|
+
},
|
|
303
|
+
close(name) {
|
|
304
|
+
if (field) {
|
|
305
|
+
field--;
|
|
306
|
+
extension?.field?.close(model, name);
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
extension?.close?.(model, name);
|
|
310
|
+
if (name === 'slicestack')
|
|
311
|
+
sliceStacks--;
|
|
312
|
+
if (name === 'object')
|
|
313
|
+
object = undefined;
|
|
314
|
+
else if (name === 'basematerials' || name === 'colorgroup')
|
|
315
|
+
colors = undefined;
|
|
316
|
+
else if (name === 'metadatagroup')
|
|
317
|
+
metadataGroups--;
|
|
318
|
+
else if (name === 'metadata' && metadataName) {
|
|
319
|
+
model.metadata[metadataName] = metadataText.trim();
|
|
320
|
+
metadataName = undefined;
|
|
321
|
+
}
|
|
322
|
+
},
|
|
323
|
+
text(text) {
|
|
324
|
+
if (metadataName)
|
|
325
|
+
metadataText += text;
|
|
326
|
+
},
|
|
327
|
+
}, F, { external });
|
|
328
|
+
if (!sawModel)
|
|
329
|
+
throw new LoaderError(F, 'syntax', 'no <model> element');
|
|
330
|
+
return model;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* The property a triangle corner names: the triangle's own `pid`, else its object's, and the corner's index, else the
|
|
334
|
+
* triangle's first, else the object's `pindex`.
|
|
335
|
+
*/
|
|
336
|
+
export function cornerProperty(model, object, mesh, triangle, corner) {
|
|
337
|
+
const pid = mesh.pid.array[triangle], id = pid === NONE ? object.pid : pid === NONE - 1 ? undefined : model.propertyIds[pid];
|
|
338
|
+
if (id === undefined)
|
|
339
|
+
return undefined;
|
|
340
|
+
const first = mesh.p.array[triangle * 3], fallback = first !== NONE ? first : (object.pindex ?? NONE), own = mesh.p.array[triangle * 3 + corner], index = own === NONE ? fallback : own;
|
|
341
|
+
return index === NONE ? undefined : { id, index };
|
|
342
|
+
}
|
|
343
|
+
const normalizePath = (path) => path.replace(/\\/g, '/').replace(/^\/+/, '').toLowerCase();
|
|
344
|
+
/**
|
|
345
|
+
* Parses a 3MF package (core specification, plus basematerials and the materials extension colorgroup, and the
|
|
346
|
+
* production extension: items and components in other model parts, UUIDs and alternative representations). Build item
|
|
347
|
+
* and component transforms are baked into the positions, which are converted to meters by default. The result has one
|
|
348
|
+
* group per build item object and color; `colors` is present only when some triangle has per-corner colors. A
|
|
349
|
+
* displacement mesh gives its base triangles, in groups marked `displaced`; a volumetric level set or a boolean shape
|
|
350
|
+
* throws `unsupported`.
|
|
351
|
+
*/
|
|
352
|
+
export async function parse3MF(input, options = {}) {
|
|
353
|
+
return read3MF(input, options);
|
|
354
|
+
}
|
|
355
|
+
/** The reader behind {@link parse3MF}, with the hooks `@margoengine/loaders/threemf-materials` uses. */
|
|
356
|
+
export async function read3MF(input, options, extension) {
|
|
357
|
+
const bytes = toBytes(input, F), limit = limits(options, F), maxDepth = options.maxDepth ?? 32, maxModelParts = options.maxModelParts ?? 65_535;
|
|
358
|
+
if (!Number.isSafeInteger(maxDepth) || maxDepth < 0)
|
|
359
|
+
throw new LoaderError(F, 'range', 'maxDepth must be a non-negative integer');
|
|
360
|
+
if (!Number.isSafeInteger(maxModelParts) || maxModelParts < 1)
|
|
361
|
+
throw new LoaderError(F, 'range', 'maxModelParts must be a positive integer');
|
|
362
|
+
if (options.units !== undefined && options.units !== 'meters' && options.units !== 'file')
|
|
363
|
+
throw new LoaderError(F, 'range', `units must be "meters" or "file"`);
|
|
364
|
+
if (!['fullres', 'lowres', 'obfuscated', undefined].includes(options.modelResolution))
|
|
365
|
+
throw new LoaderError(F, 'range', 'modelResolution must be "fullres", "lowres" or "obfuscated"');
|
|
366
|
+
if (bytes.length < 4 || bytes[0] !== 0x50 || bytes[1] !== 0x4b)
|
|
367
|
+
throw new LoaderError(F, 'header', 'not a ZIP package (missing PK signature)');
|
|
368
|
+
// A part is an entry, so the entry limit follows the part limit unless the caller sets it.
|
|
369
|
+
const entries = readZipDirectory(bytes, { ...options, maxEntries: options.maxEntries ?? 65_535 });
|
|
370
|
+
const byPath = new Map();
|
|
371
|
+
for (const entry of entries)
|
|
372
|
+
byPath.set(normalizePath(entry.name), entry);
|
|
373
|
+
const read = async (path) => {
|
|
374
|
+
throwIfAborted(options.signal, F);
|
|
375
|
+
const entry = byPath.get(normalizePath(path));
|
|
376
|
+
if (!entry)
|
|
377
|
+
throw new LoaderError(F, 'syntax', `package part "${path.slice(0, 128)}" is missing`);
|
|
378
|
+
const data = await readZipEntry(bytes, entry, options);
|
|
379
|
+
// The secure content extension's cipher files start `%3McF`.
|
|
380
|
+
if (data[0] === 0x25 && data[1] === 0x33 && data[2] === 0x4d && data[3] === 0x63 && data[4] === 0x46)
|
|
381
|
+
throw new LoaderError(F, 'unsupported', `package part "${path.slice(0, 128)}" is encrypted; parse3MFKeystore from @margoengine/loaders/threemf-secure reads its key data, and decrypting it is the caller's`);
|
|
382
|
+
return data;
|
|
383
|
+
};
|
|
384
|
+
let rootPath = '3D/3dmodel.model';
|
|
385
|
+
if (byPath.has('_rels/.rels')) {
|
|
386
|
+
readXML(decodeXML(await read('_rels/.rels')), {
|
|
387
|
+
open(name, a) {
|
|
388
|
+
if (name === 'Relationship' && a.Type?.endsWith('/3dmodel') && a.Target)
|
|
389
|
+
rootPath = a.Target;
|
|
390
|
+
},
|
|
391
|
+
close() { },
|
|
392
|
+
}, F);
|
|
393
|
+
}
|
|
394
|
+
const models = new Map(),
|
|
395
|
+
/** The package path of each model part. */
|
|
396
|
+
partPaths = new Map();
|
|
397
|
+
const load = async (path) => {
|
|
398
|
+
const key = normalizePath(path);
|
|
399
|
+
let model = models.get(key);
|
|
400
|
+
if (!model) {
|
|
401
|
+
if (models.size >= maxModelParts)
|
|
402
|
+
throw new LoaderError(F, 'limit', `package names more than ${maxModelParts} model parts (maxModelParts)`);
|
|
403
|
+
model = parseModel(decodeXML(await read(path)), limit, extension, options.external);
|
|
404
|
+
models.set(key, model);
|
|
405
|
+
partPaths.set(model, '/' + byPath.get(key).name.replace(/^\/+/, ''));
|
|
406
|
+
}
|
|
407
|
+
return model;
|
|
408
|
+
};
|
|
409
|
+
const root = await load(rootPath);
|
|
410
|
+
/** The paths a model part's objects name, in components and alternatives. */
|
|
411
|
+
const named = (model) => [...model.objects.values()].flatMap((o) => [...o.components, ...o.alternatives, ...(o.boolean ? [o.boolean.base, ...o.boolean.operands] : [])].flatMap(({ path }) => (path ? [path] : [])));
|
|
412
|
+
// Load every model part the root names before the synchronous build walk. A part's own build is ignored.
|
|
413
|
+
for (const path of [...root.items.flatMap(({ path }) => (path ? [path] : [])), ...named(root)])
|
|
414
|
+
await load(path);
|
|
415
|
+
for (const model of models.values())
|
|
416
|
+
if (model !== root && named(model).length)
|
|
417
|
+
throw new LoaderError(F, 'syntax', `${partPaths.get(model).slice(0, 128)}: only the root model part may name another part (production extension 3.2)`);
|
|
418
|
+
const partPath = (model) => (model === root ? undefined : partPaths.get(model));
|
|
419
|
+
await extension?.ready?.(read, { models, loadModelPart: load, partPath });
|
|
420
|
+
throwIfAborted(options.signal, F);
|
|
421
|
+
const scale = options.units === 'file' ? 1 : THREEMF_UNITS[root.unit];
|
|
422
|
+
const resolveColors = (model, object, mesh, t) => [0, 1, 2].map((k) => {
|
|
423
|
+
const property = cornerProperty(model, object, mesh, t, k), group = property && model.properties.get(property.id);
|
|
424
|
+
if (!group)
|
|
425
|
+
return undefined;
|
|
426
|
+
if (property.index >= group.length)
|
|
427
|
+
throw new LoaderError(F, 'range', `property index ${property.index} is outside property group ${property.id} (${group.length} entries)`);
|
|
428
|
+
return group[property.index];
|
|
429
|
+
});
|
|
430
|
+
const object = (model, id, path) => {
|
|
431
|
+
const owner = path ? models.get(normalizePath(path)) : model;
|
|
432
|
+
const found = owner.objects.get(id);
|
|
433
|
+
if (!found)
|
|
434
|
+
throw new LoaderError(F, 'syntax', `object ${id.slice(0, 32)} is not defined${path ? ` in ${path.slice(0, 128)}` : ''}`);
|
|
435
|
+
return [owner, found];
|
|
436
|
+
};
|
|
437
|
+
// Pass 1: does any referenced triangle need per-corner colors?
|
|
438
|
+
let vertexColors = false;
|
|
439
|
+
for (const model of models.values())
|
|
440
|
+
for (const o of model.objects.values()) {
|
|
441
|
+
if (!o.mesh || vertexColors)
|
|
442
|
+
continue;
|
|
443
|
+
for (let t = 0; t < o.mesh.triangles.length / 3 && !vertexColors; t++) {
|
|
444
|
+
const [a, b, c] = resolveColors(model, o, o.mesh, t);
|
|
445
|
+
vertexColors = a !== b || b !== c;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
const positions = floats(), indices = uints(), colors = vertexColors ? floats() : undefined, groups = [];
|
|
449
|
+
/** The part number and UUID of the build item being emitted. */
|
|
450
|
+
let itemPartNumber, itemUUID;
|
|
451
|
+
/** What the production extension says of the object being emitted. */
|
|
452
|
+
let identity = {};
|
|
453
|
+
const wanted = options.modelResolution ?? 'fullres';
|
|
454
|
+
/** A built object that is a beam lattice and no triangles, so an empty result can say why. */
|
|
455
|
+
let latticeOnly;
|
|
456
|
+
const emitMesh = (model, o, mesh, m) => {
|
|
457
|
+
const vertexCount = mesh.vertices.length / 3, triangleCount = mesh.triangles.length / 3;
|
|
458
|
+
if (!triangleCount && o.beamLattice)
|
|
459
|
+
latticeOnly ??= o.id;
|
|
460
|
+
if (!vertexCount && !triangleCount)
|
|
461
|
+
return; // an empty mesh adds nothing; skip the per-instance bookkeeping
|
|
462
|
+
const tri = mesh.triangles.array, source = mesh.vertices.array;
|
|
463
|
+
for (let i = 0; i < tri.length && i < triangleCount * 3; i++)
|
|
464
|
+
if (tri[i] >= vertexCount)
|
|
465
|
+
throw new LoaderError(F, 'range', `triangle index ${tri[i]} of object ${o.id} is outside 0..${vertexCount - 1}`);
|
|
466
|
+
const corner = (v) => {
|
|
467
|
+
const x = source[v * 3], y = source[v * 3 + 1], z = source[v * 3 + 2];
|
|
468
|
+
return [
|
|
469
|
+
(x * m[0] + y * m[3] + z * m[6] + m[9]) * scale,
|
|
470
|
+
(x * m[1] + y * m[4] + z * m[7] + m[10]) * scale,
|
|
471
|
+
(x * m[2] + y * m[5] + z * m[8] + m[11]) * scale,
|
|
472
|
+
];
|
|
473
|
+
};
|
|
474
|
+
const flip = determinant(m) < 0;
|
|
475
|
+
// Bucket triangles by their first-corner color so each group has one uniform color.
|
|
476
|
+
const buckets = new Map(), suffix = (t) => (extension ? `|${extension.bucket(model, o, mesh, t)}` : '');
|
|
477
|
+
const triangleColors = [];
|
|
478
|
+
let colored = false;
|
|
479
|
+
for (let t = 0; t < triangleCount; t++) {
|
|
480
|
+
const c = resolveColors(model, o, mesh, t);
|
|
481
|
+
triangleColors.push(c);
|
|
482
|
+
if (c[0])
|
|
483
|
+
colored = true;
|
|
484
|
+
const own = mesh.flat ? mesh.flat[t] : c[0], key = (own ? own.join(',') : '') + suffix(t);
|
|
485
|
+
let bucket = buckets.get(key);
|
|
486
|
+
if (!bucket)
|
|
487
|
+
buckets.set(key, (bucket = { color: own, triangles: [] }));
|
|
488
|
+
bucket.triangles.push(t);
|
|
489
|
+
}
|
|
490
|
+
const unweld = (!!colors && colored) || (!!extension && !extension.welded);
|
|
491
|
+
const base = positions.length / 3;
|
|
492
|
+
if (base + (unweld ? triangleCount * 3 : vertexCount) > limit.maxVertices)
|
|
493
|
+
throw new LoaderError(F, 'limit', `vertex count after instancing exceeds the limit of ${limit.maxVertices}`);
|
|
494
|
+
if (indices.length / 3 + triangleCount > limit.maxTriangles)
|
|
495
|
+
throw new LoaderError(F, 'limit', `triangle count after instancing exceeds the limit of ${limit.maxTriangles}`);
|
|
496
|
+
if (!unweld)
|
|
497
|
+
for (let v = 0; v < vertexCount; v++) {
|
|
498
|
+
positions.push3(...corner(v));
|
|
499
|
+
colors?.push3(1, 1, 1);
|
|
500
|
+
}
|
|
501
|
+
for (const bucket of buckets.values()) {
|
|
502
|
+
const start = indices.length;
|
|
503
|
+
for (const t of bucket.triangles) {
|
|
504
|
+
const order = flip ? [0, 2, 1] : [0, 1, 2];
|
|
505
|
+
for (const k of order) {
|
|
506
|
+
const v = tri[t * 3 + k];
|
|
507
|
+
if (unweld) {
|
|
508
|
+
indices.push(positions.length / 3);
|
|
509
|
+
positions.push3(...corner(v));
|
|
510
|
+
const color = triangleColors[t][k] ?? [1, 1, 1, 1];
|
|
511
|
+
colors?.push3(color[0], color[1], color[2]);
|
|
512
|
+
extension?.corner(model, o, mesh, t, k);
|
|
513
|
+
}
|
|
514
|
+
else
|
|
515
|
+
indices.push(base + v);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
const group = { start, count: indices.length - start, objectId: o.id };
|
|
519
|
+
if (o.name)
|
|
520
|
+
group.name = o.name;
|
|
521
|
+
if (o.type !== 'model')
|
|
522
|
+
group.objectType = o.type;
|
|
523
|
+
if (o.partNumber !== undefined)
|
|
524
|
+
group.partNumber = o.partNumber;
|
|
525
|
+
if (o.displaced)
|
|
526
|
+
group.displaced = true;
|
|
527
|
+
if (itemPartNumber !== undefined)
|
|
528
|
+
group.itemPartNumber = itemPartNumber;
|
|
529
|
+
if (itemUUID !== undefined)
|
|
530
|
+
group.itemUUID = itemUUID;
|
|
531
|
+
if (identity.uuid !== undefined)
|
|
532
|
+
group.uuid = identity.uuid;
|
|
533
|
+
if (identity.componentUUID !== undefined)
|
|
534
|
+
group.componentUUID = identity.componentUUID;
|
|
535
|
+
if (identity.alternativeUUID !== undefined)
|
|
536
|
+
group.alternativeUUID = identity.alternativeUUID;
|
|
537
|
+
if (model !== root)
|
|
538
|
+
group.modelPath = partPaths.get(model);
|
|
539
|
+
if (bucket.color)
|
|
540
|
+
group.color = [...bucket.color];
|
|
541
|
+
extension?.group(group, model, o, mesh, bucket.triangles[0]);
|
|
542
|
+
groups.push(group);
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
const visiting = new Set();
|
|
546
|
+
// Components may reuse an object many times per level: bound the expanded instances, not just the depth, or
|
|
547
|
+
// fan-out^depth references to an empty object never finish (meshes are also bounded by the vertex limits).
|
|
548
|
+
let instances = 0;
|
|
549
|
+
const emit = (model, id, path, m, depth, componentUUID) => {
|
|
550
|
+
if (depth > maxDepth)
|
|
551
|
+
throw new LoaderError(F, 'limit', `components nest deeper than ${maxDepth}`);
|
|
552
|
+
if (++instances > MAX_INSTANCES)
|
|
553
|
+
throw new LoaderError(F, 'limit', `the build expands to more than ${MAX_INSTANCES} object instances`);
|
|
554
|
+
const [owner, o] = object(model, id, path);
|
|
555
|
+
if (visiting.has(o))
|
|
556
|
+
throw new LoaderError(F, 'syntax', `object ${id.slice(0, 32)} references itself through its components`);
|
|
557
|
+
if (o.mesh && o.components.length)
|
|
558
|
+
throw new LoaderError(F, 'syntax', `object ${id.slice(0, 32)} has both a mesh and components`);
|
|
559
|
+
// Production alternatives: the first one of the resolution asked for stands in for the object, whose own content
|
|
560
|
+
// has the lowest priority.
|
|
561
|
+
const alternative = o.alternatives.find(({ resolution }) => resolution === wanted);
|
|
562
|
+
let [source, shown] = [owner, o];
|
|
563
|
+
if (alternative) {
|
|
564
|
+
[source, shown] = object(owner, alternative.objectId, alternative.path);
|
|
565
|
+
if (shown.alternatives.length)
|
|
566
|
+
throw new LoaderError(F, 'syntax', `alternative object ${shown.id.slice(0, 32)} has alternatives of its own`);
|
|
567
|
+
if (shown.mesh && shown.components.length)
|
|
568
|
+
throw new LoaderError(F, 'syntax', `object ${shown.id.slice(0, 32)} has both a mesh and components`);
|
|
569
|
+
}
|
|
570
|
+
// Core 3MF: an `other` mesh is never output, however it is reached; the type of an object of components is ignored.
|
|
571
|
+
if (shown.mesh && shown.type === 'other')
|
|
572
|
+
return;
|
|
573
|
+
if (shown.boolean && !extension?.booleans)
|
|
574
|
+
throw new LoaderError(F, 'unsupported', `object ${shown.id.slice(0, 32)} is a boolean shape; read the package with parse3MFBoolean from @margoengine/loaders/threemf-boolean`);
|
|
575
|
+
if (shown.levelSet && !extension?.levelSets)
|
|
576
|
+
throw new LoaderError(F, 'unsupported', `object ${shown.id.slice(0, 32)} is a volumetric level set; read the package with parse3MFVolumetric from @margoengine/loaders/threemf-volumetric`);
|
|
577
|
+
visiting.add(o);
|
|
578
|
+
extension?.instance?.(source, shown, m, scale, depth);
|
|
579
|
+
identity = { uuid: o.uuid, componentUUID, alternativeUUID: alternative?.uuid };
|
|
580
|
+
if (shown.mesh)
|
|
581
|
+
emitMesh(source, shown, shown.mesh, m);
|
|
582
|
+
for (const component of shown.components)
|
|
583
|
+
emit(source, component.objectId, component.path, compose(component.transform, m), depth + 1, component.uuid);
|
|
584
|
+
visiting.delete(o);
|
|
585
|
+
};
|
|
586
|
+
for (const item of root.items) {
|
|
587
|
+
itemPartNumber = item.partNumber;
|
|
588
|
+
itemUUID = item.uuid;
|
|
589
|
+
emit(root, item.objectId, item.path, item.transform, 0);
|
|
590
|
+
}
|
|
591
|
+
if (!indices.length && !extension?.hasGeometry?.()) {
|
|
592
|
+
if (latticeOnly !== undefined)
|
|
593
|
+
throw new LoaderError(F, 'syntax', `the build contains no triangles: object ${latticeOnly.slice(0, 32)} is a beam lattice with none of its own; parse3MFEvaluated from @margoengine/loaders/threemf-evaluate makes them`);
|
|
594
|
+
throw new LoaderError(F, 'syntax', 'the build contains no triangles');
|
|
595
|
+
}
|
|
596
|
+
const result = {
|
|
597
|
+
kind: 'mesh',
|
|
598
|
+
positions: positions.result(),
|
|
599
|
+
indices: indices.result(),
|
|
600
|
+
groups,
|
|
601
|
+
unit: root.unit,
|
|
602
|
+
metadata: root.metadata,
|
|
603
|
+
};
|
|
604
|
+
if (root.buildUUID !== undefined)
|
|
605
|
+
result.buildUUID = root.buildUUID;
|
|
606
|
+
if (colors)
|
|
607
|
+
result.colors = colors.result();
|
|
608
|
+
return result;
|
|
609
|
+
}
|
package/dist/tilt.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { type LoadedGeometry, type ParseLimits } from './types.js';
|
|
2
|
+
import { type BinaryInput } from './util.js';
|
|
3
|
+
import { type ZipOptions } from './zip.js';
|
|
4
|
+
/** Options of {@link parseTilt}. */
|
|
5
|
+
export interface TiltOptions extends ParseLimits, Pick<ZipOptions, 'maxEntries' | 'maxEntryBytes' | 'maxRatio'> {
|
|
6
|
+
/**
|
|
7
|
+
* Metres per sketch unit. Default 0.1: Tilt Brush sketches are drawn in decimetres. The brush size scales with it.
|
|
8
|
+
*/
|
|
9
|
+
scale?: number;
|
|
10
|
+
/**
|
|
11
|
+
* Convert from Tilt Brush's left-handed Y-up space to the right-handed Y-up space of glTF and the engine, by
|
|
12
|
+
* mirroring Z and reversing the triangle winding to match. Default true.
|
|
13
|
+
*/
|
|
14
|
+
rightHanded?: boolean;
|
|
15
|
+
/** Ribbon width multiplier, over `brushSize * pressure`. Default 1. */
|
|
16
|
+
widthScale?: number;
|
|
17
|
+
/** Skip strokes with fewer control points than this. Default 2. */
|
|
18
|
+
minControlPoints?: number;
|
|
19
|
+
signal?: AbortSignal;
|
|
20
|
+
}
|
|
21
|
+
/** One stroke of a sketch, as stored. */
|
|
22
|
+
export interface TiltStroke {
|
|
23
|
+
/** Index into {@link LoadedTilt.brushes}. */
|
|
24
|
+
brushIndex: number;
|
|
25
|
+
/** The brush GUID from `metadata.json`'s `BrushIndex`, when the file has one. */
|
|
26
|
+
brush?: string;
|
|
27
|
+
/** RGBA in [0,1]. */
|
|
28
|
+
color: [number, number, number, number];
|
|
29
|
+
/** Brush size in sketch units. */
|
|
30
|
+
size: number;
|
|
31
|
+
/** Control points: position xyz, orientation quaternion xyzw (the controller's), pressure (1 when absent) and timestamp in ms (0 when absent). */
|
|
32
|
+
points: {
|
|
33
|
+
position: [number, number, number];
|
|
34
|
+
orientation: [number, number, number, number];
|
|
35
|
+
pressure: number;
|
|
36
|
+
timestamp: number;
|
|
37
|
+
}[];
|
|
38
|
+
}
|
|
39
|
+
/** The result of {@link parseTilt}. */
|
|
40
|
+
export interface LoadedTilt {
|
|
41
|
+
/** All strokes as flat ribbons: one group per drawn stroke, with the stroke's colour and `properties.brushIndex`. Draw it double-sided. */
|
|
42
|
+
geometry: LoadedGeometry;
|
|
43
|
+
/** Every stroke as stored, in sketch space, before scale and handedness. */
|
|
44
|
+
strokes: TiltStroke[];
|
|
45
|
+
/** The `BrushIndex` GUIDs of `metadata.json`, empty when absent. */
|
|
46
|
+
brushes: string[];
|
|
47
|
+
/** The parsed `metadata.json`, when the archive has one. */
|
|
48
|
+
metadata?: Record<string, unknown>;
|
|
49
|
+
/** The `data.sketch` version. */
|
|
50
|
+
version: number;
|
|
51
|
+
}
|
|
52
|
+
/** Reads `data.sketch` itself (the stroke stream), for callers that already unzipped the `.tilt`. */
|
|
53
|
+
export declare function parseTiltSketch(input: BinaryInput, options?: TiltOptions): {
|
|
54
|
+
strokes: TiltStroke[];
|
|
55
|
+
version: number;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Parses a `.tilt` file (a zip with a 16-byte header in front: `data.sketch` and `metadata.json`) into ribbons.
|
|
59
|
+
* A ribbon runs along a stroke's control points; its width is `size * pressure * widthScale` along the controller's
|
|
60
|
+
* local X axis, its normal is across it, and every vertex carries the stroke colour.
|
|
61
|
+
*/
|
|
62
|
+
export declare function parseTilt(input: BinaryInput, options?: TiltOptions): Promise<LoadedTilt>;
|