@johnmorrisdotca/wakusei-planets 0.28.0 → 0.31.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 (56) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/NOTICE.md +20 -0
  3. package/README.md +61 -16
  4. package/dist/embed.d.ts +64 -0
  5. package/dist/embed.js +66 -0
  6. package/dist/index.d.ts +4 -1
  7. package/dist/index.js +4 -1
  8. package/dist/lineup/data.d.ts +160 -0
  9. package/dist/lineup/data.js +303 -0
  10. package/dist/lineup/element.d.ts +99 -0
  11. package/dist/lineup/element.js +782 -0
  12. package/dist/lineup/labels.d.ts +141 -0
  13. package/dist/lineup/labels.js +155 -0
  14. package/dist/lineup/layout.d.ts +174 -0
  15. package/dist/lineup/layout.js +162 -0
  16. package/dist/lineup/scene3d.d.ts +47 -0
  17. package/dist/lineup/scene3d.js +155 -0
  18. package/dist/portrait/element.d.ts +5 -2
  19. package/dist/portrait/element.js +57 -6
  20. package/dist/portrait/scene3d.d.ts +13 -0
  21. package/dist/portrait/scene3d.js +21 -2
  22. package/dist/scene/body.d.ts +3 -1
  23. package/dist/scene/body.js +2 -1
  24. package/dist/scene/detail.d.ts +8 -2
  25. package/dist/scene/detail.js +56 -28
  26. package/dist/scene/painter.d.ts +32 -0
  27. package/dist/scene/painter.js +94 -10
  28. package/dist/scene/textures.d.ts +4 -2
  29. package/dist/scene/textures.js +2 -0
  30. package/dist/scene/tiles.d.ts +46 -0
  31. package/dist/scene/tiles.js +165 -19
  32. package/dist/space/arrows.d.ts +87 -0
  33. package/dist/space/arrows.js +117 -0
  34. package/dist/space/beside.d.ts +74 -0
  35. package/dist/space/beside.js +76 -0
  36. package/dist/space/bodies.d.ts +83 -0
  37. package/dist/space/bodies.js +93 -0
  38. package/dist/space/clock.d.ts +31 -0
  39. package/dist/space/clock.js +45 -0
  40. package/dist/space/element.d.ts +98 -15
  41. package/dist/space/element.js +320 -63
  42. package/dist/space/frame.d.ts +45 -2
  43. package/dist/space/frame.js +56 -4
  44. package/dist/space/index.d.ts +6 -1
  45. package/dist/space/index.js +6 -1
  46. package/dist/space/labels.d.ts +52 -0
  47. package/dist/space/labels.js +40 -0
  48. package/dist/space/scale.d.ts +5 -5
  49. package/dist/space/scale.js +10 -4
  50. package/dist/space/scene.d.ts +50 -9
  51. package/dist/space/scene.js +313 -36
  52. package/dist/space/spin.d.ts +37 -0
  53. package/dist/space/spin.js +29 -0
  54. package/dist/version.d.ts +1 -1
  55. package/dist/version.js +1 -1
  56. package/package.json +8 -1
@@ -1,24 +1,42 @@
1
- import { BODIES } from "../facts.js";
1
+ import { BODIES, MOONS_OF } from "../facts.js";
2
2
  import { moonOffset } from "../scene/orient.js";
3
3
  import {} from "../scene/painter.js";
4
4
  import "../bodies/all.js";
5
5
  import { groundDirection } from "./aim.js";
6
- import { createTrack, planetOrbit, SPACE_PLANETS } from "./frame.js";
6
+ import { hostOf } from "./bodies.js";
7
+ import { arrowSites, ARROWS_PER_ORBIT, chevron, spinArrow } from "./arrows.js";
8
+ import { besideMoon } from "./beside.js";
9
+ import { spinAxis } from "./spin.js";
10
+ import { createTrack, moonOrbit, planetOrbit, SPACE_DWARFS, SPACE_PLANETS } from "./frame.js";
7
11
  import { ridePose } from "./ride.js";
8
- import { moonDistance, orbitLine, placeBodies, radiusIn } from "./scale.js";
12
+ import { moonDistance, orbitLine, placeBodies, placeMoon, radiusIn } from "./scale.js";
9
13
  const RAD = Math.PI / 180;
10
14
  /** 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;
15
+ export const FIT_DISTANCE = 4.1;
12
16
  const FOV = 35;
13
17
  const ORBIT_POINTS = 96;
14
- const ORBIT_BODIES = ["earth", ...SPACE_PLANETS];
18
+ const ORBIT_BODIES = ["earth", ...SPACE_PLANETS, ...SPACE_DWARFS, "moon"];
19
+ /** A dwarf planet is drawn once it is this many CSS pixels across its radius, or when it is the body looked at: Sedna at true scale is never anything but a speck, and its picture is not made for it. */
20
+ const DWARF_MIN_PX = 0.4;
21
+ /** A colour as three numbers from 0 to 1, from `#rrggbb`. */
22
+ const rgb = (hex) => [parseInt(hex.slice(1, 3), 16) / 255, parseInt(hex.slice(3, 5), 16) / 255, parseInt(hex.slice(5, 7), 16) / 255];
23
+ /** How long each arrowhead on an orbit line is, in CSS pixels: small, so it reads at every zoom and never hides a body. */
24
+ const ARROW_PX = 9;
25
+ /** A body whose disc is this many CSS pixels across its radius is "close": the orbit lines that are not its own are hidden. */
26
+ const CLOSE_UP_PX = 30;
27
+ /** How long the Moon's ring is kept before it is worked out again from the Moon's place, in milliseconds: its path changes by a couple of hundred kilometres in that time. */
28
+ const MOON_RING_KEEP_MS = 5 * 86_400_000;
29
+ /** How many radii across the picture of a body is at zoom 1 when it has more than its globe: Saturn's rings (to 2.27 radii) are inside the view, not cut by it. */
30
+ const FRAMED = { saturn: 2.3 };
15
31
  /** 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. */
