@bendyline/molen-client 0.0.9 → 0.0.11

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,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 };
@@ -1,4 +1,4 @@
1
- import { n as InputGamepad, r as InputMap } from "./input-CD-dHuv0.mjs";
1
+ import { n as InputGamepad, r as InputMap } from "./input-Ckev_Kvj.mjs";
2
2
  import { InputProfile } from "@bendyline/molen-schema";
3
3
  import * as THREE from "three";
4
4
  import { Octree } from "three/addons/math/Octree.js";
@@ -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;
@@ -161,13 +165,16 @@ declare const NAVIGATION_ACTIONS: {
161
165
  readonly jump: "nav-jump";
162
166
  readonly interact: "nav-interact";
163
167
  readonly view: "nav-view";
168
+ readonly boost: "nav-boost";
164
169
  readonly throttleUp: "nav-throttle-up";
165
170
  readonly throttleDown: "nav-throttle-down";
171
+ readonly power: "nav-power";
166
172
  readonly yawLeft: "nav-yaw-left";
167
173
  readonly yawRight: "nav-yaw-right";
168
174
  readonly engine: "nav-engine";
169
175
  readonly gear: "nav-gear";
170
176
  readonly flaps: "nav-flaps";
177
+ readonly vector: "nav-vector";
171
178
  readonly recover: "nav-recover";
172
179
  };
173
180
  /** The navigation modes with a default input profile. */
@@ -178,12 +185,15 @@ type NavigationProfileName = "orbit" | "fly" | "walk" | "drive" | "pilot";
178
185
  * 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 ←/→)
179
186
  * bank, Q/Z work the rudder or pedals, Shift/Ctrl (or +/−, Page Up/Down) set throttle or
180
187
  * collective, I toggles the engine, G the gear, F the flaps, R recovers after an impact, Space
181
- * brakes, V switches the view and E leaves. Hosts may replace any profile through
188
+ * brakes, V switches the view and E leaves. T toggles travel boost in walk, drive and pilot.
189
+ * Hosts may replace any profile through
182
190
  * `InputMap.defineProfile`.
183
191
  */
184
192
  declare function navigationInputProfiles(): Record<NavigationProfileName, InputProfile>;
185
193
  /** Flight controls beyond pitch (`-move.forward`) and roll (`move.right`), in the pilot profile. */
