@margoengine/cutscene 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 +773 -2
- package/dist/curve-editor.d.ts +79 -0
- package/dist/curve-editor.js +293 -0
- package/dist/cutscene-WWP9-stS.js +527 -0
- package/dist/cutscene.d.ts +157 -0
- package/dist/document.d.ts +234 -0
- package/dist/editor.d.ts +254 -0
- package/dist/editor.js +514 -0
- package/dist/film-titles.d.ts +162 -0
- package/dist/film-titles.js +229 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/lipsync.d.ts +72 -0
- package/dist/lipsync.js +377 -0
- package/dist/nested-editor.d.ts +69 -0
- package/dist/nested-editor.js +217 -0
- package/dist/player-CbKMbpyN.js +143 -0
- package/dist/player.d.ts +94 -0
- package/dist/record.d.ts +143 -0
- package/dist/record.js +300 -0
- package/dist/render.d.ts +100 -0
- package/dist/render.js +124 -0
- package/dist/scroll.d.ts +103 -0
- package/dist/scroll.js +135 -0
- package/dist/seek.d.ts +86 -0
- package/dist/seek.js +76 -0
- package/dist/shots.d.ts +98 -0
- package/dist/shots.js +220 -0
- package/dist/vfx.d.ts +106 -0
- package/dist/vfx.js +130 -0
- package/dist/viewport.d.ts +53 -0
- package/dist/viewport.js +103 -0
- package/package.json +122 -4
package/dist/record.js
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import { eulerToQuaternion as e, interpolateQuaternions as t } from "@margoengine/math/quat";
|
|
2
|
+
import { MargoError as n } from "@margoengine/math/errors";
|
|
3
|
+
import { InputRecorder as r } from "@margoengine/input/replay";
|
|
4
|
+
//#region src/record.ts
|
|
5
|
+
var i = (e, t) => {
|
|
6
|
+
let n = e, r = typeof n.x == "number" ? [
|
|
7
|
+
n.x,
|
|
8
|
+
n.y,
|
|
9
|
+
n.z
|
|
10
|
+
] : Array.from(e);
|
|
11
|
+
if (!r.length || !r.every(Number.isFinite)) throw RangeError(`Take sample ${t} must hold finite numbers`);
|
|
12
|
+
return r;
|
|
13
|
+
}, a = (e, t) => {
|
|
14
|
+
let n = 0;
|
|
15
|
+
for (let r = 0; r < e.length; r++) n += (e[r] - t[r]) ** 2;
|
|
16
|
+
return Math.sqrt(n);
|
|
17
|
+
}, o = (e, t, n) => e.map((e, r) => e + (t[r] - e) * n), s = (e, t) => {
|
|
18
|
+
let n = Math.abs(e[0] * t[0] + e[1] * t[1] + e[2] * t[2] + e[3] * t[3]);
|
|
19
|
+
return 2 * Math.acos(Math.min(1, n));
|
|
20
|
+
}, c = (e, n, r) => {
|
|
21
|
+
let i = t(e, n, r, .9995), a = Math.hypot(i[0], i[1], i[2], i[3]);
|
|
22
|
+
return [
|
|
23
|
+
i[0] / a,
|
|
24
|
+
i[1] / a,
|
|
25
|
+
i[2] / a,
|
|
26
|
+
i[3] / a
|
|
27
|
+
];
|
|
28
|
+
}, l = (e, t) => e.reduce((e, n, r) => Math.max(e, Math.abs(n - t[r])), 0);
|
|
29
|
+
function u(e, t, n, r, i) {
|
|
30
|
+
let a = e.length - 1;
|
|
31
|
+
if (a < 1) return a < 0 ? [] : [0];
|
|
32
|
+
let o = new Uint8Array(e.length), s = [[0, a]];
|
|
33
|
+
for (o[0] = o[a] = 1; s.length;) {
|
|
34
|
+
let [a, c] = s.pop(), l = e[c] - e[a], u = -1, d = n;
|
|
35
|
+
for (let n = a + 1; n < c; n++) {
|
|
36
|
+
let o = i(t[n], r(t[a], t[c], l > 0 ? (e[n] - e[a]) / l : 0));
|
|
37
|
+
o > d && (d = o, u = n);
|
|
38
|
+
}
|
|
39
|
+
u < 0 || (o[u] = 1, s.push([a, u], [u, c]));
|
|
40
|
+
}
|
|
41
|
+
let c = [];
|
|
42
|
+
return o.forEach((e, t) => e && c.push(t)), c.length === 2 && i(t[0], t[a]) <= n ? [0] : c;
|
|
43
|
+
}
|
|
44
|
+
var d = () => ({
|
|
45
|
+
times: [],
|
|
46
|
+
values: []
|
|
47
|
+
}), f = (e, t, n, r) => {
|
|
48
|
+
let i = e.values[0];
|
|
49
|
+
if (i && i.length !== n.length) throw RangeError(`Take sample ${r} changed its length; give the same number of values every tick`);
|
|
50
|
+
e.times.push(t), e.values.push(n);
|
|
51
|
+
}, p = class {
|
|
52
|
+
constructor({ tolerance: e = {}, now: t = () => performance.now() } = {}) {
|
|
53
|
+
this.transforms = /* @__PURE__ */ new Map(), this.properties = /* @__PURE__ */ new Map(), this.cameraRead = null, this.cameraData = d(), this.cameraFov = d(), this.cameraRoll = d(), this.events = [], this.marks = [], this.inputs = [], this.start = null, this.clock = 0, this.ticks = 0, this.stopped = !1;
|
|
54
|
+
for (let [t, n] of Object.entries(e)) if (!(n >= 0) || !Number.isFinite(n)) throw RangeError(`Take tolerance ${t} must be 0 or more`);
|
|
55
|
+
this.tolerance = {
|
|
56
|
+
position: .001,
|
|
57
|
+
rotation: .001,
|
|
58
|
+
scale: .001,
|
|
59
|
+
value: .001,
|
|
60
|
+
...e
|
|
61
|
+
}, this.now = t;
|
|
62
|
+
}
|
|
63
|
+
get time() {
|
|
64
|
+
return this.clock;
|
|
65
|
+
}
|
|
66
|
+
get frames() {
|
|
67
|
+
return this.ticks;
|
|
68
|
+
}
|
|
69
|
+
transform(e, t) {
|
|
70
|
+
if (this.open(), typeof e != "string" || !e) throw TypeError("Take transform needs the object name");
|
|
71
|
+
if (this.transforms.has(e)) throw RangeError(`Take already records '${e}'; use another name`);
|
|
72
|
+
let n = typeof t == "function" ? t : () => t;
|
|
73
|
+
return this.transforms.set(e, {
|
|
74
|
+
read: n,
|
|
75
|
+
position: d(),
|
|
76
|
+
rotation: d(),
|
|
77
|
+
scale: d()
|
|
78
|
+
}), this;
|
|
79
|
+
}
|
|
80
|
+
camera(e) {
|
|
81
|
+
if (this.open(), this.cameraRead) throw RangeError("Take already records a camera; use one per take");
|
|
82
|
+
return this.cameraRead = typeof e == "function" ? e : () => e, this;
|
|
83
|
+
}
|
|
84
|
+
property(e, t, n) {
|
|
85
|
+
if (this.open(), typeof n != "function") throw TypeError("Take property needs a function that reads the value");
|
|
86
|
+
let r = `property-${e}-${t}`;
|
|
87
|
+
if (this.properties.has(r)) throw RangeError(`Take already records ${e}.${t}; use another name`);
|
|
88
|
+
return this.properties.set(r, {
|
|
89
|
+
target: e,
|
|
90
|
+
property: t,
|
|
91
|
+
read: n,
|
|
92
|
+
data: d(),
|
|
93
|
+
scalar: !1
|
|
94
|
+
}), this;
|
|
95
|
+
}
|
|
96
|
+
input(e, { target: t = "input" } = {}) {
|
|
97
|
+
if (this.open(), this.inputs.some((e) => e.target === t)) throw RangeError(`Take already records input as '${t}'; give this one another target`);
|
|
98
|
+
return this.inputs.push({
|
|
99
|
+
recorder: new r(e, { now: () => this.clock * 1e3 }),
|
|
100
|
+
start: null,
|
|
101
|
+
target: t
|
|
102
|
+
}), this;
|
|
103
|
+
}
|
|
104
|
+
event(e, t) {
|
|
105
|
+
return this.open(), this.events.push(t === void 0 ? {
|
|
106
|
+
time: this.clock,
|
|
107
|
+
name: e
|
|
108
|
+
} : {
|
|
109
|
+
time: this.clock,
|
|
110
|
+
name: e,
|
|
111
|
+
data: t
|
|
112
|
+
}), this;
|
|
113
|
+
}
|
|
114
|
+
marker(e) {
|
|
115
|
+
return this.open(), this.marks.push({
|
|
116
|
+
time: this.clock,
|
|
117
|
+
name: e
|
|
118
|
+
}), this;
|
|
119
|
+
}
|
|
120
|
+
capture(t) {
|
|
121
|
+
this.open();
|
|
122
|
+
let n = t ?? this.now() / 1e3;
|
|
123
|
+
if (!Number.isFinite(n)) throw RangeError("Take capture time must be a finite number of seconds");
|
|
124
|
+
this.start ??= n;
|
|
125
|
+
let r = n - this.start;
|
|
126
|
+
if (r < this.clock) throw RangeError("Take capture time went backwards; pass times in order");
|
|
127
|
+
this.clock = r, this.ticks++;
|
|
128
|
+
for (let [t, n] of this.transforms) {
|
|
129
|
+
let a = n.read();
|
|
130
|
+
if (a.position && f(n.position, r, i(a.position, `${t}.position`).slice(0, 3), "position"), a.scale && f(n.scale, r, i(a.scale, `${t}.scale`).slice(0, 3), "scale"), a.rotation) {
|
|
131
|
+
let o = i(a.rotation, `${t}.rotation`), s = o.length === 3 ? e(o[0], o[1], o[2]) : o, c = n.rotation.values.at(-1), l = c && c[0] * s[0] + c[1] * s[1] + c[2] * s[2] + c[3] * s[3] < 0;
|
|
132
|
+
f(n.rotation, r, l ? s.map((e) => -e) : [...s], "rotation");
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
for (let e of this.properties.values()) {
|
|
136
|
+
let t = e.read();
|
|
137
|
+
e.scalar = typeof t == "number";
|
|
138
|
+
let n = typeof t == "number" ? [t] : [...t];
|
|
139
|
+
if (!n.every(Number.isFinite)) throw RangeError(`Take property ${e.target}.${e.property} must be finite`);
|
|
140
|
+
f(e.data, r, n, `${e.target}.${e.property}`);
|
|
141
|
+
}
|
|
142
|
+
if (this.cameraRead) {
|
|
143
|
+
let e = this.cameraRead();
|
|
144
|
+
f(this.cameraData, r, [...i(e.position, "camera position"), ...i(e.target, "camera target")], "camera"), f(this.cameraFov, r, [e.fov ?? 60], "fov"), f(this.cameraRoll, r, [e.roll ?? 0], "roll");
|
|
145
|
+
}
|
|
146
|
+
for (let e of this.inputs) e.start ??= r, e.recorder.capture();
|
|
147
|
+
return this;
|
|
148
|
+
}
|
|
149
|
+
stop() {
|
|
150
|
+
if (this.open(), !this.ticks) throw new n("state", "Take has no samples; call capture() once per tick before stop()");
|
|
151
|
+
this.stopped = !0;
|
|
152
|
+
let e = [], t = {}, r = (e, n, r) => void (t[e] = {
|
|
153
|
+
samples: n,
|
|
154
|
+
keys: r
|
|
155
|
+
});
|
|
156
|
+
this.cameraRead && e.push(this.cameraTrack(r));
|
|
157
|
+
for (let [t, n] of this.transforms) {
|
|
158
|
+
let i = this.transformTrack(t, n);
|
|
159
|
+
i && (e.push(i), r(i.id, n.position.times.length || n.rotation.times.length || n.scale.times.length, i.keys.length));
|
|
160
|
+
}
|
|
161
|
+
for (let [t, n] of this.properties) {
|
|
162
|
+
let { times: i, values: a } = n.data, s = u(i, a, this.tolerance.value, o, l).map((e) => ({
|
|
163
|
+
time: i[e],
|
|
164
|
+
value: n.scalar ? a[e][0] : a[e]
|
|
165
|
+
}));
|
|
166
|
+
e.push({
|
|
167
|
+
type: "property",
|
|
168
|
+
id: t,
|
|
169
|
+
target: n.target,
|
|
170
|
+
property: n.property,
|
|
171
|
+
keys: s
|
|
172
|
+
}), r(t, i.length, s.length);
|
|
173
|
+
}
|
|
174
|
+
for (let { recorder: t, start: n, target: r } of this.inputs) n !== null && e.push(...m(t.clip(), {
|
|
175
|
+
at: n,
|
|
176
|
+
target: r
|
|
177
|
+
}));
|
|
178
|
+
return this.events.length && e.push({
|
|
179
|
+
type: "event",
|
|
180
|
+
id: "events",
|
|
181
|
+
events: this.events
|
|
182
|
+
}), {
|
|
183
|
+
duration: this.clock,
|
|
184
|
+
tracks: e,
|
|
185
|
+
markers: this.marks,
|
|
186
|
+
kept: t
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
open() {
|
|
190
|
+
if (this.stopped) throw new n("state", "Take has stopped; create a new TakeRecorder for the next take");
|
|
191
|
+
}
|
|
192
|
+
transformTrack(e, t) {
|
|
193
|
+
let n = /* @__PURE__ */ new Map(), r = (e, t, r) => {
|
|
194
|
+
for (let i of r) {
|
|
195
|
+
let r = e.times[i], a = n.get(r);
|
|
196
|
+
a || n.set(r, a = { time: r }), a[t] = e.values[i];
|
|
197
|
+
}
|
|
198
|
+
}, { position: i, rotation: l, scale: d } = t;
|
|
199
|
+
return r(i, "position", u(i.times, i.values, this.tolerance.position, o, a)), r(l, "rotation", u(l.times, l.values, this.tolerance.rotation, c, s)), r(d, "scale", u(d.times, d.values, this.tolerance.scale, o, a)), n.size ? {
|
|
200
|
+
type: "transform",
|
|
201
|
+
id: `transform-${e}`,
|
|
202
|
+
target: e,
|
|
203
|
+
keys: [...n.values()].sort((e, t) => e.time - t.time)
|
|
204
|
+
} : null;
|
|
205
|
+
}
|
|
206
|
+
cameraTrack(e) {
|
|
207
|
+
let { times: t, values: n } = this.cameraData, r = u(t, n, this.tolerance.position, o, (e, t) => Math.max(a(e.slice(0, 3), t.slice(0, 3)), a(e.slice(3), t.slice(3)))), i = u(t, this.cameraFov.values, this.tolerance.value, o, l), s = u(t, this.cameraRoll.values, this.tolerance.value, o, l), c = [.../* @__PURE__ */ new Set([
|
|
208
|
+
...r,
|
|
209
|
+
...i,
|
|
210
|
+
...s
|
|
211
|
+
])].sort((e, t) => e - t).map((e) => {
|
|
212
|
+
let r = n[e];
|
|
213
|
+
return {
|
|
214
|
+
time: t[e],
|
|
215
|
+
position: r.slice(0, 3),
|
|
216
|
+
target: r.slice(3, 6),
|
|
217
|
+
fov: this.cameraFov.values[e][0],
|
|
218
|
+
roll: this.cameraRoll.values[e][0]
|
|
219
|
+
};
|
|
220
|
+
});
|
|
221
|
+
return e("camera", t.length, c.length), {
|
|
222
|
+
type: "camera",
|
|
223
|
+
id: "camera",
|
|
224
|
+
path: "linear",
|
|
225
|
+
keys: c
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
function m(e, { at: t = 0, target: n = "input" } = {}) {
|
|
230
|
+
let r = (n) => t + e.times[n] / 1e3, i = e.channels.map(() => [{
|
|
231
|
+
time: t,
|
|
232
|
+
value: 0
|
|
233
|
+
}]), a = [];
|
|
234
|
+
for (let t = 0; t < e.events.length; t += 3) {
|
|
235
|
+
let n = r(e.events[t]), o = e.events[t + 1], s = e.events[t + 2], c = i[o], l = c.at(-1), u = l.value, [d, f] = e.channels[o], p = {
|
|
236
|
+
binding: f,
|
|
237
|
+
value: s
|
|
238
|
+
};
|
|
239
|
+
l.time === n ? l.value = s : c.push({
|
|
240
|
+
time: n,
|
|
241
|
+
value: s,
|
|
242
|
+
ease: "step"
|
|
243
|
+
}), u === 0 && s !== 0 ? a.push({
|
|
244
|
+
time: n,
|
|
245
|
+
name: d,
|
|
246
|
+
data: p
|
|
247
|
+
}) : u !== 0 && s === 0 && a.push({
|
|
248
|
+
time: n,
|
|
249
|
+
name: `${d}:up`,
|
|
250
|
+
data: p
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
let o = (t) => {
|
|
254
|
+
let [n, r] = e.channels[t];
|
|
255
|
+
return r ? `${n}#${r}` : n;
|
|
256
|
+
}, s = e.channels.map((e, t) => ({
|
|
257
|
+
type: "property",
|
|
258
|
+
id: `${n}-${o(t)}`,
|
|
259
|
+
target: n,
|
|
260
|
+
property: o(t),
|
|
261
|
+
keys: i[t]
|
|
262
|
+
}));
|
|
263
|
+
return a.length && s.push({
|
|
264
|
+
type: "event",
|
|
265
|
+
id: `${n}-events`,
|
|
266
|
+
events: a
|
|
267
|
+
}), s;
|
|
268
|
+
}
|
|
269
|
+
function h(e, t, n) {
|
|
270
|
+
let r = JSON.parse(JSON.stringify(e));
|
|
271
|
+
r.id = n + r.id;
|
|
272
|
+
for (let e of r.keys ?? []) e.time += t;
|
|
273
|
+
for (let e of r.events ?? []) e.time += t;
|
|
274
|
+
return r;
|
|
275
|
+
}
|
|
276
|
+
var g = 1 / 60;
|
|
277
|
+
function _(e) {
|
|
278
|
+
return {
|
|
279
|
+
format: "margo-cutscene",
|
|
280
|
+
version: 1,
|
|
281
|
+
duration: Math.max(e.duration, g),
|
|
282
|
+
tracks: e.tracks,
|
|
283
|
+
...e.markers.length ? { markers: e.markers } : {}
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
function v(e, t, { at: n = e.duration, prefix: r = "" } = {}) {
|
|
287
|
+
if (!(n >= 0) || !Number.isFinite(n)) throw RangeError("appendTake needs a time of 0 or more");
|
|
288
|
+
let i = JSON.parse(JSON.stringify(e)), a = new Set(i.tracks.map((e) => e.id));
|
|
289
|
+
for (let e of t.tracks) {
|
|
290
|
+
let t = h(e, n, r);
|
|
291
|
+
if (a.has(t.id)) throw RangeError(`Track id '${t.id}' is taken; pass a prefix to appendTake`);
|
|
292
|
+
a.add(t.id), i.tracks.push(t);
|
|
293
|
+
}
|
|
294
|
+
return t.markers.length && (i.markers = [...i.markers ?? [], ...t.markers.map((e) => ({
|
|
295
|
+
...e,
|
|
296
|
+
time: e.time + n
|
|
297
|
+
}))].sort((e, t) => e.time - t.time)), i.duration = Math.max(i.duration, n + Math.max(t.duration, g)), i;
|
|
298
|
+
}
|
|
299
|
+
//#endregion
|
|
300
|
+
export { p as TakeRecorder, v as appendTake, m as inputTracks, _ as takeDocument };
|
package/dist/render.d.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// @ts-ignore optional peer: its types apply when it is installed
|
|
2
|
+
import type { Camera, Engine } from '@margoengine/core';
|
|
3
|
+
// @ts-ignore optional peer: its types apply when it is installed
|
|
4
|
+
import type { EncodeVideoOptions, EncodedVideo, RenderFramesOptions } from '@margoengine/headless/video';
|
|
5
|
+
import type { CutsceneOptions, CutsceneState } from './cutscene.js';
|
|
6
|
+
import { Cutscene } from './cutscene.js';
|
|
7
|
+
import type { CutsceneDocument } from './document.js';
|
|
8
|
+
import { CutscenePlayer } from './player.js';
|
|
9
|
+
import { seekSync } from './seek.js';
|
|
10
|
+
/** A sound to mix: an `AudioBuffer`, or anything with the same members. */
|
|
11
|
+
export interface MixSound {
|
|
12
|
+
readonly sampleRate: number;
|
|
13
|
+
readonly numberOfChannels: number;
|
|
14
|
+
readonly length: number;
|
|
15
|
+
getChannelData(channel: number): Float32Array;
|
|
16
|
+
}
|
|
17
|
+
/** Options of {@link mixCutsceneAudio}. */
|
|
18
|
+
export interface MixOptions {
|
|
19
|
+
/** Output rate, samples per second. Default 48000. */
|
|
20
|
+
sampleRate?: number;
|
|
21
|
+
/** Output channels, 1 or 2. Default 2. */
|
|
22
|
+
channels?: 1 | 2;
|
|
23
|
+
/** Start of the span to mix, seconds. Default 0. */
|
|
24
|
+
from?: number;
|
|
25
|
+
/** End of the span to mix, seconds. Default the end of the cutscene. */
|
|
26
|
+
to?: number;
|
|
27
|
+
}
|
|
28
|
+
/** What {@link mixCutsceneAudio} gives back. */
|
|
29
|
+
export interface CutsceneMix {
|
|
30
|
+
/** One array per channel. */
|
|
31
|
+
channels: Float32Array[];
|
|
32
|
+
sampleRate: number;
|
|
33
|
+
/** The largest absolute sample. Above 1 the mix will clip when encoded: lower the clips' `volume`. */
|
|
34
|
+
peak: number;
|
|
35
|
+
}
|
|
36
|
+
/** A frame of {@link cutsceneFrames}. */
|
|
37
|
+
export interface CutsceneFrame {
|
|
38
|
+
/** From 0. */
|
|
39
|
+
index: number;
|
|
40
|
+
/** Time on the cutscene, seconds. */
|
|
41
|
+
time: number;
|
|
42
|
+
/** The whole state at that time. */
|
|
43
|
+
state: CutsceneState;
|
|
44
|
+
}
|
|
45
|
+
/** Options of {@link cutsceneFrames}. */
|
|
46
|
+
export interface CutsceneFramesOptions {
|
|
47
|
+
/** Frames per second. Default 30. */
|
|
48
|
+
fps?: number;
|
|
49
|
+
/** Start of the span, seconds. Default 0. */
|
|
50
|
+
from?: number;
|
|
51
|
+
/** End of the span, seconds. Default the end of the cutscene. */
|
|
52
|
+
to?: number;
|
|
53
|
+
/**
|
|
54
|
+
* The engine you draw the frames with. A span from after 0 then seeks its fresh clocks to `from` first (`seekSync`
|
|
55
|
+
* from `@margoengine/cutscene/seek`, steps of `1 / fps`), so sky, wind, waves and every other clock show what a run
|
|
56
|
+
* from 0 shows there. Default none: the frames are plain state.
|
|
57
|
+
*/
|
|
58
|
+
engine?: Parameters<typeof seekSync>[0];
|
|
59
|
+
}
|
|
60
|
+
/** Options of {@link renderCutscene}. */
|
|
61
|
+
export interface RenderCutsceneOptions extends Omit<CutsceneFramesOptions, 'engine'>, Pick<EncodeVideoOptions, 'format' | 'codec' | 'bitrate' | 'keyFrameInterval' | 'webCodecs'>, Pick<RenderFramesOptions, 'samples' | 'shutter' | 'shutterPhase' | 'filter' | 'filterWidth' | 'seed' | 'pattern' | 'accumulate'>, CutsceneOptions {
|
|
62
|
+
/** Puts a frame's state on your scene: objects, animation, particles. May be async. Required. */
|
|
63
|
+
apply: (state: CutsceneState, frame: CutsceneFrame) => unknown;
|
|
64
|
+
/** Render through this camera, set from `state.camera` before `apply`. Default the engine's camera, left to `apply`. */
|
|
65
|
+
camera?: Camera;
|
|
66
|
+
/** The sounds the audio tracks name, by name. Default none: the file has no sound. */
|
|
67
|
+
sounds?: Readonly<Record<string, MixSound>>;
|
|
68
|
+
/** Sample rate of the sound. Default 48000. */
|
|
69
|
+
sampleRate?: number;
|
|
70
|
+
/** Bits per second of the sound. Default 128000. */
|
|
71
|
+
audioBitrate?: number;
|
|
72
|
+
/** Called after each frame is drawn, with frames done and frames in all. Default none. */
|
|
73
|
+
onProgress?: (done: number, total: number) => void;
|
|
74
|
+
/** Stops rendering; the promise rejects with the signal's reason. Default none. */
|
|
75
|
+
signal?: AbortSignal;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Mixes the sounds of the document's audio tracks to plain samples: every clip in nested timelines too, with its
|
|
79
|
+
* volume, fades, offset and nested speed, as `evaluate` reports them. The mix is a sum, with no limiter; read `peak`.
|
|
80
|
+
* A mono sound goes to both channels, and a stereo sound in a mono mix is averaged.
|
|
81
|
+
* @throws RangeError naming a sound a clip uses and `sounds` does not hold, or for a bad span.
|
|
82
|
+
*/
|
|
83
|
+
export declare function mixCutsceneAudio(cutscene: Cutscene, sounds: Readonly<Record<string, MixSound>>, _arg0?: MixOptions): CutsceneMix;
|
|
84
|
+
/**
|
|
85
|
+
* The frames of a span at a fixed rate: frame `i` shows `from + i / fps`, and the last starts before `to`. Plain state
|
|
86
|
+
* with no engine, for a pipeline of your own.
|
|
87
|
+
*/
|
|
88
|
+
export declare function cutsceneFrames(cutscene: Cutscene, _arg1?: CutsceneFramesOptions): Generator<CutsceneFrame, void, void>;
|
|
89
|
+
/**
|
|
90
|
+
* Renders a cutscene to a WebM or MP4 file with WebCodecs, at a fixed frame rate. The engine must be on a
|
|
91
|
+
* `createHeadlessCanvas` canvas of the video's size. A {@link CutscenePlayer} you pass fires every instant in order as
|
|
92
|
+
* the frames go by, as if it played; listeners may spawn things, and the frames show them. A span from after 0 first
|
|
93
|
+
* seeks the engine's clocks to `from` (`seek` from `@margoengine/cutscene/seek`), so they must be fresh. With `samples`
|
|
94
|
+
* above 1 or `shutter` above 0 every frame is that many renders over the open shutter, averaged in linear light
|
|
95
|
+
* (anti-aliasing and motion blur; `renderFrames` in `@margoengine/headless/video`): `apply` and the player run at
|
|
96
|
+
* each render's own time, and a span's seek replays the sub-frames' own steps, so it still equals a render from 0.
|
|
97
|
+
* @throws RangeError for a bad span or rate, or a sound nobody gave; MargoError `'state'` for a span from after 0 on
|
|
98
|
+
* an engine whose clocks already moved; see `encodeVideo` for what the encoder refuses.
|
|
99
|
+
*/
|
|
100
|
+
export declare function renderCutscene(engine: Engine, cutscene: Cutscene | CutscenePlayer | CutsceneDocument | string | unknown, options: RenderCutsceneOptions): Promise<EncodedVideo>;
|
package/dist/render.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { t as e } from "./cutscene-WWP9-stS.js";
|
|
2
|
+
import { t } from "./player-CbKMbpyN.js";
|
|
3
|
+
import { seek as n, seekSync as r } from "./seek.js";
|
|
4
|
+
//#region src/render.ts
|
|
5
|
+
var i = 128, a = (e, t = 0, n = e.duration) => {
|
|
6
|
+
if (!Number.isFinite(t) || !Number.isFinite(n) || t < 0 || n > e.duration || !(n > t)) throw RangeError(`Render span must lie inside the cutscene (0 to ${e.duration}) with from before to`);
|
|
7
|
+
return [t, n];
|
|
8
|
+
}, o = (e, t) => {
|
|
9
|
+
if (!(e > 0) || !Number.isFinite(e)) throw RangeError(`${t} must be a finite number above 0`);
|
|
10
|
+
};
|
|
11
|
+
function s(e, t, n) {
|
|
12
|
+
let r = n * e.sampleRate, i = Math.floor(r);
|
|
13
|
+
if (i < 0 || i >= e.length) return 0;
|
|
14
|
+
let a = e.getChannelData(t), o = i + 1 < e.length ? a[i + 1] : 0;
|
|
15
|
+
return a[i] + (o - a[i]) * (r - i);
|
|
16
|
+
}
|
|
17
|
+
function c(e, t, { sampleRate: n = 48e3, channels: r = 2, from: c, to: l } = {}) {
|
|
18
|
+
if (o(n, "sampleRate"), r !== 1 && r !== 2) throw RangeError("channels must be 1 or 2");
|
|
19
|
+
let [u, d] = a(e, c, l), f = Math.round((d - u) * n), p = Array.from({ length: r }, () => new Float32Array(f)), m = [...new Set(e.instants.filter((e) => e.type === "audio" && e.kind !== "event").map((e) => Math.ceil((e.time - u) * n - 1e-6)).filter((e) => e > 0 && e < f))].sort((e, t) => e - t);
|
|
20
|
+
m.push(f);
|
|
21
|
+
let h = (e) => u + e / n, g = 0, _ = 0;
|
|
22
|
+
for (; g < f;) {
|
|
23
|
+
for (; m[_] <= g;) _++;
|
|
24
|
+
let n = Math.min(g + i, m[_]), a = n - g, o = h(g + a / 4), c = h(g + 3 * a / 4), l = e.evaluate(o).audio, u = e.evaluate(c).audio;
|
|
25
|
+
l.forEach((e, i) => {
|
|
26
|
+
let a = u[i] ?? e, l = t[e.sound];
|
|
27
|
+
if (!l || !Object.hasOwn(t, e.sound)) throw RangeError(`No sound '${e.sound}' to mix; add it to sounds`);
|
|
28
|
+
let d = c > o ? (a.time - e.time) / (c - o) : 1, f = c > o ? (a.volume - e.volume) / (c - o) : 0;
|
|
29
|
+
for (let t = g; t < n; t++) {
|
|
30
|
+
let n = h(t) - o, i = e.time + d * n, a = Math.max(0, e.volume + f * n), c = l.numberOfChannels > 1;
|
|
31
|
+
r === 1 ? p[0][t] += a * (c ? (s(l, 0, i) + s(l, 1, i)) / 2 : s(l, 0, i)) : (p[0][t] += a * s(l, 0, i), p[1][t] += a * s(l, +!!c, i));
|
|
32
|
+
}
|
|
33
|
+
}), g = n;
|
|
34
|
+
}
|
|
35
|
+
let v = 0;
|
|
36
|
+
for (let e of p) for (let t of e) v = Math.max(v, Math.abs(t));
|
|
37
|
+
return {
|
|
38
|
+
channels: p,
|
|
39
|
+
sampleRate: n,
|
|
40
|
+
peak: v
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function* l(e, { fps: t = 30, from: n, to: i, engine: s } = {}) {
|
|
44
|
+
o(t, "fps");
|
|
45
|
+
let [c, l] = a(e, n, i), d = u(c, l, t);
|
|
46
|
+
s && c > 0 && r(s, c, { fps: t });
|
|
47
|
+
for (let n = 0; n < d; n++) {
|
|
48
|
+
let r = c + n / t;
|
|
49
|
+
yield {
|
|
50
|
+
index: n,
|
|
51
|
+
time: r,
|
|
52
|
+
state: e.evaluate(r)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
var u = (e, t, n) => Math.max(1, Math.ceil((t - e) * n - 1e-9));
|
|
57
|
+
async function d(r, i, s) {
|
|
58
|
+
let { apply: l, camera: d, fps: f = 30, from: p, to: m, sounds: h, sampleRate: g, audioBitrate: _, onProgress: v, signal: y, tracks: b, samples: x = 1, shutter: S = 0, shutterPhase: C, filter: w, filterWidth: T, seed: E, pattern: D, accumulate: O, ...k } = s;
|
|
59
|
+
if (typeof l != "function") throw TypeError("renderCutscene needs apply(state, frame) to put state on your scene");
|
|
60
|
+
o(f, "fps");
|
|
61
|
+
let A = i instanceof t ? i : new t(i instanceof e ? i : new e(i, { tracks: b })), j = A.cutscene, [M, N] = a(j, p, m), P = u(M, N, f), F = h ? c(j, h, {
|
|
62
|
+
sampleRate: g,
|
|
63
|
+
from: M,
|
|
64
|
+
to: N
|
|
65
|
+
}) : void 0, I = x !== 1 || S !== 0, L = {
|
|
66
|
+
samples: x,
|
|
67
|
+
shutter: S,
|
|
68
|
+
shutterPhase: C,
|
|
69
|
+
filter: w,
|
|
70
|
+
filterWidth: T,
|
|
71
|
+
seed: E,
|
|
72
|
+
pattern: D,
|
|
73
|
+
accumulate: O
|
|
74
|
+
}, R = I && M > 0 ? Math.round(M * f) : 0;
|
|
75
|
+
if (M > 0) {
|
|
76
|
+
let e = I ? (await import("@margoengine/headless/accumulate")).subFrameSteps({
|
|
77
|
+
...L,
|
|
78
|
+
fps: f,
|
|
79
|
+
startFrame: R
|
|
80
|
+
}) : void 0;
|
|
81
|
+
await n(r, M, {
|
|
82
|
+
fps: f,
|
|
83
|
+
signal: y,
|
|
84
|
+
steps: e
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
let { encodeVideo: z, renderFrames: B } = await import("@margoengine/headless/video");
|
|
88
|
+
A.seek(M);
|
|
89
|
+
let V = A.play(), H = M, U = async (e) => {
|
|
90
|
+
let { index: t } = e, n = Math.min(N, Math.max(0, M + (I ? e.time : t / f)));
|
|
91
|
+
n > H && (A.skip(n), H = n);
|
|
92
|
+
let r = j.evaluate(n);
|
|
93
|
+
d && r.camera && (d.position = r.camera.position, d.target = r.camera.target, d.fov = r.camera.fov, d.up = r.camera.up), await l(r, {
|
|
94
|
+
index: t,
|
|
95
|
+
time: n,
|
|
96
|
+
state: r
|
|
97
|
+
}), t && !e.sample && v?.(t, P);
|
|
98
|
+
};
|
|
99
|
+
try {
|
|
100
|
+
let e = await z(B(r, {
|
|
101
|
+
frames: P,
|
|
102
|
+
fps: f,
|
|
103
|
+
camera: d,
|
|
104
|
+
update: U,
|
|
105
|
+
signal: y,
|
|
106
|
+
...L,
|
|
107
|
+
startFrame: R
|
|
108
|
+
}), {
|
|
109
|
+
...k,
|
|
110
|
+
fps: f,
|
|
111
|
+
signal: y,
|
|
112
|
+
...F ? { audio: {
|
|
113
|
+
channels: F.channels,
|
|
114
|
+
sampleRate: F.sampleRate,
|
|
115
|
+
bitrate: _
|
|
116
|
+
} } : {}
|
|
117
|
+
});
|
|
118
|
+
return v?.(P, P), A.skip(N), e;
|
|
119
|
+
} finally {
|
|
120
|
+
A.pause(), await V;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
//#endregion
|
|
124
|
+
export { l as cutsceneFrames, c as mixCutsceneAudio, d as renderCutscene };
|
package/dist/scroll.d.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { CutscenePlayer } from './player.js';
|
|
2
|
+
/** Range playback direction. Alternate modes reverse each iteration, starting forward or backward. */
|
|
3
|
+
export type RangeDirection = 'normal' | 'reverse' | 'alternate' | 'alternateReverse';
|
|
4
|
+
/** Playback of one interval of a cutscene. */
|
|
5
|
+
export interface RangePlaybackOptions {
|
|
6
|
+
/** Seconds [from,to], with 0 <= from < to <= duration. Default the entire cutscene. */
|
|
7
|
+
range?: readonly [number, number];
|
|
8
|
+
/** Nonnegative count, including fractional counts or Infinity. Default 1. */
|
|
9
|
+
iterationCount?: number;
|
|
10
|
+
/** Default normal. */
|
|
11
|
+
direction?: RangeDirection;
|
|
12
|
+
/** Positive finite rate. Default 1. */
|
|
13
|
+
speed?: number;
|
|
14
|
+
}
|
|
15
|
+
/** A completed iteration or the end of range playback. */
|
|
16
|
+
export interface RangePlaybackEvent {
|
|
17
|
+
/** Count of fully completed iterations. */
|
|
18
|
+
iteration: number;
|
|
19
|
+
/** Direction of the leg just completed. */
|
|
20
|
+
direction: 1 | -1;
|
|
21
|
+
}
|
|
22
|
+
/** Advances a borrowed player through a range and fires its instants through skip(). Call update(delta) from your loop. */
|
|
23
|
+
export declare class RangePlayback {
|
|
24
|
+
readonly player: CutscenePlayer;
|
|
25
|
+
readonly range: readonly [number, number];
|
|
26
|
+
private readonly count;
|
|
27
|
+
private readonly mode;
|
|
28
|
+
private readonly rate;
|
|
29
|
+
private within;
|
|
30
|
+
private turns;
|
|
31
|
+
private running;
|
|
32
|
+
private ended;
|
|
33
|
+
private disposed;
|
|
34
|
+
private updating;
|
|
35
|
+
private epoch;
|
|
36
|
+
private pending?;
|
|
37
|
+
private readonly listeners;
|
|
38
|
+
constructor(player: CutscenePlayer, options?: RangePlaybackOptions);
|
|
39
|
+
/** True while this controller advances the borrowed player. */
|
|
40
|
+
get playing(): boolean;
|
|
41
|
+
/** Fully completed iterations. */
|
|
42
|
+
get iteration(): number;
|
|
43
|
+
private direction;
|
|
44
|
+
private at;
|
|
45
|
+
/** Starts or resumes. Resolves true at the final endpoint, false on pause; abort rejects and pauses. */
|
|
46
|
+
play({ signal }?: {
|
|
47
|
+
signal?: AbortSignal;
|
|
48
|
+
}): Promise<boolean>;
|
|
49
|
+
/** Stops at the current time, retaining progress for play(). */
|
|
50
|
+
pause(): void;
|
|
51
|
+
/** Pauses and resets the interval to its starting endpoint, with no instants fired. */
|
|
52
|
+
stop(): void;
|
|
53
|
+
/** Listen for each whole iteration or final completion. Returns an unsubscribe function. */
|
|
54
|
+
on(kind: 'iteration' | 'finish', listener: (event: RangePlaybackEvent) => void): () => void;
|
|
55
|
+
private emit;
|
|
56
|
+
/** Advances through all crossed legs in order. At most 1000 boundaries per call; split larger deltas. */
|
|
57
|
+
update(delta: number): void;
|
|
58
|
+
/** Pauses and removes listeners; the borrowed player remains usable. */
|
|
59
|
+
dispose(): void;
|
|
60
|
+
[Symbol.dispose](): void;
|
|
61
|
+
}
|
|
62
|
+
/** Scroll binding, using page or element scroll offsets. */
|
|
63
|
+
export interface ScrollPlaybackOptions {
|
|
64
|
+
/** Default window; an element can be used for an embedded scroll pane. */
|
|
65
|
+
source?: Window | HTMLElement;
|
|
66
|
+
/** Physical axis, default y. */
|
|
67
|
+
axis?: 'x' | 'y';
|
|
68
|
+
/** Scroll offset at range start, pixels. Default 0. */
|
|
69
|
+
start?: number;
|
|
70
|
+
/** Offset at range end, pixels. Default the source's current maximum scroll offset. */
|
|
71
|
+
end?: number;
|
|
72
|
+
/** Timeline seconds. Default [0,duration]. */
|
|
73
|
+
range?: readonly [number, number];
|
|
74
|
+
/** Exponential scrub time constant in seconds. Default 0: immediate. */
|
|
75
|
+
scrub?: number;
|
|
76
|
+
/** Bind scroll/resize events and animate smoothing. Default true; false uses update(delta) only. */
|
|
77
|
+
automatic?: boolean;
|
|
78
|
+
/** Dispose on abort. */
|
|
79
|
+
signal?: AbortSignal;
|
|
80
|
+
}
|
|
81
|
+
/** Seeks a borrowed cutscene player from scroll, firing no timeline instants. */
|
|
82
|
+
export declare class ScrollPlayback {
|
|
83
|
+
readonly player: CutscenePlayer;
|
|
84
|
+
private readonly options;
|
|
85
|
+
private readonly range;
|
|
86
|
+
private readonly source;
|
|
87
|
+
private readonly view;
|
|
88
|
+
private readonly scrub;
|
|
89
|
+
private readonly abort;
|
|
90
|
+
private frame;
|
|
91
|
+
private stamp?;
|
|
92
|
+
private disposed;
|
|
93
|
+
constructor(player: CutscenePlayer, options?: ScrollPlaybackOptions);
|
|
94
|
+
private target;
|
|
95
|
+
/** Refreshes scroll geometry and schedules smoothing. Call after changing content size without a resize/scroll event. */
|
|
96
|
+
readonly refresh: () => void;
|
|
97
|
+
private readonly tick;
|
|
98
|
+
/** Advances smoothing from current scroll geometry; automatic false lets an app use its own clock. */
|
|
99
|
+
update(delta: number): void;
|
|
100
|
+
/** Cancels the frame and event listeners without disposing the player. */
|
|
101
|
+
readonly dispose: () => void;
|
|
102
|
+
[Symbol.dispose](): void;
|
|
103
|
+
}
|