@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
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
import { interpolateQuaternions as e } from "@margoengine/math/quat";
|
|
2
|
+
import { cubicBezier as t, easeInBack as n, easeInBounce as r, easeInCirc as i, easeInCubic as a, easeInElastic as o, easeInExpo as s, easeInOutBack as c, easeInOutBounce as l, easeInOutCirc as u, easeInOutCubic as d, easeInOutExpo as f, easeInOutQuad as p, easeInOutQuart as m, easeInOutQuint as h, easeInOutSine as g, easeInQuad as _, easeInQuart as v, easeInQuint as y, easeInSine as b, easeOutBack as x, easeOutBounce as S, easeOutCirc as C, easeOutCubic as w, easeOutElastic as ee, easeOutExpo as te, easeOutQuad as ne, easeOutQuart as T, easeOutQuint as E, easeOutSine as D, easeSmoothstep as O, linear as k } from "@margoengine/tween";
|
|
3
|
+
//#region src/document.ts
|
|
4
|
+
var A = 16, j = 1e6, M = (e) => typeof e == "object" && !!e && !Array.isArray(e), N = (e) => typeof e == "number" && Number.isFinite(e), P = (e, t) => Array.isArray(e) && e.length === t && e.every(N), F = [
|
|
5
|
+
"linear",
|
|
6
|
+
"step",
|
|
7
|
+
"smoothstep"
|
|
8
|
+
];
|
|
9
|
+
function I(e, t = /* @__PURE__ */ new Set(), n = /* @__PURE__ */ new Map()) {
|
|
10
|
+
let r = [], i = (e, t) => {
|
|
11
|
+
r.length < 1e3 && r.push({
|
|
12
|
+
path: e,
|
|
13
|
+
message: t
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
if (!M(e)) return i("", "must be a cutscene document {format: \"margo-cutscene\", version: 1, duration, tracks}"), r;
|
|
17
|
+
e.format !== "margo-cutscene" && i("format", "must be 'margo-cutscene'; this is not a cutscene document"), e.version !== 1 && i("version", "must be 1; this file is from a newer version, update @margoengine/cutscene");
|
|
18
|
+
let a = 0, o = M(e.timelines) ? e.timelines : {};
|
|
19
|
+
"timelines" in e && !M(e.timelines) && i("timelines", "must be an object of name: timeline");
|
|
20
|
+
let s = (e, n) => {
|
|
21
|
+
e !== void 0 && (typeof e == "string" ? !F.includes(e) && !t.has(e) && i(n, `unknown easing '${e}'; use linear, step, smoothstep, an ease* name of @margoengine/tween or [x1, y1, x2, y2]`) : (!P(e, 4) || e[0] < 0 || e[0] > 1 || e[2] < 0 || e[2] > 1) && i(n, "must be an easing name or cubic-bezier [x1, y1, x2, y2] with x1 and x2 in [0, 1]"));
|
|
22
|
+
}, c = (e, t) => {
|
|
23
|
+
let r = (e) => t ? `${t}.${e}` : e;
|
|
24
|
+
if (!M(e)) {
|
|
25
|
+
i(t, "must be a timeline {duration, tracks}");
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
let c = e.duration, l = N(c) && c > 0;
|
|
29
|
+
l || i(r("duration"), "must be a finite number of seconds above 0");
|
|
30
|
+
let u = l ? c : Infinity, d = (e, t) => !N(e) || e < 0 || e > u ? (i(t, `must be a time in seconds from 0 to the duration (${l ? c : "duration"})`), !1) : !0, f = (e, t, n) => {
|
|
31
|
+
for (let r = 1; r < e.length; r++) N(e[r][n]) && N(e[r - 1][n]) && e[r][n] < e[r - 1][n] && i(`${t}[${r}].${n}`, `is earlier than the one before; sort the list by ${n}`);
|
|
32
|
+
}, p = (e, t) => {
|
|
33
|
+
if (!Array.isArray(e)) return i(t, "must be an array"), null;
|
|
34
|
+
if (a += e.length, a > 1e6) return i(t, `the document has more than ${j} keys, clips and events; split it`), null;
|
|
35
|
+
let n = [];
|
|
36
|
+
return e.forEach((e, r) => {
|
|
37
|
+
M(e) ? n.push(e) : i(`${t}[${r}]`, "must be an object");
|
|
38
|
+
}), n.length === e.length ? n : null;
|
|
39
|
+
};
|
|
40
|
+
if ("markers" in e) {
|
|
41
|
+
let t = p(e.markers, r("markers"));
|
|
42
|
+
t?.forEach((e, t) => {
|
|
43
|
+
d(e.time, r(`markers[${t}].time`)), (typeof e.name != "string" || !e.name) && i(r(`markers[${t}].name`), "must be a non-empty string");
|
|
44
|
+
}), t && f(t, r("markers"), "time");
|
|
45
|
+
}
|
|
46
|
+
let m = (e, t) => {
|
|
47
|
+
let n = p(e, t);
|
|
48
|
+
n && !n.length && i(t, "must hold at least one key"), n?.forEach((e, n) => {
|
|
49
|
+
let r = `${t}[${n}]`;
|
|
50
|
+
d(e.time, `${r}.time`), P(e.position, 3) || i(`${r}.position`, "must be [x, y, z] with finite numbers"), P(e.target, 3) || i(`${r}.target`, "must be [x, y, z] with finite numbers"), "fov" in e && !(N(e.fov) && e.fov > 0 && e.fov < 180) && i(`${r}.fov`, "must be degrees between 0 and 180"), "roll" in e && !N(e.roll) && i(`${r}.roll`, "must be a finite number of radians"), "cut" in e && typeof e.cut != "boolean" && i(`${r}.cut`, "must be true or false"), s(e.ease, `${r}.ease`);
|
|
51
|
+
}), n && f(n, t, "time");
|
|
52
|
+
}, h = p(e.tracks, r("tracks"));
|
|
53
|
+
if (!h) return;
|
|
54
|
+
let g = /* @__PURE__ */ new Set();
|
|
55
|
+
h.forEach((e, t) => {
|
|
56
|
+
let a = r(`tracks[${t}]`);
|
|
57
|
+
typeof e.id != "string" || !e.id ? i(`${a}.id`, "must be a non-empty string") : g.has(e.id) ? i(`${a}.id`, `'${e.id}' is used by another track; give each track its own id`) : g.add(e.id), "muted" in e && typeof e.muted != "boolean" && i(`${a}.muted`, "must be true or false");
|
|
58
|
+
let c = () => {
|
|
59
|
+
(typeof e.target != "string" || !e.target) && i(`${a}.target`, "must name the object, a non-empty string");
|
|
60
|
+
}, l = (t) => {
|
|
61
|
+
let n = p(e.clips, `${a}.clips`);
|
|
62
|
+
return n?.forEach((e, n) => {
|
|
63
|
+
let r = `${a}.clips[${n}]`, o = d(e.start, `${r}.start`);
|
|
64
|
+
!N(e.duration) || e.duration <= 0 ? i(`${r}.duration`, "must be a finite number of seconds above 0") : o && e.start + e.duration > u + 1e-9 && i(`${r}.duration`, "runs past the end of its timeline; shorten it or raise the duration"), t(e, r);
|
|
65
|
+
}), n && f(n, `${a}.clips`, "start"), n;
|
|
66
|
+
}, h = (e, t, n, r = !0) => {
|
|
67
|
+
t in e && !(N(e[t]) && (r ? e[t] >= 0 : e[t] > 0)) && i(`${n}.${t}`, `must be a finite number ${r ? "of 0 or more" : "above 0"}`);
|
|
68
|
+
};
|
|
69
|
+
switch (e.type) {
|
|
70
|
+
case "camera":
|
|
71
|
+
"path" in e && e.path !== "catmull-rom" && e.path !== "linear" && i(`${a}.path`, "must be 'catmull-rom' or 'linear'"), m(e.keys, `${a}.keys`);
|
|
72
|
+
break;
|
|
73
|
+
case "transform": {
|
|
74
|
+
c();
|
|
75
|
+
let t = p(e.keys, `${a}.keys`);
|
|
76
|
+
t && !t.length && i(`${a}.keys`, "must hold at least one key"), t?.forEach((e, t) => {
|
|
77
|
+
let n = `${a}.keys[${t}]`;
|
|
78
|
+
d(e.time, `${n}.time`), !("position" in e) && !("rotation" in e) && !("scale" in e) && i(n, "must set position, rotation or scale"), "position" in e && !P(e.position, 3) && i(`${n}.position`, "must be [x, y, z] with finite numbers"), "scale" in e && !P(e.scale, 3) && i(`${n}.scale`, "must be [x, y, z] with finite numbers"), "rotation" in e && (!P(e.rotation, 4) || Math.hypot(...e.rotation) < 1e-6) && i(`${n}.rotation`, "must be a unit quaternion [x, y, z, w]"), s(e.ease, `${n}.ease`);
|
|
79
|
+
}), t && f(t, `${a}.keys`, "time");
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
case "property": {
|
|
83
|
+
c(), (typeof e.property != "string" || !e.property) && i(`${a}.property`, "must name the property, a non-empty string");
|
|
84
|
+
let t = p(e.keys, `${a}.keys`);
|
|
85
|
+
t && !t.length && i(`${a}.keys`, "must hold at least one key");
|
|
86
|
+
let n = -2;
|
|
87
|
+
t?.forEach((e, t) => {
|
|
88
|
+
let r = `${a}.keys[${t}]`;
|
|
89
|
+
d(e.time, `${r}.time`);
|
|
90
|
+
let o = e.value, c = N(o) ? -1 : Array.isArray(o) && o.length && o.every(N) ? o.length : -3;
|
|
91
|
+
c === -3 ? i(`${r}.value`, "must be a finite number or a non-empty array of finite numbers") : n !== -2 && c !== n ? i(`${r}.value`, "must have the same shape as the first key (a number, or an array of the same length)") : n = c, s(e.ease, `${r}.ease`);
|
|
92
|
+
}), t && f(t, `${a}.keys`, "time");
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
case "animation":
|
|
96
|
+
c(), l((e, t) => {
|
|
97
|
+
(typeof e.clip != "string" || !e.clip) && i(`${t}.clip`, "must name the animation clip"), h(e, "speed", t, !1), h(e, "offset", t), h(e, "blendIn", t), h(e, "blendOut", t), "loop" in e && typeof e.loop != "boolean" && i(`${t}.loop`, "must be true or false"), e.loop === !0 && !(N(e.length) && e.length > 0) ? i(`${t}.length`, "must be the clip length in seconds (above 0) when loop is true") : h(e, "length", t, !1);
|
|
98
|
+
});
|
|
99
|
+
break;
|
|
100
|
+
case "audio":
|
|
101
|
+
l((e, t) => {
|
|
102
|
+
(typeof e.sound != "string" || !e.sound) && i(`${t}.sound`, "must name the sound"), h(e, "volume", t), h(e, "offset", t), h(e, "fadeIn", t), h(e, "fadeOut", t);
|
|
103
|
+
});
|
|
104
|
+
break;
|
|
105
|
+
case "subtitle":
|
|
106
|
+
l((e, t) => {
|
|
107
|
+
typeof e.text != "string" && i(`${t}.text`, "must be a string"), "speaker" in e && typeof e.speaker != "string" && i(`${t}.speaker`, "must be a string");
|
|
108
|
+
});
|
|
109
|
+
break;
|
|
110
|
+
case "event": {
|
|
111
|
+
let t = p(e.events, `${a}.events`);
|
|
112
|
+
t?.forEach((e, t) => {
|
|
113
|
+
d(e.time, `${a}.events[${t}].time`), (typeof e.name != "string" || !e.name) && i(`${a}.events[${t}].name`, "must be a non-empty string");
|
|
114
|
+
}), t && f(t, `${a}.events`, "time");
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
case "timeline":
|
|
118
|
+
l((e, t) => {
|
|
119
|
+
(typeof e.timeline != "string" || !Object.hasOwn(o, e.timeline)) && i(`${t}.timeline`, `must name a timeline in the document's timelines (${Object.keys(o).join(", ") || "none defined"})`), h(e, "speed", t, !1), h(e, "offset", t);
|
|
120
|
+
});
|
|
121
|
+
break;
|
|
122
|
+
default: {
|
|
123
|
+
let t = typeof e.type == "string" ? n.get(e.type) : void 0;
|
|
124
|
+
t ? t.validate(e, {
|
|
125
|
+
path: a,
|
|
126
|
+
end: u,
|
|
127
|
+
add: i,
|
|
128
|
+
time: d,
|
|
129
|
+
ease: s,
|
|
130
|
+
list: p,
|
|
131
|
+
ordered: f,
|
|
132
|
+
target: c,
|
|
133
|
+
clips: l,
|
|
134
|
+
positive: h,
|
|
135
|
+
cameraKeys: m
|
|
136
|
+
}) : i(`${a}.type`, `unknown track type ${JSON.stringify(e.type)}; use camera, transform, property, animation, audio, subtitle, event or timeline, or pass its custom track type in the tracks option`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
};
|
|
141
|
+
c(e, "");
|
|
142
|
+
for (let [e, t] of Object.entries(o)) c(t, `timelines.${e}`);
|
|
143
|
+
let l = (e) => {
|
|
144
|
+
let t = o[e];
|
|
145
|
+
return !M(t) || !Array.isArray(t.tracks) ? [] : t.tracks.flatMap((e) => M(e) && e.type === "timeline" && Array.isArray(e.clips) ? e.clips.flatMap((e) => M(e) && typeof e.timeline == "string" && Object.hasOwn(o, e.timeline) ? [e.timeline] : []) : []);
|
|
146
|
+
}, u = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map(), f = (e, t) => {
|
|
147
|
+
if (u.get(e) === 2) return d.get(e);
|
|
148
|
+
if (u.get(e) === 1) return i(`timelines.${e}`, `nests itself (${[...t, e].join(" > ")}); remove the cycle`), 0;
|
|
149
|
+
u.set(e, 1);
|
|
150
|
+
let n = 0;
|
|
151
|
+
for (let r of l(e)) n = Math.max(n, 1 + f(r, [...t, e]));
|
|
152
|
+
return u.set(e, 2), d.set(e, n), n;
|
|
153
|
+
};
|
|
154
|
+
for (let e of Object.keys(o)) f(e, []) + 1 > 16 && i(`timelines.${e}`, "nests deeper than 16 levels; flatten it");
|
|
155
|
+
return r;
|
|
156
|
+
}
|
|
157
|
+
var L = (e) => {
|
|
158
|
+
if (e && typeof e == "object" && !Object.isFrozen(e)) {
|
|
159
|
+
Object.freeze(e);
|
|
160
|
+
for (let t of Object.values(e)) L(t);
|
|
161
|
+
}
|
|
162
|
+
return e;
|
|
163
|
+
};
|
|
164
|
+
function R(e, t, n) {
|
|
165
|
+
let r;
|
|
166
|
+
try {
|
|
167
|
+
r = JSON.parse(typeof e == "string" ? e : JSON.stringify(e));
|
|
168
|
+
} catch (e) {
|
|
169
|
+
throw TypeError(`Cutscene is not valid JSON (${e.message}); save it with JSON.stringify(document)`);
|
|
170
|
+
}
|
|
171
|
+
let i = I(r, t, n);
|
|
172
|
+
if (i.length) throw TypeError(`Invalid cutscene; fix ${i[0].path || "the document"}: ${i[0].message}${i.length > 1 ? ` (and ${i.length - 1} more; see validateCutscene)` : ""}`);
|
|
173
|
+
return L(r);
|
|
174
|
+
}
|
|
175
|
+
//#endregion
|
|
176
|
+
//#region src/cutscene.ts
|
|
177
|
+
var z = {
|
|
178
|
+
easeInBack: n,
|
|
179
|
+
easeInBounce: r,
|
|
180
|
+
easeInCirc: i,
|
|
181
|
+
easeInCubic: a,
|
|
182
|
+
easeInElastic: o,
|
|
183
|
+
easeInExpo: s,
|
|
184
|
+
easeInOutBack: c,
|
|
185
|
+
easeInOutBounce: l,
|
|
186
|
+
easeInOutCirc: u,
|
|
187
|
+
easeInOutCubic: d,
|
|
188
|
+
easeInOutExpo: f,
|
|
189
|
+
easeInOutQuad: p,
|
|
190
|
+
easeInOutQuart: m,
|
|
191
|
+
easeInOutQuint: h,
|
|
192
|
+
easeInOutSine: g,
|
|
193
|
+
easeInQuad: _,
|
|
194
|
+
easeInQuart: v,
|
|
195
|
+
easeInQuint: y,
|
|
196
|
+
easeInSine: b,
|
|
197
|
+
easeOutBack: x,
|
|
198
|
+
easeOutBounce: S,
|
|
199
|
+
easeOutCirc: C,
|
|
200
|
+
easeOutCubic: w,
|
|
201
|
+
easeOutElastic: ee,
|
|
202
|
+
easeOutExpo: te,
|
|
203
|
+
easeOutQuad: ne,
|
|
204
|
+
easeOutQuart: T,
|
|
205
|
+
easeOutQuint: E,
|
|
206
|
+
easeOutSine: D,
|
|
207
|
+
easeSmoothstep: O
|
|
208
|
+
}, B = /* @__PURE__ */ new Set(/* @__PURE__ */ F.concat(/* @__PURE__ */ Object.keys(z))), V = (e) => e < 1 ? 0 : 1, H = (e) => e === void 0 || e === "linear" ? k : e === "step" ? V : e === "smoothstep" ? O : typeof e == "string" ? z[e] : t(e[0], e[1], e[2], e[3]), U = {
|
|
209
|
+
exit: 0,
|
|
210
|
+
marker: 1,
|
|
211
|
+
event: 2,
|
|
212
|
+
enter: 3
|
|
213
|
+
};
|
|
214
|
+
function W(e, t) {
|
|
215
|
+
let n = 0, r = e.length - 1, i = -1;
|
|
216
|
+
for (; n <= r;) {
|
|
217
|
+
let a = n + r >> 1;
|
|
218
|
+
e[a].time <= t ? (i = a, n = a + 1) : r = a - 1;
|
|
219
|
+
}
|
|
220
|
+
return i;
|
|
221
|
+
}
|
|
222
|
+
function G(e, t, n) {
|
|
223
|
+
let r = W(e, n);
|
|
224
|
+
if (r < 0) return {
|
|
225
|
+
i: 0,
|
|
226
|
+
j: 0,
|
|
227
|
+
u: 0
|
|
228
|
+
};
|
|
229
|
+
if (r >= e.length - 1) return {
|
|
230
|
+
i: r,
|
|
231
|
+
j: r,
|
|
232
|
+
u: 0
|
|
233
|
+
};
|
|
234
|
+
let i = e[r], a = e[r + 1];
|
|
235
|
+
if (a.cut) return {
|
|
236
|
+
i: r,
|
|
237
|
+
j: r,
|
|
238
|
+
u: 0
|
|
239
|
+
};
|
|
240
|
+
let o = a.time - i.time;
|
|
241
|
+
return {
|
|
242
|
+
i: r,
|
|
243
|
+
j: r + 1,
|
|
244
|
+
u: o > 0 ? t[r + 1](Math.min(1, Math.max(0, (n - i.time) / o))) : 1
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
var K = (e, t, n) => e.map((e, r) => e + (t[r] - e) * n), q = (e, t, n, r, i) => {
|
|
248
|
+
let a = i * i, o = a * i;
|
|
249
|
+
return t.map((s, c) => .5 * (2 * t[c] + (-e[c] + n[c]) * i + (2 * e[c] - 5 * t[c] + 4 * n[c] - r[c]) * a + (-e[c] + 3 * t[c] - 3 * n[c] + r[c]) * o));
|
|
250
|
+
};
|
|
251
|
+
function J(e, t, n) {
|
|
252
|
+
let r = t[0] - e[0], i = t[1] - e[1], a = t[2] - e[2], o = Math.hypot(r, i, a);
|
|
253
|
+
if (o < 1e-12) return [
|
|
254
|
+
0,
|
|
255
|
+
1,
|
|
256
|
+
0
|
|
257
|
+
];
|
|
258
|
+
r /= o, i /= o, a /= o;
|
|
259
|
+
let s = 0, c = 1, l = 0;
|
|
260
|
+
Math.abs(i) > 1 - 1e-9 && (c = 0, l = -1);
|
|
261
|
+
let u = s * r + c * i + l * a;
|
|
262
|
+
s -= u * r, c -= u * i, l -= u * a;
|
|
263
|
+
let d = Math.hypot(s, c, l);
|
|
264
|
+
if (s /= d, c /= d, l /= d, !n) return [
|
|
265
|
+
s,
|
|
266
|
+
c,
|
|
267
|
+
l
|
|
268
|
+
];
|
|
269
|
+
let f = Math.cos(n), p = Math.sin(n), m = i * l - a * c, h = a * s - r * l, g = r * c - i * s;
|
|
270
|
+
return [
|
|
271
|
+
s * f + m * p,
|
|
272
|
+
c * f + h * p,
|
|
273
|
+
l * f + g * p
|
|
274
|
+
];
|
|
275
|
+
}
|
|
276
|
+
function Y(e, t) {
|
|
277
|
+
let n = /* @__PURE__ */ new Map(), r = (e) => {
|
|
278
|
+
let t = n.get(e);
|
|
279
|
+
if (t) return t;
|
|
280
|
+
t = {
|
|
281
|
+
spec: e,
|
|
282
|
+
eases: /* @__PURE__ */ new Map(),
|
|
283
|
+
channels: /* @__PURE__ */ new Map(),
|
|
284
|
+
instants: []
|
|
285
|
+
}, n.set(e, t);
|
|
286
|
+
for (let n of e.tracks) if ((n.type === "camera" || n.type === "transform" || n.type === "property") && t.eases.set(n, n.keys.map((e) => H(e.ease))), n.type === "transform") {
|
|
287
|
+
let e = (e) => n.keys.flatMap((t, n) => t[e] ? [n] : []);
|
|
288
|
+
t.channels.set(n, {
|
|
289
|
+
position: e("position"),
|
|
290
|
+
rotation: e("rotation"),
|
|
291
|
+
scale: e("scale")
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
return t;
|
|
295
|
+
}, i = (n, r) => {
|
|
296
|
+
let a = [];
|
|
297
|
+
if (!r) for (let e of n.markers ?? []) a.push({
|
|
298
|
+
time: e.time,
|
|
299
|
+
kind: "marker",
|
|
300
|
+
name: e.name,
|
|
301
|
+
track: "",
|
|
302
|
+
type: "marker"
|
|
303
|
+
});
|
|
304
|
+
for (let o of n.tracks) {
|
|
305
|
+
if (o.muted) continue;
|
|
306
|
+
let n = r + o.id;
|
|
307
|
+
if (o.type === "event") for (let e of o.events) a.push({
|
|
308
|
+
time: e.time,
|
|
309
|
+
kind: "event",
|
|
310
|
+
name: e.name,
|
|
311
|
+
track: n,
|
|
312
|
+
type: "event",
|
|
313
|
+
...e.data === void 0 ? {} : { data: e.data }
|
|
314
|
+
});
|
|
315
|
+
else if (t.has(o.type)) for (let { clip: e, ...r } of t.get(o.type).instants?.(o) ?? []) a.push({
|
|
316
|
+
...r,
|
|
317
|
+
track: n,
|
|
318
|
+
type: o.type,
|
|
319
|
+
...e ? {
|
|
320
|
+
clip: e,
|
|
321
|
+
span: [e.start, e.start + e.duration]
|
|
322
|
+
} : {}
|
|
323
|
+
});
|
|
324
|
+
else if (o.type === "animation" || o.type === "audio" || o.type === "subtitle" || o.type === "timeline") for (let t of o.clips) {
|
|
325
|
+
let r = "clip" in t ? t.clip : "sound" in t ? t.sound : "text" in t ? t.text : t.timeline, s = [t.start, t.start + t.duration];
|
|
326
|
+
if (a.push({
|
|
327
|
+
time: t.start,
|
|
328
|
+
kind: "enter",
|
|
329
|
+
name: r,
|
|
330
|
+
track: n,
|
|
331
|
+
type: o.type,
|
|
332
|
+
clip: t,
|
|
333
|
+
span: s
|
|
334
|
+
}), o.type === "timeline") {
|
|
335
|
+
let r = t, o = e.timelines[r.timeline], s = r.speed ?? 1, c = r.offset ?? 0, l = Math.min(o.duration, c + t.duration * s), u = (e) => t.start + (e - c) / s;
|
|
336
|
+
for (let e of i(o, `${n}/`)) if (e.span) {
|
|
337
|
+
let t = Math.max(e.span[0], c), n = Math.min(e.span[1], l);
|
|
338
|
+
if (!(n > t)) continue;
|
|
339
|
+
let r = [u(t), u(n)];
|
|
340
|
+
a.push({
|
|
341
|
+
...e,
|
|
342
|
+
time: e.kind === "enter" ? r[0] : r[1],
|
|
343
|
+
span: r
|
|
344
|
+
});
|
|
345
|
+
} else e.time >= c && e.time <= l && a.push({
|
|
346
|
+
...e,
|
|
347
|
+
time: u(e.time)
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
a.push({
|
|
351
|
+
time: t.start + t.duration,
|
|
352
|
+
kind: "exit",
|
|
353
|
+
name: r,
|
|
354
|
+
track: n,
|
|
355
|
+
type: o.type,
|
|
356
|
+
clip: t,
|
|
357
|
+
span: s
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
return a;
|
|
362
|
+
}, a = r(e);
|
|
363
|
+
for (let t of Object.values(e.timelines ?? {})) r(t);
|
|
364
|
+
return a.instants = i(e, "").map(({ span: e, ...t }, n) => ({
|
|
365
|
+
...t,
|
|
366
|
+
order: n
|
|
367
|
+
})).sort((e, t) => e.time - t.time || U[e.kind] - U[t.kind] || e.order - t.order).map((e, t) => Object.freeze({
|
|
368
|
+
...e,
|
|
369
|
+
order: t
|
|
370
|
+
})), n;
|
|
371
|
+
}
|
|
372
|
+
function X(e, t, n, r, i) {
|
|
373
|
+
let { i: a, j: o, u: s } = G(e, t, r), c = e[a], l = e[o], u, d;
|
|
374
|
+
if (a === o) u = [...c.position], d = [...c.target];
|
|
375
|
+
else if (n === "linear") u = K(c.position, l.position, s), d = K(c.target, l.target, s);
|
|
376
|
+
else {
|
|
377
|
+
let t = a > 0 && !c.cut ? e[a - 1] : c, n = o + 1 < e.length && !e[o + 1].cut ? e[o + 1] : l;
|
|
378
|
+
u = q(t.position, c.position, l.position, n.position, s), d = q(t.target, c.target, l.target, n.target, s);
|
|
379
|
+
}
|
|
380
|
+
let f = c.fov ?? 60, p = l.fov ?? 60, m = c.roll ?? 0, h = m + ((l.roll ?? 0) - m) * s;
|
|
381
|
+
return {
|
|
382
|
+
position: u,
|
|
383
|
+
target: d,
|
|
384
|
+
fov: f + (p - f) * s,
|
|
385
|
+
roll: h,
|
|
386
|
+
up: J(u, d, h),
|
|
387
|
+
track: i
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
function Z(e, t, n) {
|
|
391
|
+
Object.defineProperty(e, t, {
|
|
392
|
+
value: n,
|
|
393
|
+
writable: !0,
|
|
394
|
+
enumerable: !0,
|
|
395
|
+
configurable: !0
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
function Q(e, t) {
|
|
399
|
+
if (Object.hasOwn(e, t)) return e[t];
|
|
400
|
+
let n = {};
|
|
401
|
+
return Z(e, t, n), n;
|
|
402
|
+
}
|
|
403
|
+
var $ = class {
|
|
404
|
+
constructor(e, t = {}) {
|
|
405
|
+
this.types = new Map((t.tracks ?? []).map((e) => [e.type, e])), this.document = R(e, B, this.types), this.compiled = Y(this.document, this.types), this.instants = Object.freeze(this.compiled.get(this.document).instants);
|
|
406
|
+
}
|
|
407
|
+
get duration() {
|
|
408
|
+
return this.document.duration;
|
|
409
|
+
}
|
|
410
|
+
evaluate(e) {
|
|
411
|
+
if (typeof e != "number" || Number.isNaN(e)) throw RangeError("Cutscene time must be a number of seconds; pass a finite time");
|
|
412
|
+
let t = Math.min(this.duration, Math.max(0, e)), n = {
|
|
413
|
+
time: t,
|
|
414
|
+
camera: null,
|
|
415
|
+
transforms: {},
|
|
416
|
+
properties: {},
|
|
417
|
+
animations: [],
|
|
418
|
+
audio: [],
|
|
419
|
+
subtitles: [],
|
|
420
|
+
marker: null
|
|
421
|
+
}, r = this.document.markers ?? [], i = W(r, t);
|
|
422
|
+
i >= 0 && (n.marker = r[i].name);
|
|
423
|
+
let a = [];
|
|
424
|
+
this.timeline(this.document, t, "", n, a);
|
|
425
|
+
for (let { track: e, local: t, path: r } of a) this.types.get(e.type).evaluate(e, t, n, r);
|
|
426
|
+
return n;
|
|
427
|
+
}
|
|
428
|
+
eventsBetween(e, t) {
|
|
429
|
+
if (Number.isNaN(e) || Number.isNaN(t) || typeof e != "number" || typeof t != "number") throw RangeError("Cutscene times must be numbers of seconds; pass finite times");
|
|
430
|
+
let n = this.duration, r = Math.min(n, Math.max(0, e)), i = Math.min(n, Math.max(0, t)), a = this.instants;
|
|
431
|
+
return r === i ? [] : r < i ? a.filter((e) => e.time >= r && (e.time < i || i === n && e.time === n)) : a.filter((e) => e.time <= r && (e.time > i || i === 0 && e.time === 0)).reverse();
|
|
432
|
+
}
|
|
433
|
+
timeline(t, n, r, i, a) {
|
|
434
|
+
let o = this.compiled.get(t);
|
|
435
|
+
for (let s of t.tracks) {
|
|
436
|
+
if (s.muted) continue;
|
|
437
|
+
let t = r + s.id;
|
|
438
|
+
switch (s.type) {
|
|
439
|
+
case "camera":
|
|
440
|
+
i.camera ||= X(s.keys, o.eases.get(s), s.path ?? "catmull-rom", n, t);
|
|
441
|
+
break;
|
|
442
|
+
case "transform": {
|
|
443
|
+
let t = o.channels.get(s), r = o.eases.get(s), a = Q(i.transforms, s.target);
|
|
444
|
+
for (let i of [
|
|
445
|
+
"position",
|
|
446
|
+
"rotation",
|
|
447
|
+
"scale"
|
|
448
|
+
]) {
|
|
449
|
+
let o = t[i];
|
|
450
|
+
if (!o.length) continue;
|
|
451
|
+
let c = o.map((e) => s.keys[e]), { i: l, j: u, u: d } = G(c, o.map((e) => r[e]), n), f = c[l][i], p = c[u][i];
|
|
452
|
+
if (i === "rotation") {
|
|
453
|
+
let t = l === u ? [...f] : e(f, p, d, .9995), n = Math.hypot(t[0], t[1], t[2], t[3]);
|
|
454
|
+
a.rotation = [
|
|
455
|
+
t[0] / n,
|
|
456
|
+
t[1] / n,
|
|
457
|
+
t[2] / n,
|
|
458
|
+
t[3] / n
|
|
459
|
+
];
|
|
460
|
+
} else a[i] = K(f, p, d);
|
|
461
|
+
}
|
|
462
|
+
break;
|
|
463
|
+
}
|
|
464
|
+
case "property": {
|
|
465
|
+
let { i: e, j: t, u: r } = G(s.keys, o.eases.get(s), n), a = s.keys[e].value, c = s.keys[t].value;
|
|
466
|
+
Z(Q(i.properties, s.target), s.property, typeof a == "number" ? a + (c - a) * r : K(a, c, r));
|
|
467
|
+
break;
|
|
468
|
+
}
|
|
469
|
+
case "animation":
|
|
470
|
+
for (let e of s.clips) {
|
|
471
|
+
if (!(n >= e.start && n < e.start + e.duration)) continue;
|
|
472
|
+
let r = n - e.start, a = e.speed ?? 1, o = (e.offset ?? 0) + r * a;
|
|
473
|
+
e.loop && e.length ? o %= e.length : e.length !== void 0 && (o = Math.min(o, e.length));
|
|
474
|
+
let c = e.blendIn ? r / e.blendIn : 1, l = e.blendOut ? (e.duration - r) / e.blendOut : 1;
|
|
475
|
+
i.animations.push({
|
|
476
|
+
target: s.target,
|
|
477
|
+
clip: e.clip,
|
|
478
|
+
time: o,
|
|
479
|
+
weight: Math.max(0, Math.min(1, c, l)),
|
|
480
|
+
track: t
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
break;
|
|
484
|
+
case "audio":
|
|
485
|
+
for (let e of s.clips) {
|
|
486
|
+
if (!(n >= e.start && n < e.start + e.duration)) continue;
|
|
487
|
+
let r = n - e.start, a = e.fadeIn ? r / e.fadeIn : 1, o = e.fadeOut ? (e.duration - r) / e.fadeOut : 1;
|
|
488
|
+
i.audio.push({
|
|
489
|
+
sound: e.sound,
|
|
490
|
+
time: (e.offset ?? 0) + r,
|
|
491
|
+
volume: (e.volume ?? 1) * Math.max(0, Math.min(1, a, o)),
|
|
492
|
+
track: t
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
break;
|
|
496
|
+
case "subtitle":
|
|
497
|
+
for (let e of s.clips) n >= e.start && n < e.start + e.duration && i.subtitles.push(e.speaker === void 0 ? {
|
|
498
|
+
text: e.text,
|
|
499
|
+
track: t
|
|
500
|
+
} : {
|
|
501
|
+
text: e.text,
|
|
502
|
+
speaker: e.speaker,
|
|
503
|
+
track: t
|
|
504
|
+
});
|
|
505
|
+
break;
|
|
506
|
+
case "timeline":
|
|
507
|
+
for (let e of s.clips) {
|
|
508
|
+
if (!(n >= e.start && n < e.start + e.duration)) continue;
|
|
509
|
+
let r = this.document.timelines[e.timeline], o = Math.min(r.duration, (e.offset ?? 0) + (n - e.start) * (e.speed ?? 1));
|
|
510
|
+
this.timeline(r, o, `${t}/`, i, a);
|
|
511
|
+
}
|
|
512
|
+
break;
|
|
513
|
+
case "event": break;
|
|
514
|
+
default: a.push({
|
|
515
|
+
track: s,
|
|
516
|
+
local: n,
|
|
517
|
+
path: t
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
function re(e, t, n) {
|
|
524
|
+
return new $(e, n).evaluate(t);
|
|
525
|
+
}
|
|
526
|
+
//#endregion
|
|
527
|
+
export { re as a, j as c, I as d, H as i, A as l, B as n, J as o, X as r, F as s, $ as t, R as u };
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { type AnimationClip, type AudioClip, type CameraKey, type ClipBase, type CutsceneDocument, type EaseSpec, type SubtitleClip, type TimelineClip, type Track, type TrackCheck } from './document.js';
|
|
2
|
+
type Ease = (u: number) => number;
|
|
3
|
+
/** Every easing name a document may use: `linear`, `step`, `smoothstep` and the `ease*` easings of `@margoengine/tween`. */
|
|
4
|
+
export declare const EASING_NAMES: ReadonlySet<string>;
|
|
5
|
+
/** The easing function of a spec the document validator accepted. */
|
|
6
|
+
export declare const easing: (spec: EaseSpec | undefined) => Ease;
|
|
7
|
+
/** Where the camera is at a time. */
|
|
8
|
+
export interface CameraState {
|
|
9
|
+
position: [number, number, number];
|
|
10
|
+
target: [number, number, number];
|
|
11
|
+
/** Vertical field of view, degrees. */
|
|
12
|
+
fov: number;
|
|
13
|
+
/** Roll about the view direction, radians. */
|
|
14
|
+
roll: number;
|
|
15
|
+
/** Screen-up direction with the roll applied (+Y rolled about the view direction), ready for `camera.up`. */
|
|
16
|
+
up: [number, number, number];
|
|
17
|
+
/** Path of the camera track (`intro/cam` inside a nested timeline). */
|
|
18
|
+
track: string;
|
|
19
|
+
}
|
|
20
|
+
/** An object's animated transform channels at a time (only the channels its tracks key). */
|
|
21
|
+
export interface TransformState {
|
|
22
|
+
position?: [number, number, number];
|
|
23
|
+
rotation?: [number, number, number, number];
|
|
24
|
+
scale?: [number, number, number];
|
|
25
|
+
}
|
|
26
|
+
/** An animation clip playing at a time, for your animation mixer. */
|
|
27
|
+
export interface AnimationState {
|
|
28
|
+
target: string;
|
|
29
|
+
clip: string;
|
|
30
|
+
/** Clip time, seconds. */
|
|
31
|
+
time: number;
|
|
32
|
+
/** Blend weight in [0, 1] from blendIn and blendOut. */
|
|
33
|
+
weight: number;
|
|
34
|
+
track: string;
|
|
35
|
+
}
|
|
36
|
+
/** A sound playing at a time: start it at `time` seconds in when you seek. */
|
|
37
|
+
export interface AudioState {
|
|
38
|
+
sound: string;
|
|
39
|
+
/** Seconds into the sound. */
|
|
40
|
+
time: number;
|
|
41
|
+
/** Gain with fades applied. */
|
|
42
|
+
volume: number;
|
|
43
|
+
track: string;
|
|
44
|
+
}
|
|
45
|
+
/** A subtitle on screen at a time. */
|
|
46
|
+
export interface SubtitleState {
|
|
47
|
+
text: string;
|
|
48
|
+
speaker?: string;
|
|
49
|
+
track: string;
|
|
50
|
+
}
|
|
51
|
+
/** Everything a cutscene sets at one time. A new object each call; it shares nothing with the document. */
|
|
52
|
+
export interface CutsceneState {
|
|
53
|
+
/** The time evaluated, clamped to [0, duration]. */
|
|
54
|
+
time: number;
|
|
55
|
+
/** The first unmuted camera track's camera (nested timelines included, in track order), or null. */
|
|
56
|
+
camera: CameraState | null;
|
|
57
|
+
/** Transform channels by target name; a later track wins a channel both key. */
|
|
58
|
+
transforms: Record<string, TransformState>;
|
|
59
|
+
/** Property values by target name, then property name. */
|
|
60
|
+
properties: Record<string, Record<string, number | number[]>>;
|
|
61
|
+
animations: AnimationState[];
|
|
62
|
+
audio: AudioState[];
|
|
63
|
+
subtitles: SubtitleState[];
|
|
64
|
+
/** Name of the last marker at or before `time` (top-level timeline), or null. */
|
|
65
|
+
marker: string | null;
|
|
66
|
+
/** What custom tracks set, under the name each documents. Absent when none did. */
|
|
67
|
+
extras?: Record<string, unknown>;
|
|
68
|
+
}
|
|
69
|
+
/** An instant the player fires: a custom event, a marker, or a clip starting (`enter`) or ending (`exit`). */
|
|
70
|
+
export interface CutsceneEvent {
|
|
71
|
+
/** Time on the top-level timeline, seconds. */
|
|
72
|
+
time: number;
|
|
73
|
+
kind: 'event' | 'marker' | 'enter' | 'exit';
|
|
74
|
+
/** The event or marker name, or the clip's animation, sound, text or timeline name. */
|
|
75
|
+
name: string;
|
|
76
|
+
/** Path of the track (`intro/sfx` inside a nested timeline); empty for a top-level marker. */
|
|
77
|
+
track: string;
|
|
78
|
+
/** Type of that track (`'marker'` for markers; a custom track's own type). */
|
|
79
|
+
type: Track['type'] | 'marker' | (string & {});
|
|
80
|
+
/** A custom event's data. */
|
|
81
|
+
data?: unknown;
|
|
82
|
+
/** The clip of an `enter` or `exit`. */
|
|
83
|
+
clip?: AnimationClip | AudioClip | SubtitleClip | TimelineClip | ClipBase;
|
|
84
|
+
/** Position in the document's event order, ties broken by it. */
|
|
85
|
+
order: number;
|
|
86
|
+
}
|
|
87
|
+
/** An instant of a custom track, on its own timeline's time. */
|
|
88
|
+
export interface TrackInstant {
|
|
89
|
+
time: number;
|
|
90
|
+
kind: 'event' | 'enter' | 'exit';
|
|
91
|
+
name: string;
|
|
92
|
+
data?: unknown;
|
|
93
|
+
/** The clip of an `enter` or `exit`; a nested window then cuts it as it cuts a built-in clip. */
|
|
94
|
+
clip?: ClipBase;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* A track type of your own, or one of an optional entry (`shotTrack()` of `@margoengine/cutscene/shots`). Pass it in
|
|
98
|
+
* the `tracks` option of `Cutscene` or `CutscenePlayer`. It runs after the built-in tracks, in document order, so it
|
|
99
|
+
* reads their `transforms` and `camera`. `evaluate` must keep the rule of `Cutscene.evaluate`: the same time gives the
|
|
100
|
+
* same state.
|
|
101
|
+
*/
|
|
102
|
+
export interface CutsceneTrackType {
|
|
103
|
+
/** The value of `type` on its tracks. */
|
|
104
|
+
type: string;
|
|
105
|
+
/** Reports every problem of a track through `check`. Must not throw. */
|
|
106
|
+
validate(track: Record<string, unknown>, check: TrackCheck): void;
|
|
107
|
+
/** The track's instants, for the player to fire. Default none. */
|
|
108
|
+
instants?(track: Record<string, unknown>): readonly TrackInstant[];
|
|
109
|
+
/** Sets what the track does at `time` on its own timeline. `path` is the track's path. */
|
|
110
|
+
evaluate(track: Record<string, unknown>, time: number, state: CutsceneState, path: string): void;
|
|
111
|
+
/** Fields for an editor key at time. Default no fields; optional track entries can supply step-sampled values. */
|
|
112
|
+
sampleKey?(track: Record<string, unknown>, time: number): Record<string, unknown>;
|
|
113
|
+
}
|
|
114
|
+
/** Options of {@link Cutscene}. */
|
|
115
|
+
export interface CutsceneOptions {
|
|
116
|
+
/** Custom track types the document may use. Default none. */
|
|
117
|
+
tracks?: readonly CutsceneTrackType[];
|
|
118
|
+
}
|
|
119
|
+
/** World +Y made perpendicular to the view and rotated by `roll` about it (Rodrigues' rotation formula). */
|
|
120
|
+
export declare function rolledUp(position: readonly number[], target: readonly number[], roll: number): [number, number, number];
|
|
121
|
+
/** A camera track's keys at time `t`. */
|
|
122
|
+
export declare function cameraAt(keys: readonly CameraKey[], eases: readonly Ease[], path: 'catmull-rom' | 'linear', t: number, track: string): CameraState;
|
|
123
|
+
/**
|
|
124
|
+
* A validated, compiled cutscene. `evaluate(t)` gives the full state at any time as a pure function of the document
|
|
125
|
+
* and `t`; `eventsBetween(from, to)` gives the instants passed between two times, in order. Build one from a
|
|
126
|
+
* {@link CutsceneDocument}, an object or JSON text; play it with {@link CutscenePlayer}.
|
|
127
|
+
*/
|
|
128
|
+
export declare class Cutscene {
|
|
129
|
+
/** The validated, deep-frozen document. */
|
|
130
|
+
readonly document: CutsceneDocument;
|
|
131
|
+
/** Every instant of the cutscene, sorted (time, then exit, marker, event, enter, then document order). */
|
|
132
|
+
readonly instants: readonly CutsceneEvent[];
|
|
133
|
+
private readonly compiled;
|
|
134
|
+
private readonly types;
|
|
135
|
+
/** @throws TypeError naming the first problem in the document and its fix. */
|
|
136
|
+
constructor(document: CutsceneDocument | string | unknown, options?: CutsceneOptions);
|
|
137
|
+
/** Length of the top-level timeline, seconds. */
|
|
138
|
+
get duration(): number;
|
|
139
|
+
/**
|
|
140
|
+
* The state at time `t` (clamped to [0, duration]). Two calls with the same `t` return equal states, whatever
|
|
141
|
+
* happened in between.
|
|
142
|
+
* @throws RangeError for a non-finite time.
|
|
143
|
+
*/
|
|
144
|
+
evaluate(t: number): CutsceneState;
|
|
145
|
+
/**
|
|
146
|
+
* The instants passed going from `from` to `to`, in the order they are passed: forward (from < to) every instant
|
|
147
|
+
* with from <= time < to, and those at the end when `to` reaches it; backward (to < from) every instant with
|
|
148
|
+
* to < time <= from, latest first, and those at 0 when `to` reaches it. Splitting a range anywhere returns the same
|
|
149
|
+
* instants, so a frame rate never changes what fires. Times are clamped to [0, duration].
|
|
150
|
+
* @throws RangeError for a NaN time.
|
|
151
|
+
*/
|
|
152
|
+
eventsBetween(from: number, to: number): CutsceneEvent[];
|
|
153
|
+
private timeline;
|
|
154
|
+
}
|
|
155
|
+
/** The state of `document` at time `t`; builds a {@link Cutscene} each call, so keep one for repeated use. */
|
|
156
|
+
export declare function evaluate(document: CutsceneDocument | string | unknown, t: number, options?: CutsceneOptions): CutsceneState;
|
|
157
|
+
export {};
|