@margoengine/loaders 0.0.0-stage → 0.1.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 +1198 -3
- 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/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-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 +161 -0
- package/dist/dae.js +633 -0
- package/dist/detect.d.ts +20 -0
- package/dist/detect.js +72 -0
- package/dist/dxf.d.ts +81 -0
- package/dist/dxf.js +774 -0
- package/dist/engine.d.ts +36 -0
- package/dist/engine.js +117 -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 +55 -0
- package/dist/gcode.js +278 -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 +120 -0
- package/dist/mtl.js +469 -0
- package/dist/normals.d.ts +7 -0
- package/dist/normals.js +31 -0
- package/dist/nrrd.d.ts +63 -0
- package/dist/nrrd.js +743 -0
- package/dist/nurbs.d.ts +118 -0
- package/dist/nurbs.js +265 -0
- package/dist/obj.d.ts +143 -0
- package/dist/obj.js +456 -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 +27 -0
- package/dist/render.js +153 -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 +42 -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-lattice.d.ts +139 -0
- package/dist/threemf-lattice.js +514 -0
- package/dist/threemf-materials.d.ts +75 -0
- package/dist/threemf-materials.js +314 -0
- package/dist/threemf-mesh.d.ts +60 -0
- package/dist/threemf-mesh.js +201 -0
- package/dist/threemf.d.ts +101 -0
- package/dist/threemf.js +415 -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 +90 -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 +246 -6
package/dist/acclaim.js
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import { multiplyQuaternions } from '@margoengine/math/quat';
|
|
2
|
+
import { LoaderError } from './types.js';
|
|
3
|
+
import { toText } from './util.js';
|
|
4
|
+
const A = 'ASF', M = 'AMC';
|
|
5
|
+
const qmul = (a, b) => multiplyQuaternions(a, b);
|
|
6
|
+
const qaxis = (axis, radians) => {
|
|
7
|
+
const q = [0, 0, 0, Math.cos(radians / 2)];
|
|
8
|
+
q[axis] = Math.sin(radians / 2);
|
|
9
|
+
return q;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Acclaim Euler angles: the listed axes turn a vector one after another, first listed first, each about the fixed
|
|
13
|
+
* frame's axis (`axis 10 20 30 XYZ` is Rz Ry Rx with column vectors: the X turn is applied first).
|
|
14
|
+
*/
|
|
15
|
+
function acclaimEuler(axes, radians) {
|
|
16
|
+
let q = [0, 0, 0, 1];
|
|
17
|
+
for (let i = 0; i < axes.length; i++)
|
|
18
|
+
q = qmul(qaxis(axes[i], radians[i]), q);
|
|
19
|
+
return q;
|
|
20
|
+
}
|
|
21
|
+
const AXIS = { x: 0, y: 1, z: 2 };
|
|
22
|
+
function orderOf(word, what) {
|
|
23
|
+
const w = (word ?? '').toLowerCase();
|
|
24
|
+
if (!/^[xyz]{3}$/.test(w) || new Set(w).size !== 3)
|
|
25
|
+
throw new LoaderError(A, 'syntax', `${what} order "${w.slice(0, 8)}" is not a permutation of XYZ`);
|
|
26
|
+
return [...w].map((c) => AXIS[c]);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Parses an Acclaim ASF skeleton (the CMU motion capture database's `.asf`). Bones become joints at each bone's start,
|
|
30
|
+
* resting unrotated with offsets in the file's world frame, so {@link parseAMC} gives tracks that the animation
|
|
31
|
+
* package plays and the rigging package's `cmu` retarget preset maps. Joint limits, ids and masses are read past.
|
|
32
|
+
*
|
|
33
|
+
* @param input The file's bytes or text.
|
|
34
|
+
* @param options Scale and the joint limit.
|
|
35
|
+
* @returns The skeleton to pass to {@link parseAMC}.
|
|
36
|
+
* @throws LoaderError with format `ASF` and code `header`, `syntax`, `range`, `truncated` or `limit`.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* import { parseASF } from '@margoengine/loaders/mocap';
|
|
41
|
+
*
|
|
42
|
+
* const skeleton = parseASF(':root\n axis XYZ\n order TX TY TZ RX RY RZ\n:bonedata\n begin\n name spine\n direction 0 1 0\n length 2\n axis 0 0 0 XYZ\n dof rx\n end\n:hierarchy\n begin\n root spine\n end\n');
|
|
43
|
+
* console.log(skeleton.bones.map((b) => b.name), skeleton.bones[1].endSite); // [ 'root', 'spine' ] [ 0, 2, 0 ]
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export function parseASF(input, options = {}) {
|
|
47
|
+
const text = toText(input, A), { scale = 1, maxJoints = 4096 } = options;
|
|
48
|
+
if (!Number.isFinite(scale) || scale <= 0)
|
|
49
|
+
throw new LoaderError(A, 'range', 'scale must be positive and finite');
|
|
50
|
+
if (!Number.isSafeInteger(maxJoints) || maxJoints < 1)
|
|
51
|
+
throw new LoaderError(A, 'range', 'maxJoints must be a positive integer');
|
|
52
|
+
const lines = text
|
|
53
|
+
.split(/\r?\n/)
|
|
54
|
+
.map((l) => l.replace(/#.*$/, '').trim())
|
|
55
|
+
.filter(Boolean);
|
|
56
|
+
if (!lines.some((l) => /^:(root|bonedata)\b/i.test(l)))
|
|
57
|
+
throw new LoaderError(A, 'header', 'missing the :root and :bonedata sections');
|
|
58
|
+
const num = (token, what) => {
|
|
59
|
+
if (token === undefined)
|
|
60
|
+
throw new LoaderError(A, 'truncated', `${what} is missing`);
|
|
61
|
+
const v = Number(token);
|
|
62
|
+
if (!Number.isFinite(v))
|
|
63
|
+
throw new LoaderError(A, 'syntax', `invalid number "${token.slice(0, 32)}" for ${what}`);
|
|
64
|
+
return v;
|
|
65
|
+
};
|
|
66
|
+
let name = '', lengthUnit = 1, angle = 'deg', section = '', current = null;
|
|
67
|
+
const root = { order: [], axis: [0, 1, 2], position: [0, 0, 0], orientation: [0, 0, 0] }, bones = [], hierarchy = [];
|
|
68
|
+
for (const line of lines) {
|
|
69
|
+
const words = line.split(/\s+/);
|
|
70
|
+
if (words[0].startsWith(':')) {
|
|
71
|
+
section = words[0].toLowerCase();
|
|
72
|
+
if (section === ':name')
|
|
73
|
+
name = words.slice(1).join(' ');
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const key = words[0].toLowerCase();
|
|
77
|
+
if (section === ':units') {
|
|
78
|
+
if (key === 'length') {
|
|
79
|
+
lengthUnit = num(words[1], 'units length');
|
|
80
|
+
if (!(lengthUnit > 0))
|
|
81
|
+
throw new LoaderError(A, 'range', `units length ${lengthUnit} is not positive`);
|
|
82
|
+
}
|
|
83
|
+
else if (key === 'angle') {
|
|
84
|
+
const a = (words[1] ?? '').toLowerCase();
|
|
85
|
+
if (a !== 'deg' && a !== 'rad')
|
|
86
|
+
throw new LoaderError(A, 'syntax', `angle unit "${a.slice(0, 16)}" is not deg or rad`);
|
|
87
|
+
angle = a;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else if (section === ':root') {
|
|
91
|
+
if (key === 'order')
|
|
92
|
+
root.order = words.slice(1).map((w) => w.toLowerCase());
|
|
93
|
+
else if (key === 'axis')
|
|
94
|
+
root.axis = orderOf(words[1], 'root axis');
|
|
95
|
+
else if (key === 'position')
|
|
96
|
+
root.position = [1, 2, 3].map((i) => num(words[i], 'root position'));
|
|
97
|
+
else if (key === 'orientation')
|
|
98
|
+
root.orientation = [1, 2, 3].map((i) => num(words[i], 'root orientation'));
|
|
99
|
+
}
|
|
100
|
+
else if (section === ':bonedata') {
|
|
101
|
+
if (key === 'begin') {
|
|
102
|
+
if (current)
|
|
103
|
+
throw new LoaderError(A, 'syntax', 'a bone begins inside another bone');
|
|
104
|
+
if (bones.length + 1 >= maxJoints)
|
|
105
|
+
throw new LoaderError(A, 'limit', `bone count exceeds the limit of ${maxJoints}`);
|
|
106
|
+
current = { name: '', direction: [0, 0, 0], length: 0, angles: [0, 0, 0], order: [0, 1, 2], dof: [] };
|
|
107
|
+
}
|
|
108
|
+
else if (key === 'end') {
|
|
109
|
+
if (!current)
|
|
110
|
+
throw new LoaderError(A, 'syntax', 'end has no bone to close');
|
|
111
|
+
const n = current.name;
|
|
112
|
+
if (!n)
|
|
113
|
+
throw new LoaderError(A, 'syntax', 'a bone has no name');
|
|
114
|
+
if (n === 'root' || bones.some((b) => b.name === n))
|
|
115
|
+
throw new LoaderError(A, 'syntax', `bone "${n.slice(0, 32)}" is named twice`);
|
|
116
|
+
bones.push(current);
|
|
117
|
+
current = null;
|
|
118
|
+
}
|
|
119
|
+
else if (!current) {
|
|
120
|
+
throw new LoaderError(A, 'syntax', `"${key.slice(0, 32)}" is outside every bone`);
|
|
121
|
+
}
|
|
122
|
+
else if (key === 'name')
|
|
123
|
+
current.name = words[1] ?? '';
|
|
124
|
+
else if (key === 'direction') {
|
|
125
|
+
const d = [1, 2, 3].map((i) => num(words[i], `bone ${current.name} direction`)), l = Math.hypot(d[0], d[1], d[2]);
|
|
126
|
+
current.direction = (l > 0 ? d.map((v) => v / l) : [0, 0, 0]);
|
|
127
|
+
}
|
|
128
|
+
else if (key === 'length') {
|
|
129
|
+
current.length = num(words[1], `bone ${current.name} length`);
|
|
130
|
+
if (current.length < 0)
|
|
131
|
+
throw new LoaderError(A, 'range', `bone ${current.name} has a negative length`);
|
|
132
|
+
}
|
|
133
|
+
else if (key === 'axis') {
|
|
134
|
+
current.angles = [1, 2, 3].map((i) => num(words[i], `bone ${current.name} axis`));
|
|
135
|
+
current.order = orderOf(words[4], `bone ${current.name} axis`);
|
|
136
|
+
}
|
|
137
|
+
else if (key === 'dof') {
|
|
138
|
+
current.dof = words.slice(1).map((w) => w.toLowerCase());
|
|
139
|
+
for (const d of current.dof)
|
|
140
|
+
if (!/^(t[xyz]|r[xyz]|l)$/.test(d))
|
|
141
|
+
throw new LoaderError(A, 'syntax', `bone ${current.name} has an unknown dof "${d.slice(0, 16)}"`);
|
|
142
|
+
}
|
|
143
|
+
// id, bodymass, cofmass and limits (with their continuation lines) are read past.
|
|
144
|
+
}
|
|
145
|
+
else if (section === ':hierarchy') {
|
|
146
|
+
if (key !== 'begin' && key !== 'end')
|
|
147
|
+
hierarchy.push(words);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (current)
|
|
151
|
+
throw new LoaderError(A, 'truncated', `bone "${current.name.slice(0, 32)}" has no end`);
|
|
152
|
+
for (const r of root.order)
|
|
153
|
+
if (!/^(t[xyz]|r[xyz])$/.test(r))
|
|
154
|
+
throw new LoaderError(A, 'syntax', `root order has an unknown channel "${r.slice(0, 16)}"`);
|
|
155
|
+
const toRad = angle === 'deg' ? Math.PI / 180 : 1, all = [
|
|
156
|
+
{
|
|
157
|
+
name: 'root',
|
|
158
|
+
direction: [0, 0, 0],
|
|
159
|
+
length: 0,
|
|
160
|
+
axis: acclaimEuler(root.axis, root.orientation.map((v) => v * toRad)),
|
|
161
|
+
dof: root.order,
|
|
162
|
+
},
|
|
163
|
+
...bones.map((b) => ({
|
|
164
|
+
name: b.name,
|
|
165
|
+
direction: b.direction,
|
|
166
|
+
length: b.length,
|
|
167
|
+
axis: acclaimEuler(b.order, b.angles.map((v) => v * toRad)),
|
|
168
|
+
dof: b.dof,
|
|
169
|
+
})),
|
|
170
|
+
], index = new Map(all.map((b, i) => [b.name, i])), parent = new Int32Array(all.length).fill(-2);
|
|
171
|
+
parent[0] = -1;
|
|
172
|
+
for (const [p, ...children] of hierarchy) {
|
|
173
|
+
const pi = index.get(p);
|
|
174
|
+
if (pi === undefined)
|
|
175
|
+
throw new LoaderError(A, 'syntax', `hierarchy names an unknown bone "${p.slice(0, 32)}"`);
|
|
176
|
+
for (const c of children) {
|
|
177
|
+
const ci = index.get(c);
|
|
178
|
+
if (ci === undefined || ci === 0)
|
|
179
|
+
throw new LoaderError(A, 'syntax', `hierarchy names an unknown bone "${c.slice(0, 32)}"`);
|
|
180
|
+
if (parent[ci] !== -2)
|
|
181
|
+
throw new LoaderError(A, 'syntax', `bone "${c.slice(0, 32)}" has two parents`);
|
|
182
|
+
parent[ci] = pi;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
// Parents first: walk down from the root; a bone the walk never reaches is outside the hierarchy.
|
|
186
|
+
const children = all.map(() => []);
|
|
187
|
+
for (let i = 1; i < all.length; i++) {
|
|
188
|
+
if (parent[i] === -2)
|
|
189
|
+
throw new LoaderError(A, 'syntax', `bone "${all[i].name.slice(0, 32)}" is not in the hierarchy`);
|
|
190
|
+
children[parent[i]].push(i);
|
|
191
|
+
}
|
|
192
|
+
const order = [0];
|
|
193
|
+
for (let k = 0; k < order.length; k++)
|
|
194
|
+
order.push(...children[order[k]]);
|
|
195
|
+
const slot = new Map(order.map((i, k) => [i, k]));
|
|
196
|
+
const end = (i) => all[i].direction.map((v) => v * all[i].length * scale);
|
|
197
|
+
const out = order.map((i) => {
|
|
198
|
+
const p = parent[i], bone = {
|
|
199
|
+
name: all[i].name,
|
|
200
|
+
parent: p < 0 ? -1 : slot.get(p),
|
|
201
|
+
translation: i === 0 ? root.position.map((v) => v * scale) : p === 0 ? [0, 0, 0] : end(p),
|
|
202
|
+
rotation: [0, 0, 0, 1],
|
|
203
|
+
};
|
|
204
|
+
if (i > 0 && !children[i].length)
|
|
205
|
+
bone.endSite = end(i);
|
|
206
|
+
return bone;
|
|
207
|
+
});
|
|
208
|
+
return { name, bones: out, lengthUnit, angle, data: order.map((i) => all[i]), scale };
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Parses an Acclaim AMC motion (the CMU database's `.amc`) for the skeleton {@link parseASF} read. Each frame turns
|
|
212
|
+
* every bone that has rotation degrees of freedom by its Euler angles in the bone's own frame (each listed axis applied
|
|
213
|
+
* in turn, first listed first) and moves the root from its rest position by its translation channels. Every bone with
|
|
214
|
+
* degrees of freedom must have a line in every frame.
|
|
215
|
+
*
|
|
216
|
+
* @param input The file's bytes or text.
|
|
217
|
+
* @param skeleton What {@link parseASF} returned for the trial's subject.
|
|
218
|
+
* @param options Frame time, clip name and the key limit.
|
|
219
|
+
* @returns The skeleton's bones, a clip in `parseBVH`'s shape (for `createKeyframeClip`) and the frame timing.
|
|
220
|
+
* @throws LoaderError with format `AMC` and code `header`, `syntax`, `range` or `limit`.
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```ts
|
|
224
|
+
* import { parseAMC, parseASF } from '@margoengine/loaders/mocap';
|
|
225
|
+
*
|
|
226
|
+
* const skeleton = parseASF(':root\n axis XYZ\n order TX TY TZ RX RY RZ\n:bonedata\n begin\n name spine\n direction 0 1 0\n length 2\n axis 0 0 0 XYZ\n dof rx\n end\n:hierarchy\n begin\n root spine\n end\n');
|
|
227
|
+
* const { clip, frames } = parseAMC(':FULLY-SPECIFIED\n:DEGREES\n1\nroot 0 1 0 0 0 0\nspine 90\n', skeleton);
|
|
228
|
+
* console.log(frames, clip.tracks.map((t) => `${t.target} ${t.path}`)); // 1 [ 'root translation', 'root rotation', 'spine rotation' ]
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
export function parseAMC(input, skeleton, options = {}) {
|
|
232
|
+
const text = toText(input, M), { frameTime = 1 / 120, name = 'mocap', maxKeys = 4_000_000 } = options;
|
|
233
|
+
if (!skeleton || !Array.isArray(skeleton.bones) || !Array.isArray(skeleton.data))
|
|
234
|
+
throw new LoaderError(M, 'range', 'parseAMC needs the skeleton parseASF returned');
|
|
235
|
+
if (!(frameTime > 0) || !Number.isFinite(frameTime))
|
|
236
|
+
throw new LoaderError(M, 'range', `frame time ${frameTime} is not positive`);
|
|
237
|
+
if (!Number.isSafeInteger(maxKeys) || maxKeys < 0)
|
|
238
|
+
throw new LoaderError(M, 'range', 'maxKeys must be a non-negative integer');
|
|
239
|
+
const toRad = skeleton.angle === 'deg' ? Math.PI / 180 : 1, data = skeleton.data, byName = new Map(data.map((d, i) => [d.name, i])), listed = data.map((d, i) => (d.dof.length ? i : -1)).filter((i) => i >= 0), tracks = [], rotationKeys = new Map(), rootMoves = data[0].dof.some((c) => c[0] === 't'), rootKeys = [];
|
|
240
|
+
if (rootMoves)
|
|
241
|
+
tracks.push({ target: data[0].name, path: 'translation', keys: rootKeys });
|
|
242
|
+
for (const i of listed)
|
|
243
|
+
if (data[i].dof.some((c) => c[0] === 'r')) {
|
|
244
|
+
const keys = [];
|
|
245
|
+
rotationKeys.set(i, keys);
|
|
246
|
+
tracks.push({ target: data[i].name, path: 'rotation', keys });
|
|
247
|
+
}
|
|
248
|
+
const lines = text.split(/\r?\n/), values = new Map(), rest = skeleton.bones[0].translation;
|
|
249
|
+
let k = 0, frames = 0;
|
|
250
|
+
for (; k < lines.length; k++) {
|
|
251
|
+
const l = lines[k].trim();
|
|
252
|
+
if (/^\d+$/.test(l))
|
|
253
|
+
break;
|
|
254
|
+
if (l && !l.startsWith('#') && !l.startsWith(':'))
|
|
255
|
+
throw new LoaderError(M, 'header', `unexpected "${l.slice(0, 32)}" before the first frame`);
|
|
256
|
+
}
|
|
257
|
+
const flush = () => {
|
|
258
|
+
if ((frames + 1) * tracks.length > maxKeys)
|
|
259
|
+
throw new LoaderError(M, 'limit', `${frames + 1} frames of ${tracks.length} tracks exceed the limit of ${maxKeys} keys`);
|
|
260
|
+
const time = frames * frameTime;
|
|
261
|
+
for (const i of listed) {
|
|
262
|
+
const d = data[i], v = values.get(i);
|
|
263
|
+
if (!v)
|
|
264
|
+
throw new LoaderError(M, 'syntax', `frame ${frames + 1} has no line for bone "${d.name}"`);
|
|
265
|
+
const axes = [], angles = [], t = [0, 0, 0];
|
|
266
|
+
d.dof.forEach((c, j) => {
|
|
267
|
+
if (c[0] === 'r') {
|
|
268
|
+
axes.push(AXIS[c[1]]);
|
|
269
|
+
angles.push(v[j] * toRad);
|
|
270
|
+
}
|
|
271
|
+
else if (c[0] === 't')
|
|
272
|
+
t[AXIS[c[1]]] = v[j] * skeleton.scale;
|
|
273
|
+
});
|
|
274
|
+
if (i === 0 && rootMoves)
|
|
275
|
+
rootKeys.push({ time, value: [rest[0] + t[0], rest[1] + t[1], rest[2] + t[2]] });
|
|
276
|
+
const keys = rotationKeys.get(i);
|
|
277
|
+
if (keys) {
|
|
278
|
+
const c = d.axis;
|
|
279
|
+
keys.push({ time, value: qmul(qmul(c, acclaimEuler(axes, angles)), [-c[0], -c[1], -c[2], c[3]]) });
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
values.clear();
|
|
283
|
+
frames++;
|
|
284
|
+
};
|
|
285
|
+
let started = false;
|
|
286
|
+
for (; k < lines.length; k++) {
|
|
287
|
+
const l = lines[k].trim();
|
|
288
|
+
if (!l || l.startsWith('#'))
|
|
289
|
+
continue;
|
|
290
|
+
if (/^\d+$/.test(l)) {
|
|
291
|
+
if (started)
|
|
292
|
+
flush();
|
|
293
|
+
started = true;
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
const words = l.split(/\s+/), i = byName.get(words[0]);
|
|
297
|
+
if (i === undefined)
|
|
298
|
+
throw new LoaderError(M, 'syntax', `frame ${frames + 1} names an unknown bone "${words[0].slice(0, 32)}"`);
|
|
299
|
+
const d = data[i];
|
|
300
|
+
if (words.length - 1 !== d.dof.length)
|
|
301
|
+
throw new LoaderError(M, 'syntax', `bone "${d.name}" has ${words.length - 1} values in frame ${frames + 1}; its dof lists ${d.dof.length}`);
|
|
302
|
+
values.set(i, words.slice(1).map((w) => {
|
|
303
|
+
const v = Number(w);
|
|
304
|
+
if (!Number.isFinite(v))
|
|
305
|
+
throw new LoaderError(M, 'syntax', `invalid number "${w.slice(0, 32)}" in frame ${frames + 1}`);
|
|
306
|
+
return v;
|
|
307
|
+
}));
|
|
308
|
+
}
|
|
309
|
+
if (started)
|
|
310
|
+
flush();
|
|
311
|
+
return { bones: skeleton.bones, clip: { name, tracks: frames ? tracks : [] }, frames, frameTime };
|
|
312
|
+
}
|
package/dist/amf.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type LoadedGeometry, type LoadedGroup, type ParseLimits } from './types.js';
|
|
2
|
+
import { type BinaryInput } from './util.js';
|
|
3
|
+
import { type XMLExternalOptions } from './xml.js';
|
|
4
|
+
import { type ZipOptions } from './zip.js';
|
|
5
|
+
/** Options of {@link parseAMF}: size limits, zip limits, units and an AbortSignal. */
|
|
6
|
+
export interface AMFOptions extends ParseLimits, XMLExternalOptions, Pick<ZipOptions, 'maxEntries' | 'maxEntryBytes' | 'maxRatio'> {
|
|
7
|
+
/** `'meters'` (default) scales by the file's unit; `'file'` keeps its own units. */
|
|
8
|
+
units?: 'meters' | 'file';
|
|
9
|
+
signal?: AbortSignal;
|
|
10
|
+
}
|
|
11
|
+
/** What {@link parseAMF} returns: one mesh with a group per object and volume. */
|
|
12
|
+
export interface AMFModel extends LoadedGeometry {
|
|
13
|
+
kind: 'mesh';
|
|
14
|
+
groups: LoadedGroup[];
|
|
15
|
+
/** The file's unit, `millimeter` when it names none. */
|
|
16
|
+
unit: string;
|
|
17
|
+
/** `<metadata>` of the file by `type`. Null prototype. */
|
|
18
|
+
metadata: Record<string, string>;
|
|
19
|
+
/** Things the parser moved past, each listed once. */
|
|
20
|
+
warnings: string[];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Reads an `.amf` file, plain XML or zipped: each object's vertices and volumes, material and object colours with
|
|
24
|
+
* per-vertex colours, and constellations, which place objects by translation and rotation. With no constellation every
|
|
25
|
+
* object appears once. Positions are in metres by default. Curved edges and colour formulas are skipped with a warning.
|
|
26
|
+
*
|
|
27
|
+
* @param input The file's bytes.
|
|
28
|
+
* @param options Size and zip limits, units and an AbortSignal.
|
|
29
|
+
* @returns The merged mesh with a group per object and volume.
|
|
30
|
+
* @throws LoaderError with code `header`, `syntax`, `range`, `limit`, `zip`, `aborted` or `unsupported` (an unknown unit).
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { parseAMF } from '@margoengine/loaders/amf';
|
|
35
|
+
*
|
|
36
|
+
* const file = `<amf unit="millimeter"><object id="1"><mesh><vertices>
|
|
37
|
+
* <vertex><coordinates><x>0</x><y>0</y><z>0</z></coordinates></vertex>
|
|
38
|
+
* <vertex><coordinates><x>10</x><y>0</y><z>0</z></coordinates></vertex>
|
|
39
|
+
* <vertex><coordinates><x>0</x><y>10</y><z>0</z></coordinates></vertex>
|
|
40
|
+
* </vertices><volume><triangle><v1>0</v1><v2>1</v2><v3>2</v3></triangle></volume></mesh></object></amf>`;
|
|
41
|
+
* const { positions, unit } = await parseAMF(new TextEncoder().encode(file));
|
|
42
|
+
* console.log(positions.length, unit);
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare function parseAMF(input: BinaryInput, options?: AMFOptions): Promise<AMFModel>;
|
package/dist/amf.js
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// Additive Manufacturing Format .amf files, plain or zipped: objects, volumes, materials and constellations.
|
|
2
|
+
import { LoaderError } from './types.js';
|
|
3
|
+
import { floats, limits, throwIfAborted, toBytes, uints } from './util.js';
|
|
4
|
+
import { decodeXML, readXMLTree } from './xml.js';
|
|
5
|
+
import { readZipDirectory, readZipEntry } from './zip.js';
|
|
6
|
+
const F = 'AMF';
|
|
7
|
+
const UNITS = Object.assign(Object.create(null), {
|
|
8
|
+
millimeter: 1e-3,
|
|
9
|
+
inch: 0.0254,
|
|
10
|
+
feet: 0.3048,
|
|
11
|
+
foot: 0.3048,
|
|
12
|
+
meter: 1,
|
|
13
|
+
micron: 1e-6,
|
|
14
|
+
});
|
|
15
|
+
const MAX_INSTANCES = 100_000;
|
|
16
|
+
const MAX_DEPTH = 32;
|
|
17
|
+
const kids = (element, name) => element.children.filter((c) => c.name === name);
|
|
18
|
+
const kid = (element, name) => element.children.find((c) => c.name === name);
|
|
19
|
+
function number(element, fallback, what) {
|
|
20
|
+
if (!element) {
|
|
21
|
+
if (fallback === undefined)
|
|
22
|
+
throw new LoaderError(F, 'syntax', `<${what}> is missing`);
|
|
23
|
+
return fallback;
|
|
24
|
+
}
|
|
25
|
+
const value = Number(element.text.trim());
|
|
26
|
+
if (!element.text.trim() || !Number.isFinite(value))
|
|
27
|
+
throw new LoaderError(F, 'syntax', `<${what}> holds "${element.text.trim().slice(0, 32)}"`);
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
/** A constant `<color>`; the format also allows formulas of x, y and z, which come back undefined. */
|
|
31
|
+
function readColor(element, warn) {
|
|
32
|
+
if (!element)
|
|
33
|
+
return undefined;
|
|
34
|
+
const channel = (name, fallback) => {
|
|
35
|
+
const text = kid(element, name)?.text.trim();
|
|
36
|
+
if (text === undefined || text === '')
|
|
37
|
+
return fallback;
|
|
38
|
+
const value = Number(text);
|
|
39
|
+
return Number.isFinite(value) ? value : Number.NaN;
|
|
40
|
+
};
|
|
41
|
+
const rgba = [channel('r'), channel('g'), channel('b'), channel('a', 1)];
|
|
42
|
+
if (rgba.some((value) => value === undefined || Number.isNaN(value))) {
|
|
43
|
+
warn('colour formulas are not read');
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
return rgba;
|
|
47
|
+
}
|
|
48
|
+
/** Row-major 3x4 of a translation, then rotations about x, y and z in degrees. */
|
|
49
|
+
function placement(instance) {
|
|
50
|
+
const value = (name) => number(kid(instance, name), 0, name), radians = Math.PI / 180, [cx, sx] = [Math.cos(value('rx') * radians), Math.sin(value('rx') * radians)], [cy, sy] = [Math.cos(value('ry') * radians), Math.sin(value('ry') * radians)], [cz, sz] = [Math.cos(value('rz') * radians), Math.sin(value('rz') * radians)];
|
|
51
|
+
// Rz * Ry * Rx, so x turns first.
|
|
52
|
+
return [
|
|
53
|
+
cz * cy,
|
|
54
|
+
cz * sy * sx - sz * cx,
|
|
55
|
+
cz * sy * cx + sz * sx,
|
|
56
|
+
value('deltax'),
|
|
57
|
+
sz * cy,
|
|
58
|
+
sz * sy * sx + cz * cx,
|
|
59
|
+
sz * sy * cx - cz * sx,
|
|
60
|
+
value('deltay'),
|
|
61
|
+
-sy,
|
|
62
|
+
cy * sx,
|
|
63
|
+
cy * cx,
|
|
64
|
+
value('deltaz'),
|
|
65
|
+
];
|
|
66
|
+
}
|
|
67
|
+
function compose(a, b) {
|
|
68
|
+
const out = new Array(12);
|
|
69
|
+
for (let r = 0; r < 3; r++)
|
|
70
|
+
for (let c = 0; c < 4; c++)
|
|
71
|
+
out[r * 4 + c] =
|
|
72
|
+
a[r * 4] * b[c] + a[r * 4 + 1] * b[4 + c] + a[r * 4 + 2] * b[8 + c] + (c === 3 ? a[r * 4 + 3] : 0);
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
const IDENTITY = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0];
|
|
76
|
+
/**
|
|
77
|
+
* Reads an `.amf` file, plain XML or zipped: each object's vertices and volumes, material and object colours with
|
|
78
|
+
* per-vertex colours, and constellations, which place objects by translation and rotation. With no constellation every
|
|
79
|
+
* object appears once. Positions are in metres by default. Curved edges and colour formulas are skipped with a warning.
|
|
80
|
+
*
|
|
81
|
+
* @param input The file's bytes.
|
|
82
|
+
* @param options Size and zip limits, units and an AbortSignal.
|
|
83
|
+
* @returns The merged mesh with a group per object and volume.
|
|
84
|
+
* @throws LoaderError with code `header`, `syntax`, `range`, `limit`, `zip`, `aborted` or `unsupported` (an unknown unit).
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* import { parseAMF } from '@margoengine/loaders/amf';
|
|
89
|
+
*
|
|
90
|
+
* const file = `<amf unit="millimeter"><object id="1"><mesh><vertices>
|
|
91
|
+
* <vertex><coordinates><x>0</x><y>0</y><z>0</z></coordinates></vertex>
|
|
92
|
+
* <vertex><coordinates><x>10</x><y>0</y><z>0</z></coordinates></vertex>
|
|
93
|
+
* <vertex><coordinates><x>0</x><y>10</y><z>0</z></coordinates></vertex>
|
|
94
|
+
* </vertices><volume><triangle><v1>0</v1><v2>1</v2><v3>2</v3></triangle></volume></mesh></object></amf>`;
|
|
95
|
+
* const { positions, unit } = await parseAMF(new TextEncoder().encode(file));
|
|
96
|
+
* console.log(positions.length, unit);
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export async function parseAMF(input, options = {}) {
|
|
100
|
+
const source = toBytes(input, F), { maxVertices, maxTriangles } = limits(options, F);
|
|
101
|
+
throwIfAborted(options.signal, F);
|
|
102
|
+
if (options.units !== undefined && options.units !== 'meters' && options.units !== 'file')
|
|
103
|
+
throw new LoaderError(F, 'range', 'units must be "meters" or "file"');
|
|
104
|
+
let bytes = source;
|
|
105
|
+
if (source[0] === 0x50 && source[1] === 0x4b) {
|
|
106
|
+
const entries = readZipDirectory(source, options), entry = entries.find((candidate) => /\.amf$/i.test(candidate.name)) ?? entries[0];
|
|
107
|
+
if (!entry)
|
|
108
|
+
throw new LoaderError(F, 'zip', 'the archive is empty');
|
|
109
|
+
bytes = await readZipEntry(source, entry, options);
|
|
110
|
+
}
|
|
111
|
+
const root = kid(readXMLTree(decodeXML(bytes), F, { external: options.external }), 'amf');
|
|
112
|
+
if (!root)
|
|
113
|
+
throw new LoaderError(F, 'header', 'no <amf> element');
|
|
114
|
+
const unit = root.attributes.unit ?? 'millimeter';
|
|
115
|
+
if (!(unit in UNITS))
|
|
116
|
+
throw new LoaderError(F, 'unsupported', `unknown unit "${unit.slice(0, 32)}"`);
|
|
117
|
+
const scale = options.units === 'file' ? 1 : UNITS[unit], warnings = new Set(), warn = (message) => void warnings.add(message), metadata = Object.create(null), materials = new Map(), objects = new Map(), constellations = new Map();
|
|
118
|
+
for (const entry of kids(root, 'metadata'))
|
|
119
|
+
metadata[entry.attributes.type ?? ''] = entry.text.trim();
|
|
120
|
+
for (const material of kids(root, 'material')) {
|
|
121
|
+
const color = readColor(kid(material, 'color'), warn);
|
|
122
|
+
if (color)
|
|
123
|
+
materials.set(material.attributes.id ?? '', color);
|
|
124
|
+
}
|
|
125
|
+
for (const object of kids(root, 'object'))
|
|
126
|
+
objects.set(object.attributes.id ?? '', object);
|
|
127
|
+
for (const entry of kids(root, 'constellation'))
|
|
128
|
+
constellations.set(entry.attributes.id ?? '', entry);
|
|
129
|
+
const positions = floats(), colors = floats(), indices = uints(), groups = [], placing = new Set(),
|
|
130
|
+
// Vertices with a colour of their own (a vertex or triangle colour), which no lower level overrides.
|
|
131
|
+
owned = [];
|
|
132
|
+
let vertexColors = false, instances = 0;
|
|
133
|
+
const emit = (object, m) => {
|
|
134
|
+
const mesh = kid(object, 'mesh');
|
|
135
|
+
if (!mesh)
|
|
136
|
+
return;
|
|
137
|
+
const vertices = kid(mesh, 'vertices');
|
|
138
|
+
if (!vertices)
|
|
139
|
+
throw new LoaderError(F, 'syntax', '<mesh> has no <vertices>');
|
|
140
|
+
const list = kids(vertices, 'vertex'), base = positions.length / 3;
|
|
141
|
+
if (kid(vertices, 'edge'))
|
|
142
|
+
warn('curved edges are drawn straight');
|
|
143
|
+
if (base + list.length > maxVertices)
|
|
144
|
+
throw new LoaderError(F, 'limit', `vertex count exceeds the limit of ${maxVertices}`);
|
|
145
|
+
for (const vertex of list) {
|
|
146
|
+
const at = kid(vertex, 'coordinates');
|
|
147
|
+
if (!at)
|
|
148
|
+
throw new LoaderError(F, 'syntax', '<vertex> has no <coordinates>');
|
|
149
|
+
const x = number(kid(at, 'x'), undefined, 'x'), y = number(kid(at, 'y'), undefined, 'y'), z = number(kid(at, 'z'), undefined, 'z');
|
|
150
|
+
positions.push3((m[0] * x + m[1] * y + m[2] * z + m[3]) * scale, (m[4] * x + m[5] * y + m[6] * z + m[7]) * scale, (m[8] * x + m[9] * y + m[10] * z + m[11]) * scale);
|
|
151
|
+
const color = readColor(kid(vertex, 'color'), warn);
|
|
152
|
+
if (color)
|
|
153
|
+
vertexColors = true;
|
|
154
|
+
const [r, g, b] = color ?? [1, 1, 1];
|
|
155
|
+
colors.push3(r, g, b);
|
|
156
|
+
owned.push(!!color);
|
|
157
|
+
}
|
|
158
|
+
const objectColor = readColor(kid(object, 'color'), warn);
|
|
159
|
+
for (const volume of kids(mesh, 'volume')) {
|
|
160
|
+
const start = indices.length,
|
|
161
|
+
// Colour precedence (ISO/ASTM 52915): triangle, then vertex, then volume, then object, then material.
|
|
162
|
+
color = readColor(kid(volume, 'color'), warn) ?? objectColor ?? materials.get(volume.attributes.materialid ?? '');
|
|
163
|
+
for (const triangle of kids(volume, 'triangle')) {
|
|
164
|
+
if (indices.length / 3 >= maxTriangles)
|
|
165
|
+
throw new LoaderError(F, 'limit', `triangle count exceeds the limit of ${maxTriangles}`);
|
|
166
|
+
const corner = (name) => {
|
|
167
|
+
const value = number(kid(triangle, name), undefined, name);
|
|
168
|
+
if (!Number.isInteger(value) || value < 0 || value >= list.length)
|
|
169
|
+
throw new LoaderError(F, 'range', `${name} ${value} is outside 0..${list.length - 1}`);
|
|
170
|
+
return base + value;
|
|
171
|
+
};
|
|
172
|
+
const corners = [corner('v1'), corner('v2'), corner('v3')], own = readColor(kid(triangle, 'color'), warn);
|
|
173
|
+
if (own) {
|
|
174
|
+
// A triangle colour outranks its vertices' colours: the triangle gets three corners of its own.
|
|
175
|
+
if (positions.length / 3 + 3 > maxVertices)
|
|
176
|
+
throw new LoaderError(F, 'limit', `vertex count exceeds the limit of ${maxVertices}`);
|
|
177
|
+
vertexColors = true;
|
|
178
|
+
for (let k = 0; k < 3; k++) {
|
|
179
|
+
const p = corners[k] * 3;
|
|
180
|
+
corners[k] = positions.length / 3;
|
|
181
|
+
positions.push3(positions.array[p], positions.array[p + 1], positions.array[p + 2]);
|
|
182
|
+
colors.push3(own[0], own[1], own[2]);
|
|
183
|
+
owned.push(true);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
else if (color)
|
|
187
|
+
// A vertex without a colour of its own shows the first volume colour that reaches it.
|
|
188
|
+
for (const v of corners)
|
|
189
|
+
if (!owned[v]) {
|
|
190
|
+
colors.array.set(color.slice(0, 3), v * 3);
|
|
191
|
+
owned[v] = true;
|
|
192
|
+
}
|
|
193
|
+
indices.push3(corners[0], corners[1], corners[2]);
|
|
194
|
+
}
|
|
195
|
+
if (indices.length === start)
|
|
196
|
+
continue;
|
|
197
|
+
const group = { start, count: indices.length - start, objectId: object.attributes.id }, name = kids(object, 'metadata')
|
|
198
|
+
.find((entry) => entry.attributes.type === 'name')
|
|
199
|
+
?.text.trim();
|
|
200
|
+
if (name)
|
|
201
|
+
group.name = name;
|
|
202
|
+
if (color)
|
|
203
|
+
group.color = [...color];
|
|
204
|
+
groups.push(group);
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
const place = (id, m, depth) => {
|
|
208
|
+
if (depth > MAX_DEPTH)
|
|
209
|
+
throw new LoaderError(F, 'limit', `constellations nest deeper than ${MAX_DEPTH}`);
|
|
210
|
+
if (++instances > MAX_INSTANCES)
|
|
211
|
+
throw new LoaderError(F, 'limit', `the file expands to more than ${MAX_INSTANCES} instances`);
|
|
212
|
+
const object = objects.get(id);
|
|
213
|
+
if (object) {
|
|
214
|
+
emit(object, m);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
const constellation = constellations.get(id);
|
|
218
|
+
if (!constellation)
|
|
219
|
+
throw new LoaderError(F, 'syntax', `instance of ${id.slice(0, 32)}, which is not defined`);
|
|
220
|
+
if (placing.has(id))
|
|
221
|
+
throw new LoaderError(F, 'syntax', `constellation ${id.slice(0, 32)} contains itself`);
|
|
222
|
+
placing.add(id);
|
|
223
|
+
for (const instance of kids(constellation, 'instance'))
|
|
224
|
+
place(instance.attributes.objectid ?? '', compose(m, placement(instance)), depth + 1);
|
|
225
|
+
placing.delete(id);
|
|
226
|
+
};
|
|
227
|
+
if (constellations.size) {
|
|
228
|
+
// Only a constellation no other one uses is a root of the build.
|
|
229
|
+
const used = new Set();
|
|
230
|
+
for (const entry of constellations.values())
|
|
231
|
+
for (const instance of kids(entry, 'instance'))
|
|
232
|
+
used.add(instance.attributes.objectid ?? '');
|
|
233
|
+
const roots = [...constellations.keys()].filter((id) => !used.has(id));
|
|
234
|
+
if (!roots.length)
|
|
235
|
+
throw new LoaderError(F, 'syntax', 'every constellation contains another');
|
|
236
|
+
for (const id of roots)
|
|
237
|
+
place(id, IDENTITY, 0);
|
|
238
|
+
}
|
|
239
|
+
else
|
|
240
|
+
for (const id of objects.keys())
|
|
241
|
+
place(id, IDENTITY, 0);
|
|
242
|
+
if (!indices.length)
|
|
243
|
+
throw new LoaderError(F, 'syntax', 'the file holds no triangles');
|
|
244
|
+
const model = {
|
|
245
|
+
kind: 'mesh',
|
|
246
|
+
positions: positions.result(),
|
|
247
|
+
indices: indices.result(),
|
|
248
|
+
groups,
|
|
249
|
+
unit,
|
|
250
|
+
metadata,
|
|
251
|
+
warnings: [...warnings],
|
|
252
|
+
};
|
|
253
|
+
if (vertexColors)
|
|
254
|
+
model.colors = colors.result();
|
|
255
|
+
return model;
|
|
256
|
+
}
|