186
194
  interface PilotInput {
195
+ /** Absolute throttle/collective, 0..1, only while a mapped hardware axis is connected. */
196
+ power?: number;
187
197
  /** Held throttle or collective change, -1..1 (more power positive). */
188
198
  throttle: number;
189
199
  /** Held rudder or pedals, -1..1 (right positive). */
@@ -192,6 +202,8 @@ interface PilotInput {
192
202
  engine: number;
193
203
  gear: number;
194
204
  flaps: number;
205
+ /** Powered-lift lever advances through 0, 25, 50, 75, 100 percent on each press. */
206
+ vector?: number;
195
207
  recover: number;
196
208
  }
197
209
  /** Everything a navigation controller reads in one frame. */
@@ -217,6 +229,8 @@ interface NavigationInput {
217
229
  interact: number;
218
230
  /** Presses of the view toggle since the previous frame (edge-triggered). */
219
231
  view: number;
232
+ /** Presses of the 4× travel boost toggle (T), edge-triggered. */
233
+ boost?: number;
220
234
  /** Clicks/taps in element-relative CSS pixels. */
221
235
  taps: Array<{
222
236
  x: number;
@@ -256,6 +270,7 @@ declare class NavigationInputSource {
256
270
  readonly pointer: NavigationPointer;
257
271
  private interactPresses;
258
272
  private viewPresses;
273
+ private boostPresses;
259
274
  private profile;
260
275
  private readonly pilotPresses;
261
276
  private readonly lookSpeed;
@@ -267,6 +282,20 @@ declare class NavigationInputSource {
267
282
  dispose(): void;
268
283
  }
269
284
  //#endregion
285
+ //#region src/navigation/controls.d.ts
286
+ interface NavigationControlsOptions {
287
+ /** Button destination. The editor opens in a modal dialog. */
288
+ container: HTMLElement;
289
+ focusTarget: HTMLElement;
290
+ /** Opt into localStorage persistence; use the same key to share mappings between hosts. */
291
+ storageKey?: string;
292
+ }
293
+ interface NavigationControls {
294
+ dispose(): void;
295
+ }
296
+ /** Controller discovery and raw axis/button mapping for the built-in navigation profiles. */
297
+ declare function createNavigationControls(source: NavigationInputSource, options: NavigationControlsOptions): NavigationControls;
298
+ //#endregion
270
299
  //#region src/navigation/orbit.d.ts
271
300
  /** The orbit camera's framing. */
272
301
  interface OrbitState {
@@ -496,6 +525,13 @@ interface WalkCollisionOptions {
496
525
  rescanIntervalMs?: number;
497
526
  /** Clock for the rescan interval, ms (default `performance.now`). */
498
527
  now?: () => number;
528
+ /**
529
+ * Collide with each instance of an instanced mesh as its bounding box, twelve triangles, instead
530
+ * of its own triangles (default false). An aircraft's neighborhood over a forest held 100k
531
+ * triangles of shrubs and crowns, about 110 ms to rebuild every 24 m; its probes and camera
532
+ * rays need the trees' extent, not their leaves.
533
+ */
534
+ instanceBounds?: boolean;
499
535
  }
500
536
  /**
501
537
  * A small collision neighborhood in metric coordinates, independent of floating-origin shifts.
@@ -504,7 +540,11 @@ interface WalkCollisionOptions {
504
540
  * neighborhood. Large geometries (terrain tiles, building cells) are walked through a per-geometry
505
541
  * spatial index, so a rebuild costs the triangles near the subject rather than the whole tile.
506
542
  * Meshes opt out with `userData.walkIgnore`, and water materials (`userData.terrainWaterMaterial`)
507
- * and `semantic:landcover` overlays are never solid.
543
+ * are never solid. Land cover is a solid walking surface: its visible elevation can differ from
544
+ * bare terrain, and full-tile land cover can replace the bare surface altogether.
545
+ * A group whose contents lie within an X/Z rectangle of its own frame declares it as
546
+ * `userData.walkFootprint` (`[minX, minZ, maxX, maxZ]`, like a terrain tile): a scan skips the
547
+ * whole group when that rectangle is out of reach.
508
548
  */
509
549
  declare class WalkCollision {
510
550
  readonly origin: THREE.Vector3;
@@ -515,6 +555,7 @@ declare class WalkCollision {
515
555
  private readonly verticalRadius;
516
556
  private readonly rescanIntervalMs;
517
557
  private readonly now;
558
+ private readonly instanceBounds;
518
559
  private meshes;
519
560
  private initialized;
520
561
  /** Vertical center of the current neighborhood, or undefined when it spans the whole column. */
@@ -560,6 +601,8 @@ interface WalkInput {
560
601
  * terrain pauses movement (it is never a bottomless pit) until coverage arrives.
561
602
  */
562
603
  declare class WalkController {
604
+ /** Four times walking/running speed; jump height and look response stay normal. */
605
+ boostMode: boolean;
563
606
  readonly feet: THREE.Vector3;
564
607
  readonly velocity: THREE.Vector3;
565
608
  grounded: boolean;
@@ -569,10 +612,13 @@ declare class WalkController {
569
612
  private jumpHeld;
570
613
  private readonly capsule;
571
614
  reset(x: number, z: number): void;
572
- /** Find open ground near the fly camera, rather than dropping through a building's roof. */
573
- place(collision: WalkCollision, sampleHeight: (x: number, z: number) => number | undefined): boolean;
615
+ /**
616
+ * Find supported open ground. A separate `groundCollision` can exclude vehicles from the
617
+ * landing ray while `collision` still rejects body overlaps, placing people beside cars.
618
+ */
619
+ place(collision: WalkCollision, sampleHeight: (x: number, z: number) => number | undefined, groundCollision?: WalkCollision): boolean;
574
620
  update(dt: number, input: WalkInput, collision: WalkCollision, sampleHeight: (x: number, z: number) => number | undefined): void;
575
621
  private setCapsule;
576
622
  }
577
623
  //#endregion
578
- export { type CameraOrientation, CollisionTree, FlyController, type FlyControllerOptions, FollowController, type FollowControllerOptions, type FollowSubject, MAX_LOOK_PITCH, NAVIGATION_ACTIONS, type NavigationEnvironment, type NavigationGestures, type NavigationInput, NavigationInputSource, type NavigationInputSourceOptions, type NavigationKeyTarget, NavigationPointer, type NavigationPointerEvent, type NavigationPointerOptions, type NavigationPointerSurface, type NavigationPose, type NavigationProfileName, type NavigationVector, type NavigationWheelEvent, OrbitController, type OrbitControllerOptions, type OrbitFlyToOptions, type OrbitState, type PilotInput, RUN_SPEED, type TouchJoystick, type TouchJoystickOptions, TouchStick, type TouchStickOptions, WALK_EYE_HEIGHT, WALK_PLACEMENT_RADIUS, WALK_SPEED, WalkCollision, type WalkCollisionOptions, WalkController, type WalkInput, applyCameraLookDelta, cameraForward, cameraPlanarForward, cameraRight, createTouchJoystick, idleNavigationInput, navigationInputProfiles, orientationFromDirection, viewNeedsImmediateUpdate };
624
+ export { type CameraOrientation, CollisionTree, FlyController, type FlyControllerOptions, FollowController, type FollowControllerOptions, type FollowSubject, MAX_LOOK_PITCH, NAVIGATION_ACTIONS, type NavigationControls, type NavigationControlsOptions, type NavigationEnvironment, type NavigationGestures, type NavigationInput, NavigationInputSource, type NavigationInputSourceOptions, type NavigationKeyTarget, NavigationPointer, type NavigationPointerEvent, type NavigationPointerOptions, type NavigationPointerSurface, type NavigationPose, type NavigationProfileName, type NavigationVector, type NavigationWheelEvent, OrbitController, type OrbitControllerOptions, type OrbitFlyToOptions, type OrbitState, type PilotInput, RUN_SPEED, type TouchJoystick, type TouchJoystickOptions, TouchStick, type TouchStickOptions, WALK_EYE_HEIGHT, WALK_PLACEMENT_RADIUS, WALK_SPEED, WalkCollision, type WalkCollisionOptions, WalkController, type WalkInput, applyCameraLookDelta, cameraForward, cameraPlanarForward, cameraRight, createNavigationControls, createTouchJoystick, idleNavigationInput, navigationInputProfiles, orientationFromDirection, viewNeedsImmediateUpdate };