@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.
- package/README.md +1 -0
- package/dist/aircraft.d.mts +115 -6
- package/dist/aircraft.mjs +755 -10
- package/dist/audio.d.mts +9 -0
- package/dist/audio.mjs +56 -8
- package/dist/camera-track.d.mts +1 -1
- package/dist/index.d.mts +24 -7
- package/dist/index.mjs +516 -46
- package/dist/{input-BVZjf4T-.mjs → input-BliZYUY1.mjs} +42 -12
- package/dist/{input-CD-dHuv0.d.mts → input-Ckev_Kvj.d.mts} +9 -0
- package/dist/instruments.d.mts +63 -0
- package/dist/instruments.mjs +137 -0
- package/dist/livery-C0abgthY.mjs +302 -0
- package/dist/model-signals-BpeXhsim.d.mts +25 -0
- package/dist/model-signals-DLgvjBeZ.mjs +172 -0
- package/dist/navigation.d.mts +52 -6
- package/dist/navigation.mjs +702 -169
- package/dist/{renderer-BRtQK4jX.d.mts → renderer-Dzus24ZF.d.mts} +147 -10
- package/dist/text.d.mts +24 -0
- package/dist/text.mjs +62 -0
- package/dist/{vehicle-interior-GWcXnvXS.d.mts → vehicle-interior-BgcC750p.d.mts} +1 -1
- package/dist/{vehicle-interior-vJQsRLQy.mjs → vehicle-interior-DfpZwWEh.mjs} +4 -1
- package/dist/vehicles.d.mts +6 -5
- package/dist/vehicles.mjs +67 -25
- package/dist/webgpu-ambient-occlusion-9gnp3qOY.mjs +126 -0
- package/dist/{webgpu-driver-Bt2mvZk9.mjs → webgpu-driver-BQnmPRqs.mjs} +41 -1
- package/dist/{webgpu-scene-optimizer-DKRyHBRC.mjs → webgpu-scene-optimizer-Cup4Yvzs.mjs} +9 -2
- package/package.json +12 -4
- package/dist/model-signals-DVlLyP-w.d.mts +0 -17
- package/dist/model-signals-Dp5OV-Y0.mjs +0 -73
package/dist/navigation.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { t as InputMap } from "./input-
|
|
1
|
+
import { t as InputMap } from "./input-BliZYUY1.mjs";
|
|
2
|
+
import { boostSpeedMultiplier } from "@bendyline/molen-schema";
|
|
2
3
|
import * as THREE from "three";
|
|
3
4
|
import { Octree } from "three/addons/math/Octree.js";
|
|
4
5
|
import { Capsule } from "three/addons/math/Capsule.js";
|
|
@@ -63,169 +64,119 @@ const LEAF_SIZE = 16;
|
|
|
63
64
|
* Rendered roads, walls and portal covers can overlap. An octree copies each triangle into
|
|
64
65
|
* every intersecting cell and repeatedly splits those copies, causing long rebuild stalls.
|
|
65
66
|
* A balanced bounding-volume tree stores each triangle once, even for coincident surfaces.
|
|
67
|
+
*
|
|
68
|
+
* Triangle bounds and centers live in flat arrays and each split selects its median instead of
|
|
69
|
+
* sorting: an aircraft's neighborhood over dense ground cover held 170k triangles, and sorting
|
|
70
|
+
* them at every level of the tree took 700 ms of one frame.
|
|
66
71
|
*/
|
|
67
72
|
var CollisionTree = class extends Octree {
|
|
68
73
|
build() {
|
|
69
74
|
this.calcBox();
|
|
70
75
|
this.subTrees = [];
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
]);
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
76
|
+
const triangles = this.triangles;
|
|
77
|
+
if (triangles.length > 0) {
|
|
78
|
+
const bounds = new Float64Array(triangles.length * 6);
|
|
79
|
+
const centers = new Float64Array(triangles.length * 3);
|
|
80
|
+
triangles.forEach(({ a, b, c }, index) => {
|
|
81
|
+
const box = index * 6;
|
|
82
|
+
bounds[box] = Math.min(a.x, b.x, c.x);
|
|
83
|
+
bounds[box + 1] = Math.min(a.y, b.y, c.y);
|
|
84
|
+
bounds[box + 2] = Math.min(a.z, b.z, c.z);
|
|
85
|
+
bounds[box + 3] = Math.max(a.x, b.x, c.x);
|
|
86
|
+
bounds[box + 4] = Math.max(a.y, b.y, c.y);
|
|
87
|
+
bounds[box + 5] = Math.max(a.z, b.z, c.z);
|
|
88
|
+
for (let axis = 0; axis < 3; axis++) centers[index * 3 + axis] = (bounds[box + axis] + bounds[box + 3 + axis]) / 2;
|
|
83
89
|
});
|
|
84
|
-
|
|
90
|
+
const order = new Uint32Array(triangles.length);
|
|
91
|
+
for (let index = 0; index < order.length; index++) order[index] = index;
|
|
92
|
+
this.subTrees.push(partition({
|
|
93
|
+
triangles,
|
|
94
|
+
bounds,
|
|
95
|
+
centers,
|
|
96
|
+
order
|
|
97
|
+
}, 0, triangles.length));
|
|
85
98
|
}
|
|
86
99
|
this.triangles = [];
|
|
87
100
|
return this;
|
|
88
101
|
}
|
|
89
102
|
};
|
|
90
|
-
function partition(entries) {
|
|
91
|
-
const bounds =
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
103
|
+
function partition(entries, start, end) {
|
|
104
|
+
const { bounds, centers, order } = entries;
|
|
105
|
+
const box = new THREE.Box3();
|
|
106
|
+
const low = [
|
|
107
|
+
Infinity,
|
|
108
|
+
Infinity,
|
|
109
|
+
Infinity
|
|
110
|
+
];
|
|
111
|
+
const high = [
|
|
112
|
+
-Infinity,
|
|
113
|
+
-Infinity,
|
|
114
|
+
-Infinity
|
|
115
|
+
];
|
|
116
|
+
for (let k = start; k < end; k++) {
|
|
117
|
+
const index = order[k];
|
|
118
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
119
|
+
const center = centers[index * 3 + axis];
|
|
120
|
+
if (center < low[axis]) low[axis] = center;
|
|
121
|
+
if (center > high[axis]) high[axis] = center;
|
|
122
|
+
}
|
|
123
|
+
box.min.x = Math.min(box.min.x, bounds[index * 6]);
|
|
124
|
+
box.min.y = Math.min(box.min.y, bounds[index * 6 + 1]);
|
|
125
|
+
box.min.z = Math.min(box.min.z, bounds[index * 6 + 2]);
|
|
126
|
+
box.max.x = Math.max(box.max.x, bounds[index * 6 + 3]);
|
|
127
|
+
box.max.y = Math.max(box.max.y, bounds[index * 6 + 4]);
|
|
128
|
+
box.max.z = Math.max(box.max.z, bounds[index * 6 + 5]);
|
|
129
|
+
}
|
|
130
|
+
const node = new Octree(box);
|
|
131
|
+
if (end - start <= LEAF_SIZE) {
|
|
132
|
+
node.triangles = Array.from(order.subarray(start, end)).sort((p, q) => p - q).map((index) => entries.triangles[index]);
|
|
100
133
|
return node;
|
|
101
134
|
}
|
|
102
|
-
const
|
|
103
|
-
const axis =
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
node.subTrees.push(partition(entries
|
|
135
|
+
const [sx, sy, sz] = high.map((value, axis) => value - low[axis]);
|
|
136
|
+
const axis = sx >= sy && sx >= sz ? 0 : sy >= sz ? 1 : 2;
|
|
137
|
+
const middle = start + Math.floor((end - start) / 2);
|
|
138
|
+
selectMedian(entries, axis, start, end - 1, middle);
|
|
139
|
+
node.subTrees.push(partition(entries, start, middle), partition(entries, middle, end));
|
|
107
140
|
return node;
|
|
108
141
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
const right = cameraRight(this.yaw);
|
|
142
|
-
const { move } = input;
|
|
143
|
-
for (let axis = 0; axis < 3; axis++) this.position[axis] = this.position[axis] + (forward[axis] * move.forward + right[axis] * move.right) * speed;
|
|
144
|
-
this.position[1] += move.up * speed;
|
|
145
|
-
const floor = environment.groundHeight(this.position[0], this.position[2]);
|
|
146
|
-
if (floor !== void 0 && this.position[1] < floor + this.minClearance) this.position[1] = floor + this.minClearance;
|
|
147
|
-
const direction = cameraForward(this.yaw, this.pitch);
|
|
148
|
-
return {
|
|
149
|
-
position: [...this.position],
|
|
150
|
-
lookAt: [
|
|
151
|
-
this.position[0] + direction[0],
|
|
152
|
-
this.position[1] + direction[1],
|
|
153
|
-
this.position[2] + direction[2]
|
|
154
|
-
],
|
|
155
|
-
direction
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
//#endregion
|
|
160
|
-
//#region src/navigation/follow.ts
|
|
161
|
-
var FollowController = class {
|
|
162
|
-
position;
|
|
163
|
-
yawOffset = 0;
|
|
164
|
-
pitchOffset = 0;
|
|
165
|
-
distance;
|
|
166
|
-
height;
|
|
167
|
-
lookHeight;
|
|
168
|
-
smoothing;
|
|
169
|
-
recenter;
|
|
170
|
-
minClearance;
|
|
171
|
-
constructor(options = {}) {
|
|
172
|
-
this.distance = options.distance ?? 6;
|
|
173
|
-
this.height = options.height ?? 2.2;
|
|
174
|
-
this.lookHeight = options.lookHeight ?? 1.2;
|
|
175
|
-
this.smoothing = options.smoothing ?? .18;
|
|
176
|
-
this.recenter = options.recenter ?? 1.5;
|
|
177
|
-
this.minClearance = options.minClearance ?? .6;
|
|
178
|
-
}
|
|
179
|
-
/** Snap next frame instead of easing (after a teleport or a subject change). */
|
|
180
|
-
reset() {
|
|
181
|
-
this.position = void 0;
|
|
182
|
-
this.yawOffset = 0;
|
|
183
|
-
this.pitchOffset = 0;
|
|
184
|
-
}
|
|
185
|
-
update(dt, subject, input, environment) {
|
|
186
|
-
const step = Math.max(0, Math.min(.1, dt));
|
|
187
|
-
const looking = input.look[0] !== 0 || input.look[1] !== 0;
|
|
188
|
-
this.yawOffset = Math.max(-Math.PI, Math.min(Math.PI, this.yawOffset - input.look[0] * .005));
|
|
189
|
-
this.pitchOffset = Math.max(-.35, Math.min(.9, this.pitchOffset + input.look[1] * .004));
|
|
190
|
-
if (!looking && this.recenter > 0) {
|
|
191
|
-
const decay = Math.exp(-step / this.recenter);
|
|
192
|
-
this.yawOffset *= decay;
|
|
193
|
-
this.pitchOffset *= decay;
|
|
194
|
-
}
|
|
195
|
-
const heading = subject.heading + this.yawOffset;
|
|
196
|
-
const lookAt = [
|
|
197
|
-
subject.position[0],
|
|
198
|
-
subject.position[1] + this.lookHeight,
|
|
199
|
-
subject.position[2]
|
|
200
|
-
];
|
|
201
|
-
const reach = this.distance * Math.cos(this.pitchOffset);
|
|
202
|
-
const ideal = [
|
|
203
|
-
subject.position[0] - Math.sin(heading) * reach,
|
|
204
|
-
subject.position[1] + this.height + this.distance * Math.sin(this.pitchOffset),
|
|
205
|
-
subject.position[2] + Math.cos(heading) * reach
|
|
206
|
-
];
|
|
207
|
-
if (this.position === void 0) this.position = [...ideal];
|
|
208
|
-
else {
|
|
209
|
-
const blend = this.smoothing <= 0 ? 1 : 1 - Math.exp(-step / this.smoothing);
|
|
210
|
-
for (let axis = 0; axis < 3; axis++) this.position[axis] = this.position[axis] + (ideal[axis] - this.position[axis]) * blend;
|
|
142
|
+
/**
|
|
143
|
+
* Reorder `order[left..right]` so position `k` holds the entry a full sort by center along `axis`
|
|
144
|
+
* would put there, with smaller entries before it and larger after. Equal centers order by
|
|
145
|
+
* triangle index, so the two halves never depend on how earlier splits arranged the range.
|
|
146
|
+
*/
|
|
147
|
+
function selectMedian({ centers, order }, axis, left, right, k) {
|
|
148
|
+
const before = (p, q) => {
|
|
149
|
+
const cp = centers[p * 3 + axis];
|
|
150
|
+
const cq = centers[q * 3 + axis];
|
|
151
|
+
return cp < cq || cp === cq && p < q;
|
|
152
|
+
};
|
|
153
|
+
const swap = (i, j) => {
|
|
154
|
+
const held = order[i];
|
|
155
|
+
order[i] = order[j];
|
|
156
|
+
order[j] = held;
|
|
157
|
+
};
|
|
158
|
+
while (right > left) {
|
|
159
|
+
const mid = left + (right - left >> 1);
|
|
160
|
+
if (before(order[mid], order[left])) swap(mid, left);
|
|
161
|
+
if (before(order[right], order[left])) swap(right, left);
|
|
162
|
+
if (before(order[right], order[mid])) swap(right, mid);
|
|
163
|
+
const pivot = order[mid];
|
|
164
|
+
let i = left;
|
|
165
|
+
let j = right;
|
|
166
|
+
while (i <= j) {
|
|
167
|
+
while (before(order[i], pivot)) i++;
|
|
168
|
+
while (before(pivot, order[j])) j--;
|
|
169
|
+
if (i <= j) {
|
|
170
|
+
swap(i, j);
|
|
171
|
+
i++;
|
|
172
|
+
j--;
|
|
173
|
+
}
|
|
211
174
|
}
|
|
212
|
-
|
|
213
|
-
if (
|
|
214
|
-
|
|
215
|
-
const dy = lookAt[1] - this.position[1];
|
|
216
|
-
const dz = lookAt[2] - this.position[2];
|
|
217
|
-
const length = Math.hypot(dx, dy, dz) || 1;
|
|
218
|
-
return {
|
|
219
|
-
position: [...this.position],
|
|
220
|
-
lookAt,
|
|
221
|
-
direction: [
|
|
222
|
-
dx / length,
|
|
223
|
-
dy / length,
|
|
224
|
-
dz / length
|
|
225
|
-
]
|
|
226
|
-
};
|
|
175
|
+
if (k <= j) right = j;
|
|
176
|
+
else if (k >= i) left = i;
|
|
177
|
+
else return;
|
|
227
178
|
}
|
|
228
|
-
}
|
|
179
|
+
}
|
|
229
180
|
//#endregion
|
|
230
181
|
//#region src/navigation/pointer.ts
|
|
231
182
|
function emptyGestures() {
|
|
@@ -408,13 +359,16 @@ const NAVIGATION_ACTIONS = {
|
|
|
408
359
|
jump: "nav-jump",
|
|
409
360
|
interact: "nav-interact",
|
|
410
361
|
view: "nav-view",
|
|
362
|
+
boost: "nav-boost",
|
|
411
363
|
throttleUp: "nav-throttle-up",
|
|
412
364
|
throttleDown: "nav-throttle-down",
|
|
365
|
+
power: "nav-power",
|
|
413
366
|
yawLeft: "nav-yaw-left",
|
|
414
367
|
yawRight: "nav-yaw-right",
|
|
415
368
|
engine: "nav-engine",
|
|
416
369
|
gear: "nav-gear",
|
|
417
370
|
flaps: "nav-flaps",
|
|
371
|
+
vector: "nav-vector",
|
|
418
372
|
recover: "nav-recover"
|
|
419
373
|
};
|
|
420
374
|
const A = NAVIGATION_ACTIONS;
|
|
@@ -478,7 +432,8 @@ const STICKS = [
|
|
|
478
432
|
* 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 ←/→)
|
|
479
433
|
* bank, Q/Z work the rudder or pedals, Shift/Ctrl (or +/−, Page Up/Down) set throttle or
|
|
480
434
|
* collective, I toggles the engine, G the gear, F the flaps, R recovers after an impact, Space
|
|
481
|
-
* brakes, V switches the view and E leaves.
|
|
435
|
+
* brakes, V switches the view and E leaves. T toggles travel boost in walk, drive and pilot.
|
|
436
|
+
* Hosts may replace any profile through
|
|
482
437
|
* `InputMap.defineProfile`.
|
|
483
438
|
*/
|
|
484
439
|
function navigationInputProfiles() {
|
|
@@ -512,7 +467,8 @@ function navigationInputProfiles() {
|
|
|
512
467
|
Space: A.jump,
|
|
513
468
|
KeyE: A.interact,
|
|
514
469
|
KeyF: A.interact,
|
|
515
|
-
KeyV: A.view
|
|
470
|
+
KeyV: A.view,
|
|
471
|
+
KeyT: A.boost
|
|
516
472
|
},
|
|
517
473
|
axes: STICKS,
|
|
518
474
|
buttons: [
|
|
@@ -564,9 +520,11 @@ function navigationInputProfiles() {
|
|
|
564
520
|
Space: A.jump,
|
|
565
521
|
KeyE: A.interact,
|
|
566
522
|
KeyV: A.view,
|
|
523
|
+
KeyT: A.boost,
|
|
567
524
|
KeyI: A.engine,
|
|
568
525
|
KeyG: A.gear,
|
|
569
526
|
KeyF: A.flaps,
|
|
527
|
+
KeyJ: A.vector,
|
|
570
528
|
KeyR: A.recover
|
|
571
529
|
},
|
|
572
530
|
axes: STICKS,
|
|
@@ -635,11 +593,13 @@ var NavigationInputSource = class {
|
|
|
635
593
|
pointer;
|
|
636
594
|
interactPresses = 0;
|
|
637
595
|
viewPresses = 0;
|
|
596
|
+
boostPresses = 0;
|
|
638
597
|
profile;
|
|
639
598
|
pilotPresses = {
|
|
640
599
|
engine: 0,
|
|
641
600
|
gear: 0,
|
|
642
601
|
flaps: 0,
|
|
602
|
+
vector: 0,
|
|
643
603
|
recover: 0
|
|
644
604
|
};
|
|
645
605
|
lookSpeed;
|
|
@@ -661,6 +621,7 @@ var NavigationInputSource = class {
|
|
|
661
621
|
"engine",
|
|
662
622
|
"gear",
|
|
663
623
|
"flaps",
|
|
624
|
+
"vector",
|
|
664
625
|
"recover"
|
|
665
626
|
].map((name) => this.map.onPress(A[name], () => {
|
|
666
627
|
this.pilotPresses[name]++;
|
|
@@ -671,13 +632,18 @@ var NavigationInputSource = class {
|
|
|
671
632
|
this.map.onPress(A.view, () => {
|
|
672
633
|
this.viewPresses++;
|
|
673
634
|
}),
|
|
635
|
+
this.map.onPress(A.boost, () => {
|
|
636
|
+
this.boostPresses++;
|
|
637
|
+
}),
|
|
674
638
|
this.map.onReset(() => {
|
|
675
639
|
this.pointer.reset();
|
|
676
640
|
this.interactPresses = 0;
|
|
677
641
|
this.viewPresses = 0;
|
|
642
|
+
this.boostPresses = 0;
|
|
678
643
|
this.pilotPresses.engine = 0;
|
|
679
644
|
this.pilotPresses.gear = 0;
|
|
680
645
|
this.pilotPresses.flaps = 0;
|
|
646
|
+
this.pilotPresses.vector = 0;
|
|
681
647
|
this.pilotPresses.recover = 0;
|
|
682
648
|
})
|
|
683
649
|
];
|
|
@@ -719,8 +685,10 @@ var NavigationInputSource = class {
|
|
|
719
685
|
jump: v(A.jump) !== 0,
|
|
720
686
|
interact: this.interactPresses,
|
|
721
687
|
view: this.viewPresses,
|
|
688
|
+
...this.boostPresses > 0 ? { boost: this.boostPresses } : {},
|
|
722
689
|
taps: gestures.taps,
|
|
723
690
|
...this.profile === "pilot" ? { pilot: {
|
|
691
|
+
...this.map.hasAxis(A.power) ? { power: v(A.power) } : {},
|
|
724
692
|
throttle: v(A.throttleUp) - v(A.throttleDown),
|
|
725
693
|
yaw: v(A.yawRight) - v(A.yawLeft),
|
|
726
694
|
...this.pilotPresses
|
|
@@ -728,9 +696,11 @@ var NavigationInputSource = class {
|
|
|
728
696
|
};
|
|
729
697
|
this.interactPresses = 0;
|
|
730
698
|
this.viewPresses = 0;
|
|
699
|
+
this.boostPresses = 0;
|
|
731
700
|
this.pilotPresses.engine = 0;
|
|
732
701
|
this.pilotPresses.gear = 0;
|
|
733
702
|
this.pilotPresses.flaps = 0;
|
|
703
|
+
this.pilotPresses.vector = 0;
|
|
734
704
|
this.pilotPresses.recover = 0;
|
|
735
705
|
return input;
|
|
736
706
|
}
|
|
@@ -741,6 +711,433 @@ var NavigationInputSource = class {
|
|
|
741
711
|
}
|
|
742
712
|
};
|
|
743
713
|
//#endregion
|
|
714
|
+
//#region src/navigation/controls.ts
|
|
715
|
+
/** Controller discovery and raw axis/button mapping for the built-in navigation profiles. */
|
|
716
|
+
function createNavigationControls(source, options) {
|
|
717
|
+
const map = source.map;
|
|
718
|
+
const doc = options.container.ownerDocument;
|
|
719
|
+
const defaults = new Map(map.profileNames().map((name) => [name, map.getProfile(name)]));
|
|
720
|
+
let storageMessage = "";
|
|
721
|
+
if (options.storageKey) try {
|
|
722
|
+
const saved = JSON.parse(doc.defaultView?.localStorage.getItem(options.storageKey) ?? "null");
|
|
723
|
+
if (saved && typeof saved === "object") {
|
|
724
|
+
for (const name of map.profileNames()) if (Object.hasOwn(saved, name)) try {
|
|
725
|
+
map.defineProfile(name, saved[name]);
|
|
726
|
+
} catch {
|
|
727
|
+
storageMessage = "Some saved mappings were invalid; defaults were restored.";
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
} catch {
|
|
731
|
+
storageMessage = "Saved controls could not be loaded. You can still remap this session.";
|
|
732
|
+
}
|
|
733
|
+
const button = (text, parent, action) => {
|
|
734
|
+
const element = doc.createElement("button");
|
|
735
|
+
element.type = "button";
|
|
736
|
+
element.textContent = text;
|
|
737
|
+
element.addEventListener("click", action);
|
|
738
|
+
parent.append(element);
|
|
739
|
+
return element;
|
|
740
|
+
};
|
|
741
|
+
const dialog = doc.createElement("dialog");
|
|
742
|
+
dialog.setAttribute("aria-label", "Joystick and controller settings");
|
|
743
|
+
dialog.className = "molen-navigation-controls";
|
|
744
|
+
dialog.style.cssText = "box-sizing:border-box;width:min(600px,calc(100vw - 24px));max-height:85vh;overflow:auto;padding:20px;border:1px solid #607b8a;border-radius:12px;background:#162b38;color:#e1edf4;font:14px/1.5 system-ui;color-scheme:dark";
|
|
745
|
+
const heading = doc.createElement("h2");
|
|
746
|
+
heading.textContent = "Joystick and controller settings";
|
|
747
|
+
dialog.append(heading);
|
|
748
|
+
const status = doc.createElement("p");
|
|
749
|
+
status.setAttribute("role", "status");
|
|
750
|
+
const hint = doc.createElement("p");
|
|
751
|
+
hint.textContent = "Press a controller button to let the browser detect it. Move each axis to identify its number below. Controls are suspended while this editor is open.";
|
|
752
|
+
dialog.append(status, hint);
|
|
753
|
+
const select = (text) => {
|
|
754
|
+
const label = doc.createElement("label");
|
|
755
|
+
label.style.cssText = "display:flex;gap:8px;align-items:center;flex-wrap:wrap;margin:10px 0;color:inherit;font:inherit";
|
|
756
|
+
label.append(`${text} `);
|
|
757
|
+
const field = doc.createElement("select");
|
|
758
|
+
field.setAttribute("aria-label", text);
|
|
759
|
+
field.style.cssText = "max-width:100%;min-width:0;font:inherit";
|
|
760
|
+
label.append(field);
|
|
761
|
+
dialog.append(label);
|
|
762
|
+
return field;
|
|
763
|
+
};
|
|
764
|
+
const option = (field, value, text) => {
|
|
765
|
+
const item = doc.createElement("option");
|
|
766
|
+
item.value = value;
|
|
767
|
+
item.textContent = text;
|
|
768
|
+
field.append(item);
|
|
769
|
+
};
|
|
770
|
+
const profile = select("Profile");
|
|
771
|
+
for (const name of map.profileNames()) option(profile, name, name === "pilot" ? "pilot (aircraft)" : name);
|
|
772
|
+
const device = select("Controller");
|
|
773
|
+
const raw = doc.createElement("pre");
|
|
774
|
+
raw.style.cssText = "white-space:pre-wrap;overflow-wrap:anywhere;font-size:12px";
|
|
775
|
+
dialog.append(raw);
|
|
776
|
+
const axis = select("Axis");
|
|
777
|
+
const axisAction = select("Axis action");
|
|
778
|
+
const invertLabel = doc.createElement("label");
|
|
779
|
+
const invert = doc.createElement("input");
|
|
780
|
+
invert.type = "checkbox";
|
|
781
|
+
invertLabel.append(invert, " Reverse axis");
|
|
782
|
+
dialog.append(invertLabel);
|
|
783
|
+
const zoneLabel = doc.createElement("label");
|
|
784
|
+
zoneLabel.style.cssText = "display:block;margin:10px 0";
|
|
785
|
+
zoneLabel.append("Dead zone ");
|
|
786
|
+
const zone = doc.createElement("input");
|
|
787
|
+
zone.type = "number";
|
|
788
|
+
zone.min = "0";
|
|
789
|
+
zone.max = "0.9";
|
|
790
|
+
zone.step = "0.01";
|
|
791
|
+
zone.value = "0.12";
|
|
792
|
+
zone.style.width = "6em";
|
|
793
|
+
zoneLabel.append(zone);
|
|
794
|
+
dialog.append(zoneLabel);
|
|
795
|
+
const bindings = doc.createElement("div");
|
|
796
|
+
const feedback = doc.createElement("p");
|
|
797
|
+
feedback.setAttribute("role", "status");
|
|
798
|
+
feedback.textContent = storageMessage;
|
|
799
|
+
let pairs = [];
|
|
800
|
+
let resume;
|
|
801
|
+
let deviceSignature = "";
|
|
802
|
+
let controlSignature = "";
|
|
803
|
+
const currentDevice = () => map.devices().find((pad) => String(pad.index) === device.value);
|
|
804
|
+
const renderBindings = () => {
|
|
805
|
+
bindings.replaceChildren();
|
|
806
|
+
const current = map.getProfile(profile.value);
|
|
807
|
+
for (const kind of ["axes", "buttons"]) current[kind]?.forEach((binding, index) => {
|
|
808
|
+
const row = doc.createElement("p");
|
|
809
|
+
row.style.cssText = "font-size:12px;overflow-wrap:anywhere";
|
|
810
|
+
row.append(`${"axis" in binding ? `Axis ${binding.axis} (${binding.mode ?? "signed"}${binding.invert ? ", reversed" : ""})` : `Button ${binding.button}`} → ${binding.action.replace("nav-", "")} · ${binding.device?.id ?? "any controller"} `);
|
|
811
|
+
button("Remove", row, () => {
|
|
812
|
+
const next = map.getProfile(profile.value);
|
|
813
|
+
next[kind]?.splice(index, 1);
|
|
814
|
+
map.defineProfile(profile.value, next);
|
|
815
|
+
renderBindings();
|
|
816
|
+
});
|
|
817
|
+
bindings.append(row);
|
|
818
|
+
});
|
|
819
|
+
};
|
|
820
|
+
const sameDevice = (binding) => {
|
|
821
|
+
const pad = currentDevice();
|
|
822
|
+
return !!pad && (binding.device?.id === void 0 || binding.device.id === pad.id) && (binding.device?.index === void 0 || binding.device.index === pad.index);
|
|
823
|
+
};
|
|
824
|
+
const bindAxis = button("Assign axis", dialog, () => {
|
|
825
|
+
const pad = currentDevice();
|
|
826
|
+
const deadZone = Number(zone.value);
|
|
827
|
+
if (!pad || !Number.isFinite(deadZone) || deadZone < 0 || deadZone >= 1) {
|
|
828
|
+
feedback.textContent = "Choose a detected controller and a dead zone from 0 to less than 1.";
|
|
829
|
+
return;
|
|
830
|
+
}
|
|
831
|
+
const next = map.getProfile(profile.value);
|
|
832
|
+
const pair = pairs[Number(axisAction.value)];
|
|
833
|
+
const actions = pair ? [pair[1], pair[2]] : [NAVIGATION_ACTIONS.power];
|
|
834
|
+
const index = Number(axis.value);
|
|
835
|
+
next.axes = (next.axes ?? []).filter((binding) => !sameDevice(binding) || binding.axis !== index && !actions.includes(binding.action));
|
|
836
|
+
const common = {
|
|
837
|
+
axis: index,
|
|
838
|
+
device: { id: pad.id },
|
|
839
|
+
invert: invert.checked,
|
|
840
|
+
deadZone
|
|
841
|
+
};
|
|
842
|
+
if (pair) next.axes.push({
|
|
843
|
+
...common,
|
|
844
|
+
action: pair[1],
|
|
845
|
+
mode: "negative"
|
|
846
|
+
}, {
|
|
847
|
+
...common,
|
|
848
|
+
action: pair[2],
|
|
849
|
+
mode: "positive"
|
|
850
|
+
});
|
|
851
|
+
else next.axes.push({
|
|
852
|
+
...common,
|
|
853
|
+
deadZone: 0,
|
|
854
|
+
action: NAVIGATION_ACTIONS.power,
|
|
855
|
+
mode: "unit"
|
|
856
|
+
});
|
|
857
|
+
map.defineProfile(profile.value, next);
|
|
858
|
+
feedback.textContent = "Axis assigned. Close the editor to try it; Save keeps it for next time.";
|
|
859
|
+
renderBindings();
|
|
860
|
+
});
|
|
861
|
+
const padButton = select("Button");
|
|
862
|
+
const buttonAction = select("Button action");
|
|
863
|
+
const bindButton = button("Assign button", dialog, () => {
|
|
864
|
+
const pad = currentDevice();
|
|
865
|
+
if (!pad) return;
|
|
866
|
+
const next = map.getProfile(profile.value);
|
|
867
|
+
const index = Number(padButton.value);
|
|
868
|
+
next.buttons = (next.buttons ?? []).filter((binding) => !sameDevice(binding) || binding.button !== index);
|
|
869
|
+
next.buttons.push({
|
|
870
|
+
button: index,
|
|
871
|
+
action: buttonAction.value,
|
|
872
|
+
device: { id: pad.id }
|
|
873
|
+
});
|
|
874
|
+
map.defineProfile(profile.value, next);
|
|
875
|
+
feedback.textContent = "Button assigned. Close the editor to try it; Save keeps it for next time.";
|
|
876
|
+
renderBindings();
|
|
877
|
+
});
|
|
878
|
+
const updateProfile = () => {
|
|
879
|
+
const pilot = profile.value === "pilot";
|
|
880
|
+
pairs = [
|
|
881
|
+
[
|
|
882
|
+
pilot ? "Pitch (positive = nose up)" : "Forward / backward",
|
|
883
|
+
NAVIGATION_ACTIONS.forward,
|
|
884
|
+
NAVIGATION_ACTIONS.back
|
|
885
|
+
],
|
|
886
|
+
[
|
|
887
|
+
pilot ? "Roll (positive = right)" : "Left / right",
|
|
888
|
+
NAVIGATION_ACTIONS.left,
|
|
889
|
+
NAVIGATION_ACTIONS.right
|
|
890
|
+
],
|
|
891
|
+
[
|
|
892
|
+
"Look left / right",
|
|
893
|
+
NAVIGATION_ACTIONS.lookLeft,
|
|
894
|
+
NAVIGATION_ACTIONS.lookRight
|
|
895
|
+
],
|
|
896
|
+
[
|
|
897
|
+
"Look up / down",
|
|
898
|
+
NAVIGATION_ACTIONS.lookUp,
|
|
899
|
+
NAVIGATION_ACTIONS.lookDown
|
|
900
|
+
],
|
|
901
|
+
...pilot ? [[
|
|
902
|
+
"Rudder (positive = right)",
|
|
903
|
+
NAVIGATION_ACTIONS.yawLeft,
|
|
904
|
+
NAVIGATION_ACTIONS.yawRight
|
|
905
|
+
]] : [[
|
|
906
|
+
"Down / up",
|
|
907
|
+
NAVIGATION_ACTIONS.down,
|
|
908
|
+
NAVIGATION_ACTIONS.up
|
|
909
|
+
]]
|
|
910
|
+
];
|
|
911
|
+
axisAction.replaceChildren();
|
|
912
|
+
pairs.forEach((pair, index) => {
|
|
913
|
+
option(axisAction, String(index), pair[0]);
|
|
914
|
+
});
|
|
915
|
+
if (pilot) option(axisAction, "power", "Throttle / collective (absolute lever)");
|
|
916
|
+
buttonAction.replaceChildren();
|
|
917
|
+
const actions = [
|
|
918
|
+
[NAVIGATION_ACTIONS.jump, pilot || profile.value === "drive" ? "Brake" : "Jump"],
|
|
919
|
+
[NAVIGATION_ACTIONS.interact, "Enter / exit vehicle"],
|
|
920
|
+
[NAVIGATION_ACTIONS.view, "Change view"],
|
|
921
|
+
...pilot ? [
|
|
922
|
+
[NAVIGATION_ACTIONS.throttleUp, "Increase throttle"],
|
|
923
|
+
[NAVIGATION_ACTIONS.throttleDown, "Decrease throttle"],
|
|
924
|
+
[NAVIGATION_ACTIONS.engine, "Engine"],
|
|
925
|
+
[NAVIGATION_ACTIONS.gear, "Landing gear"],
|
|
926
|
+
[NAVIGATION_ACTIONS.flaps, "Flaps"],
|
|
927
|
+
[NAVIGATION_ACTIONS.vector, "Thrust vector"],
|
|
928
|
+
[NAVIGATION_ACTIONS.recover, "Recover"]
|
|
929
|
+
] : [
|
|
930
|
+
[NAVIGATION_ACTIONS.sprint, "Sprint / handbrake"],
|
|
931
|
+
[NAVIGATION_ACTIONS.up, "Up"],
|
|
932
|
+
[NAVIGATION_ACTIONS.down, "Down"]
|
|
933
|
+
]
|
|
934
|
+
];
|
|
935
|
+
for (const [value, label] of actions) option(buttonAction, value, label);
|
|
936
|
+
renderBindings();
|
|
937
|
+
};
|
|
938
|
+
profile.addEventListener("change", updateProfile);
|
|
939
|
+
const mappingDetails = doc.createElement("details");
|
|
940
|
+
const mappingSummary = doc.createElement("summary");
|
|
941
|
+
mappingSummary.textContent = "Current mappings";
|
|
942
|
+
mappingDetails.append(mappingSummary, bindings);
|
|
943
|
+
dialog.append(feedback, mappingDetails);
|
|
944
|
+
button("Save", dialog, () => {
|
|
945
|
+
if (!options.storageKey) return;
|
|
946
|
+
try {
|
|
947
|
+
doc.defaultView?.localStorage.setItem(options.storageKey, JSON.stringify(Object.fromEntries(map.profileNames().map((name) => [name, map.getProfile(name)]))));
|
|
948
|
+
feedback.textContent = "Controls saved on this browser.";
|
|
949
|
+
} catch {
|
|
950
|
+
feedback.textContent = "Browser storage is unavailable. Mappings still work for this session.";
|
|
951
|
+
}
|
|
952
|
+
}).hidden = !options.storageKey;
|
|
953
|
+
button("Reset profile", dialog, () => {
|
|
954
|
+
const original = defaults.get(profile.value);
|
|
955
|
+
if (original) map.defineProfile(profile.value, original);
|
|
956
|
+
feedback.textContent = "Default gamepad mappings restored. Save to keep this reset.";
|
|
957
|
+
renderBindings();
|
|
958
|
+
});
|
|
959
|
+
button("Done", dialog, () => dialog.close());
|
|
960
|
+
const close = () => {
|
|
961
|
+
resume?.();
|
|
962
|
+
resume = void 0;
|
|
963
|
+
options.focusTarget.focus();
|
|
964
|
+
};
|
|
965
|
+
dialog.addEventListener("close", close);
|
|
966
|
+
const open = button("Controllers", options.container, () => {
|
|
967
|
+
profile.value = map.activeProfile ?? map.profileNames()[0] ?? "";
|
|
968
|
+
updateProfile();
|
|
969
|
+
resume = map.suspend();
|
|
970
|
+
doc.exitPointerLock?.();
|
|
971
|
+
dialog.showModal();
|
|
972
|
+
poll();
|
|
973
|
+
});
|
|
974
|
+
doc.body.append(dialog);
|
|
975
|
+
const poll = () => {
|
|
976
|
+
if (dialog.open) map.update();
|
|
977
|
+
const pads = map.devices();
|
|
978
|
+
open.textContent = `Controllers (${pads.length})`;
|
|
979
|
+
if (!dialog.open) return;
|
|
980
|
+
const message = map.gamepadStatus === "blocked" ? "Controller access is blocked by the browser or embedding page." : map.gamepadStatus === "unavailable" ? "This browser does not expose controller input. Use HTTPS or localhost in a browser with Gamepad API support." : pads.length ? `${pads.length} controller(s) detected.` : "No controller detected yet. Focus this page and press a controller button.";
|
|
981
|
+
if (status.textContent !== message) status.textContent = message;
|
|
982
|
+
const signature = JSON.stringify(pads.map((pad) => [pad.id, pad.index]));
|
|
983
|
+
if (signature !== deviceSignature) {
|
|
984
|
+
deviceSignature = signature;
|
|
985
|
+
const previous = device.value;
|
|
986
|
+
device.replaceChildren();
|
|
987
|
+
for (const pad of pads) option(device, String(pad.index), `${pad.id} (#${pad.index})`);
|
|
988
|
+
if (pads.some((pad) => String(pad.index) === previous)) device.value = previous;
|
|
989
|
+
}
|
|
990
|
+
const pad = currentDevice();
|
|
991
|
+
const controls = JSON.stringify([
|
|
992
|
+
pad?.id,
|
|
993
|
+
pad?.index,
|
|
994
|
+
pad?.axes.length,
|
|
995
|
+
pad?.buttons.length
|
|
996
|
+
]);
|
|
997
|
+
if (controls !== controlSignature) {
|
|
998
|
+
controlSignature = controls;
|
|
999
|
+
axis.replaceChildren();
|
|
1000
|
+
padButton.replaceChildren();
|
|
1001
|
+
pad?.axes.forEach((_, index) => {
|
|
1002
|
+
option(axis, String(index), String(index));
|
|
1003
|
+
});
|
|
1004
|
+
pad?.buttons.forEach((_, index) => {
|
|
1005
|
+
option(padButton, String(index), String(index));
|
|
1006
|
+
});
|
|
1007
|
+
}
|
|
1008
|
+
bindAxis.disabled = !pad?.axes.length;
|
|
1009
|
+
bindButton.disabled = !pad?.buttons.length;
|
|
1010
|
+
raw.textContent = pad ? `Axes: ${pad.axes.map((value, index) => `${index}: ${value.toFixed(2)}`).join(" ")}\nPressed buttons: ${pad.buttons.flatMap((value, index) => value.pressed || value.value > .5 ? [index] : []).join(", ") || "none"}` : "";
|
|
1011
|
+
};
|
|
1012
|
+
const timer = setInterval(poll, 100);
|
|
1013
|
+
return { dispose() {
|
|
1014
|
+
clearInterval(timer);
|
|
1015
|
+
resume?.();
|
|
1016
|
+
dialog.remove();
|
|
1017
|
+
open.remove();
|
|
1018
|
+
} };
|
|
1019
|
+
}
|
|
1020
|
+
//#endregion
|
|
1021
|
+
//#region src/navigation/fly.ts
|
|
1022
|
+
var FlyController = class {
|
|
1023
|
+
position;
|
|
1024
|
+
/** Engine yaw: 0 faces +X (east), -PI/2 faces north. */
|
|
1025
|
+
yaw;
|
|
1026
|
+
/** Radians above the horizon (negative looks down). */
|
|
1027
|
+
pitch;
|
|
1028
|
+
minSpeed;
|
|
1029
|
+
speedPerMeter;
|
|
1030
|
+
sprintFactor;
|
|
1031
|
+
minClearance;
|
|
1032
|
+
lookSensitivity;
|
|
1033
|
+
constructor(position, orientation, options = {}) {
|
|
1034
|
+
this.position = [...position];
|
|
1035
|
+
this.yaw = orientation.yaw;
|
|
1036
|
+
this.pitch = orientation.pitch;
|
|
1037
|
+
this.minSpeed = options.minSpeed ?? 12;
|
|
1038
|
+
this.speedPerMeter = options.speedPerMeter ?? 1.2;
|
|
1039
|
+
this.sprintFactor = options.sprintFactor ?? 4;
|
|
1040
|
+
this.minClearance = options.minClearance ?? 2;
|
|
1041
|
+
this.lookSensitivity = options.lookSensitivity ?? .003;
|
|
1042
|
+
}
|
|
1043
|
+
update(dt, input, environment) {
|
|
1044
|
+
const step = Math.max(0, Math.min(.1, dt));
|
|
1045
|
+
const turned = applyCameraLookDelta(this.yaw, this.pitch, input.look[0], input.look[1], this.lookSensitivity);
|
|
1046
|
+
this.yaw = turned.yaw;
|
|
1047
|
+
this.pitch = turned.pitch;
|
|
1048
|
+
const ground = environment.groundHeight(this.position[0], this.position[2]);
|
|
1049
|
+
const aboveGround = ground === void 0 ? 100 : Math.max(0, this.position[1] - ground);
|
|
1050
|
+
const speed = Math.max(this.minSpeed, aboveGround * this.speedPerMeter) * (input.sprint ? this.sprintFactor : 1) * step;
|
|
1051
|
+
const forward = cameraPlanarForward(this.yaw);
|
|
1052
|
+
const right = cameraRight(this.yaw);
|
|
1053
|
+
const { move } = input;
|
|
1054
|
+
for (let axis = 0; axis < 3; axis++) this.position[axis] = this.position[axis] + (forward[axis] * move.forward + right[axis] * move.right) * speed;
|
|
1055
|
+
this.position[1] += move.up * speed;
|
|
1056
|
+
const floor = environment.groundHeight(this.position[0], this.position[2]);
|
|
1057
|
+
if (floor !== void 0 && this.position[1] < floor + this.minClearance) this.position[1] = floor + this.minClearance;
|
|
1058
|
+
const direction = cameraForward(this.yaw, this.pitch);
|
|
1059
|
+
return {
|
|
1060
|
+
position: [...this.position],
|
|
1061
|
+
lookAt: [
|
|
1062
|
+
this.position[0] + direction[0],
|
|
1063
|
+
this.position[1] + direction[1],
|
|
1064
|
+
this.position[2] + direction[2]
|
|
1065
|
+
],
|
|
1066
|
+
direction
|
|
1067
|
+
};
|
|
1068
|
+
}
|
|
1069
|
+
};
|
|
1070
|
+
//#endregion
|
|
1071
|
+
//#region src/navigation/follow.ts
|
|
1072
|
+
var FollowController = class {
|
|
1073
|
+
position;
|
|
1074
|
+
yawOffset = 0;
|
|
1075
|
+
pitchOffset = 0;
|
|
1076
|
+
distance;
|
|
1077
|
+
height;
|
|
1078
|
+
lookHeight;
|
|
1079
|
+
smoothing;
|
|
1080
|
+
recenter;
|
|
1081
|
+
minClearance;
|
|
1082
|
+
constructor(options = {}) {
|
|
1083
|
+
this.distance = options.distance ?? 6;
|
|
1084
|
+
this.height = options.height ?? 2.2;
|
|
1085
|
+
this.lookHeight = options.lookHeight ?? 1.2;
|
|
1086
|
+
this.smoothing = options.smoothing ?? .18;
|
|
1087
|
+
this.recenter = options.recenter ?? 1.5;
|
|
1088
|
+
this.minClearance = options.minClearance ?? .6;
|
|
1089
|
+
}
|
|
1090
|
+
/** Snap next frame instead of easing (after a teleport or a subject change). */
|
|
1091
|
+
reset() {
|
|
1092
|
+
this.position = void 0;
|
|
1093
|
+
this.yawOffset = 0;
|
|
1094
|
+
this.pitchOffset = 0;
|
|
1095
|
+
}
|
|
1096
|
+
update(dt, subject, input, environment) {
|
|
1097
|
+
const step = Math.max(0, Math.min(.1, dt));
|
|
1098
|
+
const looking = input.look[0] !== 0 || input.look[1] !== 0;
|
|
1099
|
+
this.yawOffset = Math.max(-Math.PI, Math.min(Math.PI, this.yawOffset - input.look[0] * .005));
|
|
1100
|
+
this.pitchOffset = Math.max(-.35, Math.min(.9, this.pitchOffset + input.look[1] * .004));
|
|
1101
|
+
if (!looking && this.recenter > 0) {
|
|
1102
|
+
const decay = Math.exp(-step / this.recenter);
|
|
1103
|
+
this.yawOffset *= decay;
|
|
1104
|
+
this.pitchOffset *= decay;
|
|
1105
|
+
}
|
|
1106
|
+
const heading = subject.heading + this.yawOffset;
|
|
1107
|
+
const lookAt = [
|
|
1108
|
+
subject.position[0],
|
|
1109
|
+
subject.position[1] + this.lookHeight,
|
|
1110
|
+
subject.position[2]
|
|
1111
|
+
];
|
|
1112
|
+
const reach = this.distance * Math.cos(this.pitchOffset);
|
|
1113
|
+
const ideal = [
|
|
1114
|
+
subject.position[0] - Math.sin(heading) * reach,
|
|
1115
|
+
subject.position[1] + this.height + this.distance * Math.sin(this.pitchOffset),
|
|
1116
|
+
subject.position[2] + Math.cos(heading) * reach
|
|
1117
|
+
];
|
|
1118
|
+
if (this.position === void 0) this.position = [...ideal];
|
|
1119
|
+
else {
|
|
1120
|
+
const blend = this.smoothing <= 0 ? 1 : 1 - Math.exp(-step / this.smoothing);
|
|
1121
|
+
for (let axis = 0; axis < 3; axis++) this.position[axis] = this.position[axis] + (ideal[axis] - this.position[axis]) * blend;
|
|
1122
|
+
}
|
|
1123
|
+
const ground = environment.groundHeight(this.position[0], this.position[2]);
|
|
1124
|
+
if (ground !== void 0 && this.position[1] < ground + this.minClearance) this.position[1] = ground + this.minClearance;
|
|
1125
|
+
const dx = lookAt[0] - this.position[0];
|
|
1126
|
+
const dy = lookAt[1] - this.position[1];
|
|
1127
|
+
const dz = lookAt[2] - this.position[2];
|
|
1128
|
+
const length = Math.hypot(dx, dy, dz) || 1;
|
|
1129
|
+
return {
|
|
1130
|
+
position: [...this.position],
|
|
1131
|
+
lookAt,
|
|
1132
|
+
direction: [
|
|
1133
|
+
dx / length,
|
|
1134
|
+
dy / length,
|
|
1135
|
+
dz / length
|
|
1136
|
+
]
|
|
1137
|
+
};
|
|
1138
|
+
}
|
|
1139
|
+
};
|
|
1140
|
+
//#endregion
|
|
744
1141
|
//#region src/navigation/orbit.ts
|
|
745
1142
|
const TAU = Math.PI * 2;
|
|
746
1143
|
function smoothstep(t) {
|
|
@@ -1260,7 +1657,11 @@ function requireFinite(value, name, allowZero) {
|
|
|
1260
1657
|
* neighborhood. Large geometries (terrain tiles, building cells) are walked through a per-geometry
|
|
1261
1658
|
* spatial index, so a rebuild costs the triangles near the subject rather than the whole tile.
|
|
1262
1659
|
* Meshes opt out with `userData.walkIgnore`, and water materials (`userData.terrainWaterMaterial`)
|
|
1263
|
-
*
|
|
1660
|
+
* are never solid. Land cover is a solid walking surface: its visible elevation can differ from
|
|
1661
|
+
* bare terrain, and full-tile land cover can replace the bare surface altogether.
|
|
1662
|
+
* A group whose contents lie within an X/Z rectangle of its own frame declares it as
|
|
1663
|
+
* `userData.walkFootprint` (`[minX, minZ, maxX, maxZ]`, like a terrain tile): a scan skips the
|
|
1664
|
+
* whole group when that rectangle is out of reach.
|
|
1264
1665
|
*/
|
|
1265
1666
|
var WalkCollision = class {
|
|
1266
1667
|
origin = new THREE.Vector3();
|
|
@@ -1271,6 +1672,7 @@ var WalkCollision = class {
|
|
|
1271
1672
|
verticalRadius;
|
|
1272
1673
|
rescanIntervalMs;
|
|
1273
1674
|
now;
|
|
1675
|
+
instanceBounds;
|
|
1274
1676
|
meshes = [];
|
|
1275
1677
|
initialized = false;
|
|
1276
1678
|
/** Vertical center of the current neighborhood, or undefined when it spans the whole column. */
|
|
@@ -1286,6 +1688,7 @@ var WalkCollision = class {
|
|
|
1286
1688
|
this.verticalRadius = resolved.verticalRadius === void 0 ? void 0 : requireFinite(resolved.verticalRadius, "verticalRadius", false);
|
|
1287
1689
|
this.rescanIntervalMs = requireFinite(resolved.rescanIntervalMs ?? 0, "rescanIntervalMs", true);
|
|
1288
1690
|
this.now = resolved.now ?? (() => performance.now());
|
|
1691
|
+
this.instanceBounds = resolved.instanceBounds ?? false;
|
|
1289
1692
|
}
|
|
1290
1693
|
/**
|
|
1291
1694
|
* Track the subject at (x, z). With a `verticalRadius`, `y` bounds the neighborhood vertically.
|
|
@@ -1297,8 +1700,9 @@ var WalkCollision = class {
|
|
|
1297
1700
|
const moved = !this.initialized || band === void 0 !== (this.centerY === void 0) || Math.hypot(x - this.origin.x, z - this.origin.z) >= this.rebuildDistance || band !== void 0 && this.centerY !== void 0 && Math.abs(band - this.centerY) >= this.rebuildDistance;
|
|
1298
1701
|
const now = this.now();
|
|
1299
1702
|
if (!moved && !this.stale && now - this.scannedAt < this.rescanIntervalMs) return;
|
|
1300
|
-
root.updateWorldMatrix(true,
|
|
1703
|
+
root.updateWorldMatrix(true, false);
|
|
1301
1704
|
const inverse = root.matrixWorld.clone().invert();
|
|
1705
|
+
const candidate = new THREE.Matrix4();
|
|
1302
1706
|
const meshes = [];
|
|
1303
1707
|
const centerX = moved ? x : this.origin.x;
|
|
1304
1708
|
const centerZ = moved ? z : this.origin.z;
|
|
@@ -1306,25 +1710,44 @@ var WalkCollision = class {
|
|
|
1306
1710
|
const reach = this.radius + this.rebuildDistance;
|
|
1307
1711
|
const verticalReach = (this.verticalRadius ?? 0) + this.rebuildDistance;
|
|
1308
1712
|
const bounds = new THREE.Box3();
|
|
1309
|
-
|
|
1713
|
+
const sphere = new THREE.Sphere();
|
|
1714
|
+
const footprint = new THREE.Box3();
|
|
1715
|
+
const outOfReach = (min, max) => min.x > centerX + reach || max.x < centerX - reach || min.z > centerZ + reach || max.z < centerZ - reach || centerY !== void 0 && (min.y > centerY + verticalReach || max.y < centerY - verticalReach);
|
|
1716
|
+
const visit = (object, inspect) => {
|
|
1717
|
+
if (!object.visible) return;
|
|
1718
|
+
if (object !== root) object.updateWorldMatrix(false, false);
|
|
1719
|
+
const area = object.userData.walkFootprint;
|
|
1720
|
+
if (area !== void 0) {
|
|
1721
|
+
footprint.min.set(area[0], 0, area[1]);
|
|
1722
|
+
footprint.max.set(area[2], 0, area[3]);
|
|
1723
|
+
footprint.applyMatrix4(candidate.multiplyMatrices(inverse, object.matrixWorld));
|
|
1724
|
+
if (footprint.min.x > centerX + reach || footprint.max.x < centerX - reach || footprint.min.z > centerZ + reach || footprint.max.z < centerZ - reach) return;
|
|
1725
|
+
}
|
|
1726
|
+
inspect(object);
|
|
1727
|
+
for (const child of object.children) visit(child, inspect);
|
|
1728
|
+
};
|
|
1729
|
+
visit(root, (object) => {
|
|
1310
1730
|
if (!(object instanceof THREE.Mesh) || object.userData.walkIgnore === true) return;
|
|
1311
1731
|
if (this.ignoreVehicles && (object.userData.vehicleIds || object.userData.vehicleGeometry)) return;
|
|
1312
|
-
if (object.name === "semantic:landcover") return;
|
|
1313
1732
|
if ((Array.isArray(object.material) ? object.material : [object.material]).every((material) => material.userData.terrainWaterMaterial === true)) return;
|
|
1314
|
-
const matrix =
|
|
1733
|
+
const matrix = candidate.multiplyMatrices(inverse, object.matrixWorld);
|
|
1734
|
+
const known = object instanceof THREE.InstancedMesh ? object.boundingSphere : object.geometry.boundingSphere;
|
|
1735
|
+
if (known !== null) {
|
|
1736
|
+
sphere.copy(known).applyMatrix4(matrix);
|
|
1737
|
+
bounds.min.setScalar(-sphere.radius).add(sphere.center);
|
|
1738
|
+
bounds.max.setScalar(sphere.radius).add(sphere.center);
|
|
1739
|
+
if (outOfReach(bounds.min, bounds.max)) return;
|
|
1740
|
+
}
|
|
1315
1741
|
if (object instanceof THREE.InstancedMesh) {
|
|
1742
|
+
geometryBounds(object.geometry);
|
|
1316
1743
|
if (object.boundingBox === null) object.computeBoundingBox();
|
|
1317
1744
|
bounds.copy(object.boundingBox);
|
|
1318
|
-
} else
|
|
1319
|
-
if (object.geometry.boundingBox === null) object.geometry.computeBoundingBox();
|
|
1320
|
-
bounds.copy(object.geometry.boundingBox);
|
|
1321
|
-
}
|
|
1745
|
+
} else bounds.copy(geometryBounds(object.geometry));
|
|
1322
1746
|
bounds.applyMatrix4(matrix);
|
|
1323
|
-
if (bounds.min
|
|
1324
|
-
if (centerY !== void 0 && (bounds.min.y > centerY + verticalReach || bounds.max.y < centerY - verticalReach)) return;
|
|
1747
|
+
if (outOfReach(bounds.min, bounds.max)) return;
|
|
1325
1748
|
meshes.push({
|
|
1326
1749
|
mesh: object,
|
|
1327
|
-
matrix,
|
|
1750
|
+
matrix: matrix.clone(),
|
|
1328
1751
|
count: object instanceof THREE.InstancedMesh ? object.count : 1,
|
|
1329
1752
|
geometry: object.geometry,
|
|
1330
1753
|
version: object instanceof THREE.InstancedMesh ? object.instanceMatrix.version : (object.geometry.index?.version ?? 0) + (object.geometry.getAttribute("position")?.version ?? 0)
|
|
@@ -1378,7 +1801,7 @@ var WalkCollision = class {
|
|
|
1378
1801
|
const geometry = mesh.geometry;
|
|
1379
1802
|
const position = geometry.getAttribute("position");
|
|
1380
1803
|
if (position === void 0) continue;
|
|
1381
|
-
|
|
1804
|
+
geometryBounds(geometry);
|
|
1382
1805
|
local.multiplyMatrices(offset, matrix);
|
|
1383
1806
|
if (mesh instanceof THREE.InstancedMesh && mesh.boundingBox === null) mesh.computeBoundingBox();
|
|
1384
1807
|
bounds.copy(mesh instanceof THREE.InstancedMesh ? mesh.boundingBox : geometry.boundingBox).applyMatrix4(local);
|
|
@@ -1387,7 +1810,8 @@ var WalkCollision = class {
|
|
|
1387
1810
|
const start = geometry.drawRange.start;
|
|
1388
1811
|
const end = Math.min(index?.count ?? position.count, start + geometry.drawRange.count);
|
|
1389
1812
|
const doubleSided = mesh.userData.walkDoubleSided === true;
|
|
1390
|
-
const
|
|
1813
|
+
const boxes = this.instanceBounds && mesh instanceof THREE.InstancedMesh;
|
|
1814
|
+
const spatial = boxes ? void 0 : triangleIndexFor(geometry);
|
|
1391
1815
|
const visit = (vertex) => {
|
|
1392
1816
|
if (vertex < start || vertex + 2 >= end) return;
|
|
1393
1817
|
a.fromBufferAttribute(position, index?.getX(vertex) ?? vertex).applyMatrix4(transform);
|
|
@@ -1405,6 +1829,10 @@ var WalkCollision = class {
|
|
|
1405
1829
|
} else transform.copy(local);
|
|
1406
1830
|
bounds.copy(geometry.boundingBox).applyMatrix4(transform);
|
|
1407
1831
|
if (!inside()) continue;
|
|
1832
|
+
if (boxes) {
|
|
1833
|
+
addBox(this.octree, geometry.boundingBox, transform);
|
|
1834
|
+
continue;
|
|
1835
|
+
}
|
|
1408
1836
|
if (spatial === void 0) {
|
|
1409
1837
|
for (let vertex = start; vertex + 2 < end; vertex += 3) visit(vertex);
|
|
1410
1838
|
continue;
|
|
@@ -1418,6 +1846,106 @@ var WalkCollision = class {
|
|
|
1418
1846
|
this.octree.build();
|
|
1419
1847
|
}
|
|
1420
1848
|
};
|
|
1849
|
+
/**
|
|
1850
|
+
* The geometry's bounding box, computed once straight from a plain float position array: three's
|
|
1851
|
+
* per-vertex accessors made a freshly streamed building cell's box most of an aircraft's scan.
|
|
1852
|
+
*/
|
|
1853
|
+
function geometryBounds(geometry) {
|
|
1854
|
+
const known = geometry.boundingBox;
|
|
1855
|
+
if (known !== null) return known;
|
|
1856
|
+
const position = geometry.getAttribute("position");
|
|
1857
|
+
if (!(position instanceof THREE.BufferAttribute) || !(position.array instanceof Float32Array) || position.normalized || position.itemSize !== 3 || geometry.morphAttributes.position !== void 0) {
|
|
1858
|
+
geometry.computeBoundingBox();
|
|
1859
|
+
return geometry.boundingBox;
|
|
1860
|
+
}
|
|
1861
|
+
const array = position.array;
|
|
1862
|
+
let minX = Number.POSITIVE_INFINITY, minY = Number.POSITIVE_INFINITY, minZ = Number.POSITIVE_INFINITY, maxX = Number.NEGATIVE_INFINITY, maxY = Number.NEGATIVE_INFINITY, maxZ = Number.NEGATIVE_INFINITY;
|
|
1863
|
+
for (let i = 0, end = position.count * 3; i < end; i += 3) {
|
|
1864
|
+
const x = array[i], y = array[i + 1], z = array[i + 2];
|
|
1865
|
+
if (x < minX) minX = x;
|
|
1866
|
+
if (x > maxX) maxX = x;
|
|
1867
|
+
if (y < minY) minY = y;
|
|
1868
|
+
if (y > maxY) maxY = y;
|
|
1869
|
+
if (z < minZ) minZ = z;
|
|
1870
|
+
if (z > maxZ) maxZ = z;
|
|
1871
|
+
}
|
|
1872
|
+
geometry.boundingBox = new THREE.Box3(new THREE.Vector3(minX, minY, minZ), new THREE.Vector3(maxX, maxY, maxZ));
|
|
1873
|
+
return geometry.boundingBox;
|
|
1874
|
+
}
|
|
1875
|
+
const BOX_FACES = [
|
|
1876
|
+
[
|
|
1877
|
+
0,
|
|
1878
|
+
2,
|
|
1879
|
+
6,
|
|
1880
|
+
4
|
|
1881
|
+
],
|
|
1882
|
+
[
|
|
1883
|
+
1,
|
|
1884
|
+
5,
|
|
1885
|
+
7,
|
|
1886
|
+
3
|
|
1887
|
+
],
|
|
1888
|
+
[
|
|
1889
|
+
0,
|
|
1890
|
+
4,
|
|
1891
|
+
5,
|
|
1892
|
+
1
|
|
1893
|
+
],
|
|
1894
|
+
[
|
|
1895
|
+
2,
|
|
1896
|
+
3,
|
|
1897
|
+
7,
|
|
1898
|
+
6
|
|
1899
|
+
],
|
|
1900
|
+
[
|
|
1901
|
+
0,
|
|
1902
|
+
1,
|
|
1903
|
+
3,
|
|
1904
|
+
2
|
|
1905
|
+
],
|
|
1906
|
+
[
|
|
1907
|
+
4,
|
|
1908
|
+
6,
|
|
1909
|
+
7,
|
|
1910
|
+
5
|
|
1911
|
+
]
|
|
1912
|
+
];
|
|
1913
|
+
const corners = Array.from({ length: 8 }, () => new THREE.Vector3());
|
|
1914
|
+
const boxCenter = new THREE.Vector3();
|
|
1915
|
+
const faceCenter = new THREE.Vector3();
|
|
1916
|
+
const edge = new THREE.Vector3();
|
|
1917
|
+
const across = new THREE.Vector3();
|
|
1918
|
+
/**
|
|
1919
|
+
* Add `box`, placed by `transform`, as twelve outward-facing triangles: rays test front faces only.
|
|
1920
|
+
* A box collapsed to a point (a hidden instance) adds nothing.
|
|
1921
|
+
*/
|
|
1922
|
+
function addBox(tree, box, transform) {
|
|
1923
|
+
corners.forEach((corner, index) => {
|
|
1924
|
+
corner.set(index & 1 ? box.max.x : box.min.x, index & 2 ? box.max.y : box.min.y, index & 4 ? box.max.z : box.min.z).applyMatrix4(transform);
|
|
1925
|
+
});
|
|
1926
|
+
const [first] = corners;
|
|
1927
|
+
if (corners.every((corner) => corner.equals(first))) return;
|
|
1928
|
+
boxCenter.set(0, 0, 0);
|
|
1929
|
+
for (const corner of corners) boxCenter.add(corner);
|
|
1930
|
+
boxCenter.multiplyScalar(1 / 8);
|
|
1931
|
+
for (const face of BOX_FACES) {
|
|
1932
|
+
const [a, b, c, d] = face.map((index) => corners[index]);
|
|
1933
|
+
faceCenter.copy(a).add(b).add(c).add(d).multiplyScalar(.25).sub(boxCenter);
|
|
1934
|
+
const [p, q, r, t] = edge.subVectors(b, a).cross(across.subVectors(c, a)).dot(faceCenter) >= 0 ? [
|
|
1935
|
+
a,
|
|
1936
|
+
b,
|
|
1937
|
+
c,
|
|
1938
|
+
d
|
|
1939
|
+
] : [
|
|
1940
|
+
a,
|
|
1941
|
+
d,
|
|
1942
|
+
c,
|
|
1943
|
+
b
|
|
1944
|
+
];
|
|
1945
|
+
tree.addTriangle(new THREE.Triangle(p.clone(), q.clone(), r.clone()));
|
|
1946
|
+
tree.addTriangle(new THREE.Triangle(p.clone(), r.clone(), t.clone()));
|
|
1947
|
+
}
|
|
1948
|
+
}
|
|
1421
1949
|
//#endregion
|
|
1422
1950
|
//#region src/navigation/walk-controller.ts
|
|
1423
1951
|
const WALK_EYE_HEIGHT = 1.7;
|
|
@@ -1443,6 +1971,8 @@ const FLOOR_NORMAL = Math.cos(Math.PI / 3.6);
|
|
|
1443
1971
|
* terrain pauses movement (it is never a bottomless pit) until coverage arrives.
|
|
1444
1972
|
*/
|
|
1445
1973
|
var WalkController = class {
|
|
1974
|
+
/** Four times walking/running speed; jump height and look response stay normal. */
|
|
1975
|
+
boostMode = false;
|
|
1446
1976
|
feet = new THREE.Vector3();
|
|
1447
1977
|
velocity = new THREE.Vector3();
|
|
1448
1978
|
grounded = false;
|
|
@@ -1460,8 +1990,11 @@ var WalkController = class {
|
|
|
1460
1990
|
this.accumulator = 0;
|
|
1461
1991
|
this.jumpHeld = false;
|
|
1462
1992
|
}
|
|
1463
|
-
/**
|
|
1464
|
-
|
|
1993
|
+
/**
|
|
1994
|
+
* Find supported open ground. A separate `groundCollision` can exclude vehicles from the
|
|
1995
|
+
* landing ray while `collision` still rejects body overlaps, placing people beside cars.
|
|
1996
|
+
*/
|
|
1997
|
+
place(collision, sampleHeight, groundCollision = collision) {
|
|
1465
1998
|
const centerX = this.feet.x;
|
|
1466
1999
|
const centerZ = this.feet.z;
|
|
1467
2000
|
for (let ring = 0; ring <= PLACEMENT_RINGS; ring++) {
|
|
@@ -1471,8 +2004,8 @@ var WalkController = class {
|
|
|
1471
2004
|
const z = centerZ + Math.sin(i / count * Math.PI * 2) * ring * PLACEMENT_RING_SPACING;
|
|
1472
2005
|
const ground = sampleHeight(x, z);
|
|
1473
2006
|
if (ground === void 0) continue;
|
|
1474
|
-
const ray = new THREE.Ray(new THREE.Vector3(x -
|
|
1475
|
-
const hit =
|
|
2007
|
+
const ray = new THREE.Ray(new THREE.Vector3(x - groundCollision.origin.x, ground + 1e4, z - groundCollision.origin.z), new THREE.Vector3(0, -1, 0));
|
|
2008
|
+
const hit = groundCollision.octree.rayIntersect(ray);
|
|
1476
2009
|
if (!hit || hit.position.y > ground + 3) continue;
|
|
1477
2010
|
this.feet.set(x, hit.position.y + .02, z);
|
|
1478
2011
|
this.setCapsule(collision);
|
|
@@ -1504,7 +2037,7 @@ var WalkController = class {
|
|
|
1504
2037
|
this.grounded = false;
|
|
1505
2038
|
}
|
|
1506
2039
|
const length = Math.max(1, Math.hypot(input.forward, input.right));
|
|
1507
|
-
const speed = input.sprint ? 4 : WALK_SPEED;
|
|
2040
|
+
const speed = (input.sprint ? 4 : WALK_SPEED) * boostSpeedMultiplier(this.boostMode);
|
|
1508
2041
|
const vx = (Math.cos(input.yaw) * input.forward - Math.sin(input.yaw) * input.right) / length * speed;
|
|
1509
2042
|
const vz = (Math.sin(input.yaw) * input.forward + Math.cos(input.yaw) * input.right) / length * speed;
|
|
1510
2043
|
this.accumulator += Math.min(.1, Math.max(0, dt));
|
|
@@ -1554,4 +2087,4 @@ var WalkController = class {
|
|
|
1554
2087
|
}
|
|
1555
2088
|
};
|
|
1556
2089
|
//#endregion
|
|
1557
|
-
export { CollisionTree, FlyController, FollowController, MAX_LOOK_PITCH, NAVIGATION_ACTIONS, NavigationInputSource, NavigationPointer, OrbitController, RUN_SPEED, TouchStick, WALK_EYE_HEIGHT, WALK_PLACEMENT_RADIUS, WALK_SPEED, WalkCollision, WalkController, applyCameraLookDelta, cameraForward, cameraPlanarForward, cameraRight, createTouchJoystick, idleNavigationInput, navigationInputProfiles, orientationFromDirection, viewNeedsImmediateUpdate };
|
|
2090
|
+
export { CollisionTree, FlyController, FollowController, MAX_LOOK_PITCH, NAVIGATION_ACTIONS, NavigationInputSource, NavigationPointer, OrbitController, RUN_SPEED, TouchStick, WALK_EYE_HEIGHT, WALK_PLACEMENT_RADIUS, WALK_SPEED, WalkCollision, WalkController, applyCameraLookDelta, cameraForward, cameraPlanarForward, cameraRight, createNavigationControls, createTouchJoystick, idleNavigationInput, navigationInputProfiles, orientationFromDirection, viewNeedsImmediateUpdate };
|