@johnmorrisdotca/wakusei-planets 0.30.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.
@@ -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,
@@ -43,6 +61,11 @@ export const newSpaceModel = () => ({
43
61
  drawn: null,
44
62
  painted: { bodies: 0, moons: 0, held: 0 },
45
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,
46
69
  lookups: 0,
47
70
  motion: 0,
48
71
  drawnView: null,
@@ -55,11 +78,26 @@ export const newSpaceModel = () => ({
55
78
  rideMotion: 0,
56
79
  ratio: () => Math.min(globalThis.devicePixelRatio || 1, 2),
57
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
+ }
58
97
  const unit = (v) => {
59
98
  const l = Math.hypot(v[0], v[1], v[2]) || 1;
60
99
  return [v[0] / l, v[1] / l, v[2] / l];
61
100
  };
62
- const minus = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
63
101
  export function createSpaceItem(model, deps) {
64
102
  return {
65
103
  key: "wakusei-planets:space",
@@ -89,7 +127,7 @@ export function createSpaceItem(model, deps) {
89
127
  empty(slot);
90
128
  slot.look = model.look;
91
129
  try {
92
- 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() });
93
131
  if (ended) {
94
132
  made.dispose();
95
133
  return;
@@ -108,15 +146,54 @@ export function createSpaceItem(model, deps) {
108
146
  slots.set(id, (slot = { drawing: undefined, look: null, failed: false }));
109
147
  return slot;
110
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
111
150
  const orbits = new Map();
112
151
  for (const id of ORBIT_BODIES) {
113
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);
114
157
  const geometry = new three.BufferGeometry();
115
158
  geometry.setAttribute("position", new three.BufferAttribute(positions, 3));
116
- 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 }));
117
161
  line.frustumCulled = false;
118
162
  scene.add(line);
119
- 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);
120
197
  }
121
198
  const moonSlots = new Map();
122
199
  let releaseTimer;
@@ -139,9 +216,7 @@ export function createSpaceItem(model, deps) {
139
216
  }
140
217
  const mode = model.mode;
141
218
  const pos = placeBodies(mode, frame);
142
- const sunPos = pos.sun;
143
219
  const radiusOf = (id) => radiusIn(id, mode);
144
- const placeAll = (at) => placeBodies(mode, at);
145
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
146
221
  const moonSite = (id, at, host) => {
147
222
  const real = moonOffset(id, at);
@@ -149,15 +224,50 @@ export function createSpaceItem(model, deps) {
149
224
  const k = d === 0 ? 1 : moonDistance(mode, host, d) / d;
150
225
  return [real[0] * k, real[1] * k, real[2] * k];
151
226
  };
152
- 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]));
153
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
154
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.
155
252
  model.rideLost = false;
156
253
  model.rideMotion = 0;
157
254
  let focus = model.focus;
158
- let focusPos = pos[focus];
159
- 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
+ }
160
269
  let aimWay = 1;
270
+ let besideIn = null;
161
271
  let rideFrom = null;
162
272
  const ride = model.ride;
163
273
  if (ride) {
@@ -172,7 +282,15 @@ export function createSpaceItem(model, deps) {
172
282
  let hiddenMoon = null;
173
283
  if (typeof t === "string" && SPACE_BODIES.includes(t)) {
174
284
  const id = t;
175
- 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") {
176
294
  focus = id;
177
295
  focusPos = pos[id];
178
296
  focusRadius = radiusOf(id);
@@ -181,7 +299,8 @@ export function createSpaceItem(model, deps) {
181
299
  const ends = track.ends();
182
300
  const was = ends ? placeAll(ends[0])[id] : pos[id];
183
301
  const will = ends ? placeAll(ends[1])[id] : pos[id];
184
- 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);
185
304
  velocity = minus(will, was);
186
305
  perSecond = ends ? Math.max(1e-3, (ends[1].ms - ends[0].ms) / 1000) : 1;
187
306
  hiddenBody = id;
@@ -191,7 +310,15 @@ export function createSpaceItem(model, deps) {
191
310
  const id = t;
192
311
  const offset = moonSite(id, model.time, around);
193
312
  const later = moonSite(id, model.time + 1000, around);
194
- 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") {
195
322
  focusPos = [centre[0] + offset[0] * unitWorld, centre[1] + offset[1] * unitWorld, centre[2] + offset[2] * unitWorld];
196
323
  focusRadius = Math.max(1e-6, (BODIES[id].radiusKm / BODIES[around].radiusKm) * unitWorld);
197
324
  }
@@ -224,7 +351,17 @@ export function createSpaceItem(model, deps) {
224
351
  }
225
352
  let fovNow = FOV;
226
353
  let distance = 0;
227
- 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) {
228
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 });
229
366
  const { centre, unitWorld } = rideFrom;
230
367
  camera.position.set(centre[0] + pose.position[0] * unitWorld, centre[1] + pose.position[1] * unitWorld, centre[2] + pose.position[2] * unitWorld);
@@ -246,7 +383,9 @@ export function createSpaceItem(model, deps) {
246
383
  const lon = model.centre.lon - aimWay * model.view.yaw;
247
384
  const lat = Math.max(-90, Math.min(90, model.centre.lat - aimWay * model.view.tilt));
248
385
  const aim = groundDirection(lon, lat, model.anchor);
249
- 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));
250
389
  target.set(focusPos[0], focusPos[1], focusPos[2]);
251
390
  camera.position.set(target.x + aim[0] * distance, target.y + aim[1] * distance, target.z + aim[2] * distance);
252
391
  camera.up.set(0, 1, 0);
