@johnmorrisdotca/wakusei-planets 0.19.0 → 0.21.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.
Files changed (113) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/NOTICE.md +14 -3
  3. package/README.md +39 -6
  4. package/dist/bodies/earth.js +13 -5
  5. package/dist/calendar.d.ts +119 -0
  6. package/dist/calendar.js +155 -0
  7. package/dist/clock.d.ts +244 -0
  8. package/dist/clock.js +325 -0
  9. package/dist/coordinates.d.ts +172 -0
  10. package/dist/coordinates.js +170 -0
  11. package/dist/craft/cassini.d.ts +1 -0
  12. package/dist/craft/cassini.js +1131 -0
  13. package/dist/craft/galileo.d.ts +1 -0
  14. package/dist/craft/galileo.js +224 -0
  15. package/dist/craft/juno.d.ts +1 -0
  16. package/dist/craft/juno.js +640 -0
  17. package/dist/craft/newhorizons.d.ts +1 -0
  18. package/dist/craft/newhorizons.js +18 -0
  19. package/dist/craft/parker.d.ts +1 -0
  20. package/dist/craft/parker.js +221 -0
  21. package/dist/craft/pioneer10.d.ts +1 -0
  22. package/dist/craft/pioneer10.js +24 -0
  23. package/dist/craft/pioneer11.d.ts +1 -0
  24. package/dist/craft/pioneer11.js +34 -0
  25. package/dist/craft/voyager1.d.ts +1 -0
  26. package/dist/craft/voyager1.js +28 -0
  27. package/dist/craft/voyager2.d.ts +1 -0
  28. package/dist/craft/voyager2.js +43 -0
  29. package/dist/craft/webb.d.ts +1 -0
  30. package/dist/craft/webb.js +28 -0
  31. package/dist/data/craft.d.ts +23 -0
  32. package/dist/data/craft.js +53 -0
  33. package/dist/data/lunar.d.ts +4 -0
  34. package/dist/data/lunar.js +548 -0
  35. package/dist/data/pluto.d.ts +6 -0
  36. package/dist/data/pluto.js +29 -0
  37. package/dist/data/stars.d.ts +4 -0
  38. package/dist/data/stars.js +5088 -0
  39. package/dist/data/tables.d.ts +41 -0
  40. package/dist/data/tables.js +37 -0
  41. package/dist/data/vsop.d.ts +19 -1
  42. package/dist/data/vsop.js +25 -8
  43. package/dist/element.d.ts +2 -23
  44. package/dist/element.js +75 -110
  45. package/dist/ephemeris.d.ts +22 -14
  46. package/dist/ephemeris.js +106 -58
  47. package/dist/index.d.ts +1 -1
  48. package/dist/index.js +1 -1
  49. package/dist/mars.d.ts +41 -0
  50. package/dist/mars.js +52 -0
  51. package/dist/portrait/element.d.ts +1 -0
  52. package/dist/portrait/element.js +27 -39
  53. package/dist/scales.d.ts +46 -0
  54. package/dist/scales.js +75 -0
  55. package/dist/scene/basis.d.ts +3 -0
  56. package/dist/scene/basis.js +4 -0
  57. package/dist/scene/body.d.ts +2 -4
  58. package/dist/scene/body.js +55 -67
  59. package/dist/scene/fallback.d.ts +4 -0
  60. package/dist/scene/fallback.js +5 -0
  61. package/dist/scene/painter.js +10 -23
  62. package/dist/scene/store.d.ts +2 -2
  63. package/dist/scene/textures.d.ts +30 -0
  64. package/dist/scene/textures.js +47 -0
  65. package/dist/sidereal.d.ts +70 -0
  66. package/dist/sidereal.js +94 -0
  67. package/dist/sky/engine.d.ts +37 -0
  68. package/dist/sky/engine.js +116 -0
  69. package/dist/sky/events.d.ts +37 -0
  70. package/dist/sky/events.js +133 -0
  71. package/dist/sky/frames.d.ts +3 -0
  72. package/dist/sky/frames.js +26 -0
  73. package/dist/sky/types.d.ts +20 -0
  74. package/dist/sky/types.js +1 -0
  75. package/dist/space/aim.d.ts +18 -0
  76. package/dist/space/aim.js +28 -0
  77. package/dist/space/element.d.ts +212 -0
  78. package/dist/space/element.js +524 -0
  79. package/dist/space/frame.d.ts +152 -0
  80. package/dist/space/frame.js +169 -0
  81. package/dist/space/index.d.ts +4 -0
  82. package/dist/space/index.js +5 -0
  83. package/dist/space/ride.d.ts +89 -0
  84. package/dist/space/ride.js +58 -0
  85. package/dist/space/scale.d.ts +126 -0
  86. package/dist/space/scale.js +139 -0
  87. package/dist/space/scene.d.ts +189 -0
  88. package/dist/space/scene.js +422 -0
  89. package/dist/spacecraft-facts.d.ts +114 -0
  90. package/dist/spacecraft-facts.js +215 -0
  91. package/dist/spacecraft.d.ts +134 -0
  92. package/dist/spacecraft.js +198 -0
  93. package/dist/stars.d.ts +160 -0
  94. package/dist/stars.js +193 -0
  95. package/dist/suncalc-legacy.d.ts +187 -0
  96. package/dist/suncalc-legacy.js +168 -0
  97. package/dist/suncalc.d.ts +209 -0
  98. package/dist/suncalc.js +167 -0
  99. package/dist/tables/all.d.ts +2 -0
  100. package/dist/tables/all.js +2 -0
  101. package/dist/tables/ceres.d.ts +1 -0
  102. package/dist/tables/ceres.js +57 -0
  103. package/dist/tables/dwarfs.d.ts +1 -0
  104. package/dist/tables/dwarfs.js +59 -0
  105. package/dist/theory.d.ts +20 -0
  106. package/dist/theory.js +98 -0
  107. package/dist/time.d.ts +6 -108
  108. package/dist/time.js +7 -180
  109. package/dist/version.d.ts +1 -1
  110. package/dist/version.js +1 -1
  111. package/dist/when.d.ts +16 -0
  112. package/dist/when.js +13 -0
  113. package/package.json +95 -4
