@johnmorrisdotca/wakusei-planets 0.47.0 → 0.49.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/NOTICE.md +1 -1
- package/README.md +4 -3
- package/dist/element.d.ts +38 -0
- package/dist/element.js +49 -5
- package/dist/scene/textures.d.ts +2 -2
- package/dist/space/element.d.ts +47 -1
- package/dist/space/element.js +101 -1
- package/dist/space/face.d.ts +132 -0
- package/dist/space/face.js +156 -0
- package/dist/space/index.d.ts +1 -0
- package/dist/space/index.js +1 -0
- package/dist/surface/drive.d.ts +181 -0
- package/dist/surface/drive.js +247 -0
- package/dist/surface/dust.d.ts +46 -0
- package/dist/surface/dust.js +96 -0
- package/dist/surface/element.d.ts +19 -0
- package/dist/surface/element.js +177 -5
- package/dist/surface/glsl.d.ts +3 -0
- package/dist/surface/glsl.js +25 -1
- package/dist/surface/index.d.ts +2 -0
- package/dist/surface/index.js +2 -0
- package/dist/surface/scene.d.ts +7 -0
- package/dist/surface/scene.js +187 -3
- package/dist/surface/shapes.d.ts +2 -2
- package/dist/surface/shapes.js +9 -9
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// The dust a wheel throws: grains in free flight. On the Moon there is no air, so a grain thrown up and back goes in a clean parabola at the Moon's gravity and falls where it falls: no cloud, no drift. The field is
|
|
2
|
+
// a fixed pool of grains, each with a place, a velocity and an age; a step moves them and lets them go when they reach the ground or are old. Made, not measured: how many grains, how fast, how big. No DOM.
|
|
3
|
+
/** The Moon's surface gravity, m/s2. */
|
|
4
|
+
const G = 1.62;
|
|
5
|
+
/**
|
|
6
|
+
* A pool of grains in flight, made with `new DustField(size, seed)`: `size` is how many grains it holds (the oldest is reused when it is full) and `seed` makes the scatter the same for the same drive.
|
|
7
|
+
*
|
|
8
|
+
* @param size - How many grains the pool holds.
|
|
9
|
+
* @param seed - A seed for the scatter.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { DustField } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
14
|
+
*
|
|
15
|
+
* const dust = new DustField(64, 1);
|
|
16
|
+
* dust.emit([0, 0.3, 0], [2, 1.5, 0], 2);
|
|
17
|
+
* dust.step(0.5, () => 0);
|
|
18
|
+
* console.log(dust.live, dust.position[1]!.toFixed(2));
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export class DustField {
|
|
22
|
+
/** Each grain's place, three numbers (east, up, north), and its velocity; a grain not in flight sits far below the ground. */
|
|
23
|
+
position;
|
|
24
|
+
velocity;
|
|
25
|
+
age;
|
|
26
|
+
life;
|
|
27
|
+
#next = 0;
|
|
28
|
+
#seed;
|
|
29
|
+
/** The grains in flight. */
|
|
30
|
+
live = 0;
|
|
31
|
+
/**
|
|
32
|
+
* @param size - How many grains the pool holds; the oldest is reused when it is full.
|
|
33
|
+
* @param seed - A seed for the scatter, so the same drive is the same.
|
|
34
|
+
*/
|
|
35
|
+
constructor(size, seed = 1) {
|
|
36
|
+
this.position = new Float32Array(size * 3).fill(0);
|
|
37
|
+
this.velocity = new Float32Array(size * 3);
|
|
38
|
+
this.age = new Float32Array(size).fill(1e9);
|
|
39
|
+
this.life = new Float32Array(size).fill(0);
|
|
40
|
+
this.#seed = seed >>> 0 || 1;
|
|
41
|
+
for (let i = 0; i < size; i += 1)
|
|
42
|
+
this.position[i * 3 + 1] = -1e5;
|
|
43
|
+
}
|
|
44
|
+
/** A number from 0 to 1 that is the same for the same seed (mulberry32). */
|
|
45
|
+
#random() {
|
|
46
|
+
this.#seed = (this.#seed + 0x6d2b79f5) >>> 0;
|
|
47
|
+
let t = this.#seed;
|
|
48
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
49
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
50
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Throw one grain: from a place, with a velocity, scattered a little, for up to `life` seconds.
|
|
54
|
+
*
|
|
55
|
+
* @param from - East, up, north in metres.
|
|
56
|
+
* @param velocity - East, up, north in metres a second.
|
|
57
|
+
* @param life - The longest it flies, in seconds.
|
|
58
|
+
*/
|
|
59
|
+
emit(from, velocity, life) {
|
|
60
|
+
const i = this.#next;
|
|
61
|
+
this.#next = (this.#next + 1) % this.age.length;
|
|
62
|
+
const scatter = 0.35;
|
|
63
|
+
this.position.set(from, i * 3);
|
|
64
|
+
this.velocity[i * 3] = velocity[0] + (this.#random() - 0.5) * scatter * 2;
|
|
65
|
+
this.velocity[i * 3 + 1] = velocity[1] * (0.7 + 0.6 * this.#random());
|
|
66
|
+
this.velocity[i * 3 + 2] = velocity[2] + (this.#random() - 0.5) * scatter * 2;
|
|
67
|
+
this.age[i] = 0;
|
|
68
|
+
this.life[i] = life * (0.6 + 0.4 * this.#random());
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Move every grain on: its velocity falls at the Moon's gravity and it goes where the velocity takes it; a grain that reaches the ground, or is older than its life, is let go.
|
|
72
|
+
*
|
|
73
|
+
* @param dt - Seconds.
|
|
74
|
+
* @param ground - The ground's height over the site's ground at a place.
|
|
75
|
+
*/
|
|
76
|
+
step(dt, ground) {
|
|
77
|
+
let live = 0;
|
|
78
|
+
for (let i = 0; i < this.age.length; i += 1) {
|
|
79
|
+
if (this.age[i] >= this.life[i])
|
|
80
|
+
continue;
|
|
81
|
+
this.age[i] += dt;
|
|
82
|
+
this.velocity[i * 3 + 1] -= G * dt;
|
|
83
|
+
const [x, y, z] = [this.position[i * 3] + this.velocity[i * 3] * dt, this.position[i * 3 + 1] + this.velocity[i * 3 + 1] * dt, this.position[i * 3 + 2] + this.velocity[i * 3 + 2] * dt];
|
|
84
|
+
if (this.age[i] >= this.life[i] || y < ground(x, z)) {
|
|
85
|
+
this.age[i] = 1e9;
|
|
86
|
+
this.position[i * 3 + 1] = -1e5;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
this.position[i * 3] = x;
|
|
90
|
+
this.position[i * 3 + 1] = y;
|
|
91
|
+
this.position[i * 3 + 2] = z;
|
|
92
|
+
live += 1;
|
|
93
|
+
}
|
|
94
|
+
this.live = live;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { SharedRenderer } from "@johnmorrisdotca/kazoku-ui/renderer";
|
|
2
2
|
import type { ThreeParts } from "../scene/body.ts";
|
|
3
3
|
import { setThreeLoader } from "../scene/shared.ts";
|
|
4
|
+
import { type DriveState } from "./drive.ts";
|
|
4
5
|
import { type HeightLevel, type TerrainManifest } from "./height.ts";
|
|
5
6
|
import { type TerrainProgress } from "./load.ts";
|
|
6
7
|
import { type SurfaceScene } from "./scene.ts";
|
|
@@ -77,6 +78,7 @@ export declare class WakuseiSurface extends HTMLElement {
|
|
|
77
78
|
quality: string;
|
|
78
79
|
scale: number;
|
|
79
80
|
scaled: number;
|
|
81
|
+
vehicle: DriveState | undefined;
|
|
80
82
|
drawMs: number | undefined;
|
|
81
83
|
rocks: number;
|
|
82
84
|
};
|
|
@@ -90,6 +92,23 @@ export declare class WakuseiSurface extends HTMLElement {
|
|
|
90
92
|
* @param aside - Metres to the right.
|
|
91
93
|
*/
|
|
92
94
|
walk(ahead: number, aside?: number): void;
|
|
95
|
+
/** Which view of the rover: `chase` (from behind and above, the default) or `seat` (the commander's). The key C turns from one to the other. */
|
|
96
|
+
get driveCamera(): "chase" | "seat";
|
|
97
|
+
set driveCamera(value: "chase" | "seat");
|
|
98
|
+
/** The rover being driven, or `undefined` when it is not. */
|
|
99
|
+
get vehicle(): DriveState | undefined;
|
|
100
|
+
/**
|
|
101
|
+
* Drive by a set time, whatever the clock says: advance the rover `seconds` with the throttle and steering given (each -1 to 1) and draw. For a test or a sequence of pictures taken one step at a time; the keys and the
|
|
102
|
+
* stick do the same by the real time between frames.
|
|
103
|
+
*
|
|
104
|
+
* @param seconds - How long to drive.
|
|
105
|
+
* @param input - The throttle and the steering.
|
|
106
|
+
* @returns Whether the element is driving.
|
|
107
|
+
*/
|
|
108
|
+
driveFor(seconds: number, input?: {
|
|
109
|
+
throttle: number;
|
|
110
|
+
steer: number;
|
|
111
|
+
}): boolean;
|
|
93
112
|
/** The things at the site, at their published places (none where the site has none or `objects="off"`). */
|
|
94
113
|
get objects(): readonly SiteObject[];
|
|
95
114
|
/**
|
package/dist/surface/element.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { createGate, frameCap } from "../gate.js";
|
|
2
2
|
import { setThreeLoader, takeRenderer } from "../scene/shared.js";
|
|
3
3
|
import { TILES_BASE } from "../scene/textures.js";
|
|
4
|
-
import { madeGround, setKeepOut, standingPlace } from "./detail.js";
|
|
4
|
+
import { madeGround, rocksAround, setKeepOut, standingPlace } from "./detail.js";
|
|
5
|
+
import { driveStep, driveView, ROVER, startDrive, toWorld } from "./drive.js";
|
|
6
|
+
import { DustField } from "./dust.js";
|
|
5
7
|
import { drop, groundAt } from "./height.js";
|
|
6
8
|
import { loadTerrain } from "./load.js";
|
|
7
9
|
import { createSurfaceItem } from "./scene.js";
|
|
@@ -53,7 +55,7 @@ const num = (value, fallback) => {
|
|
|
53
55
|
* ```
|
|
54
56
|
*/
|
|
55
57
|
export class WakuseiSurface extends HTMLElement {
|
|
56
|
-
static observedAttributes = ["quality", "shadows", "detail", "exposure", "site", "time", "heading", "pitch", "fov", "tiles", "lang", "body", "standby", "scale", "stick", "objects"];
|
|
58
|
+
static observedAttributes = ["quality", "shadows", "detail", "exposure", "site", "time", "heading", "pitch", "fov", "tiles", "lang", "body", "standby", "scale", "stick", "objects", "drive", "drive-camera"];
|
|
57
59
|
#root;
|
|
58
60
|
#stage;
|
|
59
61
|
#noteEl;
|
|
@@ -99,6 +101,15 @@ export class WakuseiSurface extends HTMLElement {
|
|
|
99
101
|
#objects = [];
|
|
100
102
|
#labelEls = new Map();
|
|
101
103
|
#chosen;
|
|
104
|
+
/** The rover being driven (`drive="on"`), the view of it, the driver's own turn of the view, the path left, the dust thrown, and the places it cannot go. */
|
|
105
|
+
#vehicle;
|
|
106
|
+
#driveCamera = "chase";
|
|
107
|
+
#camOffset = { azimuth: 0, pitch: 0 };
|
|
108
|
+
#driveEyeUp = 0;
|
|
109
|
+
#trail = [];
|
|
110
|
+
#dust = new DustField(900, 17);
|
|
111
|
+
#blockers = [];
|
|
112
|
+
#blockersAt;
|
|
102
113
|
#stickPointer = -1;
|
|
103
114
|
#pointers = new Map();
|
|
104
115
|
#pinch = 0;
|
|
@@ -150,7 +161,7 @@ export class WakuseiSurface extends HTMLElement {
|
|
|
150
161
|
/** Numbers for a test or a status line: the draws, the tiles, the renderer's own counts, the mesh, the quality of the last draw and the milliseconds of the last measured one. */
|
|
151
162
|
get stats() {
|
|
152
163
|
const scene = this.#view?.scene;
|
|
153
|
-
return { state: this.#state, draws: this.#draws, tiles: this.#progress, renderer: this.#renderer?.stats, mesh: scene?.mesh, quality: this.#quality(), scale: this.#drawScale, scaled: this.#scaled, drawMs: scene?.drawMs ?? this.#drawMs, rocks: scene?.rocks ?? 0 };
|
|
164
|
+
return { state: this.#state, draws: this.#draws, tiles: this.#progress, renderer: this.#renderer?.stats, mesh: scene?.mesh, quality: this.#quality(), scale: this.#drawScale, scaled: this.#scaled, vehicle: this.#vehicle === undefined ? undefined : { ...this.#vehicle, spin: [...this.#vehicle.spin] }, drawMs: scene?.drawMs ?? this.#drawMs, rocks: scene?.rocks ?? 0 };
|
|
154
165
|
}
|
|
155
166
|
/** The quality of the next draw: the `quality` attribute, or in `auto` the quality of motion while the view moves and the machine's own quality of rest otherwise. */
|
|
156
167
|
#quality() {
|
|
@@ -219,6 +230,14 @@ export class WakuseiSurface extends HTMLElement {
|
|
|
219
230
|
attributeChangedCallback(name) {
|
|
220
231
|
if (!this.isConnected)
|
|
221
232
|
return;
|
|
233
|
+
if (name === "drive") {
|
|
234
|
+
this.#syncDrive();
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (name === "drive-camera") {
|
|
238
|
+
this.driveCamera = this.getAttribute("drive-camera") === "seat" ? "seat" : "chase";
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
222
241
|
if (name === "stick") {
|
|
223
242
|
this.#showStick();
|
|
224
243
|
return;
|
|
@@ -305,6 +324,7 @@ export class WakuseiSurface extends HTMLElement {
|
|
|
305
324
|
const stand = this.getAttribute("detail") === "off" ? { x: 0, y: 0 } : standingPlace(0, 0);
|
|
306
325
|
this.#at = { east: stand.x, north: stand.y };
|
|
307
326
|
this.#placed = true;
|
|
327
|
+
this.#syncDrive();
|
|
308
328
|
}
|
|
309
329
|
if (this.#view === undefined) {
|
|
310
330
|
if (this.#renderer === undefined) {
|
|
@@ -392,6 +412,8 @@ export class WakuseiSurface extends HTMLElement {
|
|
|
392
412
|
}
|
|
393
413
|
/** The eyes' height over the site's origin: the ground under them (the made craters too: the feet follow them) and a person's height. */
|
|
394
414
|
#eyeUp() {
|
|
415
|
+
if (this.#vehicle !== undefined)
|
|
416
|
+
return this.#driveEyeUp;
|
|
395
417
|
return this.#groundAt(this.#at.east, this.#at.north) + EYE_HEIGHT;
|
|
396
418
|
}
|
|
397
419
|
/** Ask for a draw. A change of the view (`moving`, the default) is drawn at the quality of motion and followed, a moment after the last, by one at the quality of rest. */
|
|
@@ -446,11 +468,147 @@ export class WakuseiSurface extends HTMLElement {
|
|
|
446
468
|
this.#measured += 1;
|
|
447
469
|
if (this.#drawScale < 1)
|
|
448
470
|
this.#scaled += 1;
|
|
449
|
-
scene.apply({ eye: { east: this.#at.east, north: this.#at.north, up: this.#eyeUp() }, look: this.#look, sky: this.#sky, shadows: this.getAttribute("shadows") !== "off", detail: this.getAttribute("detail") !== "off", exposure: num(this.getAttribute("exposure"), 1.6), quality: this.#quality(), scale: this.#drawScale, objects: this.#objects.length > 0, measure: measure || this.getAttribute("measure") !== null });
|
|
471
|
+
scene.apply({ eye: { east: this.#at.east, north: this.#at.north, up: this.#eyeUp() }, look: this.#look, sky: this.#sky, shadows: this.getAttribute("shadows") !== "off", detail: this.getAttribute("detail") !== "off", exposure: num(this.getAttribute("exposure"), 1.6), quality: this.#quality(), scale: this.#drawScale, objects: this.#objects.length > 0, ...(this.#vehicle === undefined ? {} : { vehicle: this.#vehicle, trail: this.#trail, dust: this.#dust.position }), measure: measure || this.getAttribute("measure") !== null });
|
|
450
472
|
this.#view.invalidate();
|
|
451
473
|
this.#draws += 1;
|
|
452
474
|
this.#placeLabels();
|
|
453
475
|
}
|
|
476
|
+
// --- driving the rover (`drive="on"`) ---------------------------------------------------------------------------------------------------------------
|
|
477
|
+
/** Which view of the rover: `chase` (from behind and above, the default) or `seat` (the commander's). The key C turns from one to the other. */
|
|
478
|
+
get driveCamera() {
|
|
479
|
+
return this.#driveCamera;
|
|
480
|
+
}
|
|
481
|
+
set driveCamera(value) {
|
|
482
|
+
this.#driveCamera = value === "seat" ? "seat" : "chase";
|
|
483
|
+
this.#syncDriveCamera();
|
|
484
|
+
this.#changed();
|
|
485
|
+
}
|
|
486
|
+
/** The rover being driven, or `undefined` when it is not. */
|
|
487
|
+
get vehicle() {
|
|
488
|
+
return this.#vehicle === undefined ? undefined : { ...this.#vehicle, spin: [...this.#vehicle.spin] };
|
|
489
|
+
}
|
|
490
|
+
/** Start or stop driving as the `drive` attribute says: the rover starts at its published place (where its last traverse ends), looking along its heading from behind; stopping puts the eyes back on the ground there. */
|
|
491
|
+
#syncDrive() {
|
|
492
|
+
const on = this.getAttribute("drive") === "on" && this.#terrain !== undefined && this.#placed;
|
|
493
|
+
if (!on) {
|
|
494
|
+
if (this.#vehicle === undefined)
|
|
495
|
+
return;
|
|
496
|
+
const v = this.#vehicle;
|
|
497
|
+
this.#vehicle = undefined;
|
|
498
|
+
this.#trail = [];
|
|
499
|
+
this.#at = { east: v.east, north: v.north };
|
|
500
|
+
this.#changed();
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
if (this.#vehicle !== undefined)
|
|
504
|
+
return;
|
|
505
|
+
const rover = this.#objects.find((o) => o.kind === "rover");
|
|
506
|
+
const [east, north, heading] = rover === undefined ? [this.#at.east, this.#at.north, this.#look.azimuth] : [rover.east, rover.north, rover.heading ?? 0];
|
|
507
|
+
this.#driveCamera = this.getAttribute("drive-camera") === "seat" ? "seat" : "chase";
|
|
508
|
+
this.#vehicle = startDrive(east, north, heading, this.#groundAt(east, north));
|
|
509
|
+
this.#trail = [east, north];
|
|
510
|
+
this.#dust = new DustField(900, 17);
|
|
511
|
+
this.#camOffset = { azimuth: 0, pitch: 0 };
|
|
512
|
+
this.#blockersAt = undefined;
|
|
513
|
+
for (const el of this.#labelEls.values())
|
|
514
|
+
el.hidden = true;
|
|
515
|
+
this.choose(undefined);
|
|
516
|
+
this.#syncDriveCamera();
|
|
517
|
+
this.#changed();
|
|
518
|
+
}
|
|
519
|
+
/** The eyes where the view of the rover puts them. */
|
|
520
|
+
#syncDriveCamera() {
|
|
521
|
+
const v = this.#vehicle;
|
|
522
|
+
if (v === undefined)
|
|
523
|
+
return;
|
|
524
|
+
const view = driveView(v, this.#driveCamera, this.#camOffset, (e, n) => this.#groundAt(e, n));
|
|
525
|
+
this.#at = { east: view.eye.east, north: view.eye.north };
|
|
526
|
+
this.#driveEyeUp = view.eye.up;
|
|
527
|
+
this.#look = clampLook({ azimuth: view.azimuth, pitch: view.pitch, fov: this.#look.fov });
|
|
528
|
+
}
|
|
529
|
+
/** The big rocks and the things near the rover, as circles it cannot enter (found again when it has gone 8 m from where they were found). */
|
|
530
|
+
#obstacles() {
|
|
531
|
+
const v = this.#vehicle;
|
|
532
|
+
if (this.#blockersAt === undefined || Math.hypot(v.east - this.#blockersAt.east, v.north - this.#blockersAt.north) > 8) {
|
|
533
|
+
this.#blockersAt = { east: v.east, north: v.north };
|
|
534
|
+
const list = [];
|
|
535
|
+
if (this.getAttribute("detail") !== "off")
|
|
536
|
+
for (const rock of rocksAround(v.east, v.north, 28, 0.0024))
|
|
537
|
+
if (rock.size >= 0.6)
|
|
538
|
+
list.push({ x: rock.x, y: rock.y, r: rock.size * 0.5 + 1.1 });
|
|
539
|
+
const near = { lander: 6, flag: 1.6, station: 2.8, instrument: 1.8, rover: 0 };
|
|
540
|
+
for (const o of this.#objects)
|
|
541
|
+
if (near[o.kind] > 0)
|
|
542
|
+
list.push({ x: o.east, y: o.north, r: near[o.kind] });
|
|
543
|
+
this.#blockers = list;
|
|
544
|
+
}
|
|
545
|
+
return this.#blockers;
|
|
546
|
+
}
|
|
547
|
+
/** Advance the rover by `seconds` (in steps of at most a sixtieth), throwing dust from the wheels that are down and leaving tracks. */
|
|
548
|
+
#advance(seconds, input) {
|
|
549
|
+
let v = this.#vehicle;
|
|
550
|
+
const ground = (e, n) => this.#groundAt(e, n);
|
|
551
|
+
const blockers = this.#obstacles();
|
|
552
|
+
const blocked = (e, n) => blockers.some((b) => (e - b.x) ** 2 + (n - b.y) ** 2 < b.r * b.r);
|
|
553
|
+
const steps = Math.max(1, Math.ceil(seconds * 60));
|
|
554
|
+
const dt = seconds / steps;
|
|
555
|
+
for (let i = 0; i < steps; i += 1) {
|
|
556
|
+
v = driveStep(v, input, dt, ground, blocked);
|
|
557
|
+
// each wheel on the ground throws grains up and back: the faster, the more
|
|
558
|
+
if (!v.airborne && Math.abs(v.speed) > 0.6) {
|
|
559
|
+
const h = (v.heading * Math.PI) / 180;
|
|
560
|
+
const [fx, fz] = [Math.sin(h), Math.cos(h)];
|
|
561
|
+
const grains = Math.abs(v.speed) * dt * 26;
|
|
562
|
+
const [rx, rz] = [Math.cos(h), -Math.sin(h)];
|
|
563
|
+
for (const [wx, , wz] of ROVER.wheels) {
|
|
564
|
+
const w = toWorld(v, [wx, 0, wz]);
|
|
565
|
+
for (let k = 0; k < grains + Math.random() * 0.999; k += 1) {
|
|
566
|
+
// thrown up and back from the tyre in a fan: the faster the rover, the wider and farther
|
|
567
|
+
const back = -Math.sign(v.speed) * Math.abs(v.speed) * (0.3 + 0.3 * Math.random());
|
|
568
|
+
const side = (Math.random() - 0.5) * 1.6 * Math.min(1, Math.abs(v.speed));
|
|
569
|
+
this.#dust.emit([w[0], w[1] + 0.05, w[2]], [fx * back + rx * side, 0.5 + 0.18 * Math.abs(v.speed), fz * back + rz * side], 2);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
this.#dust.step(dt, ground);
|
|
574
|
+
}
|
|
575
|
+
this.#vehicle = v;
|
|
576
|
+
// the tracks: a point every 1.2 m
|
|
577
|
+
const last = this.#trail.length >= 2 ? [this.#trail[this.#trail.length - 2], this.#trail[this.#trail.length - 1]] : [Infinity, Infinity];
|
|
578
|
+
if (Math.hypot(v.east - last[0], v.north - last[1]) >= 1.2 && this.#trail.length < 12_000)
|
|
579
|
+
this.#trail.push(v.east, v.north);
|
|
580
|
+
this.#syncDriveCamera();
|
|
581
|
+
}
|
|
582
|
+
/** While a key or the stick is asked for, and while the rover is still rolling, settling on its springs or throwing dust: a step at most thirty times a second, and nothing once it is at rest. */
|
|
583
|
+
#driveTick() {
|
|
584
|
+
const k = this.#keys;
|
|
585
|
+
const now = performance.now();
|
|
586
|
+
const dt = Math.min(0.1, (now - this.#walkAt) / 1000);
|
|
587
|
+
this.#walkAt = now;
|
|
588
|
+
const throttle = Math.max(-1, Math.min(1, (k.has("w") || k.has("ArrowUp") ? 1 : 0) - (k.has("s") || k.has("ArrowDown") ? 1 : 0) + this.#push.y));
|
|
589
|
+
const steer = Math.max(-1, Math.min(1, (k.has("d") || k.has("ArrowRight") ? 1 : 0) - (k.has("a") || k.has("ArrowLeft") ? 1 : 0) + this.#push.x));
|
|
590
|
+
this.#advance(dt, { throttle, steer });
|
|
591
|
+
this.#changed();
|
|
592
|
+
const v = this.#vehicle;
|
|
593
|
+
const rolling = Math.abs(v.speed) > 0.01 || v.airborne || Math.abs(v.vy) > 0.02 || Math.abs(v.steer) > 0.005 || this.#dust.live > 0;
|
|
594
|
+
if (throttle !== 0 || steer !== 0 || rolling)
|
|
595
|
+
this.#walkTimer = setTimeout(this.#walkTick, 33);
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* Drive by a set time, whatever the clock says: advance the rover `seconds` with the throttle and steering given (each -1 to 1) and draw. For a test or a sequence of pictures taken one step at a time; the keys and the
|
|
599
|
+
* stick do the same by the real time between frames.
|
|
600
|
+
*
|
|
601
|
+
* @param seconds - How long to drive.
|
|
602
|
+
* @param input - The throttle and the steering.
|
|
603
|
+
* @returns Whether the element is driving.
|
|
604
|
+
*/
|
|
605
|
+
driveFor(seconds, input = { throttle: 0, steer: 0 }) {
|
|
606
|
+
if (this.#vehicle === undefined)
|
|
607
|
+
return false;
|
|
608
|
+
this.#advance(seconds, input);
|
|
609
|
+
this.#changed();
|
|
610
|
+
return true;
|
|
611
|
+
}
|
|
454
612
|
// --- the things at the site: a small button on each, which goes to it and says what it is ----------------------------------------------------------------------------------------------------
|
|
455
613
|
/** The things at the site, at their published places (none where the site has none or `objects="off"`). */
|
|
456
614
|
get objects() {
|
|
@@ -489,7 +647,7 @@ export class WakuseiSurface extends HTMLElement {
|
|
|
489
647
|
}
|
|
490
648
|
/** Put each button on its thing in the picture, or hide it behind the viewer, off the picture or too far to tell. Done at each draw: nothing moves by itself. */
|
|
491
649
|
#placeLabels() {
|
|
492
|
-
if (this.#labelEls.size === 0)
|
|
650
|
+
if (this.#labelEls.size === 0 || this.#vehicle !== undefined)
|
|
493
651
|
return;
|
|
494
652
|
const box = this.getBoundingClientRect();
|
|
495
653
|
const [w, h] = [box.width, box.height];
|
|
@@ -516,6 +674,8 @@ export class WakuseiSurface extends HTMLElement {
|
|
|
516
674
|
* @returns Whether there is such a thing here.
|
|
517
675
|
*/
|
|
518
676
|
choose(id) {
|
|
677
|
+
if (this.#vehicle !== undefined)
|
|
678
|
+
return false; // the rover is being driven: the eyes are its
|
|
519
679
|
const about = this.#root.getElementById("about");
|
|
520
680
|
const o = id === undefined ? undefined : this.#objects.find((x) => x.id === id);
|
|
521
681
|
if (id !== undefined && o === undefined)
|
|
@@ -616,6 +776,12 @@ export class WakuseiSurface extends HTMLElement {
|
|
|
616
776
|
}
|
|
617
777
|
const box = this.getBoundingClientRect();
|
|
618
778
|
const perPixel = this.#look.fov / Math.max(1, box.height);
|
|
779
|
+
if (this.#vehicle !== undefined) {
|
|
780
|
+
this.#camOffset = { azimuth: this.#camOffset.azimuth - (event.clientX - was.x) * perPixel, pitch: Math.max(-60, Math.min(60, this.#camOffset.pitch + (event.clientY - was.y) * perPixel)) };
|
|
781
|
+
this.#syncDriveCamera();
|
|
782
|
+
this.#changed();
|
|
783
|
+
return;
|
|
784
|
+
}
|
|
619
785
|
this.#look = clampLook({ ...this.#look, azimuth: this.#look.azimuth - (event.clientX - was.x) * perPixel, pitch: this.#look.pitch + (event.clientY - was.y) * perPixel });
|
|
620
786
|
this.#changed();
|
|
621
787
|
};
|
|
@@ -641,6 +807,10 @@ export class WakuseiSurface extends HTMLElement {
|
|
|
641
807
|
};
|
|
642
808
|
#onKeyDown = (event) => {
|
|
643
809
|
const key = event.key.length === 1 ? event.key.toLowerCase() : event.key;
|
|
810
|
+
if (key === "c" && this.#vehicle !== undefined) {
|
|
811
|
+
this.driveCamera = this.#driveCamera === "chase" ? "seat" : "chase";
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
644
814
|
if (key === "+" || key === "=")
|
|
645
815
|
return this.#zoom(0.85);
|
|
646
816
|
if (key === "-" || key === "_")
|
|
@@ -663,6 +833,8 @@ export class WakuseiSurface extends HTMLElement {
|
|
|
663
833
|
/** While a key is held: a step every frame at a walking pace (and a turn at a quarter turn a second), at most thirty a second; nothing when no key is held. */
|
|
664
834
|
#walkTick = () => {
|
|
665
835
|
clearTimeout(this.#walkTimer);
|
|
836
|
+
if (this.#vehicle !== undefined)
|
|
837
|
+
return this.#driveTick();
|
|
666
838
|
if (this.#keys.size === 0 && this.#push.x === 0 && this.#push.y === 0)
|
|
667
839
|
return;
|
|
668
840
|
const now = performance.now();
|
package/dist/surface/glsl.d.ts
CHANGED
|
@@ -24,3 +24,6 @@ export declare const TRACK_FRAGMENT = "\nprecision highp float;\nvarying vec2 vU
|
|
|
24
24
|
/** The things at a site: the rocks' light, with a flat colour for each corner (the gold of the lunar module's foil, the grey of the rover's frame). */
|
|
25
25
|
export declare const OBJECT_VERTEX: string;
|
|
26
26
|
export declare const OBJECT_FRAGMENT: string;
|
|
27
|
+
/** A grain of dust in flight: a small square that always faces the viewer, at a place in the site's plane, its size in metres. */
|
|
28
|
+
export declare const DUST_VERTEX = "\nprecision highp float;\n// position (declared by three.js) is the grain's place; the corner of its square is in uv, -1 to 1\nuniform mat4 uVP;\nuniform vec3 uRight;\nuniform vec3 uUp;\nuniform float uSize;\nvarying vec2 vUv;\nvoid main() {\n vUv = uv * 2.0 - 1.0;\n vec3 w = vec3(position.x, position.y, -position.z) + (uRight * vUv.x + uUp * vUv.y) * uSize;\n gl_Position = uVP * vec4(w, 1.0);\n}\n";
|
|
29
|
+
export declare const DUST_FRAGMENT = "\nprecision highp float;\nvarying vec2 vUv;\nvoid main() {\n float r = length(vUv);\n if (r > 1.0) discard;\n gl_FragColor = vec4(vec3(0.6), 0.55 * (1.0 - r * r));\n}\n";
|
package/dist/surface/glsl.js
CHANGED
|
@@ -454,4 +454,28 @@ void main() {
|
|
|
454
454
|
`;
|
|
455
455
|
/** The things at a site: the rocks' light, with a flat colour for each corner (the gold of the lunar module's foil, the grey of the rover's frame). */
|
|
456
456
|
export const OBJECT_VERTEX = ROCK_VERTEX.replace("attribute float tone;", "attribute float tone;\nattribute vec3 tint;").replace("varying float vTone;", "varying float vTone;\nvarying vec3 vTint;").replace("vTone = tone;", "vTone = tone;\n vTint = tint;");
|
|
457
|
-
export const OBJECT_FRAGMENT = ROCK_FRAGMENT.replace("varying float vTone;", "varying float vTone;\nvarying vec3 vTint;").replace("gl_FragColor = vec4(vec3(pow(clamp(light, 0.0, 1.0), 1.0 / 2.2)), 1.0);", "gl_FragColor = vec4(pow(clamp(vec3(light) * vTint, 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);");
|
|
457
|
+
export const OBJECT_FRAGMENT = ROCK_FRAGMENT.replace("varying float vTone;", "varying float vTone;\nvarying vec3 vTint;").replace("uAlbedo * 1.1 * vTone * (shine + 0.12 * (n.y * 0.5 + 0.5))", "uAlbedo * 2.4 * vTone * (shine + 0.3 * (n.y * 0.5 + 0.5))").replace("gl_FragColor = vec4(vec3(pow(clamp(light, 0.0, 1.0), 1.0 / 2.2)), 1.0);", "gl_FragColor = vec4(pow(clamp(vec3(light) * vTint, 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);");
|
|
458
|
+
/** A grain of dust in flight: a small square that always faces the viewer, at a place in the site's plane, its size in metres. */
|
|
459
|
+
export const DUST_VERTEX = /* glsl */ `
|
|
460
|
+
precision highp float;
|
|
461
|
+
// position (declared by three.js) is the grain's place; the corner of its square is in uv, -1 to 1
|
|
462
|
+
uniform mat4 uVP;
|
|
463
|
+
uniform vec3 uRight;
|
|
464
|
+
uniform vec3 uUp;
|
|
465
|
+
uniform float uSize;
|
|
466
|
+
varying vec2 vUv;
|
|
467
|
+
void main() {
|
|
468
|
+
vUv = uv * 2.0 - 1.0;
|
|
469
|
+
vec3 w = vec3(position.x, position.y, -position.z) + (uRight * vUv.x + uUp * vUv.y) * uSize;
|
|
470
|
+
gl_Position = uVP * vec4(w, 1.0);
|
|
471
|
+
}
|
|
472
|
+
`;
|
|
473
|
+
export const DUST_FRAGMENT = /* glsl */ `
|
|
474
|
+
precision highp float;
|
|
475
|
+
varying vec2 vUv;
|
|
476
|
+
void main() {
|
|
477
|
+
float r = length(vUv);
|
|
478
|
+
if (r > 1.0) discard;
|
|
479
|
+
gl_FragColor = vec4(vec3(0.6), 0.55 * (1.0 - r * r));
|
|
480
|
+
}
|
|
481
|
+
`;
|
package/dist/surface/index.d.ts
CHANGED
|
@@ -5,6 +5,8 @@ export { decodeHeight, drop, emptyLevel, fillFromCoarser, groundAt, sampleLevel,
|
|
|
5
5
|
export { loadPlan, loadTerrain, type LoadJob, type LoadOptions, type Terrain, type TerrainProgress } from "./load.ts";
|
|
6
6
|
export type { SurfaceState } from "./element.ts";
|
|
7
7
|
export { landerParts, roverParts } from "./shapes.ts";
|
|
8
|
+
export { DustField } from "./dust.ts";
|
|
9
|
+
export { driveParts, driveStep, driveView, startDrive, ROVER, type DriveInput, type DriveState, type DriveView } from "./drive.ts";
|
|
8
10
|
export { keepAway, objectParts, objectsOf, partShadows, traverseOf, KEEP_OUT, type Part, type SiteObject } from "./objects.ts";
|
|
9
11
|
export { craterIn, craterProfile, madeGround, setKeepOut, rimBoost, rocksAround, standingPlace, type Crater, type Rock } from "./detail.ts";
|
|
10
12
|
export { rockMesh, type RockMesh } from "./rocks.ts";
|
package/dist/surface/index.js
CHANGED
|
@@ -5,6 +5,8 @@ export { EYE_HEIGHT, FOV_LIMITS, WALK_SPEED, clampLook, forward, stepOver, viewP
|
|
|
5
5
|
export { decodeHeight, drop, emptyLevel, fillFromCoarser, groundAt, sampleLevel, tileAddress } from "./height.js";
|
|
6
6
|
export { loadPlan, loadTerrain } from "./load.js";
|
|
7
7
|
export { landerParts, roverParts } from "./shapes.js";
|
|
8
|
+
export { DustField } from "./dust.js";
|
|
9
|
+
export { driveParts, driveStep, driveView, startDrive, ROVER } from "./drive.js";
|
|
8
10
|
export { keepAway, objectParts, objectsOf, partShadows, traverseOf, KEEP_OUT } from "./objects.js";
|
|
9
11
|
export { craterIn, craterProfile, madeGround, setKeepOut, rimBoost, rocksAround, standingPlace } from "./detail.js";
|
|
10
12
|
export { rockMesh } from "./rocks.js";
|
package/dist/surface/scene.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Item3D, Scene3D } from "@johnmorrisdotca/kazoku-ui/renderer";
|
|
2
2
|
import type { ThreeParts } from "../scene/body.ts";
|
|
3
|
+
import { type DriveState } from "./drive.ts";
|
|
3
4
|
import { type SiteObject } from "./objects.ts";
|
|
4
5
|
import type { HeightLevel, TerrainManifest } from "./height.ts";
|
|
5
6
|
import type { MoonSky } from "./sky.ts";
|
|
@@ -26,6 +27,12 @@ export interface SurfaceState {
|
|
|
26
27
|
quality?: "full" | "medium" | "fast";
|
|
27
28
|
/** The share of the canvas's width and height the ground is drawn at, 1 (the default) to 0.25: below 1 it is drawn to a smaller target and stretched, which is what a draw while the view moves does on a machine that is slow. It needs `WebGLRenderTarget` in the three.js module (a page's own cut-down module may not have it; the draw is then at full size). */
|
|
28
29
|
scale?: number;
|
|
30
|
+
/** The rover being driven, as `driveStep` leaves it: drawn in place of the one standing at its published place. */
|
|
31
|
+
vehicle?: DriveState;
|
|
32
|
+
/** The path the driven rover has left, east, north, east, north, ... in metres: drawn as tracks. */
|
|
33
|
+
trail?: readonly number[];
|
|
34
|
+
/** The grains of dust in flight, east, up, north for each (`DustField.position`), drawn as small squares. */
|
|
35
|
+
dust?: Float32Array;
|
|
29
36
|
/** Whether the things at the site (the lander, the rover, the flag, the station) and the rover's tracks are drawn (default true; the made ground keeps away from them while they are). */
|
|
30
37
|
objects?: boolean;
|
|
31
38
|
/** Time the next draw to the end of the graphics queue (it waits for the card, so it is for a few draws, not every one). */
|