@bendyline/molen-client 0.0.8 → 0.0.10

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.
@@ -0,0 +1,137 @@
1
+ import { i as compileModelSignalExpressions, n as modelSignalDisplacement, r as readModelSignals, t as createModelSignalVisual } from "./model-signals-DLgvjBeZ.mjs";
2
+ //#region src/instruments.ts
3
+ /** The same binding can calibrate tick placement with modelSignalDisplacement(binding, reading). */
4
+ function dialInstrument(options) {
5
+ return {
6
+ node: options.node,
7
+ source: options.source,
8
+ property: "rotation",
9
+ axis: options.axis ?? "z",
10
+ keys: options.calibration.map(([reading, angle]) => [reading, angle]),
11
+ responseSeconds: options.responseSeconds ?? 0,
12
+ ...options.period === void 0 ? {} : { period: options.period },
13
+ missing: options.missing ?? "hold"
14
+ };
15
+ }
16
+ /** Three independent, continuous needles: one turn per 1,000 / 10,000 / 100,000 feet. */
17
+ function altimeterInstrument(options) {
18
+ return [
19
+ [options.hundreds, 1e3],
20
+ [options.thousands, 1e4],
21
+ [options.tenThousands, 1e5]
22
+ ].flatMap(([node, feet]) => node === void 0 ? [] : [{
23
+ node,
24
+ source: options.source,
25
+ property: "rotation",
26
+ axis: options.axis ?? "z",
27
+ scale: (options.direction ?? -1) * 2 * Math.PI / feet,
28
+ period: 2 * Math.PI,
29
+ responseSeconds: options.responseSeconds ?? 0
30
+ }]);
31
+ }
32
+ /** Moving horizon behind a fixed aircraft symbol. Geometry supplies the circular mask. */
33
+ function attitudeInstrument(options) {
34
+ return [{
35
+ node: options.bankNode,
36
+ source: options.rollSource ?? "roll",
37
+ property: "rotation",
38
+ axis: options.bankAxis ?? "z",
39
+ scale: options.bankDirection ?? -1,
40
+ period: 2 * Math.PI,
41
+ responseSeconds: options.responseSeconds ?? 0
42
+ }, {
43
+ node: options.pitchNode,
44
+ source: options.pitchSource ?? "pitch",
45
+ property: "position",
46
+ axis: options.pitchAxis ?? "y",
47
+ scale: options.pitchScale,
48
+ min: -Math.abs(options.pitchLimit),
49
+ max: Math.abs(options.pitchLimit),
50
+ responseSeconds: options.responseSeconds ?? 0
51
+ }];
52
+ }
53
+ /** Own an authored lamp/flag node; positive means on, missing readings turn it off. */
54
+ function annunciatorInstrument(node, source) {
55
+ return {
56
+ node,
57
+ source,
58
+ property: "visible",
59
+ scale: 1,
60
+ missing: "hide"
61
+ };
62
+ }
63
+ /** Fixed-width unsigned integer display; no DOM, textures or per-frame glyph allocation. */
64
+ function numericInstrument(options) {
65
+ if (!options.prefix || options.digits.length < 1 || options.digits.length > 9) throw new Error("Numeric instruments require a prefix and 1–9 digits");
66
+ const derived = Object.create(null);
67
+ const bindings = [];
68
+ const whole = `${options.prefix}.whole`;
69
+ derived[whole] = {
70
+ op: "floor",
71
+ args: [options.source]
72
+ };
73
+ const segmentMasks = [
74
+ 63,
75
+ 6,
76
+ 91,
77
+ 79,
78
+ 102,
79
+ 109,
80
+ 125,
81
+ 7,
82
+ 127,
83
+ 111
84
+ ];
85
+ const valid = `${options.prefix}.valid`, positive = `${options.prefix}.positive`, inRange = `${options.prefix}.inRange`;
86
+ derived[positive] = {
87
+ op: "greater",
88
+ args: [whole, -1]
89
+ };
90
+ derived[inRange] = {
91
+ op: "less",
92
+ args: [whole, 10 ** options.digits.length]
93
+ };
94
+ derived[valid] = {
95
+ op: "and",
96
+ args: [positive, inRange]
97
+ };
98
+ options.digits.forEach((nodes, index) => {
99
+ const quotient = `${options.prefix}.${index}.quotient`, integer = `${options.prefix}.${index}.integer`;
100
+ const digit = `${options.prefix}.${index}.digit`, display = `${options.prefix}.${index}.display`;
101
+ derived[quotient] = {
102
+ op: "divide",
103
+ args: [whole, 10 ** (options.digits.length - index - 1)]
104
+ };
105
+ derived[integer] = {
106
+ op: "floor",
107
+ args: [quotient]
108
+ };
109
+ derived[digit] = {
110
+ op: "modulo",
111
+ args: [integer, 10]
112
+ };
113
+ derived[display] = {
114
+ op: "if",
115
+ args: [
116
+ valid,
117
+ digit,
118
+ -1
119
+ ]
120
+ };
121
+ nodes.forEach((node, segment) => {
122
+ bindings.push({
123
+ node,
124
+ source: display,
125
+ property: "visible",
126
+ missing: "hide",
127
+ keys: [[-1, 0], ...segmentMasks.map((mask, number) => [number, Number((mask & 1 << segment) !== 0)])]
128
+ });
129
+ });
130
+ });
131
+ return {
132
+ derived,
133
+ bindings
134
+ };
135
+ }
136
+ //#endregion
137
+ export { altimeterInstrument, annunciatorInstrument, attitudeInstrument, compileModelSignalExpressions, createModelSignalVisual, dialInstrument, modelSignalDisplacement, numericInstrument, readModelSignals };
@@ -0,0 +1,25 @@
1
+ import { ModelSignalBinding, ModelSignalSource, ModelSignalSpec } from "@bendyline/molen-schema";
2
+ import { Object3D } from "three";
3
+
4
+ //#region src/model-signals.d.ts
5
+ type ModelSignals = Readonly<Record<string, number | undefined>>;
6
+ interface ModelSignalVisual {
7
+ readonly missingNodes: readonly string[];
8
+ /** Unavailable binding sources on the most recent update (including invalid formulas). */
9
+ readonly missingSignals: readonly string[];
10
+ /** Absolute simulation time enables responseSeconds; omission applies readings immediately. */
11
+ update(signals: ModelSignals, timeSeconds?: number): void;
12
+ /** Restore authored transforms before removing or replacing bindings. */
13
+ reset(): void;
14
+ }
15
+ /** Read data only: no expressions, evaluation, prototype traversal, or simulation writes. */
16
+ declare function readModelSignals(sources: Readonly<Record<string, ModelSignalSource>> | undefined, readComponent: (name: string) => unknown): ModelSignals;
17
+ /**
18
+ * A binding's displacement for a signal, before its limits: linear in the signal, or along its
19
+ * keyframed curve, which holds its end values outside the keys. Exported for tests.
20
+ */
21
+ declare function modelSignalDisplacement(binding: ModelSignalBinding, signal: number): number;
22
+ /** Model-agnostic; moves existing geometry without creating a cockpit or changing physics. */
23
+ declare function createModelSignalVisual(model: Object3D, spec: ModelSignalSpec): ModelSignalVisual;
24
+ //#endregion
25
+ export { readModelSignals as a, modelSignalDisplacement as i, ModelSignals as n, createModelSignalVisual as r, ModelSignalVisual as t };
@@ -0,0 +1,172 @@
1
+ import { modelSignalDerivedSchema } from "@bendyline/molen-schema";
2
+ //#region src/model-signal-expressions.ts
3
+ function evaluate(expression, values) {
4
+ const args = expression.args.map((arg) => typeof arg === "number" ? arg : Object.hasOwn(values, arg) ? values[arg] : void 0);
5
+ const [a, b, c] = args;
6
+ if (expression.op === "if") return a === void 0 ? void 0 : a !== 0 ? b : c;
7
+ if (expression.op === "coalesce") return args.find((value) => value !== void 0);
8
+ if (args.some((value) => value === void 0)) return void 0;
9
+ const numbers = args;
10
+ const x = a, y = b, z = c;
11
+ switch (expression.op) {
12
+ case "add": return numbers.reduce((sum, value) => sum + value, 0);
13
+ case "subtract": return x - y;
14
+ case "multiply": return numbers.reduce((product, value) => product * value, 1);
15
+ case "divide": return y === 0 ? void 0 : x / y;
16
+ case "min": return Math.min(...numbers);
17
+ case "max": return Math.max(...numbers);
18
+ case "clamp": return y > z ? void 0 : Math.max(y, Math.min(z, x));
19
+ case "abs": return Math.abs(x);
20
+ case "sqrt": return x < 0 ? void 0 : Math.sqrt(x);
21
+ case "floor": return Math.floor(x);
22
+ case "modulo": return y <= 0 ? void 0 : (x % y + y) % y;
23
+ case "sin": return Math.sin(x);
24
+ case "cos": return Math.cos(x);
25
+ case "atan2": return Math.atan2(x, y);
26
+ case "less": return Number(x < y);
27
+ case "greater": return Number(x > y);
28
+ case "equal": return Number(x === y);
29
+ case "and": return Number(numbers.every((value) => value !== 0));
30
+ case "or": return Number(numbers.some((value) => value !== 0));
31
+ case "not": return Number(x === 0);
32
+ }
33
+ }
34
+ /** Compile a bounded dependency graph once. Evaluation is read-only and never executes code. */
35
+ function compileModelSignalExpressions(definitions) {
36
+ const derived = modelSignalDerivedSchema.parse(definitions ?? {});
37
+ const order = [], visited = /* @__PURE__ */ new Set();
38
+ const visit = (name) => {
39
+ if (visited.has(name)) return;
40
+ visited.add(name);
41
+ const expression = derived[name];
42
+ if (!expression) return;
43
+ for (const arg of expression.args) if (typeof arg === "string" && Object.hasOwn(derived, arg)) visit(arg);
44
+ order.push([name, expression]);
45
+ };
46
+ for (const name of Object.keys(derived)) visit(name);
47
+ return (sources) => {
48
+ const values = Object.create(null);
49
+ for (const [name, value] of Object.entries(sources)) if (typeof value === "number" && Number.isFinite(value)) values[name] = value;
50
+ for (const [name, expression] of order) {
51
+ const value = evaluate(expression, values);
52
+ values[name] = value !== void 0 && Number.isFinite(value) ? value : void 0;
53
+ }
54
+ return values;
55
+ };
56
+ }
57
+ //#endregion
58
+ //#region src/model-signals.ts
59
+ /** Read data only: no expressions, evaluation, prototype traversal, or simulation writes. */
60
+ function readModelSignals(sources, readComponent) {
61
+ const signals = Object.create(null);
62
+ for (const [name, source] of Object.entries(sources ?? {})) {
63
+ signals[name] = void 0;
64
+ signals[name] = void 0;
65
+ let value = readComponent(source.component);
66
+ for (const part of source.path) {
67
+ if (value === null || typeof value !== "object" || !Object.hasOwn(value, part)) {
68
+ value = void 0;
69
+ break;
70
+ }
71
+ value = value[part];
72
+ }
73
+ if (typeof value === "boolean") signals[name] = Number(value);
74
+ else if (typeof value === "number" && Number.isFinite(value)) signals[name] = value;
75
+ }
76
+ return signals;
77
+ }
78
+ /**
79
+ * A binding's displacement for a signal, before its limits: linear in the signal, or along its
80
+ * keyframed curve, which holds its end values outside the keys. Exported for tests.
81
+ */
82
+ function modelSignalDisplacement(binding, signal) {
83
+ const keys = binding.keys;
84
+ if (keys === void 0) return signal * (binding.scale ?? 1) + (binding.offset ?? 0);
85
+ const first = keys[0];
86
+ const last = keys[keys.length - 1];
87
+ if (first === void 0 || last === void 0) return 0;
88
+ if (signal <= first[0]) return first[1];
89
+ if (signal >= last[0]) return last[1];
90
+ let index = 1;
91
+ while (index < keys.length - 1 && signal > (keys[index]?.[0] ?? Infinity)) index++;
92
+ const [x0, y0] = keys[index - 1] ?? first;
93
+ const [x1, y1] = keys[index] ?? last;
94
+ let f = (signal - x0) / (x1 - x0);
95
+ if (binding.ease === "smooth") f = f * f * (3 - 2 * f);
96
+ return y0 + (y1 - y0) * f;
97
+ }
98
+ /** Model-agnostic; moves existing geometry without creating a cockpit or changing physics. */
99
+ function createModelSignalVisual(model, spec) {
100
+ const derive = compileModelSignalExpressions(spec.derived);
101
+ let missingSignals = [];
102
+ const nodes = /* @__PURE__ */ new Map();
103
+ model.traverse((node) => {
104
+ const matches = nodes.get(node.name) ?? [];
105
+ matches.push(node);
106
+ nodes.set(node.name, matches);
107
+ });
108
+ const missingNodes = [...new Set(spec.bindings.map((b) => b.node))].filter((name) => !nodes.has(name));
109
+ const bindings = spec.bindings.flatMap((binding) => (nodes.get(binding.node) ?? []).map((node) => {
110
+ node.matrixAutoUpdate = true;
111
+ return {
112
+ binding,
113
+ node,
114
+ rest: binding.property === "visible" ? Number(node.visible) : node[binding.property][binding.axis ?? "z"],
115
+ current: void 0,
116
+ time: void 0
117
+ };
118
+ }));
119
+ return {
120
+ missingNodes,
121
+ get missingSignals() {
122
+ return missingSignals;
123
+ },
124
+ update(sources, timeSeconds) {
125
+ const signals = derive(sources);
126
+ const missing = /* @__PURE__ */ new Set();
127
+ const time = timeSeconds !== void 0 && Number.isFinite(timeSeconds) ? timeSeconds : void 0;
128
+ for (const state of bindings) {
129
+ const { binding, node, rest } = state;
130
+ const write = (value) => {
131
+ if (binding.property === "visible") node.visible = value > 0;
132
+ else node[binding.property][binding.axis ?? "z"] = value;
133
+ };
134
+ const signal = signals[binding.source];
135
+ const raw = signal === void 0 ? NaN : modelSignalDisplacement(binding, signal);
136
+ if (!Number.isFinite(raw)) {
137
+ missing.add(binding.source);
138
+ state.time = void 0;
139
+ if (binding.missing === "rest" || binding.missing === "hide") {
140
+ write(binding.missing === "hide" ? 0 : rest);
141
+ state.current = void 0;
142
+ } else write(state.current === void 0 ? rest : binding.property === "visible" ? state.current : rest + state.current);
143
+ continue;
144
+ }
145
+ let value = Math.max(binding.min ?? -Infinity, Math.min(binding.max ?? Infinity, raw));
146
+ const period = binding.period;
147
+ const wrap = (v) => period === void 0 ? v : (v % period + period) % period;
148
+ value = wrap(value);
149
+ if (binding.responseSeconds && time !== void 0 && state.time !== void 0 && time >= state.time && state.current !== void 0) {
150
+ let delta = value - state.current;
151
+ if (period !== void 0) delta = ((delta + period / 2) % period + period) % period - period / 2;
152
+ value = wrap(state.current + delta * -Math.expm1(-(time - state.time) / binding.responseSeconds));
153
+ }
154
+ state.current = value;
155
+ state.time = time;
156
+ write(binding.property === "visible" ? value : rest + value);
157
+ }
158
+ missingSignals = [...missing];
159
+ },
160
+ reset() {
161
+ for (const state of bindings) {
162
+ const { binding, node, rest } = state;
163
+ if (binding.property === "visible") node.visible = rest > 0;
164
+ else node[binding.property][binding.axis ?? "z"] = rest;
165
+ state.current = state.time = void 0;
166
+ }
167
+ missingSignals = [];
168
+ }
169
+ };
170
+ }
171
+ //#endregion
172
+ export { compileModelSignalExpressions as i, modelSignalDisplacement as n, readModelSignals as r, createModelSignalVisual as t };
@@ -30,6 +30,10 @@ declare function viewNeedsImmediateUpdate(previousPosition: NavigationVector, pr
30
30
  * Rendered roads, walls and portal covers can overlap. An octree copies each triangle into
31
31
  * every intersecting cell and repeatedly splits those copies, causing long rebuild stalls.
32
32
  * A balanced bounding-volume tree stores each triangle once, even for coincident surfaces.
33
+ *
34
+ * Triangle bounds and centers live in flat arrays and each split selects its median instead of
35
+ * sorting: an aircraft's neighborhood over dense ground cover held 170k triangles, and sorting
36
+ * them at every level of the tree took 700 ms of one frame.
33
37
  */
34
38
  declare class CollisionTree extends Octree {
35
39
  override build(): this;
@@ -56,6 +60,12 @@ interface NavigationPointerSurface {
56
60
  ownerDocument?: {
57
61
  pointerLockElement?: unknown;
58
62
  exitPointerLock?(): void;
63
+ addEventListener?(type: string, listener: () => void): void;
64
+ removeEventListener?(type: string, listener: () => void): void;
65
+ defaultView?: {
66
+ addEventListener(type: string, listener: () => void): void;
67
+ removeEventListener(type: string, listener: () => void): void;
68
+ } | null;
59
69
  } | null;
60
70
  }
61
71
  /** The pointer-event fields the tracker reads. */
@@ -162,14 +172,15 @@ declare const NAVIGATION_ACTIONS: {
162
172
  readonly engine: "nav-engine";
163
173
  readonly gear: "nav-gear";
164
174
  readonly flaps: "nav-flaps";
175
+ readonly vector: "nav-vector";
165
176
  readonly recover: "nav-recover";
166
177
  };
167
178
  /** The navigation modes with a default input profile. */
168
179
  type NavigationProfileName = "orbit" | "fly" | "walk" | "drive" | "pilot";
169
180
  /**
170
181
  * Default key/gamepad bindings per navigation mode. Orbit and fly use Q/E for down/up; walk and
171
- * drive use E to interact (board or leave a vehicle), Space to jump or brake, and V to switch
172
- * the vehicle view. Pilot flies an aircraft: W/S (or ↑/↓) pitch the nose down/up, A/D (or ←/→)
182
+ * drive use E to interact (board or leave a vehicle), Space to jump or brake, Shift to sprint
183
+ * or pull the handbrake, and V to switch the vehicle view. Pilot flies an aircraft: W/S (or ↑/↓) pitch the nose down/up, A/D (or ←/→)
173
184
  * bank, Q/Z work the rudder or pedals, Shift/Ctrl (or +/−, Page Up/Down) set throttle or
174
185
  * collective, I toggles the engine, G the gear, F the flaps, R recovers after an impact, Space
175
186
  * brakes, V switches the view and E leaves. Hosts may replace any profile through
@@ -186,6 +197,8 @@ interface PilotInput {
186
197
  engine: number;
187
198
  gear: number;
188
199
  flaps: number;
200
+ /** Powered-lift lever advances through 0, 25, 50, 75, 100 percent on each press. */
201
+ vector?: number;
189
202
  recover: number;
190
203
  }
191
204
  /** Everything a navigation controller reads in one frame. */
@@ -445,6 +458,8 @@ declare class TouchStick {
445
458
  };
446
459
  }
