@margoengine/cutscene 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +774 -3
- package/dist/curve-editor.d.ts +79 -0
- package/dist/curve-editor.js +293 -0
- package/dist/cutscene-DAWl8liP.js +496 -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-DtIkr2sQ.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 +124 -6
package/dist/scroll.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
2
|
+
//#region src/scroll.ts
|
|
3
|
+
var t = (e, t = [0, e.duration]) => {
|
|
4
|
+
let [n, r] = t;
|
|
5
|
+
if (!Number.isFinite(n) || !Number.isFinite(r) || n < 0 || n >= r || r > e.duration) throw RangeError("Cutscene range must satisfy 0 <= from < to <= duration; pass an interval in seconds");
|
|
6
|
+
return [n, r];
|
|
7
|
+
}, n = class {
|
|
8
|
+
constructor(e, n = {}) {
|
|
9
|
+
if (this.player = e, this.within = 0, this.turns = 0, this.running = !1, this.ended = !1, this.disposed = !1, this.updating = !1, this.epoch = 0, this.listeners = /* @__PURE__ */ new Map(), this.range = t(e, n.range), this.count = n.iterationCount ?? 1, this.mode = n.direction ?? "normal", this.rate = n.speed ?? 1, !(this.count >= 0) || Number.isNaN(this.count)) throw RangeError("Iteration count must be nonnegative or Infinity; pass a number");
|
|
10
|
+
if (![
|
|
11
|
+
"normal",
|
|
12
|
+
"reverse",
|
|
13
|
+
"alternate",
|
|
14
|
+
"alternateReverse"
|
|
15
|
+
].includes(this.mode)) throw RangeError("Unknown range direction; use normal, reverse, alternate or alternateReverse");
|
|
16
|
+
if (!(this.rate > 0) || !Number.isFinite(this.rate)) throw RangeError("Range speed must be finite and positive; use direction to reverse");
|
|
17
|
+
}
|
|
18
|
+
get playing() {
|
|
19
|
+
return this.running;
|
|
20
|
+
}
|
|
21
|
+
get iteration() {
|
|
22
|
+
return this.turns;
|
|
23
|
+
}
|
|
24
|
+
direction() {
|
|
25
|
+
return (this.mode === "reverse" || this.mode === "alternateReverse") == ((this.mode === "alternate" || this.mode === "alternateReverse") && this.turns % 2 == 1) ? 1 : -1;
|
|
26
|
+
}
|
|
27
|
+
at(e) {
|
|
28
|
+
return this.direction() === 1 ? this.range[0] + e : this.range[1] - e;
|
|
29
|
+
}
|
|
30
|
+
play({ signal: t } = {}) {
|
|
31
|
+
if (this.disposed) throw new e("disposed", "Range playback is disposed; create a new controller");
|
|
32
|
+
return t?.throwIfAborted(), this.pause(), this.player.pause(), this.ended &&= (this.within = 0, this.turns = 0, !1), this.player.seek(this.at(this.within)), this.count === 0 ? (this.ended = !0, Promise.resolve(!0)) : (this.running = !0, new Promise((e, n) => {
|
|
33
|
+
let r = () => {
|
|
34
|
+
this.running = !1, this.pending?.cleanup(), this.pending = void 0, n(t.reason);
|
|
35
|
+
};
|
|
36
|
+
t?.addEventListener("abort", r, { once: !0 }), this.pending = {
|
|
37
|
+
resolve: e,
|
|
38
|
+
reject: n,
|
|
39
|
+
cleanup: () => t?.removeEventListener("abort", r)
|
|
40
|
+
};
|
|
41
|
+
}));
|
|
42
|
+
}
|
|
43
|
+
pause() {
|
|
44
|
+
this.epoch++, this.running = !1;
|
|
45
|
+
let e = this.pending;
|
|
46
|
+
this.pending = void 0, e?.cleanup(), e?.resolve(!1);
|
|
47
|
+
}
|
|
48
|
+
stop() {
|
|
49
|
+
this.pause(), this.within = 0, this.turns = 0, this.ended = !1, this.player.seek(this.at(0));
|
|
50
|
+
}
|
|
51
|
+
on(e, t) {
|
|
52
|
+
if (!["iteration", "finish"].includes(e)) throw RangeError("Unknown range event; use iteration or finish");
|
|
53
|
+
let n = this.listeners.get(e) ?? /* @__PURE__ */ new Set();
|
|
54
|
+
return this.listeners.set(e, n), n.add(t), () => void n.delete(t);
|
|
55
|
+
}
|
|
56
|
+
emit(e, t) {
|
|
57
|
+
for (let n of [...this.listeners.get(e) ?? []]) n({
|
|
58
|
+
iteration: this.turns,
|
|
59
|
+
direction: t
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
update(t) {
|
|
63
|
+
if (this.disposed) throw new e("disposed", "Range playback is disposed; create a new controller");
|
|
64
|
+
if (!(t >= 0) || !Number.isFinite(t)) throw RangeError("Range delta must be finite and nonnegative; pass seconds");
|
|
65
|
+
if (this.updating) throw new e("state", "Range update is already running; update once per frame outside listeners");
|
|
66
|
+
if (!this.running || t === 0) return;
|
|
67
|
+
let n = this.range[1] - this.range[0], r = n * this.count, i = this.epoch, a = Math.min(t * this.rate, r - this.turns * n - this.within);
|
|
68
|
+
if (!Number.isFinite(a) || Math.floor((this.within + a) / n) > 1e3) throw RangeError("Range update crosses more than 1000 boundaries; pass a smaller delta");
|
|
69
|
+
this.updating = !0;
|
|
70
|
+
try {
|
|
71
|
+
for (; this.running && a > 0;) {
|
|
72
|
+
let e = this.direction(), t = Math.min(n, r - this.turns * n), o = Math.min(a, t - this.within);
|
|
73
|
+
this.within += o, a -= o;
|
|
74
|
+
let s = this.at(this.within);
|
|
75
|
+
if (this.player.skip(s), !this.running || this.epoch !== i) return;
|
|
76
|
+
if (this.player.time !== s) {
|
|
77
|
+
this.pause();
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (this.within === t) {
|
|
81
|
+
let a = t === n;
|
|
82
|
+
if (a && this.turns++, this.turns * n + (a ? 0 : this.within) >= r) {
|
|
83
|
+
this.running = !1, this.ended = !0;
|
|
84
|
+
let t = this.pending;
|
|
85
|
+
this.pending = void 0, t?.cleanup(), t?.resolve(!0), a && this.emit("iteration", e), this.emit("finish", e);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (this.within = 0, this.emit("iteration", e), !this.running || this.epoch !== i) return;
|
|
89
|
+
this.player.seek(this.at(0));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
} finally {
|
|
93
|
+
this.updating = !1;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
dispose() {
|
|
97
|
+
this.disposed || (this.pause(), this.disposed = !0, this.listeners.clear());
|
|
98
|
+
}
|
|
99
|
+
[Symbol.dispose]() {
|
|
100
|
+
this.dispose();
|
|
101
|
+
}
|
|
102
|
+
}, r = class {
|
|
103
|
+
constructor(e, n = {}) {
|
|
104
|
+
if (this.player = e, this.options = n, this.abort = new AbortController(), this.frame = 0, this.disposed = !1, this.refresh = () => {
|
|
105
|
+
this.disposed || (this.scrub === 0 ? this.player.seek(this.target()) : this.options.automatic !== !1 && !this.frame && (this.stamp = void 0, this.frame = this.view.requestAnimationFrame(this.tick)));
|
|
106
|
+
}, this.tick = (e) => {
|
|
107
|
+
this.frame = 0, this.update(this.stamp === void 0 ? 0 : (e - this.stamp) / 1e3), this.stamp = e;
|
|
108
|
+
let t = this.target();
|
|
109
|
+
Math.abs(this.player.time - t) > 1e-7 ? this.frame = this.view.requestAnimationFrame(this.tick) : this.player.seek(t);
|
|
110
|
+
}, this.dispose = () => {
|
|
111
|
+
this.disposed || (this.disposed = !0, this.abort.abort(), this.frame && this.view.cancelAnimationFrame(this.frame), this.frame = 0, this.options.signal?.removeEventListener("abort", this.dispose));
|
|
112
|
+
}, n.signal?.throwIfAborted(), this.range = t(e, n.range), this.source = n.source ?? window, this.view = "ownerDocument" in this.source ? this.source.ownerDocument.defaultView : this.source, this.scrub = n.scrub ?? 0, !(this.scrub >= 0) || !Number.isFinite(this.scrub)) throw RangeError("Scroll scrub must be finite and nonnegative; pass seconds");
|
|
113
|
+
if (n.axis !== void 0 && n.axis !== "x" && n.axis !== "y") throw RangeError("Scroll axis must be x or y; choose a physical axis");
|
|
114
|
+
if (!Number.isFinite(n.start ?? 0) || !Number.isFinite(n.end ?? 0) || n.end !== void 0 && n.end <= (n.start ?? 0)) throw RangeError("Scroll offsets must be finite with end above start; pass pixel offsets");
|
|
115
|
+
e.pause(), n.automatic !== !1 && (this.source.addEventListener("scroll", this.refresh, {
|
|
116
|
+
passive: !0,
|
|
117
|
+
signal: this.abort.signal
|
|
118
|
+
}), this.view.addEventListener("resize", this.refresh, { signal: this.abort.signal })), n.signal?.addEventListener("abort", this.dispose, { once: !0 }), this.refresh();
|
|
119
|
+
}
|
|
120
|
+
target() {
|
|
121
|
+
let e = this.options.axis === "x", t = "ownerDocument" in this.source ? this.source : this.source.document.scrollingElement, n = e ? t.scrollLeft : t.scrollTop, r = this.options.start ?? 0, i = this.options.end ?? (e ? t.scrollWidth - t.clientWidth : t.scrollHeight - t.clientHeight), a = i > r ? Math.max(0, Math.min(1, (n - r) / (i - r))) : 0;
|
|
122
|
+
return this.range[0] + (this.range[1] - this.range[0]) * a;
|
|
123
|
+
}
|
|
124
|
+
update(t) {
|
|
125
|
+
if (this.disposed) throw new e("disposed", "Scroll playback is disposed; create a new binding");
|
|
126
|
+
if (!(t >= 0) || !Number.isFinite(t)) throw RangeError("Scroll delta must be finite and nonnegative; pass seconds");
|
|
127
|
+
let n = this.target();
|
|
128
|
+
this.player.seek(this.scrub === 0 ? n : n + (this.player.time - n) * Math.exp(-t / this.scrub));
|
|
129
|
+
}
|
|
130
|
+
[Symbol.dispose]() {
|
|
131
|
+
this.dispose();
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
//#endregion
|
|
135
|
+
export { n as RangePlayback, r as ScrollPlayback };
|
package/dist/seek.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// @ts-ignore optional peer: its types apply when it is installed
|
|
2
|
+
import type { Scene } from '@margoengine/core';
|
|
3
|
+
/** Something with its own clock that a seek can replay: a time-driven plugin, or one passed as `extra`. */
|
|
4
|
+
export interface Clock {
|
|
5
|
+
/** Seconds on this clock since it started. A fresh clock reads 0. */
|
|
6
|
+
readonly time: number;
|
|
7
|
+
/** Moves the clock by `delta` seconds: the CPU part of one frame update, with no GPU call and no listener call. */
|
|
8
|
+
advanceTime(delta: number): void;
|
|
9
|
+
}
|
|
10
|
+
/** Options of {@link advanceTime}. */
|
|
11
|
+
export interface AdvanceTimeOptions {
|
|
12
|
+
/** Scenes whose plugins step, in this order. Default `[engine.scene]`. */
|
|
13
|
+
scenes?: Iterable<Pick<Scene, 'plugins'>>;
|
|
14
|
+
/** Clocks that are not plugins (an unattached WeatherMachine, a mixer), stepped after the plugins, in order. */
|
|
15
|
+
extra?: Iterable<Clock>;
|
|
16
|
+
}
|
|
17
|
+
/** Options of {@link seek}. */
|
|
18
|
+
export interface SeekOptions extends AdvanceTimeOptions {
|
|
19
|
+
/** Steps per second; each step advances every clock by `1 / fps`. Use the rate the film renders at. Default 60. */
|
|
20
|
+
fps?: number;
|
|
21
|
+
/** The steps themselves, in seconds, in place of `Math.round(seconds * fps)` steps of `1 / fps`: the deltas a
|
|
22
|
+
* sub-framed film's renders gave its clocks (`subFrameSteps` from `@margoengine/headless/accumulate`). Each is
|
|
23
|
+
* from 0 to 1. `onStep` then gets the running sum as its film time. Default none. */
|
|
24
|
+
steps?: Iterable<number>;
|
|
25
|
+
/** Runs before each step with the step number from 0 and its film time, so film code can set what clocks read. */
|
|
26
|
+
onStep?: (step: number, time: number) => unknown;
|
|
27
|
+
/** Stops the seek between steps with the signal's reason. */
|
|
28
|
+
signal?: AbortSignal;
|
|
29
|
+
}
|
|
30
|
+
/** What {@link seek} did. */
|
|
31
|
+
export interface SeekResult {
|
|
32
|
+
/** Steps run: `Math.round(seconds * fps)`, or the number of `steps` given. */
|
|
33
|
+
steps: number;
|
|
34
|
+
/** Names of the clocks it moved: plugin names, then `extra[i]` (or the clock's own string `name`). */
|
|
35
|
+
clocks: string[];
|
|
36
|
+
/** Plugins with a frame update but no `advanceTime`: they restart at a seek and show it, so list them in your report. */
|
|
37
|
+
unseekable: string[];
|
|
38
|
+
}
|
|
39
|
+
/** What a seek reads of an engine: the plugins of its scene. An `Engine` fits, and so does `{ scene: { plugins: new Map() } }`. */
|
|
40
|
+
type Engine = {
|
|
41
|
+
readonly scene: Pick<Scene, 'plugins'>;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Moves every clock one step of `delta` seconds without drawing: the plugins of each scene in attach order (the order
|
|
45
|
+
* the engine updates them), then `extra`. Plugins without `advanceTime` are left alone. Run it where a continuous run
|
|
46
|
+
* would draw a frame; a draw does not move any clock that `advanceTime` moves.
|
|
47
|
+
*/
|
|
48
|
+
export declare function advanceTime(engine: Engine, delta: number, options?: AdvanceTimeOptions): void;
|
|
49
|
+
/**
|
|
50
|
+
* Brings a fresh world to film time `seconds`: runs `Math.round(seconds * fps)` steps of `1 / fps` over every clock,
|
|
51
|
+
* as {@link advanceTime} does, so the next live frame draws what a run started at 0 draws there, bit for bit. Every
|
|
52
|
+
* clock must read 0 (a fresh start); a seek does not go back. Resolves with the step count and the clocks it moved.
|
|
53
|
+
* Cost: steps x clocks `advanceTime` calls (15 minutes at 60 fps is 54,000 steps), CPU only.
|
|
54
|
+
*/
|
|
55
|
+
export declare function seek(engine: Engine, seconds: number, options?: SeekOptions): Promise<SeekResult>;
|
|
56
|
+
/**
|
|
57
|
+
* {@link seek} without waiting, for code that cannot await (a generator of frames): the same steps in the same order,
|
|
58
|
+
* with no `onStep` and no turn of the event loop; a `signal` is checked before it starts.
|
|
59
|
+
*/
|
|
60
|
+
export declare function seekSync(engine: Engine, seconds: number, options?: Omit<SeekOptions, 'onStep'>): SeekResult;
|
|
61
|
+
/**
|
|
62
|
+
* A clock that can go back to its start (GAP-FILM-RESET): `resetTime()` puts its time, counters, seeds and history
|
|
63
|
+
* (GPU history included: the next frame draws it as a fresh one would) back to what a newly made one holds, so one
|
|
64
|
+
* process can render several scenes, or seek back, instead of a fresh process per scene. Its options and resources stay.
|
|
65
|
+
*/
|
|
66
|
+
export interface ResettableClock extends Clock {
|
|
67
|
+
/** Back to time 0 and the state of a fresh clock with the same options; history is dropped. */
|
|
68
|
+
resetTime(): void;
|
|
69
|
+
}
|
|
70
|
+
/** What {@link resetClocks} did. */
|
|
71
|
+
export interface ResetResult {
|
|
72
|
+
/** Names of the clocks it put back to 0, in step order. */
|
|
73
|
+
clocks: string[];
|
|
74
|
+
/**
|
|
75
|
+
* Clocks with no `resetTime` yet, and plugins with a frame update but no clock: they keep their time and history, so
|
|
76
|
+
* a seek after the reset refuses the clocks among them (they no longer read 0). Report them; the list only shrinks.
|
|
77
|
+
*/
|
|
78
|
+
unresettable: string[];
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Puts every clock of the engine's scenes (and `extra`) back to its start, so a {@link seek} from 0 can follow in the
|
|
82
|
+
* same process: a seek to T after a reset draws what a fresh world seeked to T draws, byte for byte, for every clock
|
|
83
|
+
* listed in `clocks`. Clocks without `resetTime` are left alone and listed in `unresettable`.
|
|
84
|
+
*/
|
|
85
|
+
export declare function resetClocks(engine: Engine, options?: AdvanceTimeOptions): ResetResult;
|
|
86
|
+
export {};
|
package/dist/seek.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
2
|
+
//#region src/seek.ts
|
|
3
|
+
var t = (e) => typeof e?.advanceTime == "function";
|
|
4
|
+
function n(e, n, r) {
|
|
5
|
+
let i = [];
|
|
6
|
+
for (let a of n.scenes ?? [e.scene]) for (let [e, n] of a.plugins) t(n) ? i.push([e, n]) : typeof n.onPreUpdate == "function" && r?.push(e);
|
|
7
|
+
let a = 0;
|
|
8
|
+
for (let e of n.extra ?? []) {
|
|
9
|
+
if (!t(e)) throw TypeError(`extra[${a}] needs advanceTime(delta); pass a clock such as a WeatherMachine`);
|
|
10
|
+
let n = e.name;
|
|
11
|
+
i.push([typeof n == "string" && n ? n : `extra[${a}]`, e]), a++;
|
|
12
|
+
}
|
|
13
|
+
return i;
|
|
14
|
+
}
|
|
15
|
+
var r = (e) => {
|
|
16
|
+
if (!Number.isFinite(e) || e < 0 || e > 1) throw RangeError("advanceTime needs a delta from 0 to 1 second, as a frame delta");
|
|
17
|
+
};
|
|
18
|
+
function i(e, t, i = {}) {
|
|
19
|
+
r(t);
|
|
20
|
+
for (let [, r] of n(e, i)) r.advanceTime(t);
|
|
21
|
+
}
|
|
22
|
+
function a(t, i, a) {
|
|
23
|
+
let { fps: o = 60 } = a;
|
|
24
|
+
if (!Number.isFinite(i) || i < 0) throw RangeError("seek needs a finite time of 0 or more seconds");
|
|
25
|
+
if (!Number.isFinite(o) || o < 1) throw RangeError("seek needs fps of 1 or more steps per second");
|
|
26
|
+
a.signal?.throwIfAborted();
|
|
27
|
+
let s = [], c = n(t, a, s), l = c.filter(([, e]) => e.time !== 0).map(([e]) => e);
|
|
28
|
+
if (l.length) throw new e("state", `seek needs a fresh start, and ${l.join(", ")} already moved; seek before the first frame; see https://margoengine.com/errors#film-seek`);
|
|
29
|
+
let u = 1 / o;
|
|
30
|
+
r(u);
|
|
31
|
+
let d = a.steps ? [...a.steps] : void 0;
|
|
32
|
+
if (d) for (let e of d) r(e);
|
|
33
|
+
return {
|
|
34
|
+
clocks: c,
|
|
35
|
+
unseekable: s,
|
|
36
|
+
steps: d?.length ?? Math.round(i * o),
|
|
37
|
+
list: d,
|
|
38
|
+
delta: u,
|
|
39
|
+
fps: o
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
async function o(e, t, n = {}) {
|
|
43
|
+
let { onStep: r, signal: i } = n, { clocks: o, unseekable: s, steps: c, list: l, delta: u, fps: d } = a(e, t, n), f = 0;
|
|
44
|
+
for (let e = 0; e < c; e++) {
|
|
45
|
+
r && await r(e, l ? f : e / d);
|
|
46
|
+
let t = l ? l[e] : u;
|
|
47
|
+
f += t;
|
|
48
|
+
for (let [, e] of o) e.advanceTime(t);
|
|
49
|
+
i && e % 1e3 == 999 && await new Promise((e) => setTimeout(e, 0)), i?.throwIfAborted();
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
steps: c,
|
|
53
|
+
clocks: o.map(([e]) => e),
|
|
54
|
+
unseekable: s
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function s(e, t, n = {}) {
|
|
58
|
+
let { clocks: r, unseekable: i, steps: o, list: s, delta: c } = a(e, t, n);
|
|
59
|
+
for (let e = 0; e < o; e++) for (let [, t] of r) t.advanceTime(s ? s[e] : c);
|
|
60
|
+
return {
|
|
61
|
+
steps: o,
|
|
62
|
+
clocks: r.map(([e]) => e),
|
|
63
|
+
unseekable: i
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
var c = (e) => typeof e.resetTime == "function";
|
|
67
|
+
function l(e, t = {}) {
|
|
68
|
+
let r = [], i = [];
|
|
69
|
+
for (let [a, o] of n(e, t, r)) c(o) ? (o.resetTime(), i.push(a)) : r.push(a);
|
|
70
|
+
return {
|
|
71
|
+
clocks: i,
|
|
72
|
+
unresettable: r
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
export { i as advanceTime, l as resetClocks, o as seek, s as seekSync };
|
package/dist/shots.d.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { type CutsceneState, type CutsceneTrackType } from './cutscene.js';
|
|
2
|
+
import type { CameraKey, ClipBase, EaseSpec, Vec3 } from './index.js';
|
|
3
|
+
/**
|
|
4
|
+
* A camera a shot can use. Where it stands comes from `keys` (a move, timed from the start of the shot), `live` (a
|
|
5
|
+
* camera of the running game), or `position` and `target`. `follow` then makes the position relative to an object and
|
|
6
|
+
* `lookAt` aims at an object instead of the target.
|
|
7
|
+
*/
|
|
8
|
+
export interface VirtualCamera {
|
|
9
|
+
/** A camera move: the keys of a camera track, with time 0 at the start of the shot. */
|
|
10
|
+
keys?: CameraKey[];
|
|
11
|
+
/** Path through `keys`: `'catmull-rom'` (default) or `'linear'`. */
|
|
12
|
+
path?: 'catmull-rom' | 'linear';
|
|
13
|
+
/** Names a function in the `live` option of {@link shotTrack}, read at every evaluate. */
|
|
14
|
+
live?: string;
|
|
15
|
+
/** Default [0, 0, 0], or the offset from `follow`. */
|
|
16
|
+
position?: Vec3;
|
|
17
|
+
/** The point looked at. Needed unless `keys`, `live` or `lookAt` gives one. */
|
|
18
|
+
target?: Vec3;
|
|
19
|
+
/** Vertical field of view, degrees (default 60). */
|
|
20
|
+
fov?: number;
|
|
21
|
+
/** Roll about the view direction, radians (default 0). */
|
|
22
|
+
roll?: number;
|
|
23
|
+
/** Object whose position `position` is measured from. */
|
|
24
|
+
follow?: string;
|
|
25
|
+
/** With `follow`: measure in the object's own axes, so the camera turns with it (default false). */
|
|
26
|
+
followRotation?: boolean;
|
|
27
|
+
/** Object to aim at. */
|
|
28
|
+
lookAt?: string;
|
|
29
|
+
/** Added to the `lookAt` object's position (default [0, 0, 0]). */
|
|
30
|
+
lookOffset?: Vec3;
|
|
31
|
+
}
|
|
32
|
+
/** A shot: a camera for a span of the timeline. Shots may not overlap; between shots the last one holds. */
|
|
33
|
+
export interface ShotClip extends ClipBase {
|
|
34
|
+
/** Key of the camera in the track's `cameras`. */
|
|
35
|
+
camera: string;
|
|
36
|
+
/** Seconds to blend from the shot before; 0 is a cut (default 0). At most the shot's `duration`. */
|
|
37
|
+
blendIn?: number;
|
|
38
|
+
/** Shape of the blend (default `'smoothstep'`). */
|
|
39
|
+
ease?: EaseSpec;
|
|
40
|
+
/**
|
|
41
|
+
* `'direct'` (default) moves the camera straight across and turns its view. `'orbit'` swings it around the point
|
|
42
|
+
* looked at, for two cameras on one subject, so the blend does not cut through the subject.
|
|
43
|
+
*/
|
|
44
|
+
blend?: 'direct' | 'orbit';
|
|
45
|
+
/** Rate of the camera's move (default 1). */
|
|
46
|
+
speed?: number;
|
|
47
|
+
/** Seconds into the camera's move at the start of the shot (default 0). */
|
|
48
|
+
offset?: number;
|
|
49
|
+
}
|
|
50
|
+
/** Cameras and the shots that use them. */
|
|
51
|
+
export interface ShotTrack {
|
|
52
|
+
type: 'shot';
|
|
53
|
+
id: string;
|
|
54
|
+
cameras: Record<string, VirtualCamera>;
|
|
55
|
+
clips: ShotClip[];
|
|
56
|
+
muted?: boolean;
|
|
57
|
+
}
|
|
58
|
+
/** Which shots are on screen: what {@link shotOf} gives. */
|
|
59
|
+
export interface ShotState {
|
|
60
|
+
/** The shot's camera. */
|
|
61
|
+
camera: string;
|
|
62
|
+
/** During a blend, the camera it blends from. */
|
|
63
|
+
from?: string;
|
|
64
|
+
/** How far the blend is, 0 to 1 after easing; 1 when no blend runs. */
|
|
65
|
+
weight: number;
|
|
66
|
+
/** Path of the track. */
|
|
67
|
+
track: string;
|
|
68
|
+
}
|
|
69
|
+
/** Where an object is, from {@link ShotTrackOptions.locate}. */
|
|
70
|
+
export interface ShotObject {
|
|
71
|
+
position: readonly number[];
|
|
72
|
+
/** Unit quaternion, for `followRotation`. */
|
|
73
|
+
rotation?: readonly number[];
|
|
74
|
+
}
|
|
75
|
+
/** What a `live` camera gives. */
|
|
76
|
+
export interface LiveCamera {
|
|
77
|
+
position: readonly number[];
|
|
78
|
+
target: readonly number[];
|
|
79
|
+
/** Degrees (default 60). */
|
|
80
|
+
fov?: number;
|
|
81
|
+
/** Radians (default 0). */
|
|
82
|
+
roll?: number;
|
|
83
|
+
}
|
|
84
|
+
/** Options of {@link shotTrack}. */
|
|
85
|
+
export interface ShotTrackOptions {
|
|
86
|
+
/** Finds an object the cutscene does not move. Objects with a transform track are read from it first. Default none. */
|
|
87
|
+
locate?: (name: string) => ShotObject | undefined;
|
|
88
|
+
/** Cameras of the running game by name, so a cutscene can blend out of and back into play. Default none. */
|
|
89
|
+
live?: Readonly<Record<string, () => LiveCamera>>;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The shot track type: pass it in the `tracks` option of `Cutscene` or `CutscenePlayer`. A shot track sets
|
|
93
|
+
* `state.camera`, in place of any camera track; the first unmuted shot track wins. Shots fire `enter` and `exit`.
|
|
94
|
+
* @throws RangeError from `evaluate` when a shot needs an object or a live camera nothing provides.
|
|
95
|
+
*/
|
|
96
|
+
export declare function shotTrack(options?: ShotTrackOptions): CutsceneTrackType;
|
|
97
|
+
/** Which shot a state shows, or null when it has no shot track. */
|
|
98
|
+
export declare function shotOf(state: CutsceneState): ShotState | null;
|
package/dist/shots.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { i as e, o as t, r as n } from "./cutscene-DAWl8liP.js";
|
|
2
|
+
import { rotateByQuaternion as r } from "@margoengine/math/quat";
|
|
3
|
+
//#region src/shots.ts
|
|
4
|
+
var i = (e) => Array.isArray(e) && e.length === 3 && e.every((e) => Number.isFinite(e));
|
|
5
|
+
function a(e, t, n) {
|
|
6
|
+
let { add: r } = n, a = e.keys !== void 0;
|
|
7
|
+
a && (n.cameraKeys(e.keys, `${t}.keys`), "path" in e && e.path !== "catmull-rom" && e.path !== "linear" && r(`${t}.path`, "must be 'catmull-rom' or 'linear'"));
|
|
8
|
+
for (let n of [
|
|
9
|
+
"live",
|
|
10
|
+
"follow",
|
|
11
|
+
"lookAt"
|
|
12
|
+
]) n in e && (typeof e[n] != "string" || !e[n]) && r(`${t}.${n}`, "must be a non-empty string");
|
|
13
|
+
for (let n of [
|
|
14
|
+
"position",
|
|
15
|
+
"target",
|
|
16
|
+
"lookOffset"
|
|
17
|
+
]) n in e && !i(e[n]) && r(`${t}.${n}`, "must be [x, y, z] with finite numbers");
|
|
18
|
+
if (a && "live" in e && r(`${t}.live`, "cannot go with keys; use one source for the camera"), a || "live" in e) for (let n of [
|
|
19
|
+
"position",
|
|
20
|
+
"target",
|
|
21
|
+
"follow",
|
|
22
|
+
"lookAt"
|
|
23
|
+
]) n in e && r(`${t}.${n}`, "cannot go with keys or live, which set where the camera is");
|
|
24
|
+
else !("target" in e) && !("lookAt" in e) && r(t, "must say what it looks at: keys, live, target or lookAt");
|
|
25
|
+
"fov" in e && !(typeof e.fov == "number" && e.fov > 0 && e.fov < 180) && r(`${t}.fov`, "must be degrees between 0 and 180"), "roll" in e && !Number.isFinite(e.roll) && r(`${t}.roll`, "must be a finite number of radians"), "followRotation" in e && typeof e.followRotation != "boolean" && r(`${t}.followRotation`, "must be true or false");
|
|
26
|
+
}
|
|
27
|
+
function o(e, t) {
|
|
28
|
+
let { path: n, add: r } = t, i = e.cameras, o = typeof i == "object" && !!i && !Array.isArray(i);
|
|
29
|
+
if (!o) r(`${n}.cameras`, "must be an object of name: camera");
|
|
30
|
+
else for (let [e, o] of Object.entries(i)) typeof o != "object" || !o || Array.isArray(o) ? r(`${n}.cameras.${e}`, "must be an object") : a(o, `${n}.cameras.${e}`, t);
|
|
31
|
+
let s = t.clips((e, n) => {
|
|
32
|
+
(typeof e.camera != "string" || !o || !Object.hasOwn(i, e.camera)) && r(`${n}.camera`, `must name one of the track's cameras (${o ? Object.keys(i).join(", ") : "none"})`), t.positive(e, "blendIn", n), t.positive(e, "speed", n, !1), t.positive(e, "offset", n), t.ease(e.ease, `${n}.ease`), "blend" in e && e.blend !== "direct" && e.blend !== "orbit" && r(`${n}.blend`, "must be 'direct' or 'orbit'"), typeof e.blendIn == "number" && typeof e.duration == "number" && e.blendIn > e.duration && r(`${n}.blendIn`, "is longer than the shot; shorten the blend or lengthen the shot");
|
|
33
|
+
});
|
|
34
|
+
s?.forEach((e, t) => {
|
|
35
|
+
let i = s[t - 1];
|
|
36
|
+
i ? i.start + i.duration > e.start + 1e-9 && r(`${n}.clips[${t}].start`, "overlaps the shot before; shots follow each other, use blendIn to blend") : typeof e.blendIn == "number" && e.blendIn > 0 && r(`${n}.clips[0].blendIn`, "has no shot before it to blend from; start with a shot of a live camera");
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
var s = (e) => Math.hypot(e[0], e[1], e[2]), c = (e, t) => [
|
|
40
|
+
e[0] - t[0],
|
|
41
|
+
e[1] - t[1],
|
|
42
|
+
e[2] - t[2]
|
|
43
|
+
], l = (e, t, n) => [
|
|
44
|
+
e[0] + (t[0] - e[0]) * n,
|
|
45
|
+
e[1] + (t[1] - e[1]) * n,
|
|
46
|
+
e[2] + (t[2] - e[2]) * n
|
|
47
|
+
];
|
|
48
|
+
function u(e, t, n) {
|
|
49
|
+
let r = s(e), i = s(t), a = r + (i - r) * n;
|
|
50
|
+
if (r < 1e-12 || i < 1e-12) return l(e, t, n);
|
|
51
|
+
let o = e[0] / r, c = e[1] / r, u = e[2] / r, d = Math.min(1, Math.max(-1, (o * t[0] + c * t[1] + u * t[2]) / i)), f = t[0] / i - o * d, p = t[1] / i - c * d, m = t[2] / i - u * d, h = Math.hypot(f, p, m);
|
|
52
|
+
if (h < 1e-9) {
|
|
53
|
+
if (d > 0) return [
|
|
54
|
+
o * a,
|
|
55
|
+
c * a,
|
|
56
|
+
u * a
|
|
57
|
+
];
|
|
58
|
+
let e = Math.hypot(o, u) > 1e-6;
|
|
59
|
+
f = e ? u : 1, p = 0, m = e ? -o : 0, h = Math.hypot(f, p, m);
|
|
60
|
+
}
|
|
61
|
+
let g = Math.atan2(h, d) * n, _ = Math.cos(g), v = Math.sin(g) / h;
|
|
62
|
+
return [
|
|
63
|
+
(o * _ + f * v) * a,
|
|
64
|
+
(c * _ + p * v) * a,
|
|
65
|
+
(u * _ + m * v) * a
|
|
66
|
+
];
|
|
67
|
+
}
|
|
68
|
+
function d(e, n, r, i, a) {
|
|
69
|
+
if (r <= 0) return {
|
|
70
|
+
...e,
|
|
71
|
+
track: a
|
|
72
|
+
};
|
|
73
|
+
if (r >= 1) return {
|
|
74
|
+
...n,
|
|
75
|
+
track: a
|
|
76
|
+
};
|
|
77
|
+
let o = e.roll + (n.roll - e.roll) * r, s, d;
|
|
78
|
+
if (i === "orbit") {
|
|
79
|
+
d = l(e.target, n.target, r);
|
|
80
|
+
let t = u(c(e.position, e.target), c(n.position, n.target), r);
|
|
81
|
+
s = [
|
|
82
|
+
d[0] + t[0],
|
|
83
|
+
d[1] + t[1],
|
|
84
|
+
d[2] + t[2]
|
|
85
|
+
];
|
|
86
|
+
} else {
|
|
87
|
+
s = l(e.position, n.position, r);
|
|
88
|
+
let t = u(c(e.target, e.position), c(n.target, n.position), r);
|
|
89
|
+
d = [
|
|
90
|
+
s[0] + t[0],
|
|
91
|
+
s[1] + t[1],
|
|
92
|
+
s[2] + t[2]
|
|
93
|
+
];
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
position: s,
|
|
97
|
+
target: d,
|
|
98
|
+
fov: e.fov + (n.fov - e.fov) * r,
|
|
99
|
+
roll: o,
|
|
100
|
+
up: t(s, d, o),
|
|
101
|
+
track: a
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function f(r = {}) {
|
|
105
|
+
let { locate: i, live: a = {} } = r, s = /* @__PURE__ */ new WeakMap(), c = (t) => {
|
|
106
|
+
let n = s.get(t);
|
|
107
|
+
return n || s.set(t, n = t.keys.map((t) => e(t.ease))), n;
|
|
108
|
+
}, l = (e, t) => {
|
|
109
|
+
let n = t.transforms[e];
|
|
110
|
+
if (n?.position) return n;
|
|
111
|
+
let r = i?.(e);
|
|
112
|
+
if (!r) throw RangeError(`No object '${e}' for a shot; key its position on a transform track or pass locate`);
|
|
113
|
+
return r;
|
|
114
|
+
}, u = (e, r, i, o) => {
|
|
115
|
+
if (e.keys) return n(e.keys, c(e), e.path ?? "catmull-rom", r, o);
|
|
116
|
+
let s, u, d = e.fov, f = e.roll;
|
|
117
|
+
if (e.live !== void 0) {
|
|
118
|
+
if (!Object.hasOwn(a, e.live)) throw RangeError(`No live camera '${e.live}' for a shot; pass it in the live option`);
|
|
119
|
+
let t = a[e.live]();
|
|
120
|
+
s = t.position, u = t.target, d ??= t.fov, f ??= t.roll;
|
|
121
|
+
} else {
|
|
122
|
+
if (s = e.position ?? [
|
|
123
|
+
0,
|
|
124
|
+
0,
|
|
125
|
+
0
|
|
126
|
+
], u = e.target ?? [
|
|
127
|
+
0,
|
|
128
|
+
0,
|
|
129
|
+
0
|
|
130
|
+
], e.follow !== void 0) {
|
|
131
|
+
let t = l(e.follow, i), n = e.followRotation && t.rotation ? p(s, t.rotation) : s;
|
|
132
|
+
s = [
|
|
133
|
+
t.position[0] + n[0],
|
|
134
|
+
t.position[1] + n[1],
|
|
135
|
+
t.position[2] + n[2]
|
|
136
|
+
];
|
|
137
|
+
}
|
|
138
|
+
if (e.lookAt !== void 0) {
|
|
139
|
+
let t = l(e.lookAt, i).position, n = e.lookOffset ?? [
|
|
140
|
+
0,
|
|
141
|
+
0,
|
|
142
|
+
0
|
|
143
|
+
];
|
|
144
|
+
u = [
|
|
145
|
+
t[0] + n[0],
|
|
146
|
+
t[1] + n[1],
|
|
147
|
+
t[2] + n[2]
|
|
148
|
+
];
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
position: [
|
|
153
|
+
s[0],
|
|
154
|
+
s[1],
|
|
155
|
+
s[2]
|
|
156
|
+
],
|
|
157
|
+
target: [
|
|
158
|
+
u[0],
|
|
159
|
+
u[1],
|
|
160
|
+
u[2]
|
|
161
|
+
],
|
|
162
|
+
fov: d ?? 60,
|
|
163
|
+
roll: f ?? 0,
|
|
164
|
+
up: t(s, u, f ?? 0),
|
|
165
|
+
track: o
|
|
166
|
+
};
|
|
167
|
+
};
|
|
168
|
+
return {
|
|
169
|
+
type: "shot",
|
|
170
|
+
validate: o,
|
|
171
|
+
instants(e) {
|
|
172
|
+
return e.clips.flatMap((e) => [{
|
|
173
|
+
time: e.start,
|
|
174
|
+
kind: "enter",
|
|
175
|
+
name: e.camera,
|
|
176
|
+
clip: e
|
|
177
|
+
}, {
|
|
178
|
+
time: e.start + e.duration,
|
|
179
|
+
kind: "exit",
|
|
180
|
+
name: e.camera,
|
|
181
|
+
clip: e
|
|
182
|
+
}]);
|
|
183
|
+
},
|
|
184
|
+
evaluate(t, n, r, i) {
|
|
185
|
+
let { cameras: a, clips: o } = t, s = r.extras ??= {};
|
|
186
|
+
if (s.shot || !o.length) return;
|
|
187
|
+
let c = 0;
|
|
188
|
+
for (; c + 1 < o.length && o[c + 1].start <= n;) c++;
|
|
189
|
+
let l = o[c], f = (e) => (e.offset ?? 0) + Math.max(0, n - e.start) * (e.speed ?? 1), p = u(a[l.camera], f(l), r, i), m = n - l.start;
|
|
190
|
+
if (!(c > 0 && l.blendIn !== void 0 && m < l.blendIn)) {
|
|
191
|
+
r.camera = p, s.shot = {
|
|
192
|
+
camera: l.camera,
|
|
193
|
+
weight: 1,
|
|
194
|
+
track: i
|
|
195
|
+
};
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
let h = o[c - 1], g = e(l.ease ?? "smoothstep")(m / l.blendIn);
|
|
199
|
+
r.camera = d(u(a[h.camera], f(h), r, i), p, g, l.blend ?? "direct", i), s.shot = {
|
|
200
|
+
camera: l.camera,
|
|
201
|
+
from: h.camera,
|
|
202
|
+
weight: g,
|
|
203
|
+
track: i
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
var p = (e, t) => {
|
|
209
|
+
let n = [
|
|
210
|
+
e[0],
|
|
211
|
+
e[1],
|
|
212
|
+
e[2]
|
|
213
|
+
];
|
|
214
|
+
return r(n, t, n);
|
|
215
|
+
};
|
|
216
|
+
function m(e) {
|
|
217
|
+
return e.extras?.shot ?? null;
|
|
218
|
+
}
|
|
219
|
+
//#endregion
|
|
220
|
+
export { m as shotOf, f as shotTrack };
|