@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/3ds.js
ADDED
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
// 3D Studio .3ds files: the chunk tree, triangle meshes, UVs, materials, the face lists that assign them, cameras, lights
|
|
2
|
+
// and the keyframer's nodes and tracks.
|
|
3
|
+
import { LoaderError } from './types.js';
|
|
4
|
+
import { floats, latin1, limits, toBytes, uints } from './util.js';
|
|
5
|
+
const F = '3DS';
|
|
6
|
+
const MAX_DEPTH = 32;
|
|
7
|
+
class Reader {
|
|
8
|
+
constructor(bytes) {
|
|
9
|
+
this.bytes = bytes;
|
|
10
|
+
this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
11
|
+
}
|
|
12
|
+
/** The chunks directly inside `start..end`, each checked against its parent. */
|
|
13
|
+
*chunks(start, end) {
|
|
14
|
+
for (let pos = start; pos + 6 <= end;) {
|
|
15
|
+
const id = this.view.getUint16(pos, true), length = this.view.getUint32(pos + 2, true);
|
|
16
|
+
if (length < 6 || pos + length > end)
|
|
17
|
+
throw new LoaderError(F, 'truncated', `chunk 0x${id.toString(16)} at ${pos} runs past its parent`);
|
|
18
|
+
yield { id, start: pos + 6, end: pos + length };
|
|
19
|
+
pos += length;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** Reads a zero-ended name and returns it with the offset after it. */
|
|
23
|
+
name(start, end) {
|
|
24
|
+
let pos = start;
|
|
25
|
+
while (pos < end && this.bytes[pos])
|
|
26
|
+
pos++;
|
|
27
|
+
if (pos >= end)
|
|
28
|
+
throw new LoaderError(F, 'truncated', 'a name has no end');
|
|
29
|
+
return [latin1(this.bytes, start, pos), pos + 1];
|
|
30
|
+
}
|
|
31
|
+
need(start, size, end, what) {
|
|
32
|
+
if (start + size > end)
|
|
33
|
+
throw new LoaderError(F, 'truncated', `${what} run past their chunk`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function color(reader, from, to) {
|
|
37
|
+
let found;
|
|
38
|
+
for (const { id, start, end } of reader.chunks(from, to)) {
|
|
39
|
+
// Prefer the gamma-corrected form when a file gives both, which is the one the artist saw.
|
|
40
|
+
if (id === 0x0011 || id === 0x0012) {
|
|
41
|
+
reader.need(start, 3, end, 'colour bytes');
|
|
42
|
+
found = [reader.bytes[start] / 255, reader.bytes[start + 1] / 255, reader.bytes[start + 2] / 255];
|
|
43
|
+
}
|
|
44
|
+
else if ((id === 0x0010 || id === 0x0013) && !found) {
|
|
45
|
+
reader.need(start, 12, end, 'colour floats');
|
|
46
|
+
const at = (i) => reader.view.getFloat32(start + i * 4, true);
|
|
47
|
+
found = [at(0), at(1), at(2)];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return found;
|
|
51
|
+
}
|
|
52
|
+
function percent(reader, chunk) {
|
|
53
|
+
for (const { id, start, end } of reader.chunks(chunk.start, chunk.end)) {
|
|
54
|
+
if (id === 0x0030) {
|
|
55
|
+
reader.need(start, 2, end, 'a percentage');
|
|
56
|
+
return reader.view.getInt16(start, true) / 100;
|
|
57
|
+
}
|
|
58
|
+
if (id === 0x0031) {
|
|
59
|
+
reader.need(start, 4, end, 'a percentage');
|
|
60
|
+
return reader.view.getFloat32(start, true) / 100;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return 0;
|
|
64
|
+
}
|
|
65
|
+
function material(reader, chunk) {
|
|
66
|
+
const out = { name: '', color: [1, 1, 1, 1] };
|
|
67
|
+
for (const part of reader.chunks(chunk.start, chunk.end)) {
|
|
68
|
+
if (part.id === 0xa000)
|
|
69
|
+
[out.name] = reader.name(part.start, part.end);
|
|
70
|
+
else if (part.id === 0xa020) {
|
|
71
|
+
const rgb = color(reader, part.start, part.end);
|
|
72
|
+
if (rgb)
|
|
73
|
+
out.color = [...rgb, out.color[3]];
|
|
74
|
+
}
|
|
75
|
+
else if (part.id === 0xa050)
|
|
76
|
+
out.color[3] = 1 - percent(reader, part);
|
|
77
|
+
else if (part.id === 0xa200)
|
|
78
|
+
for (const map of reader.chunks(part.start, part.end))
|
|
79
|
+
if (map.id === 0xa300)
|
|
80
|
+
[out.texture] = reader.name(map.start, map.end);
|
|
81
|
+
}
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
function vector(reader, at, end, what) {
|
|
85
|
+
reader.need(at, 12, end, what);
|
|
86
|
+
const { view } = reader;
|
|
87
|
+
return [view.getFloat32(at, true), view.getFloat32(at + 4, true), view.getFloat32(at + 8, true)];
|
|
88
|
+
}
|
|
89
|
+
function camera(reader, name, chunk) {
|
|
90
|
+
reader.need(chunk.start, 32, chunk.end, 'camera values');
|
|
91
|
+
const { view } = reader;
|
|
92
|
+
return {
|
|
93
|
+
name,
|
|
94
|
+
position: vector(reader, chunk.start, chunk.end, 'camera values'),
|
|
95
|
+
target: vector(reader, chunk.start + 12, chunk.end, 'camera values'),
|
|
96
|
+
roll: view.getFloat32(chunk.start + 24, true),
|
|
97
|
+
lens: view.getFloat32(chunk.start + 28, true),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function light(reader, name, chunk) {
|
|
101
|
+
const out = {
|
|
102
|
+
name,
|
|
103
|
+
position: vector(reader, chunk.start, chunk.end, 'light values'),
|
|
104
|
+
color: color(reader, chunk.start + 12, chunk.end) ?? [1, 1, 1],
|
|
105
|
+
on: true,
|
|
106
|
+
multiplier: 1,
|
|
107
|
+
};
|
|
108
|
+
for (const part of reader.chunks(chunk.start + 12, chunk.end)) {
|
|
109
|
+
if (part.id === 0x4620)
|
|
110
|
+
out.on = false;
|
|
111
|
+
else if (part.id === 0x465b) {
|
|
112
|
+
reader.need(part.start, 4, part.end, 'the light multiplier');
|
|
113
|
+
out.multiplier = reader.view.getFloat32(part.start, true);
|
|
114
|
+
}
|
|
115
|
+
else if (part.id === 0x4610) {
|
|
116
|
+
reader.need(part.start, 20, part.end, 'spotlight values');
|
|
117
|
+
out.spot = {
|
|
118
|
+
target: vector(reader, part.start, part.end, 'spotlight values'),
|
|
119
|
+
hotspot: reader.view.getFloat32(part.start + 12, true),
|
|
120
|
+
falloff: reader.view.getFloat32(part.start + 16, true),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return out;
|
|
125
|
+
}
|
|
126
|
+
const NODE_KINDS = {
|
|
127
|
+
0xb001: 'ambient',
|
|
128
|
+
0xb002: 'object',
|
|
129
|
+
0xb003: 'camera',
|
|
130
|
+
0xb004: 'cameraTarget',
|
|
131
|
+
0xb005: 'light',
|
|
132
|
+
0xb006: 'spotTarget',
|
|
133
|
+
0xb007: 'spot',
|
|
134
|
+
};
|
|
135
|
+
/** Each track chunk's name in {@link ThreeDSNode.tracks} and the floats one key's value holds. */
|
|
136
|
+
const TRACKS = {
|
|
137
|
+
0xb020: ['position', 3],
|
|
138
|
+
0xb021: ['rotation', 4],
|
|
139
|
+
0xb022: ['scale', 3],
|
|
140
|
+
0xb023: ['fov', 1],
|
|
141
|
+
0xb024: ['roll', 1],
|
|
142
|
+
0xb025: ['color', 3],
|
|
143
|
+
0xb027: ['hotspot', 1],
|
|
144
|
+
0xb028: ['falloff', 1],
|
|
145
|
+
};
|
|
146
|
+
const SPLINE = ['tension', 'continuity', 'bias', 'easeTo', 'easeFrom'];
|
|
147
|
+
function track(reader, chunk, size) {
|
|
148
|
+
const { view } = reader;
|
|
149
|
+
reader.need(chunk.start, 14, chunk.end, 'a track header');
|
|
150
|
+
// The flags, eight bytes that mean nothing, and the key count.
|
|
151
|
+
const flags = view.getUint16(chunk.start, true), count = view.getUint32(chunk.start + 10, true), keys = [];
|
|
152
|
+
let at = chunk.start + 14;
|
|
153
|
+
for (let i = 0; i < count; i++) {
|
|
154
|
+
reader.need(at, 6, chunk.end, 'track keys');
|
|
155
|
+
const key = { frame: view.getUint32(at, true), value: [] }, used = view.getUint16(at + 4, true);
|
|
156
|
+
at += 6;
|
|
157
|
+
SPLINE.forEach((name, bit) => {
|
|
158
|
+
if (!(used & (1 << bit)))
|
|
159
|
+
return;
|
|
160
|
+
reader.need(at, 4, chunk.end, 'track keys');
|
|
161
|
+
key[name] = view.getFloat32(at, true);
|
|
162
|
+
at += 4;
|
|
163
|
+
});
|
|
164
|
+
reader.need(at, size * 4, chunk.end, 'track keys');
|
|
165
|
+
for (let k = 0; k < size; k++, at += 4)
|
|
166
|
+
key.value.push(view.getFloat32(at, true));
|
|
167
|
+
keys.push(key);
|
|
168
|
+
}
|
|
169
|
+
return { flags, keys };
|
|
170
|
+
}
|
|
171
|
+
function keyframes(reader, chunk) {
|
|
172
|
+
const out = { start: 0, end: 0, length: 0, nodes: [] }, { view } = reader;
|
|
173
|
+
for (const part of reader.chunks(chunk.start, chunk.end)) {
|
|
174
|
+
if (part.id === 0xb00a) {
|
|
175
|
+
const [, after] = reader.name(part.start + 2, part.end);
|
|
176
|
+
reader.need(after, 4, part.end, 'the keyframer header');
|
|
177
|
+
out.length = view.getUint32(after, true);
|
|
178
|
+
}
|
|
179
|
+
else if (part.id === 0xb008) {
|
|
180
|
+
reader.need(part.start, 8, part.end, 'the keyframer segment');
|
|
181
|
+
out.start = view.getUint32(part.start, true);
|
|
182
|
+
out.end = view.getUint32(part.start + 4, true);
|
|
183
|
+
}
|
|
184
|
+
else if (NODE_KINDS[part.id]) {
|
|
185
|
+
const node = {
|
|
186
|
+
id: out.nodes.length,
|
|
187
|
+
kind: NODE_KINDS[part.id],
|
|
188
|
+
name: '',
|
|
189
|
+
parent: -1,
|
|
190
|
+
pivot: [0, 0, 0],
|
|
191
|
+
tracks: {},
|
|
192
|
+
};
|
|
193
|
+
for (const field of reader.chunks(part.start, part.end)) {
|
|
194
|
+
if (field.id === 0xb030) {
|
|
195
|
+
reader.need(field.start, 2, field.end, 'a node id');
|
|
196
|
+
node.id = view.getUint16(field.start, true);
|
|
197
|
+
}
|
|
198
|
+
else if (field.id === 0xb010) {
|
|
199
|
+
const [name, after] = reader.name(field.start, field.end);
|
|
200
|
+
reader.need(after, 6, field.end, 'a node header');
|
|
201
|
+
node.name = name;
|
|
202
|
+
node.parent = view.getInt16(after + 4, true);
|
|
203
|
+
}
|
|
204
|
+
else if (field.id === 0xb011)
|
|
205
|
+
[node.instance] = reader.name(field.start, field.end);
|
|
206
|
+
else if (field.id === 0xb013)
|
|
207
|
+
node.pivot = vector(reader, field.start, field.end, 'a pivot');
|
|
208
|
+
else if (TRACKS[field.id]) {
|
|
209
|
+
const [name, size] = TRACKS[field.id];
|
|
210
|
+
node.tracks[name] = track(reader, field, size);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
out.nodes.push(node);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return out;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Reads a 3D Studio `.3ds` file: every triangle mesh with its UVs, one group per object and material, the materials'
|
|
220
|
+
* colours, transparency and diffuse map names, the cameras and lights, and the keyframer's nodes and tracks. Vertices are
|
|
221
|
+
* saved in world space, so no transform applies. Cameras, lights and keyframes are returned as the file holds them, in
|
|
222
|
+
* its coordinates. `loadTextures` from `@margoengine/loaders/textures` reads the maps the materials name.
|
|
223
|
+
*
|
|
224
|
+
* @param input The file's bytes.
|
|
225
|
+
* @param options The shared size limits.
|
|
226
|
+
* @returns The merged mesh, its materials, the master scale, cameras, lights and keyframes.
|
|
227
|
+
* @throws LoaderError with code `header`, `truncated`, `range`, `limit` or `syntax` (no mesh in the file).
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* ```ts
|
|
231
|
+
* import { parse3DS } from '@margoengine/loaders/3ds';
|
|
232
|
+
*
|
|
233
|
+
* const bytes = await (await fetch('/models/teapot.3ds')).arrayBuffer();
|
|
234
|
+
* const { groups, materials, cameras, keyframes } = parse3DS(bytes);
|
|
235
|
+
* console.log(groups.length, materials[0]?.texture, cameras[0]?.lens, keyframes?.nodes.length);
|
|
236
|
+
* ```
|
|
237
|
+
*/
|
|
238
|
+
export function parse3DS(input, options = {}) {
|
|
239
|
+
const bytes = toBytes(input, F), { maxVertices, maxTriangles } = limits(options, F);
|
|
240
|
+
if (bytes.length < 6)
|
|
241
|
+
throw new LoaderError(F, 'header', 'the file is too small');
|
|
242
|
+
const reader = new Reader(bytes), { view } = reader;
|
|
243
|
+
if (view.getUint16(0, true) !== 0x4d4d)
|
|
244
|
+
throw new LoaderError(F, 'header', 'missing the 0x4D4D main chunk');
|
|
245
|
+
const length = view.getUint32(2, true);
|
|
246
|
+
if (length < 6 || length > bytes.length)
|
|
247
|
+
throw new LoaderError(F, 'truncated', 'the main chunk runs past the file');
|
|
248
|
+
const positions = floats(), uvs = floats(), indices = uints(), groups = [], materials = [], cameras = [], lights = [], pending = [];
|
|
249
|
+
let masterScale = 1, textured = false, ambient, keyframer;
|
|
250
|
+
const trimesh = (name, chunk) => {
|
|
251
|
+
const base = positions.length / 3;
|
|
252
|
+
let count = 0, faces, coordinates = false;
|
|
253
|
+
const lists = [];
|
|
254
|
+
for (const part of reader.chunks(chunk.start, chunk.end)) {
|
|
255
|
+
if (part.id === 0x4110) {
|
|
256
|
+
reader.need(part.start, 2, part.end, 'vertices');
|
|
257
|
+
count = view.getUint16(part.start, true);
|
|
258
|
+
reader.need(part.start + 2, count * 12, part.end, 'vertices');
|
|
259
|
+
if (base + count > maxVertices)
|
|
260
|
+
throw new LoaderError(F, 'limit', `vertex count exceeds the limit of ${maxVertices}`);
|
|
261
|
+
for (let i = 0, at = part.start + 2; i < count; i++, at += 12)
|
|
262
|
+
positions.push3(view.getFloat32(at, true), view.getFloat32(at + 4, true), view.getFloat32(at + 8, true));
|
|
263
|
+
}
|
|
264
|
+
else if (part.id === 0x4120) {
|
|
265
|
+
reader.need(part.start, 2, part.end, 'faces');
|
|
266
|
+
const total = view.getUint16(part.start, true), after = part.start + 2 + total * 8;
|
|
267
|
+
reader.need(part.start + 2, total * 8, part.end, 'faces');
|
|
268
|
+
faces = new Uint32Array(total * 3);
|
|
269
|
+
for (let i = 0; i < total; i++)
|
|
270
|
+
for (let k = 0; k < 3; k++)
|
|
271
|
+
faces[i * 3 + k] = view.getUint16(part.start + 2 + i * 8 + k * 2, true);
|
|
272
|
+
for (const list of reader.chunks(after, part.end)) {
|
|
273
|
+
if (list.id !== 0x4130)
|
|
274
|
+
continue;
|
|
275
|
+
const [material, at] = reader.name(list.start, list.end);
|
|
276
|
+
reader.need(at, 2, list.end, 'a face list');
|
|
277
|
+
const size = view.getUint16(at, true);
|
|
278
|
+
reader.need(at + 2, size * 2, list.end, 'a face list');
|
|
279
|
+
const members = [];
|
|
280
|
+
for (let i = 0; i < size; i++) {
|
|
281
|
+
const face = view.getUint16(at + 2 + i * 2, true);
|
|
282
|
+
if (face >= total)
|
|
283
|
+
throw new LoaderError(F, 'range', `face ${face} of ${name.slice(0, 32)} does not exist`);
|
|
284
|
+
members.push(face);
|
|
285
|
+
}
|
|
286
|
+
lists.push({ material, faces: members });
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
else if (part.id === 0x4140) {
|
|
290
|
+
reader.need(part.start, 2, part.end, 'texture coordinates');
|
|
291
|
+
const total = view.getUint16(part.start, true);
|
|
292
|
+
reader.need(part.start + 2, total * 8, part.end, 'texture coordinates');
|
|
293
|
+
if (uvs.length < base * 2)
|
|
294
|
+
for (let i = uvs.length; i < base * 2; i++)
|
|
295
|
+
uvs.push(0);
|
|
296
|
+
for (let i = 0, at = part.start + 2; i < total; i++, at += 8) {
|
|
297
|
+
uvs.push(view.getFloat32(at, true));
|
|
298
|
+
uvs.push(1 - view.getFloat32(at + 4, true));
|
|
299
|
+
}
|
|
300
|
+
coordinates = textured = true;
|
|
301
|
+
if (count && total !== count)
|
|
302
|
+
throw new LoaderError(F, 'range', `${name.slice(0, 32)} has ${total} coordinates for ${count} vertices`);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
if (textured && !coordinates)
|
|
306
|
+
for (let i = 0; i < count * 2; i++)
|
|
307
|
+
uvs.push(0);
|
|
308
|
+
if (!faces)
|
|
309
|
+
return;
|
|
310
|
+
for (let i = 0; i < faces.length; i++)
|
|
311
|
+
if (faces[i] >= count)
|
|
312
|
+
throw new LoaderError(F, 'range', `face index ${faces[i]} is outside 0..${count - 1}`);
|
|
313
|
+
pending.push({ name, base, faces, lists });
|
|
314
|
+
};
|
|
315
|
+
const walk = (start, end, depth) => {
|
|
316
|
+
if (depth > MAX_DEPTH)
|
|
317
|
+
throw new LoaderError(F, 'limit', `chunks nest deeper than ${MAX_DEPTH}`);
|
|
318
|
+
for (const chunk of reader.chunks(start, end)) {
|
|
319
|
+
if (chunk.id === 0x3d3d)
|
|
320
|
+
walk(chunk.start, chunk.end, depth + 1);
|
|
321
|
+
else if (chunk.id === 0xafff)
|
|
322
|
+
materials.push(material(reader, chunk));
|
|
323
|
+
else if (chunk.id === 0xb000)
|
|
324
|
+
keyframer = keyframes(reader, chunk);
|
|
325
|
+
else if (chunk.id === 0x2100)
|
|
326
|
+
ambient = color(reader, chunk.start, chunk.end);
|
|
327
|
+
else if (chunk.id === 0x0100) {
|
|
328
|
+
reader.need(chunk.start, 4, chunk.end, 'the master scale');
|
|
329
|
+
masterScale = view.getFloat32(chunk.start, true);
|
|
330
|
+
}
|
|
331
|
+
else if (chunk.id === 0x4000) {
|
|
332
|
+
const [name, after] = reader.name(chunk.start, chunk.end);
|
|
333
|
+
for (const part of reader.chunks(after, chunk.end)) {
|
|
334
|
+
if (part.id === 0x4100)
|
|
335
|
+
trimesh(name, part);
|
|
336
|
+
else if (part.id === 0x4600)
|
|
337
|
+
lights.push(light(reader, name, part));
|
|
338
|
+
else if (part.id === 0x4700)
|
|
339
|
+
cameras.push(camera(reader, name, part));
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
walk(6, length, 0);
|
|
345
|
+
// Materials can follow the meshes that use them, so the groups are built last.
|
|
346
|
+
const emit = (name, base, faces, members, materialName) => {
|
|
347
|
+
const start = indices.length;
|
|
348
|
+
for (const face of members) {
|
|
349
|
+
if (indices.length / 3 >= maxTriangles)
|
|
350
|
+
throw new LoaderError(F, 'limit', `triangle count exceeds the limit of ${maxTriangles}`);
|
|
351
|
+
indices.push3(base + faces[face * 3], base + faces[face * 3 + 1], base + faces[face * 3 + 2]);
|
|
352
|
+
}
|
|
353
|
+
if (indices.length === start)
|
|
354
|
+
return;
|
|
355
|
+
const group = { start, count: indices.length - start, name }, index = materials.findIndex((entry) => entry.name === materialName);
|
|
356
|
+
if (index >= 0) {
|
|
357
|
+
group.material = index;
|
|
358
|
+
group.color = [...materials[index].color];
|
|
359
|
+
}
|
|
360
|
+
groups.push(group);
|
|
361
|
+
};
|
|
362
|
+
for (const { name, base, faces, lists } of pending) {
|
|
363
|
+
const rest = new Set(Array.from({ length: faces.length / 3 }, (_, i) => i));
|
|
364
|
+
for (const list of lists) {
|
|
365
|
+
const members = list.faces.filter((face) => rest.delete(face));
|
|
366
|
+
emit(name, base, faces, members, list.material);
|
|
367
|
+
}
|
|
368
|
+
emit(name, base, faces, rest);
|
|
369
|
+
}
|
|
370
|
+
if (!indices.length)
|
|
371
|
+
throw new LoaderError(F, 'syntax', 'the file holds no triangle mesh');
|
|
372
|
+
const result = {
|
|
373
|
+
kind: 'mesh',
|
|
374
|
+
positions: positions.result(),
|
|
375
|
+
indices: indices.result(),
|
|
376
|
+
groups,
|
|
377
|
+
materials,
|
|
378
|
+
masterScale,
|
|
379
|
+
cameras,
|
|
380
|
+
lights,
|
|
381
|
+
};
|
|
382
|
+
if (ambient)
|
|
383
|
+
result.ambient = ambient;
|
|
384
|
+
if (keyframer)
|
|
385
|
+
result.keyframes = keyframer;
|
|
386
|
+
if (textured) {
|
|
387
|
+
while (uvs.length < (result.positions.length / 3) * 2)
|
|
388
|
+
uvs.push(0);
|
|
389
|
+
result.uvs = uvs.result();
|
|
390
|
+
}
|
|
391
|
+
return result;
|
|
392
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { BVH, BVHBone } from './mocap.js';
|
|
2
|
+
import { type BinaryInput } from './util.js';
|
|
3
|
+
/** One bone of an {@link ASFSkeleton}, as {@link parseAMC} reads it. */
|
|
4
|
+
export interface ASFBoneData {
|
|
5
|
+
/** The bone's name; `root` for the root. */
|
|
6
|
+
readonly name: string;
|
|
7
|
+
/** Unit direction from the bone's start to its end, in the rest frame. */
|
|
8
|
+
readonly direction: readonly [number, number, number];
|
|
9
|
+
/** Length in file units. */
|
|
10
|
+
readonly length: number;
|
|
11
|
+
/** The bone's frame C in the rest frame, as a quaternion [x, y, z, w]. */
|
|
12
|
+
readonly axis: readonly [number, number, number, number];
|
|
13
|
+
/** The AMC values this bone reads, in file order: `tx` `ty` `tz` `rx` `ry` `rz`, or `l` (length, read and unused). */
|
|
14
|
+
readonly dof: readonly string[];
|
|
15
|
+
}
|
|
16
|
+
/** What {@link parseASF} returns: the skeleton {@link parseAMC} plays a motion file on. */
|
|
17
|
+
export interface ASFSkeleton {
|
|
18
|
+
/** The skeleton name the file gives (`:name`), or ''. */
|
|
19
|
+
readonly name: string;
|
|
20
|
+
/**
|
|
21
|
+
* The joints, parents first, in `parseBVH`'s shape: `root`, then one joint per bone at the bone's start (its parent
|
|
22
|
+
* bone's end), resting unrotated. A bone with no children carries its own end as `endSite`.
|
|
23
|
+
*/
|
|
24
|
+
readonly bones: BVHBone[];
|
|
25
|
+
/** The `:units length` value the file gives (CMU files: 0.45 for inches, so metres are `0.0254 / 0.45` times file units). */
|
|
26
|
+
readonly lengthUnit: number;
|
|
27
|
+
/** Angles in degrees (`:units angle deg`, the default) or radians. */
|
|
28
|
+
readonly angle: 'deg' | 'rad';
|
|
29
|
+
/** Per-joint data in the order of `bones`. */
|
|
30
|
+
readonly data: readonly ASFBoneData[];
|
|
31
|
+
/** The multiplier `parseASF` applied to lengths, which `parseAMC` applies to root translations. */
|
|
32
|
+
readonly scale: number;
|
|
33
|
+
}
|
|
34
|
+
/** Options of {@link parseASF}. */
|
|
35
|
+
export interface ASFOptions {
|
|
36
|
+
/** Multiplies lengths and root positions. Default 1 (file units); CMU files in metres: `0.0254 / 0.45`. */
|
|
37
|
+
scale?: number;
|
|
38
|
+
/** Most bones. Default 4,096. */
|
|
39
|
+
maxJoints?: number;
|
|
40
|
+
}
|
|
41
|
+
/** Options of {@link parseAMC}. */
|
|
42
|
+
export interface AMCOptions {
|
|
43
|
+
/** Seconds per frame; AMC files do not store it. Default 1/120 (most CMU trials; the database lists 60 for some). */
|
|
44
|
+
frameTime?: number;
|
|
45
|
+
/** The clip's name. Default `'mocap'`. */
|
|
46
|
+
name?: string;
|
|
47
|
+
/** Most keys, frames times tracks. Default 4,000,000. */
|
|
48
|
+
maxKeys?: number;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Parses an Acclaim ASF skeleton (the CMU motion capture database's `.asf`). Bones become joints at each bone's start,
|
|
52
|
+
* resting unrotated with offsets in the file's world frame, so {@link parseAMC} gives tracks that the animation
|
|
53
|
+
* package plays and the rigging package's `cmu` retarget preset maps. Joint limits, ids and masses are read past.
|
|
54
|
+
*
|
|
55
|
+
* @param input The file's bytes or text.
|
|
56
|
+
* @param options Scale and the joint limit.
|
|
57
|
+
* @returns The skeleton to pass to {@link parseAMC}.
|
|
58
|
+
* @throws LoaderError with format `ASF` and code `header`, `syntax`, `range`, `truncated` or `limit`.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* import { parseASF } from '@margoengine/loaders/mocap';
|
|
63
|
+
*
|
|
64
|
+
* 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');
|
|
65
|
+
* console.log(skeleton.bones.map((b) => b.name), skeleton.bones[1].endSite); // [ 'root', 'spine' ] [ 0, 2, 0 ]
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare function parseASF(input: BinaryInput | string, options?: ASFOptions): ASFSkeleton;
|
|
69
|
+
/**
|
|
70
|
+
* Parses an Acclaim AMC motion (the CMU database's `.amc`) for the skeleton {@link parseASF} read. Each frame turns
|
|
71
|
+
* every bone that has rotation degrees of freedom by its Euler angles in the bone's own frame (each listed axis applied
|
|
72
|
+
* in turn, first listed first) and moves the root from its rest position by its translation channels. Every bone with
|
|
73
|
+
* degrees of freedom must have a line in every frame.
|
|
74
|
+
*
|
|
75
|
+
* @param input The file's bytes or text.
|
|
76
|
+
* @param skeleton What {@link parseASF} returned for the trial's subject.
|
|
77
|
+
* @param options Frame time, clip name and the key limit.
|
|
78
|
+
* @returns The skeleton's bones, a clip in `parseBVH`'s shape (for `createKeyframeClip`) and the frame timing.
|
|
79
|
+
* @throws LoaderError with format `AMC` and code `header`, `syntax`, `range` or `limit`.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* import { parseAMC, parseASF } from '@margoengine/loaders/mocap';
|
|
84
|
+
*
|
|
85
|
+
* 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');
|
|
86
|
+
* const { clip, frames } = parseAMC(':FULLY-SPECIFIED\n:DEGREES\n1\nroot 0 1 0 0 0 0\nspine 90\n', skeleton);
|
|
87
|
+
* console.log(frames, clip.tracks.map((t) => `${t.target} ${t.path}`)); // 1 [ 'root translation', 'root rotation', 'spine rotation' ]
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export declare function parseAMC(input: BinaryInput | string, skeleton: ASFSkeleton, options?: AMCOptions): BVH;
|