447
460
  interface TouchJoystickOptions extends TouchStickOptions {
461
+ /** Refocus the keyboard surface before moving (e.g. after using a HUD button). */
462
+ focusTarget?: Pick<HTMLElement, "focus">;
448
463
  /** Software-device name in the InputMap (default `'touch-joystick'`). */
449
464
  source?: string;
450
465
  /** Extra class names for styling the root element. */
@@ -488,6 +503,13 @@ interface WalkCollisionOptions {
488
503
  rescanIntervalMs?: number;
489
504
  /** Clock for the rescan interval, ms (default `performance.now`). */
490
505
  now?: () => number;
506
+ /**
507
+ * Collide with each instance of an instanced mesh as its bounding box, twelve triangles, instead
508
+ * of its own triangles (default false). An aircraft's neighborhood over a forest held 100k
509
+ * triangles of shrubs and crowns, about 110 ms to rebuild every 24 m; its probes and camera
510
+ * rays need the trees' extent, not their leaves.
511
+ */
512
+ instanceBounds?: boolean;
491
513
  }
492
514
  /**
493
515
  * A small collision neighborhood in metric coordinates, independent of floating-origin shifts.
@@ -507,6 +529,7 @@ declare class WalkCollision {
507
529
  private readonly verticalRadius;
508
530
  private readonly rescanIntervalMs;
509
531
  private readonly now;
532
+ private readonly instanceBounds;
510
533
  private meshes;
511
534
  private initialized;
512
535
  /** Vertical center of the current neighborhood, or undefined when it spans the whole column. */