16
32
  const MOON_MIN_PLANET_PX = 4;
17
33
  const MOON_MIN_PX = 2.2;
34
+ /** ...or a planet's moons are shown when the planet is smaller than that but its farthest moon is this many CSS pixels from it. */
35
+ const MOON_MIN_SYSTEM_PX = 30;
18
36
  /** A moon that has been out of sight this long is let go (its pictures too). */
19
37
  const MOON_KEEP_MS = 4000;
20
38
  /**
21
- * 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.
39
+ * The nineteen bodies the space view places, names and lets a reader choose: the Earth, the Sun, the Moon, the seven other planets, Pluto and the eight other dwarf planets. The moons of the planets and of the dwarf planets are placed round their hosts.
22
40
  *
23
41
  * @example
24
42
  * ```ts no-run
@@ -27,10 +45,10 @@ const MOON_KEEP_MS = 4000;
27
45
  * console.log(SPACE_BODIES.join(", "));
28
46
  * ```
29
47
  */
30
- export const SPACE_BODIES = ["earth", "sun", "moon", ...SPACE_PLANETS];
48
+ export const SPACE_BODIES = ["earth", "sun", "moon", ...SPACE_PLANETS, ...SPACE_DWARFS];
31
49
  export const newSpaceModel = () => ({
32
50
  time: Date.now(),
33
- mode: "compressed",
51
+ mode: "true",
34
52
  view: { yaw: 0, tilt: 0, zoom: 1, roll: 0, panX: 0, panY: 0 },
35
53
  centre: { lon: 0, lat: 0 },
36
54
  anchor: null,
@@ -42,6 +60,12 @@ export const newSpaceModel = () => ({
42
60
  look: "realistic",
43
61
  drawn: null,
44
62
  painted: { bodies: 0, moons: 0, held: 0 },
63
+ tiles: { requested: 0, loading: 0, failed: 0, shown: null },
64
+ rings: { drawn: [], miss: {}, hidden: [] },
65
+ arrows: {},
66
+ help: false,
67
+ arrowPx: 0,
68
+ helpDrawn: null,
45
69
  lookups: 0,
46
70
  motion: 0,
47
71
  drawnView: null,
@@ -54,11 +78,26 @@ export const newSpaceModel = () => ({
54
78
  rideMotion: 0,
55
79
  ratio: () => Math.min(globalThis.devicePixelRatio || 1, 2),
56
80
  });
81
+ const minus = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
82
+ /** How far a place is from the nearest point of a line through `points`, in the line's units. */
83
+ const plus = (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
84
+ function distanceToLine(place, points) {
85
+ let best = Number.POSITIVE_INFINITY;
86
+ for (let i = 0; i + 1 < points.length; i += 1) {
87
+ const a = points[i];
88
+ const b = points[i + 1];
89
+ const ab = minus(b, a);
90
+ const am = minus(place, a);
91
+ const len2 = ab[0] * ab[0] + ab[1] * ab[1] + ab[2] * ab[2];
92
+ const t = len2 === 0 ? 0 : Math.max(0, Math.min(1, (am[0] * ab[0] + am[1] * ab[1] + am[2] * ab[2]) / len2));
93
+ best = Math.min(best, Math.hypot(place[0] - (a[0] + ab[0] * t), place[1] - (a[1] + ab[1] * t), place[2] - (a[2] + ab[2] * t)));
94
+ }
95
+ return best;
96
+ }
57
97
  const unit = (v) => {
58
98
  const l = Math.hypot(v[0], v[1], v[2]) || 1;
59
99
  return [v[0] / l, v[1] / l, v[2] / l];
60
100
  };
61
- const minus = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
62
101
  export function createSpaceItem(model, deps) {
63
102
  return {
64
103
  key: "wakusei-planets:space",
@@ -88,7 +127,7 @@ export function createSpaceItem(model, deps) {
88
127
  empty(slot);
89
128
  slot.look = model.look;
90
129
  try {
91
- const made = deps.painter().make(three, id, { look: model.look, invalidate: () => deps.invalidate(), ratio: model.ratio(), maxTexture: deps.cap() });
130
+ const made = deps.painter().make(three, id, { look: model.look, mapped: true, invalidate: () => deps.invalidate(), ratio: model.ratio(), maxTexture: deps.cap() });
92
131
  if (ended) {
93
132
  made.dispose();
94
133
  return;
@@ -107,15 +146,54 @@ export function createSpaceItem(model, deps) {
107
146
  slots.set(id, (slot = { drawing: undefined, look: null, failed: false }));
108
147
  return slot;
109
148
  };
149
+ // each line has its colour and a fade (the fourth number of the colour) at every point, so that near the body looked at the part of a line that would cross it is let go
110
150
  const orbits = new Map();
111
151
  for (const id of ORBIT_BODIES) {
112
152
  const positions = new Float32Array((ORBIT_POINTS + 1) * 3);
153
+ const colours = new Float32Array((ORBIT_POINTS + 1) * 4);
154
+ const base = rgb(id === "earth" ? "#6aa8ff" : id === "moon" ? "#c5cdd9" : BODIES[id].colour);
155
+ for (let i = 0; i <= ORBIT_POINTS; i += 1)
156
+ colours.set([base[0], base[1], base[2], 1], i * 4);
113
157
  const geometry = new three.BufferGeometry();
114
158
  geometry.setAttribute("position", new three.BufferAttribute(positions, 3));
115
- const line = new three.Line(geometry, new three.LineBasicMaterial({ color: id === "earth" ? "#6aa8ff" : BODIES[id].colour, transparent: true, opacity: 0.55 }));
159
+ geometry.setAttribute("color", new three.BufferAttribute(colours, 4));
160
+ const line = new three.Line(geometry, new three.LineBasicMaterial({ vertexColors: true, transparent: true, opacity: 0.55 }));
116
161
  line.frustumCulled = false;
117
162
  scene.add(line);
118
- orbits.set(id, { line, positions, samples: [], epoch: Number.NaN });
163
+ orbits.set(id, { line, positions, colours, faded: false, samples: [], epoch: Number.NaN });
164
+ }
165
+ // the arrowheads of every orbit line: two strokes each, in the colour of the line, in one object; and the help mode's axis and curved arrow in another (drawn over the body, not hidden by it)
166
+ const ARROW_VERTICES = ORBIT_BODIES.length * ARROWS_PER_ORBIT * 4;
167
+ const arrowPositions = new Float32Array(ARROW_VERTICES * 3);
168
+ const arrowColours = new Float32Array(ARROW_VERTICES * 3);
169
+ const arrowGeometry = new three.BufferGeometry();
170
+ arrowGeometry.setAttribute("position", new three.BufferAttribute(arrowPositions, 3));
171
+ arrowGeometry.setAttribute("color", new three.BufferAttribute(arrowColours, 3));
172
+ const arrowLines = new three.LineSegments(arrowGeometry, new three.LineBasicMaterial({ vertexColors: true, transparent: true, opacity: 0.95 }));
173
+ arrowLines.frustumCulled = false;
174
+ scene.add(arrowLines);
175
+ ORBIT_BODIES.forEach((id, k) => {
176
+ const c = rgb(id === "earth" ? "#6aa8ff" : id === "moon" ? "#c5cdd9" : BODIES[id].colour);
177
+ for (let v = 0; v < ARROWS_PER_ORBIT * 4; v += 1)
178
+ arrowColours.set(c, (k * ARROWS_PER_ORBIT * 4 + v) * 3);
179
+ });
180
+ const HELP_SEGMENTS = 28;
181
+ const HELP_VERTICES = (1 + HELP_SEGMENTS + 2) * 2;
182
+ const helpPositions = new Float32Array(HELP_VERTICES * 3);
183
+ const helpColours = new Float32Array(HELP_VERTICES * 3);
184
+ const helpGeometry = new three.BufferGeometry();
185
+ helpGeometry.setAttribute("position", new three.BufferAttribute(helpPositions, 3));
186
+ helpGeometry.setAttribute("color", new three.BufferAttribute(helpColours, 3));
187
+ const helpLines = new three.LineSegments(helpGeometry, new three.LineBasicMaterial({ vertexColors: true, transparent: true, opacity: 0.95, depthTest: false }));
188
+ helpLines.frustumCulled = false;
189
+ helpLines.renderOrder = 10;
190
+ helpLines.visible = false;
191
+ scene.add(helpLines);
192
+ const AXIS_COLOUR = rgb("#9fd0ff");
193
+ const SPIN_COLOUR = rgb("#ffd24a");
194
+ for (let v = 0; v < HELP_VERTICES; v += 1) {
195
+ const c = v < 2 ? AXIS_COLOUR : SPIN_COLOUR;
196
+ helpColours.set(c, v * 3);
119
197
  }
120
198
  const moonSlots = new Map();
121
199
  let releaseTimer;
@@ -129,6 +207,7 @@ export function createSpaceItem(model, deps) {
129
207
  scene,
130
208
  camera,
131
209
  update() {
210
+ deps.advance?.();
132
211
  const frame = track.at(model.time);
133
212
  model.lookups = track.lookups;
134
213
  if (model.stepMs !== trackStep) {
@@ -137,9 +216,7 @@ export function createSpaceItem(model, deps) {
137
216
  }
138
217
  const mode = model.mode;
139
218
  const pos = placeBodies(mode, frame);
140
- const sunPos = pos.sun;
141
219
  const radiusOf = (id) => radiusIn(id, mode);
142
- const placeAll = (at) => placeBodies(mode, at);
143
220
  // where a moon is round its planet, as the view draws it: in the planet's radii, drawn nearer in a compressed view where the real distance would cross the next planet's orbit
144
221
  const moonSite = (id, at, host) => {
145
222
  const real = moonOffset(id, at);
@@ -147,15 +224,50 @@ export function createSpaceItem(model, deps) {
147
224
  const k = d === 0 ? 1 : moonDistance(mode, host, d) / d;
148
225
  return [real[0] * k, real[1] * k, real[2] * k];
149
226
  };
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]));
227
+ // The places are in Earth radii from the Earth's centre, which at a dwarf planet is millions: a body a few hundredths of a radius across (Phobos, Eris seen close) would be lost to the GPU's single-precision
228
+ // numbers, which the bodies' shaders use for world positions. So the whole scene is moved to put the body looked at (or the target of a ride) at the origin: every place below is relative to it, and the
229
+ // camera's place that is reported, and kept for a glide, has it added back.
230
+ const absoluteSite = (id) => {
231
+ if (SPACE_BODIES.includes(id))
232
+ return pos[id];
233
+ const host = (hostOf(id) ?? "earth");
234
+ const offset = moonSite(id, model.time, host);
235
+ const hostRadius = radiusOf(host);
236
+ return [pos[host][0] + offset[0] * hostRadius, pos[host][1] + offset[1] * hostRadius, pos[host][2] + offset[2] * hostRadius];
237
+ };
238
+ const held = model.ride;
239
+ const origin = absoluteSite(held ? (typeof held.target === "string" ? held.target : held.around) : model.focus);
240
+ for (const id of Object.keys(pos))
241
+ pos[id] = minus(pos[id], origin);
242
+ const sunPos = pos.sun;
243
+ const placeAll = (at) => {
244
+ const placed = placeBodies(mode, at);
245
+ for (const id of Object.keys(placed))
246
+ placed[id] = minus(placed[id], origin);
247
+ return placed;
248
+ };
249
+ const toSunOf = (id) => unit(minus(frame.sun, id === "earth" ? [0, 0, 0] : id === "moon" ? frame.moon : id === "sun" ? [0, 0, 0] : SPACE_DWARFS.includes(id) ? frame.dwarfs[id] : frame.planets[id]));
151
250
  // 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
251
  // 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
252
  model.rideLost = false;
154
253
  model.rideMotion = 0;
155
254
  let focus = model.focus;
156
- let focusPos = pos[focus];
157
- let focusRadius = radiusOf(focus);
255
+ let focusPos;
256
+ let focusRadius;
257
+ if (SPACE_BODIES.includes(focus)) {
258
+ focusPos = pos[focus];
259
+ focusRadius = radiusOf(focus);
260
+ }
261
+ else {
262
+ // a moon of a planet: its place round the planet, in the planet's radii as the view draws them
263
+ const host = (hostOf(focus) ?? "earth");
264
+ const offset = moonSite(focus, model.time, host);
265
+ const hostRadius = radiusOf(host);
266
+ focusPos = [pos[host][0] + offset[0] * hostRadius, pos[host][1] + offset[1] * hostRadius, pos[host][2] + offset[2] * hostRadius];
267
+ focusRadius = Math.max(1e-9, (BODIES[focus].radiusKm / BODIES[host].radiusKm) * hostRadius);
268
+ }
158
269
  let aimWay = 1;
270
+ let besideIn = null;
159
271
  let rideFrom = null;
160
272
  const ride = model.ride;
161
273
  if (ride) {
@@ -170,7 +282,15 @@ export function createSpaceItem(model, deps) {
170
282
  let hiddenMoon = null;
171
283
  if (typeof t === "string" && SPACE_BODIES.includes(t)) {
172
284
  const id = t;
173
- if (ride.view === "follow") {
285
+ if (ride.view === "beside") {
286
+ // beyond the Moon and off the line to the Earth, the Earth in view behind it
287
+ if (id === "moon")
288
+ besideIn = { moon: pos.moon, moonRadius: radiusOf("moon"), host: pos.earth, hostRadius: radiusOf("earth"), sun: pos.sun };
289
+ focus = id;
290
+ focusPos = pos[id];
291
+ focusRadius = radiusOf(id);
292
+ }
293
+ else if (ride.view === "follow") {
174
294
  focus = id;
175
295
  focusPos = pos[id];
176
296
  focusRadius = radiusOf(id);
@@ -179,7 +299,8 @@ export function createSpaceItem(model, deps) {
179
299
  const ends = track.ends();
180
300
  const was = ends ? placeAll(ends[0])[id] : pos[id];
181
301
  const will = ends ? placeAll(ends[1])[id] : pos[id];
182
- position = pos[id];
302
+ // from the centre of the body it goes round (the places are relative to the origin the scene was moved to)
303
+ position = minus(pos[id], centre);
183
304
  velocity = minus(will, was);
184
305
  perSecond = ends ? Math.max(1e-3, (ends[1].ms - ends[0].ms) / 1000) : 1;
185
306
  hiddenBody = id;
@@ -189,7 +310,15 @@ export function createSpaceItem(model, deps) {
189
310
  const id = t;
190
311
  const offset = moonSite(id, model.time, around);
191
312
  const later = moonSite(id, model.time + 1000, around);
192
- if (ride.view === "follow") {
313
+ if (ride.view === "beside") {
314
+ const there = [centre[0] + offset[0] * unitWorld, centre[1] + offset[1] * unitWorld, centre[2] + offset[2] * unitWorld];
315
+ const radius = Math.max(1e-6, (BODIES[id].radiusKm / BODIES[around].radiusKm) * unitWorld);
316
+ besideIn = { moon: there, moonRadius: radius, host: centre, hostRadius: unitWorld, sun: pos.sun };
317
+ focus = id;
318
+ focusPos = there;
319
+ focusRadius = radius;
320
+ }
321
+ else if (ride.view === "follow") {
193
322
  focusPos = [centre[0] + offset[0] * unitWorld, centre[1] + offset[1] * unitWorld, centre[2] + offset[2] * unitWorld];
194
323
  focusRadius = Math.max(1e-6, (BODIES[id].radiusKm / BODIES[around].radiusKm) * unitWorld);
195
324
  }
@@ -222,7 +351,17 @@ export function createSpaceItem(model, deps) {
222
351
  }
223
352
  let fovNow = FOV;
224
353
  let distance = 0;
225
- if (rideFrom) {
354
+ if (besideIn) {
355
+ const view = besideMoon({ ...besideIn, aspect: size.width / Math.max(1, size.height), zoom: model.view.zoom, yaw: model.view.yaw, tilt: model.view.tilt });
356
+ camera.position.set(view.position[0], view.position[1], view.position[2]);
357
+ camera.up.set(view.up[0], view.up[1], view.up[2]);
358
+ camera.lookAt(view.position[0] + view.forward[0], view.position[1] + view.forward[1], view.position[2] + view.forward[2]);
359
+ fovNow = view.fov;
360
+ distance = view.distance;
361
+ camera.near = Math.max(1e-4, (view.distance - besideIn.moonRadius) * 0.1);
362
+ camera.far = Math.max(2000, 400 + (mode === "true" ? 40 * 23_481 : 0));
363
+ }
364
+ else if (rideFrom) {
226
365
  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
366
  const { centre, unitWorld } = rideFrom;
228
367
  camera.position.set(centre[0] + pose.position[0] * unitWorld, centre[1] + pose.position[1] * unitWorld, centre[2] + pose.position[2] * unitWorld);
@@ -244,7 +383,9 @@ export function createSpaceItem(model, deps) {
244
383
  const lon = model.centre.lon - aimWay * model.view.yaw;
245
384
  const lat = Math.max(-90, Math.min(90, model.centre.lat - aimWay * model.view.tilt));
246
385
  const aim = groundDirection(lon, lat, model.anchor);
247
- distance = Math.max(1.05 * focusRadius, (FIT_DISTANCE * focusRadius) / Math.max(1e-9, model.view.zoom));
386
+ // at zoom 1 the whole picture of the body fits the shorter side of the view, in any shape: a tall view is as far again as it is taller than wide, and a ringed body is framed with its rings
387
+ const across = focusRadius * (FRAMED[focus] ?? 1) * Math.max(1, size.height / Math.max(1, size.width));
388
+ distance = Math.max(1.05 * focusRadius, (FIT_DISTANCE * across) / Math.max(1e-9, model.view.zoom));
248
389
  target.set(focusPos[0], focusPos[1], focusPos[2]);
249
390
  camera.position.set(target.x + aim[0] * distance, target.y + aim[1] * distance, target.z + aim[2] * distance);
250
391
  camera.up.set(0, 1, 0);
@@ -258,7 +399,9 @@ export function createSpaceItem(model, deps) {
258
399
  const k = Math.min(1, (performance.now() - glide.start) / glide.duration);
259
400
  const ease = k * k * (3 - 2 * k);
260
401
  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);
402
+ // the pose the last draw left is absolute (from the Earth's centre); the scene is now relative to the new origin
403
+ const [fx, fy, fz] = [glide.from.p[0] - origin[0], glide.from.p[1] - origin[1], glide.from.p[2] - origin[2]];
404
+ camera.position.set(fx + (camera.position.x - fx) * ease, fy + (camera.position.y - fy) * ease, fz + (camera.position.z - fz) * ease);
262
405
  fromQ.set(glide.from.q[0], glide.from.q[1], glide.from.q[2], glide.from.q[3]);
263
406
  camera.quaternion.copy(fromQ.slerp(desiredQ, ease));
264
407
  fovNow = glide.from.fov + (fovNow - glide.from.fov) * ease;
@@ -278,8 +421,8 @@ export function createSpaceItem(model, deps) {
278
421
  camera.updateMatrixWorld();
279
422
  const hiddenBody = rideFrom?.hiddenBody ?? null;
280
423
  const hiddenMoon = rideFrom?.hiddenMoon ?? null;
281
- model.camera = { position: [camera.position.x, camera.position.y, camera.position.z], fov: fovNow };
282
- 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 };
424
+ model.camera = { position: [camera.position.x + origin[0], camera.position.y + origin[1], camera.position.z + origin[2]], fov: fovNow };
425
+ model.pose = { p: [camera.position.x + origin[0], camera.position.y + origin[1], camera.position.z + origin[2]], q: [camera.quaternion.x, camera.quaternion.y, camera.quaternion.z, camera.quaternion.w], fov: fovNow, valid: true };
283
426
  model.drawnView = { yaw: model.view.yaw, tilt: model.view.tilt, zoom: model.view.zoom, roll: model.view.roll, panX: model.view.panX, panY: model.view.panY };
284
427
  model.heightPx = size.height;
285
428
  // how many pixels across each body is: for the pictures it needs and for what is chosen
@@ -301,6 +444,13 @@ export function createSpaceItem(model, deps) {
301
444
  let paintedBodies = 0;
302
445
  for (const id of SPACE_BODIES) {
303
446
  const slot = slotOf(id);
447
+ // a dwarf planet too small to see is not made, and one that was made but has shrunk away is hidden
448
+ const unseen = SPACE_DWARFS.includes(id) && px[id] < DWARF_MIN_PX && focus !== id && model.focus !== id && hostOf(model.focus) !== id;
449
+ if (unseen) {
450
+ if (slot.drawing)
451
+ slot.drawing.object.visible = false;
452
+ continue;
453
+ }
304
454
  fill(id, slot);
305
455
  const drawing = slot.drawing;
306
456
  if (!drawing)
@@ -313,43 +463,151 @@ export function createSpaceItem(model, deps) {
313
463
  drawing.object.quaternion.copy(camera.quaternion);
314
464
  const lit = id !== "sun" && model.light === "lit";
315
465
  const toSun = lit ? unit([camera.position.x - pos[id][0], camera.position.y - pos[id][1], camera.position.z - pos[id][2]]) : toSunOf(id);
316
- drawing.update({ toSun, ms: model.time, radiusPx: px[id] });
466
+ drawing.update({ toSun, ms: model.time, radiusPx: px[id], view: { camera, width: size.width, height: size.height } });
317
467
  }
318
468
  model.painted.bodies = paintedBodies;
319
469
  // the orbits round the Sun, placed by the same mapping as the bodies
470
+ const drawnRings = [];
471
+ const hiddenRings = [];
472
+ const miss = {};
473
+ // Close to a body (its disc is a few dozen pixels across) the other orbit lines are seen edge-on, long straight scratches across it: only the lines of its own system are kept, and of those the part near it fades
474
+ const nearId = (rideFrom ? rideFrom.around : SPACE_BODIES.includes(focus) ? focus : (hostOf(focus) ?? "earth"));
475
+ const closeUp = px[nearId] >= CLOSE_UP_PX;
476
+ const ownLine = (id) => id === nearId || (id === "moon" && (nearId === "earth" || nearId === "moon"));
477
+ const nearRadius = radiusOf(nearId);
320
478
  for (const id of ORBIT_BODIES) {
321
479
  const o = orbits.get(id);
322
- o.line.visible = model.orbits;
480
+ const hide = closeUp && !ownLine(id);
481
+ o.line.visible = model.orbits && !hide;
323
482
  if (!model.orbits)
324
483
  continue;
325
- if (!Number.isFinite(o.epoch) || Math.abs(model.time - o.epoch) > 365 * 86_400_000) {
326
- o.samples = planetOrbit(id, model.time, ORBIT_POINTS);
484
+ const keep = id === "moon" ? MOON_RING_KEEP_MS : 365 * 86_400_000;
485
+ if (!Number.isFinite(o.epoch) || Math.abs(model.time - o.epoch) > keep) {
486
+ o.samples = id === "moon" ? moonOrbit(model.time, ORBIT_POINTS) : planetOrbit(id, model.time, ORBIT_POINTS);
327
487
  o.epoch = model.time;
328
488
  }
329
- const line = orbitLine(mode, sunPos, o.samples);
489
+ // the Moon's ring is round the Earth, which is the middle of the view; the others are round the Sun
490
+ const line = id === "moon" ? o.samples.map((p) => plus(placeMoon(mode, p), pos.earth)) : orbitLine(mode, sunPos, o.samples);
330
491
  for (let i = 0; i <= ORBIT_POINTS; i += 1) {
331
492
  o.positions[i * 3] = line[i][0];
332
493
  o.positions[i * 3 + 1] = line[i][1];
333
494
  o.positions[i * 3 + 2] = line[i][2];
334
495
  }
335
496
  o.line.geometry.getAttribute("position").needsUpdate = true;
497
+ // the part of its own line near the body is let go, from 3 radii out to 12 (a close view sees a few radii across): never a line across the disc
498
+ const fade = closeUp && ownLine(id);
499
+ if (fade || o.faded) {
500
+ for (let i = 0; i <= ORBIT_POINTS; i += 1) {
501
+ const away = Math.hypot(line[i][0] - pos[nearId][0], line[i][1] - pos[nearId][1], line[i][2] - pos[nearId][2]) / nearRadius;
502
+ const t = Math.max(0, Math.min(1, (away - 3) / 9));
503
+ o.colours[i * 4 + 3] = fade ? t * t * (3 - 2 * t) : 1;
504
+ }
505
+ o.line.geometry.getAttribute("color").needsUpdate = true;
506
+ o.faded = fade;
507
+ }
508
+ if (hide)
509
+ hiddenRings.push(id);
510
+ // how far the body is from its own line, against the line's size: the check that it sits on it
511
+ const centre = id === "moon" ? pos.earth : sunPos;
512
+ const radius = Math.hypot(...minus(line[0], centre)) || 1;
513
+ drawnRings.push(id);
514
+ miss[id] = distanceToLine(pos[id], line) / radius;
515
+ }
516
+ model.rings = { drawn: drawnRings, miss, hidden: hiddenRings };
517
+ // the arrowheads on the lines: small in screen pixels at the place, pointing the way the body goes (the lines run from the body once round in the order it goes, whichever way the clock runs)
518
+ const e = camera.matrixWorld.elements;
519
+ const camRight = [e[0], e[1], e[2]];
520
+ const camUp = [e[4], e[5], e[6]];
521
+ const camForward = [-e[8], -e[9], -e[10]];
522
+ const pixel = (at) => {
523
+ const d = (at[0] - camera.position.x) * camForward[0] + (at[1] - camera.position.y) * camForward[1] + (at[2] - camera.position.z) * camForward[2];
524
+ return d <= 0 ? 0 : (d * 2 * half) / Math.max(1, size.height);
525
+ };
526
+ const arrowsNow = {};
527
+ let measured = 0;
528
+ arrowPositions.fill(0);
529
+ ORBIT_BODIES.forEach((id, k) => {
530
+ const o = orbits.get(id);
531
+ if (!model.orbits || !drawnRings.includes(id) || hiddenRings.includes(id))
532
+ return;
533
+ const line = [];
534
+ for (let i = 0; i <= ORBIT_POINTS; i += 1)
535
+ line.push([o.positions[i * 3], o.positions[i * 3 + 1], o.positions[i * 3 + 2]]);
536
+ const sites = arrowSites(line);
537
+ arrowsNow[id] = sites;
538
+ sites.forEach((site, j) => {
539
+ // an arrow on the faded part of a line goes with it
540
+ if (closeUp && Math.hypot(site.position[0] - pos[nearId][0], site.position[1] - pos[nearId][1], site.position[2] - pos[nearId][2]) < 12 * nearRadius)
541
+ return;
542
+ const head = chevron(site.position, site.direction, camRight, camUp, ARROW_PX * pixel(site.position));
543
+ if (!head)
544
+ return;
545
+ const base = (k * ARROWS_PER_ORBIT + j) * 4 * 3;
546
+ arrowPositions.set([...head[0], ...head[1], ...head[0], ...head[2]], base);
547
+ if (measured === 0) {
548
+ const tip = labelFor(head[0]);
549
+ const wing = labelFor(head[1]);
550
+ if (tip && wing)
551
+ measured = Math.hypot(tip.x - wing.x, tip.y - wing.y);
552
+ }
553
+ });
554
+ });
555
+ model.arrows = arrowsNow;
556
+ model.arrowPx = measured;
557
+ arrowLines.visible = model.orbits;
558
+ arrowGeometry.getAttribute("position").needsUpdate = true;
559
+ arrowGeometry.getAttribute("color").needsUpdate = true;
560
+ // help mode: on the body looked at, the line of its axis and a curved arrow round it the way it turns
561
+ helpLines.visible = model.help;
562
+ model.helpDrawn = null;
563
+ if (model.help) {
564
+ const turn = spinAxis(focus, model.time);
565
+ const spin = spinArrow(focusPos, turn.axis, 1.3 * focusRadius, 0.4 * focusRadius, HELP_SEGMENTS);
566
+ const reach = 1.8 * focusRadius;
567
+ const segments = [
568
+ [focusPos[0] - turn.pole[0] * reach, focusPos[1] - turn.pole[1] * reach, focusPos[2] - turn.pole[2] * reach],
569
+ [focusPos[0] + turn.pole[0] * reach, focusPos[1] + turn.pole[1] * reach, focusPos[2] + turn.pole[2] * reach],
570
+ ];
571
+ for (let i = 0; i < HELP_SEGMENTS; i += 1)
572
+ segments.push(spin.arc[i], spin.arc[i + 1]);
573
+ const tip = spin.arc[HELP_SEGMENTS];
574
+ segments.push(tip, spin.head[0], tip, spin.head[1]);
575
+ segments.forEach((p, i) => helpPositions.set(p, i * 3));
576
+ helpGeometry.getAttribute("position").needsUpdate = true;
577
+ // the axis the drawn arc turns about, measured from its own points, round the middle of its circle
578
+ const middle = [focusPos[0] + turn.axis[0] * 0.4 * focusRadius, focusPos[1] + turn.axis[1] * 0.4 * focusRadius, focusPos[2] + turn.axis[2] * 0.4 * focusRadius];
579
+ let sum = [0, 0, 0];
580
+ for (let i = 0; i < HELP_SEGMENTS; i += 1) {
581
+ const a = minus(spin.arc[i], middle);
582
+ const b = minus(spin.arc[i + 1], middle);
583
+ sum = [sum[0] + a[1] * b[2] - a[2] * b[1], sum[1] + a[2] * b[0] - a[0] * b[2], sum[2] + a[0] * b[1] - a[1] * b[0]];
584
+ }
585
+ model.helpDrawn = { body: focus, axis: turn.axis, pole: turn.pole, retrograde: turn.retrograde, arcTurn: unit(sum) };
336
586
  }
337
587
  // the moons of a planet, once it is big enough on the screen to show them
338
588
  const now = performance.now();
339
589
  const seen = new Set();
340
- for (const id of SPACE_PLANETS) {
590
+ const moonPlaces = {};
591
+ for (const id of [...SPACE_PLANETS, ...SPACE_DWARFS]) {
341
592
  const planet = slotOf(id).drawing;
342
- if (!planet || px[id] < MOON_MIN_PLANET_PX)
593
+ if (!planet)
594
+ continue;
595
+ const sites = (MOONS_OF[id] ?? []).map((moon) => ({ name: moon, offset: moonOffset(moon, model.time), radius: BODIES[moon].radiusKm / BODIES[id].radiusKm }));
596
+ // the moons are shown once the planet is a few pixels across, or the system round it is wide enough on the screen to tell its moons from the planet, or one of its moons is the body looked at
597
+ let spread = 1;
598
+ for (const site of sites)
599
+ spread = Math.max(spread, moonDistance(mode, id, Math.hypot(site.offset[0], site.offset[1], site.offset[2])));
600
+ if (px[id] < MOON_MIN_PLANET_PX && px[id] * spread < MOON_MIN_SYSTEM_PX && hostOf(model.focus) !== id)
343
601
  continue;
344
602
  const radius = radiusOf(id);
345
- for (const site of deps.painter().moons(id, model.time)) {
603
+ for (const site of sites) {
346
604
  const reach = Math.hypot(site.offset[0], site.offset[1], site.offset[2]);
347
605
  const k = reach === 0 ? 1 : moonDistance(mode, id, reach) / reach;
348
606
  const at = [pos[id][0] + site.offset[0] * k * radius, pos[id][1] + site.offset[1] * k * radius, pos[id][2] + site.offset[2] * k * radius];
349
607
  projected.set(at[0], at[1], at[2]).project(camera);
350
608
  if (projected.z > 1 || Math.abs(projected.x) > 1.25 || Math.abs(projected.y) > 1.25)
351
609
  continue;
352
- const key = site.name.toLowerCase();
610
+ const key = site.name;
353
611
  if (key === hiddenMoon)
354
612
  continue;
355
613
  let held = moonSlots.get(key);
@@ -367,7 +625,10 @@ export function createSpaceItem(model, deps) {
367
625
  drawing.object.position.set(at[0], at[1], at[2]);
368
626
  drawing.object.scale.setScalar(drawn);
369
627
  const moonPx = away <= drawn * 1.001 ? 1e5 : ((size.height / 2) * (drawn / Math.sqrt(away * away - drawn * drawn))) / half;
370
- drawing.update({ toSun: model.light === "lit" ? unit([camera.position.x - at[0], camera.position.y - at[1], camera.position.z - at[2]]) : toSunOf(id), ms: model.time, radiusPx: moonPx });
628
+ drawing.update({ toSun: model.light === "lit" ? unit([camera.position.x - at[0], camera.position.y - at[1], camera.position.z - at[2]]) : toSunOf(id), ms: model.time, radiusPx: moonPx, view: { camera, width: size.width, height: size.height } });
629
+ const spot = labelFor(at);
630
+ if (spot)
631
+ moonPlaces[key] = { x: spot.x, y: spot.y, r: moonPx };
371
632
  }
372
633
  }
373
634
  let soonest = Number.POSITIVE_INFINITY;
@@ -393,8 +654,20 @@ export function createSpaceItem(model, deps) {
393
654
  paintedMoons += 1;
394
655
  model.painted.moons = paintedMoons;
395
656
  model.painted.held = moonSlots.size;
657
+ const tiles = { requested: 0, loading: 0, failed: 0, shown: null };
658
+ for (const painting of [...[...slots.values()].map((slot) => slot.drawing), ...[...moonSlots.values()].map((held) => held.slot.drawing)]) {
659
+ const stats = painting?.tiles;
660
+ if (stats === undefined)
661
+ continue;
662
+ tiles.requested += stats.requested;
663
+ tiles.loading += stats.loading;
664
+ tiles.failed += stats.failed;
665
+ if (stats.shown !== null)
666
+ tiles.shown = Math.max(tiles.shown ?? 0, stats.shown);
667
+ }
668
+ model.tiles = tiles;
396
669
  if (deps.onLabels && model.labels) {
397
- const places = {};
670
+ const places = { ...moonPlaces };
398
671
  for (const id of SPACE_BODIES) {
399
672
  const at = id === hiddenBody || (rideFrom && id === rideFrom.around) ? null : labelFor(pos[id]);
400
673
  places[id] = at ? { x: at.x, y: at.y, r: px[id] } : null;
@@ -424,6 +697,10 @@ export function createSpaceItem(model, deps) {
424
697
  o.line.geometry.dispose();
425
698
  o.line.material.dispose();
426
699
  }
700
+ for (const lines of [arrowLines, helpLines]) {
701
+ lines.geometry.dispose();
702
+ lines.material.dispose();
703
+ }
427
704
  },
428
705
  };
429
706
  },
@@ -0,0 +1,37 @@
1
+ import type { BodyId } from "../facts.ts";
2
+ import { type Vec3 } from "../vec.ts";
3
+ /**
4
+ * How a body turns at one moment: `axis`, the unit vector it turns about by the right-hand rule (the fingers of the right hand curl the way the body turns when the thumb points along it, so it points to the north
5
+ * pole of a body that turns the usual way and to the south pole of one that turns backwards, Venus and Uranus), and `pole`, the unit vector to its north pole as the IAU defines it.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import type { SpinAxis } from "@johnmorrisdotca/wakusei-planets/space";
10
+ *
11
+ * const earth: SpinAxis = { axis: [0, 1, 0], pole: [0, 1, 0], retrograde: false };
12
+ * console.log(earth.retrograde);
13
+ * ```
14
+ */
15
+ export interface SpinAxis {
16
+ axis: Vec3;
17
+ pole: Vec3;
18
+ /** True when it turns the other way from most bodies: the axis points away from the north pole. */
19
+ retrograde: boolean;
20
+ }
21
+ /**
22
+ * The axis a body spins about at `ms`, in the scene's axes (J2000 equatorial, y north), from how its prime meridian moves over ten minutes, so it is right for a body that turns backwards (Venus, Uranus, Pluto), one
23
+ * that is tipped on its side (Uranus) and a moon that keeps one face to its planet (it turns once an orbit, the way it goes round).
24
+ *
25
+ * @param id - The body's name.
26
+ * @param ms - The moment, in milliseconds since the Unix epoch.
27
+ * @returns The spin axis and the north pole.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * import { spinAxis } from "@johnmorrisdotca/wakusei-planets/space";
32
+ *
33
+ * const venus = spinAxis("venus", Date.UTC(2026, 10, 2));
34
+ * console.log(venus.retrograde, venus.axis.map((n) => n.toFixed(2)).join(", "));
35
+ * ```
36
+ */
37
+ export declare function spinAxis(id: BodyId, ms: number): SpinAxis;
@@ -0,0 +1,29 @@
1
+ // Which way a body spins, as the space view's help mode shows it: the axis it turns about (by the right-hand rule, so the fingers curl the way it turns) and the line of its poles. Pure.
2
+ import { orientation } from "../scene/orient.js";
3
+ import { cross, length, unit } from "../vec.js";
4
+ /**
5
+ * The axis a body spins about at `ms`, in the scene's axes (J2000 equatorial, y north), from how its prime meridian moves over ten minutes, so it is right for a body that turns backwards (Venus, Uranus, Pluto), one
6
+ * that is tipped on its side (Uranus) and a moon that keeps one face to its planet (it turns once an orbit, the way it goes round).
7
+ *
8
+ * @param id - The body's name.
9
+ * @param ms - The moment, in milliseconds since the Unix epoch.
10
+ * @returns The spin axis and the north pole.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { spinAxis } from "@johnmorrisdotca/wakusei-planets/space";
15
+ *
16
+ * const venus = spinAxis("venus", Date.UTC(2026, 10, 2));
17
+ * console.log(venus.retrograde, venus.axis.map((n) => n.toFixed(2)).join(", "));
18
+ * ```
19
+ */
20
+ export function spinAxis(id, ms) {
21
+ const now = orientation(id, ms);
22
+ const later = orientation(id, ms + 600_000);
23
+ const turn = cross(now.meridian, later.meridian);
24
+ const pole = unit(now.pole);
25
+ // a body that turns by less than a rounding error in ten minutes (none here) would have no axis: fall back to the pole
26
+ const axis = length(turn) < 1e-12 ? pole : unit(turn);
27
+ const along = axis[0] * pole[0] + axis[1] * pole[1] + axis[2] * pole[2];
28
+ return { axis, pole, retrograde: along < 0 };
29
+ }
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "0.28.0";
1
+ export declare const VERSION = "0.31.0";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "0.28.0";
1
+ export const VERSION = "0.31.0";