@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/dae-pose.js
ADDED
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
import { splitTRS } from '@margoengine/math/quat';
|
|
2
|
+
import { LoaderError } from './types.js';
|
|
3
|
+
import { computeVertexNormals } from './normals.js';
|
|
4
|
+
import { cofactors, converter, F, IDENTITY, invert, multiply, stackMatrix, transformInto, } from './dae-common.js';
|
|
5
|
+
import { animateTransforms, sampleDAESampler } from './dae-animation.js';
|
|
6
|
+
import { positionCount } from './dae-controllers.js';
|
|
7
|
+
function selection(dae, clip) {
|
|
8
|
+
let included = () => true, start = 0, end = Infinity, name = '';
|
|
9
|
+
if (clip !== undefined) {
|
|
10
|
+
const found = Number.isInteger(clip) ? dae.clips[clip] : undefined;
|
|
11
|
+
if (!found)
|
|
12
|
+
throw new RangeError(`DAE clip ${clip} does not exist; pass an index below ${dae.clips.length}`);
|
|
13
|
+
// Parents come before their nested animations, so one pass marks every descendant.
|
|
14
|
+
const marked = dae.animations.map(() => false);
|
|
15
|
+
dae.animations.forEach((a, i) => {
|
|
16
|
+
marked[i] = found.animations.includes(i) || (a.parent >= 0 && marked[a.parent]);
|
|
17
|
+
});
|
|
18
|
+
included = (index) => marked[index];
|
|
19
|
+
start = found.start;
|
|
20
|
+
end = found.end ?? Infinity;
|
|
21
|
+
name = found.name ?? found.id ?? '';
|
|
22
|
+
}
|
|
23
|
+
const channels = [];
|
|
24
|
+
dae.animations.forEach((a, i) => included(i) && channels.push(...a.channels));
|
|
25
|
+
const byNode = new Map();
|
|
26
|
+
for (const channel of channels)
|
|
27
|
+
if (channel.node !== undefined) {
|
|
28
|
+
const list = byNode.get(channel.node);
|
|
29
|
+
if (list)
|
|
30
|
+
list.push(channel);
|
|
31
|
+
else
|
|
32
|
+
byNode.set(channel.node, [channel]);
|
|
33
|
+
}
|
|
34
|
+
return { channels, byNode, start, end, name };
|
|
35
|
+
}
|
|
36
|
+
function weightsAt(dae, channels, time) {
|
|
37
|
+
const weights = dae.morphs.map((m) => [...m.weights]);
|
|
38
|
+
for (const channel of channels) {
|
|
39
|
+
if (channel.morph === undefined)
|
|
40
|
+
continue;
|
|
41
|
+
const value = sampleDAESampler(channel.sampler, time), target = weights[channel.morph];
|
|
42
|
+
if (channel.index === undefined)
|
|
43
|
+
for (let k = 0; k < target.length; k++)
|
|
44
|
+
target[k] = value[k];
|
|
45
|
+
else
|
|
46
|
+
target[channel.index] = value[0];
|
|
47
|
+
}
|
|
48
|
+
return weights;
|
|
49
|
+
}
|
|
50
|
+
const finiteTime = (time) => {
|
|
51
|
+
if (typeof time !== 'number' || !Number.isFinite(time))
|
|
52
|
+
throw new RangeError(`DAE pose time ${time} is invalid; pass finite seconds`);
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Every node's local and world matrix and every morph's weights at `time` seconds, from the document's animation
|
|
56
|
+
* channels (or one clip's). Channels replace values of the node's transform stack as written, which is then converted
|
|
57
|
+
* to Y-up meters like `node.matrix`.
|
|
58
|
+
*/
|
|
59
|
+
export function poseDAE(dae, time, options = {}) {
|
|
60
|
+
finiteTime(time);
|
|
61
|
+
const { channels, byNode, start, end } = selection(dae, options.clip), at = options.clip === undefined ? time : Math.min(start + Math.max(0, time), end), convert = converter(dae.upAxis, dae.meter), local = new Map(), world = new Map();
|
|
62
|
+
const walk = (node, parent) => {
|
|
63
|
+
const animated = node.id === undefined ? undefined : byNode.get(node.id), matrix = animated ? convert(stackMatrix(animateTransforms(node.transforms, animated, at))) : node.matrix, w = multiply(parent, matrix);
|
|
64
|
+
local.set(node, matrix);
|
|
65
|
+
world.set(node, w);
|
|
66
|
+
for (const child of node.children)
|
|
67
|
+
walk(child, w);
|
|
68
|
+
};
|
|
69
|
+
for (const node of dae.nodes)
|
|
70
|
+
walk(node, IDENTITY);
|
|
71
|
+
return { local, world, weights: weightsAt(dae, channels, at) };
|
|
72
|
+
}
|
|
73
|
+
/** The scene at rest: `node.matrix` everywhere and the morphs' default weights. */
|
|
74
|
+
export function restPose(dae) {
|
|
75
|
+
const local = new Map(), world = new Map();
|
|
76
|
+
const walk = (node, parent) => {
|
|
77
|
+
const w = multiply(parent, node.matrix);
|
|
78
|
+
local.set(node, node.matrix);
|
|
79
|
+
world.set(node, w);
|
|
80
|
+
for (const child of node.children)
|
|
81
|
+
walk(child, w);
|
|
82
|
+
};
|
|
83
|
+
for (const node of dae.nodes)
|
|
84
|
+
walk(node, IDENTITY);
|
|
85
|
+
return { local, world, weights: dae.morphs.map((m) => [...m.weights]) };
|
|
86
|
+
}
|
|
87
|
+
function withNormals(geometry, positions, normals) {
|
|
88
|
+
return {
|
|
89
|
+
...geometry,
|
|
90
|
+
positions,
|
|
91
|
+
normals: normals ?? computeVertexNormals(positions, geometry.indices),
|
|
92
|
+
groups: geometry.groups.map((group, materialIndex) => ({ ...group, materialIndex })),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* A copy of a morph's base mesh with its targets blended at `weights` (default: the morph's own weights), position
|
|
97
|
+
* by position: `NORMALIZED` (1 - sum w) base + sum w target, `RELATIVE` base + sum w target. Normals blend the same
|
|
98
|
+
* way and are renormalised (computed when the base has none). Vertex order is the base mesh's, so the result can
|
|
99
|
+
* replace a mesh's geometry every frame.
|
|
100
|
+
*/
|
|
101
|
+
export function morphDAE(dae, morph, weights) {
|
|
102
|
+
const m = Number.isInteger(morph) ? dae.morphs[morph] : undefined;
|
|
103
|
+
if (!m)
|
|
104
|
+
throw new RangeError(`DAE morph ${morph} does not exist; pass an index below ${dae.morphs.length}`);
|
|
105
|
+
const w = weights ?? m.weights;
|
|
106
|
+
if (w.length !== m.targets.length)
|
|
107
|
+
throw new RangeError(`DAE morph "${m.id}" has ${m.targets.length} targets; pass one weight each`);
|
|
108
|
+
for (let i = 0; i < w.length; i++)
|
|
109
|
+
if (typeof w[i] !== 'number' || !Number.isFinite(w[i]))
|
|
110
|
+
throw new RangeError(`DAE morph "${m.id}" weight ${i} is not finite; pass finite numbers`);
|
|
111
|
+
const base = dae.geometries[m.base], count = base.positions.length / 3, positions = Float32Array.from(base.positions), normals = base.normals && Float32Array.from(base.normals), relative = m.method === 'RELATIVE';
|
|
112
|
+
m.targets.forEach((t, i) => {
|
|
113
|
+
const weight = w[i];
|
|
114
|
+
if (!weight)
|
|
115
|
+
return;
|
|
116
|
+
const target = dae.geometries[t],
|
|
117
|
+
// The first corner of each target position.
|
|
118
|
+
corner = new Int32Array(positionCount(base.positionIndices)).fill(-1);
|
|
119
|
+
target.positionIndices.forEach((p, c) => {
|
|
120
|
+
if (p < corner.length && corner[p] < 0)
|
|
121
|
+
corner[p] = c;
|
|
122
|
+
});
|
|
123
|
+
for (let v = 0; v < count; v++) {
|
|
124
|
+
const c = corner[base.positionIndices[v]];
|
|
125
|
+
for (let k = 0; k < 3; k++) {
|
|
126
|
+
const b = base.positions[v * 3 + k], p = c < 0 ? (relative ? 0 : b) : target.positions[c * 3 + k];
|
|
127
|
+
positions[v * 3 + k] += weight * (relative ? p : p - b);
|
|
128
|
+
if (!normals || !base.normals)
|
|
129
|
+
continue;
|
|
130
|
+
const bn = base.normals[v * 3 + k], n = c < 0 || !target.normals ? (relative ? 0 : bn) : target.normals[c * 3 + k];
|
|
131
|
+
normals[v * 3 + k] += weight * (relative ? n : n - bn);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
if (normals)
|
|
136
|
+
for (let v = 0; v < normals.length; v += 3) {
|
|
137
|
+
const length = Math.hypot(normals[v], normals[v + 1], normals[v + 2]) || 1;
|
|
138
|
+
normals[v] /= length;
|
|
139
|
+
normals[v + 1] /= length;
|
|
140
|
+
normals[v + 2] /= length;
|
|
141
|
+
}
|
|
142
|
+
return withNormals(base, positions, normals);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* @internal An instance's mesh in world space at a pose: morphed by the pose's weights, then skinned by its joints'
|
|
146
|
+
* world matrices (sum w joint * inverseBind * bindShape) or moved by `world`. `flip` is set when the transform
|
|
147
|
+
* mirrors, so the caller can keep outward winding.
|
|
148
|
+
*/
|
|
149
|
+
export function deformInstance(dae, instance, world, pose) {
|
|
150
|
+
const geometry = dae.geometries[instance.geometry];
|
|
151
|
+
if (!geometry)
|
|
152
|
+
throw new LoaderError(F, 'range', `instance names geometry ${instance.geometry}, which does not exist`);
|
|
153
|
+
const source = instance.morph !== undefined && dae.morphs[instance.morph]
|
|
154
|
+
? morphDAE(dae, instance.morph, pose.weights[instance.morph])
|
|
155
|
+
: geometry, count = source.positions.length / 3, positions = new Float32Array(count * 3), normals = source.normals && new Float32Array(count * 3), skin = instance.skin === undefined ? undefined : dae.skins[instance.skin];
|
|
156
|
+
if (!skin || !instance.joints) {
|
|
157
|
+
const co = cofactors(world);
|
|
158
|
+
for (let v = 0; v < count; v++)
|
|
159
|
+
transformInto(world, source.positions, source.normals, v * 3, positions, normals, v * 3, co);
|
|
160
|
+
return { positions, normals, flip: co.determinant < 0 };
|
|
161
|
+
}
|
|
162
|
+
const joints = instance.joints.map((joint, j) => multiply(multiply(pose.world.get(joint) ?? joint.matrix, skin.inverseBindMatrices.subarray(j * 16, j * 16 + 16)), skin.bindShapeMatrix)), m = new Array(16);
|
|
163
|
+
let flip = false;
|
|
164
|
+
for (let v = 0; v < count; v++) {
|
|
165
|
+
const p = source.positionIndices[v];
|
|
166
|
+
m.fill(0);
|
|
167
|
+
let sum = 0;
|
|
168
|
+
for (let k = 0; k < 4; k++) {
|
|
169
|
+
const w = skin.vertexWeights[p * 4 + k];
|
|
170
|
+
if (!w)
|
|
171
|
+
continue;
|
|
172
|
+
const j = skin.vertexJoints[p * 4 + k], matrix = j < 0 ? skin.bindShapeMatrix : joints[j];
|
|
173
|
+
for (let e = 0; e < 16; e++)
|
|
174
|
+
m[e] += w * matrix[e];
|
|
175
|
+
sum += w;
|
|
176
|
+
}
|
|
177
|
+
// A position without weights stays in the bind shape.
|
|
178
|
+
if (!sum)
|
|
179
|
+
for (let e = 0; e < 16; e++)
|
|
180
|
+
m[e] = skin.bindShapeMatrix[e];
|
|
181
|
+
const co = cofactors(m);
|
|
182
|
+
if (!v)
|
|
183
|
+
flip = co.determinant < 0;
|
|
184
|
+
transformInto(m, source.positions, source.normals, v * 3, positions, normals, v * 3, co);
|
|
185
|
+
}
|
|
186
|
+
return { positions, normals, flip };
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* A unique name for every node, depth first: its id, else its name, else `node`, with `#2`, `#3`... added to repeats
|
|
190
|
+
* (copies made by `instance_node`). Bones of {@link bindDAESkin} and tracks of {@link daeKeyframeClip} use these.
|
|
191
|
+
*/
|
|
192
|
+
export function daeNodeNames(dae) {
|
|
193
|
+
const names = new Map(), used = new Set();
|
|
194
|
+
const walk = (node) => {
|
|
195
|
+
const base = node.id ?? node.name ?? 'node';
|
|
196
|
+
let name = base;
|
|
197
|
+
for (let n = 2; used.has(name); n++)
|
|
198
|
+
name = `${base}#${n}`;
|
|
199
|
+
used.add(name);
|
|
200
|
+
names.set(node, name);
|
|
201
|
+
node.children.forEach(walk);
|
|
202
|
+
};
|
|
203
|
+
dae.nodes.forEach(walk);
|
|
204
|
+
return names;
|
|
205
|
+
}
|
|
206
|
+
function trs(m) {
|
|
207
|
+
const split = splitTRS(m, Infinity);
|
|
208
|
+
if (split.ok)
|
|
209
|
+
return { translation: split.position, rotation: split.rotation, scale: split.scale };
|
|
210
|
+
return { translation: [m[12], m[13], m[14]], rotation: [0, 0, 0, 1], scale: [0, 0, 0] };
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Skeleton and skin data of a skinned instance (default: the first one in the scene) for the animation package's
|
|
214
|
+
* `Skeleton` and `Skin`. Throws a `LoaderError` (`unsupported`) when the document has no skinned instance.
|
|
215
|
+
*/
|
|
216
|
+
export function bindDAESkin(dae, instance, options = {}) {
|
|
217
|
+
const parents = new Map(), order = [];
|
|
218
|
+
let owner;
|
|
219
|
+
const walk = (node, parent) => {
|
|
220
|
+
parents.set(node, parent);
|
|
221
|
+
order.push(node);
|
|
222
|
+
for (const i of node.instances)
|
|
223
|
+
if (!owner && (instance ? i === instance : i.skin !== undefined && i.joints)) {
|
|
224
|
+
owner = node;
|
|
225
|
+
instance ??= i;
|
|
226
|
+
}
|
|
227
|
+
for (const child of node.children)
|
|
228
|
+
walk(child, node);
|
|
229
|
+
};
|
|
230
|
+
for (const node of dae.nodes)
|
|
231
|
+
walk(node, undefined);
|
|
232
|
+
if (!instance && !owner)
|
|
233
|
+
throw new LoaderError(F, 'unsupported', 'the document has no skinned instance');
|
|
234
|
+
const skin = instance?.skin === undefined ? undefined : dae.skins[instance.skin];
|
|
235
|
+
if (!owner || !instance || !skin || !instance.joints)
|
|
236
|
+
throw new TypeError('bindDAESkin needs a skinned instance of this document; pass one whose skin and joints are set');
|
|
237
|
+
const joints = instance.joints, mesh = dae.geometries[instance.geometry], source = instance.morph !== undefined
|
|
238
|
+
? morphDAE(dae, instance.morph, options.weights)
|
|
239
|
+
: withNormals(mesh, mesh.positions, mesh.normals);
|
|
240
|
+
// The bind shape, applied once.
|
|
241
|
+
const count = source.positions.length / 3, positions = new Float32Array(count * 3), normals = new Float32Array(count * 3), co = cofactors(skin.bindShapeMatrix);
|
|
242
|
+
for (let v = 0; v < count; v++)
|
|
243
|
+
transformInto(skin.bindShapeMatrix, source.positions, source.normals, v * 3, positions, normals, v * 3, co);
|
|
244
|
+
let indices = source.indices;
|
|
245
|
+
if (co.determinant < 0) {
|
|
246
|
+
indices = Uint32Array.from(source.indices);
|
|
247
|
+
for (let i = 0; i < indices.length; i += 3)
|
|
248
|
+
[indices[i + 1], indices[i + 2]] = [indices[i + 2], indices[i + 1]];
|
|
249
|
+
}
|
|
250
|
+
// Bones: the joints and every ancestor, in scene order.
|
|
251
|
+
const included = new Set();
|
|
252
|
+
for (const joint of joints)
|
|
253
|
+
for (let n = joint; n && !included.has(n); n = parents.get(n))
|
|
254
|
+
included.add(n);
|
|
255
|
+
const nodes = order.filter((n) => included.has(n)), roots = nodes.filter((n) => !parents.get(n)).length;
|
|
256
|
+
let unweighted = false;
|
|
257
|
+
for (let v = 0; v < count && !unweighted; v++) {
|
|
258
|
+
const p = source.positionIndices[v];
|
|
259
|
+
let sum = 0;
|
|
260
|
+
for (let k = 0; k < 4; k++)
|
|
261
|
+
if (skin.vertexJoints[p * 4 + k] >= 0)
|
|
262
|
+
sum += skin.vertexWeights[p * 4 + k];
|
|
263
|
+
unweighted = !(sum > 0.999999);
|
|
264
|
+
}
|
|
265
|
+
const extra = roots > 1 || unweighted ? 1 : 0, names = daeNodeNames(dae), taken = new Set(names.values());
|
|
266
|
+
let rootName = skin.id || 'root';
|
|
267
|
+
for (let n = 2; taken.has(rootName); n++)
|
|
268
|
+
rootName = `${skin.id || 'root'}#${n}`;
|
|
269
|
+
const bones = extra
|
|
270
|
+
? [{ name: rootName, parent: -1, translation: [0, 0, 0], rotation: [0, 0, 0, 1], scale: [1, 1, 1] }]
|
|
271
|
+
: [];
|
|
272
|
+
const boneOf = new Map(), worlds = new Map();
|
|
273
|
+
for (const node of nodes) {
|
|
274
|
+
const parent = parents.get(node), world = multiply(parent ? (worlds.get(parent) ?? IDENTITY) : IDENTITY, node.matrix);
|
|
275
|
+
worlds.set(node, world);
|
|
276
|
+
boneOf.set(node, bones.length);
|
|
277
|
+
bones.push({
|
|
278
|
+
name: names.get(node),
|
|
279
|
+
parent: parent && boneOf.has(parent) ? boneOf.get(parent) : extra ? 0 : -1,
|
|
280
|
+
...trs(node.matrix),
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
const inverseBindMatrices = new Float64Array(bones.length * 16), bound = new Set();
|
|
284
|
+
if (extra)
|
|
285
|
+
inverseBindMatrices.set(IDENTITY, 0);
|
|
286
|
+
joints.forEach((joint, j) => {
|
|
287
|
+
const bone = boneOf.get(joint);
|
|
288
|
+
if (bound.has(bone))
|
|
289
|
+
return;
|
|
290
|
+
bound.add(bone);
|
|
291
|
+
inverseBindMatrices.set(skin.inverseBindMatrices.subarray(j * 16, j * 16 + 16), bone * 16);
|
|
292
|
+
});
|
|
293
|
+
for (const node of nodes) {
|
|
294
|
+
const bone = boneOf.get(node);
|
|
295
|
+
if (!bound.has(bone))
|
|
296
|
+
inverseBindMatrices.set(invert(worlds.get(node)) ?? IDENTITY, bone * 16);
|
|
297
|
+
}
|
|
298
|
+
const vertexJoints = new Uint32Array(count * 4), vertexWeights = new Float32Array(count * 4);
|
|
299
|
+
for (let v = 0; v < count; v++) {
|
|
300
|
+
const p = source.positionIndices[v];
|
|
301
|
+
for (let k = 0; k < 4; k++) {
|
|
302
|
+
const j = skin.vertexJoints[p * 4 + k], w = skin.vertexWeights[p * 4 + k];
|
|
303
|
+
vertexJoints[v * 4 + k] = w && j >= 0 ? boneOf.get(joints[j]) : 0;
|
|
304
|
+
vertexWeights[v * 4 + k] = w;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return {
|
|
308
|
+
node: owner,
|
|
309
|
+
instance,
|
|
310
|
+
geometry: { ...source, positions, normals, indices },
|
|
311
|
+
bones,
|
|
312
|
+
inverseBindMatrices,
|
|
313
|
+
joints: vertexJoints,
|
|
314
|
+
weights: vertexWeights,
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* The document's animation (or one clip) as keyframe tracks: translation, rotation and scale for every animated node
|
|
319
|
+
* and weights for every node whose morph is animated. COLLADA channels address single transform elements, so each
|
|
320
|
+
* node is sampled at every key time of its channels and every `1 / fps` second between them, then split into TRS
|
|
321
|
+
* (exact at those times, linear between); a node whose channels all STEP keeps only its keys, held (`discrete`).
|
|
322
|
+
*/
|
|
323
|
+
export function daeKeyframeClip(dae, options = {}) {
|
|
324
|
+
const fps = options.fps ?? 30;
|
|
325
|
+
if (typeof fps !== 'number' || !Number.isFinite(fps) || fps <= 0)
|
|
326
|
+
throw new RangeError(`DAE keyframe fps ${fps} is invalid; pass samples per second > 0`);
|
|
327
|
+
const selected = selection(dae, options.clip), keyTimes = (channels) => channels.flatMap((c) => [...c.sampler.input]), all = keyTimes(selected.channels), first = all.reduce((a, b) => Math.min(a, b), 0), start = options.clip === undefined ? first : selected.start, end = Math.max(start, Math.min(selected.end, all.reduce((a, b) => Math.max(a, b), start)));
|
|
328
|
+
if ((end - start) * fps > 1e6)
|
|
329
|
+
throw new RangeError(`DAE keyframe clip of ${end - start} s at ${fps} fps exceeds 1,000,000 samples; lower fps`);
|
|
330
|
+
const timesFor = (channels, step) => {
|
|
331
|
+
const times = [start, end, ...keyTimes(channels)].filter((t) => t >= start && t <= end);
|
|
332
|
+
if (!step)
|
|
333
|
+
for (let k = 0; start + k / fps < end; k++)
|
|
334
|
+
times.push(start + k / fps);
|
|
335
|
+
times.sort((a, b) => a - b);
|
|
336
|
+
return times.filter((t, i) => !i || t - times[i - 1] > 1e-9);
|
|
337
|
+
};
|
|
338
|
+
const convert = converter(dae.upAxis, dae.meter), names = daeNodeNames(dae), tracks = [], weighted = new Set(), morphChannels = selected.channels.filter((c) => c.morph !== undefined);
|
|
339
|
+
const walk = (node) => {
|
|
340
|
+
const channels = node.id === undefined ? undefined : selected.byNode.get(node.id), target = names.get(node);
|
|
341
|
+
if (channels) {
|
|
342
|
+
const step = channels.every((c) => c.sampler.interpolations.every((i) => i === 'STEP')), interpolation = step ? 'discrete' : 'linear', translation = [], rotation = [], scale = [];
|
|
343
|
+
let previous;
|
|
344
|
+
for (const t of timesFor(channels, step)) {
|
|
345
|
+
const split = trs(convert(stackMatrix(animateTransforms(node.transforms, channels, t)))), q = split.rotation;
|
|
346
|
+
// Keep neighbouring quaternions in one hemisphere.
|
|
347
|
+
if (previous && q[0] * previous[0] + q[1] * previous[1] + q[2] * previous[2] + q[3] * previous[3] < 0)
|
|
348
|
+
for (let c = 0; c < 4; c++)
|
|
349
|
+
q[c] = -q[c];
|
|
350
|
+
previous = q;
|
|
351
|
+
const time = t - start;
|
|
352
|
+
translation.push({ time, value: split.translation, interpolation });
|
|
353
|
+
rotation.push({ time, value: q, interpolation });
|
|
354
|
+
scale.push({ time, value: split.scale, interpolation });
|
|
355
|
+
}
|
|
356
|
+
tracks.push({ target, path: 'translation', keys: translation }, { target, path: 'rotation', keys: rotation }, { target, path: 'scale', keys: scale });
|
|
357
|
+
}
|
|
358
|
+
for (const instance of node.instances) {
|
|
359
|
+
const morph = instance.morph;
|
|
360
|
+
if (morph === undefined || weighted.has(node))
|
|
361
|
+
continue;
|
|
362
|
+
const channels = morphChannels.filter((c) => c.morph === morph);
|
|
363
|
+
if (!channels.length)
|
|
364
|
+
continue;
|
|
365
|
+
weighted.add(node);
|
|
366
|
+
const step = channels.every((c) => c.sampler.interpolations.every((i) => i === 'STEP'));
|
|
367
|
+
tracks.push({
|
|
368
|
+
target,
|
|
369
|
+
path: 'weights',
|
|
370
|
+
keys: timesFor(channels, step).map((t) => ({
|
|
371
|
+
time: t - start,
|
|
372
|
+
value: weightsAt(dae, channels, t)[morph],
|
|
373
|
+
interpolation: step ? 'discrete' : 'linear',
|
|
374
|
+
})),
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
node.children.forEach(walk);
|
|
378
|
+
};
|
|
379
|
+
dae.nodes.forEach(walk);
|
|
380
|
+
return { name: options.name ?? selected.name, tracks };
|
|
381
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import type { MutableVec3 } from '@margoengine/math';
|
|
2
|
+
import type { DAEPose } from './dae-pose.js';
|
|
3
|
+
import type { DAENode, LoadedDAE } from './dae.js';
|
|
4
|
+
import type { XMLElement } from './xml.js';
|
|
5
|
+
/**
|
|
6
|
+
* An `<instance_camera>`: the `<camera>`'s `technique_common` optics on the node that instances it. The camera looks
|
|
7
|
+
* down the -Z of its frame with +Y up. Angles are degrees as written; distances and magnifications are in meters.
|
|
8
|
+
*/
|
|
9
|
+
export interface DAECamera {
|
|
10
|
+
/** The `<camera>`'s id. */
|
|
11
|
+
id?: string;
|
|
12
|
+
/** The `<camera>`'s name. */
|
|
13
|
+
name?: string;
|
|
14
|
+
/** The node holding the `<instance_camera>`. */
|
|
15
|
+
node: DAENode;
|
|
16
|
+
/** Which optics the camera has. */
|
|
17
|
+
type: 'perspective' | 'orthographic';
|
|
18
|
+
/**
|
|
19
|
+
* What the file gives: `xfov` alone, `yfov` alone, both, or one with `aspectRatio` (width over height). Full angles
|
|
20
|
+
* in degrees; `znear` and `zfar` in meters.
|
|
21
|
+
*/
|
|
22
|
+
perspective?: {
|
|
23
|
+
xfov?: number;
|
|
24
|
+
yfov?: number;
|
|
25
|
+
aspectRatio?: number;
|
|
26
|
+
znear: number;
|
|
27
|
+
zfar: number;
|
|
28
|
+
};
|
|
29
|
+
/** Half-width `xmag` and/or half-height `ymag` of the view (read as glTF's), or one with `aspectRatio`; meters. */
|
|
30
|
+
orthographic?: {
|
|
31
|
+
xmag?: number;
|
|
32
|
+
ymag?: number;
|
|
33
|
+
aspectRatio?: number;
|
|
34
|
+
znear: number;
|
|
35
|
+
zfar: number;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* The camera's frame at rest in Y-up meters, column-major: translation is the position, -Z (column 2 negated) the
|
|
39
|
+
* view direction and +Y (column 1) the up direction. A posed frame is `daeFrame(dae, camera.node, pose)`.
|
|
40
|
+
*/
|
|
41
|
+
matrix: number[];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* An `<instance_light>`: the `<light>`'s `technique_common` on the node that instances it, at rest in Y-up meters.
|
|
45
|
+
* Attenuation is `1 / (constant + linear d + quadratic d^2)` with d in meters (the file's coefficients divided by
|
|
46
|
+
* `meter` and `meter^2`).
|
|
47
|
+
*/
|
|
48
|
+
export interface DAELight {
|
|
49
|
+
/** The `<light>`'s id. */
|
|
50
|
+
id?: string;
|
|
51
|
+
/** The `<light>`'s name. */
|
|
52
|
+
name?: string;
|
|
53
|
+
/** The node holding the `<instance_light>`. */
|
|
54
|
+
node: DAENode;
|
|
55
|
+
/** The `technique_common` light element. */
|
|
56
|
+
type: 'ambient' | 'directional' | 'point' | 'spot';
|
|
57
|
+
/** Linear RGB as written (it carries the intensity, so it may exceed 1). */
|
|
58
|
+
color: MutableVec3;
|
|
59
|
+
/** Point and spot: default 1. */
|
|
60
|
+
constantAttenuation?: number;
|
|
61
|
+
/** Point and spot, per meter: default 0. */
|
|
62
|
+
linearAttenuation?: number;
|
|
63
|
+
/** Point and spot, per square meter: default 0. */
|
|
64
|
+
quadraticAttenuation?: number;
|
|
65
|
+
/** Spot: the full cone angle in degrees, default 180 (a hemisphere). */
|
|
66
|
+
falloffAngle?: number;
|
|
67
|
+
/** Spot: the exponent of cos(angle from the axis) inside the cone, default 0 (even light up to the edge). */
|
|
68
|
+
falloffExponent?: number;
|
|
69
|
+
/** World position in meters. */
|
|
70
|
+
position: MutableVec3;
|
|
71
|
+
/** Unit world direction the light travels: the frame's -Z (meaningful for directional and spot lights). */
|
|
72
|
+
direction: MutableVec3;
|
|
73
|
+
/** The light's frame at rest, Y-up meters, column-major, as {@link DAECamera.matrix}. */
|
|
74
|
+
matrix: number[];
|
|
75
|
+
}
|
|
76
|
+
/** @internal An instance found while reading the scene, resolved once the hierarchy exists. */
|
|
77
|
+
export interface DAEPlacement {
|
|
78
|
+
node: DAENode;
|
|
79
|
+
kind: 'camera' | 'light';
|
|
80
|
+
url: string;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* A node's camera or light frame in Y-up meters, column-major: the node's world matrix (from `pose`, else at rest)
|
|
84
|
+
* times the up-axis rotation, so the file's local -Z view axis and +Y up stay the frame's -Z and +Y. Throws a
|
|
85
|
+
* TypeError when the node is not in the document.
|
|
86
|
+
*/
|
|
87
|
+
export declare function daeFrame(dae: Pick<LoadedDAE, 'nodes' | 'upAxis'>, node: DAENode, pose?: DAEPose): number[];
|
|
88
|
+
/** @internal Resolves the scene's camera and light instances into data at rest. */
|
|
89
|
+
export declare function placeScene(dae: Pick<LoadedDAE, 'nodes' | 'upAxis' | 'meter'>, ids: Map<string, XMLElement>, placements: readonly DAEPlacement[], warn: (message: string) => void): {
|
|
90
|
+
cameras: DAECamera[];
|
|
91
|
+
lights: DAELight[];
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* What an engine camera takes to view through a {@link DAECamera}: `Object.assign(engine.camera, view)`. `fov` is
|
|
95
|
+
* vertical degrees (perspective only); an orthographic camera sets `project` (core's `Camera.project`), a perspective
|
|
96
|
+
* one clears it.
|
|
97
|
+
*/
|
|
98
|
+
export interface DAECameraView {
|
|
99
|
+
/** Eye position in meters. */
|
|
100
|
+
position: MutableVec3;
|
|
101
|
+
/** One meter ahead along the view, so orbit controls turn around that point. */
|
|
102
|
+
target: MutableVec3;
|
|
103
|
+
/** Unit screen-up direction. */
|
|
104
|
+
up: MutableVec3;
|
|
105
|
+
/** Vertical field of view in degrees (perspective). */
|
|
106
|
+
fov?: number;
|
|
107
|
+
/** Near clip distance in meters. */
|
|
108
|
+
near: number;
|
|
109
|
+
/** Far clip distance in meters. */
|
|
110
|
+
far: number;
|
|
111
|
+
/** Width over height when the camera fixes it (`aspect_ratio`, or both fovs or both magnifications). */
|
|
112
|
+
aspect?: number;
|
|
113
|
+
/** Orthographic projection for core's `Camera.project`; undefined for perspective (clears an earlier one). */
|
|
114
|
+
project: ((out: Float64Array, aspect: number, webgpu: boolean) => void) | undefined;
|
|
115
|
+
}
|
|
116
|
+
/** Options of {@link daeCameraView}. */
|
|
117
|
+
export interface DAECameraViewOptions {
|
|
118
|
+
/** Pose the camera's node (else at rest). */
|
|
119
|
+
pose?: DAEPose;
|
|
120
|
+
/** The viewport's width over height: needed for a perspective camera that gives only `xfov`. */
|
|
121
|
+
aspect?: number;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* How an engine camera views through `dae.cameras[camera]` (default the first), from its node's frame at rest (or at
|
|
125
|
+
* `options.pose`). Perspective follows the spec's combinations: `yfov` is the vertical angle; `xfov` with
|
|
126
|
+
* `aspect_ratio` gives yfov = 2 atan(tan(xfov / 2) / aspect); `xfov` with `yfov` fixes the aspect as
|
|
127
|
+
* tan(xfov / 2) / tan(yfov / 2) and keeps yfov; `xfov` alone takes `options.aspect`. Orthographic: `ymag` is the
|
|
128
|
+
* half-height and the width follows the viewport; `xmag` alone is the half-width (the height follows the viewport);
|
|
129
|
+
* `xmag` with `aspect_ratio` gives a half-height of xmag / aspect. Throws a RangeError when the camera does not exist
|
|
130
|
+
* or `xfov` alone has no valid `options.aspect`.
|
|
131
|
+
*/
|
|
132
|
+
export declare function daeCameraView(dae: LoadedDAE, camera?: number, options?: DAECameraViewOptions): DAECameraView;
|
|
133
|
+
/** A point or spot light in the shape of @margoengine/clustered's `PointLight` and `SpotLight`, for `setLights`. */
|
|
134
|
+
export interface DAELocalLight {
|
|
135
|
+
/** World position in meters. */
|
|
136
|
+
position: MutableVec3;
|
|
137
|
+
/** Where the light falls to 1/256 (0, unlimited, when it does not fall off). */
|
|
138
|
+
range: number;
|
|
139
|
+
/** Unit linear RGB (the brightest channel is 1). */
|
|
140
|
+
color: MutableVec3;
|
|
141
|
+
/** The brightest channel over the attenuation term kept. */
|
|
142
|
+
intensity: number;
|
|
143
|
+
/** 2 for inverse-square, 1 for linear, 0 for constant attenuation (the strongest term that is set). */
|
|
144
|
+
decay: number;
|
|
145
|
+
/** Spot: the unit direction the cone points along. */
|
|
146
|
+
direction?: MutableVec3;
|
|
147
|
+
/** Spot: the cone half-angle in radians (half the falloff angle, at most pi / 2). */
|
|
148
|
+
outerAngle?: number;
|
|
149
|
+
/** Spot: where the edge starts fading, from the falloff exponent. */
|
|
150
|
+
innerAngle?: number;
|
|
151
|
+
}
|
|
152
|
+
/** A directional light in the shape of @margoengine/clustered/directional's `DirectionalLight`. */
|
|
153
|
+
export interface DAEDirectionalLight {
|
|
154
|
+
/** Unit direction the light travels (from the light toward the scene). */
|
|
155
|
+
direction: MutableVec3;
|
|
156
|
+
/** Unit linear RGB. */
|
|
157
|
+
color: MutableVec3;
|
|
158
|
+
/** The brightest channel of the light's color. */
|
|
159
|
+
intensity: number;
|
|
160
|
+
}
|
|
161
|
+
/** The document's lights converted for the engine by {@link daeEngineLights}. */
|
|
162
|
+
export interface DAEEngineLights {
|
|
163
|
+
/**
|
|
164
|
+
* The first directional light, for core's sun: `engine.scene.lighting.direction.set([...sun.toward, sun.intensity])` and
|
|
165
|
+
* `engine.scene.lighting.radiance.set(sun.color)`.
|
|
166
|
+
*/
|
|
167
|
+
sun?: {
|
|
168
|
+
toward: MutableVec3;
|
|
169
|
+
color: MutableVec3;
|
|
170
|
+
intensity: number;
|
|
171
|
+
};
|
|
172
|
+
/**
|
|
173
|
+
* Every ambient light summed, for core's ambient: `engine.scene.lighting.ambient.set(ambient.color)` and
|
|
174
|
+
* `engine.scene.lighting.radiance[3] = ambient.intensity`.
|
|
175
|
+
*/
|
|
176
|
+
ambient?: {
|
|
177
|
+
color: MutableVec3;
|
|
178
|
+
intensity: number;
|
|
179
|
+
};
|
|
180
|
+
/** The other directional lights, for `DirectionalLights.setLights`. */
|
|
181
|
+
directional: DAEDirectionalLight[];
|
|
182
|
+
/** Point and spot lights, for `Clustered.setLights`. */
|
|
183
|
+
local: DAELocalLight[];
|
|
184
|
+
}
|
|
185
|
+
/** Options of {@link daeEngineLights}. */
|
|
186
|
+
export interface DAEEngineLightOptions {
|
|
187
|
+
/** Place the lights at this pose (else at rest). */
|
|
188
|
+
pose?: DAEPose;
|
|
189
|
+
/** Multiplies every intensity (an exposure). Default 1. */
|
|
190
|
+
intensity?: number;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* The document's lights (at rest, or at `options.pose`) in the shapes the engine takes. Colors are split into a unit
|
|
194
|
+
* color and an intensity (the brightest channel). The first directional light is core's sun, the others are for
|
|
195
|
+
* @margoengine/clustered/directional; ambient lights add up into core's ambient. Point and spot attenuation
|
|
196
|
+
* `1 / (c + l d + q d^2)` is approximated by its strongest set term: q gives inverse-square (`decay` 2,
|
|
197
|
+
* intensity / q), else l gives `decay` 1 (intensity / l), else c gives `decay` 0 (intensity / c, `range` 0). The
|
|
198
|
+
* dropped terms make the light brighter near its position than the file's, and the same far away. `range` ends where
|
|
199
|
+
* intensity / d^decay is 1/256. A spot's full `falloff_angle` becomes `outerAngle` (half of it, at most a
|
|
200
|
+
* hemisphere); its `falloff_exponent` e (cos^e fading within the cone) becomes an `innerAngle` where cos^e is 1/2, a
|
|
201
|
+
* hard edge for e = 0. Throws a RangeError for a negative or non-finite `intensity`.
|
|
202
|
+
*/
|
|
203
|
+
export declare function daeEngineLights(dae: LoadedDAE, options?: DAEEngineLightOptions): DAEEngineLights;
|