@johnmorrisdotca/wakusei-planets 0.45.1 → 0.46.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 +7 -0
- package/NOTICE.md +11 -0
- package/README.md +4 -3
- package/dist/scene/textures.d.ts +2 -2
- package/dist/space/element.d.ts +7 -0
- package/dist/space/element.js +12 -0
- package/dist/surface/detail.d.ts +169 -0
- package/dist/surface/detail.js +292 -0
- package/dist/surface/element.d.ts +90 -0
- package/dist/surface/element.js +482 -0
- package/dist/surface/glsl.d.ts +20 -0
- package/dist/surface/glsl.js +403 -0
- package/dist/surface/height.d.ts +257 -0
- package/dist/surface/height.js +232 -0
- package/dist/surface/index.d.ts +8 -0
- package/dist/surface/index.js +8 -0
- package/dist/surface/land.d.ts +111 -0
- package/dist/surface/land.js +261 -0
- package/dist/surface/load.d.ts +133 -0
- package/dist/surface/load.js +183 -0
- package/dist/surface/rocks.d.ts +45 -0
- package/dist/surface/rocks.js +166 -0
- package/dist/surface/scene.d.ts +64 -0
- package/dist/surface/scene.js +323 -0
- package/dist/surface/site.d.ts +58 -0
- package/dist/surface/site.js +38 -0
- package/dist/surface/sky.d.ts +81 -0
- package/dist/surface/sky.js +60 -0
- package/dist/surface/view.d.ts +129 -0
- package/dist/surface/view.js +139 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +23 -1
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { SharedRenderer } from "@johnmorrisdotca/kazoku-ui/renderer";
|
|
2
|
+
import type { ThreeParts } from "../scene/body.ts";
|
|
3
|
+
import { setThreeLoader } from "../scene/shared.ts";
|
|
4
|
+
import { type HeightLevel, type TerrainManifest } from "./height.ts";
|
|
5
|
+
import { type TerrainProgress } from "./load.ts";
|
|
6
|
+
import { type SurfaceScene } from "./scene.ts";
|
|
7
|
+
import { type MoonSky } from "./sky.ts";
|
|
8
|
+
import { type Look } from "./view.ts";
|
|
9
|
+
export { setThreeLoader };
|
|
10
|
+
/**
|
|
11
|
+
* What the element is doing: `loading` (the heights are on their way), `live` (drawn), `lost` (the page's graphics context was lost and will come back), `unavailable` (no WebGL, or no heights for
|
|
12
|
+
* the site: nothing can be drawn) or `idle` (not yet asked to).
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts no-run
|
|
16
|
+
* import type { SurfaceState } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
17
|
+
*
|
|
18
|
+
* const ready = (state: SurfaceState): boolean => state === "live";
|
|
19
|
+
* console.log(ready("loading"));
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export type SurfaceState = "idle" | "loading" | "live" | "lost" | "unavailable";
|
|
23
|
+
/**
|
|
24
|
+
* The `<wakusei-surface>` custom element: standing on a body.
|
|
25
|
+
*
|
|
26
|
+
* Attributes: `body` (`moon`), `site` (a name from `SURFACE_SITES`, `apollo-17`), `time` (an ISO date and time, or milliseconds since 1970; the site's own landing moment when left out; `now` for the real time),
|
|
27
|
+
* `heading` (degrees clockwise from north to look toward), `pitch` (degrees up), `fov` (the view's height in degrees: zoom), `tiles` (the folder the height tiles are served from; the demo site's `tiles/` by default),
|
|
28
|
+
* `lang` (`en` or `ja`: the words on the picture).
|
|
29
|
+
*
|
|
30
|
+
* A drag turns the view; the keys W, A, S, D and the arrow keys walk (1.4 metres a second, over the real ground: it rises and falls under the eyes), Q and E turn, + and - zoom, and Ctrl with the wheel or a
|
|
31
|
+
* pinch zooms. Nothing is drawn at rest.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts no-run
|
|
35
|
+
* import type { WakuseiSurface } from "@johnmorrisdotca/wakusei-planets/surface/element";
|
|
36
|
+
*
|
|
37
|
+
* const view = document.createElement("wakusei-surface") as WakuseiSurface;
|
|
38
|
+
* view.setAttribute("site", "apollo-17");
|
|
39
|
+
* document.body.append(view);
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare class WakuseiSurface extends HTMLElement {
|
|
43
|
+
#private;
|
|
44
|
+
static observedAttributes: string[];
|
|
45
|
+
/** Whether the first view of the ground is in (the heights are fetched and the view could draw at once): true while on `standby` too, which is how a page makes ready without taking the renderer. */
|
|
46
|
+
get loaded(): boolean;
|
|
47
|
+
/** The state now. */
|
|
48
|
+
get state(): SurfaceState;
|
|
49
|
+
/** The eyes' look now. Setting it turns the view (azimuth and pitch in degrees, `fov` the view's height in degrees); it is drawn at once. */
|
|
50
|
+
get look(): Look;
|
|
51
|
+
set look(value: Look);
|
|
52
|
+
/** Where the eyes are: metres east and north of the site, and metres over the ground under them. */
|
|
53
|
+
get position(): {
|
|
54
|
+
east: number;
|
|
55
|
+
north: number;
|
|
56
|
+
up: number;
|
|
57
|
+
};
|
|
58
|
+
/** The sky now: the Sun and the Earth in it. */
|
|
59
|
+
get sky(): MoonSky | undefined;
|
|
60
|
+
/** The moment shown, milliseconds since 1970. */
|
|
61
|
+
get time(): number;
|
|
62
|
+
set time(value: number);
|
|
63
|
+
/** The height model as loaded, or `undefined` before: for a status line and a test. */
|
|
64
|
+
get terrain(): {
|
|
65
|
+
manifest: TerrainManifest;
|
|
66
|
+
levels: readonly HeightLevel[];
|
|
67
|
+
bytes: number;
|
|
68
|
+
} | undefined;
|
|
69
|
+
/** 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. */
|
|
70
|
+
get stats(): {
|
|
71
|
+
state: SurfaceState;
|
|
72
|
+
draws: number;
|
|
73
|
+
tiles: TerrainProgress | undefined;
|
|
74
|
+
renderer: SharedRenderer<ThreeParts>["stats"] | undefined;
|
|
75
|
+
mesh: SurfaceScene["mesh"] | undefined;
|
|
76
|
+
quality: string;
|
|
77
|
+
drawMs: number | undefined;
|
|
78
|
+
rocks: number;
|
|
79
|
+
};
|
|
80
|
+
connectedCallback(): void;
|
|
81
|
+
disconnectedCallback(): void;
|
|
82
|
+
attributeChangedCallback(name: string): void;
|
|
83
|
+
/**
|
|
84
|
+
* Walk: `ahead` metres along the heading (negative backs up) and `aside` to its right. The ground under the eyes is read from the heights, so the eyes rise and fall with it.
|
|
85
|
+
*
|
|
86
|
+
* @param ahead - Metres forward.
|
|
87
|
+
* @param aside - Metres to the right.
|
|
88
|
+
*/
|
|
89
|
+
walk(ahead: number, aside?: number): void;
|
|
90
|
+
}
|
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
import { createGate, frameCap } from "../gate.js";
|
|
2
|
+
import { setThreeLoader, takeRenderer } from "../scene/shared.js";
|
|
3
|
+
import { TILES_BASE } from "../scene/textures.js";
|
|
4
|
+
import { madeGround, standingPlace } from "./detail.js";
|
|
5
|
+
import { drop, groundAt } from "./height.js";
|
|
6
|
+
import { loadTerrain } from "./load.js";
|
|
7
|
+
import { createSurfaceItem } from "./scene.js";
|
|
8
|
+
import { moonSky } from "./sky.js";
|
|
9
|
+
import { siteById } from "./site.js";
|
|
10
|
+
import { clampLook, EYE_HEIGHT, stepOver, WALK_SPEED } from "./view.js";
|
|
11
|
+
export { setThreeLoader };
|
|
12
|
+
const TEMPLATE = `<style>
|
|
13
|
+
:host { display: block; width: var(--wakusei-size, 960px); max-width: 100%; aspect-ratio: var(--wakusei-ratio, 16 / 9); position: relative; overflow: hidden; background: #000; contain: layout style; touch-action: none; user-select: none; cursor: grab; }
|
|
14
|
+
:host(:focus-visible) { outline: 2px solid currentColor; outline-offset: 2px; }
|
|
15
|
+
:host([data-dragging]) { cursor: grabbing; }
|
|
16
|
+
#stage { position: absolute; inset: 0; }
|
|
17
|
+
#note { position: absolute; left: 8px; right: 8px; bottom: 6px; margin: 0; font: 400 11px/1.35 system-ui, sans-serif; color: #c8ccd4; text-shadow: 0 1px 2px #000; pointer-events: none; }
|
|
18
|
+
#status { position: absolute; inset: 0; display: grid; place-items: center; font: 500 13px system-ui, sans-serif; color: #c8ccd4; pointer-events: none; }
|
|
19
|
+
#status[hidden] { display: none; }
|
|
20
|
+
</style><div id="stage"></div><p id="note"></p><p id="status" role="status"></p>`;
|
|
21
|
+
const num = (value, fallback) => {
|
|
22
|
+
const n = value === null || value.trim() === "" ? NaN : Number(value);
|
|
23
|
+
return Number.isFinite(n) ? n : fallback;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The `<wakusei-surface>` custom element: standing on a body.
|
|
27
|
+
*
|
|
28
|
+
* Attributes: `body` (`moon`), `site` (a name from `SURFACE_SITES`, `apollo-17`), `time` (an ISO date and time, or milliseconds since 1970; the site's own landing moment when left out; `now` for the real time),
|
|
29
|
+
* `heading` (degrees clockwise from north to look toward), `pitch` (degrees up), `fov` (the view's height in degrees: zoom), `tiles` (the folder the height tiles are served from; the demo site's `tiles/` by default),
|
|
30
|
+
* `lang` (`en` or `ja`: the words on the picture).
|
|
31
|
+
*
|
|
32
|
+
* A drag turns the view; the keys W, A, S, D and the arrow keys walk (1.4 metres a second, over the real ground: it rises and falls under the eyes), Q and E turn, + and - zoom, and Ctrl with the wheel or a
|
|
33
|
+
* pinch zooms. Nothing is drawn at rest.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts no-run
|
|
37
|
+
* import type { WakuseiSurface } from "@johnmorrisdotca/wakusei-planets/surface/element";
|
|
38
|
+
*
|
|
39
|
+
* const view = document.createElement("wakusei-surface") as WakuseiSurface;
|
|
40
|
+
* view.setAttribute("site", "apollo-17");
|
|
41
|
+
* document.body.append(view);
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export class WakuseiSurface extends HTMLElement {
|
|
45
|
+
static observedAttributes = ["quality", "shadows", "detail", "exposure", "site", "time", "heading", "pitch", "fov", "tiles", "lang", "body", "standby"];
|
|
46
|
+
#root;
|
|
47
|
+
#stage;
|
|
48
|
+
#noteEl;
|
|
49
|
+
#statusEl;
|
|
50
|
+
#state = "idle";
|
|
51
|
+
#renderer;
|
|
52
|
+
#release;
|
|
53
|
+
#view;
|
|
54
|
+
#terrain;
|
|
55
|
+
#loading;
|
|
56
|
+
#abort;
|
|
57
|
+
#gate;
|
|
58
|
+
#look = { azimuth: 232, pitch: 13, fov: 74 };
|
|
59
|
+
#at = { east: 0, north: 0 };
|
|
60
|
+
#time = 0;
|
|
61
|
+
#sky;
|
|
62
|
+
#visible = typeof IntersectionObserver !== "function";
|
|
63
|
+
#io;
|
|
64
|
+
#keys = new Set();
|
|
65
|
+
#walkTimer;
|
|
66
|
+
#walkAt = 0;
|
|
67
|
+
#progress;
|
|
68
|
+
/** What has changed in the terrain since the card was last sent it, and the timer that sends it (the tiles come in dozens, the card is sent them at most every 300 ms). */
|
|
69
|
+
#pending = new Set();
|
|
70
|
+
#flush;
|
|
71
|
+
#placed = false;
|
|
72
|
+
/** Whether the view is being moved now (a drag, a step, a zoom): drawn at the quality of motion, and once more at the quality of rest when it stops. */
|
|
73
|
+
#moving = false;
|
|
74
|
+
#settle;
|
|
75
|
+
/** The quality of rest in `auto`: stepped down when a draw at rest takes too long on this machine. */
|
|
76
|
+
#restQuality = "full";
|
|
77
|
+
#drawMs;
|
|
78
|
+
#measured = 0;
|
|
79
|
+
#pointers = new Map();
|
|
80
|
+
#pinch = 0;
|
|
81
|
+
#draws = 0;
|
|
82
|
+
#onContext = (state) => {
|
|
83
|
+
if (state === "unavailable")
|
|
84
|
+
this.#set("unavailable");
|
|
85
|
+
else if (state === "lost")
|
|
86
|
+
this.#set("lost");
|
|
87
|
+
else if (this.#view?.live)
|
|
88
|
+
this.#set("live");
|
|
89
|
+
};
|
|
90
|
+
/** Whether the first view of the ground is in (the heights are fetched and the view could draw at once): true while on `standby` too, which is how a page makes ready without taking the renderer. */
|
|
91
|
+
get loaded() {
|
|
92
|
+
return this.#terrain !== undefined;
|
|
93
|
+
}
|
|
94
|
+
/** The state now. */
|
|
95
|
+
get state() {
|
|
96
|
+
return this.#state;
|
|
97
|
+
}
|
|
98
|
+
/** The eyes' look now. Setting it turns the view (azimuth and pitch in degrees, `fov` the view's height in degrees); it is drawn at once. */
|
|
99
|
+
get look() {
|
|
100
|
+
return { ...this.#look };
|
|
101
|
+
}
|
|
102
|
+
set look(value) {
|
|
103
|
+
this.#look = clampLook(value);
|
|
104
|
+
this.#changed();
|
|
105
|
+
}
|
|
106
|
+
/** Where the eyes are: metres east and north of the site, and metres over the ground under them. */
|
|
107
|
+
get position() {
|
|
108
|
+
return { ...this.#at, up: EYE_HEIGHT };
|
|
109
|
+
}
|
|
110
|
+
/** The sky now: the Sun and the Earth in it. */
|
|
111
|
+
get sky() {
|
|
112
|
+
return this.#sky;
|
|
113
|
+
}
|
|
114
|
+
/** The moment shown, milliseconds since 1970. */
|
|
115
|
+
get time() {
|
|
116
|
+
return this.#time;
|
|
117
|
+
}
|
|
118
|
+
set time(value) {
|
|
119
|
+
this.#time = value;
|
|
120
|
+
this.#changed();
|
|
121
|
+
}
|
|
122
|
+
/** The height model as loaded, or `undefined` before: for a status line and a test. */
|
|
123
|
+
get terrain() {
|
|
124
|
+
return this.#terrain;
|
|
125
|
+
}
|
|
126
|
+
/** 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. */
|
|
127
|
+
get stats() {
|
|
128
|
+
const scene = this.#view?.scene;
|
|
129
|
+
return { state: this.#state, draws: this.#draws, tiles: this.#progress, renderer: this.#renderer?.stats, mesh: scene?.mesh, quality: this.#quality(), drawMs: scene?.drawMs ?? this.#drawMs, rocks: scene?.rocks ?? 0 };
|
|
130
|
+
}
|
|
131
|
+
/** 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. */
|
|
132
|
+
#quality() {
|
|
133
|
+
const own = this.getAttribute("quality");
|
|
134
|
+
if (own === "full" || own === "medium" || own === "fast")
|
|
135
|
+
return own;
|
|
136
|
+
return this.#moving ? "fast" : this.#restQuality;
|
|
137
|
+
}
|
|
138
|
+
connectedCallback() {
|
|
139
|
+
this.#root = this.shadowRoot ?? this.attachShadow({ mode: "open" });
|
|
140
|
+
this.#root.innerHTML = TEMPLATE;
|
|
141
|
+
this.#stage = this.#root.getElementById("stage");
|
|
142
|
+
this.#noteEl = this.#root.getElementById("note");
|
|
143
|
+
this.#statusEl = this.#root.getElementById("status");
|
|
144
|
+
if (!this.hasAttribute("role"))
|
|
145
|
+
this.setAttribute("role", "img");
|
|
146
|
+
if (!this.hasAttribute("tabindex"))
|
|
147
|
+
this.setAttribute("tabindex", "0");
|
|
148
|
+
this.setAttribute("data-state", this.#state);
|
|
149
|
+
this.#gate = createGate(1000 / frameCap(30, { deviceMemory: navigator.deviceMemory, hardwareConcurrency: navigator.hardwareConcurrency }), () => this.#draw());
|
|
150
|
+
this.#readAttributes();
|
|
151
|
+
this.addEventListener("pointerdown", this.#onPointerDown);
|
|
152
|
+
this.addEventListener("pointermove", this.#onPointerMove);
|
|
153
|
+
this.addEventListener("pointerup", this.#onPointerUp);
|
|
154
|
+
this.addEventListener("pointercancel", this.#onPointerUp);
|
|
155
|
+
this.addEventListener("wheel", this.#onWheel, { passive: false });
|
|
156
|
+
this.addEventListener("keydown", this.#onKeyDown);
|
|
157
|
+
this.addEventListener("keyup", this.#onKeyUp);
|
|
158
|
+
this.addEventListener("blur", this.#onBlur);
|
|
159
|
+
if (typeof IntersectionObserver === "function") {
|
|
160
|
+
this.#io = new IntersectionObserver((entries) => {
|
|
161
|
+
const last = entries[entries.length - 1];
|
|
162
|
+
if (last === undefined)
|
|
163
|
+
return;
|
|
164
|
+
this.#visible = last.isIntersecting;
|
|
165
|
+
if (this.#visible)
|
|
166
|
+
void this.#start();
|
|
167
|
+
else
|
|
168
|
+
this.#view?.blur();
|
|
169
|
+
});
|
|
170
|
+
this.#io.observe(this);
|
|
171
|
+
}
|
|
172
|
+
void this.#start();
|
|
173
|
+
}
|
|
174
|
+
disconnectedCallback() {
|
|
175
|
+
this.#io?.disconnect();
|
|
176
|
+
this.#abort?.abort();
|
|
177
|
+
clearTimeout(this.#flush);
|
|
178
|
+
this.#flush = undefined;
|
|
179
|
+
this.#gate?.cancel();
|
|
180
|
+
clearTimeout(this.#walkTimer);
|
|
181
|
+
for (const [type, fn] of [["pointerdown", this.#onPointerDown], ["pointermove", this.#onPointerMove], ["pointerup", this.#onPointerUp], ["pointercancel", this.#onPointerUp], ["wheel", this.#onWheel], ["keydown", this.#onKeyDown], ["keyup", this.#onKeyUp], ["blur", this.#onBlur]])
|
|
182
|
+
this.removeEventListener(type, fn);
|
|
183
|
+
this.#view?.dispose();
|
|
184
|
+
this.#view = undefined;
|
|
185
|
+
this.#release?.();
|
|
186
|
+
this.#release = undefined;
|
|
187
|
+
this.#renderer = undefined;
|
|
188
|
+
}
|
|
189
|
+
attributeChangedCallback(name) {
|
|
190
|
+
if (!this.isConnected)
|
|
191
|
+
return;
|
|
192
|
+
if (name === "standby") {
|
|
193
|
+
if (!this.hasAttribute("standby"))
|
|
194
|
+
void this.#start();
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (name === "site" || name === "body" || name === "tiles") {
|
|
198
|
+
this.#terrain = undefined;
|
|
199
|
+
this.#loading = undefined;
|
|
200
|
+
this.#pending.clear();
|
|
201
|
+
this.removeAttribute("data-loading");
|
|
202
|
+
this.#view?.dispose();
|
|
203
|
+
this.#view = undefined;
|
|
204
|
+
this.#readAttributes();
|
|
205
|
+
void this.#start();
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
this.#readAttributes();
|
|
209
|
+
this.#changed();
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Walk: `ahead` metres along the heading (negative backs up) and `aside` to its right. The ground under the eyes is read from the heights, so the eyes rise and fall with it.
|
|
213
|
+
*
|
|
214
|
+
* @param ahead - Metres forward.
|
|
215
|
+
* @param aside - Metres to the right.
|
|
216
|
+
*/
|
|
217
|
+
walk(ahead, aside = 0) {
|
|
218
|
+
this.#at = stepOver(this.#at, this.#look.azimuth, ahead, aside);
|
|
219
|
+
this.#changed();
|
|
220
|
+
}
|
|
221
|
+
#site() {
|
|
222
|
+
return siteById(this.getAttribute("site") ?? "apollo-17");
|
|
223
|
+
}
|
|
224
|
+
#readAttributes() {
|
|
225
|
+
const site = this.#site();
|
|
226
|
+
if (site === undefined) {
|
|
227
|
+
this.#set("unavailable", "no such site");
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
const look = site.look;
|
|
231
|
+
this.#look = clampLook({ azimuth: num(this.getAttribute("heading"), look.azimuth), pitch: num(this.getAttribute("pitch"), look.pitch), fov: num(this.getAttribute("fov"), look.fov) });
|
|
232
|
+
const time = this.getAttribute("time");
|
|
233
|
+
this.#time = time === null || time === "" ? site.time : time === "now" ? Date.now() : Number.isFinite(Number(time)) ? Number(time) : Date.parse(time);
|
|
234
|
+
if (!Number.isFinite(this.#time))
|
|
235
|
+
this.#time = site.time;
|
|
236
|
+
this.#sky = moonSky(site.lat, site.lon, this.#time);
|
|
237
|
+
this.setAttribute("aria-label", `${site.name[this.#lang()]}: ${this.#lang() === "ja" ? "月面に立った視点" : "standing on the Moon"}`);
|
|
238
|
+
}
|
|
239
|
+
#lang() {
|
|
240
|
+
return this.getAttribute("lang") === "ja" ? "ja" : "en";
|
|
241
|
+
}
|
|
242
|
+
#set(state, text = "") {
|
|
243
|
+
this.#state = state;
|
|
244
|
+
this.setAttribute("data-state", state);
|
|
245
|
+
if (this.#statusEl !== undefined) {
|
|
246
|
+
this.#statusEl.textContent = state === "live" ? "" : text;
|
|
247
|
+
this.#statusEl.hidden = state === "live" || text === "";
|
|
248
|
+
}
|
|
249
|
+
this.dispatchEvent(new CustomEvent("wakusei-surface-state", { detail: { state } }));
|
|
250
|
+
}
|
|
251
|
+
async #start() {
|
|
252
|
+
if (!this.isConnected || !this.#visible || this.#state === "unavailable")
|
|
253
|
+
return;
|
|
254
|
+
const site = this.#site();
|
|
255
|
+
if (site === undefined)
|
|
256
|
+
return;
|
|
257
|
+
if (this.#terrain === undefined) {
|
|
258
|
+
this.#loading ??= this.#load(site.body, site.id);
|
|
259
|
+
await this.#loading;
|
|
260
|
+
}
|
|
261
|
+
if (this.#terrain === undefined)
|
|
262
|
+
return;
|
|
263
|
+
// on standby the ground is loaded and nothing more: the renderer is another view's until the attribute goes
|
|
264
|
+
if (this.hasAttribute("standby"))
|
|
265
|
+
return;
|
|
266
|
+
if (!this.#placed) {
|
|
267
|
+
// a step or two from the landing point, out of any made crater
|
|
268
|
+
const stand = this.getAttribute("detail") === "off" ? { x: 0, y: 0 } : standingPlace(0, 0);
|
|
269
|
+
this.#at = { east: stand.x, north: stand.y };
|
|
270
|
+
this.#placed = true;
|
|
271
|
+
}
|
|
272
|
+
if (this.#view === undefined) {
|
|
273
|
+
if (this.#renderer === undefined) {
|
|
274
|
+
const taken = takeRenderer(this.#onContext);
|
|
275
|
+
this.#renderer = taken.renderer;
|
|
276
|
+
this.#release = taken.release;
|
|
277
|
+
}
|
|
278
|
+
const item = createSurfaceItem({ levels: this.#terrain.levels, pictures: this.#terrain.pictures, horizons: this.#terrain.horizons, manifest: this.#terrain.manifest, groundY: (east, north) => this.#groundAt(east, north), key: `surface:${site.id}` });
|
|
279
|
+
this.#view = this.#renderer.view(this.#stage, item, { still: null });
|
|
280
|
+
}
|
|
281
|
+
const ok = await this.#view.focus();
|
|
282
|
+
if (ok) {
|
|
283
|
+
this.#set("live");
|
|
284
|
+
this.#noteEl.textContent = this.#noteText();
|
|
285
|
+
this.#changed();
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
const now = this.#state;
|
|
289
|
+
if (now !== "unavailable" && now !== "lost")
|
|
290
|
+
this.#set("unavailable", this.#lang() === "ja" ? "この端末では3Dを描けません" : "This device cannot draw 3D");
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
async #load(body, site) {
|
|
294
|
+
this.#set("loading", this.#lang() === "ja" ? "地形を読み込んでいます" : "Loading the ground");
|
|
295
|
+
this.#abort?.abort();
|
|
296
|
+
this.#abort = new AbortController();
|
|
297
|
+
try {
|
|
298
|
+
const base = this.getAttribute("tiles");
|
|
299
|
+
const folder = base === null || base === "" ? TILES_BASE : base;
|
|
300
|
+
this.#terrain = await loadTerrain(folder, body, site, {
|
|
301
|
+
signal: this.#abort.signal,
|
|
302
|
+
onProgress: (progress) => {
|
|
303
|
+
this.#progress = progress;
|
|
304
|
+
if (this.#state === "loading")
|
|
305
|
+
this.#statusEl.textContent = `${this.#lang() === "ja" ? "地形を読み込んでいます" : "Loading the ground"} ${Math.round((100 * Math.min(progress.done, progress.first)) / progress.first)} %`;
|
|
306
|
+
},
|
|
307
|
+
onChange: (id) => {
|
|
308
|
+
this.#pending.add(id);
|
|
309
|
+
this.#flush ??= setTimeout(() => this.#sendChanges(), 300);
|
|
310
|
+
},
|
|
311
|
+
});
|
|
312
|
+
// the first view is in and the ground is whole; the rest comes in place, nearest first
|
|
313
|
+
this.setAttribute("data-loading", "more");
|
|
314
|
+
const terrain = this.#terrain;
|
|
315
|
+
terrain.complete.then(() => {
|
|
316
|
+
if (this.#terrain === terrain)
|
|
317
|
+
this.setAttribute("data-loading", "done");
|
|
318
|
+
}, (error) => {
|
|
319
|
+
if (this.#terrain === terrain && error.name !== "AbortError")
|
|
320
|
+
this.setAttribute("data-loading", "failed");
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
catch (error) {
|
|
324
|
+
if (error.name !== "AbortError")
|
|
325
|
+
this.#set("unavailable", this.#lang() === "ja" ? "この場所の地形がありません" : "No ground for this site yet");
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
/** Send the card what has changed in the terrain and draw it. */
|
|
329
|
+
#sendChanges() {
|
|
330
|
+
this.#flush = undefined;
|
|
331
|
+
const ids = [...this.#pending];
|
|
332
|
+
this.#pending.clear();
|
|
333
|
+
const scene = this.#view?.scene;
|
|
334
|
+
if (scene === undefined || ids.length === 0)
|
|
335
|
+
return;
|
|
336
|
+
scene.refresh(ids);
|
|
337
|
+
this.#changed();
|
|
338
|
+
}
|
|
339
|
+
#noteText() {
|
|
340
|
+
const m = this.#terrain?.manifest;
|
|
341
|
+
if (m === undefined)
|
|
342
|
+
return "";
|
|
343
|
+
const made = this.getAttribute("detail") === "off" ? "" : this.#lang() === "ja" ? "5 m より細かい地形(小さなクレーター、砂塵、岩)は計算で作ったもので、実測ではありません。" : "Detail finer than the 5 m grid (small craters, dust, rocks) is made, not measured.";
|
|
344
|
+
const picture = m.picture === null || m.picture === undefined ? "" : ` ${m.picture.credit}.`;
|
|
345
|
+
return this.#lang() === "ja" ? `高さの実測: ${m.accuracy}。${m.credit}。${made}` : `Heights measured: ${m.accuracy}. ${m.credit}.${picture} ${made}`;
|
|
346
|
+
}
|
|
347
|
+
/** The ground's height at a point over the site's: the measured heights, the made craters on them (if the page has them on) and the Moon's curve taken off. */
|
|
348
|
+
#groundAt(east, north) {
|
|
349
|
+
const t = this.#terrain;
|
|
350
|
+
const h = groundAt(t.levels, t.manifest.height, east, north) ?? t.manifest.ground;
|
|
351
|
+
const made = this.getAttribute("detail") === "off" ? 0 : madeGround(east, north, 0.35).h;
|
|
352
|
+
return h - t.manifest.ground - drop(Math.hypot(east, north), t.manifest.radius) + made;
|
|
353
|
+
}
|
|
354
|
+
/** 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. */
|
|
355
|
+
#eyeUp() {
|
|
356
|
+
return this.#groundAt(this.#at.east, this.#at.north) + EYE_HEIGHT;
|
|
357
|
+
}
|
|
358
|
+
/** 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. */
|
|
359
|
+
#changed(moving = true) {
|
|
360
|
+
if (!this.#view?.live)
|
|
361
|
+
return;
|
|
362
|
+
if (moving) {
|
|
363
|
+
this.#moving = true;
|
|
364
|
+
clearTimeout(this.#settle);
|
|
365
|
+
this.#settle = setTimeout(() => {
|
|
366
|
+
this.#moving = false;
|
|
367
|
+
this.#gate?.request();
|
|
368
|
+
}, 160);
|
|
369
|
+
}
|
|
370
|
+
this.#gate?.request();
|
|
371
|
+
}
|
|
372
|
+
#draw() {
|
|
373
|
+
const scene = this.#view?.scene;
|
|
374
|
+
if (scene === undefined || this.#terrain === undefined)
|
|
375
|
+
return;
|
|
376
|
+
const site = this.#site();
|
|
377
|
+
if (site === undefined)
|
|
378
|
+
return;
|
|
379
|
+
this.#sky = moonSky(site.lat, site.lon, this.#time);
|
|
380
|
+
// `auto`: a draw at rest is timed now and then, and a machine that takes too long at rest steps down (a card does a draw at full quality in a few milliseconds; a phone may not)
|
|
381
|
+
const auto = this.getAttribute("quality") === null || this.getAttribute("quality") === "auto";
|
|
382
|
+
const was = scene.drawMs;
|
|
383
|
+
if (auto && !this.#moving && was !== undefined && this.#measured > 0) {
|
|
384
|
+
if (was > 120 && this.#restQuality === "full")
|
|
385
|
+
this.#restQuality = "medium";
|
|
386
|
+
else if (was > 250 && this.#restQuality === "medium")
|
|
387
|
+
this.#restQuality = "fast";
|
|
388
|
+
}
|
|
389
|
+
const measure = auto && !this.#moving && this.#measured < 4;
|
|
390
|
+
if (measure)
|
|
391
|
+
this.#measured += 1;
|
|
392
|
+
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(), measure: measure || this.getAttribute("measure") !== null });
|
|
393
|
+
this.#view.invalidate();
|
|
394
|
+
this.#draws += 1;
|
|
395
|
+
}
|
|
396
|
+
// --- hands ---------------------------------------------------------------------------------------------------------------------------------------
|
|
397
|
+
#onPointerDown = (event) => {
|
|
398
|
+
this.#pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
|
|
399
|
+
this.setPointerCapture(event.pointerId);
|
|
400
|
+
this.setAttribute("data-dragging", "");
|
|
401
|
+
this.#pinch = this.#pointers.size === 2 ? this.#spread() : 0;
|
|
402
|
+
};
|
|
403
|
+
#onPointerMove = (event) => {
|
|
404
|
+
const was = this.#pointers.get(event.pointerId);
|
|
405
|
+
if (was === undefined)
|
|
406
|
+
return;
|
|
407
|
+
this.#pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
|
|
408
|
+
if (this.#pointers.size >= 2) {
|
|
409
|
+
const now = this.#spread();
|
|
410
|
+
if (this.#pinch > 0 && now > 0)
|
|
411
|
+
this.#zoom(this.#pinch / now);
|
|
412
|
+
this.#pinch = now;
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
const box = this.getBoundingClientRect();
|
|
416
|
+
const perPixel = this.#look.fov / Math.max(1, box.height);
|
|
417
|
+
this.#look = clampLook({ ...this.#look, azimuth: this.#look.azimuth - (event.clientX - was.x) * perPixel, pitch: this.#look.pitch + (event.clientY - was.y) * perPixel });
|
|
418
|
+
this.#changed();
|
|
419
|
+
};
|
|
420
|
+
#onPointerUp = (event) => {
|
|
421
|
+
this.#pointers.delete(event.pointerId);
|
|
422
|
+
if (this.#pointers.size === 0)
|
|
423
|
+
this.removeAttribute("data-dragging");
|
|
424
|
+
this.#pinch = 0;
|
|
425
|
+
};
|
|
426
|
+
#spread() {
|
|
427
|
+
const [a, b] = [...this.#pointers.values()];
|
|
428
|
+
return a === undefined || b === undefined ? 0 : Math.hypot(a.x - b.x, a.y - b.y);
|
|
429
|
+
}
|
|
430
|
+
#zoom(factor) {
|
|
431
|
+
this.#look = clampLook({ ...this.#look, fov: this.#look.fov * factor });
|
|
432
|
+
this.#changed();
|
|
433
|
+
}
|
|
434
|
+
#onWheel = (event) => {
|
|
435
|
+
if (!event.ctrlKey && !event.metaKey)
|
|
436
|
+
return;
|
|
437
|
+
event.preventDefault();
|
|
438
|
+
this.#zoom(Math.exp(event.deltaY * 0.0015));
|
|
439
|
+
};
|
|
440
|
+
#onKeyDown = (event) => {
|
|
441
|
+
const key = event.key.length === 1 ? event.key.toLowerCase() : event.key;
|
|
442
|
+
if (key === "+" || key === "=")
|
|
443
|
+
return this.#zoom(0.85);
|
|
444
|
+
if (key === "-" || key === "_")
|
|
445
|
+
return this.#zoom(1 / 0.85);
|
|
446
|
+
if (!["w", "a", "s", "d", "q", "e", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(key))
|
|
447
|
+
return;
|
|
448
|
+
event.preventDefault();
|
|
449
|
+
if (!this.#keys.has(key)) {
|
|
450
|
+
this.#keys.add(key);
|
|
451
|
+
this.#walkAt = performance.now();
|
|
452
|
+
this.#walkTick();
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
#onKeyUp = (event) => {
|
|
456
|
+
this.#keys.delete(event.key.length === 1 ? event.key.toLowerCase() : event.key);
|
|
457
|
+
};
|
|
458
|
+
#onBlur = () => {
|
|
459
|
+
this.#keys.clear();
|
|
460
|
+
};
|
|
461
|
+
/** 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. */
|
|
462
|
+
#walkTick = () => {
|
|
463
|
+
clearTimeout(this.#walkTimer);
|
|
464
|
+
if (this.#keys.size === 0)
|
|
465
|
+
return;
|
|
466
|
+
const now = performance.now();
|
|
467
|
+
const dt = Math.min(0.1, (now - this.#walkAt) / 1000);
|
|
468
|
+
this.#walkAt = now;
|
|
469
|
+
const k = this.#keys;
|
|
470
|
+
const ahead = (k.has("w") || k.has("ArrowUp") ? 1 : 0) - (k.has("s") || k.has("ArrowDown") ? 1 : 0);
|
|
471
|
+
const aside = (k.has("d") ? 1 : 0) - (k.has("a") ? 1 : 0);
|
|
472
|
+
const turn = (k.has("e") || k.has("ArrowRight") ? 1 : 0) - (k.has("q") || k.has("ArrowLeft") ? 1 : 0);
|
|
473
|
+
if (turn !== 0)
|
|
474
|
+
this.#look = clampLook({ ...this.#look, azimuth: this.#look.azimuth + turn * 60 * dt });
|
|
475
|
+
if (ahead !== 0 || aside !== 0)
|
|
476
|
+
this.#at = stepOver(this.#at, this.#look.azimuth, ahead * WALK_SPEED * dt, aside * WALK_SPEED * dt);
|
|
477
|
+
this.#changed();
|
|
478
|
+
this.#walkTimer = setTimeout(this.#walkTick, 33);
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
if (typeof customElements !== "undefined" && customElements.get("wakusei-surface") === undefined)
|
|
482
|
+
customElements.define("wakusei-surface", WakuseiSurface);
|