@@ -0,0 +1,189 @@
1
+ import type * as THREE from "three";
2
+ import type { Item3D } from "@johnmorrisdotca/kazoku-ui/renderer";
3
+ import { type MoonId } from "../facts.ts";
4
+ import type { ThreeParts } from "../scene/body.ts";
5
+ import { type WakuseiPainter } from "../scene/painter.ts";
6
+ import "../bodies/all.ts";
7
+ import { type SpaceFrame, type SpaceVec3 } from "./frame.ts";
8
+ import { type RideLook } from "./ride.ts";
9
+ import { type ScaleMode } from "./scale.ts";
10
+ /** The parts of three.js the space view uses: the bodies' and a perspective camera. */
11
+ export type SpaceThree = ThreeParts & Pick<typeof THREE, "PerspectiveCamera">;
12
+ /** The camera's distance, in radii of the body it looks at, at which that body fills the view at zoom 1. */
13
+ export declare const FIT_DISTANCE = 3.4;
14
+ /**
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.
16
+ *
17
+ * @example
18
+ * ```ts no-run
19
+ * import { SPACE_BODIES } from "@johnmorrisdotca/wakusei-planets/space/element";
20
+ *
21
+ * console.log(SPACE_BODIES.join(", "));
22
+ * ```
23
+ */
24
+ export declare const SPACE_BODIES: readonly ["earth", "sun", "moon", "mercury", "venus", "mars", "jupiter", "saturn", "uranus", "neptune", "pluto"];
25
+ /**
26
+ * One of the bodies in `SPACE_BODIES`, by the package's usual lower-case name.
27
+ *
28
+ * @example
29
+ * ```ts no-run
30
+ * import { SPACE_BODIES, type SpaceBody } from "@johnmorrisdotca/wakusei-planets/space/element";
31
+ *
32
+ * const mars: SpaceBody = "mars";
33
+ * console.log(SPACE_BODIES.includes(mars));
34
+ * ```
35
+ */
36
+ export type SpaceBody = (typeof SPACE_BODIES)[number];
37
+ /**
38
+ * How the Earth is lit: `sun` is the true light; `lit` is daylight from the camera's side, so a view of the night side is never black.
39
+ *
40
+ * @example
41
+ * ```ts no-run
42
+ * import type { SpaceLight } from "@johnmorrisdotca/wakusei-planets/space/element";
43
+ *
44
+ * const light: SpaceLight = "lit";
45
+ * console.log(light);
46
+ * ```
47
+ */
48
+ export type SpaceLight = "sun" | "lit";
49
+ /**
50
+ * Any moving point a camera can ride: where it is and which way it goes at a moment, from the centre of the body it goes round, in kilometres and kilometres a second, in the scene's axes (J2000
51
+ * equatorial, y north). It returns `null` when it cannot be placed then (its data too old, or it is gone). The space view knows nothing of what the point is: a satellite, a probe, a station.
52
+ *
53
+ * @example
54
+ * ```ts no-run
55
+ * import type { RidePoint } from "@johnmorrisdotca/wakusei-planets/space/element";
56
+ *
57
+ * const circle: RidePoint = { at: (ms) => ({ position: [7000 * Math.cos(ms / 1e6), 0, 7000 * Math.sin(ms / 1e6)], velocity: [0, 7.5, 0] }) };
58
+ * console.log(circle.at(0)?.position[0]);
59
+ * ```
60
+ */
61
+ export interface RidePoint {
62
+ at(ms: number): {
63
+ position: SpaceVec3;
64
+ velocity: SpaceVec3;
65
+ } | null;
66
+ }
67
+ /**
68
+ * What a camera can ride along with: a body of the view (`moon`, `mars`, ... going round the Earth), a moon of a planet in the view (`io`, going round Jupiter), or any moving point of your own.
69
+ *
70
+ * @example
71
+ * ```ts no-run
72
+ * import type { RideTarget } from "@johnmorrisdotca/wakusei-planets/space/element";
73
+ *
74
+ * const targets: RideTarget[] = ["moon", "io", { at: () => null }];
75
+ * console.log(targets.length);
76
+ * ```
77
+ */
78
+ export type RideTarget = SpaceBody | MoonId | RidePoint;
79
+ /** What the scene rides: the target, the body it goes round, and how. */
80
+ export interface RideState {
81
+ target: RideTarget;
82
+ /** The body the target goes round: the Earth for a body of the view or a point (unless said), the planet for a moon. */
83
+ around: SpaceBody;
84
+ /** `from`: the camera is at the target, looking at the body it goes round. `follow`: the camera keeps a distance from it, looking at it. */
85
+ view: "from" | "follow";
86
+ look: RideLook;
87
+ /** For `follow` a point: how far the camera keeps, in kilometres. */
88
+ distanceKm: number;
89
+ }
90
+ /** The camera as the last draw left it, for the next one to start from when the ride changes. */
91
+ export interface CameraPose {
92
+ p: SpaceVec3;
93
+ q: [number, number, number, number];
94
+ fov: number;
95
+ valid: boolean;
96
+ }
97
+ export interface SpaceModel {
98
+ /** Moment shown, milliseconds since the epoch. */
99
+ time: number;
100
+ mode: ScaleMode;
101
+ /** The shared controls' yaw, tilt and zoom. */
102
+ view: {
103
+ yaw: number;
104
+ tilt: number;
105
+ zoom: number;
106
+ };
107
+ /** The home place: the camera starts above it. */
108
+ centre: {
109
+ lon: number;
110
+ lat: number;
111
+ };
112
+ /** The moment the camera was last pointed at the ground: it is fixed to the stars, not to the ground. */
113
+ anchor: number | null;
114
+ orbits: boolean;
115
+ labels: boolean;
116
+ /** Milliseconds of game time one second of wall time covers (the ephemeris is looked up once per such span). */
117
+ stepMs: number;
118
+ /** The body the camera looks at, and (in `zoom`) fills the view with at 1x. */
119
+ focus: SpaceBody;
120
+ light: SpaceLight;
121
+ /** How the painter draws the bodies (`realistic` or `painted`). */
122
+ look: string;
123
+ /** Filled at each draw. */
124
+ drawn: {
125
+ frame: SpaceFrame;
126
+ distance: number;
127
+ radiusPx: number;
128
+ } | null;
129
+ /** What the painter has made now: the bodies drawn, and the moons shown. */
130
+ painted: {
131
+ bodies: number;
132
+ moons: number;
133
+ held: number;
134
+ };
135
+ /** How many frames the ephemeris has worked out. */
136
+ lookups: number;
137
+ /** Pixels a game-second moves things on the screen (the fastest turning body in sight): the draw rate follows it. */
138
+ motion: number;
139
+ /** The shared controls' view as the last draw used it, and how tall the view was (CSS pixels). */
140
+ drawnView: {
141
+ yaw: number;
142
+ tilt: number;
143
+ zoom: number;
144
+ } | null;
145
+ heightPx: number;
146
+ camera: {
147
+ position: [number, number, number];
148
+ fov: number;
149
+ };
150
+ /** The body or point the camera rides along with, or null. */
151
+ ride: RideState | null;
152
+ /** Set at each draw: the target of the ride could not be placed, so the ordinary camera was used. */
153
+ rideLost: boolean;
154
+ /** 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. */
155
+ transition: {
156
+ start: number;
157
+ duration: number;
158
+ from: {
159
+ p: SpaceVec3;
160
+ q: [number, number, number, number];
161
+ fov: number;
162
+ };
163
+ } | null;
164
+ pose: CameraPose;
165
+ /** Pixels a game-second that riding makes things move on the screen (0 when not riding): the draw rate follows it. */
166
+ rideMotion: number;
167
+ ratio(): number;
168
+ }
169
+ export declare const newSpaceModel: () => SpaceModel;
170
+ /** Where a body is on the screen: its centre and its apparent radius, in CSS pixels in the box. */
171
+ export interface BodyPlace {
172
+ x: number;
173
+ y: number;
174
+ r: number;
175
+ }
176
+ export interface SpaceDeps {
177
+ painter: () => WakuseiPainter;
178
+ cap: () => number;
179
+ invalidate: () => void;
180
+ /** 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. */
181
+ onLabels?: (places: Record<SpaceBody, BodyPlace | null>) => void;
182
+ /** 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). */
183
+ onMoving?: () => void;
184
+ /** The target of the ride could not be placed at this draw. */
185
+ onRideLost?: () => void;
186
+ /** The painter could not make a body (the three.js parts handed over lack something it needs). */
187
+ onPainterError?: (error: unknown) => void;
188
+ }
189
+ export declare function createSpaceItem(model: SpaceModel, deps: SpaceDeps): Item3D<ThreeParts>;
@@ -0,0 +1,422 @@
1
+ import { BODIES } from "../facts.js";
2
+ import { moonOffset } from "../scene/orient.js";
3
+ import {} from "../scene/painter.js";
4
+ import "../bodies/all.js";
5
+ import { groundDirection } from "./aim.js";
6
+ import { createTrack, planetOrbit, SPACE_PLANETS } from "./frame.js";
7
+ import { ridePose } from "./ride.js";
8
+ import { placeAround, placeMoon, placeSun, radiusIn } from "./scale.js";
9
+ const RAD = Math.PI / 180;
10
+ /** The camera's distance, in radii of the body it looks at, at which that body fills the view at zoom 1. */
11
+ export const FIT_DISTANCE = 3.4;
12
+ const FOV = 35;
13
+ const ORBIT_POINTS = 96;
14
+ /** 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. */
15
+ const MOON_MIN_PLANET_PX = 4;
16
+ const MOON_MIN_PX = 2.2;
17
+ /** A moon that has been out of sight this long is let go (its pictures too). */
18
+ const MOON_KEEP_MS = 4000;
19
+ /**
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.
21
+ *
22
+ * @example
23
+ * ```ts no-run
24
+ * import { SPACE_BODIES } from "@johnmorrisdotca/wakusei-planets/space/element";
25
+ *
26
+ * console.log(SPACE_BODIES.join(", "));
27
+ * ```
28
+ */
29
+ export const SPACE_BODIES = ["earth", "sun", "moon", ...SPACE_PLANETS];
30
+ export const newSpaceModel = () => ({
31
+ time: Date.now(),
32
+ mode: "compressed",
33
+ view: { yaw: 0, tilt: 0, zoom: 1 },
34
+ centre: { lon: 0, lat: 0 },
35
+ anchor: null,
36
+ orbits: true,
37
+ labels: true,
38
+ stepMs: 1000,
39
+ focus: "earth",
40
+ light: "sun",
41
+ look: "realistic",
42
+ drawn: null,
43
+ painted: { bodies: 0, moons: 0, held: 0 },
44
+ lookups: 0,
45
+ motion: 0,
46
+ drawnView: null,
47
+ heightPx: 420,
48
+ camera: { position: [0, 0, 0], fov: FOV },
49
+ ride: null,
50
+ rideLost: false,
51
+ transition: null,
52
+ pose: { p: [0, 0, 0], q: [0, 0, 0, 1], fov: FOV, valid: false },
53
+ rideMotion: 0,
54
+ ratio: () => Math.min(globalThis.devicePixelRatio || 1, 2),
55
+ });
56
+ const unit = (v) => {
57
+ const l = Math.hypot(v[0], v[1], v[2]) || 1;
58
+ return [v[0] / l, v[1] / l, v[2] / l];
59
+ };
60
+ const minus = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
61
+ export function createSpaceItem(model, deps) {
62
+ return {
63
+ key: "wakusei-planets:space",
64
+ build({ three: parts, width, height }) {
65
+ const three = parts;
66
+ let size = { width, height };
67
+ const track = createTrack(model.stepMs);
68
+ let trackStep = model.stepMs;
69
+ const scene = new three.Scene();
70
+ const camera = new three.PerspectiveCamera(FOV, width / Math.max(1, height), 0.05, 1000);
71
+ const target = new three.Vector3();
72
+ const projected = new three.Vector3();
73
+ const desiredQ = camera.quaternion.clone();
74
+ const fromQ = camera.quaternion.clone();
75
+ let ended = false;
76
+ const slots = new Map();
77
+ const empty = (slot) => {
78
+ if (!slot.drawing)
79
+ return;
80
+ scene.remove(slot.drawing.object);
81
+ slot.drawing.dispose();
82
+ slot.drawing = undefined;
83
+ };
84
+ const fill = (id, slot) => {
85
+ if (slot.look === model.look || slot.failed)
86
+ return;
87
+ empty(slot);
88
+ slot.look = model.look;
89
+ try {
90
+ const made = deps.painter().make(three, id, { look: model.look, invalidate: () => deps.invalidate(), ratio: model.ratio(), maxTexture: deps.cap() });
91
+ if (ended) {
92
+ made.dispose();
93
+ return;
94
+ }
95
+ slot.drawing = made;
96
+ scene.add(made.object);
97
+ }
98
+ catch (error) {
99
+ slot.failed = true;
100
+ deps.onPainterError?.(error);
101
+ }
102
+ };
103
+ const slotOf = (id) => {
104
+ let slot = slots.get(id);
105
+ if (!slot)
106
+ slots.set(id, (slot = { drawing: undefined, look: null, failed: false }));
107
+ return slot;
108
+ };
109
+ const orbits = new Map();
110
+ for (const id of SPACE_PLANETS) {
111
+ const positions = new Float32Array((ORBIT_POINTS + 1) * 3);
112
+ const geometry = new three.BufferGeometry();
113
+ geometry.setAttribute("position", new three.BufferAttribute(positions, 3));
114
+ const line = new three.Line(geometry, new three.LineBasicMaterial({ color: BODIES[id].colour, transparent: true, opacity: 0.55 }));
115
+ line.frustumCulled = false;
116
+ scene.add(line);
117
+ orbits.set(id, { line, positions, samples: [], epoch: Number.NaN });
118
+ }
119
+ const moonSlots = new Map();
120
+ let releaseTimer;
121
+ const labelFor = (v) => {
122
+ projected.set(v[0], v[1], v[2]).project(camera);
123
+ if (projected.z > 1 || projected.z < -1)
124
+ return null;
125
+ return { x: ((projected.x + 1) / 2) * size.width, y: ((1 - projected.y) / 2) * size.height };
126
+ };
127
+ return {
128
+ scene,
129
+ camera,
130
+ update() {
131
+ const frame = track.at(model.time);
132
+ model.lookups = track.lookups;
133
+ if (model.stepMs !== trackStep) {
134
+ trackStep = model.stepMs;
135
+ track.setStep(model.stepMs);
136
+ }
137
+ const mode = model.mode;
138
+ const sunPos = placeSun(mode, frame.sun);
139
+ const pos = { earth: [0, 0, 0], moon: placeMoon(mode, frame.moon), sun: sunPos };
140
+ for (const id of SPACE_PLANETS)
141
+ pos[id] = placeAround(mode, sunPos, minus(frame.planets[id], frame.sun));
142
+ const radiusOf = (id) => radiusIn(id, mode);
143
+ const placeAll = (at) => {
144
+ const sunAt = placeSun(mode, at.sun);
145
+ const all = { earth: [0, 0, 0], moon: placeMoon(mode, at.moon), sun: sunAt };
146
+ for (const id of SPACE_PLANETS)
147
+ all[id] = placeAround(mode, sunAt, minus(at.planets[id], at.sun));
148
+ return all;
149
+ };
150
+ const toSunOf = (id) => unit(minus(frame.sun, id === "earth" ? [0, 0, 0] : id === "moon" ? frame.moon : id === "sun" ? [0, 0, 0] : frame.planets[id]));
151
+ // riding along: the camera sits at a moving point and looks at the body it goes round (a moon over its planet, a point over any body). The point's place and way of going are in radii of
152
+ // that body, from its centre; the body is drawn at `centre` with the radius `unitWorld`, so the same numbers place the camera in the scene.
153
+ model.rideLost = false;
154
+ model.rideMotion = 0;
155
+ let focus = model.focus;
156
+ let focusPos = pos[focus];
157
+ let focusRadius = radiusOf(focus);
158
+ let aimWay = 1;
159
+ let rideFrom = null;
160
+ const ride = model.ride;
161
+ if (ride) {
162
+ const t = ride.target;
163
+ const around = ride.around;
164
+ const unitWorld = radiusOf(around);
165
+ const centre = pos[around];
166
+ let position;
167
+ let velocity = [0, 0, 0];
168
+ let perSecond = 1;
169
+ let hiddenBody = null;
170
+ let hiddenMoon = null;
171
+ if (typeof t === "string" && SPACE_BODIES.includes(t)) {
172
+ const id = t;
173
+ if (ride.view === "follow") {
174
+ focus = id;
175
+ focusPos = pos[id];
176
+ focusRadius = radiusOf(id);
177
+ }
178
+ else {
179
+ const ends = track.ends();
180
+ const was = ends ? placeAll(ends[0])[id] : pos[id];
181
+ const will = ends ? placeAll(ends[1])[id] : pos[id];
182
+ position = pos[id];
183
+ velocity = minus(will, was);
184
+ perSecond = ends ? Math.max(1e-3, (ends[1].ms - ends[0].ms) / 1000) : 1;
185
+ hiddenBody = id;
186
+ }
187
+ }
188
+ else if (typeof t === "string") {
189
+ const id = t;
190
+ const offset = moonOffset(id, model.time);
191
+ const later = moonOffset(id, model.time + 1000);
192
+ if (ride.view === "follow") {
193
+ focusPos = [centre[0] + offset[0] * unitWorld, centre[1] + offset[1] * unitWorld, centre[2] + offset[2] * unitWorld];
194
+ focusRadius = Math.max(1e-6, (BODIES[id].radiusKm / BODIES[around].radiusKm) * unitWorld);
195
+ }
196
+ else {
197
+ position = offset;
198
+ velocity = minus(later, offset);
199
+ hiddenMoon = id;
200
+ }
201
+ }
202
+ else {
203
+ const place = t.at(model.time);
204
+ if (!place)
205
+ model.rideLost = true;
206
+ else {
207
+ const km = BODIES[around].radiusKm;
208
+ const inRadii = [place.position[0] / km, place.position[1] / km, place.position[2] / km];
209
+ if (ride.view === "follow") {
210
+ focusPos = [centre[0] + inRadii[0] * unitWorld, centre[1] + inRadii[1] * unitWorld, centre[2] + inRadii[2] * unitWorld];
211
+ focusRadius = Math.max(1e-6, (ride.distanceKm / km / FIT_DISTANCE) * unitWorld);
212
+ aimWay = -1;
213
+ }
214
+ else {
215
+ position = inRadii;
216
+ velocity = [place.velocity[0] / km, place.velocity[1] / km, place.velocity[2] / km];
217
+ }
218
+ }
219
+ }
220
+ if (position && ride.view === "from")
221
+ rideFrom = { around, centre, unitWorld, position, velocity, perSecond, hiddenBody, hiddenMoon };
222
+ }
223
+ let fovNow = FOV;
224
+ let distance = 0;
225
+ if (rideFrom) {
226
+ const pose = ridePose({ position: rideFrom.position, velocity: rideFrom.velocity, radius: 1, look: ride.look, yaw: model.view.yaw, tilt: model.view.tilt, zoom: model.view.zoom });
227
+ const { centre, unitWorld } = rideFrom;
228
+ camera.position.set(centre[0] + pose.position[0] * unitWorld, centre[1] + pose.position[1] * unitWorld, centre[2] + pose.position[2] * unitWorld);
229
+ camera.up.set(pose.up[0], pose.up[1], pose.up[2]);
230
+ camera.lookAt(camera.position.x + pose.forward[0], camera.position.y + pose.forward[1], camera.position.z + pose.forward[2]);
231
+ fovNow = pose.fov;
232
+ const away = Math.hypot(...rideFrom.position);
233
+ camera.near = Math.max(0.0008, Math.min(0.05, (away - 1) * 0.1)) * unitWorld;
234
+ camera.far = Math.max(2000, 400 + (mode === "true" ? 40 * 23_481 : 0));
235
+ distance = away * unitWorld;
236
+ // how fast the ground goes by under the camera, in pixels a game-second: its speed over its height, times the focal length in pixels
237
+ const focal = size.height / 2 / Math.tan((fovNow / 2) * RAD);
238
+ const height = Math.max(50 / BODIES[rideFrom.around].radiusKm, away - 1);
239
+ model.rideMotion = (Math.hypot(...rideFrom.velocity) / rideFrom.perSecond / height) * focal;
240
+ }
241
+ else {
242
+ // looks at the focus body from the direction the sky was in when it was last moved, turned by yaw and tilt, as far as the zoom says
243
+ model.anchor ??= model.time;
244
+ const lon = model.centre.lon - aimWay * model.view.yaw;
245
+ const lat = Math.max(-90, Math.min(90, model.centre.lat - aimWay * model.view.tilt));
246
+ const aim = groundDirection(lon, lat, model.anchor);
247
+ distance = Math.max(1.05 * focusRadius, (FIT_DISTANCE * focusRadius) / Math.max(1e-9, model.view.zoom));
248
+ target.set(focusPos[0], focusPos[1], focusPos[2]);
249
+ camera.position.set(target.x + aim[0] * distance, target.y + aim[1] * distance, target.z + aim[2] * distance);
250
+ camera.up.set(0, 1, 0);
251
+ camera.lookAt(target);
252
+ camera.near = Math.max(0.0005, distance * 0.02 - focusRadius * 0.5, focusRadius * 0.01);
253
+ camera.far = Math.max(2000, distance * 4 + (mode === "true" ? 40 * 23_481 : 400));
254
+ }
255
+ // a ride that changed: glide from where the last draw left the camera
256
+ const glide = model.transition;
257
+ if (glide) {
258
+ const k = Math.min(1, (performance.now() - glide.start) / glide.duration);
259
+ const ease = k * k * (3 - 2 * k);
260
+ desiredQ.copy(camera.quaternion);
261
+ camera.position.set(glide.from.p[0] + (camera.position.x - glide.from.p[0]) * ease, glide.from.p[1] + (camera.position.y - glide.from.p[1]) * ease, glide.from.p[2] + (camera.position.z - glide.from.p[2]) * ease);
262
+ fromQ.set(glide.from.q[0], glide.from.q[1], glide.from.q[2], glide.from.q[3]);
263
+ camera.quaternion.copy(fromQ.slerp(desiredQ, ease));
264
+ fovNow = glide.from.fov + (fovNow - glide.from.fov) * ease;
265
+ if (k >= 1)
266
+ model.transition = null;
267
+ }
268
+ camera.fov = fovNow;
269
+ camera.aspect = size.width / Math.max(1, size.height);
270
+ camera.updateProjectionMatrix();
271
+ camera.updateMatrixWorld();
272
+ const hiddenBody = rideFrom?.hiddenBody ?? null;
273
+ const hiddenMoon = rideFrom?.hiddenMoon ?? null;
274
+ model.camera = { position: [camera.position.x, camera.position.y, camera.position.z], fov: fovNow };
275
+ model.pose = { p: [camera.position.x, camera.position.y, camera.position.z], q: [camera.quaternion.x, camera.quaternion.y, camera.quaternion.z, camera.quaternion.w], fov: fovNow, valid: true };
276
+ model.drawnView = { yaw: model.view.yaw, tilt: model.view.tilt, zoom: model.view.zoom };
277
+ model.heightPx = size.height;
278
+ // how many pixels across each body is: for the pictures it needs and for what is chosen
279
+ const half = Math.tan((fovNow / 2) * RAD);
280
+ const radiusPxOf = (id) => {
281
+ const d = Math.hypot(pos[id][0] - camera.position.x, pos[id][1] - camera.position.y, pos[id][2] - camera.position.z);
282
+ const r = radiusOf(id);
283
+ return d <= r * 1.001 ? 1e5 : ((size.height / 2) * (r / Math.sqrt(d * d - r * r))) / half;
284
+ };
285
+ const px = {};
286
+ for (const id of SPACE_BODIES)
287
+ px[id] = radiusPxOf(id);
288
+ model.drawn = { frame, distance, radiusPx: px.earth };
289
+ let motion = 0;
290
+ for (const id of SPACE_BODIES)
291
+ motion = Math.max(motion, (Math.abs(BODIES[id].prime.rate) / 86_400) * RAD * Math.min(px[id], size.height));
292
+ model.motion = motion;
293
+ // the bodies, drawn by the painter, one slot each
294
+ let paintedBodies = 0;
295
+ for (const id of SPACE_BODIES) {
296
+ const slot = slotOf(id);
297
+ fill(id, slot);
298
+ const drawing = slot.drawing;
299
+ if (!drawing)
300
+ continue;
301
+ paintedBodies += 1;
302
+ drawing.object.visible = id !== hiddenBody;
303
+ drawing.object.position.set(pos[id][0], pos[id][1], pos[id][2]);
304
+ drawing.object.scale.setScalar(radiusOf(id));
305
+ if (drawing.billboard)
306
+ drawing.object.quaternion.copy(camera.quaternion);
307
+ const lit = id === "earth" && model.light === "lit";
308
+ const toSun = lit ? unit([camera.position.x, camera.position.y, camera.position.z]) : toSunOf(id);
309
+ drawing.update({ toSun, ms: model.time, radiusPx: px[id] });
310
+ }
311
+ model.painted.bodies = paintedBodies;
312
+ // the planets' orbits round the Sun
313
+ for (const id of SPACE_PLANETS) {
314
+ const o = orbits.get(id);
315
+ o.line.visible = model.orbits;
316
+ if (!model.orbits)
317
+ continue;
318
+ if (!Number.isFinite(o.epoch) || Math.abs(model.time - o.epoch) > 365 * 86_400_000) {
319
+ o.samples = planetOrbit(id, model.time, ORBIT_POINTS);
320
+ o.epoch = model.time;
321
+ }
322
+ for (let i = 0; i <= ORBIT_POINTS; i += 1) {
323
+ const p = placeAround(mode, sunPos, o.samples[i]);
324
+ o.positions[i * 3] = p[0];
325
+ o.positions[i * 3 + 1] = p[1];
326
+ o.positions[i * 3 + 2] = p[2];
327
+ }
328
+ o.line.geometry.getAttribute("position").needsUpdate = true;
329
+ }
330
+ // the moons of a planet, once it is big enough on the screen to show them
331
+ const now = performance.now();
332
+ const seen = new Set();
333
+ for (const id of SPACE_PLANETS) {
334
+ const planet = slotOf(id).drawing;
335
+ if (!planet || px[id] < MOON_MIN_PLANET_PX)
336
+ continue;
337
+ const radius = radiusOf(id);
338
+ for (const site of deps.painter().moons(id, model.time)) {
339
+ const at = [pos[id][0] + site.offset[0] * radius, pos[id][1] + site.offset[1] * radius, pos[id][2] + site.offset[2] * radius];
340
+ projected.set(at[0], at[1], at[2]).project(camera);
341
+ if (projected.z > 1 || Math.abs(projected.x) > 1.25 || Math.abs(projected.y) > 1.25)
342
+ continue;
343
+ const key = site.name.toLowerCase();
344
+ if (key === hiddenMoon)
345
+ continue;
346
+ let held = moonSlots.get(key);
347
+ if (!held)
348
+ moonSlots.set(key, (held = { slot: { drawing: undefined, look: null, failed: false }, seen: now }));
349
+ held.seen = now;
350
+ seen.add(key);
351
+ fill(key, held.slot);
352
+ const drawing = held.slot.drawing;
353
+ if (!drawing)
354
+ continue;
355
+ const away = Math.hypot(at[0] - camera.position.x, at[1] - camera.position.y, at[2] - camera.position.z);
356
+ const drawn = Math.max(site.radius * radius, (away * half * MOON_MIN_PX) / (size.height / 2));
357
+ drawing.object.visible = true;
358
+ drawing.object.position.set(at[0], at[1], at[2]);
359
+ drawing.object.scale.setScalar(drawn);
360
+ const moonPx = away <= drawn * 1.001 ? 1e5 : ((size.height / 2) * (drawn / Math.sqrt(away * away - drawn * drawn))) / half;
361
+ drawing.update({ toSun: toSunOf(id), ms: model.time, radiusPx: moonPx });
362
+ }
363
+ }
364
+ let soonest = Number.POSITIVE_INFINITY;
365
+ for (const [key, held] of moonSlots) {
366
+ if (seen.has(key))
367
+ continue;
368
+ if (held.slot.drawing)
369
+ held.slot.drawing.object.visible = false;
370
+ if (now - held.seen > MOON_KEEP_MS) {
371
+ empty(held.slot);
372
+ moonSlots.delete(key);
373
+ }
374
+ else
375
+ soonest = Math.min(soonest, held.seen + MOON_KEEP_MS - now);
376
+ }
377
+ // a view at rest draws nothing, so a moon that has left the view would be kept for ever: ask for one more draw when the first of them is due to be let go
378
+ if (releaseTimer !== undefined)
379
+ clearTimeout(releaseTimer);
380
+ releaseTimer = soonest < Number.POSITIVE_INFINITY ? setTimeout(() => deps.invalidate(), soonest + 50) : undefined;
381
+ let paintedMoons = 0;
382
+ for (const held of moonSlots.values())
383
+ if (held.slot.drawing?.object.visible)
384
+ paintedMoons += 1;
385
+ model.painted.moons = paintedMoons;
386
+ model.painted.held = moonSlots.size;
387
+ if (deps.onLabels && model.labels) {
388
+ const places = {};
389
+ for (const id of SPACE_BODIES) {
390
+ const at = id === hiddenBody || (rideFrom && id === rideFrom.around) ? null : labelFor(pos[id]);
391
+ places[id] = at ? { x: at.x, y: at.y, r: px[id] } : null;
392
+ }
393
+ deps.onLabels(places);
394
+ }
395
+ if (model.rideLost)
396
+ deps.onRideLost?.();
397
+ if (model.transition)
398
+ deps.onMoving?.();
399
+ return false;
400
+ },
401
+ resize(w, h) {
402
+ size = { width: w, height: h };
403
+ },
404
+ dispose() {
405
+ if (releaseTimer !== undefined)
406
+ clearTimeout(releaseTimer);
407
+ ended = true;
408
+ for (const slot of slots.values())
409
+ empty(slot);
410
+ slots.clear();
411
+ for (const held of moonSlots.values())
412
+ empty(held.slot);
413
+ moonSlots.clear();
414
+ for (const o of orbits.values()) {
415
+ o.line.geometry.dispose();
416
+ o.line.material.dispose();
417
+ }
418
+ },
419
+ };
420
+ },
421
+ };
422
+ }