@margoengine/netcode 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 +1387 -2
- package/dist/client.d.ts +167 -0
- package/dist/clock.d.ts +29 -0
- package/dist/extrapolate.d.ts +42 -0
- package/dist/extrapolate.js +25 -0
- package/dist/game.d.ts +68 -0
- package/dist/generate-rust.d.ts +14 -0
- package/dist/generate-rust.js +184 -0
- package/dist/generate.d.ts +25 -0
- package/dist/generate.js +156 -0
- package/dist/hitboxes.d.ts +74 -0
- package/dist/hitboxes.js +124 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +457 -0
- package/dist/interest.d.ts +101 -0
- package/dist/interest.js +166 -0
- package/dist/interpolation-delay.d.ts +114 -0
- package/dist/interpolation-delay.js +79 -0
- package/dist/interpolation-fields.d.ts +117 -0
- package/dist/interpolation-fields.js +147 -0
- package/dist/interpolation-timeline.d.ts +100 -0
- package/dist/interpolation-timeline.js +122 -0
- package/dist/interpolation.d.ts +39 -0
- package/dist/interpolation.js +61 -0
- package/dist/lag.d.ts +62 -0
- package/dist/lobby.d.ts +77 -0
- package/dist/lobby.js +96 -0
- package/dist/loopback.d.ts +92 -0
- package/dist/media.d.ts +215 -0
- package/dist/media.js +305 -0
- package/dist/migrate.d.ts +113 -0
- package/dist/migrate.js +289 -0
- package/dist/node.d.ts +101 -0
- package/dist/node.js +166 -0
- package/dist/physics.d.ts +133 -0
- package/dist/physics.js +169 -0
- package/dist/presence.d.ts +156 -0
- package/dist/presence.js +226 -0
- package/dist/protocol-asi_qndb.js +122 -0
- package/dist/protocol.d.ts +75 -0
- package/dist/relay.d.ts +152 -0
- package/dist/relay.js +235 -0
- package/dist/rollback.d.ts +131 -0
- package/dist/rollback.js +194 -0
- package/dist/rooms.d.ts +366 -0
- package/dist/rooms.js +479 -0
- package/dist/schema-CMfqK4Gu.js +616 -0
- package/dist/schema.d.ts +263 -0
- package/dist/server-DAVzHcWW.js +203 -0
- package/dist/server.d.ts +147 -0
- package/dist/shared/bytes.d.ts +38 -0
- package/dist/signal.d.ts +156 -0
- package/dist/signal.js +261 -0
- package/dist/smoothing.d.ts +76 -0
- package/dist/smoothing.js +155 -0
- package/dist/transport-DeQodwyv.js +49 -0
- package/dist/transport.d.ts +76 -0
- package/dist/webrtc.d.ts +32 -0
- package/dist/webrtc.js +64 -0
- package/dist/websocket.d.ts +27 -0
- package/dist/websocket.js +42 -0
- package/dist/webtransport.d.ts +83 -0
- package/dist/webtransport.js +120 -0
- package/package.json +138 -4
package/dist/interest.js
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
//#region src/interest.ts
|
|
2
|
+
var e = (e) => {
|
|
3
|
+
let t = 1;
|
|
4
|
+
for (; e >= 128; t++) e = Math.floor(e / 128);
|
|
5
|
+
return t;
|
|
6
|
+
}, t = (e, t) => {
|
|
7
|
+
if (!(t > 0) || !Number.isFinite(t)) throw RangeError(`InterestManager ${e} is ${t}; pass a number above 0`);
|
|
8
|
+
return t;
|
|
9
|
+
}, n = (e, t, n) => Math.imul(e, 73856093) ^ Math.imul(t, 19349663) ^ Math.imul(n, 83492791) | 0, r = 2 ** 30, i = class {
|
|
10
|
+
constructor(n, r) {
|
|
11
|
+
if (this.game = n, this.options = r, this.managed = [], this.viewers = /* @__PURE__ */ new Map(), this.round = 0, this.view = (t, n, r) => {
|
|
12
|
+
t !== this.indexed && this.index(t);
|
|
13
|
+
let i = this.game, a = i.schema, o = this.options, s = typeof o.budget == "function" ? o.budget(n) : o.budget ?? Infinity, c = o.self ? o.self(n) : n, l = this.origin(t, n, c), u = { ...t }, d = { ...t }, f = {
|
|
14
|
+
inScope: 0,
|
|
15
|
+
sent: 0,
|
|
16
|
+
deferred: 0,
|
|
17
|
+
removed: 0,
|
|
18
|
+
bytes: 0,
|
|
19
|
+
over: 0
|
|
20
|
+
};
|
|
21
|
+
if (!(s > 0)) throw RangeError(`InterestManager budget of player ${n} is ${s}; pass bytes above 0`);
|
|
22
|
+
let p = this.viewers.get(n);
|
|
23
|
+
if (!p) {
|
|
24
|
+
let e = this.managed.map(() => ({
|
|
25
|
+
acc: /* @__PURE__ */ new Map(),
|
|
26
|
+
last: /* @__PURE__ */ new Map(),
|
|
27
|
+
scoped: /* @__PURE__ */ new Set()
|
|
28
|
+
}));
|
|
29
|
+
p = {
|
|
30
|
+
round: this.round,
|
|
31
|
+
tracks: e,
|
|
32
|
+
stats: f
|
|
33
|
+
}, this.viewers.set(n, p);
|
|
34
|
+
}
|
|
35
|
+
let { tracks: m } = p;
|
|
36
|
+
p.round = this.round, p.stats = f;
|
|
37
|
+
let h = 21, g = [], _ = [], v = [], y = [];
|
|
38
|
+
this.managed.forEach((i, o) => {
|
|
39
|
+
let s = t[i.field], p = r?.[i.field], b = m[o], x = this.scope(i, s, b, l, n), S = /* @__PURE__ */ new Map(), C = i.rules.priority, w = s.get(c);
|
|
40
|
+
if (y.push(S), d[i.field] = p ?? /* @__PURE__ */ new Map(), u[i.field] = S, h += i.reserve, f.inScope += x.size, w !== void 0) {
|
|
41
|
+
x.delete(c), b.acc.delete(c), S.set(c, w);
|
|
42
|
+
let e = p?.get(c);
|
|
43
|
+
(e === void 0 || !a.equals(i.entry, e, w)) && (h += this.bytes(i.entry, c, w, e)), f.sent++;
|
|
44
|
+
}
|
|
45
|
+
for (let [e, t] of x) {
|
|
46
|
+
let r = s.get(e), o = p?.get(e), c = b.last.get(e);
|
|
47
|
+
if (o !== void 0 && a.equals(i.entry, o, r)) {
|
|
48
|
+
S.set(e, o), b.acc.delete(e);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
let l = C ? C(r, e, n, t) : this.radius / Math.max(t, this.radius / 16);
|
|
52
|
+
if (!(l >= 0) || !Number.isFinite(l)) throw RangeError(`InterestManager priority of ${i.field}(${e}) is ${l}; return 0 or more`);
|
|
53
|
+
let u = (b.acc.get(e) ?? 0) + l;
|
|
54
|
+
b.acc.set(e, u);
|
|
55
|
+
let d = {
|
|
56
|
+
track: b,
|
|
57
|
+
out: S,
|
|
58
|
+
entry: i.entry,
|
|
59
|
+
id: e,
|
|
60
|
+
value: r,
|
|
61
|
+
held: o,
|
|
62
|
+
base: o,
|
|
63
|
+
cost: 0,
|
|
64
|
+
acc: u
|
|
65
|
+
};
|
|
66
|
+
c !== void 0 && (o === void 0 || !a.equals(i.entry, c, o)) ? (d.held = c, d.cost = this.bytes(i.entry, e, c, o), g.push(d)) : (d.cost = this.bytes(i.entry, e, r, o), v.push(d));
|
|
67
|
+
}
|
|
68
|
+
for (let [t, n] of p ?? []) t !== c && !x.has(t) && _.push({
|
|
69
|
+
track: b,
|
|
70
|
+
out: S,
|
|
71
|
+
entry: i.entry,
|
|
72
|
+
id: t,
|
|
73
|
+
value: void 0,
|
|
74
|
+
held: n,
|
|
75
|
+
base: n,
|
|
76
|
+
cost: e(t),
|
|
77
|
+
acc: 0
|
|
78
|
+
});
|
|
79
|
+
for (let e of b.acc.keys()) x.has(e) || b.acc.delete(e);
|
|
80
|
+
b.scoped = new Set(x.keys()), w !== void 0 && b.scoped.add(c);
|
|
81
|
+
}), h += r === void 0 ? a.encode(i.stateType, d).length : a.encodeDelta(i.stateType, d, r).length;
|
|
82
|
+
for (let e of g) h + e.cost <= s ? (h += e.cost, e.out.set(e.id, e.held)) : (e.cost = 0, e.base !== void 0 && e.out.set(e.id, e.base));
|
|
83
|
+
for (let e of _) h + e.cost <= s ? (h += e.cost, f.removed++) : e.out.set(e.id, e.held);
|
|
84
|
+
let b = (e, t) => t.acc - e.acc || e.id - t.id;
|
|
85
|
+
for (let e of v.sort(b)) h + e.cost <= s ? (h += e.cost, e.out.set(e.id, e.value), e.track.acc.delete(e.id), f.sent++) : (f.deferred++, e.base !== void 0 && e.out.set(e.id, e.base));
|
|
86
|
+
let x = f.deferred === 0;
|
|
87
|
+
for (let e of g.sort(b)) {
|
|
88
|
+
let t = x ? this.bytes(e.entry, e.id, e.value, e.base) : Infinity;
|
|
89
|
+
h - e.cost + t <= s ? (h += t - e.cost, e.out.set(e.id, e.value), e.track.acc.delete(e.id), f.sent++) : f.deferred++;
|
|
90
|
+
}
|
|
91
|
+
return m.forEach((e, t) => e.last = y[t]), f.bytes = h, f.over = Math.max(0, h - s), u;
|
|
92
|
+
}, this.radius = t("radius", r?.radius), this.leave = r.leaveRadius ?? this.radius * 1.1, !(this.leave >= this.radius) || !Number.isFinite(this.leave)) throw RangeError(`InterestManager leaveRadius is ${this.leave}; pass radius or more`);
|
|
93
|
+
if (this.cell = t("cell", r.cell ?? this.radius), typeof r.budget == "number" && !(r.budget > 0)) throw RangeError(`InterestManager budget is ${r.budget}; pass bytes above 0`);
|
|
94
|
+
let i = n.schema.json.types[n.stateType];
|
|
95
|
+
for (let [t, a] of Object.entries(r.collections ?? {})) {
|
|
96
|
+
let r = i[t];
|
|
97
|
+
if (r?.type !== "map" || r.of.type !== "struct") throw TypeError(`InterestManager collection '${t}' is not a map of structs in ${n.stateType}; fix it`);
|
|
98
|
+
if (typeof a?.position != "function") throw TypeError(`InterestManager collection '${t}' needs position(value); pass it`);
|
|
99
|
+
this.managed.push({
|
|
100
|
+
field: t,
|
|
101
|
+
entry: r.of.name,
|
|
102
|
+
reserve: 2 * e(r.maxSize),
|
|
103
|
+
rules: a,
|
|
104
|
+
cells: /* @__PURE__ */ new Map(),
|
|
105
|
+
places: /* @__PURE__ */ new Map()
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
if (!this.managed.length) throw TypeError("InterestManager needs at least one collection; pass { collections }");
|
|
109
|
+
}
|
|
110
|
+
stats(e) {
|
|
111
|
+
return this.viewers.get(e)?.stats;
|
|
112
|
+
}
|
|
113
|
+
cellOf(e) {
|
|
114
|
+
return Math.min(r, Math.max(-r, Math.floor(e / this.cell)));
|
|
115
|
+
}
|
|
116
|
+
index(e) {
|
|
117
|
+
this.indexed = e, this.round++;
|
|
118
|
+
for (let [e, t] of this.viewers) t.round < this.round - 1 && this.viewers.delete(e);
|
|
119
|
+
for (let t of this.managed) {
|
|
120
|
+
t.cells.clear(), t.places.clear();
|
|
121
|
+
for (let [r, i] of e[t.field]) {
|
|
122
|
+
let e = t.rules.position(i, r);
|
|
123
|
+
if (e.length < 2 || e.length > 3) throw RangeError(`InterestManager position of ${t.field}(${r}) is not 2 or 3 numbers; return [x, y] or [x, y, z]`);
|
|
124
|
+
for (let n = 0; n < e.length; n++) if (!Number.isFinite(e[n])) throw RangeError(`InterestManager position of ${t.field}(${r}) is not finite; fix the position`);
|
|
125
|
+
t.places.set(r, e);
|
|
126
|
+
let a = n(this.cellOf(e[0]), this.cellOf(e[1]), e.length > 2 ? this.cellOf(e[2]) : 0), o = t.cells.get(a);
|
|
127
|
+
o ? o.push(r) : t.cells.set(a, [r]);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
scope(e, t, r, i, a) {
|
|
132
|
+
let o = /* @__PURE__ */ new Map(), { always: s, visible: c } = e.rules, l = (t) => {
|
|
133
|
+
let n = e.places.get(t), r = i, a = 0;
|
|
134
|
+
for (let e = 0; e < Math.max(n.length, r.length); e++) a += ((n[e] ?? 0) - (r[e] ?? 0)) ** 2;
|
|
135
|
+
return Math.sqrt(a);
|
|
136
|
+
}, u = (e) => {
|
|
137
|
+
let n = l(e);
|
|
138
|
+
n > (r.scoped.has(e) ? this.leave : this.radius) || (!c || c(t.get(e), e, a, n)) && o.set(e, n);
|
|
139
|
+
};
|
|
140
|
+
if (i) {
|
|
141
|
+
let t = Math.ceil(this.leave / this.cell), r = this.cellOf(i[0]), a = this.cellOf(i[1]), o = i.length > 2, s = o ? this.cellOf(i[2]) : 0;
|
|
142
|
+
for (let i = r - t; i <= r + t; i++) for (let r = a - t; r <= a + t; r++) for (let a = o ? s - t : 0; a <= (o ? s + t : 0); a++) {
|
|
143
|
+
let t = e.cells.get(n(i, r, a));
|
|
144
|
+
if (t) for (let e of t) u(e);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (s) for (let [e, n] of t) !o.has(e) && s(n, e, a) && o.set(e, i ? l(e) : 0);
|
|
148
|
+
return o;
|
|
149
|
+
}
|
|
150
|
+
bytes(t, n, r, i) {
|
|
151
|
+
let a = this.game.schema;
|
|
152
|
+
return e(n) + (i === void 0 ? a.encode(t, r) : a.encodeDelta(t, r, i)).length;
|
|
153
|
+
}
|
|
154
|
+
origin(e, t, n) {
|
|
155
|
+
if (this.options.focus) return this.options.focus(e, t);
|
|
156
|
+
for (let e of this.managed) {
|
|
157
|
+
let t = e.places.get(n);
|
|
158
|
+
if (t) return t;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
clear() {
|
|
162
|
+
this.viewers.clear(), this.indexed = void 0;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
//#endregion
|
|
166
|
+
export { i as InterestManager };
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/** Options of {@link AdaptiveDelay}. All times are seconds. */
|
|
2
|
+
export interface AdaptiveDelayOptions {
|
|
3
|
+
/** Share of recent arrivals the delay must cover, 0.5 to 1 (default 0.99). */
|
|
4
|
+
quantile?: number;
|
|
5
|
+
/** Arrivals remembered, an integer 16 to 65536 (default 256, about 13 s at 20 snapshots a second). */
|
|
6
|
+
window?: number;
|
|
7
|
+
/** Delay used while fewer than 8 arrivals are known, and the floor until then (default 0.1). */
|
|
8
|
+
initial?: number;
|
|
9
|
+
/** Smallest delay (default 0). */
|
|
10
|
+
min?: number;
|
|
11
|
+
/** Largest delay (default 1). */
|
|
12
|
+
max?: number;
|
|
13
|
+
/** Added to the measured delay (default 0). */
|
|
14
|
+
margin?: number;
|
|
15
|
+
/** How much slower than real time the view may run to gain delay, 0 to 0.9 (default 0.25). */
|
|
16
|
+
slowdown?: number;
|
|
17
|
+
/** How much faster than real time the view may run to shed delay, 0 to 1 (default 0.05). */
|
|
18
|
+
speedup?: number;
|
|
19
|
+
/** An arrival needing over this many times the delay starts a spike, above 1 (default 4). */
|
|
20
|
+
spikeHead?: number;
|
|
21
|
+
/** A spike ends at an arrival needing no more than this many times the delay before it, 1 or more (default 2). */
|
|
22
|
+
spikeTail?: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Picks how far behind the newest snapshot to draw from what the link actually does, and runs the view's clock. Feed
|
|
26
|
+
* it every snapshot with {@link AdaptiveDelay.observe}, then sample a snapshot buffer at
|
|
27
|
+
* {@link AdaptiveDelay.renderTime} each frame. The two clocks need no synchronisation: only differences of `received
|
|
28
|
+
* - sent` are used, and the smallest of them stands for the link's fixed delay and the clock offset.
|
|
29
|
+
*
|
|
30
|
+
* What is measured is the delay each arrival would have needed. For the view to have something to blend toward just
|
|
31
|
+
* before a snapshot landed, it must not have passed the newest snapshot then held. That one number holds the send
|
|
32
|
+
* interval, the jitter and any lost snapshots, so a clean link settles on exactly one send interval.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { SnapshotBuffer, createSnapshot } from '@margoengine/netcode/interpolation';
|
|
37
|
+
* import { AdaptiveDelay } from '@margoengine/netcode/interpolation-delay';
|
|
38
|
+
*
|
|
39
|
+
* const buffer = new SnapshotBuffer(),
|
|
40
|
+
* delay = new AdaptiveDelay(),
|
|
41
|
+
* pose = createSnapshot();
|
|
42
|
+
* function onSnapshot(serverTime: number, position: number[]) {
|
|
43
|
+
* buffer.push(serverTime, position);
|
|
44
|
+
* delay.observe(serverTime, performance.now() / 1000);
|
|
45
|
+
* }
|
|
46
|
+
* function onFrame() {
|
|
47
|
+
* const time = delay.renderTime(performance.now() / 1000);
|
|
48
|
+
* if (!Number.isNaN(time)) buffer.sample(time, pose);
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export declare class AdaptiveDelay {
|
|
53
|
+
/** Share of recent arrivals the delay covers. */
|
|
54
|
+
readonly quantile: number;
|
|
55
|
+
/** Arrivals remembered. */
|
|
56
|
+
readonly window: number;
|
|
57
|
+
/** See {@link AdaptiveDelayOptions.initial}. */
|
|
58
|
+
readonly initial: number;
|
|
59
|
+
/** Smallest delay in seconds. */
|
|
60
|
+
readonly min: number;
|
|
61
|
+
/** Largest delay in seconds. */
|
|
62
|
+
readonly max: number;
|
|
63
|
+
/** See {@link AdaptiveDelayOptions.margin}. */
|
|
64
|
+
readonly margin: number;
|
|
65
|
+
/** See {@link AdaptiveDelayOptions.slowdown}. */
|
|
66
|
+
readonly slowdown: number;
|
|
67
|
+
/** See {@link AdaptiveDelayOptions.speedup}. */
|
|
68
|
+
readonly speedup: number;
|
|
69
|
+
/** See {@link AdaptiveDelayOptions.spikeHead}. */
|
|
70
|
+
readonly spikeHead: number;
|
|
71
|
+
/** See {@link AdaptiveDelayOptions.spikeTail}. */
|
|
72
|
+
readonly spikeTail: number;
|
|
73
|
+
/** Whether a delay spike is being followed. */
|
|
74
|
+
spiking: boolean;
|
|
75
|
+
/** The delay the view is heading for, in seconds. */
|
|
76
|
+
target: number;
|
|
77
|
+
private readonly transits;
|
|
78
|
+
private transitHead;
|
|
79
|
+
private transitCount;
|
|
80
|
+
private floor;
|
|
81
|
+
private readonly gaps;
|
|
82
|
+
private readonly sorted;
|
|
83
|
+
private gapHead;
|
|
84
|
+
private gapCount;
|
|
85
|
+
private newest;
|
|
86
|
+
private spikeFrom;
|
|
87
|
+
private spikeLength;
|
|
88
|
+
private rendered;
|
|
89
|
+
private lastNow;
|
|
90
|
+
/** @throws RangeError for an option out of range, or `min` above `max`. */
|
|
91
|
+
constructor(options?: AdaptiveDelayOptions);
|
|
92
|
+
/** The smallest `received - sent` remembered: the link's fixed delay plus the clocks' offset (NaN before any). */
|
|
93
|
+
get offset(): number;
|
|
94
|
+
/** The delay the view runs at now in seconds: {@link AdaptiveDelay.target} until the first `renderTime`. */
|
|
95
|
+
get delay(): number;
|
|
96
|
+
/**
|
|
97
|
+
* Records one snapshot: the sender's time on it and the local time it arrived, in seconds.
|
|
98
|
+
* @returns Whether it was newer than every snapshot before it; an older one only teaches the link's floor.
|
|
99
|
+
* @throws RangeError for a time that is not finite.
|
|
100
|
+
*/
|
|
101
|
+
observe(sent: number, received: number): boolean;
|
|
102
|
+
/**
|
|
103
|
+
* The sender's time to draw at local time `now` in seconds, or NaN before the first snapshot. It never goes back and
|
|
104
|
+
* only moves off real time by `slowdown` or `speedup`, so a change of delay shows as slightly slow or fast motion;
|
|
105
|
+
* a view further behind than `max` jumps forward.
|
|
106
|
+
* @throws RangeError for a time that is not finite.
|
|
107
|
+
*/
|
|
108
|
+
renderTime(now: number): number;
|
|
109
|
+
/** Forgets the link and the view's clock, as after a reconnect. */
|
|
110
|
+
reset(): void;
|
|
111
|
+
private clamp;
|
|
112
|
+
private addTransit;
|
|
113
|
+
private addGap;
|
|
114
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
//#region src/interpolation-delay.ts
|
|
2
|
+
var e = (e, t, n, r) => {
|
|
3
|
+
if (!(t >= n && t <= r)) throw RangeError(`AdaptiveDelay ${e} is ${t}; pass a number from ${n} to ${r}`);
|
|
4
|
+
return t;
|
|
5
|
+
}, t = class {
|
|
6
|
+
constructor(t = {}) {
|
|
7
|
+
this.spiking = !1, this.transitHead = 0, this.transitCount = 0, this.floor = Infinity, this.gapHead = 0, this.gapCount = 0, this.newest = -Infinity, this.spikeFrom = 0, this.spikeLength = 0, this.rendered = NaN, this.lastNow = NaN;
|
|
8
|
+
let { quantile: n = .99, window: r = 256, initial: i = .1, min: a = 0, max: o = 1, margin: s = 0, slowdown: c = .25, speedup: l = .05, spikeHead: u = 4, spikeTail: d = 2 } = t;
|
|
9
|
+
if (this.quantile = e("quantile", n, .5, 1), !Number.isInteger(r)) throw RangeError(`AdaptiveDelay window is ${r}; pass an integer`);
|
|
10
|
+
this.window = e("window", r, 16, 65536), this.max = e("max", o, 0, 60), this.min = e("min", a, 0, this.max), this.initial = e("initial", i, 0, 60), this.margin = e("margin", s, 0, 60), this.slowdown = e("slowdown", c, 0, .9), this.speedup = e("speedup", l, 0, 1), this.spikeTail = e("spikeTail", d, 1, 1e6), this.spikeHead = e("spikeHead", u, 1 + 2 ** -52, 1e6), this.transits = new Float64Array(r), this.gaps = new Float64Array(r), this.sorted = new Float64Array(r), this.target = this.clamp(i);
|
|
11
|
+
}
|
|
12
|
+
get offset() {
|
|
13
|
+
return this.transitCount ? this.floor : NaN;
|
|
14
|
+
}
|
|
15
|
+
get delay() {
|
|
16
|
+
return Number.isNaN(this.rendered) ? this.target : this.lastNow - this.floor - this.rendered;
|
|
17
|
+
}
|
|
18
|
+
observe(e, t) {
|
|
19
|
+
if (!Number.isFinite(e) || !Number.isFinite(t)) throw RangeError(`AdaptiveDelay.observe got ${e}, ${t}; pass finite times in seconds`);
|
|
20
|
+
if (this.addTransit(t - e), e <= this.newest) return !1;
|
|
21
|
+
let n = this.newest === -Infinity, r = t - this.newest;
|
|
22
|
+
if (this.newest = e, n) return !0;
|
|
23
|
+
let i = r - this.floor;
|
|
24
|
+
if (this.spiking) {
|
|
25
|
+
if (this.spikeLength++, i <= this.spikeTail * this.spikeFrom) this.spiking = !1;
|
|
26
|
+
else if (this.spikeLength > this.window >> 2) this.spiking = !1, this.gapCount = this.gapHead = this.transitCount = this.transitHead = 0, this.addTransit(t - e);
|
|
27
|
+
else return this.target = this.clamp(i + this.margin), !0;
|
|
28
|
+
} else if (this.gapCount >= 8 && i > this.spikeHead * this.target) return this.spiking = !0, this.spikeFrom = this.target, this.spikeLength = 0, this.target = this.clamp(i + this.margin), !0;
|
|
29
|
+
this.addGap(r);
|
|
30
|
+
let a = this.sorted[Math.min(this.gapCount - 1, Math.ceil(this.quantile * this.gapCount) - 1)] - this.floor;
|
|
31
|
+
return this.target = this.clamp((this.gapCount < 8 ? Math.max(this.initial, a) : a) + this.margin), !0;
|
|
32
|
+
}
|
|
33
|
+
renderTime(e) {
|
|
34
|
+
if (!Number.isFinite(e)) throw RangeError(`AdaptiveDelay.renderTime got ${e}; pass a finite time in seconds`);
|
|
35
|
+
if (!this.transitCount) return NaN;
|
|
36
|
+
let t = e - this.floor - this.target;
|
|
37
|
+
if (Number.isNaN(this.rendered)) return this.rendered = t, this.lastNow = e, t;
|
|
38
|
+
let n = Math.max(0, e - this.lastNow), r = t - this.rendered - n;
|
|
39
|
+
return this.lastNow = Math.max(this.lastNow, e), r > this.max ? this.rendered = t : this.rendered += n + Math.min(n * this.speedup, Math.max(-n * this.slowdown, r)), this.rendered;
|
|
40
|
+
}
|
|
41
|
+
reset() {
|
|
42
|
+
this.gapCount = this.gapHead = this.transitCount = this.transitHead = 0, this.floor = Infinity, this.newest = -Infinity, this.rendered = this.lastNow = NaN, this.spiking = !1, this.target = this.clamp(this.initial);
|
|
43
|
+
}
|
|
44
|
+
clamp(e) {
|
|
45
|
+
return Math.min(this.max, Math.max(this.min, e));
|
|
46
|
+
}
|
|
47
|
+
addTransit(e) {
|
|
48
|
+
let t = this.transits, n = this.window;
|
|
49
|
+
if (this.transitCount < n) {
|
|
50
|
+
t[(this.transitHead + this.transitCount++) % n] = e, this.floor = this.transitCount === 1 ? e : Math.min(this.floor, e);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
let r = t[this.transitHead];
|
|
54
|
+
if (t[this.transitHead] = e, this.transitHead = (this.transitHead + 1) % n, e <= this.floor) this.floor = e;
|
|
55
|
+
else if (r === this.floor) {
|
|
56
|
+
let e = Infinity;
|
|
57
|
+
for (let r = 0; r < n; r++) t[r] < e && (e = t[r]);
|
|
58
|
+
this.floor = e;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
addGap(e) {
|
|
62
|
+
let t = this.sorted, r = this.window;
|
|
63
|
+
if (this.gapCount === r) {
|
|
64
|
+
let i = this.gaps[this.gapHead], a = n(t, r, i);
|
|
65
|
+
t.copyWithin(a, a + 1, r), this.gapCount--, this.gaps[this.gapHead] = e, this.gapHead = (this.gapHead + 1) % r;
|
|
66
|
+
} else this.gaps[(this.gapHead + this.gapCount) % r] = e;
|
|
67
|
+
let i = n(t, this.gapCount, e);
|
|
68
|
+
t.copyWithin(i + 1, i, this.gapCount), t[i] = e, this.gapCount++;
|
|
69
|
+
}
|
|
70
|
+
}, n = (e, t, n) => {
|
|
71
|
+
let r = 0, i = t;
|
|
72
|
+
for (; r < i;) {
|
|
73
|
+
let t = r + i >>> 1;
|
|
74
|
+
e[t] < n ? r = t + 1 : i = t;
|
|
75
|
+
}
|
|
76
|
+
return r;
|
|
77
|
+
};
|
|
78
|
+
//#endregion
|
|
79
|
+
export { t as AdaptiveDelay };
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How one field blends between snapshots.
|
|
3
|
+
* - `linear`: a straight line, for scale, opacity, a blend weight. Holds past the newest snapshot.
|
|
4
|
+
* - `hermite`: cubic through the two ends using the `velocity` field, which must be a `linear` field of the same
|
|
5
|
+
* size. Runs on at that velocity past the newest snapshot, up to `maxPrediction`.
|
|
6
|
+
* - `angle`: radians, the short way round.
|
|
7
|
+
* - `quaternion`: `[x, y, z, w]`, normalized when pushed, shortest-arc slerp.
|
|
8
|
+
* - `color`: RGB or RGBA from 0 to 1, blended in linear light. `space` says what you pass and get back: `'srgb'`
|
|
9
|
+
* (default) or `'linear'`. Alpha is always straight.
|
|
10
|
+
* - `time`: animation time in seconds. With `period` it loops, so 0.9 to 0.1 of a 1 s clip goes forward through the
|
|
11
|
+
* seam. With `clip`, the name of a `step` field, it never blends across a change of clip. Runs on at its last rate
|
|
12
|
+
* past the newest snapshot, up to `maxPrediction`.
|
|
13
|
+
* - `step`: no blend, for clip numbers and flags. Changes when the next snapshot's time is reached.
|
|
14
|
+
*
|
|
15
|
+
* `size` is how many numbers the field has. Left out, `linear` and `step` hold one plain number, `hermite` and `color`
|
|
16
|
+
* hold 3.
|
|
17
|
+
*/
|
|
18
|
+
export type FieldSpec = {
|
|
19
|
+
readonly type: 'linear' | 'step';
|
|
20
|
+
readonly size?: number;
|
|
21
|
+
} | {
|
|
22
|
+
readonly type: 'hermite';
|
|
23
|
+
readonly velocity: string;
|
|
24
|
+
readonly size?: number;
|
|
25
|
+
} | {
|
|
26
|
+
readonly type: 'angle';
|
|
27
|
+
} | {
|
|
28
|
+
readonly type: 'quaternion';
|
|
29
|
+
} | {
|
|
30
|
+
readonly type: 'color';
|
|
31
|
+
readonly size?: 3 | 4;
|
|
32
|
+
readonly space?: 'srgb' | 'linear';
|
|
33
|
+
} | {
|
|
34
|
+
readonly type: 'time';
|
|
35
|
+
readonly period?: number;
|
|
36
|
+
readonly clip?: string;
|
|
37
|
+
};
|
|
38
|
+
/** The fields of a {@link FieldBuffer}, by name. */
|
|
39
|
+
export type FieldSpecs = Readonly<Record<string, FieldSpec>>;
|
|
40
|
+
/** What a sample holds: a plain number for one-number fields, a Float64Array for the rest. */
|
|
41
|
+
export type FieldValues<F extends FieldSpecs> = {
|
|
42
|
+
[K in keyof F]: F[K] extends {
|
|
43
|
+
readonly size: number;
|
|
44
|
+
} ? Float64Array : F[K] extends {
|
|
45
|
+
readonly type: 'quaternion' | 'color' | 'hermite';
|
|
46
|
+
} ? Float64Array : number;
|
|
47
|
+
};
|
|
48
|
+
/** What a snapshot passes: a number or an array per field. */
|
|
49
|
+
export type FieldInput<F extends FieldSpecs> = {
|
|
50
|
+
readonly [K in keyof F]?: F[K] extends {
|
|
51
|
+
readonly size: number;
|
|
52
|
+
} ? ArrayLike<number> : F[K] extends {
|
|
53
|
+
readonly type: 'quaternion' | 'color' | 'hermite';
|
|
54
|
+
} ? ArrayLike<number> : number;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* A ring of timestamped snapshots of whatever fields you name, each blended its own way.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* import { FieldBuffer } from '@margoengine/netcode/interpolation-fields';
|
|
62
|
+
*
|
|
63
|
+
* const buffer = new FieldBuffer({
|
|
64
|
+
* scale: { type: 'linear', size: 3 },
|
|
65
|
+
* tint: { type: 'color' },
|
|
66
|
+
* clip: { type: 'step' },
|
|
67
|
+
* clipTime: { type: 'time', period: 1.2, clip: 'clip' },
|
|
68
|
+
* });
|
|
69
|
+
* buffer.push(10, { scale: [1, 1, 1], tint: [1, 0, 0], clip: 2, clipTime: 1.1 });
|
|
70
|
+
* buffer.push(10.1, { scale: [2, 2, 2], tint: [0, 0, 1], clip: 2, clipTime: 0.01 });
|
|
71
|
+
* const now = buffer.sample(10.05);
|
|
72
|
+
* console.log(now?.scale, now?.tint, now?.clipTime);
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export declare class FieldBuffer<const F extends FieldSpecs> {
|
|
76
|
+
/** Snapshots kept. */
|
|
77
|
+
readonly capacity: number;
|
|
78
|
+
/** Seconds `hermite` and `time` fields run on past the newest snapshot. */
|
|
79
|
+
readonly maxPrediction: number;
|
|
80
|
+
private readonly plans;
|
|
81
|
+
private readonly stride;
|
|
82
|
+
private readonly rows;
|
|
83
|
+
private readonly ring;
|
|
84
|
+
private readonly scratch;
|
|
85
|
+
private readonly output;
|
|
86
|
+
/**
|
|
87
|
+
* @param fields The fields by name.
|
|
88
|
+
* @param options `capacity` snapshots kept, an integer 2 to 65536 (default 32); `maxPrediction` in seconds, 0 to 10
|
|
89
|
+
* (default 0.25).
|
|
90
|
+
* @throws TypeError for a field that does not fit {@link FieldSpec}.
|
|
91
|
+
* @throws RangeError for an option out of range.
|
|
92
|
+
*/
|
|
93
|
+
constructor(fields: F, { capacity, maxPrediction }?: {
|
|
94
|
+
capacity?: number;
|
|
95
|
+
maxPrediction?: number;
|
|
96
|
+
});
|
|
97
|
+
/** A blank sample, for use as a reusable `sample` output. */
|
|
98
|
+
create(): FieldValues<F>;
|
|
99
|
+
/**
|
|
100
|
+
* Adds a snapshot at `time` seconds. A field left out keeps its value from the snapshot before, so a snapshot need
|
|
101
|
+
* only hold what changed; the first must hold every field.
|
|
102
|
+
* @returns False for a time not newer than the newest, which changes nothing.
|
|
103
|
+
* @throws RangeError for a time or value that is not finite, or a zero quaternion.
|
|
104
|
+
* @throws TypeError for a value of the wrong shape, or a field missing from the first snapshot.
|
|
105
|
+
*/
|
|
106
|
+
push(time: number, values: FieldInput<F>): boolean;
|
|
107
|
+
/**
|
|
108
|
+
* The fields at `time` seconds, or null while empty. Before the oldest snapshot it gives the oldest. The default
|
|
109
|
+
* output is reused by every call.
|
|
110
|
+
* @throws RangeError for a time that is not finite.
|
|
111
|
+
*/
|
|
112
|
+
sample(time: number, output?: FieldValues<F>): FieldValues<F> | null;
|
|
113
|
+
private hermite;
|
|
114
|
+
private time;
|
|
115
|
+
/** Drops every snapshot. */
|
|
116
|
+
clear(): void;
|
|
117
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { TimeRing as e, blendAngle as t, normalizeQuaternion as n, slerpInto as r } from "./interpolation-timeline.js";
|
|
2
|
+
import { LinearToSRGB as i, SRGBToLinear as a } from "@margoengine/math/color";
|
|
3
|
+
//#region src/interpolation-fields.ts
|
|
4
|
+
var o = (e, t) => /* @__PURE__ */ TypeError(`FieldBuffer field '${e}' ${t}; fix the field list`);
|
|
5
|
+
function s(e) {
|
|
6
|
+
let t = [], n = 1;
|
|
7
|
+
for (let [r, i] of Object.entries(e)) {
|
|
8
|
+
if (![
|
|
9
|
+
"linear",
|
|
10
|
+
"step",
|
|
11
|
+
"hermite",
|
|
12
|
+
"angle",
|
|
13
|
+
"quaternion",
|
|
14
|
+
"color",
|
|
15
|
+
"time"
|
|
16
|
+
].includes(i?.type)) throw o(r, `has type ${i?.type}`);
|
|
17
|
+
let e = i.type === "quaternion" ? 4 : i.type === "angle" || i.type === "time" ? void 0 : i.size, a = i.type === "hermite" || i.type === "color" ? 3 : 1, s = e ?? a;
|
|
18
|
+
if (!Number.isInteger(s) || s < 1 || s > 64) throw o(r, `has size ${s}; pass 1 to 64`);
|
|
19
|
+
if (i.type === "color" && s !== 3 && s !== 4) throw o(r, "needs size 3 or 4");
|
|
20
|
+
let c = i.type === "time" ? i.period ?? 0 : 0;
|
|
21
|
+
if (!(c >= 0) || !Number.isFinite(c)) throw o(r, `has period ${c}`);
|
|
22
|
+
t.push({
|
|
23
|
+
name: r,
|
|
24
|
+
type: i.type,
|
|
25
|
+
offset: n,
|
|
26
|
+
size: s,
|
|
27
|
+
scalar: e === void 0 && a === 1,
|
|
28
|
+
velocity: -1,
|
|
29
|
+
period: c,
|
|
30
|
+
clip: -1,
|
|
31
|
+
srgb: i.type === "color" && (i.space ?? "srgb") === "srgb"
|
|
32
|
+
}), n += s;
|
|
33
|
+
}
|
|
34
|
+
return t.forEach((n, r) => {
|
|
35
|
+
let i = Object.values(e)[r];
|
|
36
|
+
if (i.type === "hermite") {
|
|
37
|
+
let e = t.find((e) => e.name === i.velocity);
|
|
38
|
+
if (e?.type !== "linear" || e.size !== n.size) throw o(n.name, `needs velocity '${i.velocity}' to be a linear field of size ${n.size}`);
|
|
39
|
+
n.velocity = e.offset;
|
|
40
|
+
}
|
|
41
|
+
if (i.type === "time" && i.clip !== void 0) {
|
|
42
|
+
let e = t.find((e) => e.name === i.clip);
|
|
43
|
+
if (e?.type !== "step" || e.size !== 1) throw o(n.name, `needs clip '${i.clip}' to be a step field`);
|
|
44
|
+
n.clip = e.offset;
|
|
45
|
+
}
|
|
46
|
+
}), {
|
|
47
|
+
plans: t,
|
|
48
|
+
floats: n
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
var c = class {
|
|
52
|
+
constructor(t, { capacity: n = 32, maxPrediction: r = .25 } = {}) {
|
|
53
|
+
if (!(r >= 0 && r <= 10)) throw RangeError(`FieldBuffer maxPrediction is ${r}; pass 0 to 10 seconds`);
|
|
54
|
+
let i = s(t);
|
|
55
|
+
if (this.plans = i.plans, this.stride = i.floats, this.capacity = n, this.maxPrediction = r, !Number.isInteger(n) || n < 2 || n > 65536) throw RangeError(`FieldBuffer capacity is ${n}; pass an integer from 2 to 65536`);
|
|
56
|
+
this.rows = new Float64Array(n * this.stride), this.ring = new e(n, this.rows, this.stride), this.scratch = new Float64Array(this.stride), this.output = this.create();
|
|
57
|
+
}
|
|
58
|
+
create() {
|
|
59
|
+
let e = {};
|
|
60
|
+
for (let t of this.plans) e[t.name] = t.scalar ? 0 : new Float64Array(t.size);
|
|
61
|
+
return e;
|
|
62
|
+
}
|
|
63
|
+
push(e, t) {
|
|
64
|
+
if (!Number.isFinite(e)) throw RangeError(`FieldBuffer time is ${e}; pass a finite number of seconds`);
|
|
65
|
+
let r = this.scratch, i = this.ring.last, o = t;
|
|
66
|
+
for (let e of this.plans) {
|
|
67
|
+
let t = o[e.name];
|
|
68
|
+
if (t === void 0) {
|
|
69
|
+
if (i < 0) throw TypeError(`FieldBuffer's first snapshot has no '${e.name}'; pass every field`);
|
|
70
|
+
for (let t = 0; t < e.size; t++) r[e.offset + t] = this.rows[i * this.stride + e.offset + t];
|
|
71
|
+
} else {
|
|
72
|
+
if (e.scalar ? typeof t != "number" : typeof t == "number" || t.length !== e.size) throw TypeError(`FieldBuffer field '${e.name}' needs ${e.scalar ? "a number" : `${e.size} numbers`}`);
|
|
73
|
+
for (let n = 0; n < e.size; n++) {
|
|
74
|
+
let i = e.scalar ? t : t[n];
|
|
75
|
+
if (!Number.isFinite(i)) throw RangeError(`FieldBuffer field '${e.name}' must be finite`);
|
|
76
|
+
r[e.offset + n] = e.srgb && n < 3 ? a(i) : i;
|
|
77
|
+
}
|
|
78
|
+
e.type === "quaternion" && n(t, r, e.offset);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
let s = this.ring.push(e);
|
|
82
|
+
return s < 0 ? !1 : (this.rows.set(r.subarray(1), s * this.stride + 1), !0);
|
|
83
|
+
}
|
|
84
|
+
sample(e, n = this.output) {
|
|
85
|
+
if (!Number.isFinite(e)) throw RangeError(`FieldBuffer time is ${e}; pass a finite number of seconds`);
|
|
86
|
+
let a = this.ring;
|
|
87
|
+
if (!a.locate(e)) return null;
|
|
88
|
+
let o = this.rows, s = this.scratch, c = a.a * this.stride, l = a.b * this.stride, u = a.u, d = o[l] - o[c], f = Math.min(a.past, this.maxPrediction);
|
|
89
|
+
for (let e of this.plans) {
|
|
90
|
+
let n = e.offset;
|
|
91
|
+
switch (e.type) {
|
|
92
|
+
case "step":
|
|
93
|
+
for (let t = 0; t < e.size; t++) s[n + t] = o[c + n + t];
|
|
94
|
+
break;
|
|
95
|
+
case "angle":
|
|
96
|
+
s[n] = t(o[c + n], o[l + n], u);
|
|
97
|
+
break;
|
|
98
|
+
case "quaternion":
|
|
99
|
+
if (c === l) for (let e = 0; e < 4; e++) s[n + e] = o[c + n + e];
|
|
100
|
+
else r(s, n, o, c + n, l + n, u);
|
|
101
|
+
break;
|
|
102
|
+
case "hermite":
|
|
103
|
+
this.hermite(e, c, l, u, d, f);
|
|
104
|
+
break;
|
|
105
|
+
case "time":
|
|
106
|
+
s[n] = this.time(e, c, l, u, f);
|
|
107
|
+
break;
|
|
108
|
+
default: for (let t = 0; t < e.size; t++) s[n + t] = o[c + n + t] + (o[l + n + t] - o[c + n + t]) * u;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
let p = n;
|
|
112
|
+
for (let e of this.plans) {
|
|
113
|
+
if (e.scalar) {
|
|
114
|
+
p[e.name] = s[e.offset];
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
let t = p[e.name];
|
|
118
|
+
if (t?.length !== e.size) throw TypeError(`FieldBuffer output has no '${e.name}'; use create()`);
|
|
119
|
+
for (let n = 0; n < e.size; n++) t[n] = e.srgb && n < 3 ? i(s[e.offset + n]) : s[e.offset + n];
|
|
120
|
+
}
|
|
121
|
+
return n;
|
|
122
|
+
}
|
|
123
|
+
hermite(e, t, n, r, i, a) {
|
|
124
|
+
let o = this.rows, s = this.scratch, c = e.offset, l = e.velocity;
|
|
125
|
+
if (t === n) {
|
|
126
|
+
for (let n = 0; n < e.size; n++) s[c + n] = o[t + c + n] + o[t + l + n] * a;
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
let u = r * r, d = u * r;
|
|
130
|
+
for (let a = 0; a < e.size; a++) s[c + a] = (2 * d - 3 * u + 1) * o[t + c + a] + (d - 2 * u + r) * i * o[t + l + a] + (-2 * d + 3 * u) * o[n + c + a] + (d - u) * i * o[n + l + a];
|
|
131
|
+
}
|
|
132
|
+
time(e, t, n, r, i) {
|
|
133
|
+
let a = this.rows, o = e.offset, s = e.period, c = (e) => s ? e - Math.floor(e / s) * s : e, l = (e, t) => {
|
|
134
|
+
let n = t - e;
|
|
135
|
+
return s ? c(n + s / 2) - s / 2 : n;
|
|
136
|
+
};
|
|
137
|
+
if (t !== n) return e.clip >= 0 && a[t + e.clip] !== a[n + e.clip] ? a[t + o] : c(a[t + o] + l(a[t + o], a[n + o]) * r);
|
|
138
|
+
if (!i || this.ring.previous < 0) return a[t + o];
|
|
139
|
+
let u = this.ring.previous * this.stride;
|
|
140
|
+
return e.clip >= 0 && a[u + e.clip] !== a[t + e.clip] ? a[t + o] : c(a[t + o] + l(a[u + o], a[t + o]) / (a[t] - a[u]) * i);
|
|
141
|
+
}
|
|
142
|
+
clear() {
|
|
143
|
+
this.ring.clear();
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
//#endregion
|
|
147
|
+
export { c as FieldBuffer };
|