@bendyline/molen-client 0.0.2 → 0.0.4
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 +3 -0
- package/dist/assets-Bp0ox8YY.d.mts +59 -0
- package/dist/audio.d.mts +526 -0
- package/dist/audio.mjs +1740 -0
- package/dist/camera-track.d.mts +1 -1
- package/dist/index.d.mts +19 -167
- package/dist/index.mjs +725 -482
- package/dist/input-CD-dHuv0.d.mts +120 -0
- package/dist/input-CfbIm1MS.mjs +361 -0
- package/dist/markers.d.mts +137 -0
- package/dist/markers.mjs +347 -0
- package/dist/navigation.d.mts +557 -0
- package/dist/navigation.mjs +1506 -0
- package/dist/{renderer-B3pd4N-J.d.mts → renderer-DfWc3MFM.d.mts} +111 -5
- package/dist/vehicles.d.mts +31 -2
- package/dist/vehicles.mjs +96 -28
- package/dist/webgpu-driver-Bt2mvZk9.mjs +165 -0
- package/dist/webgpu-sky-reflections-CymGEes-.mjs +22 -0
- package/package.json +17 -4
- package/dist/webgpu-driver-DXsRuz8j.mjs +0 -75
|
@@ -0,0 +1,1506 @@
|
|
|
1
|
+
import { t as InputMap } from "./input-CfbIm1MS.mjs";
|
|
2
|
+
import * as THREE from "three";
|
|
3
|
+
import { Octree } from "three/addons/math/Octree.js";
|
|
4
|
+
import { Capsule } from "three/addons/math/Capsule.js";
|
|
5
|
+
//#region src/navigation/camera-math.ts
|
|
6
|
+
/** Pitch limit shared by look controls: just short of straight up/down. */
|
|
7
|
+
const MAX_LOOK_PITCH = 1.5;
|
|
8
|
+
function cameraForward(yaw, pitch) {
|
|
9
|
+
return [
|
|
10
|
+
Math.cos(pitch) * Math.cos(yaw),
|
|
11
|
+
Math.sin(pitch),
|
|
12
|
+
Math.cos(pitch) * Math.sin(yaw)
|
|
13
|
+
];
|
|
14
|
+
}
|
|
15
|
+
function cameraRight(yaw) {
|
|
16
|
+
return [
|
|
17
|
+
Math.cos(yaw + Math.PI / 2),
|
|
18
|
+
0,
|
|
19
|
+
Math.sin(yaw + Math.PI / 2)
|
|
20
|
+
];
|
|
21
|
+
}
|
|
22
|
+
function cameraPlanarForward(yaw) {
|
|
23
|
+
return [
|
|
24
|
+
Math.cos(yaw),
|
|
25
|
+
0,
|
|
26
|
+
Math.sin(yaw)
|
|
27
|
+
];
|
|
28
|
+
}
|
|
29
|
+
/** Apply a pointer delta (pixels) to a yaw/pitch pair; moving right turns right. */
|
|
30
|
+
function applyCameraLookDelta(yaw, pitch, deltaX, deltaY, sensitivity = .003) {
|
|
31
|
+
return {
|
|
32
|
+
yaw: yaw + deltaX * sensitivity,
|
|
33
|
+
pitch: Math.max(-1.5, Math.min(MAX_LOOK_PITCH, pitch - deltaY * sensitivity))
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/** Yaw/pitch of a direction vector (the inverse of `cameraForward`). */
|
|
37
|
+
function orientationFromDirection(direction) {
|
|
38
|
+
const [x, y, z] = direction;
|
|
39
|
+
return {
|
|
40
|
+
yaw: Math.atan2(z, x),
|
|
41
|
+
pitch: Math.atan2(y, Math.hypot(x, z))
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Detect camera changes that can outrun a throttled terrain-selection update. Terrain selectors
|
|
46
|
+
* have a view guard band, so tiny per-frame changes can wait for their normal cadence; larger
|
|
47
|
+
* turns or translations need coverage replanned before the next render.
|
|
48
|
+
*/
|
|
49
|
+
function viewNeedsImmediateUpdate(previousPosition, previousDirection, position, direction, movementThreshold, turnThresholdRadians) {
|
|
50
|
+
const dx = position[0] - previousPosition[0];
|
|
51
|
+
const dy = position[1] - previousPosition[1];
|
|
52
|
+
const dz = position[2] - previousPosition[2];
|
|
53
|
+
if (dx * dx + dy * dy + dz * dz >= movementThreshold * movementThreshold) return true;
|
|
54
|
+
const previousLength = Math.hypot(...previousDirection);
|
|
55
|
+
const directionLength = Math.hypot(...direction);
|
|
56
|
+
if (previousLength === 0 || directionLength === 0) return true;
|
|
57
|
+
return (previousDirection[0] * direction[0] + previousDirection[1] * direction[1] + previousDirection[2] * direction[2]) / (previousLength * directionLength) <= Math.cos(turnThresholdRadians);
|
|
58
|
+
}
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/navigation/collision-tree.ts
|
|
61
|
+
const LEAF_SIZE = 16;
|
|
62
|
+
/** Keep Three's collision queries, but partition triangles without duplicating them.
|
|
63
|
+
* Rendered roads, walls and portal covers can overlap. An octree copies each triangle into
|
|
64
|
+
* every intersecting cell and repeatedly splits those copies, causing long rebuild stalls.
|
|
65
|
+
* A balanced bounding-volume tree stores each triangle once, even for coincident surfaces.
|
|
66
|
+
*/
|
|
67
|
+
var CollisionTree = class extends Octree {
|
|
68
|
+
build() {
|
|
69
|
+
this.calcBox();
|
|
70
|
+
this.subTrees = [];
|
|
71
|
+
if (this.triangles.length > 0) {
|
|
72
|
+
const entries = this.triangles.map((triangle) => {
|
|
73
|
+
const bounds = new THREE.Box3().setFromPoints([
|
|
74
|
+
triangle.a,
|
|
75
|
+
triangle.b,
|
|
76
|
+
triangle.c
|
|
77
|
+
]);
|
|
78
|
+
return {
|
|
79
|
+
triangle,
|
|
80
|
+
bounds,
|
|
81
|
+
center: bounds.getCenter(new THREE.Vector3())
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
this.subTrees.push(partition(entries));
|
|
85
|
+
}
|
|
86
|
+
this.triangles = [];
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
function partition(entries) {
|
|
91
|
+
const bounds = new THREE.Box3();
|
|
92
|
+
const centers = new THREE.Box3();
|
|
93
|
+
for (const entry of entries) {
|
|
94
|
+
bounds.union(entry.bounds);
|
|
95
|
+
centers.expandByPoint(entry.center);
|
|
96
|
+
}
|
|
97
|
+
const node = new Octree(bounds);
|
|
98
|
+
if (entries.length <= LEAF_SIZE) {
|
|
99
|
+
node.triangles = entries.map((entry) => entry.triangle);
|
|
100
|
+
return node;
|
|
101
|
+
}
|
|
102
|
+
const size = centers.getSize(new THREE.Vector3());
|
|
103
|
+
const axis = size.x >= size.y && size.x >= size.z ? "x" : size.y >= size.z ? "y" : "z";
|
|
104
|
+
entries.sort((a, b) => a.center[axis] - b.center[axis]);
|
|
105
|
+
const middle = Math.floor(entries.length / 2);
|
|
106
|
+
node.subTrees.push(partition(entries.slice(0, middle)), partition(entries.slice(middle)));
|
|
107
|
+
return node;
|
|
108
|
+
}
|
|
109
|
+
//#endregion
|
|
110
|
+
//#region src/navigation/fly.ts
|
|
111
|
+
var FlyController = class {
|
|
112
|
+
position;
|
|
113
|
+
/** Engine yaw: 0 faces +X (east), -PI/2 faces north. */
|
|
114
|
+
yaw;
|
|
115
|
+
/** Radians above the horizon (negative looks down). */
|
|
116
|
+
pitch;
|
|
117
|
+
minSpeed;
|
|
118
|
+
speedPerMeter;
|
|
119
|
+
sprintFactor;
|
|
120
|
+
minClearance;
|
|
121
|
+
lookSensitivity;
|
|
122
|
+
constructor(position, orientation, options = {}) {
|
|
123
|
+
this.position = [...position];
|
|
124
|
+
this.yaw = orientation.yaw;
|
|
125
|
+
this.pitch = orientation.pitch;
|
|
126
|
+
this.minSpeed = options.minSpeed ?? 12;
|
|
127
|
+
this.speedPerMeter = options.speedPerMeter ?? 1.2;
|
|
128
|
+
this.sprintFactor = options.sprintFactor ?? 4;
|
|
129
|
+
this.minClearance = options.minClearance ?? 2;
|
|
130
|
+
this.lookSensitivity = options.lookSensitivity ?? .003;
|
|
131
|
+
}
|
|
132
|
+
update(dt, input, environment) {
|
|
133
|
+
const step = Math.max(0, Math.min(.1, dt));
|
|
134
|
+
const turned = applyCameraLookDelta(this.yaw, this.pitch, input.look[0], input.look[1], this.lookSensitivity);
|
|
135
|
+
this.yaw = turned.yaw;
|
|
136
|
+
this.pitch = turned.pitch;
|
|
137
|
+
const ground = environment.groundHeight(this.position[0], this.position[2]);
|
|
138
|
+
const aboveGround = ground === void 0 ? 100 : Math.max(0, this.position[1] - ground);
|
|
139
|
+
const speed = Math.max(this.minSpeed, aboveGround * this.speedPerMeter) * (input.sprint ? this.sprintFactor : 1) * step;
|
|
140
|
+
const forward = cameraPlanarForward(this.yaw);
|
|
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;
|
|
211
|
+
}
|
|
212
|
+
const ground = environment.groundHeight(this.position[0], this.position[2]);
|
|
213
|
+
if (ground !== void 0 && this.position[1] < ground + this.minClearance) this.position[1] = ground + this.minClearance;
|
|
214
|
+
const dx = lookAt[0] - this.position[0];
|
|
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
|
+
};
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
//#endregion
|
|
230
|
+
//#region src/navigation/pointer.ts
|
|
231
|
+
function emptyGestures() {
|
|
232
|
+
return {
|
|
233
|
+
look: [0, 0],
|
|
234
|
+
pan: [0, 0],
|
|
235
|
+
zoom: 0,
|
|
236
|
+
twist: 0,
|
|
237
|
+
taps: []
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Tracks pointer gestures on a surface. One mouse button or finger drags to look; the secondary
|
|
242
|
+
* or middle button, shift-drag, or two fingers pan; the wheel or a two-finger pinch zooms; two
|
|
243
|
+
* fingers rotating twist. Enable pointer lock for mouse-look (walk mode) with `setPointerLock`.
|
|
244
|
+
*/
|
|
245
|
+
var NavigationPointer = class {
|
|
246
|
+
surface;
|
|
247
|
+
pointers = /* @__PURE__ */ new Map();
|
|
248
|
+
pending = emptyGestures();
|
|
249
|
+
pointerLock = false;
|
|
250
|
+
listeners = [];
|
|
251
|
+
tapSlop;
|
|
252
|
+
tapMilliseconds;
|
|
253
|
+
wheelScale;
|
|
254
|
+
now;
|
|
255
|
+
constructor(surface, options = {}) {
|
|
256
|
+
this.surface = surface;
|
|
257
|
+
this.tapSlop = options.tapSlop ?? 6;
|
|
258
|
+
this.tapMilliseconds = options.tapMilliseconds ?? 350;
|
|
259
|
+
this.wheelScale = options.wheelScale ?? 400;
|
|
260
|
+
this.now = options.now ?? (() => performance.now());
|
|
261
|
+
this.listen("pointerdown", (event) => this.down(event));
|
|
262
|
+
this.listen("pointermove", (event) => this.move(event));
|
|
263
|
+
this.listen("pointerup", (event) => this.up(event, true));
|
|
264
|
+
this.listen("pointercancel", (event) => this.up(event, false));
|
|
265
|
+
this.listen("wheel", (event) => this.wheel(event), { passive: false });
|
|
266
|
+
this.listen("contextmenu", (event) => event.preventDefault?.());
|
|
267
|
+
}
|
|
268
|
+
/** Drop the accumulated gestures and return them. Call once per frame. */
|
|
269
|
+
consume() {
|
|
270
|
+
const gestures = this.pending;
|
|
271
|
+
this.pending = emptyGestures();
|
|
272
|
+
return gestures;
|
|
273
|
+
}
|
|
274
|
+
/** Request pointer lock on the next press (mouse-look); releasing unlocks immediately. */
|
|
275
|
+
setPointerLock(enabled) {
|
|
276
|
+
this.pointerLock = enabled;
|
|
277
|
+
const document = this.surface.ownerDocument;
|
|
278
|
+
if (!enabled && document?.pointerLockElement === this.surface) document.exitPointerLock?.();
|
|
279
|
+
}
|
|
280
|
+
/** Forget held pointers and pending gestures (e.g. on blur or a mode change). */
|
|
281
|
+
reset() {
|
|
282
|
+
this.pointers.clear();
|
|
283
|
+
this.pending = emptyGestures();
|
|
284
|
+
}
|
|
285
|
+
dispose() {
|
|
286
|
+
for (const [type, listener] of this.listeners) this.surface.removeEventListener(type, listener);
|
|
287
|
+
this.listeners.length = 0;
|
|
288
|
+
this.setPointerLock(false);
|
|
289
|
+
this.reset();
|
|
290
|
+
}
|
|
291
|
+
listen(type, handler, options) {
|
|
292
|
+
const listener = handler;
|
|
293
|
+
this.surface.addEventListener(type, listener, options);
|
|
294
|
+
this.listeners.push([type, listener]);
|
|
295
|
+
}
|
|
296
|
+
locked() {
|
|
297
|
+
return this.surface.ownerDocument?.pointerLockElement === this.surface;
|
|
298
|
+
}
|
|
299
|
+
down(event) {
|
|
300
|
+
this.surface.focus?.({ preventScroll: true });
|
|
301
|
+
if (this.pointerLock && event.pointerType !== "touch" && !this.locked()) {
|
|
302
|
+
const request = this.surface.requestPointerLock?.();
|
|
303
|
+
if (request instanceof Promise) request.catch(() => {});
|
|
304
|
+
}
|
|
305
|
+
const button = event.button ?? 0;
|
|
306
|
+
this.pointers.set(event.pointerId, {
|
|
307
|
+
x: event.clientX,
|
|
308
|
+
y: event.clientY,
|
|
309
|
+
startX: event.clientX,
|
|
310
|
+
startY: event.clientY,
|
|
311
|
+
startTime: this.now(),
|
|
312
|
+
moved: false,
|
|
313
|
+
pan: button === 1 || button === 2 || event.shiftKey === true
|
|
314
|
+
});
|
|
315
|
+
this.surface.setPointerCapture?.(event.pointerId);
|
|
316
|
+
}
|
|
317
|
+
move(event) {
|
|
318
|
+
if (this.locked() && this.pointers.size <= 1) {
|
|
319
|
+
this.pending.look[0] += event.movementX ?? 0;
|
|
320
|
+
this.pending.look[1] += event.movementY ?? 0;
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
const pointer = this.pointers.get(event.pointerId);
|
|
324
|
+
if (pointer === void 0) return;
|
|
325
|
+
const dx = event.clientX - pointer.x;
|
|
326
|
+
const dy = event.clientY - pointer.y;
|
|
327
|
+
if (!pointer.moved && Math.hypot(event.clientX - pointer.startX, event.clientY - pointer.startY) > this.tapSlop) pointer.moved = true;
|
|
328
|
+
if (this.pointers.size >= 2) this.twoFinger(event.pointerId, event.clientX, event.clientY);
|
|
329
|
+
else if (pointer.pan) {
|
|
330
|
+
this.pending.pan[0] += dx;
|
|
331
|
+
this.pending.pan[1] += dy;
|
|
332
|
+
} else {
|
|
333
|
+
this.pending.look[0] += dx;
|
|
334
|
+
this.pending.look[1] += dy;
|
|
335
|
+
}
|
|
336
|
+
pointer.x = event.clientX;
|
|
337
|
+
pointer.y = event.clientY;
|
|
338
|
+
}
|
|
339
|
+
twoFinger(movedId, x, y) {
|
|
340
|
+
const [first, second] = [...this.pointers.entries()];
|
|
341
|
+
if (first === void 0 || second === void 0) return;
|
|
342
|
+
const before = [first[1], second[1]];
|
|
343
|
+
const after = [first, second].map(([id, pointer]) => id === movedId ? {
|
|
344
|
+
x,
|
|
345
|
+
y
|
|
346
|
+
} : {
|
|
347
|
+
x: pointer.x,
|
|
348
|
+
y: pointer.y
|
|
349
|
+
});
|
|
350
|
+
const [a0, b0] = before;
|
|
351
|
+
const [a1, b1] = after;
|
|
352
|
+
const distance0 = Math.hypot(b0.x - a0.x, b0.y - a0.y);
|
|
353
|
+
const distance1 = Math.hypot(b1.x - a1.x, b1.y - a1.y);
|
|
354
|
+
if (distance0 > 0 && distance1 > 0) this.pending.zoom += Math.log(distance1 / distance0);
|
|
355
|
+
const angle0 = Math.atan2(b0.y - a0.y, b0.x - a0.x);
|
|
356
|
+
let twist = Math.atan2(b1.y - a1.y, b1.x - a1.x) - angle0;
|
|
357
|
+
if (twist > Math.PI) twist -= Math.PI * 2;
|
|
358
|
+
if (twist < -Math.PI) twist += Math.PI * 2;
|
|
359
|
+
this.pending.twist += twist;
|
|
360
|
+
this.pending.pan[0] += (a1.x + b1.x - a0.x - b0.x) / 2;
|
|
361
|
+
this.pending.pan[1] += (a1.y + b1.y - a0.y - b0.y) / 2;
|
|
362
|
+
for (const pointer of this.pointers.values()) pointer.moved = true;
|
|
363
|
+
}
|
|
364
|
+
up(event, completed) {
|
|
365
|
+
const pointer = this.pointers.get(event.pointerId);
|
|
366
|
+
if (pointer === void 0) return;
|
|
367
|
+
this.pointers.delete(event.pointerId);
|
|
368
|
+
if (this.surface.hasPointerCapture?.(event.pointerId) === true) this.surface.releasePointerCapture?.(event.pointerId);
|
|
369
|
+
const quick = this.now() - pointer.startTime <= this.tapMilliseconds;
|
|
370
|
+
if (completed && !pointer.moved && quick && this.pointers.size === 0) {
|
|
371
|
+
const rect = this.surface.getBoundingClientRect?.() ?? {
|
|
372
|
+
left: 0,
|
|
373
|
+
top: 0
|
|
374
|
+
};
|
|
375
|
+
this.pending.taps.push({
|
|
376
|
+
x: event.clientX - rect.left,
|
|
377
|
+
y: event.clientY - rect.top
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
wheel(event) {
|
|
382
|
+
event.preventDefault?.();
|
|
383
|
+
const pixels = event.deltaY * (event.deltaMode === 1 ? 16 : event.deltaMode === 2 ? 400 : 1);
|
|
384
|
+
this.pending.zoom -= pixels / this.wheelScale;
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
//#endregion
|
|
388
|
+
//#region src/navigation/input-source.ts
|
|
389
|
+
/** Named actions the navigation profiles bind; software controls drive the same names. */
|
|
390
|
+
const NAVIGATION_ACTIONS = {
|
|
391
|
+
forward: "nav-forward",
|
|
392
|
+
back: "nav-back",
|
|
393
|
+
left: "nav-left",
|
|
394
|
+
right: "nav-right",
|
|
395
|
+
up: "nav-up",
|
|
396
|
+
down: "nav-down",
|
|
397
|
+
lookLeft: "nav-look-left",
|
|
398
|
+
lookRight: "nav-look-right",
|
|
399
|
+
lookUp: "nav-look-up",
|
|
400
|
+
lookDown: "nav-look-down",
|
|
401
|
+
sprint: "nav-sprint",
|
|
402
|
+
jump: "nav-jump",
|
|
403
|
+
interact: "nav-interact",
|
|
404
|
+
view: "nav-view",
|
|
405
|
+
throttleUp: "nav-throttle-up",
|
|
406
|
+
throttleDown: "nav-throttle-down",
|
|
407
|
+
yawLeft: "nav-yaw-left",
|
|
408
|
+
yawRight: "nav-yaw-right",
|
|
409
|
+
engine: "nav-engine",
|
|
410
|
+
gear: "nav-gear",
|
|
411
|
+
flaps: "nav-flaps",
|
|
412
|
+
recover: "nav-recover"
|
|
413
|
+
};
|
|
414
|
+
const A = NAVIGATION_ACTIONS;
|
|
415
|
+
const MOVE_KEYS = {
|
|
416
|
+
KeyW: A.forward,
|
|
417
|
+
ArrowUp: A.forward,
|
|
418
|
+
KeyS: A.back,
|
|
419
|
+
ArrowDown: A.back,
|
|
420
|
+
KeyA: A.left,
|
|
421
|
+
ArrowLeft: A.left,
|
|
422
|
+
KeyD: A.right,
|
|
423
|
+
ArrowRight: A.right,
|
|
424
|
+
ShiftLeft: A.sprint,
|
|
425
|
+
ShiftRight: A.sprint
|
|
426
|
+
};
|
|
427
|
+
const STICKS = [
|
|
428
|
+
{
|
|
429
|
+
axis: 0,
|
|
430
|
+
action: A.left,
|
|
431
|
+
mode: "negative"
|
|
432
|
+
},
|
|
433
|
+
{
|
|
434
|
+
axis: 0,
|
|
435
|
+
action: A.right,
|
|
436
|
+
mode: "positive"
|
|
437
|
+
},
|
|
438
|
+
{
|
|
439
|
+
axis: 1,
|
|
440
|
+
action: A.forward,
|
|
441
|
+
mode: "negative"
|
|
442
|
+
},
|
|
443
|
+
{
|
|
444
|
+
axis: 1,
|
|
445
|
+
action: A.back,
|
|
446
|
+
mode: "positive"
|
|
447
|
+
},
|
|
448
|
+
{
|
|
449
|
+
axis: 2,
|
|
450
|
+
action: A.lookLeft,
|
|
451
|
+
mode: "negative"
|
|
452
|
+
},
|
|
453
|
+
{
|
|
454
|
+
axis: 2,
|
|
455
|
+
action: A.lookRight,
|
|
456
|
+
mode: "positive"
|
|
457
|
+
},
|
|
458
|
+
{
|
|
459
|
+
axis: 3,
|
|
460
|
+
action: A.lookUp,
|
|
461
|
+
mode: "negative"
|
|
462
|
+
},
|
|
463
|
+
{
|
|
464
|
+
axis: 3,
|
|
465
|
+
action: A.lookDown,
|
|
466
|
+
mode: "positive"
|
|
467
|
+
}
|
|
468
|
+
];
|
|
469
|
+
/**
|
|
470
|
+
* Default key/gamepad bindings per navigation mode. Orbit and fly use Q/E for down/up; walk and
|
|
471
|
+
* drive use E to interact (board or leave a vehicle), Space to jump or brake, and V to switch
|
|
472
|
+
* the vehicle view. Pilot flies an aircraft: W/S (or ↑/↓) pitch the nose down/up, A/D (or ←/→)
|
|
473
|
+
* bank, Q/Z work the rudder or pedals, Shift/Ctrl (or +/−, Page Up/Down) set throttle or
|
|
474
|
+
* collective, I toggles the engine, G the gear, F the flaps, R recovers after an impact, Space
|
|
475
|
+
* brakes, V switches the view and E leaves. Hosts may replace any profile through
|
|
476
|
+
* `InputMap.defineProfile`.
|
|
477
|
+
*/
|
|
478
|
+
function navigationInputProfiles() {
|
|
479
|
+
const aerial = {
|
|
480
|
+
bindings: {
|
|
481
|
+
...MOVE_KEYS,
|
|
482
|
+
KeyE: A.up,
|
|
483
|
+
PageUp: A.up,
|
|
484
|
+
KeyQ: A.down,
|
|
485
|
+
PageDown: A.down
|
|
486
|
+
},
|
|
487
|
+
axes: STICKS,
|
|
488
|
+
buttons: [
|
|
489
|
+
{
|
|
490
|
+
button: 5,
|
|
491
|
+
action: A.up
|
|
492
|
+
},
|
|
493
|
+
{
|
|
494
|
+
button: 4,
|
|
495
|
+
action: A.down
|
|
496
|
+
},
|
|
497
|
+
{
|
|
498
|
+
button: 10,
|
|
499
|
+
action: A.sprint
|
|
500
|
+
}
|
|
501
|
+
]
|
|
502
|
+
};
|
|
503
|
+
const ground = {
|
|
504
|
+
bindings: {
|
|
505
|
+
...MOVE_KEYS,
|
|
506
|
+
Space: A.jump,
|
|
507
|
+
KeyE: A.interact,
|
|
508
|
+
KeyF: A.interact,
|
|
509
|
+
KeyV: A.view
|
|
510
|
+
},
|
|
511
|
+
axes: STICKS,
|
|
512
|
+
buttons: [
|
|
513
|
+
{
|
|
514
|
+
button: 0,
|
|
515
|
+
action: A.jump
|
|
516
|
+
},
|
|
517
|
+
{
|
|
518
|
+
button: 2,
|
|
519
|
+
action: A.interact
|
|
520
|
+
},
|
|
521
|
+
{
|
|
522
|
+
button: 3,
|
|
523
|
+
action: A.view
|
|
524
|
+
},
|
|
525
|
+
{
|
|
526
|
+
button: 10,
|
|
527
|
+
action: A.sprint
|
|
528
|
+
}
|
|
529
|
+
]
|
|
530
|
+
};
|
|
531
|
+
return {
|
|
532
|
+
orbit: aerial,
|
|
533
|
+
fly: aerial,
|
|
534
|
+
walk: ground,
|
|
535
|
+
drive: ground,
|
|
536
|
+
pilot: {
|
|
537
|
+
bindings: {
|
|
538
|
+
KeyW: A.forward,
|
|
539
|
+
ArrowUp: A.forward,
|
|
540
|
+
KeyS: A.back,
|
|
541
|
+
ArrowDown: A.back,
|
|
542
|
+
KeyA: A.left,
|
|
543
|
+
ArrowLeft: A.left,
|
|
544
|
+
KeyD: A.right,
|
|
545
|
+
ArrowRight: A.right,
|
|
546
|
+
KeyQ: A.yawLeft,
|
|
547
|
+
KeyZ: A.yawRight,
|
|
548
|
+
ShiftLeft: A.throttleUp,
|
|
549
|
+
ShiftRight: A.throttleUp,
|
|
550
|
+
Equal: A.throttleUp,
|
|
551
|
+
NumpadAdd: A.throttleUp,
|
|
552
|
+
PageUp: A.throttleUp,
|
|
553
|
+
ControlLeft: A.throttleDown,
|
|
554
|
+
ControlRight: A.throttleDown,
|
|
555
|
+
Minus: A.throttleDown,
|
|
556
|
+
NumpadSubtract: A.throttleDown,
|
|
557
|
+
PageDown: A.throttleDown,
|
|
558
|
+
Space: A.jump,
|
|
559
|
+
KeyE: A.interact,
|
|
560
|
+
KeyV: A.view,
|
|
561
|
+
KeyI: A.engine,
|
|
562
|
+
KeyG: A.gear,
|
|
563
|
+
KeyF: A.flaps,
|
|
564
|
+
KeyR: A.recover
|
|
565
|
+
},
|
|
566
|
+
axes: STICKS,
|
|
567
|
+
buttons: [
|
|
568
|
+
{
|
|
569
|
+
button: 7,
|
|
570
|
+
action: A.throttleUp
|
|
571
|
+
},
|
|
572
|
+
{
|
|
573
|
+
button: 6,
|
|
574
|
+
action: A.throttleDown
|
|
575
|
+
},
|
|
576
|
+
{
|
|
577
|
+
button: 4,
|
|
578
|
+
action: A.yawLeft
|
|
579
|
+
},
|
|
580
|
+
{
|
|
581
|
+
button: 5,
|
|
582
|
+
action: A.yawRight
|
|
583
|
+
},
|
|
584
|
+
{
|
|
585
|
+
button: 0,
|
|
586
|
+
action: A.jump
|
|
587
|
+
},
|
|
588
|
+
{
|
|
589
|
+
button: 2,
|
|
590
|
+
action: A.interact
|
|
591
|
+
},
|
|
592
|
+
{
|
|
593
|
+
button: 3,
|
|
594
|
+
action: A.view
|
|
595
|
+
},
|
|
596
|
+
{
|
|
597
|
+
button: 1,
|
|
598
|
+
action: A.engine
|
|
599
|
+
}
|
|
600
|
+
]
|
|
601
|
+
}
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
/** An input with no intent; controllers treat it as "hold still". */
|
|
605
|
+
function idleNavigationInput() {
|
|
606
|
+
return {
|
|
607
|
+
move: {
|
|
608
|
+
forward: 0,
|
|
609
|
+
right: 0,
|
|
610
|
+
up: 0
|
|
611
|
+
},
|
|
612
|
+
look: [0, 0],
|
|
613
|
+
pan: [0, 0],
|
|
614
|
+
zoom: 0,
|
|
615
|
+
twist: 0,
|
|
616
|
+
sprint: false,
|
|
617
|
+
jump: false,
|
|
618
|
+
interact: 0,
|
|
619
|
+
view: 0,
|
|
620
|
+
taps: []
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* Combines an InputMap (keys, gamepad, software controls) with pointer gestures into one
|
|
625
|
+
* `NavigationInput` per frame. Switch profiles with `setProfile(mode)` when navigation changes.
|
|
626
|
+
*/
|
|
627
|
+
var NavigationInputSource = class {
|
|
628
|
+
map;
|
|
629
|
+
pointer;
|
|
630
|
+
interactPresses = 0;
|
|
631
|
+
viewPresses = 0;
|
|
632
|
+
profile;
|
|
633
|
+
pilotPresses = {
|
|
634
|
+
engine: 0,
|
|
635
|
+
gear: 0,
|
|
636
|
+
flaps: 0,
|
|
637
|
+
recover: 0
|
|
638
|
+
};
|
|
639
|
+
lookSpeed;
|
|
640
|
+
unsubscribe;
|
|
641
|
+
constructor(options) {
|
|
642
|
+
const profiles = navigationInputProfiles();
|
|
643
|
+
this.map = new InputMap({
|
|
644
|
+
...profiles.orbit,
|
|
645
|
+
profiles,
|
|
646
|
+
profile: options.profile ?? "orbit",
|
|
647
|
+
target: options.keyTarget ?? options.element,
|
|
648
|
+
...options.getGamepads !== void 0 ? { getGamepads: options.getGamepads } : {}
|
|
649
|
+
});
|
|
650
|
+
this.pointer = new NavigationPointer(options.element, options);
|
|
651
|
+
this.lookSpeed = options.gamepadLookSpeed ?? 900;
|
|
652
|
+
this.profile = options.profile ?? "orbit";
|
|
653
|
+
this.unsubscribe = [
|
|
654
|
+
...[
|
|
655
|
+
"engine",
|
|
656
|
+
"gear",
|
|
657
|
+
"flaps",
|
|
658
|
+
"recover"
|
|
659
|
+
].map((name) => this.map.onPress(A[name], () => {
|
|
660
|
+
this.pilotPresses[name]++;
|
|
661
|
+
})),
|
|
662
|
+
this.map.onPress(A.interact, () => {
|
|
663
|
+
this.interactPresses++;
|
|
664
|
+
}),
|
|
665
|
+
this.map.onPress(A.view, () => {
|
|
666
|
+
this.viewPresses++;
|
|
667
|
+
}),
|
|
668
|
+
this.map.onReset(() => this.pointer.reset())
|
|
669
|
+
];
|
|
670
|
+
}
|
|
671
|
+
setProfile(profile) {
|
|
672
|
+
this.profile = profile;
|
|
673
|
+
this.map.setProfile(profile);
|
|
674
|
+
this.pointer.setPointerLock(profile === "walk");
|
|
675
|
+
}
|
|
676
|
+
/** Poll devices and drain this frame's gestures and presses. `dt` is in seconds. */
|
|
677
|
+
read(dt) {
|
|
678
|
+
this.map.update();
|
|
679
|
+
const v = (action) => this.map.value(action);
|
|
680
|
+
const gestures = this.pointer.consume();
|
|
681
|
+
const stickX = v(A.lookRight) - v(A.lookLeft);
|
|
682
|
+
const stickY = v(A.lookDown) - v(A.lookUp);
|
|
683
|
+
const input = {
|
|
684
|
+
move: {
|
|
685
|
+
forward: v(A.forward) - v(A.back),
|
|
686
|
+
right: v(A.right) - v(A.left),
|
|
687
|
+
up: v(A.up) - v(A.down)
|
|
688
|
+
},
|
|
689
|
+
look: [gestures.look[0] + stickX * this.lookSpeed * dt, gestures.look[1] + stickY * this.lookSpeed * dt],
|
|
690
|
+
pan: gestures.pan,
|
|
691
|
+
zoom: gestures.zoom,
|
|
692
|
+
twist: gestures.twist,
|
|
693
|
+
sprint: v(A.sprint) !== 0,
|
|
694
|
+
jump: v(A.jump) !== 0,
|
|
695
|
+
interact: this.interactPresses,
|
|
696
|
+
view: this.viewPresses,
|
|
697
|
+
taps: gestures.taps,
|
|
698
|
+
...this.profile === "pilot" ? { pilot: {
|
|
699
|
+
throttle: v(A.throttleUp) - v(A.throttleDown),
|
|
700
|
+
yaw: v(A.yawRight) - v(A.yawLeft),
|
|
701
|
+
...this.pilotPresses
|
|
702
|
+
} } : {}
|
|
703
|
+
};
|
|
704
|
+
this.interactPresses = 0;
|
|
705
|
+
this.viewPresses = 0;
|
|
706
|
+
this.pilotPresses.engine = 0;
|
|
707
|
+
this.pilotPresses.gear = 0;
|
|
708
|
+
this.pilotPresses.flaps = 0;
|
|
709
|
+
this.pilotPresses.recover = 0;
|
|
710
|
+
return input;
|
|
711
|
+
}
|
|
712
|
+
dispose() {
|
|
713
|
+
for (const unsubscribe of this.unsubscribe) unsubscribe();
|
|
714
|
+
this.pointer.dispose();
|
|
715
|
+
this.map.dispose();
|
|
716
|
+
}
|
|
717
|
+
};
|
|
718
|
+
//#endregion
|
|
719
|
+
//#region src/navigation/orbit.ts
|
|
720
|
+
const TAU = Math.PI * 2;
|
|
721
|
+
function smoothstep(t) {
|
|
722
|
+
return t * t * (3 - 2 * t);
|
|
723
|
+
}
|
|
724
|
+
function wrapAngle(angle) {
|
|
725
|
+
return angle - TAU * Math.floor((angle + Math.PI) / TAU);
|
|
726
|
+
}
|
|
727
|
+
function copyState(state) {
|
|
728
|
+
return {
|
|
729
|
+
...state,
|
|
730
|
+
target: [...state.target]
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
var OrbitController = class {
|
|
734
|
+
goal;
|
|
735
|
+
current;
|
|
736
|
+
flight;
|
|
737
|
+
clock = 0;
|
|
738
|
+
minRange;
|
|
739
|
+
maxRange;
|
|
740
|
+
minPitch;
|
|
741
|
+
maxPitch;
|
|
742
|
+
rotateSensitivity;
|
|
743
|
+
smoothing;
|
|
744
|
+
minClearance;
|
|
745
|
+
constructor(initial, options = {}) {
|
|
746
|
+
this.minRange = options.minRange ?? 20;
|
|
747
|
+
this.maxRange = options.maxRange ?? 4e5;
|
|
748
|
+
this.minPitch = options.minPitch ?? .08;
|
|
749
|
+
this.maxPitch = options.maxPitch ?? Math.PI / 2 - .01;
|
|
750
|
+
this.rotateSensitivity = options.rotateSensitivity ?? .005;
|
|
751
|
+
this.smoothing = options.smoothing ?? .12;
|
|
752
|
+
this.minClearance = options.minClearance ?? 3;
|
|
753
|
+
this.goal = this.clamp(copyState(initial));
|
|
754
|
+
this.current = copyState(this.goal);
|
|
755
|
+
}
|
|
756
|
+
/** The state the camera is currently showing. */
|
|
757
|
+
get state() {
|
|
758
|
+
return copyState(this.current);
|
|
759
|
+
}
|
|
760
|
+
/** Whether a fly-to animation is running. */
|
|
761
|
+
get flying() {
|
|
762
|
+
return this.flight !== void 0;
|
|
763
|
+
}
|
|
764
|
+
/** Jump to a framing immediately (fields omitted keep their values). */
|
|
765
|
+
set(state) {
|
|
766
|
+
this.flight = void 0;
|
|
767
|
+
this.goal = this.clamp({
|
|
768
|
+
...copyState(this.goal),
|
|
769
|
+
...state
|
|
770
|
+
});
|
|
771
|
+
this.current = copyState(this.goal);
|
|
772
|
+
}
|
|
773
|
+
/** Animate to a framing; direct input during the flight cancels it. */
|
|
774
|
+
flyTo(state, options = {}) {
|
|
775
|
+
const from = copyState(this.current);
|
|
776
|
+
const to = this.clamp({
|
|
777
|
+
...copyState(this.goal),
|
|
778
|
+
...state
|
|
779
|
+
});
|
|
780
|
+
const distance = Math.hypot(to.target[0] - from.target[0], to.target[2] - from.target[2]);
|
|
781
|
+
const durationMs = options.durationMs ?? Math.min(3e3, Math.max(600, 600 + Math.log2(1 + distance / 500) * 400));
|
|
782
|
+
const lift = Math.max(0, Math.min(this.maxRange, distance * .6) - Math.max(from.range, to.range));
|
|
783
|
+
this.flight = {
|
|
784
|
+
from,
|
|
785
|
+
to,
|
|
786
|
+
startMs: this.clock,
|
|
787
|
+
durationMs: Math.max(1, durationMs),
|
|
788
|
+
lift
|
|
789
|
+
};
|
|
790
|
+
this.goal = copyState(to);
|
|
791
|
+
}
|
|
792
|
+
/**
|
|
793
|
+
* Advance by `dt` seconds under `input` and return the pose to render. The target settles onto
|
|
794
|
+
* the ground as terrain streams in, and the camera never dips below `minClearance`.
|
|
795
|
+
*/
|
|
796
|
+
update(dt, input, environment) {
|
|
797
|
+
const step = Math.max(0, Math.min(.25, dt));
|
|
798
|
+
this.clock += step * 1e3;
|
|
799
|
+
if (this.flight !== void 0 && this.hasManipulation(input)) {
|
|
800
|
+
this.goal = copyState(this.current);
|
|
801
|
+
this.flight = void 0;
|
|
802
|
+
}
|
|
803
|
+
if (this.flight !== void 0) this.advanceFlight();
|
|
804
|
+
else {
|
|
805
|
+
this.applyInput(step, input);
|
|
806
|
+
const blend = this.smoothing <= 0 ? 1 : 1 - Math.exp(-step / this.smoothing);
|
|
807
|
+
this.current = this.interpolate(this.current, this.goal, blend, 0);
|
|
808
|
+
}
|
|
809
|
+
const ground = environment.groundHeight(this.current.target[0], this.current.target[2]);
|
|
810
|
+
if (ground !== void 0) {
|
|
811
|
+
const settle = 1 - Math.exp(-step / .15);
|
|
812
|
+
this.current.target[1] += (ground - this.current.target[1]) * settle;
|
|
813
|
+
this.goal.target[1] = this.current.target[1];
|
|
814
|
+
}
|
|
815
|
+
return this.pose(environment);
|
|
816
|
+
}
|
|
817
|
+
hasManipulation(input) {
|
|
818
|
+
const { move } = input;
|
|
819
|
+
return input.look[0] !== 0 || input.look[1] !== 0 || input.pan[0] !== 0 || input.pan[1] !== 0 || input.zoom !== 0 || input.twist !== 0 || move.forward !== 0 || move.right !== 0 || move.up !== 0;
|
|
820
|
+
}
|
|
821
|
+
applyInput(dt, input) {
|
|
822
|
+
const goal = this.goal;
|
|
823
|
+
goal.heading = wrapAngle(goal.heading - input.look[0] * this.rotateSensitivity - input.twist);
|
|
824
|
+
goal.pitch += input.look[1] * this.rotateSensitivity;
|
|
825
|
+
const zoom = input.zoom - input.move.up * dt * 1.5;
|
|
826
|
+
goal.range *= Math.exp(-zoom);
|
|
827
|
+
const metersPerPixel = goal.range * .0012 / Math.max(.3, Math.sin(goal.pitch));
|
|
828
|
+
const keySpeed = goal.range * (input.sprint ? 2.4 : .8);
|
|
829
|
+
const alongRight = -input.pan[0] * metersPerPixel + input.move.right * keySpeed * dt;
|
|
830
|
+
const alongForward = input.pan[1] * metersPerPixel + input.move.forward * keySpeed * dt;
|
|
831
|
+
const sin = Math.sin(goal.heading);
|
|
832
|
+
const cos = Math.cos(goal.heading);
|
|
833
|
+
goal.target[0] += sin * alongForward + cos * alongRight;
|
|
834
|
+
goal.target[2] += -cos * alongForward + sin * alongRight;
|
|
835
|
+
this.goal = this.clamp(goal);
|
|
836
|
+
}
|
|
837
|
+
advanceFlight() {
|
|
838
|
+
const flight = this.flight;
|
|
839
|
+
const t = Math.min(1, (this.clock - flight.startMs) / flight.durationMs);
|
|
840
|
+
const eased = smoothstep(t);
|
|
841
|
+
this.current = this.interpolate(flight.from, flight.to, eased, flight.lift * Math.sin(Math.PI * t));
|
|
842
|
+
if (t >= 1) {
|
|
843
|
+
this.current = copyState(flight.to);
|
|
844
|
+
this.flight = void 0;
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
interpolate(from, to, t, lift) {
|
|
848
|
+
const heading = from.heading + wrapAngle(to.heading - from.heading) * t;
|
|
849
|
+
return {
|
|
850
|
+
target: [
|
|
851
|
+
from.target[0] + (to.target[0] - from.target[0]) * t,
|
|
852
|
+
from.target[1] + (to.target[1] - from.target[1]) * t,
|
|
853
|
+
from.target[2] + (to.target[2] - from.target[2]) * t
|
|
854
|
+
],
|
|
855
|
+
range: Math.exp(Math.log(from.range) + (Math.log(to.range) - Math.log(from.range)) * t) + lift,
|
|
856
|
+
heading: wrapAngle(heading),
|
|
857
|
+
pitch: from.pitch + (to.pitch - from.pitch) * t
|
|
858
|
+
};
|
|
859
|
+
}
|
|
860
|
+
clamp(state) {
|
|
861
|
+
state.range = Math.min(this.maxRange, Math.max(this.minRange, state.range));
|
|
862
|
+
state.pitch = Math.min(this.maxPitch, Math.max(this.minPitch, state.pitch));
|
|
863
|
+
state.heading = wrapAngle(state.heading);
|
|
864
|
+
return state;
|
|
865
|
+
}
|
|
866
|
+
pose(environment) {
|
|
867
|
+
const { target, range, heading, pitch } = this.current;
|
|
868
|
+
const direction = [
|
|
869
|
+
Math.sin(heading) * Math.cos(pitch),
|
|
870
|
+
-Math.sin(pitch),
|
|
871
|
+
-Math.cos(heading) * Math.cos(pitch)
|
|
872
|
+
];
|
|
873
|
+
const position = [
|
|
874
|
+
target[0] - direction[0] * range,
|
|
875
|
+
target[1] - direction[1] * range,
|
|
876
|
+
target[2] - direction[2] * range
|
|
877
|
+
];
|
|
878
|
+
const ground = environment.groundHeight(position[0], position[2]);
|
|
879
|
+
if (ground !== void 0 && position[1] < ground + this.minClearance) {
|
|
880
|
+
position[1] = ground + this.minClearance;
|
|
881
|
+
const dx = target[0] - position[0];
|
|
882
|
+
const dy = target[1] - position[1];
|
|
883
|
+
const dz = target[2] - position[2];
|
|
884
|
+
const length = Math.hypot(dx, dy, dz) || 1;
|
|
885
|
+
direction[0] = dx / length;
|
|
886
|
+
direction[1] = dy / length;
|
|
887
|
+
direction[2] = dz / length;
|
|
888
|
+
}
|
|
889
|
+
return {
|
|
890
|
+
position,
|
|
891
|
+
lookAt: [...target],
|
|
892
|
+
direction
|
|
893
|
+
};
|
|
894
|
+
}
|
|
895
|
+
};
|
|
896
|
+
//#endregion
|
|
897
|
+
//#region src/navigation/touch-joystick.ts
|
|
898
|
+
/** Stick geometry without a DOM: feed it pointer positions, read axes. */
|
|
899
|
+
var TouchStick = class {
|
|
900
|
+
center;
|
|
901
|
+
offset = {
|
|
902
|
+
x: 0,
|
|
903
|
+
y: 0
|
|
904
|
+
};
|
|
905
|
+
radius;
|
|
906
|
+
deadZone;
|
|
907
|
+
constructor(options = {}) {
|
|
908
|
+
this.radius = options.radius ?? 56;
|
|
909
|
+
this.deadZone = options.deadZone ?? .12;
|
|
910
|
+
}
|
|
911
|
+
get active() {
|
|
912
|
+
return this.center !== void 0;
|
|
913
|
+
}
|
|
914
|
+
/** Knob displacement from the center in CSS pixels, clamped to the radius. */
|
|
915
|
+
get knob() {
|
|
916
|
+
return { ...this.offset };
|
|
917
|
+
}
|
|
918
|
+
press(x, y) {
|
|
919
|
+
this.center = {
|
|
920
|
+
x,
|
|
921
|
+
y
|
|
922
|
+
};
|
|
923
|
+
this.offset = {
|
|
924
|
+
x: 0,
|
|
925
|
+
y: 0
|
|
926
|
+
};
|
|
927
|
+
}
|
|
928
|
+
drag(x, y) {
|
|
929
|
+
if (this.center === void 0) return;
|
|
930
|
+
let dx = x - this.center.x;
|
|
931
|
+
let dy = y - this.center.y;
|
|
932
|
+
const length = Math.hypot(dx, dy);
|
|
933
|
+
if (length > this.radius) {
|
|
934
|
+
dx *= this.radius / length;
|
|
935
|
+
dy *= this.radius / length;
|
|
936
|
+
}
|
|
937
|
+
this.offset = {
|
|
938
|
+
x: dx,
|
|
939
|
+
y: dy
|
|
940
|
+
};
|
|
941
|
+
}
|
|
942
|
+
release() {
|
|
943
|
+
this.center = void 0;
|
|
944
|
+
this.offset = {
|
|
945
|
+
x: 0,
|
|
946
|
+
y: 0
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
/** Movement intent: forward is up the screen, both in -1..1 after the dead zone. */
|
|
950
|
+
axes() {
|
|
951
|
+
const length = Math.hypot(this.offset.x, this.offset.y) / this.radius;
|
|
952
|
+
if (length <= this.deadZone) return {
|
|
953
|
+
forward: 0,
|
|
954
|
+
right: 0
|
|
955
|
+
};
|
|
956
|
+
const scale = (length - this.deadZone) / (1 - this.deadZone) / length;
|
|
957
|
+
const right = this.offset.x / this.radius * scale;
|
|
958
|
+
const forward = -this.offset.y / this.radius * scale;
|
|
959
|
+
return {
|
|
960
|
+
forward: Math.abs(forward) < 1e-9 ? 0 : forward,
|
|
961
|
+
right
|
|
962
|
+
};
|
|
963
|
+
}
|
|
964
|
+
};
|
|
965
|
+
const ACTIONS = NAVIGATION_ACTIONS;
|
|
966
|
+
/**
|
|
967
|
+
* Add a movement stick to `container` (bottom-left by default styling). The stick captures its
|
|
968
|
+
* own pointer, so the rest of the canvas keeps handling look/zoom gestures.
|
|
969
|
+
*/
|
|
970
|
+
function createTouchJoystick(container, input, options = {}) {
|
|
971
|
+
const stick = new TouchStick(options);
|
|
972
|
+
const source = options.source ?? "touch-joystick";
|
|
973
|
+
const document = container.ownerDocument;
|
|
974
|
+
const root = document.createElement("div");
|
|
975
|
+
root.className = ["molen-touch-joystick", options.className].filter(Boolean).join(" ");
|
|
976
|
+
root.setAttribute("aria-hidden", "true");
|
|
977
|
+
const size = stick.radius * 2 + 32;
|
|
978
|
+
Object.assign(root.style, {
|
|
979
|
+
position: "absolute",
|
|
980
|
+
left: "24px",
|
|
981
|
+
bottom: "24px",
|
|
982
|
+
width: `${size}px`,
|
|
983
|
+
height: `${size}px`,
|
|
984
|
+
borderRadius: "50%",
|
|
985
|
+
background: "rgba(255, 255, 255, 0.16)",
|
|
986
|
+
border: "2px solid rgba(255, 255, 255, 0.45)",
|
|
987
|
+
touchAction: "none",
|
|
988
|
+
userSelect: "none",
|
|
989
|
+
zIndex: "2"
|
|
990
|
+
});
|
|
991
|
+
const knob = document.createElement("div");
|
|
992
|
+
Object.assign(knob.style, {
|
|
993
|
+
position: "absolute",
|
|
994
|
+
left: "50%",
|
|
995
|
+
top: "50%",
|
|
996
|
+
width: "56px",
|
|
997
|
+
height: "56px",
|
|
998
|
+
margin: "-28px 0 0 -28px",
|
|
999
|
+
borderRadius: "50%",
|
|
1000
|
+
background: "rgba(255, 255, 255, 0.75)",
|
|
1001
|
+
boxShadow: "0 2px 6px rgba(0, 0, 0, 0.3)",
|
|
1002
|
+
pointerEvents: "none"
|
|
1003
|
+
});
|
|
1004
|
+
root.append(knob);
|
|
1005
|
+
container.append(root);
|
|
1006
|
+
let pointer;
|
|
1007
|
+
const publish = () => {
|
|
1008
|
+
const { forward, right } = stick.axes();
|
|
1009
|
+
input.setVirtual(source, ACTIONS.forward, Math.max(0, forward));
|
|
1010
|
+
input.setVirtual(source, ACTIONS.back, Math.max(0, -forward));
|
|
1011
|
+
input.setVirtual(source, ACTIONS.right, Math.max(0, right));
|
|
1012
|
+
input.setVirtual(source, ACTIONS.left, Math.max(0, -right));
|
|
1013
|
+
const { x, y } = stick.knob;
|
|
1014
|
+
knob.style.transform = `translate(${x}px, ${y}px)`;
|
|
1015
|
+
};
|
|
1016
|
+
const center = () => {
|
|
1017
|
+
const rect = root.getBoundingClientRect();
|
|
1018
|
+
return {
|
|
1019
|
+
x: rect.left + rect.width / 2,
|
|
1020
|
+
y: rect.top + rect.height / 2
|
|
1021
|
+
};
|
|
1022
|
+
};
|
|
1023
|
+
const onDown = (event) => {
|
|
1024
|
+
if (pointer !== void 0) return;
|
|
1025
|
+
event.preventDefault();
|
|
1026
|
+
event.stopPropagation();
|
|
1027
|
+
pointer = event.pointerId;
|
|
1028
|
+
root.setPointerCapture(event.pointerId);
|
|
1029
|
+
const { x, y } = center();
|
|
1030
|
+
stick.press(x, y);
|
|
1031
|
+
stick.drag(event.clientX, event.clientY);
|
|
1032
|
+
publish();
|
|
1033
|
+
};
|
|
1034
|
+
const onMove = (event) => {
|
|
1035
|
+
if (event.pointerId !== pointer) return;
|
|
1036
|
+
event.stopPropagation();
|
|
1037
|
+
stick.drag(event.clientX, event.clientY);
|
|
1038
|
+
publish();
|
|
1039
|
+
};
|
|
1040
|
+
const onUp = (event) => {
|
|
1041
|
+
if (event.pointerId !== pointer) return;
|
|
1042
|
+
event.stopPropagation();
|
|
1043
|
+
pointer = void 0;
|
|
1044
|
+
stick.release();
|
|
1045
|
+
input.clearVirtual(source);
|
|
1046
|
+
knob.style.transform = "";
|
|
1047
|
+
};
|
|
1048
|
+
root.addEventListener("pointerdown", onDown);
|
|
1049
|
+
root.addEventListener("pointermove", onMove);
|
|
1050
|
+
root.addEventListener("pointerup", onUp);
|
|
1051
|
+
root.addEventListener("pointercancel", onUp);
|
|
1052
|
+
return {
|
|
1053
|
+
element: root,
|
|
1054
|
+
stick,
|
|
1055
|
+
setVisible(visible) {
|
|
1056
|
+
root.style.display = visible ? "" : "none";
|
|
1057
|
+
if (!visible) {
|
|
1058
|
+
pointer = void 0;
|
|
1059
|
+
stick.release();
|
|
1060
|
+
input.clearVirtual(source);
|
|
1061
|
+
}
|
|
1062
|
+
},
|
|
1063
|
+
dispose() {
|
|
1064
|
+
root.removeEventListener("pointerdown", onDown);
|
|
1065
|
+
root.removeEventListener("pointermove", onMove);
|
|
1066
|
+
root.removeEventListener("pointerup", onUp);
|
|
1067
|
+
root.removeEventListener("pointercancel", onUp);
|
|
1068
|
+
input.clearVirtual(source);
|
|
1069
|
+
root.remove();
|
|
1070
|
+
}
|
|
1071
|
+
};
|
|
1072
|
+
}
|
|
1073
|
+
const MIN_CELLS = 4;
|
|
1074
|
+
const MAX_CELLS = 64;
|
|
1075
|
+
const TARGET_TRIANGLES_PER_CELL = 48;
|
|
1076
|
+
const indexes = /* @__PURE__ */ new WeakMap();
|
|
1077
|
+
/** The geometry's index, building it on first use or after its buffers change; undefined when small. */
|
|
1078
|
+
function triangleIndexFor(geometry) {
|
|
1079
|
+
const position = geometry.getAttribute("position");
|
|
1080
|
+
if (position === void 0) return void 0;
|
|
1081
|
+
const index = geometry.index;
|
|
1082
|
+
const triangleCount = Math.floor((index?.count ?? position.count) / 3);
|
|
1083
|
+
if (triangleCount < 4096) return void 0;
|
|
1084
|
+
const positionVersion = position.version ?? 0;
|
|
1085
|
+
const indexVersion = index?.version ?? -1;
|
|
1086
|
+
const existing = indexes.get(geometry);
|
|
1087
|
+
if (existing !== void 0 && existing.positionVersion === positionVersion && existing.indexVersion === indexVersion && existing.triangleCount === triangleCount) return existing;
|
|
1088
|
+
const built = {
|
|
1089
|
+
...buildTriangleIndex(position, index, triangleCount),
|
|
1090
|
+
positionVersion,
|
|
1091
|
+
indexVersion,
|
|
1092
|
+
triangleCount
|
|
1093
|
+
};
|
|
1094
|
+
indexes.set(geometry, built);
|
|
1095
|
+
return built;
|
|
1096
|
+
}
|
|
1097
|
+
function cellOf(value, min, size, cells) {
|
|
1098
|
+
return Math.max(0, Math.min(cells - 1, Math.floor((value - min) / size)));
|
|
1099
|
+
}
|
|
1100
|
+
/** @internal Exposed for tests; hosts use {@link triangleIndexFor}. */
|
|
1101
|
+
function buildTriangleIndex(position, index, triangleCount) {
|
|
1102
|
+
let minX = Number.POSITIVE_INFINITY;
|
|
1103
|
+
let maxX = Number.NEGATIVE_INFINITY;
|
|
1104
|
+
let minZ = Number.POSITIVE_INFINITY;
|
|
1105
|
+
let maxZ = Number.NEGATIVE_INFINITY;
|
|
1106
|
+
for (let i = 0; i < position.count; i++) {
|
|
1107
|
+
const x = position.getX(i);
|
|
1108
|
+
const z = position.getZ(i);
|
|
1109
|
+
if (x < minX) minX = x;
|
|
1110
|
+
if (x > maxX) maxX = x;
|
|
1111
|
+
if (z < minZ) minZ = z;
|
|
1112
|
+
if (z > maxZ) maxZ = z;
|
|
1113
|
+
}
|
|
1114
|
+
if (!Number.isFinite(minX) || !Number.isFinite(minZ)) minX = maxX = minZ = maxZ = 0;
|
|
1115
|
+
const cells = Math.max(MIN_CELLS, Math.min(MAX_CELLS, Math.round(Math.sqrt(triangleCount / TARGET_TRIANGLES_PER_CELL))));
|
|
1116
|
+
const cellWidth = Math.max(maxX - minX, 1e-6) / cells;
|
|
1117
|
+
const cellDepth = Math.max(maxZ - minZ, 1e-6) / cells;
|
|
1118
|
+
const ranges = new Int32Array(triangleCount * 4);
|
|
1119
|
+
const offsets = new Uint32Array(cells * cells + 1);
|
|
1120
|
+
for (let t = 0; t < triangleCount; t++) {
|
|
1121
|
+
const vertex = t * 3;
|
|
1122
|
+
const i0 = index === null ? vertex : index.getX(vertex);
|
|
1123
|
+
const i1 = index === null ? vertex + 1 : index.getX(vertex + 1);
|
|
1124
|
+
const i2 = index === null ? vertex + 2 : index.getX(vertex + 2);
|
|
1125
|
+
const x0 = position.getX(i0);
|
|
1126
|
+
const x1 = position.getX(i1);
|
|
1127
|
+
const x2 = position.getX(i2);
|
|
1128
|
+
const z0 = position.getZ(i0);
|
|
1129
|
+
const z1 = position.getZ(i1);
|
|
1130
|
+
const z2 = position.getZ(i2);
|
|
1131
|
+
const cx0 = cellOf(Math.min(x0, x1, x2), minX, cellWidth, cells);
|
|
1132
|
+
const cx1 = cellOf(Math.max(x0, x1, x2), minX, cellWidth, cells);
|
|
1133
|
+
const cz0 = cellOf(Math.min(z0, z1, z2), minZ, cellDepth, cells);
|
|
1134
|
+
const cz1 = cellOf(Math.max(z0, z1, z2), minZ, cellDepth, cells);
|
|
1135
|
+
ranges[t * 4] = cx0;
|
|
1136
|
+
ranges[t * 4 + 1] = cx1;
|
|
1137
|
+
ranges[t * 4 + 2] = cz0;
|
|
1138
|
+
ranges[t * 4 + 3] = cz1;
|
|
1139
|
+
for (let cz = cz0; cz <= cz1; cz++) for (let cx = cx0; cx <= cx1; cx++) {
|
|
1140
|
+
const slot = cz * cells + cx + 1;
|
|
1141
|
+
offsets[slot] = offsets[slot] + 1;
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
for (let cell = 0; cell < cells * cells; cell++) offsets[cell + 1] = offsets[cell] + offsets[cell + 1];
|
|
1145
|
+
const triangles = new Uint32Array(offsets[cells * cells]);
|
|
1146
|
+
const cursor = offsets.slice(0, cells * cells);
|
|
1147
|
+
for (let t = 0; t < triangleCount; t++) {
|
|
1148
|
+
const cx0 = ranges[t * 4];
|
|
1149
|
+
const cx1 = ranges[t * 4 + 1];
|
|
1150
|
+
const cz0 = ranges[t * 4 + 2];
|
|
1151
|
+
const cz1 = ranges[t * 4 + 3];
|
|
1152
|
+
for (let cz = cz0; cz <= cz1; cz++) for (let cx = cx0; cx <= cx1; cx++) {
|
|
1153
|
+
const cell = cz * cells + cx;
|
|
1154
|
+
triangles[cursor[cell]] = t;
|
|
1155
|
+
cursor[cell] = cursor[cell] + 1;
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
return {
|
|
1159
|
+
cells,
|
|
1160
|
+
minX,
|
|
1161
|
+
minZ,
|
|
1162
|
+
cellWidth,
|
|
1163
|
+
cellDepth,
|
|
1164
|
+
offsets,
|
|
1165
|
+
triangles,
|
|
1166
|
+
seen: new Uint32Array(triangleCount),
|
|
1167
|
+
stamp: 0
|
|
1168
|
+
};
|
|
1169
|
+
}
|
|
1170
|
+
/**
|
|
1171
|
+
* Visit each triangle whose local-space footprint can overlap the XZ box, once. Candidates are
|
|
1172
|
+
* cell-level: the caller still tests the triangle itself.
|
|
1173
|
+
*/
|
|
1174
|
+
function forEachIndexedTriangle(index, minX, maxX, minZ, maxZ, visit) {
|
|
1175
|
+
const { cells, cellWidth, cellDepth, offsets, triangles, seen } = index;
|
|
1176
|
+
if (!(maxX >= minX && maxZ >= minZ) || maxX < index.minX || minX > index.minX + cellWidth * cells || maxZ < index.minZ || minZ > index.minZ + cellDepth * cells) return;
|
|
1177
|
+
if (index.stamp === 4294967295) {
|
|
1178
|
+
seen.fill(0);
|
|
1179
|
+
index.stamp = 0;
|
|
1180
|
+
}
|
|
1181
|
+
const stamp = ++index.stamp;
|
|
1182
|
+
const cx0 = cellOf(minX, index.minX, cellWidth, cells);
|
|
1183
|
+
const cx1 = cellOf(maxX, index.minX, cellWidth, cells);
|
|
1184
|
+
const cz0 = cellOf(minZ, index.minZ, cellDepth, cells);
|
|
1185
|
+
const cz1 = cellOf(maxZ, index.minZ, cellDepth, cells);
|
|
1186
|
+
for (let cz = cz0; cz <= cz1; cz++) for (let cx = cx0; cx <= cx1; cx++) {
|
|
1187
|
+
const cell = cz * cells + cx;
|
|
1188
|
+
const end = offsets[cell + 1];
|
|
1189
|
+
for (let k = offsets[cell]; k < end; k++) {
|
|
1190
|
+
const triangle = triangles[k];
|
|
1191
|
+
if (seen[triangle] === stamp) continue;
|
|
1192
|
+
seen[triangle] = stamp;
|
|
1193
|
+
visit(triangle);
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
//#endregion
|
|
1198
|
+
//#region src/navigation/walk-collision.ts
|
|
1199
|
+
const COLLISION_RADIUS = 24;
|
|
1200
|
+
const REBUILD_DISTANCE = 6;
|
|
1201
|
+
function requireFinite(value, name, allowZero) {
|
|
1202
|
+
if (!Number.isFinite(value) || (allowZero ? value < 0 : value <= 0)) throw new RangeError(`${name} must be a finite ${allowZero ? "nonnegative" : "positive"} number`);
|
|
1203
|
+
return value;
|
|
1204
|
+
}
|
|
1205
|
+
/**
|
|
1206
|
+
* A small collision neighborhood in metric coordinates, independent of floating-origin shifts.
|
|
1207
|
+
* Only visible, resident geometry under `root` participates; evicted tiles and hidden layers
|
|
1208
|
+
* disappear on the next scan, and instance transforms are expanded only inside the
|
|
1209
|
+
* neighborhood. Large geometries (terrain tiles, building cells) are walked through a per-geometry
|
|
1210
|
+
* spatial index, so a rebuild costs the triangles near the subject rather than the whole tile.
|
|
1211
|
+
* Meshes opt out with `userData.walkIgnore`, and water materials (`userData.terrainWaterMaterial`)
|
|
1212
|
+
* and `semantic:landcover` overlays are never solid.
|
|
1213
|
+
*/
|
|
1214
|
+
var WalkCollision = class {
|
|
1215
|
+
origin = new THREE.Vector3();
|
|
1216
|
+
octree = new CollisionTree();
|
|
1217
|
+
ignoreVehicles;
|
|
1218
|
+
radius;
|
|
1219
|
+
rebuildDistance;
|
|
1220
|
+
verticalRadius;
|
|
1221
|
+
rescanIntervalMs;
|
|
1222
|
+
now;
|
|
1223
|
+
meshes = [];
|
|
1224
|
+
initialized = false;
|
|
1225
|
+
/** Vertical center of the current neighborhood, or undefined when it spans the whole column. */
|
|
1226
|
+
centerY;
|
|
1227
|
+
scannedAt = Number.NEGATIVE_INFINITY;
|
|
1228
|
+
stale = true;
|
|
1229
|
+
constructor(options = false) {
|
|
1230
|
+
const resolved = typeof options === "boolean" ? { ignoreVehicles: options } : options;
|
|
1231
|
+
this.ignoreVehicles = resolved.ignoreVehicles ?? false;
|
|
1232
|
+
this.radius = requireFinite(resolved.radius ?? COLLISION_RADIUS, "radius", false);
|
|
1233
|
+
this.rebuildDistance = requireFinite(resolved.rebuildDistance ?? REBUILD_DISTANCE, "rebuildDistance", true);
|
|
1234
|
+
if (this.rebuildDistance >= this.radius) throw new RangeError("rebuildDistance must be smaller than radius");
|
|
1235
|
+
this.verticalRadius = resolved.verticalRadius === void 0 ? void 0 : requireFinite(resolved.verticalRadius, "verticalRadius", false);
|
|
1236
|
+
this.rescanIntervalMs = requireFinite(resolved.rescanIntervalMs ?? 0, "rescanIntervalMs", true);
|
|
1237
|
+
this.now = resolved.now ?? (() => performance.now());
|
|
1238
|
+
}
|
|
1239
|
+
/**
|
|
1240
|
+
* Track the subject at (x, z). With a `verticalRadius`, `y` bounds the neighborhood vertically.
|
|
1241
|
+
* Scans the visible scene when the subject leaves the neighborhood, when the rescan interval
|
|
1242
|
+
* has passed, or after `invalidate()`; rebuilds the tree only when nearby geometry changed.
|
|
1243
|
+
*/
|
|
1244
|
+
update(root, x, z, y) {
|
|
1245
|
+
const band = this.verticalRadius !== void 0 && y !== void 0 ? y : void 0;
|
|
1246
|
+
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;
|
|
1247
|
+
const now = this.now();
|
|
1248
|
+
if (!moved && !this.stale && now - this.scannedAt < this.rescanIntervalMs) return;
|
|
1249
|
+
root.updateWorldMatrix(true, true);
|
|
1250
|
+
const inverse = root.matrixWorld.clone().invert();
|
|
1251
|
+
const meshes = [];
|
|
1252
|
+
const centerX = moved ? x : this.origin.x;
|
|
1253
|
+
const centerZ = moved ? z : this.origin.z;
|
|
1254
|
+
const centerY = moved ? band : this.centerY;
|
|
1255
|
+
const reach = this.radius + this.rebuildDistance;
|
|
1256
|
+
const verticalReach = (this.verticalRadius ?? 0) + this.rebuildDistance;
|
|
1257
|
+
const bounds = new THREE.Box3();
|
|
1258
|
+
root.traverseVisible((object) => {
|
|
1259
|
+
if (!(object instanceof THREE.Mesh) || object.userData.walkIgnore === true) return;
|
|
1260
|
+
if (this.ignoreVehicles && (object.userData.vehicleIds || object.userData.vehicleGeometry)) return;
|
|
1261
|
+
if (object.name === "semantic:landcover") return;
|
|
1262
|
+
if ((Array.isArray(object.material) ? object.material : [object.material]).every((material) => material.userData.terrainWaterMaterial === true)) return;
|
|
1263
|
+
const matrix = new THREE.Matrix4().multiplyMatrices(inverse, object.matrixWorld);
|
|
1264
|
+
if (object instanceof THREE.InstancedMesh) {
|
|
1265
|
+
if (object.boundingBox === null) object.computeBoundingBox();
|
|
1266
|
+
bounds.copy(object.boundingBox);
|
|
1267
|
+
} else {
|
|
1268
|
+
if (object.geometry.boundingBox === null) object.geometry.computeBoundingBox();
|
|
1269
|
+
bounds.copy(object.geometry.boundingBox);
|
|
1270
|
+
}
|
|
1271
|
+
bounds.applyMatrix4(matrix);
|
|
1272
|
+
if (bounds.min.x > centerX + reach || bounds.max.x < centerX - reach || bounds.min.z > centerZ + reach || bounds.max.z < centerZ - reach) return;
|
|
1273
|
+
if (centerY !== void 0 && (bounds.min.y > centerY + verticalReach || bounds.max.y < centerY - verticalReach)) return;
|
|
1274
|
+
meshes.push({
|
|
1275
|
+
mesh: object,
|
|
1276
|
+
matrix,
|
|
1277
|
+
count: object instanceof THREE.InstancedMesh ? object.count : 1,
|
|
1278
|
+
geometry: object.geometry,
|
|
1279
|
+
version: object instanceof THREE.InstancedMesh ? object.instanceMatrix.version : (object.geometry.index?.version ?? 0) + (object.geometry.getAttribute("position")?.version ?? 0)
|
|
1280
|
+
});
|
|
1281
|
+
});
|
|
1282
|
+
this.scannedAt = now;
|
|
1283
|
+
this.stale = false;
|
|
1284
|
+
const unchanged = meshes.length === this.meshes.length && meshes.every((entry, index) => {
|
|
1285
|
+
const previous = this.meshes[index];
|
|
1286
|
+
return previous?.mesh === entry.mesh && previous.count === entry.count && previous.geometry === entry.geometry && previous.version === entry.version && previous.matrix.equals(entry.matrix);
|
|
1287
|
+
});
|
|
1288
|
+
if (this.initialized && unchanged && !moved) return;
|
|
1289
|
+
this.initialized = true;
|
|
1290
|
+
this.meshes = meshes;
|
|
1291
|
+
this.origin.set(x, 0, z);
|
|
1292
|
+
this.centerY = band;
|
|
1293
|
+
this.rebuild(meshes, x, z, band);
|
|
1294
|
+
}
|
|
1295
|
+
/**
|
|
1296
|
+
* Scan the scene on the next `update` regardless of the rescan interval: call it before a
|
|
1297
|
+
* query that must see geometry published this frame, such as placing a walker.
|
|
1298
|
+
*/
|
|
1299
|
+
invalidate() {
|
|
1300
|
+
this.stale = true;
|
|
1301
|
+
}
|
|
1302
|
+
clear() {
|
|
1303
|
+
this.meshes = [];
|
|
1304
|
+
this.initialized = false;
|
|
1305
|
+
this.centerY = void 0;
|
|
1306
|
+
this.scannedAt = Number.NEGATIVE_INFINITY;
|
|
1307
|
+
this.stale = true;
|
|
1308
|
+
this.octree.clear();
|
|
1309
|
+
}
|
|
1310
|
+
rebuild(meshes, x, z, centerY) {
|
|
1311
|
+
this.octree.clear();
|
|
1312
|
+
const radius = this.radius;
|
|
1313
|
+
const minY = centerY === void 0 ? Number.NEGATIVE_INFINITY : centerY - this.verticalRadius;
|
|
1314
|
+
const maxY = centerY === void 0 ? Number.POSITIVE_INFINITY : centerY + this.verticalRadius;
|
|
1315
|
+
const offset = new THREE.Matrix4().makeTranslation(-x, 0, -z);
|
|
1316
|
+
const local = new THREE.Matrix4();
|
|
1317
|
+
const instance = new THREE.Matrix4();
|
|
1318
|
+
const transform = new THREE.Matrix4();
|
|
1319
|
+
const toLocal = new THREE.Matrix4();
|
|
1320
|
+
const bounds = new THREE.Box3();
|
|
1321
|
+
const query = new THREE.Box3();
|
|
1322
|
+
const a = new THREE.Vector3();
|
|
1323
|
+
const b = new THREE.Vector3();
|
|
1324
|
+
const c = new THREE.Vector3();
|
|
1325
|
+
const inside = () => bounds.min.x <= radius && bounds.max.x >= -radius && bounds.min.z <= radius && bounds.max.z >= -radius && bounds.min.y <= maxY && bounds.max.y >= minY;
|
|
1326
|
+
for (const { mesh, matrix, count } of meshes) {
|
|
1327
|
+
const geometry = mesh.geometry;
|
|
1328
|
+
const position = geometry.getAttribute("position");
|
|
1329
|
+
if (position === void 0) continue;
|
|
1330
|
+
if (geometry.boundingBox === null) geometry.computeBoundingBox();
|
|
1331
|
+
local.multiplyMatrices(offset, matrix);
|
|
1332
|
+
if (mesh instanceof THREE.InstancedMesh && mesh.boundingBox === null) mesh.computeBoundingBox();
|
|
1333
|
+
bounds.copy(mesh instanceof THREE.InstancedMesh ? mesh.boundingBox : geometry.boundingBox).applyMatrix4(local);
|
|
1334
|
+
if (!inside()) continue;
|
|
1335
|
+
const index = geometry.index;
|
|
1336
|
+
const start = geometry.drawRange.start;
|
|
1337
|
+
const end = Math.min(index?.count ?? position.count, start + geometry.drawRange.count);
|
|
1338
|
+
const doubleSided = mesh.userData.walkDoubleSided === true;
|
|
1339
|
+
const spatial = triangleIndexFor(geometry);
|
|
1340
|
+
const visit = (vertex) => {
|
|
1341
|
+
if (vertex < start || vertex + 2 >= end) return;
|
|
1342
|
+
a.fromBufferAttribute(position, index?.getX(vertex) ?? vertex).applyMatrix4(transform);
|
|
1343
|
+
b.fromBufferAttribute(position, index?.getX(vertex + 1) ?? vertex + 1).applyMatrix4(transform);
|
|
1344
|
+
c.fromBufferAttribute(position, index?.getX(vertex + 2) ?? vertex + 2).applyMatrix4(transform);
|
|
1345
|
+
if (a.equals(b) && b.equals(c)) return;
|
|
1346
|
+
if (Math.min(a.x, b.x, c.x) > radius || Math.max(a.x, b.x, c.x) < -radius || Math.min(a.z, b.z, c.z) > radius || Math.max(a.z, b.z, c.z) < -radius || Math.min(a.y, b.y, c.y) > maxY || Math.max(a.y, b.y, c.y) < minY) return;
|
|
1347
|
+
this.octree.addTriangle(new THREE.Triangle(a.clone(), b.clone(), c.clone()));
|
|
1348
|
+
if (doubleSided) this.octree.addTriangle(new THREE.Triangle(c.clone(), b.clone(), a.clone()));
|
|
1349
|
+
};
|
|
1350
|
+
for (let i = 0; i < count; i++) {
|
|
1351
|
+
if (mesh instanceof THREE.InstancedMesh) {
|
|
1352
|
+
mesh.getMatrixAt(i, instance);
|
|
1353
|
+
transform.multiplyMatrices(local, instance);
|
|
1354
|
+
} else transform.copy(local);
|
|
1355
|
+
bounds.copy(geometry.boundingBox).applyMatrix4(transform);
|
|
1356
|
+
if (!inside()) continue;
|
|
1357
|
+
if (spatial === void 0) {
|
|
1358
|
+
for (let vertex = start; vertex + 2 < end; vertex += 3) visit(vertex);
|
|
1359
|
+
continue;
|
|
1360
|
+
}
|
|
1361
|
+
query.min.set(-radius, Math.max(minY, bounds.min.y), -radius);
|
|
1362
|
+
query.max.set(radius, Math.min(maxY, bounds.max.y), radius);
|
|
1363
|
+
query.applyMatrix4(toLocal.copy(transform).invert());
|
|
1364
|
+
forEachIndexedTriangle(spatial, query.min.x, query.max.x, query.min.z, query.max.z, (t) => visit(t * 3));
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
this.octree.build();
|
|
1368
|
+
}
|
|
1369
|
+
};
|
|
1370
|
+
//#endregion
|
|
1371
|
+
//#region src/navigation/walk-controller.ts
|
|
1372
|
+
const WALK_EYE_HEIGHT = 1.7;
|
|
1373
|
+
const WALK_SPEED = 1.5;
|
|
1374
|
+
const RUN_SPEED = 4;
|
|
1375
|
+
const BODY_HEIGHT = 1.8;
|
|
1376
|
+
const BODY_RADIUS = .3;
|
|
1377
|
+
/** `place` tries rings of landing spots this far apart, out to this many rings. */
|
|
1378
|
+
const PLACEMENT_RING_SPACING = 2;
|
|
1379
|
+
const PLACEMENT_RINGS = 8;
|
|
1380
|
+
/**
|
|
1381
|
+
* How far from the requested spot `place` may land the body, including its radius: the ground a
|
|
1382
|
+
* host should have loaded (terrain and anything built on it) before calling `place`.
|
|
1383
|
+
*/
|
|
1384
|
+
const WALK_PLACEMENT_RADIUS = 16.3;
|
|
1385
|
+
const STEP = 1 / 120;
|
|
1386
|
+
const GRAVITY = 9.81;
|
|
1387
|
+
const FLOOR_NORMAL = Math.cos(Math.PI / 3.6);
|
|
1388
|
+
/**
|
|
1389
|
+
* First-person walking over streamed geometry: a 1.8 m capsule with gravity, jumping, wall
|
|
1390
|
+
* sliding and a 50° walkable-slope limit, integrated at a fixed 120 Hz so results agree across
|
|
1391
|
+
* frame rates. `feet` is the world position; put the camera `WALK_EYE_HEIGHT` above it. Missing
|
|
1392
|
+
* terrain pauses movement (it is never a bottomless pit) until coverage arrives.
|
|
1393
|
+
*/
|
|
1394
|
+
var WalkController = class {
|
|
1395
|
+
feet = new THREE.Vector3();
|
|
1396
|
+
velocity = new THREE.Vector3();
|
|
1397
|
+
grounded = false;
|
|
1398
|
+
ready = false;
|
|
1399
|
+
waitingForTerrain = false;
|
|
1400
|
+
accumulator = 0;
|
|
1401
|
+
jumpHeld = false;
|
|
1402
|
+
capsule = new Capsule();
|
|
1403
|
+
reset(x, z) {
|
|
1404
|
+
this.feet.set(x, 0, z);
|
|
1405
|
+
this.velocity.set(0, 0, 0);
|
|
1406
|
+
this.grounded = false;
|
|
1407
|
+
this.ready = false;
|
|
1408
|
+
this.waitingForTerrain = true;
|
|
1409
|
+
this.accumulator = 0;
|
|
1410
|
+
this.jumpHeld = false;
|
|
1411
|
+
}
|
|
1412
|
+
/** Find open ground near the fly camera, rather than dropping through a building's roof. */
|
|
1413
|
+
place(collision, sampleHeight) {
|
|
1414
|
+
const centerX = this.feet.x;
|
|
1415
|
+
const centerZ = this.feet.z;
|
|
1416
|
+
for (let ring = 0; ring <= PLACEMENT_RINGS; ring++) {
|
|
1417
|
+
const count = ring === 0 ? 1 : ring * 8;
|
|
1418
|
+
for (let i = 0; i < count; i++) {
|
|
1419
|
+
const x = centerX + Math.cos(i / count * Math.PI * 2) * ring * PLACEMENT_RING_SPACING;
|
|
1420
|
+
const z = centerZ + Math.sin(i / count * Math.PI * 2) * ring * PLACEMENT_RING_SPACING;
|
|
1421
|
+
const ground = sampleHeight(x, z);
|
|
1422
|
+
if (ground === void 0) continue;
|
|
1423
|
+
const ray = new THREE.Ray(new THREE.Vector3(x - collision.origin.x, ground + 1e4, z - collision.origin.z), new THREE.Vector3(0, -1, 0));
|
|
1424
|
+
const hit = collision.octree.rayIntersect(ray);
|
|
1425
|
+
if (!hit || hit.position.y > ground + 3) continue;
|
|
1426
|
+
this.feet.set(x, hit.position.y + .02, z);
|
|
1427
|
+
this.setCapsule(collision);
|
|
1428
|
+
const overlap = collision.octree.capsuleIntersect(this.capsule);
|
|
1429
|
+
if (overlap && (overlap.normal.y < FLOOR_NORMAL || overlap.depth > .15)) continue;
|
|
1430
|
+
this.ready = true;
|
|
1431
|
+
this.waitingForTerrain = false;
|
|
1432
|
+
this.grounded = true;
|
|
1433
|
+
return true;
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1436
|
+
this.feet.set(centerX, 0, centerZ);
|
|
1437
|
+
return false;
|
|
1438
|
+
}
|
|
1439
|
+
update(dt, input, collision, sampleHeight) {
|
|
1440
|
+
if (!this.ready) return;
|
|
1441
|
+
const ground = sampleHeight(this.feet.x, this.feet.z);
|
|
1442
|
+
if (ground !== void 0 && ground > this.feet.y + BODY_RADIUS) {
|
|
1443
|
+
const support = collision.octree.rayIntersect(new THREE.Ray(new THREE.Vector3(this.feet.x - collision.origin.x, ground + 3, this.feet.z - collision.origin.z), new THREE.Vector3(0, -1, 0)));
|
|
1444
|
+
if (support && support.position.y > this.feet.y) {
|
|
1445
|
+
this.feet.y = support.position.y + .02;
|
|
1446
|
+
this.velocity.y = Math.max(0, this.velocity.y);
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
const jump = input.jump && !this.jumpHeld;
|
|
1450
|
+
this.jumpHeld = input.jump;
|
|
1451
|
+
if (jump && this.grounded) {
|
|
1452
|
+
this.velocity.y = 4.5;
|
|
1453
|
+
this.grounded = false;
|
|
1454
|
+
}
|
|
1455
|
+
const length = Math.max(1, Math.hypot(input.forward, input.right));
|
|
1456
|
+
const speed = input.sprint ? 4 : WALK_SPEED;
|
|
1457
|
+
const vx = (Math.cos(input.yaw) * input.forward - Math.sin(input.yaw) * input.right) / length * speed;
|
|
1458
|
+
const vz = (Math.sin(input.yaw) * input.forward + Math.cos(input.yaw) * input.right) / length * speed;
|
|
1459
|
+
this.accumulator += Math.min(.1, Math.max(0, dt));
|
|
1460
|
+
while (this.accumulator >= STEP) {
|
|
1461
|
+
this.accumulator -= STEP;
|
|
1462
|
+
const nextX = this.feet.x + vx * STEP;
|
|
1463
|
+
const nextZ = this.feet.z + vz * STEP;
|
|
1464
|
+
this.waitingForTerrain = sampleHeight(nextX, nextZ) === void 0;
|
|
1465
|
+
if (this.waitingForTerrain) {
|
|
1466
|
+
this.velocity.set(0, 0, 0);
|
|
1467
|
+
this.accumulator = 0;
|
|
1468
|
+
break;
|
|
1469
|
+
}
|
|
1470
|
+
this.velocity.x = vx;
|
|
1471
|
+
this.velocity.z = vz;
|
|
1472
|
+
this.velocity.y = Math.max(-35, this.velocity.y - GRAVITY * STEP);
|
|
1473
|
+
this.feet.addScaledVector(this.velocity, STEP);
|
|
1474
|
+
this.setCapsule(collision);
|
|
1475
|
+
this.grounded = false;
|
|
1476
|
+
for (let pass = 0; pass < 4; pass++) {
|
|
1477
|
+
const hit = collision.octree.capsuleIntersect(this.capsule);
|
|
1478
|
+
if (!hit) break;
|
|
1479
|
+
if (hit.normal.y >= FLOOR_NORMAL) this.grounded = true;
|
|
1480
|
+
if (hit.normal.y > 0 && hit.normal.y < FLOOR_NORMAL) {
|
|
1481
|
+
const horizontal = Math.hypot(hit.normal.x, hit.normal.z);
|
|
1482
|
+
hit.normal.y = 0;
|
|
1483
|
+
hit.normal.divideScalar(horizontal);
|
|
1484
|
+
const intoWall = this.velocity.dot(hit.normal);
|
|
1485
|
+
if (intoWall < 0) this.velocity.addScaledVector(hit.normal, -intoWall);
|
|
1486
|
+
this.capsule.translate(hit.normal.multiplyScalar((hit.depth + 1e-5) / horizontal));
|
|
1487
|
+
} else {
|
|
1488
|
+
const intoSurface = this.velocity.dot(hit.normal);
|
|
1489
|
+
if (intoSurface < 0) this.velocity.addScaledVector(hit.normal, -intoSurface);
|
|
1490
|
+
this.capsule.translate(hit.normal.multiplyScalar(hit.depth + 1e-5));
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
this.feet.copy(this.capsule.start).add(collision.origin);
|
|
1494
|
+
this.feet.y -= BODY_RADIUS;
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
setCapsule(collision) {
|
|
1498
|
+
this.capsule.start.copy(this.feet).sub(collision.origin);
|
|
1499
|
+
this.capsule.start.y += BODY_RADIUS;
|
|
1500
|
+
this.capsule.end.copy(this.feet).sub(collision.origin);
|
|
1501
|
+
this.capsule.end.y += BODY_HEIGHT - BODY_RADIUS;
|
|
1502
|
+
this.capsule.radius = BODY_RADIUS;
|
|
1503
|
+
}
|
|
1504
|
+
};
|
|
1505
|
+
//#endregion
|
|
1506
|
+
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 };
|