@johnmorrisdotca/chikyu-earth 0.10.0 → 0.11.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 +28 -11
- package/README.md +206 -4
- package/dist/element.d.ts +5 -2
- package/dist/element.js +26 -5
- package/dist/realistic/earth.d.ts +15 -0
- package/dist/realistic/earth.js +6 -2
- package/dist/realistic/element.d.ts +4 -0
- package/dist/realistic/element.js +14 -1
- package/dist/realistic/index.d.ts +1 -1
- package/dist/realistic/shaders.d.ts +1 -1
- package/dist/realistic/shaders.js +2 -0
- package/dist/space/element.d.ts +21 -2
- package/dist/space/element.js +43 -3
- package/dist/space/ephemeris.d.ts +2 -2
- package/dist/space/ephemeris.js +2 -2
- package/dist/space/index.d.ts +1 -1
- package/dist/space/scale.js +1 -0
- package/dist/space/scene.d.ts +69 -7
- package/dist/space/scene.js +155 -22
- package/package.json +8 -3
package/dist/space/element.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { GeoPoint } from "../types.ts";
|
|
2
2
|
import { setThreeLoader } from "../realistic/shared.ts";
|
|
3
|
+
import type { LightMode } from "../realistic/earth.ts";
|
|
3
4
|
import type { SatelliteInfo, SatelliteRef, Satellites } from "../satellites/types.ts";
|
|
5
|
+
import { type PlanetName } from "./ephemeris.ts";
|
|
4
6
|
import { type SatelliteStyle } from "./satellite-layer.ts";
|
|
5
7
|
import type { RideLook } from "./ride.ts";
|
|
6
8
|
import { type ScaleMode } from "./scale.ts";
|
|
@@ -53,7 +55,7 @@ export { setThreeLoader };
|
|
|
53
55
|
*/
|
|
54
56
|
export type SpaceState = "loading" | "live" | "unavailable" | "lost" | "idle";
|
|
55
57
|
/**
|
|
56
|
-
* A named distance for `zoomFor`: the Earth, the Moon, the Sun, the inner planets
|
|
58
|
+
* A named distance for `zoomFor`: the Earth, the Moon, the Sun, the inner planets, the whole solar system, or a planet with its nearer moons (`Jupiter-moons`: the zoom that shows them, when a body painter that draws moons is set).
|
|
57
59
|
*
|
|
58
60
|
* @example
|
|
59
61
|
* ```ts no-run
|
|
@@ -63,7 +65,7 @@ export type SpaceState = "loading" | "live" | "unavailable" | "lost" | "idle";
|
|
|
63
65
|
* console.log(typeof zoomTo);
|
|
64
66
|
* ```
|
|
65
67
|
*/
|
|
66
|
-
export type SpacePreset = "earth" | "moon" | "sun" | "inner" | "solar-system"
|
|
68
|
+
export type SpacePreset = "earth" | "moon" | "sun" | "inner" | "solar-system" | `${Exclude<PlanetName, "Mercury" | "Venus">}-moons`;
|
|
67
69
|
/**
|
|
68
70
|
* How a ride along is done: `view` `from` (the default) puts the camera at the body looking at the Earth, `follow` keeps it a fixed distance from the body looking at it (`distanceKm`, 3000 by default,
|
|
69
71
|
* for a satellite; a planet or the Moon is kept at the distance the zoom says); `look` `down` (the default) or `horizon` is where a camera at the body points; `smooth` (the default) glides
|
|
@@ -143,6 +145,17 @@ export declare class ChikyuSpace extends HTMLElement {
|
|
|
143
145
|
set scale(value: ScaleMode);
|
|
144
146
|
get labels(): boolean;
|
|
145
147
|
set labels(value: boolean);
|
|
148
|
+
/**
|
|
149
|
+
* How the Sun, the planets, Pluto and their moons are drawn: one of the `looks` of the body painter set with `setBodyPainter` (wakusei-planets has `realistic` and `painted`), or `plain` (the sphere in the right colour, no moons); empty for the painter's first.
|
|
150
|
+
* With no painter they are plain spheres and this does nothing. Changing it draws the bodies again with the new look.
|
|
151
|
+
*/
|
|
152
|
+
get bodies(): string;
|
|
153
|
+
set bodies(value: string);
|
|
154
|
+
/** How the Earth is lit: `sun` (true, the default), `lit` (always lit, as if by daylight from the camera's side, so a ride on the night side is never a black frame) or `boost` (the true terminator, with the night side raised). The Sun, the Moon and the planets keep their true places. */
|
|
155
|
+
get light(): LightMode;
|
|
156
|
+
set light(value: LightMode);
|
|
157
|
+
/** The looks the body painter offers (empty with no painter), for a page to build its choice from. */
|
|
158
|
+
get bodyLooks(): readonly string[];
|
|
146
159
|
get orbits(): boolean;
|
|
147
160
|
set orbits(value: boolean);
|
|
148
161
|
/**
|
|
@@ -235,6 +248,12 @@ export declare class ChikyuSpace extends HTMLElement {
|
|
|
235
248
|
position: [number, number, number];
|
|
236
249
|
fov: number;
|
|
237
250
|
};
|
|
251
|
+
/** What the body painter has made at the last draw: the Sun and planets with a drawing, and the moons shown round a planet that is near. */
|
|
252
|
+
bodies: {
|
|
253
|
+
look: string;
|
|
254
|
+
painted: number;
|
|
255
|
+
moons: number;
|
|
256
|
+
};
|
|
238
257
|
};
|
|
239
258
|
}
|
|
240
259
|
declare global {
|
package/dist/space/element.js
CHANGED
|
@@ -5,7 +5,7 @@ import { MAX_TEXTURE } from "../realistic/tiers.js";
|
|
|
5
5
|
import { MAX_SATELLITE_SPEED } from "../satellites/limits.js";
|
|
6
6
|
import { AU_IN_EARTH_RADII, EARTH_RADIUS_KM, frameAt } from "./ephemeris.js";
|
|
7
7
|
import { MAX_ORBIT_GROUP } from "./satellite-layer.js";
|
|
8
|
-
import { placeAround, placeMoon, placeSun } from "./scale.js";
|
|
8
|
+
import { placeAround, placeMoon, placeSun, RADIUS } from "./scale.js";
|
|
9
9
|
import { BODY_NAMES, createSpaceItem, FIT_DISTANCE, newSpaceModel } from "./scene.js";
|
|
10
10
|
export { BODY_NAMES };
|
|
11
11
|
let painter;
|
|
@@ -27,8 +27,10 @@ export function setBodyPainter(next) {
|
|
|
27
27
|
painter = next;
|
|
28
28
|
}
|
|
29
29
|
/** Mean radii in kilometres (IAU), for what a click tells. */
|
|
30
|
-
const RADIUS_KM = { Sun: 695_700, Mercury: 2439.7, Venus: 6051.8, Earth: 6371, Moon: 1737.4, Mars: 3389.5, Jupiter: 69_911, Saturn: 58_232, Uranus: 25_362, Neptune: 24_622 };
|
|
30
|
+
const RADIUS_KM = { Sun: 695_700, Mercury: 2439.7, Venus: 6051.8, Earth: 6371, Moon: 1737.4, Mars: 3389.5, Jupiter: 69_911, Saturn: 58_232, Uranus: 25_362, Neptune: 24_622, Pluto: 1188.3 };
|
|
31
31
|
export { setThreeLoader };
|
|
32
|
+
/** A `light` attribute: `lit` or `boost`, else the true sun. */
|
|
33
|
+
const readLight = (value) => (value === "lit" || value === "boost" ? value : "sun");
|
|
32
34
|
const RAD = Math.PI / 180;
|
|
33
35
|
/** The Earth's own turn, degrees a second. */
|
|
34
36
|
const EARTH_RATE = 360 / 86_164.0905;
|
|
@@ -65,7 +67,7 @@ const STYLE = `:host{display:block;aspect-ratio:1/1;position:relative;overflow:h
|
|
|
65
67
|
* ```
|
|
66
68
|
*/
|
|
67
69
|
export class ChikyuSpace extends HTMLElement {
|
|
68
|
-
static observedAttributes = ["center", "time", "speed", "scale", "labels", "orbits"];
|
|
70
|
+
static observedAttributes = ["center", "time", "speed", "scale", "labels", "orbits", "bodies", "light"];
|
|
69
71
|
#model = newSpaceModel();
|
|
70
72
|
#mode = "now";
|
|
71
73
|
#speed = 1;
|
|
@@ -93,6 +95,8 @@ export class ChikyuSpace extends HTMLElement {
|
|
|
93
95
|
this.#model.mode = this.getAttribute("scale") === "true" ? "true" : "compressed";
|
|
94
96
|
this.#model.labels = this.getAttribute("labels") !== "off";
|
|
95
97
|
this.#model.orbits = this.getAttribute("orbits") !== "off";
|
|
98
|
+
this.#model.bodies = this.getAttribute("bodies") ?? "";
|
|
99
|
+
this.#model.light = readLight(this.getAttribute("light"));
|
|
96
100
|
this.#model.stepMs = Math.max(1000, Math.abs(this.#speed) * 1000);
|
|
97
101
|
this.#model.speed = this.#speed;
|
|
98
102
|
if (!this.hasAttribute("role"))
|
|
@@ -129,6 +133,7 @@ export class ChikyuSpace extends HTMLElement {
|
|
|
129
133
|
onRideLost: () => this.#rideLost(),
|
|
130
134
|
onSatellitesError: (error) => this.dispatchEvent(new CustomEvent("chikyu-satellites-unavailable", { detail: { reason: error instanceof Error ? error.message : String(error) } })),
|
|
131
135
|
...(painter ? { painter } : {}),
|
|
136
|
+
onPainterError: (error) => this.dispatchEvent(new CustomEvent("chikyu-bodies-unavailable", { detail: { reason: error instanceof Error ? error.message : String(error) } })),
|
|
132
137
|
});
|
|
133
138
|
this.#view = taken.renderer.view(this.#stage, item, { still: null });
|
|
134
139
|
this.#set("loading");
|
|
@@ -170,6 +175,10 @@ export class ChikyuSpace extends HTMLElement {
|
|
|
170
175
|
this.labels = value !== "off";
|
|
171
176
|
else if (name === "orbits")
|
|
172
177
|
this.orbits = value !== "off";
|
|
178
|
+
else if (name === "bodies")
|
|
179
|
+
this.bodies = value ?? "";
|
|
180
|
+
else if (name === "light")
|
|
181
|
+
this.light = readLight(value);
|
|
173
182
|
}
|
|
174
183
|
get centre() {
|
|
175
184
|
return { ...this.#model.centre };
|
|
@@ -219,6 +228,29 @@ export class ChikyuSpace extends HTMLElement {
|
|
|
219
228
|
label.hidden = true;
|
|
220
229
|
this.#gate?.request();
|
|
221
230
|
}
|
|
231
|
+
/**
|
|
232
|
+
* How the Sun, the planets, Pluto and their moons are drawn: one of the `looks` of the body painter set with `setBodyPainter` (wakusei-planets has `realistic` and `painted`), or `plain` (the sphere in the right colour, no moons); empty for the painter's first.
|
|
233
|
+
* With no painter they are plain spheres and this does nothing. Changing it draws the bodies again with the new look.
|
|
234
|
+
*/
|
|
235
|
+
get bodies() {
|
|
236
|
+
return this.#model.bodies;
|
|
237
|
+
}
|
|
238
|
+
set bodies(value) {
|
|
239
|
+
this.#model.bodies = value;
|
|
240
|
+
this.#gate?.request();
|
|
241
|
+
}
|
|
242
|
+
/** How the Earth is lit: `sun` (true, the default), `lit` (always lit, as if by daylight from the camera's side, so a ride on the night side is never a black frame) or `boost` (the true terminator, with the night side raised). The Sun, the Moon and the planets keep their true places. */
|
|
243
|
+
get light() {
|
|
244
|
+
return this.#model.light;
|
|
245
|
+
}
|
|
246
|
+
set light(value) {
|
|
247
|
+
this.#model.light = value;
|
|
248
|
+
this.#gate?.request();
|
|
249
|
+
}
|
|
250
|
+
/** The looks the body painter offers (empty with no painter), for a page to build its choice from. */
|
|
251
|
+
get bodyLooks() {
|
|
252
|
+
return painter?.looks ?? [];
|
|
253
|
+
}
|
|
222
254
|
get orbits() {
|
|
223
255
|
return this.#model.orbits;
|
|
224
256
|
}
|
|
@@ -394,6 +426,13 @@ export class ChikyuSpace extends HTMLElement {
|
|
|
394
426
|
}
|
|
395
427
|
/** The zoom that shows a part of the sky: the Earth alone, the Moon's orbit, the Sun, the planets out to Mars, or all of them, at this scale and moment. */
|
|
396
428
|
zoomFor(preset) {
|
|
429
|
+
if (preset.endsWith("-moons")) {
|
|
430
|
+
const name = preset.slice(0, -6);
|
|
431
|
+
const sites = painter?.moons?.(name, this.#model.time) ?? [];
|
|
432
|
+
const reach = Math.max(1, ...sites.map((site) => Math.hypot(...site.offset)).filter((r) => r <= 22));
|
|
433
|
+
const radius = RADIUS[name][this.#model.mode];
|
|
434
|
+
return Math.min(1, (FIT_DISTANCE * radius) / ((reach * radius * 1.15) / Math.tan(17.5 * RAD)));
|
|
435
|
+
}
|
|
397
436
|
const frame = this.#model.drawn?.frame ?? frameAt(this.#model.time);
|
|
398
437
|
const mode = this.#model.mode;
|
|
399
438
|
const sun = placeSun(mode, frame.sun);
|
|
@@ -423,6 +462,7 @@ export class ChikyuSpace extends HTMLElement {
|
|
|
423
462
|
satellites: this.#satelliteStats(),
|
|
424
463
|
ride: { riding: this.#model.ride !== null, lost: this.#model.rideLost, gliding: this.#model.transition !== null },
|
|
425
464
|
camera: { position: [this.#model.pose.p[0], this.#model.pose.p[1], this.#model.pose.p[2]], fov: this.#model.pose.fov },
|
|
465
|
+
bodies: { look: this.#model.bodies || painter?.looks?.[0] || "plain", painted: this.#model.painted.bodies, moons: this.#model.painted.moons },
|
|
426
466
|
};
|
|
427
467
|
}
|
|
428
468
|
#satelliteStats() {
|
|
@@ -20,9 +20,9 @@ export type Vec3 = [number, number, number];
|
|
|
20
20
|
* console.log(PLANETS.join(", "));
|
|
21
21
|
* ```
|
|
22
22
|
*/
|
|
23
|
-
export declare const PLANETS: readonly ["Mercury", "Venus", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"];
|
|
23
|
+
export declare const PLANETS: readonly ["Mercury", "Venus", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"];
|
|
24
24
|
/**
|
|
25
|
-
* One of the
|
|
25
|
+
* One of the planets drawn (Pluto too), by name.
|
|
26
26
|
*
|
|
27
27
|
* @example
|
|
28
28
|
* ```ts
|
package/dist/space/ephemeris.js
CHANGED
|
@@ -11,7 +11,7 @@ import { Body, GeoVector, HelioVector, SiderealTime } from "astronomy-engine";
|
|
|
11
11
|
* console.log(PLANETS.join(", "));
|
|
12
12
|
* ```
|
|
13
13
|
*/
|
|
14
|
-
export const PLANETS = ["Mercury", "Venus", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"];
|
|
14
|
+
export const PLANETS = ["Mercury", "Venus", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"];
|
|
15
15
|
/** Equatorial J2000 axes (x to the vernal equinox, z to the north pole) to the scene's (y up). */
|
|
16
16
|
const scene = (v) => [v.x, v.z, -v.y];
|
|
17
17
|
const AU_KM = 149_597_870.7;
|
|
@@ -157,7 +157,7 @@ export function createTrack(initialStepMs = 1000) {
|
|
|
157
157
|
* ```
|
|
158
158
|
*/
|
|
159
159
|
export function orbitPath(name, ms, count = 96) {
|
|
160
|
-
const periodDays = { Mercury: 87.97, Venus: 224.7, Mars: 686.98, Jupiter: 4332.6, Saturn: 10759, Uranus: 30688, Neptune: 60182 }[name];
|
|
160
|
+
const periodDays = { Mercury: 87.97, Venus: 224.7, Mars: 686.98, Jupiter: 4332.6, Saturn: 10759, Uranus: 30688, Neptune: 60182, Pluto: 90560 }[name];
|
|
161
161
|
const points = [];
|
|
162
162
|
for (let i = 0; i <= count; i += 1)
|
|
163
163
|
points.push(scene(HelioVector(Body[name], new Date(ms + (i / count) * periodDays * 86_400_000))));
|
package/dist/space/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { AU_IN_EARTH_RADII, blend, createTrack, EARTH_RADIUS_KM, frameAt, orbitPath, PLANETS, type Frame, type PlanetName, type Vec3 } from "./ephemeris.ts";
|
|
2
2
|
export { compress, MOON_COMPRESSED, placeAround, placeMoon, placeSun, RADIUS, SUN_COMPRESSED, truePosition, type ScaleMode } from "./scale.ts";
|
|
3
|
-
export { plainPainter, type BodyDrawing, type BodyPainter } from "./scene.ts";
|
|
3
|
+
export { plainPainter, type BodyDrawing, type BodyMoon, type BodyPainter, type PaintContext } from "./scene.ts";
|
|
4
4
|
export { RIDE_FOV, ridePose, type RideInput, type RideLook, type RidePose } from "./ride.ts";
|
package/dist/space/scale.js
CHANGED
package/dist/space/scene.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type * as THREE from "three";
|
|
|
2
2
|
import type { AssetCache, Item3D } from "@johnmorrisdotca/kazoku-ui/renderer";
|
|
3
3
|
import { type ThreeParts } from "../realistic/textures.ts";
|
|
4
4
|
import type { Layer } from "../realistic/tiers.ts";
|
|
5
|
+
import type { LightMode } from "../realistic/earth.ts";
|
|
5
6
|
import { type Frame, type PlanetName, type Vec3 } from "./ephemeris.ts";
|
|
6
7
|
import { type ScaleMode } from "./scale.ts";
|
|
7
8
|
import type { SatelliteRef } from "../satellites/types.ts";
|
|
@@ -11,7 +12,7 @@ type Three = ThreeParts;
|
|
|
11
12
|
/** The camera's distance, in Earth radii, at which the Earth fills the view at zoom 1. */
|
|
12
13
|
export declare const FIT_DISTANCE = 3.4;
|
|
13
14
|
/**
|
|
14
|
-
* The
|
|
15
|
+
* The eleven bodies the space view places, names and lets a reader choose: the Earth, the Sun, the Moon, the seven other planets and Pluto.
|
|
15
16
|
*
|
|
16
17
|
* @example
|
|
17
18
|
* ```ts no-run
|
|
@@ -20,7 +21,7 @@ export declare const FIT_DISTANCE = 3.4;
|
|
|
20
21
|
* console.log(BODY_NAMES.join(", "));
|
|
21
22
|
* ```
|
|
22
23
|
*/
|
|
23
|
-
export declare const BODY_NAMES: readonly ["Earth", "Sun", "Moon", "Mercury", "Venus", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"];
|
|
24
|
+
export declare const BODY_NAMES: readonly ["Earth", "Sun", "Moon", "Mercury", "Venus", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"];
|
|
24
25
|
/**
|
|
25
26
|
* The name of one of the bodies in `BODY_NAMES`.
|
|
26
27
|
*
|
|
@@ -83,12 +84,21 @@ export interface SpaceModel {
|
|
|
83
84
|
stepMs: number;
|
|
84
85
|
/** The body the camera looks at, and (in `zoom`) fills the view with at 1x. */
|
|
85
86
|
focus: BodyName;
|
|
87
|
+
/** How the Earth is lit (see `LightMode`); the Sun, the Moon and the planets keep their true places either way. */
|
|
88
|
+
light: LightMode;
|
|
89
|
+
/** How the painter draws the Sun, the planets and their moons (one of its `looks`, or `plain` for the sphere in the right colour); empty for its first. */
|
|
90
|
+
bodies: string;
|
|
86
91
|
/** Filled at each draw. */
|
|
87
92
|
drawn: {
|
|
88
93
|
frame: Frame;
|
|
89
94
|
distance: number;
|
|
90
95
|
radiusPx: number;
|
|
91
96
|
} | null;
|
|
97
|
+
/** What the painter has made now: the Sun and planets with a drawing, and the moons shown (set at each draw). */
|
|
98
|
+
painted: {
|
|
99
|
+
bodies: number;
|
|
100
|
+
moons: number;
|
|
101
|
+
};
|
|
92
102
|
/** How many times astronomy-engine has been asked for a frame. */
|
|
93
103
|
lookups: number;
|
|
94
104
|
/** Game seconds a second (the clock's speed). */
|
|
@@ -145,13 +155,59 @@ export interface BodyDrawing {
|
|
|
145
155
|
object: THREE.Object3D;
|
|
146
156
|
/** Told at each draw the unit direction from the body to the Sun, in scene axes (x to the vernal equinox, y north). */
|
|
147
157
|
light?(toSun: Vec3): void;
|
|
158
|
+
/** Told at each draw, after the object is placed and scaled: the direction to the Sun, the moment shown, and how many CSS pixels across its radius the body is (a sharper picture is worth fetching only for a big one). */
|
|
159
|
+
update?(info: {
|
|
160
|
+
toSun: Vec3;
|
|
161
|
+
ms: number;
|
|
162
|
+
radiusPx: number;
|
|
163
|
+
}): void;
|
|
148
164
|
/** True to have the scene turn the object to face the camera (a disc or a glow). */
|
|
149
165
|
billboard?: boolean;
|
|
150
166
|
dispose(): void;
|
|
151
167
|
}
|
|
152
168
|
/**
|
|
153
|
-
*
|
|
154
|
-
*
|
|
169
|
+
* What the scene tells a painter when it makes a body.
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```ts no-run
|
|
173
|
+
* import type { PaintContext } from "@johnmorrisdotca/chikyu-earth/space";
|
|
174
|
+
*
|
|
175
|
+
* const context: PaintContext = { look: "painted", invalidate: () => undefined, ratio: 2, maxTexture: 2048 };
|
|
176
|
+
* console.log(context.look);
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
export interface PaintContext {
|
|
180
|
+
/** The look asked for: one of the painter's `looks`, or empty for its first. */
|
|
181
|
+
look: string;
|
|
182
|
+
/** Ask for a new draw (a picture has arrived). */
|
|
183
|
+
invalidate(): void;
|
|
184
|
+
/** Device pixels to a CSS pixel. */
|
|
185
|
+
ratio: number;
|
|
186
|
+
/** The widest picture, in pixels, the GPU should be given. */
|
|
187
|
+
maxTexture: number;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* One moon of a planet at a moment: its name, where it is from the planet's centre and how big it is, both in radii of the planet, in scene axes.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```ts no-run
|
|
194
|
+
* import type { BodyMoon } from "@johnmorrisdotca/chikyu-earth/space";
|
|
195
|
+
*
|
|
196
|
+
* const io: BodyMoon = { name: "Io", offset: [5.9, 0, 0], radius: 0.0255 };
|
|
197
|
+
* console.log(io.name);
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
export interface BodyMoon {
|
|
201
|
+
name: string;
|
|
202
|
+
offset: Readonly<Vec3>;
|
|
203
|
+
radius: number;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* The seam for drawing the Sun, the planets and their moons. The space view places them, sizes them, lights them and labels them; a painter only makes what is drawn at
|
|
207
|
+
* each place. The default (`plainPainter`) is a sphere in the body's colour, and a glow for the Sun. Another package can supply better looking ones:
|
|
208
|
+
* `@johnmorrisdotca/wakusei-planets/painter` draws the real Sun, planets, Pluto and moons.
|
|
209
|
+
* `make` may return a drawing at once or later (a promise); a drawing may update itself and ask for a redraw when a picture arrives. `moons` is optional: the moons the painter can
|
|
210
|
+
* draw for a planet at a moment, which the view shows round the planet when it is near enough to see them. `looks` names what `context.look` may be.
|
|
155
211
|
*
|
|
156
212
|
* @example
|
|
157
213
|
* ```ts no-run
|
|
@@ -162,7 +218,9 @@ export interface BodyDrawing {
|
|
|
162
218
|
* ```
|
|
163
219
|
*/
|
|
164
220
|
export interface BodyPainter {
|
|
165
|
-
make(three: ThreeParts, name:
|
|
221
|
+
make(three: ThreeParts, name: string, context?: PaintContext): BodyDrawing | Promise<BodyDrawing>;
|
|
222
|
+
moons?(planet: PlanetName, ms: number): readonly BodyMoon[];
|
|
223
|
+
looks?: readonly string[];
|
|
166
224
|
}
|
|
167
225
|
export interface SpaceDeps {
|
|
168
226
|
assets: AssetCache;
|
|
@@ -184,8 +242,10 @@ export interface SpaceDeps {
|
|
|
184
242
|
onRideLost?: () => void;
|
|
185
243
|
/** The satellites could not be drawn (the three.js parts handed over lack `Points`). */
|
|
186
244
|
onSatellitesError?: (error: unknown) => void;
|
|
187
|
-
/** How the Sun
|
|
245
|
+
/** How the Sun, the planets and their moons are drawn; left out, plain spheres in the right colours (see `BodyPainter`). */
|
|
188
246
|
painter?: BodyPainter;
|
|
247
|
+
/** The painter could not make a body (the three.js parts handed over lack something it needs); the body is drawn plain instead. */
|
|
248
|
+
onPainterError?: (error: unknown) => void;
|
|
189
249
|
}
|
|
190
250
|
/**
|
|
191
251
|
* The default look: a sphere in the planet's colour, lit by the Sun, and for the Sun a glowing disc.
|
|
@@ -197,6 +257,8 @@ export interface SpaceDeps {
|
|
|
197
257
|
* console.log(typeof plainPainter.make);
|
|
198
258
|
* ```
|
|
199
259
|
*/
|
|
200
|
-
export declare const plainPainter:
|
|
260
|
+
export declare const plainPainter: {
|
|
261
|
+
make(three: ThreeParts, name: string): BodyDrawing;
|
|
262
|
+
} & BodyPainter;
|
|
201
263
|
export declare function createSpaceItem(model: SpaceModel, deps: SpaceDeps): Item3D<Three>;
|
|
202
264
|
export {};
|
package/dist/space/scene.js
CHANGED
|
@@ -10,9 +10,14 @@ const RAD = Math.PI / 180;
|
|
|
10
10
|
export const FIT_DISTANCE = 3.4;
|
|
11
11
|
const FOV = 35;
|
|
12
12
|
const LAYERS = ["day", "night", "clouds", "water", "moon"];
|
|
13
|
-
|
|
13
|
+
/** The moons of a planet are shown once the planet is this many CSS pixels across its radius, and each is drawn at least this many pixels across its radius. */
|
|
14
|
+
const MOON_MIN_PLANET_PX = 4;
|
|
15
|
+
const MOON_MIN_PX = 2.2;
|
|
16
|
+
/** A moon that has been out of sight this long is let go (its pictures too). */
|
|
17
|
+
const MOON_KEEP_MS = 4000;
|
|
18
|
+
const PLAIN_COLOR = (name) => (name === "Sun" ? "#ffb347" : COLORS[name] ?? "#9a948b");
|
|
14
19
|
/**
|
|
15
|
-
* The
|
|
20
|
+
* The eleven bodies the space view places, names and lets a reader choose: the Earth, the Sun, the Moon, the seven other planets and Pluto.
|
|
16
21
|
*
|
|
17
22
|
* @example
|
|
18
23
|
* ```ts no-run
|
|
@@ -22,8 +27,8 @@ const PLAIN_COLOR = (name) => (name === "Sun" ? "#ffb347" : COLORS[name]);
|
|
|
22
27
|
* ```
|
|
23
28
|
*/
|
|
24
29
|
export const BODY_NAMES = ["Earth", "Sun", "Moon", ...PLANETS];
|
|
25
|
-
const COLORS = { Mercury: "#a79f96", Venus: "#e3c98a", Mars: "#c1593a", Jupiter: "#d2b48c", Saturn: "#e4d4a0", Uranus: "#9fd6dc", Neptune: "#4a6bd6" };
|
|
26
|
-
const ORBIT_COLORS = { Mercury: "#6b655f", Venus: "#8c7c55", Mars: "#7a4034", Jupiter: "#85735a", Saturn: "#8d8567", Uranus: "#5f8a8e", Neptune: "#34499a" };
|
|
30
|
+
const COLORS = { Mercury: "#a79f96", Venus: "#e3c98a", Mars: "#c1593a", Jupiter: "#d2b48c", Saturn: "#e4d4a0", Uranus: "#9fd6dc", Neptune: "#4a6bd6", Pluto: "#c9b8a6" };
|
|
31
|
+
const ORBIT_COLORS = { Mercury: "#6b655f", Venus: "#8c7c55", Mars: "#7a4034", Jupiter: "#85735a", Saturn: "#8d8567", Uranus: "#5f8a8e", Neptune: "#34499a", Pluto: "#7d7468" };
|
|
27
32
|
export const newSpaceModel = () => ({
|
|
28
33
|
time: Date.now(),
|
|
29
34
|
mode: "compressed",
|
|
@@ -34,7 +39,10 @@ export const newSpaceModel = () => ({
|
|
|
34
39
|
labels: true,
|
|
35
40
|
stepMs: 1000,
|
|
36
41
|
focus: "Earth",
|
|
42
|
+
bodies: "",
|
|
43
|
+
light: "sun",
|
|
37
44
|
drawn: null,
|
|
45
|
+
painted: { bodies: 0, moons: 0 },
|
|
38
46
|
lookups: 0,
|
|
39
47
|
speed: 1,
|
|
40
48
|
ride: null,
|
|
@@ -130,7 +138,7 @@ export function createSpaceItem(model, deps) {
|
|
|
130
138
|
const earthMaterial = new three.ShaderMaterial({
|
|
131
139
|
vertexShader: SURFACE_VERT,
|
|
132
140
|
fragmentShader: SURFACE_FRAG,
|
|
133
|
-
uniforms: { dayMap: uniform.day, nightMap: uniform.night, waterMap: uniform.water, sunDir: sunEarth, lights: { value: 1 }, ortho: { value: 0 } },
|
|
141
|
+
uniforms: { dayMap: uniform.day, nightMap: uniform.night, waterMap: uniform.water, sunDir: sunEarth, lights: { value: 1 }, boost: { value: 0 }, ortho: { value: 0 } },
|
|
134
142
|
});
|
|
135
143
|
const cloudShift = { value: 0 };
|
|
136
144
|
const cloudMaterial = new three.ShaderMaterial({
|
|
@@ -161,19 +169,67 @@ export function createSpaceItem(model, deps) {
|
|
|
161
169
|
const painter = deps.painter ?? plainPainter;
|
|
162
170
|
const scene = new three.Scene();
|
|
163
171
|
scene.add(earth, clouds, glow, moon);
|
|
164
|
-
|
|
165
|
-
|
|
172
|
+
// the Sun, the planets and the moons are made by the painter, one slot each: a slot is filled again when the look changes, and may be filled later (a painter may be slow)
|
|
173
|
+
let ended = false;
|
|
174
|
+
const newSlot = (name) => ({ name, drawing: undefined, look: null, token: 0, plain: false });
|
|
175
|
+
const lookNow = () => model.bodies || painter.looks?.[0] || "";
|
|
176
|
+
const empty = (slot) => {
|
|
177
|
+
slot.token += 1;
|
|
178
|
+
if (!slot.drawing)
|
|
179
|
+
return;
|
|
180
|
+
scene.remove(slot.drawing.object);
|
|
181
|
+
slot.drawing.dispose();
|
|
182
|
+
slot.drawing = undefined;
|
|
183
|
+
};
|
|
184
|
+
const fill = (slot, isMoon = false) => {
|
|
185
|
+
const look = lookNow();
|
|
186
|
+
if (slot.look === look)
|
|
187
|
+
return;
|
|
188
|
+
empty(slot);
|
|
189
|
+
slot.look = look;
|
|
190
|
+
const token = slot.token;
|
|
191
|
+
const arrive = (made) => {
|
|
192
|
+
if (ended || slot.token !== token) {
|
|
193
|
+
made.dispose();
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
slot.drawing = made;
|
|
197
|
+
scene.add(made.object);
|
|
198
|
+
deps.invalidate();
|
|
199
|
+
};
|
|
200
|
+
const fail = (error) => {
|
|
201
|
+
if (ended || slot.token !== token)
|
|
202
|
+
return;
|
|
203
|
+
deps.onPainterError?.(error);
|
|
204
|
+
if (!isMoon && !slot.plain) {
|
|
205
|
+
slot.plain = true;
|
|
206
|
+
slot.look = null;
|
|
207
|
+
fill(slot);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
try {
|
|
211
|
+
const made = (slot.plain || look === "plain" ? plainPainter : painter).make(three, slot.name, { look, invalidate: () => deps.invalidate(), ratio: model.ratio(), maxTexture: deps.cap() });
|
|
212
|
+
if (typeof made.then === "function")
|
|
213
|
+
made.then(arrive, fail);
|
|
214
|
+
else
|
|
215
|
+
arrive(made);
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
fail(error);
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
const sunSlot = newSlot("Sun");
|
|
166
222
|
const planets = new Map();
|
|
167
223
|
for (const name of PLANETS) {
|
|
168
|
-
const drawing = painter.make(three, name);
|
|
169
224
|
const positions = new Float32Array(96 * 3);
|
|
170
225
|
const lineGeometry = new three.BufferGeometry();
|
|
171
226
|
lineGeometry.setAttribute("position", new three.BufferAttribute(positions, 3));
|
|
172
227
|
const orbit = new three.LineLoop(lineGeometry, new three.LineBasicMaterial({ color: ORBIT_COLORS[name], transparent: true, opacity: 0.7 }));
|
|
173
228
|
orbit.frustumCulled = false;
|
|
174
|
-
scene.add(
|
|
175
|
-
planets.set(name, {
|
|
229
|
+
scene.add(orbit);
|
|
230
|
+
planets.set(name, { slot: newSlot(name), orbit, positions, samples: [], epoch: Number.NaN, toSun: [1, 0, 0] });
|
|
176
231
|
}
|
|
232
|
+
const moonSlots = new Map();
|
|
177
233
|
let layer;
|
|
178
234
|
const rideState = new Float64Array(6);
|
|
179
235
|
let selectedShown = false;
|
|
@@ -215,7 +271,8 @@ export function createSpaceItem(model, deps) {
|
|
|
215
271
|
const toSun = unit(frame.sun);
|
|
216
272
|
sunEarth.value.set(toSun[0], toSun[1], toSun[2]);
|
|
217
273
|
cloudShift.value = -(((model.time / 3_600_000) * 1.5) % 360) / 360;
|
|
218
|
-
earthMaterial.uniforms["lights"].value = 1;
|
|
274
|
+
earthMaterial.uniforms["lights"].value = model.light === "lit" ? 0 : 1;
|
|
275
|
+
earthMaterial.uniforms["boost"].value = model.light === "boost" ? 1 : 0;
|
|
219
276
|
// the Moon, with its near side to the Earth
|
|
220
277
|
moon.position.set(moonPos[0], moonPos[1], moonPos[2]);
|
|
221
278
|
moon.scale.setScalar(RADIUS["Moon"][mode]);
|
|
@@ -229,15 +286,23 @@ export function createSpaceItem(model, deps) {
|
|
|
229
286
|
pos[name] = placeAround(mode, sunPos, helioOf(frame.planets[name]));
|
|
230
287
|
const radiusOf = (name) => (name === "Earth" ? 1 : RADIUS[name][mode]);
|
|
231
288
|
// the Sun and the planets, drawn by the painter, and the planets' orbits round the Sun
|
|
232
|
-
|
|
233
|
-
sunDrawing.
|
|
234
|
-
if (sunDrawing
|
|
235
|
-
sunDrawing.object.
|
|
289
|
+
fill(sunSlot);
|
|
290
|
+
const sunDrawing = sunSlot.drawing;
|
|
291
|
+
if (sunDrawing) {
|
|
292
|
+
sunDrawing.object.position.set(sunPos[0], sunPos[1], sunPos[2]);
|
|
293
|
+
sunDrawing.object.scale.setScalar(radiusOf("Sun"));
|
|
294
|
+
if (sunDrawing.billboard)
|
|
295
|
+
sunDrawing.object.quaternion.copy(camera.quaternion);
|
|
296
|
+
}
|
|
236
297
|
for (const name of PLANETS) {
|
|
237
298
|
const p = planets.get(name);
|
|
238
|
-
p.
|
|
239
|
-
p.
|
|
240
|
-
p.drawing
|
|
299
|
+
fill(p.slot);
|
|
300
|
+
p.toSun = unit([frame.sun[0] - frame.planets[name][0], frame.sun[1] - frame.planets[name][1], frame.sun[2] - frame.planets[name][2]]);
|
|
301
|
+
if (p.slot.drawing) {
|
|
302
|
+
p.slot.drawing.object.position.set(pos[name][0], pos[name][1], pos[name][2]);
|
|
303
|
+
p.slot.drawing.object.scale.setScalar(radiusOf(name));
|
|
304
|
+
p.slot.drawing.light?.(p.toSun);
|
|
305
|
+
}
|
|
241
306
|
p.orbit.visible = model.orbits;
|
|
242
307
|
if (model.orbits) {
|
|
243
308
|
if (!Number.isFinite(p.epoch) || Math.abs(model.time - p.epoch) > 365 * 86_400_000) {
|
|
@@ -302,9 +367,11 @@ export function createSpaceItem(model, deps) {
|
|
|
302
367
|
}
|
|
303
368
|
const hidden = rideFrom?.hidden ?? null;
|
|
304
369
|
moon.visible = hidden !== "Moon";
|
|
305
|
-
sunDrawing
|
|
370
|
+
if (sunDrawing)
|
|
371
|
+
sunDrawing.object.visible = hidden !== "Sun";
|
|
306
372
|
for (const [name, p] of planets)
|
|
307
|
-
p.drawing
|
|
373
|
+
if (p.slot.drawing)
|
|
374
|
+
p.slot.drawing.object.visible = hidden !== name;
|
|
308
375
|
let fovNow = FOV;
|
|
309
376
|
let distance = 0;
|
|
310
377
|
if (rideFrom) {
|
|
@@ -357,6 +424,9 @@ export function createSpaceItem(model, deps) {
|
|
|
357
424
|
camera.aspect = size.width / Math.max(1, size.height);
|
|
358
425
|
camera.updateProjectionMatrix();
|
|
359
426
|
camera.updateMatrixWorld();
|
|
427
|
+
// always lit: the light comes from the camera's side of the Earth (the Sun, Moon and planets stay where they are)
|
|
428
|
+
if (model.light === "lit")
|
|
429
|
+
sunEarth.value.set(camera.position.x, camera.position.y, camera.position.z).normalize();
|
|
360
430
|
sunView.value.copy(sunEarth.value).transformDirection(camera.matrixWorldInverse);
|
|
361
431
|
const pose = model.pose;
|
|
362
432
|
pose.p[0] = camera.position.x;
|
|
@@ -381,6 +451,65 @@ export function createSpaceItem(model, deps) {
|
|
|
381
451
|
for (const name of BODY_NAMES)
|
|
382
452
|
px[name] = radiusPxOf(name);
|
|
383
453
|
model.drawn = { frame, distance, radiusPx: px.Earth };
|
|
454
|
+
let paintedBodies = sunSlot.drawing ? 1 : 0;
|
|
455
|
+
for (const p of planets.values())
|
|
456
|
+
if (p.slot.drawing)
|
|
457
|
+
paintedBodies += 1;
|
|
458
|
+
let paintedMoons = 0;
|
|
459
|
+
for (const held of moonSlots.values())
|
|
460
|
+
if (held.slot.drawing?.object.visible)
|
|
461
|
+
paintedMoons += 1;
|
|
462
|
+
model.painted = { bodies: paintedBodies, moons: paintedMoons };
|
|
463
|
+
const sunToSun = unit([-frame.sun[0], -frame.sun[1], -frame.sun[2]]);
|
|
464
|
+
sunDrawing?.update?.({ toSun: sunToSun, ms: model.time, radiusPx: px.Sun });
|
|
465
|
+
for (const name of PLANETS) {
|
|
466
|
+
const p = planets.get(name);
|
|
467
|
+
p.slot.drawing?.update?.({ toSun: p.toSun, ms: model.time, radiusPx: px[name] });
|
|
468
|
+
}
|
|
469
|
+
if (painter.moons && lookNow() !== "plain") {
|
|
470
|
+
const now = performance.now();
|
|
471
|
+
const seen = new Set();
|
|
472
|
+
for (const name of PLANETS) {
|
|
473
|
+
const p = planets.get(name);
|
|
474
|
+
const planet = p.slot.drawing;
|
|
475
|
+
if (!planet || !planet.object.visible || px[name] < MOON_MIN_PLANET_PX)
|
|
476
|
+
continue;
|
|
477
|
+
const radius = radiusOf(name);
|
|
478
|
+
for (const site of painter.moons(name, model.time)) {
|
|
479
|
+
const at = [pos[name][0] + site.offset[0] * radius, pos[name][1] + site.offset[1] * radius, pos[name][2] + site.offset[2] * radius];
|
|
480
|
+
projected.set(at[0], at[1], at[2]).project(camera);
|
|
481
|
+
if (projected.z > 1 || Math.abs(projected.x) > 1.25 || Math.abs(projected.y) > 1.25)
|
|
482
|
+
continue;
|
|
483
|
+
let held = moonSlots.get(site.name);
|
|
484
|
+
if (!held)
|
|
485
|
+
moonSlots.set(site.name, (held = { slot: newSlot(site.name), seen: now }));
|
|
486
|
+
held.seen = now;
|
|
487
|
+
seen.add(site.name);
|
|
488
|
+
fill(held.slot, true);
|
|
489
|
+
const drawing = held.slot.drawing;
|
|
490
|
+
if (!drawing)
|
|
491
|
+
continue;
|
|
492
|
+
const away = Math.hypot(at[0] - camera.position.x, at[1] - camera.position.y, at[2] - camera.position.z);
|
|
493
|
+
const drawn = Math.max(site.radius * radius, (away * half * MOON_MIN_PX) / (size.height / 2));
|
|
494
|
+
drawing.object.visible = true;
|
|
495
|
+
drawing.object.position.set(at[0], at[1], at[2]);
|
|
496
|
+
drawing.object.scale.setScalar(drawn);
|
|
497
|
+
const moonPx = away <= drawn * 1.001 ? 1e5 : ((size.height / 2) * (drawn / Math.sqrt(away * away - drawn * drawn))) / half;
|
|
498
|
+
drawing.light?.(p.toSun);
|
|
499
|
+
drawing.update?.({ toSun: p.toSun, ms: model.time, radiusPx: moonPx });
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
for (const [key, held] of moonSlots) {
|
|
503
|
+
if (seen.has(key))
|
|
504
|
+
continue;
|
|
505
|
+
if (held.slot.drawing)
|
|
506
|
+
held.slot.drawing.object.visible = false;
|
|
507
|
+
if (now - held.seen > MOON_KEEP_MS) {
|
|
508
|
+
empty(held.slot);
|
|
509
|
+
moonSlots.delete(key);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
384
513
|
const ratio = model.ratio();
|
|
385
514
|
const earthPx = Math.min(px.Earth, 1e5) * ratio;
|
|
386
515
|
textures.sync({ day: earthPx, night: earthPx, clouds: earthPx, water: earthPx, moon: Math.min(px.Moon, 1e5) * ratio });
|
|
@@ -411,9 +540,13 @@ export function createSpaceItem(model, deps) {
|
|
|
411
540
|
layer?.dispose();
|
|
412
541
|
model.pick = null;
|
|
413
542
|
textures.dispose();
|
|
414
|
-
|
|
543
|
+
ended = true;
|
|
544
|
+
empty(sunSlot);
|
|
545
|
+
for (const held of moonSlots.values())
|
|
546
|
+
empty(held.slot);
|
|
547
|
+
moonSlots.clear();
|
|
415
548
|
for (const p of planets.values()) {
|
|
416
|
-
p.
|
|
549
|
+
empty(p.slot);
|
|
417
550
|
p.orbit.geometry.dispose();
|
|
418
551
|
p.orbit.material.dispose();
|
|
419
552
|
}
|