motionary 10.6.0 → 10.8.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/AGENTS.md +2 -0
- package/CHANGELOG.md +36 -0
- package/README.md +77 -4
- package/bin/motionary-mcp.mjs +160 -11
- package/dist/chunks/{anim-image-DECLkPH4.cjs → anim-image-BzTQa2k3.cjs} +2 -2
- package/dist/chunks/{anim-image-DECLkPH4.cjs.map → anim-image-BzTQa2k3.cjs.map} +1 -1
- package/dist/chunks/{anim-image-DNghy017.js → anim-image-iD646Q7i.js} +2 -2
- package/dist/chunks/{anim-image-DNghy017.js.map → anim-image-iD646Q7i.js.map} +1 -1
- package/dist/chunks/{keyframes-F6vp6tZZ.js → keyframes-B4zefz0k.js} +2 -2
- package/dist/chunks/{keyframes-F6vp6tZZ.js.map → keyframes-B4zefz0k.js.map} +1 -1
- package/dist/chunks/{keyframes-CbUZjD_5.cjs → keyframes-BlOyB1PP.cjs} +2 -2
- package/dist/chunks/{keyframes-CbUZjD_5.cjs.map → keyframes-BlOyB1PP.cjs.map} +1 -1
- package/dist/chunks/{registry-dr1CPTk1.cjs → registry-BZxDVL3h.cjs} +2 -2
- package/dist/chunks/{registry-dr1CPTk1.cjs.map → registry-BZxDVL3h.cjs.map} +1 -1
- package/dist/chunks/{registry-D8LAoXI-.js → registry-Bkt3-3ET.js} +3 -3
- package/dist/chunks/{registry-D8LAoXI-.js.map → registry-Bkt3-3ET.js.map} +1 -1
- package/dist/chunks/runtime-link-CUiZ8lbE.cjs +38 -0
- package/dist/chunks/runtime-link-CUiZ8lbE.cjs.map +1 -0
- package/dist/chunks/runtime-link-c13gskBZ.js +36 -0
- package/dist/chunks/runtime-link-c13gskBZ.js.map +1 -0
- package/dist/chunks/{ticker-DaAlEyOd.cjs → ticker-BfxAECfO.cjs} +2 -2
- package/dist/chunks/{ticker-DaAlEyOd.cjs.map → ticker-BfxAECfO.cjs.map} +1 -1
- package/dist/chunks/{ticker-Do9bGsaV.js → ticker-ZtweD3U4.js} +2 -2
- package/dist/chunks/{ticker-Do9bGsaV.js.map → ticker-ZtweD3U4.js.map} +1 -1
- package/dist/chunks/{tween-CmkdSyaC.cjs → tween--MD-39y0.cjs} +2 -2
- package/dist/chunks/{tween-CmkdSyaC.cjs.map → tween--MD-39y0.cjs.map} +1 -1
- package/dist/chunks/{tween-BzCGq_Ba.js → tween-DACdkLyO.js} +2 -2
- package/dist/chunks/{tween-BzCGq_Ba.js.map → tween-DACdkLyO.js.map} +1 -1
- package/dist/components/ai.cjs +314 -0
- package/dist/components/ai.cjs.map +1 -0
- package/dist/components/ai.d.cts +58 -0
- package/dist/components/ai.d.ts +58 -0
- package/dist/components/ai.js +311 -0
- package/dist/components/ai.js.map +1 -0
- package/dist/components/snap-carousel.cjs +216 -0
- package/dist/components/snap-carousel.cjs.map +1 -0
- package/dist/components/snap-carousel.d.cts +80 -0
- package/dist/components/snap-carousel.d.ts +80 -0
- package/dist/components/snap-carousel.js +214 -0
- package/dist/components/snap-carousel.js.map +1 -0
- package/dist/components/widgets.cjs +626 -268
- package/dist/components/widgets.cjs.map +1 -1
- package/dist/components/widgets.d.cts +515 -39
- package/dist/components/widgets.d.ts +515 -39
- package/dist/components/widgets.js +617 -262
- package/dist/components/widgets.js.map +1 -1
- package/dist/llms-full.txt +191 -6
- package/dist/llms.txt +10 -3
- package/dist/manifest.json +497 -19
- package/dist/runtime/drag-snap.cjs +269 -0
- package/dist/runtime/drag-snap.cjs.map +1 -0
- package/dist/runtime/drag-snap.d.cts +109 -0
- package/dist/runtime/drag-snap.d.ts +109 -0
- package/dist/runtime/drag-snap.iife.js +2 -0
- package/dist/runtime/drag-snap.iife.js.map +1 -0
- package/dist/runtime/drag-snap.js +261 -0
- package/dist/runtime/drag-snap.js.map +1 -0
- package/dist/runtime/format-apng.cjs +4 -4
- package/dist/runtime/format-apng.js +4 -4
- package/dist/runtime/format-css.cjs +4 -4
- package/dist/runtime/format-css.js +4 -4
- package/dist/runtime/format-gif.cjs +4 -4
- package/dist/runtime/format-gif.js +4 -4
- package/dist/runtime/format-gltf.cjs +37 -18
- package/dist/runtime/format-gltf.cjs.map +1 -1
- package/dist/runtime/format-gltf.d.cts +18 -2
- package/dist/runtime/format-gltf.d.ts +18 -2
- package/dist/runtime/format-gltf.iife.js +1 -1
- package/dist/runtime/format-gltf.iife.js.map +1 -1
- package/dist/runtime/format-gltf.js +37 -18
- package/dist/runtime/format-gltf.js.map +1 -1
- package/dist/runtime/format-motion.cjs +4 -4
- package/dist/runtime/format-motion.js +4 -4
- package/dist/runtime/format-obj.cjs +1 -1
- package/dist/runtime/format-obj.d.cts +2 -0
- package/dist/runtime/format-obj.d.ts +2 -0
- package/dist/runtime/format-obj.iife.js.map +1 -1
- package/dist/runtime/format-obj.js +1 -1
- package/dist/runtime/format-scene.cjs +241 -0
- package/dist/runtime/format-scene.cjs.map +1 -0
- package/dist/runtime/format-scene.d.cts +349 -0
- package/dist/runtime/format-scene.d.ts +349 -0
- package/dist/runtime/format-scene.iife.js +2 -0
- package/dist/runtime/format-scene.iife.js.map +1 -0
- package/dist/runtime/format-scene.js +233 -0
- package/dist/runtime/format-scene.js.map +1 -0
- package/dist/runtime/format-sprite.cjs +3 -3
- package/dist/runtime/format-sprite.js +3 -3
- package/dist/runtime/format-svg.cjs +3 -3
- package/dist/runtime/format-svg.js +3 -3
- package/dist/runtime/format-webp.cjs +4 -4
- package/dist/runtime/format-webp.js +4 -4
- package/dist/runtime/gl.cjs +17 -4
- package/dist/runtime/gl.cjs.map +1 -1
- package/dist/runtime/gl.d.cts +2 -0
- package/dist/runtime/gl.d.ts +2 -0
- package/dist/runtime/gl.iife.js +1 -1
- package/dist/runtime/gl.iife.js.map +1 -1
- package/dist/runtime/gl.js +17 -4
- package/dist/runtime/gl.js.map +1 -1
- package/dist/runtime/gltf-anim.cjs +251 -0
- package/dist/runtime/gltf-anim.cjs.map +1 -0
- package/dist/runtime/gltf-anim.d.cts +216 -0
- package/dist/runtime/gltf-anim.d.ts +216 -0
- package/dist/runtime/gltf-anim.iife.js +2 -0
- package/dist/runtime/gltf-anim.iife.js.map +1 -0
- package/dist/runtime/gltf-anim.js +243 -0
- package/dist/runtime/gltf-anim.js.map +1 -0
- package/dist/runtime/physics.cjs +739 -0
- package/dist/runtime/physics.cjs.map +1 -0
- package/dist/runtime/physics.d.cts +250 -0
- package/dist/runtime/physics.d.ts +250 -0
- package/dist/runtime/physics.iife.js +2 -0
- package/dist/runtime/physics.iife.js.map +1 -0
- package/dist/runtime/physics.js +731 -0
- package/dist/runtime/physics.js.map +1 -0
- package/dist/runtime/scroll.cjs +2 -2
- package/dist/runtime/scroll.js +2 -2
- package/dist/runtime/smooth.cjs +1 -1
- package/dist/runtime/smooth.js +1 -1
- package/dist/runtime/text.cjs +1 -1
- package/dist/runtime/text.js +1 -1
- package/dist/runtime/vector.cjs +421 -11
- package/dist/runtime/vector.cjs.map +1 -1
- package/dist/runtime/vector.d.cts +20 -4
- package/dist/runtime/vector.d.ts +20 -4
- package/dist/runtime/vector.iife.js +1 -1
- package/dist/runtime/vector.iife.js.map +1 -1
- package/dist/runtime/vector.js +421 -12
- package/dist/runtime/vector.js.map +1 -1
- package/dist/runtime.cjs +3 -3
- package/dist/runtime.d.cts +1 -1
- package/dist/runtime.d.ts +1 -1
- package/dist/runtime.iife.js +1 -1
- package/dist/runtime.iife.js.map +1 -1
- package/dist/runtime.js +5 -5
- package/dist/widgets.umd.js +1 -1
- package/dist/widgets.umd.js.map +1 -1
- package/docs/ROADMAP.md +2 -2
- package/docs/components/README.md +5 -2
- package/docs/components/usa-gl-scene.md +27 -6
- package/docs/components/usa-lottie-player.md +18 -0
- package/docs/components/usa-motion-prompt.md +32 -0
- package/docs/components/usa-physics-playground.md +63 -0
- package/docs/components/usa-snap-carousel.md +68 -0
- package/docs/components.md +95 -3
- package/docs/runtime/drag-snap.md +60 -0
- package/docs/runtime/format-gltf.md +3 -2
- package/docs/runtime/format-scene.md +59 -0
- package/docs/runtime/gltf-anim.md +62 -0
- package/docs/runtime/physics.md +61 -0
- package/docs/runtime/vector.md +3 -2
- package/package.json +61 -1
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var registry = require('../chunks/registry-BZxDVL3h.cjs');
|
|
4
|
+
var runtime_gl = require('./gl.cjs');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `motionary/runtime/gltf-anim` (10.8) — glTF 2.0 animation, skinning and
|
|
8
|
+
* morph targets for models loaded by `motionary/runtime/format-gltf`
|
|
9
|
+
* (own implementation; requires `use(gl, formatGltf, gltfAnim)`).
|
|
10
|
+
*
|
|
11
|
+
* - animation channels on node `translation` / `rotation` / `scale` /
|
|
12
|
+
* `weights` with `LINEAR` (quaternions slerped), `STEP` and
|
|
13
|
+
* `CUBICSPLINE` (Hermite with in / out tangents) samplers;
|
|
14
|
+
* - morph targets (POSITION + NORMAL deltas) with mesh default weights,
|
|
15
|
+
* node weights and animated weights;
|
|
16
|
+
* - skins: joint hierarchy, inverse bind matrices, 4 joints / vertex
|
|
17
|
+
* (JOINTS_0 / WEIGHTS_0), skinned on the CPU and re-uploaded each frame
|
|
18
|
+
* (`geometry.version`), so it works with every runtime/gl material;
|
|
19
|
+
* - `gltfAnimator(model)` plays a clip (by name or index) with loop / speed
|
|
20
|
+
* / seek, `update(dt)` from your loop or `play()` on the shared ticker.
|
|
21
|
+
*
|
|
22
|
+
* Sampling and deformation are pure (SSR / workers / tests).
|
|
23
|
+
*/
|
|
24
|
+
const model = (root) => {
|
|
25
|
+
const x = root.extras;
|
|
26
|
+
if (!x?.gltf || !x.read)
|
|
27
|
+
throw new Error('[motionary] gltf-anim: pass the node returned by loadGltf() / gltfToNode() (motionary/runtime/format-gltf 10.8+)');
|
|
28
|
+
return x;
|
|
29
|
+
};
|
|
30
|
+
/** The animation clips of a loaded model. */
|
|
31
|
+
function gltfClips(root) {
|
|
32
|
+
const { gltf, nodes, read } = model(root);
|
|
33
|
+
return (gltf.animations || []).map((a, ai) => {
|
|
34
|
+
let duration = 0;
|
|
35
|
+
const channels = [];
|
|
36
|
+
for (const c of a.channels || []) {
|
|
37
|
+
const node = nodes[c.target?.node];
|
|
38
|
+
const s = a.samplers?.[c.sampler];
|
|
39
|
+
if (!node || !s || !['translation', 'rotation', 'scale', 'weights'].includes(c.target.path))
|
|
40
|
+
continue; // KHR_animation_pointer etc.
|
|
41
|
+
const times = read(s.input), values = read(s.output);
|
|
42
|
+
const interpolation = s.interpolation || 'LINEAR';
|
|
43
|
+
const size = values.length / times.length / (interpolation === 'CUBICSPLINE' ? 3 : 1);
|
|
44
|
+
duration = Math.max(duration, times[times.length - 1] || 0);
|
|
45
|
+
channels.push({ node, path: c.target.path, interpolation, times, values, size });
|
|
46
|
+
}
|
|
47
|
+
return { name: a.name || `animation${ai}`, duration, channels };
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/** Value of a channel at time `t` (seconds; clamped to the key range). */
|
|
51
|
+
function sampleChannel(ch, t) {
|
|
52
|
+
const { times: T, values: V, size: n } = ch;
|
|
53
|
+
const cubic = ch.interpolation === 'CUBICSPLINE';
|
|
54
|
+
const at = (k) => Array.from(V.subarray((cubic ? k * 3 + 1 : k) * n, (cubic ? k * 3 + 1 : k) * n + n));
|
|
55
|
+
const last = T.length - 1;
|
|
56
|
+
if (t <= T[0] || last === 0)
|
|
57
|
+
return at(0);
|
|
58
|
+
if (t >= T[last])
|
|
59
|
+
return at(last);
|
|
60
|
+
let k = 0;
|
|
61
|
+
while (k < last - 1 && T[k + 1] <= t)
|
|
62
|
+
k++;
|
|
63
|
+
const td = T[k + 1] - T[k], s = (t - T[k]) / td;
|
|
64
|
+
let out;
|
|
65
|
+
if (ch.interpolation === 'STEP')
|
|
66
|
+
return at(k);
|
|
67
|
+
if (cubic) {
|
|
68
|
+
const s2 = s * s, s3 = s2 * s;
|
|
69
|
+
const h00 = 2 * s3 - 3 * s2 + 1, h10 = s3 - 2 * s2 + s, h01 = -2 * s3 + 3 * s2, h11 = s3 - s2;
|
|
70
|
+
const p0 = k * 3 * n, p1 = (k + 1) * 3 * n;
|
|
71
|
+
out = Array.from({ length: n }, (_, i) => h00 * V[p0 + n + i] + h10 * td * V[p0 + 2 * n + i] + h01 * V[p1 + n + i] + h11 * td * V[p1 + i]);
|
|
72
|
+
if (ch.path === 'rotation') {
|
|
73
|
+
const l = Math.hypot(...out) || 1;
|
|
74
|
+
out = out.map((v) => v / l);
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
const a = at(k), b = at(k + 1);
|
|
79
|
+
if (ch.path === 'rotation')
|
|
80
|
+
return runtime_gl.quatSlerp(a, b, s);
|
|
81
|
+
return a.map((v, i) => v + (b[i] - v) * s);
|
|
82
|
+
}
|
|
83
|
+
/** Pose the model at time `t` of a clip (sets node TRS and morph weights). */
|
|
84
|
+
function applyClip(clip, t) {
|
|
85
|
+
for (const ch of clip.channels) {
|
|
86
|
+
const v = sampleChannel(ch, t);
|
|
87
|
+
const n = ch.node;
|
|
88
|
+
if (ch.path === 'weights')
|
|
89
|
+
n.extras.weights = v;
|
|
90
|
+
else {
|
|
91
|
+
n.matrix = null; // animated nodes are TRS (glTF spec)
|
|
92
|
+
if (ch.path === 'translation')
|
|
93
|
+
n.position = v;
|
|
94
|
+
else if (ch.path === 'rotation')
|
|
95
|
+
n.rotation = v;
|
|
96
|
+
else
|
|
97
|
+
n.scale = v;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const meshesOf = (n) => (n.mesh ? (Array.isArray(n.mesh) ? n.mesh : [n.mesh]) : []);
|
|
102
|
+
/**
|
|
103
|
+
* Apply morph weights and skins to every deformable mesh of the model
|
|
104
|
+
* (call after posing; updates world matrices). Returns how many meshes changed.
|
|
105
|
+
*/
|
|
106
|
+
function deformModel(root) {
|
|
107
|
+
var _a;
|
|
108
|
+
const { gltf, nodes, read } = model(root);
|
|
109
|
+
root.updateWorld();
|
|
110
|
+
let changed = 0;
|
|
111
|
+
const cache = ((_a = root.extras).skinCache || (_a.skinCache = {}));
|
|
112
|
+
for (const node of nodes) {
|
|
113
|
+
const ms = meshesOf(node);
|
|
114
|
+
if (!ms.length)
|
|
115
|
+
continue;
|
|
116
|
+
const si = node.extras.skin;
|
|
117
|
+
let jm = null;
|
|
118
|
+
if (si !== undefined && gltf.skins?.[si]) {
|
|
119
|
+
const sk = gltf.skins[si];
|
|
120
|
+
const c = (cache[si] || (cache[si] = {
|
|
121
|
+
joints: sk.joints.map((j) => nodes[j]),
|
|
122
|
+
ibm: sk.joints.map((_, i) => (sk.inverseBindMatrices !== undefined ? new Float32Array(read(sk.inverseBindMatrices).subarray(i * 16, i * 16 + 16)) : runtime_gl.mat4.identity())),
|
|
123
|
+
}));
|
|
124
|
+
const inv = runtime_gl.mat4.invert(node.world) || runtime_gl.mat4.identity();
|
|
125
|
+
jm = c.joints.map((j, i) => runtime_gl.mat4.multiply(inv, runtime_gl.mat4.multiply(j.world, c.ibm[i])));
|
|
126
|
+
}
|
|
127
|
+
const w = node.extras.weights || [];
|
|
128
|
+
for (const m of ms) {
|
|
129
|
+
const g = m.geometry;
|
|
130
|
+
const src = g.deform;
|
|
131
|
+
if (!src || (!jm && !src.targets?.length))
|
|
132
|
+
continue;
|
|
133
|
+
deformGeometry(g, src, w, jm);
|
|
134
|
+
changed++;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return changed;
|
|
138
|
+
}
|
|
139
|
+
/** Morph + skin one geometry from its source data (pure). */
|
|
140
|
+
function deformGeometry(g, src, weights, joints) {
|
|
141
|
+
const P = src.positions, N = src.normals, n = P.length / 3;
|
|
142
|
+
const pos = g.positions !== P && g.positions.length === P.length ? g.positions : (g.positions = new Float32Array(P.length));
|
|
143
|
+
const nrm = N ? (g.normals && g.normals !== N && g.normals.length === N.length ? g.normals : (g.normals = new Float32Array(N.length))) : null;
|
|
144
|
+
pos.set(P);
|
|
145
|
+
if (nrm && N)
|
|
146
|
+
nrm.set(N);
|
|
147
|
+
(src.targets || []).forEach((tg, k) => {
|
|
148
|
+
const wk = weights[k] || 0;
|
|
149
|
+
if (!wk)
|
|
150
|
+
return;
|
|
151
|
+
if (tg.positions)
|
|
152
|
+
for (let i = 0; i < pos.length; i++)
|
|
153
|
+
pos[i] += wk * tg.positions[i];
|
|
154
|
+
if (tg.normals && nrm)
|
|
155
|
+
for (let i = 0; i < nrm.length; i++)
|
|
156
|
+
nrm[i] += wk * tg.normals[i];
|
|
157
|
+
});
|
|
158
|
+
if (joints && src.joints && src.weights) {
|
|
159
|
+
const J = src.joints, W = src.weights, m = new Float32Array(16);
|
|
160
|
+
for (let v = 0; v < n; v++) {
|
|
161
|
+
m.fill(0);
|
|
162
|
+
let tw = 0;
|
|
163
|
+
for (let k = 0; k < 4; k++) {
|
|
164
|
+
const wt = W[v * 4 + k];
|
|
165
|
+
if (!wt)
|
|
166
|
+
continue;
|
|
167
|
+
const jm = joints[J[v * 4 + k]];
|
|
168
|
+
if (!jm)
|
|
169
|
+
continue;
|
|
170
|
+
tw += wt;
|
|
171
|
+
for (let e = 0; e < 16; e++)
|
|
172
|
+
m[e] += wt * jm[e];
|
|
173
|
+
}
|
|
174
|
+
if (!tw)
|
|
175
|
+
continue;
|
|
176
|
+
const x = pos[v * 3], y = pos[v * 3 + 1], z = pos[v * 3 + 2];
|
|
177
|
+
pos[v * 3] = m[0] * x + m[4] * y + m[8] * z + m[12];
|
|
178
|
+
pos[v * 3 + 1] = m[1] * x + m[5] * y + m[9] * z + m[13];
|
|
179
|
+
pos[v * 3 + 2] = m[2] * x + m[6] * y + m[10] * z + m[14];
|
|
180
|
+
if (nrm) {
|
|
181
|
+
const a = nrm[v * 3], b = nrm[v * 3 + 1], c = nrm[v * 3 + 2];
|
|
182
|
+
const nx = m[0] * a + m[4] * b + m[8] * c, ny = m[1] * a + m[5] * b + m[9] * c, nz = m[2] * a + m[6] * b + m[10] * c;
|
|
183
|
+
const l = Math.hypot(nx, ny, nz) || 1;
|
|
184
|
+
nrm[v * 3] = nx / l;
|
|
185
|
+
nrm[v * 3 + 1] = ny / l;
|
|
186
|
+
nrm[v * 3 + 2] = nz / l;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
g.version = (g.version || 0) + 1;
|
|
191
|
+
}
|
|
192
|
+
/** A clip player for a loaded model. */
|
|
193
|
+
function gltfAnimator(root, o = {}) {
|
|
194
|
+
const clips = gltfClips(root);
|
|
195
|
+
let clip = null, off = null;
|
|
196
|
+
const pick = (c) => (typeof c === 'number' ? clips[c] : clips.find((x) => x.name === c) || clips[Number(c)]) || null;
|
|
197
|
+
const pose = () => {
|
|
198
|
+
if (clip)
|
|
199
|
+
applyClip(clip, a.time);
|
|
200
|
+
deformModel(root);
|
|
201
|
+
};
|
|
202
|
+
const a = {
|
|
203
|
+
clips,
|
|
204
|
+
get clip() {
|
|
205
|
+
return clip;
|
|
206
|
+
},
|
|
207
|
+
time: 0,
|
|
208
|
+
speed: o.speed ?? 1,
|
|
209
|
+
loop: o.loop ?? true,
|
|
210
|
+
get playing() {
|
|
211
|
+
return !!off;
|
|
212
|
+
},
|
|
213
|
+
use(c) {
|
|
214
|
+
clip = pick(c);
|
|
215
|
+
a.time = 0;
|
|
216
|
+
pose();
|
|
217
|
+
},
|
|
218
|
+
seek(t) {
|
|
219
|
+
const d = clip?.duration || 0;
|
|
220
|
+
a.time = d ? (a.loop ? ((t % d) + d) % d : Math.min(d, Math.max(0, t))) : 0;
|
|
221
|
+
pose();
|
|
222
|
+
},
|
|
223
|
+
update(dt) {
|
|
224
|
+
a.seek(a.time + dt * a.speed);
|
|
225
|
+
},
|
|
226
|
+
play() {
|
|
227
|
+
if (!off)
|
|
228
|
+
off = registry.requireModule('core', 'motionary/runtime/gltf-anim').getTicker().add((_, ms) => a.update(Math.min(ms, 100) / 1000));
|
|
229
|
+
},
|
|
230
|
+
pause() {
|
|
231
|
+
off?.();
|
|
232
|
+
off = null;
|
|
233
|
+
},
|
|
234
|
+
dispose() {
|
|
235
|
+
a.pause();
|
|
236
|
+
},
|
|
237
|
+
};
|
|
238
|
+
clip = pick(o.clip ?? 0);
|
|
239
|
+
pose();
|
|
240
|
+
return a;
|
|
241
|
+
}
|
|
242
|
+
const gltfAnim = { id: 'gltf-anim', version: registry.RUNTIME_VERSION, requires: ['core', 'gl', 'format-gltf'], api: { gltfClips, sampleChannel, applyClip, deformModel, deformGeometry, gltfAnimator } };
|
|
243
|
+
|
|
244
|
+
exports.applyClip = applyClip;
|
|
245
|
+
exports.deformGeometry = deformGeometry;
|
|
246
|
+
exports.deformModel = deformModel;
|
|
247
|
+
exports.gltfAnim = gltfAnim;
|
|
248
|
+
exports.gltfAnimator = gltfAnimator;
|
|
249
|
+
exports.gltfClips = gltfClips;
|
|
250
|
+
exports.sampleChannel = sampleChannel;
|
|
251
|
+
//# sourceMappingURL=gltf-anim.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gltf-anim.cjs","sources":["../../src/runtime/gltf-anim.ts"],"sourcesContent":["/**\n * `motionary/runtime/gltf-anim` (10.8) — glTF 2.0 animation, skinning and\n * morph targets for models loaded by `motionary/runtime/format-gltf`\n * (own implementation; requires `use(gl, formatGltf, gltfAnim)`).\n *\n * - animation channels on node `translation` / `rotation` / `scale` /\n * `weights` with `LINEAR` (quaternions slerped), `STEP` and\n * `CUBICSPLINE` (Hermite with in / out tangents) samplers;\n * - morph targets (POSITION + NORMAL deltas) with mesh default weights,\n * node weights and animated weights;\n * - skins: joint hierarchy, inverse bind matrices, 4 joints / vertex\n * (JOINTS_0 / WEIGHTS_0), skinned on the CPU and re-uploaded each frame\n * (`geometry.version`), so it works with every runtime/gl material;\n * - `gltfAnimator(model)` plays a clip (by name or index) with loop / speed\n * / seek, `update(dt)` from your loop or `play()` on the shared ticker.\n *\n * Sampling and deformation are pure (SSR / workers / tests).\n */\nimport { RUNTIME_VERSION, requireModule, type RuntimeModule } from './registry';\nimport type { CoreApi } from './index';\nimport { mat4, quatSlerp, type GlNode, type Geometry, type Mat4, type Quat } from './gl';\n\ntype Num = number;\nexport type ChannelPath = 'translation' | 'rotation' | 'scale' | 'weights';\nexport type Interpolation = 'LINEAR' | 'STEP' | 'CUBICSPLINE';\nexport interface GltfChannel {\n node: GlNode;\n path: ChannelPath;\n interpolation: Interpolation;\n times: Float32Array;\n values: Float32Array;\n /** Components per key (3, 4 or the morph target count). */\n size: Num;\n}\nexport interface GltfClip {\n name: string;\n duration: Num;\n channels: GltfChannel[];\n}\n\n/** Morph / skin source data kept on a geometry by format-gltf (10.8). */\nexport interface DeformSource {\n positions: Float32Array;\n normals?: Float32Array;\n joints?: Float32Array;\n weights?: Float32Array;\n targets?: { positions?: Float32Array; normals?: Float32Array }[];\n}\n\nconst model = (root: GlNode): { gltf: any; nodes: GlNode[]; read: (i: Num) => Float32Array } => {\n const x = root.extras as any;\n if (!x?.gltf || !x.read) throw new Error('[motionary] gltf-anim: pass the node returned by loadGltf() / gltfToNode() (motionary/runtime/format-gltf 10.8+)');\n return x;\n};\n\n/** The animation clips of a loaded model. */\nexport function gltfClips(root: GlNode): GltfClip[] {\n const { gltf, nodes, read } = model(root);\n return (gltf.animations || []).map((a: any, ai: Num) => {\n let duration = 0;\n const channels: GltfChannel[] = [];\n for (const c of a.channels || []) {\n const node = nodes[c.target?.node];\n const s = a.samplers?.[c.sampler];\n if (!node || !s || !['translation', 'rotation', 'scale', 'weights'].includes(c.target.path)) continue; // KHR_animation_pointer etc.\n const times = read(s.input), values = read(s.output);\n const interpolation: Interpolation = s.interpolation || 'LINEAR';\n const size = values.length / times.length / (interpolation === 'CUBICSPLINE' ? 3 : 1);\n duration = Math.max(duration, times[times.length - 1] || 0);\n channels.push({ node, path: c.target.path, interpolation, times, values, size });\n }\n return { name: a.name || `animation${ai}`, duration, channels };\n });\n}\n\n/** Value of a channel at time `t` (seconds; clamped to the key range). */\nexport function sampleChannel(ch: GltfChannel, t: Num): Num[] {\n const { times: T, values: V, size: n } = ch;\n const cubic = ch.interpolation === 'CUBICSPLINE';\n const at = (k: Num) => Array.from(V.subarray((cubic ? k * 3 + 1 : k) * n, (cubic ? k * 3 + 1 : k) * n + n));\n const last = T.length - 1;\n if (t <= T[0] || last === 0) return at(0);\n if (t >= T[last]) return at(last);\n let k = 0;\n while (k < last - 1 && T[k + 1] <= t) k++;\n const td = T[k + 1] - T[k], s = (t - T[k]) / td;\n let out: Num[];\n if (ch.interpolation === 'STEP') return at(k);\n if (cubic) {\n const s2 = s * s, s3 = s2 * s;\n const h00 = 2 * s3 - 3 * s2 + 1, h10 = s3 - 2 * s2 + s, h01 = -2 * s3 + 3 * s2, h11 = s3 - s2;\n const p0 = k * 3 * n, p1 = (k + 1) * 3 * n;\n out = Array.from({ length: n }, (_, i) => h00 * V[p0 + n + i] + h10 * td * V[p0 + 2 * n + i] + h01 * V[p1 + n + i] + h11 * td * V[p1 + i]);\n if (ch.path === 'rotation') {\n const l = Math.hypot(...out) || 1;\n out = out.map((v) => v / l);\n }\n return out;\n }\n const a = at(k), b = at(k + 1);\n if (ch.path === 'rotation') return quatSlerp(a as Quat, b as Quat, s);\n return a.map((v, i) => v + (b[i] - v) * s);\n}\n\n/** Pose the model at time `t` of a clip (sets node TRS and morph weights). */\nexport function applyClip(clip: GltfClip, t: Num): void {\n for (const ch of clip.channels) {\n const v = sampleChannel(ch, t);\n const n = ch.node;\n if (ch.path === 'weights') n.extras.weights = v;\n else {\n n.matrix = null; // animated nodes are TRS (glTF spec)\n if (ch.path === 'translation') n.position = v as [Num, Num, Num];\n else if (ch.path === 'rotation') n.rotation = v as Quat;\n else n.scale = v as [Num, Num, Num];\n }\n }\n}\n\nconst meshesOf = (n: GlNode) => (n.mesh ? (Array.isArray(n.mesh) ? n.mesh : [n.mesh]) : []);\n\n/**\n * Apply morph weights and skins to every deformable mesh of the model\n * (call after posing; updates world matrices). Returns how many meshes changed.\n */\nexport function deformModel(root: GlNode): Num {\n const { gltf, nodes, read } = model(root);\n root.updateWorld();\n let changed = 0;\n const cache = ((root.extras as any).skinCache ||= {}) as Record<Num, { joints: GlNode[]; ibm: Mat4[] }>;\n for (const node of nodes) {\n const ms = meshesOf(node);\n if (!ms.length) continue;\n const si = node.extras.skin as Num | undefined;\n let jm: Mat4[] | null = null;\n if (si !== undefined && gltf.skins?.[si]) {\n const sk = gltf.skins[si];\n const c = (cache[si] ||= {\n joints: sk.joints.map((j: Num) => nodes[j]),\n ibm: sk.joints.map((_: Num, i: Num) => (sk.inverseBindMatrices !== undefined ? new Float32Array(read(sk.inverseBindMatrices).subarray(i * 16, i * 16 + 16)) : mat4.identity())),\n });\n const inv = mat4.invert(node.world) || mat4.identity();\n jm = c.joints.map((j, i) => mat4.multiply(inv, mat4.multiply(j.world, c.ibm[i])));\n }\n const w = (node.extras.weights as Num[] | undefined) || [];\n for (const m of ms) {\n const g = m.geometry as Geometry & { deform?: DeformSource };\n const src = g.deform;\n if (!src || (!jm && !src.targets?.length)) continue;\n deformGeometry(g, src, w, jm);\n changed++;\n }\n }\n return changed;\n}\n\n/** Morph + skin one geometry from its source data (pure). */\nexport function deformGeometry(g: Geometry, src: DeformSource, weights: Num[], joints: Mat4[] | null): void {\n const P = src.positions, N = src.normals, n = P.length / 3;\n const pos = g.positions !== P && g.positions.length === P.length ? g.positions : (g.positions = new Float32Array(P.length));\n const nrm = N ? (g.normals && g.normals !== N && g.normals.length === N.length ? g.normals : (g.normals = new Float32Array(N.length))) : null;\n pos.set(P);\n if (nrm && N) nrm.set(N);\n (src.targets || []).forEach((tg, k) => {\n const wk = weights[k] || 0;\n if (!wk) return;\n if (tg.positions) for (let i = 0; i < pos.length; i++) pos[i] += wk * tg.positions[i];\n if (tg.normals && nrm) for (let i = 0; i < nrm.length; i++) nrm[i] += wk * tg.normals[i];\n });\n if (joints && src.joints && src.weights) {\n const J = src.joints, W = src.weights, m = new Float32Array(16);\n for (let v = 0; v < n; v++) {\n m.fill(0);\n let tw = 0;\n for (let k = 0; k < 4; k++) {\n const wt = W[v * 4 + k];\n if (!wt) continue;\n const jm = joints[J[v * 4 + k]];\n if (!jm) continue;\n tw += wt;\n for (let e = 0; e < 16; e++) m[e] += wt * jm[e];\n }\n if (!tw) continue;\n const x = pos[v * 3], y = pos[v * 3 + 1], z = pos[v * 3 + 2];\n pos[v * 3] = m[0] * x + m[4] * y + m[8] * z + m[12];\n pos[v * 3 + 1] = m[1] * x + m[5] * y + m[9] * z + m[13];\n pos[v * 3 + 2] = m[2] * x + m[6] * y + m[10] * z + m[14];\n if (nrm) {\n const a = nrm[v * 3], b = nrm[v * 3 + 1], c = nrm[v * 3 + 2];\n const nx = m[0] * a + m[4] * b + m[8] * c, ny = m[1] * a + m[5] * b + m[9] * c, nz = m[2] * a + m[6] * b + m[10] * c;\n const l = Math.hypot(nx, ny, nz) || 1;\n nrm[v * 3] = nx / l;\n nrm[v * 3 + 1] = ny / l;\n nrm[v * 3 + 2] = nz / l;\n }\n }\n }\n g.version = (g.version || 0) + 1;\n}\n\nexport interface GltfAnimatorOptions {\n /** Clip name or index (default 0). */\n clip?: string | Num;\n loop?: boolean;\n speed?: Num;\n}\nexport interface GltfAnimator {\n readonly clips: GltfClip[];\n readonly clip: GltfClip | null;\n time: Num;\n speed: Num;\n loop: boolean;\n readonly playing: boolean;\n /** Switch clip (name or index); keeps playing state. */\n use(clip: string | Num): void;\n seek(t: Num): void;\n /** Advance by `dt` seconds and pose + deform. */\n update(dt: Num): void;\n /** Run on the shared ticker. */\n play(): void;\n pause(): void;\n dispose(): void;\n}\n\n/** A clip player for a loaded model. */\nexport function gltfAnimator(root: GlNode, o: GltfAnimatorOptions = {}): GltfAnimator {\n const clips = gltfClips(root);\n let clip: GltfClip | null = null, off: (() => void) | null = null;\n const pick = (c: string | Num) => (typeof c === 'number' ? clips[c] : clips.find((x) => x.name === c) || clips[Number(c)]) || null;\n const pose = () => {\n if (clip) applyClip(clip, a.time);\n deformModel(root);\n };\n const a: GltfAnimator = {\n clips,\n get clip() {\n return clip;\n },\n time: 0,\n speed: o.speed ?? 1,\n loop: o.loop ?? true,\n get playing() {\n return !!off;\n },\n use(c) {\n clip = pick(c);\n a.time = 0;\n pose();\n },\n seek(t) {\n const d = clip?.duration || 0;\n a.time = d ? (a.loop ? ((t % d) + d) % d : Math.min(d, Math.max(0, t))) : 0;\n pose();\n },\n update(dt) {\n a.seek(a.time + dt * a.speed);\n },\n play() {\n if (!off) off = requireModule<CoreApi>('core', 'motionary/runtime/gltf-anim').getTicker().add((_, ms) => a.update(Math.min(ms, 100) / 1000));\n },\n pause() {\n off?.();\n off = null;\n },\n dispose() {\n a.pause();\n },\n };\n clip = pick(o.clip ?? 0);\n pose();\n return a;\n}\n\nexport interface GltfAnimApi {\n gltfClips: typeof gltfClips;\n sampleChannel: typeof sampleChannel;\n applyClip: typeof applyClip;\n deformModel: typeof deformModel;\n deformGeometry: typeof deformGeometry;\n gltfAnimator: typeof gltfAnimator;\n}\n\nexport const gltfAnim: RuntimeModule<GltfAnimApi> = { id: 'gltf-anim', version: RUNTIME_VERSION, requires: ['core', 'gl', 'format-gltf'], api: { gltfClips, sampleChannel, applyClip, deformModel, deformGeometry, gltfAnimator } };\n"],"names":["quatSlerp","mat4","requireModule","RUNTIME_VERSION"],"mappings":";;;;;AAAA;;;;;;;;;;;;;;;;;AAiBG;AAgCH,MAAM,KAAK,GAAG,CAAC,IAAY,KAAoE;AAC7F,IAAA,MAAM,CAAC,GAAG,IAAI,CAAC,MAAa;IAC5B,IAAI,CAAC,CAAC,EAAE,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI;AAAE,QAAA,MAAM,IAAI,KAAK,CAAC,kHAAkH,CAAC;AAC5J,IAAA,OAAO,CAAC;AACV,CAAC;AAED;AACM,SAAU,SAAS,CAAC,IAAY,EAAA;AACpC,IAAA,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC;AACzC,IAAA,OA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216 @@
|
|
|
1
|
+
/** A runtime module: `{ id, version, api }`, registered with `use()`. */
|
|
2
|
+
interface RuntimeModule<A = unknown> {
|
|
3
|
+
/** Module id: 'core', 'format-css', 'scroll', … (import path `motionary/runtime/<id>`). */
|
|
4
|
+
id: string;
|
|
5
|
+
version: string;
|
|
6
|
+
/** Other modules this one needs (registered first by `use()` callers). */
|
|
7
|
+
requires?: string[];
|
|
8
|
+
/** The module's public API (what `requireModule(id)` returns). */
|
|
9
|
+
api: A;
|
|
10
|
+
/** Optional one-time setup, called on first registration. */
|
|
11
|
+
setup?(registry: RuntimeRegistry): void;
|
|
12
|
+
}
|
|
13
|
+
interface RuntimeRegistry {
|
|
14
|
+
version: string;
|
|
15
|
+
modules: Map<string, RuntimeModule>;
|
|
16
|
+
/** Shared per-page state slots (the ticker lives here). */
|
|
17
|
+
slots: Record<string, unknown>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* `motionary/runtime/gl` (10.5) — a small WebGL2 scene renderer written for
|
|
22
|
+
* Motionary (not a Three.js clone; own API, own shaders):
|
|
23
|
+
*
|
|
24
|
+
* - **math** (`mat4`, quaternions — column-major `Float32Array`s), pure;
|
|
25
|
+
* - **scene graph**: `GlNode` (position / rotation quaternion / scale,
|
|
26
|
+
* children, optional mesh), `Camera` (perspective, `lookAt`), lights
|
|
27
|
+
* (`ambient`, up to 4 directional / point), `bounds()` + `frameNode()`;
|
|
28
|
+
* - **geometry** builders (`box`, `plane`, `sphere`, `torus`) and custom
|
|
29
|
+
* geometry from typed arrays (indexed 16 / 32-bit or not);
|
|
30
|
+
* - **materials**: `standard` (metallic-roughness PBR approximation, base
|
|
31
|
+
* colour texture, emissive, ACES-free Reinhard tone mapping, sRGB),
|
|
32
|
+
* `unlit`, and `shader` (your GLSL ES 3.00 fragment with the standard
|
|
33
|
+
* varyings + your uniforms);
|
|
34
|
+
* - **textures** from images, canvases, bitmaps and **videos** (updated per
|
|
35
|
+
* decoded frame with `requestVideoFrameCallback`; `scrubVideo()` seeks a
|
|
36
|
+
* video from a 0–1 progress, e.g. a scroll scene);
|
|
37
|
+
* - `orbitControls()` — drag / wheel / pinch / arrow keys, damping, optional
|
|
38
|
+
* auto-rotate (off under reduced motion).
|
|
39
|
+
*
|
|
40
|
+
* Math, geometry and the scene graph are pure (SSR / workers); rendering
|
|
41
|
+
* needs WebGL2 (`createRenderer()` throws a clear error without it). Model
|
|
42
|
+
* loaders live in `motionary/runtime/format-gltf` and `format-obj`.
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
type Vec3 = [number, number, number];
|
|
46
|
+
type Quat = [number, number, number, number];
|
|
47
|
+
type Mat4 = Float32Array;
|
|
48
|
+
interface Geometry {
|
|
49
|
+
positions: Float32Array;
|
|
50
|
+
normals?: Float32Array;
|
|
51
|
+
uvs?: Float32Array;
|
|
52
|
+
indices?: Uint16Array | Uint32Array;
|
|
53
|
+
/** 'triangles' (default), 'lines', 'points'. */
|
|
54
|
+
mode?: 'triangles' | 'lines' | 'points';
|
|
55
|
+
/** Bump after changing positions / normals in place (10.8: skinning, morph targets) — the renderer re-uploads them. */
|
|
56
|
+
version?: number;
|
|
57
|
+
/** Renderer cache. */
|
|
58
|
+
_gpu?: unknown;
|
|
59
|
+
}
|
|
60
|
+
interface GlTexture {
|
|
61
|
+
source: TexImageSource | {
|
|
62
|
+
width: number;
|
|
63
|
+
height: number;
|
|
64
|
+
data: Uint8Array | Uint8ClampedArray;
|
|
65
|
+
};
|
|
66
|
+
/** sRGB colour data (default true for colour maps). */
|
|
67
|
+
srgb?: boolean;
|
|
68
|
+
repeat?: boolean;
|
|
69
|
+
flipY?: boolean;
|
|
70
|
+
/** Re-upload every frame a new video frame is ready. */
|
|
71
|
+
video?: boolean;
|
|
72
|
+
needsUpdate?: boolean;
|
|
73
|
+
_gpu?: unknown;
|
|
74
|
+
}
|
|
75
|
+
interface Material {
|
|
76
|
+
type: 'standard' | 'unlit' | 'shader';
|
|
77
|
+
/** Linear RGBA multiplier (sRGB input converted by the shader). */
|
|
78
|
+
color: [number, number, number, number];
|
|
79
|
+
map?: GlTexture | null;
|
|
80
|
+
metallic: number;
|
|
81
|
+
roughness: number;
|
|
82
|
+
emissive: Vec3;
|
|
83
|
+
doubleSided?: boolean;
|
|
84
|
+
transparent?: boolean;
|
|
85
|
+
wireframe?: boolean;
|
|
86
|
+
/** 'shader' materials: GLSL ES 3.00 fragment body + uniforms. */
|
|
87
|
+
fragment?: string;
|
|
88
|
+
uniforms?: Record<string, number | number[] | Float32Array | GlTexture>;
|
|
89
|
+
_gpu?: unknown;
|
|
90
|
+
}
|
|
91
|
+
interface Mesh {
|
|
92
|
+
geometry: Geometry;
|
|
93
|
+
material: Material;
|
|
94
|
+
}
|
|
95
|
+
declare class GlNode {
|
|
96
|
+
name: string;
|
|
97
|
+
position: Vec3;
|
|
98
|
+
rotation: Quat;
|
|
99
|
+
scale: Vec3;
|
|
100
|
+
/** Set to use a fixed local matrix instead of position / rotation / scale (glTF `matrix`). */
|
|
101
|
+
matrix: Mat4 | null;
|
|
102
|
+
children: GlNode[];
|
|
103
|
+
parent: GlNode | null;
|
|
104
|
+
mesh: Mesh | Mesh[] | null;
|
|
105
|
+
visible: boolean;
|
|
106
|
+
/** Free-form data (loaders put source info here). */
|
|
107
|
+
extras: Record<string, unknown>;
|
|
108
|
+
readonly world: Mat4;
|
|
109
|
+
constructor(name?: string, mesh?: Mesh | Mesh[] | null);
|
|
110
|
+
add(...nodes: GlNode[]): this;
|
|
111
|
+
remove(n: GlNode): void;
|
|
112
|
+
setEuler(x: number, y: number, z: number): this;
|
|
113
|
+
local(): Mat4;
|
|
114
|
+
/** Recompute world matrices of this subtree. */
|
|
115
|
+
updateWorld(parent?: Mat4): void;
|
|
116
|
+
traverse(fn: (n: GlNode) => void): void;
|
|
117
|
+
find(name: string): GlNode | null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* `motionary/runtime/gltf-anim` (10.8) — glTF 2.0 animation, skinning and
|
|
122
|
+
* morph targets for models loaded by `motionary/runtime/format-gltf`
|
|
123
|
+
* (own implementation; requires `use(gl, formatGltf, gltfAnim)`).
|
|
124
|
+
*
|
|
125
|
+
* - animation channels on node `translation` / `rotation` / `scale` /
|
|
126
|
+
* `weights` with `LINEAR` (quaternions slerped), `STEP` and
|
|
127
|
+
* `CUBICSPLINE` (Hermite with in / out tangents) samplers;
|
|
128
|
+
* - morph targets (POSITION + NORMAL deltas) with mesh default weights,
|
|
129
|
+
* node weights and animated weights;
|
|
130
|
+
* - skins: joint hierarchy, inverse bind matrices, 4 joints / vertex
|
|
131
|
+
* (JOINTS_0 / WEIGHTS_0), skinned on the CPU and re-uploaded each frame
|
|
132
|
+
* (`geometry.version`), so it works with every runtime/gl material;
|
|
133
|
+
* - `gltfAnimator(model)` plays a clip (by name or index) with loop / speed
|
|
134
|
+
* / seek, `update(dt)` from your loop or `play()` on the shared ticker.
|
|
135
|
+
*
|
|
136
|
+
* Sampling and deformation are pure (SSR / workers / tests).
|
|
137
|
+
*/
|
|
138
|
+
|
|
139
|
+
type Num = number;
|
|
140
|
+
type ChannelPath = 'translation' | 'rotation' | 'scale' | 'weights';
|
|
141
|
+
type Interpolation = 'LINEAR' | 'STEP' | 'CUBICSPLINE';
|
|
142
|
+
interface GltfChannel {
|
|
143
|
+
node: GlNode;
|
|
144
|
+
path: ChannelPath;
|
|
145
|
+
interpolation: Interpolation;
|
|
146
|
+
times: Float32Array;
|
|
147
|
+
values: Float32Array;
|
|
148
|
+
/** Components per key (3, 4 or the morph target count). */
|
|
149
|
+
size: Num;
|
|
150
|
+
}
|
|
151
|
+
interface GltfClip {
|
|
152
|
+
name: string;
|
|
153
|
+
duration: Num;
|
|
154
|
+
channels: GltfChannel[];
|
|
155
|
+
}
|
|
156
|
+
/** Morph / skin source data kept on a geometry by format-gltf (10.8). */
|
|
157
|
+
interface DeformSource {
|
|
158
|
+
positions: Float32Array;
|
|
159
|
+
normals?: Float32Array;
|
|
160
|
+
joints?: Float32Array;
|
|
161
|
+
weights?: Float32Array;
|
|
162
|
+
targets?: {
|
|
163
|
+
positions?: Float32Array;
|
|
164
|
+
normals?: Float32Array;
|
|
165
|
+
}[];
|
|
166
|
+
}
|
|
167
|
+
/** The animation clips of a loaded model. */
|
|
168
|
+
declare function gltfClips(root: GlNode): GltfClip[];
|
|
169
|
+
/** Value of a channel at time `t` (seconds; clamped to the key range). */
|
|
170
|
+
declare function sampleChannel(ch: GltfChannel, t: Num): Num[];
|
|
171
|
+
/** Pose the model at time `t` of a clip (sets node TRS and morph weights). */
|
|
172
|
+
declare function applyClip(clip: GltfClip, t: Num): void;
|
|
173
|
+
/**
|
|
174
|
+
* Apply morph weights and skins to every deformable mesh of the model
|
|
175
|
+
* (call after posing; updates world matrices). Returns how many meshes changed.
|
|
176
|
+
*/
|
|
177
|
+
declare function deformModel(root: GlNode): Num;
|
|
178
|
+
/** Morph + skin one geometry from its source data (pure). */
|
|
179
|
+
declare function deformGeometry(g: Geometry, src: DeformSource, weights: Num[], joints: Mat4[] | null): void;
|
|
180
|
+
interface GltfAnimatorOptions {
|
|
181
|
+
/** Clip name or index (default 0). */
|
|
182
|
+
clip?: string | Num;
|
|
183
|
+
loop?: boolean;
|
|
184
|
+
speed?: Num;
|
|
185
|
+
}
|
|
186
|
+
interface GltfAnimator {
|
|
187
|
+
readonly clips: GltfClip[];
|
|
188
|
+
readonly clip: GltfClip | null;
|
|
189
|
+
time: Num;
|
|
190
|
+
speed: Num;
|
|
191
|
+
loop: boolean;
|
|
192
|
+
readonly playing: boolean;
|
|
193
|
+
/** Switch clip (name or index); keeps playing state. */
|
|
194
|
+
use(clip: string | Num): void;
|
|
195
|
+
seek(t: Num): void;
|
|
196
|
+
/** Advance by `dt` seconds and pose + deform. */
|
|
197
|
+
update(dt: Num): void;
|
|
198
|
+
/** Run on the shared ticker. */
|
|
199
|
+
play(): void;
|
|
200
|
+
pause(): void;
|
|
201
|
+
dispose(): void;
|
|
202
|
+
}
|
|
203
|
+
/** A clip player for a loaded model. */
|
|
204
|
+
declare function gltfAnimator(root: GlNode, o?: GltfAnimatorOptions): GltfAnimator;
|
|
205
|
+
interface GltfAnimApi {
|
|
206
|
+
gltfClips: typeof gltfClips;
|
|
207
|
+
sampleChannel: typeof sampleChannel;
|
|
208
|
+
applyClip: typeof applyClip;
|
|
209
|
+
deformModel: typeof deformModel;
|
|
210
|
+
deformGeometry: typeof deformGeometry;
|
|
211
|
+
gltfAnimator: typeof gltfAnimator;
|
|
212
|
+
}
|
|
213
|
+
declare const gltfAnim: RuntimeModule<GltfAnimApi>;
|
|
214
|
+
|
|
215
|
+
export { applyClip, deformGeometry, deformModel, gltfAnim, gltfAnimator, gltfClips, sampleChannel };
|
|
216
|
+
export type { ChannelPath, DeformSource, GltfAnimApi, GltfAnimator, GltfAnimatorOptions, GltfChannel, GltfClip, Interpolation };
|