@johnmorrisdotca/chikyu-earth 0.7.0 → 0.9.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 +63 -14
- package/NOTICE.md +11 -0
- package/README.md +22 -0
- package/dist/realistic/textures.d.ts +3 -0
- package/dist/satellites/index.d.ts +3 -0
- package/dist/satellites/index.js +3 -0
- package/dist/satellites/limits.d.ts +22 -0
- package/dist/satellites/limits.js +23 -0
- package/dist/satellites/track.d.ts +50 -0
- package/dist/satellites/track.js +227 -0
- package/dist/satellites/types.d.ts +161 -0
- package/dist/satellites/types.js +3 -0
- package/dist/space/element.d.ts +114 -4
- package/dist/space/element.js +228 -11
- package/dist/space/ephemeris.d.ts +3 -1
- package/dist/space/ephemeris.js +5 -1
- package/dist/space/index.d.ts +1 -0
- package/dist/space/index.js +1 -0
- package/dist/space/ride.d.ts +83 -0
- package/dist/space/ride.js +57 -0
- package/dist/space/satellite-layer.d.ts +53 -0
- package/dist/space/satellite-layer.js +230 -0
- package/dist/space/scene.d.ts +71 -0
- package/dist/space/scene.js +149 -16
- package/package.json +20 -4
|
@@ -112,7 +112,7 @@ export declare function blend(a: Frame, b: Frame, t: number): Frame;
|
|
|
112
112
|
* `stepMs` ahead, so playing time at a steady rate costs one pair of lookups every `stepMs` and every draw in between is arithmetic.
|
|
113
113
|
*
|
|
114
114
|
* @param initialStepMs - How far ahead the second answer looks, in milliseconds.
|
|
115
|
-
* @returns A track: `at(ms)` for a frame, `lookups` for how many times the ephemeris was asked, `reset()` and `setStep(ms)`.
|
|
115
|
+
* @returns A track: `at(ms)` for a frame, `lookups` for how many times the ephemeris was asked, `ends()` for the two answers it is between (undefined before the first `at`), `reset()` and `setStep(ms)`.
|
|
116
116
|
*
|
|
117
117
|
* @example
|
|
118
118
|
* ```ts
|
|
@@ -126,6 +126,8 @@ export declare function blend(a: Frame, b: Frame, t: number): Frame;
|
|
|
126
126
|
*/
|
|
127
127
|
export declare function createTrack(initialStepMs?: number): {
|
|
128
128
|
at(ms: number): Frame;
|
|
129
|
+
/** The two answers the track is between now (undefined before the first), for working out which way something is going. */
|
|
130
|
+
ends(): [Frame, Frame] | undefined;
|
|
129
131
|
/** How many times astronomy-engine has been asked for a frame. */
|
|
130
132
|
readonly lookups: number;
|
|
131
133
|
reset(): void;
|
package/dist/space/ephemeris.js
CHANGED
|
@@ -95,7 +95,7 @@ export function blend(a, b, t) {
|
|
|
95
95
|
* `stepMs` ahead, so playing time at a steady rate costs one pair of lookups every `stepMs` and every draw in between is arithmetic.
|
|
96
96
|
*
|
|
97
97
|
* @param initialStepMs - How far ahead the second answer looks, in milliseconds.
|
|
98
|
-
* @returns A track: `at(ms)` for a frame, `lookups` for how many times the ephemeris was asked, `reset()` and `setStep(ms)`.
|
|
98
|
+
* @returns A track: `at(ms)` for a frame, `lookups` for how many times the ephemeris was asked, `ends()` for the two answers it is between (undefined before the first `at`), `reset()` and `setStep(ms)`.
|
|
99
99
|
*
|
|
100
100
|
* @example
|
|
101
101
|
* ```ts
|
|
@@ -121,6 +121,10 @@ export function createTrack(initialStepMs = 1000) {
|
|
|
121
121
|
}
|
|
122
122
|
return blend(a, b, (ms - a.ms) / (b.ms - a.ms));
|
|
123
123
|
},
|
|
124
|
+
/** The two answers the track is between now (undefined before the first), for working out which way something is going. */
|
|
125
|
+
ends() {
|
|
126
|
+
return a && b ? [a, b] : undefined;
|
|
127
|
+
},
|
|
124
128
|
/** How many times astronomy-engine has been asked for a frame. */
|
|
125
129
|
get lookups() {
|
|
126
130
|
return lookups;
|
package/dist/space/index.d.ts
CHANGED
|
@@ -1,3 +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
3
|
export { plainPainter, type BodyDrawing, type BodyPainter } from "./scene.ts";
|
|
4
|
+
export { RIDE_FOV, ridePose, type RideInput, type RideLook, type RidePose } from "./ride.ts";
|
package/dist/space/index.js
CHANGED
|
@@ -3,3 +3,4 @@
|
|
|
3
3
|
export { AU_IN_EARTH_RADII, blend, createTrack, EARTH_RADIUS_KM, frameAt, orbitPath, PLANETS } from "./ephemeris.js";
|
|
4
4
|
export { compress, MOON_COMPRESSED, placeAround, placeMoon, placeSun, RADIUS, SUN_COMPRESSED, truePosition } from "./scale.js";
|
|
5
5
|
export { plainPainter } from "./scene.js";
|
|
6
|
+
export { RIDE_FOV, ridePose } from "./ride.js";
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { Vec3 } from "./ephemeris.ts";
|
|
2
|
+
/**
|
|
3
|
+
* The field of view, in degrees, of a camera riding along at zoom 1 (the Earth's view from outside is 35).
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { RIDE_FOV } from "@johnmorrisdotca/chikyu-earth/space";
|
|
8
|
+
*
|
|
9
|
+
* console.log(RIDE_FOV);
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare const RIDE_FOV = 60;
|
|
13
|
+
/**
|
|
14
|
+
* Where a riding camera looks: `down` straight at the Earth's centre (the ground goes by under the camera), or `horizon` tipped forward along the way it is going until the horizon
|
|
15
|
+
* is a little above the middle of the view.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import type { RideLook } from "@johnmorrisdotca/chikyu-earth/space";
|
|
20
|
+
*
|
|
21
|
+
* const looks: RideLook[] = ["down", "horizon"];
|
|
22
|
+
* console.log(looks.join(" or "));
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export type RideLook = "down" | "horizon";
|
|
26
|
+
/**
|
|
27
|
+
* What `ridePose` is asked: the camera's place and its way of travel (any length), where it looks, and the shared controls' yaw, tilt and zoom.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import type { RideInput } from "@johnmorrisdotca/chikyu-earth/space";
|
|
32
|
+
*
|
|
33
|
+
* const input: RideInput = { position: [1.066, 0, 0], velocity: [0, 0, -7.66], look: "down", yaw: 0, tilt: 0, zoom: 1 };
|
|
34
|
+
* console.log(input.look);
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export interface RideInput {
|
|
38
|
+
/** The camera's place in Earth radii from the Earth's centre, in the scene's axes. */
|
|
39
|
+
position: Vec3;
|
|
40
|
+
/** Its direction of travel, in any unit: only the direction is used. */
|
|
41
|
+
velocity: Vec3;
|
|
42
|
+
look: RideLook;
|
|
43
|
+
/** Degrees: turns the view round the straight-down line (0 has the way it is going at the top of the picture). */
|
|
44
|
+
yaw: number;
|
|
45
|
+
/** Degrees: tips the view forward from where `look` puts it, towards the horizon and beyond. */
|
|
46
|
+
tilt: number;
|
|
47
|
+
/** 1 is the natural view; more narrows the field of view. */
|
|
48
|
+
zoom: number;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The camera `ridePose` works out: where it is, which way it looks, which way is up in the picture, its field of view in degrees, and how big the Earth looks from there (its angular radius in degrees).
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* import type { RidePose } from "@johnmorrisdotca/chikyu-earth/space";
|
|
56
|
+
*
|
|
57
|
+
* const describe = (pose: RidePose): string => `looking ${pose.forward.map((n) => n.toFixed(2)).join(",")} with a ${pose.fov} degree view`;
|
|
58
|
+
* console.log(typeof describe);
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export interface RidePose {
|
|
62
|
+
position: Vec3;
|
|
63
|
+
forward: Vec3;
|
|
64
|
+
up: Vec3;
|
|
65
|
+
fov: number;
|
|
66
|
+
earthRadiusDegrees: number;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The camera of a body-borne view: at `position`, looking at the Earth, with the way the body is going at the top of the picture. `look: "horizon"` tips it forward until the
|
|
70
|
+
* horizon is a little above the middle; `tilt` tips it further, `yaw` turns it round the straight-down line, and `zoom` narrows the field of view.
|
|
71
|
+
*
|
|
72
|
+
* @param input - The camera's place, its way of travel, the look, and the shared controls' yaw, tilt and zoom.
|
|
73
|
+
* @returns The camera's place, forward and up directions (unit vectors), its field of view, and the Earth's angular radius from there.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* import { ridePose } from "@johnmorrisdotca/chikyu-earth/space";
|
|
78
|
+
*
|
|
79
|
+
* const pose = ridePose({ position: [1.066, 0, 0], velocity: [0, 0, -7.66], look: "down", yaw: 0, tilt: 0, zoom: 1 });
|
|
80
|
+
* console.log(pose.forward.map((n) => n.toFixed(2)).join(","), pose.earthRadiusDegrees.toFixed(0));
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export declare function ridePose(input: RideInput): RidePose;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const RAD = Math.PI / 180;
|
|
2
|
+
/**
|
|
3
|
+
* The field of view, in degrees, of a camera riding along at zoom 1 (the Earth's view from outside is 35).
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { RIDE_FOV } from "@johnmorrisdotca/chikyu-earth/space";
|
|
8
|
+
*
|
|
9
|
+
* console.log(RIDE_FOV);
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export const RIDE_FOV = 60;
|
|
13
|
+
const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
14
|
+
const cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
|
|
15
|
+
const scale = (a, k) => [a[0] * k, a[1] * k, a[2] * k];
|
|
16
|
+
const add = (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
|
|
17
|
+
const norm = (a) => Math.hypot(a[0], a[1], a[2]);
|
|
18
|
+
const unit = (a) => {
|
|
19
|
+
const n = norm(a);
|
|
20
|
+
return n === 0 ? [0, 0, 0] : [a[0] / n, a[1] / n, a[2] / n];
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* The camera of a body-borne view: at `position`, looking at the Earth, with the way the body is going at the top of the picture. `look: "horizon"` tips it forward until the
|
|
24
|
+
* horizon is a little above the middle; `tilt` tips it further, `yaw` turns it round the straight-down line, and `zoom` narrows the field of view.
|
|
25
|
+
*
|
|
26
|
+
* @param input - The camera's place, its way of travel, the look, and the shared controls' yaw, tilt and zoom.
|
|
27
|
+
* @returns The camera's place, forward and up directions (unit vectors), its field of view, and the Earth's angular radius from there.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { ridePose } from "@johnmorrisdotca/chikyu-earth/space";
|
|
32
|
+
*
|
|
33
|
+
* const pose = ridePose({ position: [1.066, 0, 0], velocity: [0, 0, -7.66], look: "down", yaw: 0, tilt: 0, zoom: 1 });
|
|
34
|
+
* console.log(pose.forward.map((n) => n.toFixed(2)).join(","), pose.earthRadiusDegrees.toFixed(0));
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export function ridePose(input) {
|
|
38
|
+
const distance = Math.max(1e-9, norm(input.position));
|
|
39
|
+
const nadir = scale(input.position, -1 / distance);
|
|
40
|
+
let ahead = add(input.velocity, scale(nadir, -dot(input.velocity, nadir)));
|
|
41
|
+
if (norm(ahead) < 1e-12 * Math.max(1, norm(input.velocity))) {
|
|
42
|
+
// no way of travel to go by (or it is straight at the Earth): north, or east where north is straight down
|
|
43
|
+
const north = [0, 1, 0];
|
|
44
|
+
ahead = add(north, scale(nadir, -dot(north, nadir)));
|
|
45
|
+
if (norm(ahead) < 1e-9)
|
|
46
|
+
ahead = cross([1, 0, 0], nadir);
|
|
47
|
+
}
|
|
48
|
+
ahead = unit(ahead);
|
|
49
|
+
const turn = input.yaw * RAD;
|
|
50
|
+
ahead = add(scale(ahead, Math.cos(turn)), scale(cross(nadir, ahead), Math.sin(turn)));
|
|
51
|
+
const earthRadiusDegrees = distance <= 1 ? 90 : Math.asin(1 / distance) / RAD;
|
|
52
|
+
const base = input.look === "horizon" ? Math.max(0, earthRadiusDegrees - 12) : 0;
|
|
53
|
+
const pitch = Math.min(150, Math.max(0, base + input.tilt)) * RAD;
|
|
54
|
+
const forward = add(scale(nadir, Math.cos(pitch)), scale(ahead, Math.sin(pitch)));
|
|
55
|
+
const up = add(scale(ahead, Math.cos(pitch)), scale(nadir, -Math.sin(pitch)));
|
|
56
|
+
return { position: [input.position[0], input.position[1], input.position[2]], forward, up, fov: Math.min(100, Math.max(5, RIDE_FOV / Math.max(0.05, input.zoom))), earthRadiusDegrees };
|
|
57
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type * as THREE from "three";
|
|
2
|
+
import type { SatelliteRef, Satellites } from "../satellites/types.ts";
|
|
3
|
+
import type { ThreeParts } from "../realistic/textures.ts";
|
|
4
|
+
type Three = ThreeParts;
|
|
5
|
+
/** How a group looks: the colour of its dots and their size in CSS pixels, and whether every orbit is drawn (only for a small group). */
|
|
6
|
+
export interface SatelliteStyle {
|
|
7
|
+
color: string;
|
|
8
|
+
size: number;
|
|
9
|
+
orbits: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface SatelliteGroupState extends SatelliteStyle {
|
|
12
|
+
satellites: Satellites;
|
|
13
|
+
visible: boolean;
|
|
14
|
+
}
|
|
15
|
+
/** The part of the space view's model the satellites read and write. */
|
|
16
|
+
export interface SatelliteState {
|
|
17
|
+
groups: Map<string, SatelliteGroupState>;
|
|
18
|
+
selected: SatelliteRef | null;
|
|
19
|
+
/** Written at each draw: satellites placed, whether the clock is too fast for the groups, and the milliseconds the last update took. */
|
|
20
|
+
drawn: {
|
|
21
|
+
placed: number;
|
|
22
|
+
tooFast: boolean;
|
|
23
|
+
updateMs: number;
|
|
24
|
+
maxUpdateMs: number;
|
|
25
|
+
updates: number;
|
|
26
|
+
totalMs: number;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export declare const newSatelliteState: () => SatelliteState;
|
|
30
|
+
/** The most orbits a group may draw all at once, one line each. */
|
|
31
|
+
export declare const MAX_ORBIT_GROUP = 40;
|
|
32
|
+
export interface SatelliteLayer {
|
|
33
|
+
/** Place every shown group for `model.time` and the chosen satellite exactly. */
|
|
34
|
+
update(time: number, speed: number): void;
|
|
35
|
+
/** The satellite nearest to a point of the view (CSS pixels) within `radius`, if one is in front of the Earth. */
|
|
36
|
+
pick(x: number, y: number, camera: THREE.PerspectiveCamera, size: {
|
|
37
|
+
width: number;
|
|
38
|
+
height: number;
|
|
39
|
+
}, radius: number): SatelliteRef | null;
|
|
40
|
+
/** Where the chosen satellite is in the view, if it is. */
|
|
41
|
+
place(camera: THREE.PerspectiveCamera, size: {
|
|
42
|
+
width: number;
|
|
43
|
+
height: number;
|
|
44
|
+
}): {
|
|
45
|
+
x: number;
|
|
46
|
+
y: number;
|
|
47
|
+
} | null;
|
|
48
|
+
/** The chosen satellite's position and velocity now (Earth radii, km/s), or false. */
|
|
49
|
+
exact(ref: SatelliteRef, time: number, out: Float64Array): boolean;
|
|
50
|
+
dispose(): void;
|
|
51
|
+
}
|
|
52
|
+
export declare function createSatelliteLayer(three: Three, scene: THREE.Scene, state: SatelliteState, ratio: () => number): SatelliteLayer;
|
|
53
|
+
export {};
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { MAX_SATELLITE_SPEED } from "../satellites/limits.js";
|
|
2
|
+
export const newSatelliteState = () => ({ groups: new Map(), selected: null, drawn: { placed: 0, tooFast: false, updateMs: 0, maxUpdateMs: 0, updates: 0, totalMs: 0 } });
|
|
3
|
+
/** The most orbits a group may draw all at once, one line each. */
|
|
4
|
+
export const MAX_ORBIT_GROUP = 40;
|
|
5
|
+
const ORBIT_POINTS = 120;
|
|
6
|
+
const ORBIT_REFRESH_GAME_MS = 30 * 60_000;
|
|
7
|
+
const ORBIT_REFRESH_WALL_MS = 1000;
|
|
8
|
+
const POINT_VERT = /* glsl */ `
|
|
9
|
+
uniform float size;
|
|
10
|
+
void main() {
|
|
11
|
+
gl_PointSize = size;
|
|
12
|
+
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
|
|
13
|
+
}`;
|
|
14
|
+
const POINT_FRAG = /* glsl */ `
|
|
15
|
+
uniform vec3 tint;
|
|
16
|
+
uniform float ring;
|
|
17
|
+
void main() {
|
|
18
|
+
float d = length(gl_PointCoord - vec2(0.5));
|
|
19
|
+
if (d > 0.5) discard;
|
|
20
|
+
float a = ring > 0.5 ? smoothstep(0.30, 0.38, d) * (1.0 - smoothstep(0.44, 0.5, d)) : smoothstep(0.5, 0.2, d);
|
|
21
|
+
gl_FragColor = vec4(tint, a);
|
|
22
|
+
}`;
|
|
23
|
+
export function createSatelliteLayer(three, scene, state, ratio) {
|
|
24
|
+
if (!three.Points)
|
|
25
|
+
throw new Error("Satellites need `Points` from three.js: add it to the parts the three loader hands over (see setThreeLoader)");
|
|
26
|
+
const Points = three.Points;
|
|
27
|
+
const drawn = new Map();
|
|
28
|
+
const tmp = new Float64Array(6);
|
|
29
|
+
const projected = new three.Vector3();
|
|
30
|
+
const selectedMaterial = new three.ShaderMaterial({ vertexShader: POINT_VERT, fragmentShader: POINT_FRAG, uniforms: { size: { value: 14 }, tint: { value: new three.Color("#ffffff") }, ring: { value: 1 } }, transparent: true, depthWrite: false });
|
|
31
|
+
const selectedPosition = new Float32Array(3);
|
|
32
|
+
const selectedGeometry = new three.BufferGeometry();
|
|
33
|
+
selectedGeometry.setAttribute("position", new three.BufferAttribute(selectedPosition, 3));
|
|
34
|
+
const selectedPoints = new Points(selectedGeometry, selectedMaterial);
|
|
35
|
+
selectedPoints.frustumCulled = false;
|
|
36
|
+
selectedPoints.renderOrder = 6;
|
|
37
|
+
selectedPoints.visible = false;
|
|
38
|
+
scene.add(selectedPoints);
|
|
39
|
+
const selectedLine = { positions: new Float32Array(ORBIT_POINTS * 3), geometry: new three.BufferGeometry(), material: new three.LineBasicMaterial({ color: "#ffffff", transparent: true, opacity: 0.85 }) };
|
|
40
|
+
selectedLine.geometry.setAttribute("position", new three.BufferAttribute(selectedLine.positions, 3));
|
|
41
|
+
const selectedLoop = new three.LineLoop(selectedLine.geometry, selectedLine.material);
|
|
42
|
+
selectedLoop.frustumCulled = false;
|
|
43
|
+
selectedLoop.visible = false;
|
|
44
|
+
scene.add(selectedLoop);
|
|
45
|
+
let selectedKey = "";
|
|
46
|
+
let selectedAt = 0;
|
|
47
|
+
let selectedWall = 0;
|
|
48
|
+
const drop = (entry) => {
|
|
49
|
+
scene.remove(entry.points);
|
|
50
|
+
entry.geometry.dispose();
|
|
51
|
+
entry.material.dispose();
|
|
52
|
+
for (const line of entry.lines) {
|
|
53
|
+
scene.remove(line.loop);
|
|
54
|
+
line.geometry.dispose();
|
|
55
|
+
line.loop.material.dispose();
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const orbitsOf = (entry, group, time, wall) => {
|
|
59
|
+
const wanted = group.orbits && group.satellites.count <= MAX_ORBIT_GROUP;
|
|
60
|
+
if (!wanted) {
|
|
61
|
+
for (const line of entry.lines)
|
|
62
|
+
line.loop.visible = false;
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (entry.lines.length === group.satellites.count && Math.abs(time - entry.linesAt) < ORBIT_REFRESH_GAME_MS && wall - entry.linesWall < 60_000) {
|
|
66
|
+
for (const line of entry.lines)
|
|
67
|
+
line.loop.visible = true;
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (entry.lines.length === group.satellites.count && wall - entry.linesWall < ORBIT_REFRESH_WALL_MS)
|
|
71
|
+
return;
|
|
72
|
+
while (entry.lines.length < group.satellites.count) {
|
|
73
|
+
const positions = new Float32Array(ORBIT_POINTS * 3);
|
|
74
|
+
const geometry = new three.BufferGeometry();
|
|
75
|
+
geometry.setAttribute("position", new three.BufferAttribute(positions, 3));
|
|
76
|
+
const material = new three.LineBasicMaterial({ color: group.color, transparent: true, opacity: 0.45 });
|
|
77
|
+
const loop = new three.LineLoop(geometry, material);
|
|
78
|
+
loop.frustumCulled = false;
|
|
79
|
+
scene.add(loop);
|
|
80
|
+
entry.lines.push({ loop, geometry, positions });
|
|
81
|
+
}
|
|
82
|
+
for (let i = 0; i < group.satellites.count; i += 1) {
|
|
83
|
+
const line = entry.lines[i];
|
|
84
|
+
const path = group.satellites.orbit(i, time, ORBIT_POINTS);
|
|
85
|
+
line.positions.set(path.subarray(0, ORBIT_POINTS * 3));
|
|
86
|
+
line.geometry.getAttribute("position").needsUpdate = true;
|
|
87
|
+
line.loop.visible = group.satellites.visible(i) || path.some((v) => v !== 0);
|
|
88
|
+
}
|
|
89
|
+
entry.linesAt = time;
|
|
90
|
+
entry.linesWall = wall;
|
|
91
|
+
};
|
|
92
|
+
const find = (ref) => state.groups.get(ref.group)?.satellites;
|
|
93
|
+
return {
|
|
94
|
+
update(time, speed) {
|
|
95
|
+
const started = performance.now();
|
|
96
|
+
const tooFast = Math.abs(speed) > MAX_SATELLITE_SPEED;
|
|
97
|
+
const step = Math.min(60_000, Math.max(1000, Math.abs(speed) * 1000));
|
|
98
|
+
for (const [name, entry] of drawn) {
|
|
99
|
+
if (state.groups.get(name)?.satellites !== entry.source) {
|
|
100
|
+
drop(entry);
|
|
101
|
+
drawn.delete(name);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
let placed = 0;
|
|
105
|
+
for (const [name, group] of state.groups) {
|
|
106
|
+
let entry = drawn.get(name);
|
|
107
|
+
if (!entry) {
|
|
108
|
+
const positions = new Float32Array(group.satellites.count * 3);
|
|
109
|
+
const geometry = new three.BufferGeometry();
|
|
110
|
+
const attribute = new three.BufferAttribute(positions, 3);
|
|
111
|
+
geometry.setAttribute("position", attribute);
|
|
112
|
+
const material = new three.ShaderMaterial({ vertexShader: POINT_VERT, fragmentShader: POINT_FRAG, uniforms: { size: { value: group.size * ratio() }, tint: { value: new three.Color(group.color) }, ring: { value: 0 } }, transparent: true, depthWrite: false });
|
|
113
|
+
const points = new Points(geometry, material);
|
|
114
|
+
points.frustumCulled = false;
|
|
115
|
+
points.renderOrder = 5;
|
|
116
|
+
scene.add(points);
|
|
117
|
+
entry = { source: group.satellites, points, geometry, material, positions, attribute, lines: [], linesAt: Number.NaN, linesWall: 0, style: "" };
|
|
118
|
+
drawn.set(name, entry);
|
|
119
|
+
}
|
|
120
|
+
const style = `${group.color}|${group.size}|${ratio()}`;
|
|
121
|
+
if (style !== entry.style) {
|
|
122
|
+
entry.style = style;
|
|
123
|
+
entry.material.uniforms["size"].value = group.size * ratio();
|
|
124
|
+
entry.material.uniforms["tint"].value.set(group.color);
|
|
125
|
+
}
|
|
126
|
+
const show = group.visible && !tooFast;
|
|
127
|
+
entry.points.visible = show;
|
|
128
|
+
if (!show) {
|
|
129
|
+
for (const line of entry.lines)
|
|
130
|
+
line.loop.visible = false;
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
group.satellites.setStep(step);
|
|
134
|
+
group.satellites.at(time, entry.positions);
|
|
135
|
+
entry.attribute.needsUpdate = true;
|
|
136
|
+
placed += group.satellites.placed;
|
|
137
|
+
orbitsOf(entry, group, time, started);
|
|
138
|
+
}
|
|
139
|
+
const chosen = state.selected;
|
|
140
|
+
const set = chosen ? find(chosen) : undefined;
|
|
141
|
+
if (chosen && set && set.exact(chosen.index, time, tmp)) {
|
|
142
|
+
selectedPosition[0] = tmp[0];
|
|
143
|
+
selectedPosition[1] = tmp[1];
|
|
144
|
+
selectedPosition[2] = tmp[2];
|
|
145
|
+
selectedGeometry.getAttribute("position").needsUpdate = true;
|
|
146
|
+
selectedPoints.visible = true;
|
|
147
|
+
const key = `${chosen.group}:${chosen.index}`;
|
|
148
|
+
if (key !== selectedKey || (Math.abs(time - selectedAt) > ORBIT_REFRESH_GAME_MS && started - selectedWall > ORBIT_REFRESH_WALL_MS)) {
|
|
149
|
+
selectedKey = key;
|
|
150
|
+
selectedAt = time;
|
|
151
|
+
selectedWall = started;
|
|
152
|
+
selectedLine.positions.set(set.orbit(chosen.index, time, ORBIT_POINTS).subarray(0, ORBIT_POINTS * 3));
|
|
153
|
+
selectedLine.geometry.getAttribute("position").needsUpdate = true;
|
|
154
|
+
}
|
|
155
|
+
selectedLoop.visible = true;
|
|
156
|
+
selectedMaterial.uniforms["size"].value = 14 * ratio();
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
selectedPoints.visible = false;
|
|
160
|
+
selectedLoop.visible = false;
|
|
161
|
+
selectedKey = "";
|
|
162
|
+
}
|
|
163
|
+
state.drawn.placed = placed;
|
|
164
|
+
state.drawn.tooFast = tooFast;
|
|
165
|
+
state.drawn.updateMs = performance.now() - started;
|
|
166
|
+
if (state.drawn.updateMs > state.drawn.maxUpdateMs)
|
|
167
|
+
state.drawn.maxUpdateMs = state.drawn.updateMs;
|
|
168
|
+
state.drawn.updates += 1;
|
|
169
|
+
state.drawn.totalMs += state.drawn.updateMs;
|
|
170
|
+
},
|
|
171
|
+
pick(x, y, camera, size, radius) {
|
|
172
|
+
let best = null;
|
|
173
|
+
let bestD = radius * radius;
|
|
174
|
+
const c = camera.position;
|
|
175
|
+
for (const [name, entry] of drawn) {
|
|
176
|
+
const group = state.groups.get(name);
|
|
177
|
+
if (!group || !entry.points.visible)
|
|
178
|
+
continue;
|
|
179
|
+
const p = entry.positions;
|
|
180
|
+
for (let i = 0; i < group.satellites.count; i += 1) {
|
|
181
|
+
if (!group.satellites.visible(i))
|
|
182
|
+
continue;
|
|
183
|
+
projected.set(p[i * 3], p[i * 3 + 1], p[i * 3 + 2]).project(camera);
|
|
184
|
+
if (projected.z > 1 || projected.z < -1)
|
|
185
|
+
continue;
|
|
186
|
+
const dx = ((projected.x + 1) / 2) * size.width - x;
|
|
187
|
+
const dy = ((1 - projected.y) / 2) * size.height - y;
|
|
188
|
+
const d = dx * dx + dy * dy;
|
|
189
|
+
if (d > bestD)
|
|
190
|
+
continue;
|
|
191
|
+
// behind the Earth: the line from the camera to the satellite passes through the unit sphere before it gets there
|
|
192
|
+
const px = p[i * 3] - c.x;
|
|
193
|
+
const py = p[i * 3 + 1] - c.y;
|
|
194
|
+
const pz = p[i * 3 + 2] - c.z;
|
|
195
|
+
const len2 = px * px + py * py + pz * pz;
|
|
196
|
+
const t = Math.max(0, Math.min(1, -(c.x * px + c.y * py + c.z * pz) / len2));
|
|
197
|
+
const nx = c.x + px * t;
|
|
198
|
+
const ny = c.y + py * t;
|
|
199
|
+
const nz = c.z + pz * t;
|
|
200
|
+
if (nx * nx + ny * ny + nz * nz < 0.998)
|
|
201
|
+
continue;
|
|
202
|
+
best = { group: name, index: i };
|
|
203
|
+
bestD = d;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return best;
|
|
207
|
+
},
|
|
208
|
+
place(camera, size) {
|
|
209
|
+
if (!selectedPoints.visible)
|
|
210
|
+
return null;
|
|
211
|
+
projected.set(selectedPosition[0], selectedPosition[1], selectedPosition[2]).project(camera);
|
|
212
|
+
if (projected.z > 1 || projected.z < -1)
|
|
213
|
+
return null;
|
|
214
|
+
return { x: ((projected.x + 1) / 2) * size.width, y: ((1 - projected.y) / 2) * size.height };
|
|
215
|
+
},
|
|
216
|
+
exact(ref, time, out) {
|
|
217
|
+
return find(ref)?.exact(ref.index, time, out) ?? false;
|
|
218
|
+
},
|
|
219
|
+
dispose() {
|
|
220
|
+
for (const entry of drawn.values())
|
|
221
|
+
drop(entry);
|
|
222
|
+
drawn.clear();
|
|
223
|
+
scene.remove(selectedPoints, selectedLoop);
|
|
224
|
+
selectedGeometry.dispose();
|
|
225
|
+
selectedMaterial.dispose();
|
|
226
|
+
selectedLine.geometry.dispose();
|
|
227
|
+
selectedLine.material.dispose();
|
|
228
|
+
},
|
|
229
|
+
};
|
|
230
|
+
}
|
package/dist/space/scene.d.ts
CHANGED
|
@@ -4,6 +4,9 @@ import { type ThreeParts } from "../realistic/textures.ts";
|
|
|
4
4
|
import type { Layer } from "../realistic/tiers.ts";
|
|
5
5
|
import { type Frame, type PlanetName, type Vec3 } from "./ephemeris.ts";
|
|
6
6
|
import { type ScaleMode } from "./scale.ts";
|
|
7
|
+
import type { SatelliteRef } from "../satellites/types.ts";
|
|
8
|
+
import { type RideLook } from "./ride.ts";
|
|
9
|
+
import { type SatelliteState } from "./satellite-layer.ts";
|
|
7
10
|
type Three = ThreeParts;
|
|
8
11
|
/** The camera's distance, in Earth radii, at which the Earth fills the view at zoom 1. */
|
|
9
12
|
export declare const FIT_DISTANCE = 3.4;
|
|
@@ -30,6 +33,33 @@ export declare const BODY_NAMES: readonly ["Earth", "Sun", "Moon", "Mercury", "V
|
|
|
30
33
|
* ```
|
|
31
34
|
*/
|
|
32
35
|
export type BodyName = (typeof BODY_NAMES)[number];
|
|
36
|
+
/**
|
|
37
|
+
* What a camera can ride along with: the name of one of the bodies, or a satellite of a group (`{ group, index }`).
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts no-run
|
|
41
|
+
* import type { RideTarget } from "@johnmorrisdotca/chikyu-earth/space/element";
|
|
42
|
+
*
|
|
43
|
+
* const targets: RideTarget[] = ["Moon", { group: "stations", index: 0 }];
|
|
44
|
+
* console.log(targets.length);
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export type RideTarget = BodyName | SatelliteRef;
|
|
48
|
+
export interface RideState {
|
|
49
|
+
target: RideTarget;
|
|
50
|
+
/** `from`: the camera is at the body, looking at the Earth. `follow`: the camera keeps a fixed distance from it, looking at it. */
|
|
51
|
+
view: "from" | "follow";
|
|
52
|
+
look: RideLook;
|
|
53
|
+
/** For `follow` a satellite: how far the camera keeps, in kilometres. */
|
|
54
|
+
distanceKm: number;
|
|
55
|
+
}
|
|
56
|
+
/** The camera as the last draw left it, for the next one to start from when the ride changes. */
|
|
57
|
+
export interface Pose {
|
|
58
|
+
p: Vec3;
|
|
59
|
+
q: [number, number, number, number];
|
|
60
|
+
fov: number;
|
|
61
|
+
valid: boolean;
|
|
62
|
+
}
|
|
33
63
|
export interface SpaceModel {
|
|
34
64
|
/** Moment shown, milliseconds since the epoch. */
|
|
35
65
|
time: number;
|
|
@@ -61,6 +91,36 @@ export interface SpaceModel {
|
|
|
61
91
|
} | null;
|
|
62
92
|
/** How many times astronomy-engine has been asked for a frame. */
|
|
63
93
|
lookups: number;
|
|
94
|
+
/** Game seconds a second (the clock's speed). */
|
|
95
|
+
speed: number;
|
|
96
|
+
/** The satellites shown, the one chosen, and what the last draw did with them. */
|
|
97
|
+
satellites: SatelliteState;
|
|
98
|
+
/** The body or satellite the camera rides along with, or null. */
|
|
99
|
+
ride: RideState | null;
|
|
100
|
+
/** Set at each draw: the target of the ride could not be placed (a satellite whose elements are too old, or that is gone), so the ordinary camera was used. */
|
|
101
|
+
rideLost: boolean;
|
|
102
|
+
/** A camera move in progress from where the last draw left it: when it began (ms of the page's clock), how long it takes. */
|
|
103
|
+
transition: {
|
|
104
|
+
start: number;
|
|
105
|
+
duration: number;
|
|
106
|
+
from: {
|
|
107
|
+
p: Vec3;
|
|
108
|
+
q: [number, number, number, number];
|
|
109
|
+
fov: number;
|
|
110
|
+
};
|
|
111
|
+
} | null;
|
|
112
|
+
/** The shared controls' view as the last draw used it, and how tall the view was (CSS pixels): `setView` skips a draw for a turn that moves nothing by half a pixel. */
|
|
113
|
+
drawnView: {
|
|
114
|
+
yaw: number;
|
|
115
|
+
tilt: number;
|
|
116
|
+
zoom: number;
|
|
117
|
+
} | null;
|
|
118
|
+
heightPx: number;
|
|
119
|
+
pose: Pose;
|
|
120
|
+
/** Pixels a second that riding makes things move on the screen at 1x clock speed (0 when not riding): the draw rate follows it. */
|
|
121
|
+
rideMotion: number;
|
|
122
|
+
/** The satellite at a point of the view (CSS pixels), within a radius, if a draw has been made and one is there. */
|
|
123
|
+
pick: ((x: number, y: number, radius: number) => SatelliteRef | null) | null;
|
|
64
124
|
ratio(): number;
|
|
65
125
|
}
|
|
66
126
|
export declare const newSpaceModel: () => SpaceModel;
|
|
@@ -113,6 +173,17 @@ export interface SpaceDeps {
|
|
|
113
173
|
onTier?: (layer: Layer, width: number) => void;
|
|
114
174
|
/** Where each body is on the screen, in CSS pixels in the box, or null when it is behind the camera. Called at each draw. */
|
|
115
175
|
onLabels?: (places: Record<BodyName, BodyPlace | null>) => void;
|
|
176
|
+
/** Where the chosen satellite is on the screen, or null. Called at each draw while a satellite is chosen (and once when it is let go). */
|
|
177
|
+
onSelected?: (place: {
|
|
178
|
+
x: number;
|
|
179
|
+
y: number;
|
|
180
|
+
} | null) => void;
|
|
181
|
+
/** Called at the end of a draw while the camera is moving from one ride to another, so the next frame is asked for (at the page's frame cap). */
|
|
182
|
+
onMoving?: () => void;
|
|
183
|
+
/** The target of the ride could not be placed at this draw. */
|
|
184
|
+
onRideLost?: () => void;
|
|
185
|
+
/** The satellites could not be drawn (the three.js parts handed over lack `Points`). */
|
|
186
|
+
onSatellitesError?: (error: unknown) => void;
|
|
116
187
|
/** How the Sun and the planets are drawn; left out, plain spheres in the right colours (see `BodyPainter`). */
|
|
117
188
|
painter?: BodyPainter;
|
|
118
189
|
}
|