@@ -260,7 +399,9 @@ export function createSpaceItem(model, deps) {
260
399
  const k = Math.min(1, (performance.now() - glide.start) / glide.duration);
261
400
  const ease = k * k * (3 - 2 * k);
262
401
  desiredQ.copy(camera.quaternion);
263
- 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);
264
405
  fromQ.set(glide.from.q[0], glide.from.q[1], glide.from.q[2], glide.from.q[3]);
265
406
  camera.quaternion.copy(fromQ.slerp(desiredQ, ease));
266
407
  fovNow = glide.from.fov + (fovNow - glide.from.fov) * ease;
@@ -280,8 +421,8 @@ export function createSpaceItem(model, deps) {
280
421
  camera.updateMatrixWorld();
281
422
  const hiddenBody = rideFrom?.hiddenBody ?? null;
282
423
  const hiddenMoon = rideFrom?.hiddenMoon ?? null;
283
- model.camera = { position: [camera.position.x, camera.position.y, camera.position.z], fov: fovNow };
284
- 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 };
285
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 };
286
427
  model.heightPx = size.height;
287
428
  // how many pixels across each body is: for the pictures it needs and for what is chosen
@@ -303,6 +444,13 @@ export function createSpaceItem(model, deps) {
303
444
  let paintedBodies = 0;
304
445
  for (const id of SPACE_BODIES) {
305
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
+ }
306
454
  fill(id, slot);
307
455
  const drawing = slot.drawing;
308
456
  if (!drawing)
@@ -319,39 +467,147 @@ export function createSpaceItem(model, deps) {
319
467
  }
320
468
  model.painted.bodies = paintedBodies;
321
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);
322
478
  for (const id of ORBIT_BODIES) {
323
479
  const o = orbits.get(id);
324
- o.line.visible = model.orbits;
480
+ const hide = closeUp && !ownLine(id);
481
+ o.line.visible = model.orbits && !hide;
325
482
  if (!model.orbits)
326
483
  continue;
327
- if (!Number.isFinite(o.epoch) || Math.abs(model.time - o.epoch) > 365 * 86_400_000) {
328
- 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);
329
487
  o.epoch = model.time;
330
488
  }
331
- 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);
332
491
  for (let i = 0; i <= ORBIT_POINTS; i += 1) {
333
492
  o.positions[i * 3] = line[i][0];
334
493
  o.positions[i * 3 + 1] = line[i][1];
335
494
  o.positions[i * 3 + 2] = line[i][2];
336
495
  }
337
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) };
338
586
  }
339
587
  // the moons of a planet, once it is big enough on the screen to show them
340
588
  const now = performance.now();
341
589
  const seen = new Set();
342
- for (const id of SPACE_PLANETS) {
590
+ const moonPlaces = {};
591
+ for (const id of [...SPACE_PLANETS, ...SPACE_DWARFS]) {
343
592
  const planet = slotOf(id).drawing;
344
- 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)
345
601
  continue;
346
602
  const radius = radiusOf(id);
347
- for (const site of deps.painter().moons(id, model.time)) {
603
+ for (const site of sites) {
348
604
  const reach = Math.hypot(site.offset[0], site.offset[1], site.offset[2]);
349
605
  const k = reach === 0 ? 1 : moonDistance(mode, id, reach) / reach;
350
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];
351
607
  projected.set(at[0], at[1], at[2]).project(camera);
352
608
  if (projected.z > 1 || Math.abs(projected.x) > 1.25 || Math.abs(projected.y) > 1.25)
353
609
  continue;
354
- const key = site.name.toLowerCase();
610
+ const key = site.name;
355
611
  if (key === hiddenMoon)
356
612
  continue;
357
613
  let held = moonSlots.get(key);
@@ -370,6 +626,9 @@ export function createSpaceItem(model, deps) {
370
626
  drawing.object.scale.setScalar(drawn);
371
627
  const moonPx = away <= drawn * 1.001 ? 1e5 : ((size.height / 2) * (drawn / Math.sqrt(away * away - drawn * drawn))) / half;
372
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 };
373
632
  }
374
633
  }
375
634
  let soonest = Number.POSITIVE_INFINITY;
@@ -408,7 +667,7 @@ export function createSpaceItem(model, deps) {
408
667
  }
409
668
  model.tiles = tiles;
410
669
  if (deps.onLabels && model.labels) {
411
- const places = {};
670
+ const places = { ...moonPlaces };
412
671
  for (const id of SPACE_BODIES) {
413
672
  const at = id === hiddenBody || (rideFrom && id === rideFrom.around) ? null : labelFor(pos[id]);
414
673
  places[id] = at ? { x: at.x, y: at.y, r: px[id] } : null;
@@ -438,6 +697,10 @@ export function createSpaceItem(model, deps) {
438
697
  o.line.geometry.dispose();
439
698
  o.line.material.dispose();
440
699
  }
700
+ for (const lines of [arrowLines, helpLines]) {
701
+ lines.geometry.dispose();
702
+ lines.material.dispose();
703
+ }
441
704
  },
442
705
  };
443
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.30.0";
1
+ export declare const VERSION = "0.31.0";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "0.30.0";
1
+ export const VERSION = "0.31.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/wakusei-planets",
3
- "version": "0.30.0",
3
+ "version": "0.31.0",
4
4
  "description": "Wakusei Planets 惑星 — The Sun, planets and moons for the web: each as a clean line drawing, a shaded realistic drawing or a painted surface map, with real sizes, axial tilt and rotation as data, and positions of the moons for any date and time. SVG for the lines, three.js (optional) for the rest.",
5
5
  "keywords": [
6
6
  "planets",