@johnmorrisdotca/wakusei-planets 0.35.0 → 0.37.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 (49) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/NOTICE.md +31 -1
  3. package/README.md +5 -4
  4. package/dist/data/moon-catalogue.d.ts +31 -0
  5. package/dist/data/moon-catalogue.js +471 -0
  6. package/dist/data/orbiters.d.ts +368 -0
  7. package/dist/data/orbiters.js +370 -0
  8. package/dist/data/places-planned.d.ts +3 -0
  9. package/dist/data/places-planned.js +51 -0
  10. package/dist/element.d.ts +28 -0
  11. package/dist/element.js +172 -1
  12. package/dist/moon-catalogue.d.ts +296 -0
  13. package/dist/moon-catalogue.js +234 -0
  14. package/dist/orbiter-facts.d.ts +480 -0
  15. package/dist/orbiter-facts.js +189 -0
  16. package/dist/orbiters.d.ts +256 -0
  17. package/dist/orbiters.js +275 -0
  18. package/dist/place-marks.d.ts +182 -0
  19. package/dist/place-marks.js +194 -0
  20. package/dist/places/mars.d.ts +1 -1
  21. package/dist/places/mars.js +4 -2
  22. package/dist/places/moon.d.ts +1 -1
  23. package/dist/places/moon.js +4 -2
  24. package/dist/places.d.ts +4 -2
  25. package/dist/places.js +2 -0
  26. package/dist/scene/place-layer.d.ts +68 -0
  27. package/dist/scene/place-layer.js +213 -0
  28. package/dist/scene/textures.d.ts +2 -2
  29. package/dist/space/all-moons.d.ts +30 -0
  30. package/dist/space/all-moons.js +61 -0
  31. package/dist/space/bodies.d.ts +18 -0
  32. package/dist/space/bodies.js +18 -0
  33. package/dist/space/element.d.ts +182 -6
  34. package/dist/space/element.js +763 -26
  35. package/dist/space/frame.d.ts +14 -0
  36. package/dist/space/frame.js +16 -0
  37. package/dist/space/index.d.ts +5 -2
  38. package/dist/space/index.js +5 -2
  39. package/dist/space/regions.d.ts +111 -0
  40. package/dist/space/regions.js +84 -0
  41. package/dist/space/scene.d.ts +131 -8
  42. package/dist/space/scene.js +312 -15
  43. package/dist/space/sight.d.ts +106 -0
  44. package/dist/space/sight.js +84 -0
  45. package/dist/space/travel.d.ts +256 -0
  46. package/dist/space/travel.js +597 -0
  47. package/dist/version.d.ts +1 -1
  48. package/dist/version.js +1 -1
  49. package/package.json +13 -2
@@ -1,5 +1,6 @@
1
1
  import { BODIES, MOONS_OF } from "../facts.js";
2
2
  import { createSmallBodyPoints } from "../door/points.js";
3
+ import { createMoonDots } from "./all-moons.js";
3
4
  import { toTerrestrialTime } from "../clock.js";
4
5
  import { moonOffset, orientation } from "../scene/orient.js";
5
6
  import {} from "../scene/painter.js";
@@ -10,9 +11,12 @@ import { arrowSites, ARROWS_PER_ORBIT, chevron, MAX_ARROWS_PER_ORBIT, spinArrow
10
11
  import { besideMoon } from "./beside.js";
11
12
  import { coveredBy } from "./covered.js";
12
13
  import { spinAxis } from "./spin.js";
13
- import { createTrack, moonOrbit, planetOrbit, SPACE_DWARFS, SPACE_MINORS, SPACE_PLANETS } from "./frame.js";
14
+ import { createTrack, eclipticToScene, frameAt, moonOrbit, planetOrbit, SPACE_DWARFS, SPACE_MINORS, SPACE_PLANETS } from "./frame.js";
15
+ import { BALL_FRAGMENT, VERTEX } from "../scene/glsl.js";
14
16
  import { ridePose } from "./ride.js";
15
- import { moonDistance, orbitLine, placeBodies, placeMoon, radiusIn } from "./scale.js";
17
+ import { moonDistance, orbitLine, placeAround, placeBodies, placeMoon, radiusIn } from "./scale.js";
18
+ import { regionOrbits, regionView } from "./regions.js";
19
+ import { travelPlan, travelPose } from "./travel.js";
16
20
  const RAD = Math.PI / 180;
17
21
  /** The camera's distance, in radii of the body it looks at, at which that body fills the view at zoom 1. */
18
22
  export const FIT_DISTANCE = 4.1;
@@ -38,6 +42,16 @@ const MOON_MIN_PX = 2.2;
38
42
  const MOON_MIN_SYSTEM_PX = 30;
39
43
  /** A moon that has been out of sight this long is let go (its pictures too). */
40
44
  const MOON_KEEP_MS = 4000;
45
+ /** An orbiter is drawn as a dot this many CSS pixels across its radius, once its body is at least `ORBITER_HOST_PX` across its radius on the screen (nearer, the dots would all sit on the body's own dot). */
46
+ const ORBITER_PX = 2.2;
47
+ const ORBITER_HOST_PX = 24;
48
+ /** An orbiter's orbit line is worked out again when the moment has moved this far from the line's epoch (the node and periapsis drift a little a day). */
49
+ const ORBITER_LINE_KEEP_MS = 86_400_000;
50
+ /** The Roadster is drawn as a dot this many CSS pixels across its radius: a car is nothing at any scale of the view. */
51
+ const ROADSTER_PX = 3;
52
+ /** The Roadster's orbit line spans this long round the moment shown (about one trip round the Sun), and is worked out again when the moment has moved this far from the line's middle. */
53
+ const ROADSTER_ORBIT_MS = 560 * 86_400_000;
54
+ const ROADSTER_ORBIT_KEEP_MS = 30 * 86_400_000;
41
55
  /**
42
56
  * The twenty-four bodies the space view places, names and lets a reader choose: the Earth, the Sun, the Moon, the seven other planets, Pluto, the eight other dwarf planets and the five minor bodies (Vesta, Pallas, Chiron, Pholus and Halley). The moons of the planets and of the dwarf planets are placed round their hosts.
43
57
  *
@@ -57,7 +71,10 @@ export const newSpaceModel = () => ({
57
71
  anchor: null,
58
72
  orbits: true,
59
73
  labels: true,
74
+ allMoons: null,
75
+ moonDots: 0,
60
76
  stepMs: 1000,
77
+ speed: 1,
61
78
  focus: "earth",
62
79
  light: "sun",
63
80
  look: "realistic",
@@ -70,6 +87,12 @@ export const newSpaceModel = () => ({
70
87
  helpThrough: false,
71
88
  arrowPx: 0,
72
89
  arrowCount: ARROWS_PER_ORBIT,
90
+ orbiters: false,
91
+ orbiterSet: null,
92
+ orbitersDrawn: { count: 0, status: {} },
93
+ roadster: false,
94
+ roadsterPath: null,
95
+ roadsterDrawn: { shown: false, position: null, radiusPx: 0 },
73
96
  label: null,
74
97
  labelVersion: 0,
75
98
  labelsDrawn: { count: 0, focus: null },
@@ -83,6 +106,9 @@ export const newSpaceModel = () => ({
83
106
  ride: null,
84
107
  rideLost: false,
85
108
  transition: null,
109
+ travelling: null,
110
+ region: null,
111
+ regionDrawn: null,
86
112
  pose: { p: [0, 0, 0], q: [0, 0, 0, 1], fov: FOV, valid: false },
87
113
  rideMotion: 0,
88
114
  ratio: () => Math.min(globalThis.devicePixelRatio || 1, 2),
@@ -121,11 +147,18 @@ export function createSpaceItem(model, deps) {
121
147
  if (points !== undefined)
122
148
  scene.add(points.object);
123
149
  let pointsVersion = -1;
150
+ // the catalogue's other moons as dots (a page's own subset of three needs Points and PointsMaterial for them)
151
+ const dots = typeof three.Points === "function" ? createMoonDots(three) : undefined;
152
+ if (dots !== undefined)
153
+ scene.add(dots.object);
124
154
  const camera = new three.PerspectiveCamera(FOV, width / Math.max(1, height), 0.05, 1000);
125
155
  const target = new three.Vector3();
126
156
  const projected = new three.Vector3();
127
- const desiredQ = camera.quaternion.clone();
128
157
  const fromQ = camera.quaternion.clone();
158
+ const scratchA = new three.Vector3();
159
+ const scratchB = new three.Vector3();
160
+ // the orbit lines of a stage out from the Sun, kept while the stage, the scale and the day are the same
161
+ let regionLines = null;
129
162
  let ended = false;
130
163
  const slots = new Map();
131
164
  const empty = (slot) => {
@@ -219,6 +252,42 @@ export function createSpaceItem(model, deps) {
219
252
  }
220
253
  const moonSlots = new Map();
221
254
  let releaseTimer;
255
+ // Starman and the Roadster: a dot (a shaded ball, lit by the Sun) on its own orbit line, in the car's warm red; made once, shown only while the view asks for it
256
+ const roadsterGeometry = new three.SphereGeometry(1, 24, 12);
257
+ const roadsterMaterial = new three.ShaderMaterial({ vertexShader: VERTEX, fragmentShader: BALL_FRAGMENT, uniforms: { uLight: { value: new three.Vector3(0, 0, 1) }, uColour: { value: new three.Vector3(0.93, 0.28, 0.2) } } });
258
+ const roadsterMesh = new three.Mesh(roadsterGeometry, roadsterMaterial);
259
+ roadsterMesh.visible = false;
260
+ roadsterMesh.frustumCulled = false;
261
+ scene.add(roadsterMesh);
262
+ const roadsterPositions = new Float32Array((ORBIT_POINTS + 1) * 3);
263
+ const roadsterLineGeometry = new three.BufferGeometry();
264
+ roadsterLineGeometry.setAttribute("position", new three.BufferAttribute(roadsterPositions, 3));
265
+ const roadsterLine = new three.Line(roadsterLineGeometry, new three.LineBasicMaterial({ color: 0xe8705a, transparent: true, opacity: 0.55 }));
266
+ roadsterLine.visible = false;
267
+ roadsterLine.frustumCulled = false;
268
+ scene.add(roadsterLine);
269
+ const roadsterOrbit = { samples: [], epoch: Number.NaN };
270
+ const orbiterDrawings = new Map();
271
+ const orbiterGeometry = new three.SphereGeometry(1, 16, 8);
272
+ const orbiterDrawing = (id) => {
273
+ let drawing = orbiterDrawings.get(id);
274
+ if (drawing !== undefined)
275
+ return drawing;
276
+ const material = new three.ShaderMaterial({ vertexShader: VERTEX, fragmentShader: BALL_FRAGMENT, uniforms: { uLight: { value: new three.Vector3(0, 0, 1) }, uColour: { value: new three.Vector3(0.75, 0.9, 1) } } });
277
+ const mesh = new three.Mesh(orbiterGeometry, material);
278
+ mesh.frustumCulled = false;
279
+ scene.add(mesh);
280
+ const positions = new Float32Array((ORBIT_POINTS + 1) * 3);
281
+ const geometry = new three.BufferGeometry();
282
+ geometry.setAttribute("position", new three.BufferAttribute(positions, 3));
283
+ const lineMaterial = new three.LineBasicMaterial({ color: 0x9fd0ff, transparent: true, opacity: 0.45 });
284
+ const line = new three.Line(geometry, lineMaterial);
285
+ line.frustumCulled = false;
286
+ scene.add(line);
287
+ drawing = { mesh, material, line, lineMaterial, positions, epoch: Number.NaN };
288
+ orbiterDrawings.set(id, drawing);
289
+ return drawing;
290
+ };
222
291
  const labelFor = (v) => {
223
292
  projected.set(v[0], v[1], v[2]).project(camera);
224
293
  if (projected.z > 1 || projected.z < -1)
@@ -409,26 +478,104 @@ export function createSpaceItem(model, deps) {
409
478
  const across = focusRadius * (FRAMED[focus] ?? 1) * Math.max(1, size.height / Math.max(1, size.width));
410
479
  distance = Math.max(1.05 * focusRadius, (FIT_DISTANCE * across) / Math.max(1e-9, model.view.zoom));
411
480
  target.set(focusPos[0], focusPos[1], focusPos[2]);
481
+ model.regionDrawn = null;
482
+ if (model.region !== null && focus === "sun") {
483
+ // a stage out from the Sun: the camera stands back along the same line of sight until every orbit of the stage is in the picture, whatever the zoom says
484
+ if (regionLines === null || regionLines.id !== model.region || regionLines.mode !== mode || Math.abs(model.time - regionLines.epoch) > 86_400_000) {
485
+ regionLines = { id: model.region, mode, epoch: model.time, lines: regionOrbits(model.region, mode, model.time).map((line) => line.map((p) => minus(p, origin))) };
486
+ }
487
+ const framed = regionView({ centre: focusPos, orbits: regionLines.lines, aspect: size.width / Math.max(1, size.height), forward: [-aim[0], -aim[1], -aim[2]], up: [0, 1, 0] });
488
+ distance = Math.hypot(framed.position[0] - focusPos[0], framed.position[1] - focusPos[1], framed.position[2] - focusPos[2]);
489
+ model.regionDrawn = { id: model.region, reach: framed.reach, distance };
490
+ }
412
491
  camera.position.set(target.x + aim[0] * distance, target.y + aim[1] * distance, target.z + aim[2] * distance);
413
492
  camera.up.set(0, 1, 0);
414
493
  camera.lookAt(target);
415
494
  camera.near = Math.max(0.0005, distance * 0.02 - focusRadius * 0.5, focusRadius * 0.01);
416
495
  camera.far = Math.max(2000, distance * 4 + (mode === "true" ? 40 * 23_481 : 400));
417
496
  }
418
- // a ride that changed: glide from where the last draw left the camera
497
+ // a move to a new target: the camera travels from where the last draw left it to the view just set up, along the path `travelPose` gives (the body left kept in the picture through the
498
+ // turn, the destination from then on, nothing passed close), worked out once at the move's first draw
419
499
  const glide = model.transition;
500
+ model.travelling = null;
420
501
  if (glide) {
421
- const k = Math.min(1, (performance.now() - glide.start) / glide.duration);
422
- const ease = k * k * (3 - 2 * k);
423
- desiredQ.copy(camera.quaternion);
424
- // the pose the last draw left is absolute (from the Earth's centre); the scene is now relative to the new origin
425
- const [fx, fy, fz] = [glide.from.p[0] - origin[0], glide.from.p[1] - origin[1], glide.from.p[2] - origin[2]];
426
- camera.position.set(fx + (camera.position.x - fx) * ease, fy + (camera.position.y - fy) * ease, fz + (camera.position.z - fz) * ease);
427
- fromQ.set(glide.from.q[0], glide.from.q[1], glide.from.q[2], glide.from.q[3]);
428
- camera.quaternion.copy(fromQ.slerp(desiredQ, ease));
429
- fovNow = glide.from.fov + (fovNow - glide.from.fov) * ease;
430
- if (k >= 1)
502
+ if (glide.input === null) {
503
+ // the pose the last draw left is absolute (from the Earth's centre); the scene is now relative to the new origin
504
+ const from = [glide.from.p[0] - origin[0], glide.from.p[1] - origin[1], glide.from.p[2] - origin[2]];
505
+ fromQ.set(glide.from.q[0], glide.from.q[1], glide.from.q[2], glide.from.q[3]);
506
+ const sf = scratchA.set(0, 0, -1).applyQuaternion(fromQ);
507
+ const su = scratchB.set(0, 1, 0).applyQuaternion(fromQ);
508
+ const start = { position: from, forward: [sf.x, sf.y, sf.z], up: [su.x, su.y, su.z], fov: glide.from.fov };
509
+ const ef = scratchA.set(0, 0, -1).applyQuaternion(camera.quaternion);
510
+ const eu = scratchB.set(0, 1, 0).applyQuaternion(camera.quaternion);
511
+ const radiusOfAny = (id) => {
512
+ if (SPACE_BODIES.includes(id))
513
+ return radiusOf(id);
514
+ const host = (hostOf(id) ?? "earth");
515
+ return Math.max(1e-6, (BODIES[id].radiusKm / BODIES[host].radiusKm) * radiusOf(host));
516
+ };
517
+ // every body is where the clock puts it as the move starts and where it puts it as the move ends (`later`, in the scene's places as of this draw): a clock that runs moves them, the Moon about three
518
+ // Earth radii in five game hours
519
+ const familyOf = (id) => (id === null ? null : id === "moon" ? "earth" : hostOf(id) ?? id);
520
+ const family = familyOf(glide.leaving);
521
+ const around = family !== null && family === familyOf(glide.destination) && (glide.leaving !== glide.destination) && (glide.leaving !== family || glide.destination !== family) ? family : null;
522
+ const build = (ms) => {
523
+ const later = ms === model.time ? pos : placeBodies(mode, frameAt(ms));
524
+ if (later !== pos)
525
+ for (const id of Object.keys(later))
526
+ later[id] = minus(later[id], origin);
527
+ const siteAt = (id, table, at) => {
528
+ if (SPACE_BODIES.includes(id))
529
+ return table[id];
530
+ const host = (hostOf(id) ?? "earth");
531
+ const offset = moonSite(id, at, host);
532
+ const hostRadius = radiusOf(host);
533
+ return [table[host][0] + offset[0] * hostRadius, table[host][1] + offset[1] * hostRadius, table[host][2] + offset[2] * hostRadius];
534
+ };
535
+ const bodyOf = (id) => (id === null ? null : { from: siteAt(id, pos, model.time), to: siteAt(id, later, ms), radius: radiusOfAny(id) });
536
+ const destination = bodyOf(glide.destination) ?? { from: focusPos, to: focusPos, radius: focusRadius };
537
+ if (model.regionDrawn !== null && glide.destination === "sun")
538
+ destination.reach = model.regionDrawn.reach;
539
+ const leaving = bodyOf(glide.leaving) ?? { ...destination };
540
+ if (glide.leavingReach > 0 && glide.leaving === "sun")
541
+ leaving.reach = glide.leavingReach;
542
+ // the last view is the one framed for the destination where it is now: it moves with the destination
543
+ const shift = destination.to === undefined ? [0, 0, 0] : [destination.to[0] - destination.from[0], destination.to[1] - destination.from[1], destination.to[2] - destination.from[2]];
544
+ const end = { position: [camera.position.x + shift[0], camera.position.y + shift[1], camera.position.z + shift[2]], forward: [ef.x, ef.y, ef.z], up: [eu.x, eu.y, eu.z], fov: fovNow };
545
+ // every body of the view is kept clear of, the two ends of the move and the planet circled aside
546
+ const others = SPACE_BODIES.filter((id) => id !== glide.leaving && id !== glide.destination && id !== family).map((id) => bodyOf(id));
547
+ const input = { start, end, leaving, destination, others, aspect: size.width / Math.max(1, size.height), kind: glide.kind, pace: glide.pace };
548
+ if (around !== null)
549
+ input.around = bodyOf(around);
550
+ return { input, plan: travelPlan(input) };
551
+ };
552
+ // the length of the move is worked out for bodies that stand still, then the move is worked out again for where the clock takes them in that time (once: the length may change a little)
553
+ let built = build(model.time);
554
+ if (model.speed !== 0) {
555
+ const duration = glide.duration > 0 ? glide.duration : built.plan.duration;
556
+ built = build(model.time + duration * model.speed);
557
+ }
558
+ glide.input = built.input;
559
+ glide.plan = built.plan;
560
+ glide.origin = origin;
561
+ if (glide.duration <= 0)
562
+ glide.duration = glide.plan.duration;
563
+ }
564
+ const k = Math.min(1, (performance.now() - glide.start) / Math.max(1, glide.duration));
565
+ const sample = travelPose(glide.input, k, glide.plan);
566
+ // the path is in the scene's places as of the move's first draw: the scene's origin moves with the body looked at, so the camera is moved by as much
567
+ const [ox, oy, oz] = [glide.origin[0] - origin[0], glide.origin[1] - origin[1], glide.origin[2] - origin[2]];
568
+ camera.position.set(sample.position[0] + ox, sample.position[1] + oy, sample.position[2] + oz);
569
+ camera.up.set(sample.up[0], sample.up[1], sample.up[2]);
570
+ target.set(camera.position.x + sample.forward[0], camera.position.y + sample.forward[1], camera.position.z + sample.forward[2]);
571
+ camera.lookAt(target);
572
+ fovNow = sample.fov;
573
+ model.travelling = { kind: glide.plan.kind, path: glide.plan.path, stage: sample.stage, progress: k, duration: glide.duration, leaving: sample.leaving.inside, destination: sample.destination.inside };
574
+ // the move is over at its last draw: nothing is travelling after it
575
+ if (k >= 1) {
431
576
  model.transition = null;
577
+ model.travelling = null;
578
+ }
432
579
  }
433
580
  // the controls' roll turns the picture clockwise about the line of sight, and a pan moves everything by that many CSS pixels at any depth: the window of the projection moves the other way
434
581
  if (model.view.roll !== 0)
@@ -490,6 +637,18 @@ export function createSpaceItem(model, deps) {
490
637
  drawing.update({ toSun, ms: model.time, radiusPx: px[id], view: { camera, width: size.width, height: size.height } });
491
638
  }
492
639
  model.painted.bodies = paintedBodies;
640
+ if (deps.onSurface) {
641
+ const shown = rideFrom ? undefined : (slots.get(model.focus)?.drawing ?? moonSlots.get(model.focus)?.slot.drawing);
642
+ if (shown === undefined || !shown.object.visible)
643
+ deps.onSurface(null);
644
+ else {
645
+ const at = shown.object.position;
646
+ const r = shown.object.scale.x;
647
+ const d = Math.hypot(at.x - camera.position.x, at.y - camera.position.y, at.z - camera.position.z);
648
+ const { pole, meridian } = orientation(model.focus, model.time);
649
+ deps.onSurface({ id: model.focus, centre: [at.x, at.y, at.z], radius: r, pole, meridian, eye: [camera.position.x, camera.position.y, camera.position.z], project: labelFor, width: size.width, height: size.height, radiusPx: d <= r * 1.001 ? 1e5 : ((size.height / 2) * (r / Math.sqrt(d * d - r * r))) / half });
650
+ }
651
+ }
493
652
  if (points !== undefined && deps.book !== undefined) {
494
653
  if (pointsVersion !== deps.book.version) {
495
654
  pointsVersion = deps.book.version;
@@ -499,6 +658,11 @@ export function createSpaceItem(model, deps) {
499
658
  points.update({ jd: toTerrestrialTime(model.time) / 86_400_000 + 2_440_587.5, mode, sun: sunPos, width: size.width * ratio, height: size.height * ratio, ratio });
500
659
  model.door.points = points.count;
501
660
  }
661
+ if (dots !== undefined) {
662
+ const list = model.allMoons?.(model.time) ?? [];
663
+ dots.object.visible = list.length > 0;
664
+ model.moonDots = list.length === 0 ? 0 : dots.update(list, mode, (host) => pos[host], radiusOf, model.ratio());
665
+ }
502
666
  // the orbits round the Sun, placed by the same mapping as the bodies
503
667
  const drawnRings = [];
504
668
  const hiddenRings = [];
@@ -547,6 +711,99 @@ export function createSpaceItem(model, deps) {
547
711
  miss[id] = distanceToLine(pos[id], line) / radius;
548
712
  }
549
713
  model.rings = { drawn: drawnRings, miss, hidden: hiddenRings };
714
+ // Starman and the Roadster: where the table puts the car, in the same mapping as the planets, as a dot a few pixels across, on its orbit line (about one trip round the Sun, from the table, worked out again every month)
715
+ const path = model.roadster ? model.roadsterPath : null;
716
+ let roadsterAt = null;
717
+ let roadsterPx = 0;
718
+ if (path !== null && model.time >= path.record.start && model.time <= path.record.end) {
719
+ roadsterAt = placeAround(mode, sunPos, eclipticToScene(path.positionAu(model.time)));
720
+ const away = Math.hypot(roadsterAt[0] - camera.position.x, roadsterAt[1] - camera.position.y, roadsterAt[2] - camera.position.z);
721
+ const drawn = (away * half * ROADSTER_PX) / (size.height / 2);
722
+ roadsterPx = away <= drawn ? 1e5 : ((size.height / 2) * (drawn / Math.sqrt(away * away - drawn * drawn))) / half;
723
+ roadsterMesh.visible = true;
724
+ roadsterMesh.position.set(roadsterAt[0], roadsterAt[1], roadsterAt[2]);
725
+ roadsterMesh.scale.setScalar(drawn);
726
+ roadsterMaterial.uniforms.uLight.value.set(...unit(minus(sunPos, roadsterAt)));
727
+ if (!Number.isFinite(roadsterOrbit.epoch) || Math.abs(model.time - roadsterOrbit.epoch) > ROADSTER_ORBIT_KEEP_MS) {
728
+ roadsterOrbit.samples = [];
729
+ for (let i = 0; i <= ORBIT_POINTS; i += 1) {
730
+ const t = Math.max(path.record.start, Math.min(path.record.end, model.time - ROADSTER_ORBIT_MS / 2 + (ROADSTER_ORBIT_MS * i) / ORBIT_POINTS));
731
+ roadsterOrbit.samples.push(eclipticToScene(path.positionAu(t)));
732
+ }
733
+ roadsterOrbit.epoch = model.time;
734
+ }
735
+ const line = orbitLine(mode, sunPos, roadsterOrbit.samples);
736
+ for (let i = 0; i <= ORBIT_POINTS; i += 1) {
737
+ roadsterPositions[i * 3] = line[i][0];
738
+ roadsterPositions[i * 3 + 1] = line[i][1];
739
+ roadsterPositions[i * 3 + 2] = line[i][2];
740
+ }
741
+ roadsterLineGeometry.getAttribute("position").needsUpdate = true;
742
+ roadsterLine.visible = model.orbits && !closeUp;
743
+ }
744
+ else {
745
+ roadsterMesh.visible = false;
746
+ roadsterLine.visible = false;
747
+ }
748
+ // the orbiters round the Moon and Mars: each a dot at the table's place, scaled with its body's drawn radius, on its orbit line, once its body is big enough on the screen; a modelled path is drawn dim
749
+ const orbiterSet = model.orbiters ? model.orbiterSet : null;
750
+ const orbiterPlaces = {};
751
+ const orbiterStatus = {};
752
+ if (orbiterSet !== null) {
753
+ for (const id of orbiterSet.ids) {
754
+ const centre = orbiterSet.centre(id);
755
+ const hostPx = px[centre];
756
+ const km = BODIES[centre].radiusKm;
757
+ const at = hostPx >= ORBITER_HOST_PX ? orbiterSet.position(id, model.time) : null;
758
+ const drawing = orbiterDrawings.get(id) ?? (at === null ? undefined : orbiterDrawing(id));
759
+ if (drawing === undefined)
760
+ continue;
761
+ if (at === null) {
762
+ drawing.mesh.visible = false;
763
+ drawing.line.visible = false;
764
+ continue;
765
+ }
766
+ const status = orbiterSet.status(id, model.time);
767
+ const hostRadius = radiusOf(centre);
768
+ const here = eclipticToScene(at);
769
+ const place = [pos[centre][0] + (here[0] / km) * hostRadius, pos[centre][1] + (here[1] / km) * hostRadius, pos[centre][2] + (here[2] / km) * hostRadius];
770
+ const away = Math.hypot(place[0] - camera.position.x, place[1] - camera.position.y, place[2] - camera.position.z);
771
+ const drawn = (away * half * ORBITER_PX) / (size.height / 2);
772
+ const dotPx = away <= drawn ? 1e5 : ((size.height / 2) * (drawn / Math.sqrt(away * away - drawn * drawn))) / half;
773
+ drawing.mesh.visible = true;
774
+ drawing.mesh.position.set(place[0], place[1], place[2]);
775
+ drawing.mesh.scale.setScalar(drawn);
776
+ drawing.material.uniforms.uLight.value.set(...toSunOf(centre));
777
+ const dim = status === "modelled" || status === "predicted";
778
+ drawing.material.uniforms.uColour.value.set(dim ? 0.55 : 0.75, dim ? 0.6 : 0.9, dim ? 0.65 : 1);
779
+ if (!Number.isFinite(drawing.epoch) || Math.abs(model.time - drawing.epoch) > ORBITER_LINE_KEEP_MS) {
780
+ const points = orbiterSet.path(id, model.time, ORBIT_POINTS);
781
+ for (let i = 0; i <= ORBIT_POINTS; i += 1) {
782
+ const p = eclipticToScene(points[i] ?? [0, 0, 0]);
783
+ drawing.positions[i * 3] = p[0] / km;
784
+ drawing.positions[i * 3 + 1] = p[1] / km;
785
+ drawing.positions[i * 3 + 2] = p[2] / km;
786
+ }
787
+ drawing.line.geometry.getAttribute("position").needsUpdate = true;
788
+ drawing.epoch = model.time;
789
+ }
790
+ // the line is in the body's radii round the origin: placed and scaled with the body
791
+ drawing.line.position.set(pos[centre][0], pos[centre][1], pos[centre][2]);
792
+ drawing.line.scale.setScalar(hostRadius);
793
+ drawing.lineMaterial.opacity = dim ? 0.25 : 0.45;
794
+ drawing.line.visible = model.orbits;
795
+ orbiterPlaces[id] = { at: place, px: dotPx };
796
+ orbiterStatus[id] = status;
797
+ }
798
+ }
799
+ else {
800
+ for (const drawing of orbiterDrawings.values()) {
801
+ drawing.mesh.visible = false;
802
+ drawing.line.visible = false;
803
+ }
804
+ }
805
+ model.orbitersDrawn = { count: Object.keys(orbiterPlaces).length, status: orbiterStatus };
806
+ model.roadsterDrawn = { shown: roadsterAt !== null, position: roadsterAt === null ? null : [roadsterAt[0] + origin[0], roadsterAt[1] + origin[1], roadsterAt[2] + origin[2]], radiusPx: roadsterPx };
550
807
  // 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)
551
808
  const e = camera.matrixWorld.elements;
552
809
  const camRight = [e[0], e[1], e[2]];
@@ -730,6 +987,18 @@ export function createSpaceItem(model, deps) {
730
987
  const drawing = slots.get(id)?.drawing;
731
988
  places[id] = at ? { x: at.x, y: at.y, r: px[id], named: drawing !== undefined && drawing.object.visible && drawing.label?.shown === true } : null;
732
989
  }
990
+ for (const [id, { at, px: dotPx }] of Object.entries(orbiterPlaces)) {
991
+ const spot = labelFor(at);
992
+ places[id] = spot ? { x: spot.x, y: spot.y, r: dotPx } : null;
993
+ if (spot)
994
+ moonDistances[id] = Math.hypot(at[0] - camera.position.x, at[1] - camera.position.y, at[2] - camera.position.z);
995
+ }
996
+ if (roadsterAt !== null) {
997
+ const spot = labelFor(roadsterAt);
998
+ places["roadster"] = spot ? { x: spot.x, y: spot.y, r: roadsterPx } : null;
999
+ if (spot)
1000
+ moonDistances["roadster"] = Math.hypot(roadsterAt[0] - camera.position.x, roadsterAt[1] - camera.position.y, roadsterAt[2] - camera.position.z);
1001
+ }
733
1002
  // a body behind another: its place is inside a nearer body's disc
734
1003
  const entries = [];
735
1004
  for (const [id, at] of Object.entries(places)) {
@@ -743,6 +1012,25 @@ export function createSpaceItem(model, deps) {
743
1012
  places[id].behind = cover;
744
1013
  deps.onLabels(places);
745
1014
  }
1015
+ if (deps.onSight && (deps.wantSight?.() ?? true)) {
1016
+ // each body in the camera's own axes: in front when it is ahead (negative z), its place from the projection (a pan included), and the way to it on the screen from its x and y, which holds behind the camera too
1017
+ const sights = {};
1018
+ const sight = (id, at, r) => {
1019
+ projected.set(at[0], at[1], at[2]).applyMatrix4(camera.matrixWorldInverse);
1020
+ const [cx, cy, front] = [projected.x, projected.y, projected.z < 0];
1021
+ projected.applyMatrix4(camera.projectionMatrix);
1022
+ sights[id] = { front, x: front ? ((projected.x + 1) / 2) * size.width : 0, y: front ? ((1 - projected.y) / 2) * size.height : 0, r: front ? r : 0, dx: cx, dy: -cy };
1023
+ };
1024
+ for (const id of SPACE_BODIES)
1025
+ if (id !== hiddenBody && !(rideFrom && id === rideFrom.around))
1026
+ sight(id, pos[id], px[id]);
1027
+ const moons = new Set([...(MOONS_OF[hostOf(model.focus) ?? model.focus] ?? []), ...Object.keys(moonPlaces)]);
1028
+ for (const id of moons)
1029
+ if (id !== hiddenMoon && !SPACE_BODIES.includes(id))
1030
+ sight(id, absoluteSite(id), moonPlaces[id]?.r ?? 0);
1031
+ projected.set(0, 0, -1).applyMatrix4(camera.projectionMatrix);
1032
+ deps.onSight(sights, { width: size.width, height: size.height, axis: { x: ((projected.x + 1) / 2) * size.width, y: ((1 - projected.y) / 2) * size.height } });
1033
+ }
746
1034
  if (model.rideLost)
747
1035
  deps.onRideLost?.();
748
1036
  if (model.transition)
@@ -754,6 +1042,7 @@ export function createSpaceItem(model, deps) {
754
1042
  },
755
1043
  dispose() {
756
1044
  points?.dispose();
1045
+ dots?.dispose();
757
1046
  if (releaseTimer !== undefined)
758
1047
  clearTimeout(releaseTimer);
759
1048
  ended = true;
@@ -767,10 +1056,18 @@ export function createSpaceItem(model, deps) {
767
1056
  o.line.geometry.dispose();
768
1057
  o.line.material.dispose();
769
1058
  }
770
- for (const lines of [arrowLines, helpLines]) {
1059
+ for (const lines of [arrowLines, helpLines, roadsterLine]) {
771
1060
  lines.geometry.dispose();
772
1061
  lines.material.dispose();
773
1062
  }
1063
+ roadsterGeometry.dispose();
1064
+ roadsterMaterial.dispose();
1065
+ for (const drawing of orbiterDrawings.values()) {
1066
+ drawing.material.dispose();
1067
+ drawing.line.geometry.dispose();
1068
+ drawing.lineMaterial.dispose();
1069
+ }
1070
+ orbiterGeometry.dispose();
774
1071
  },
775
1072
  };
776
1073
  },
@@ -0,0 +1,106 @@
1
+ /**
2
+ * An edge of the picture: the one a body out of view lies beyond.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import type { SightEdge } from "@johnmorrisdotca/wakusei-planets/space";
7
+ *
8
+ * const edge: SightEdge = "left";
9
+ * console.log(edge);
10
+ * ```
11
+ */
12
+ export type SightEdge = "top" | "right" | "bottom" | "left";
13
+ /**
14
+ * What the camera sees of one body, in CSS pixels in the picture: whether the body is in front of the camera, its place on the screen when it is (it may be off the picture), its radius there, and the way to it on the
15
+ * screen (right and down are positive), which holds for a body behind the camera too.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import type { SightInput } from "@johnmorrisdotca/wakusei-planets/space";
20
+ *
21
+ * const mars: SightInput = { front: true, x: 900, y: 120, r: 2, dx: 500, dy: -80 };
22
+ * console.log(mars.front);
23
+ * ```
24
+ */
25
+ export interface SightInput {
26
+ front: boolean;
27
+ x: number;
28
+ y: number;
29
+ r: number;
30
+ dx: number;
31
+ dy: number;
32
+ }
33
+ /**
34
+ * Where a body is for the picture: `inView` when its disc is on the picture; otherwise the `edge` it lies beyond, and `x`, `y`, the point on that edge where a line from the middle of the view to the body crosses it, with
35
+ * `along` the same point measured along the edge (from the left for the top and bottom, from the top for the sides). `angle` is the way to the body from the middle of the view in degrees clockwise from the top of the
36
+ * picture (0 up, 90 right), and `behind` is true for a body behind the camera. A body in view has `edge` null and `x`, `y` its own place.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * import type { Sight } from "@johnmorrisdotca/wakusei-planets/space";
41
+ *
42
+ * const sight: Sight = { inView: false, behind: false, edge: "right", x: 640, y: 140, along: 140, angle: 81 };
43
+ * console.log(sight.edge, sight.along);
44
+ * ```
45
+ */
46
+ export interface Sight {
47
+ inView: boolean;
48
+ behind: boolean;
49
+ edge: SightEdge | null;
50
+ x: number;
51
+ y: number;
52
+ along: number;
53
+ angle: number;
54
+ }
55
+ /**
56
+ * Where a body is for a picture `width` by `height`: in view, or beyond which edge and where along it. The line to a body out of view starts at `axis`, the point of the picture straight ahead of the camera (the middle,
57
+ * unless the view is panned), kept inside the picture.
58
+ *
59
+ * @param input - The body as the camera sees it.
60
+ * @param width - The picture's width in CSS pixels.
61
+ * @param height - The picture's height in CSS pixels.
62
+ * @param axis - The point straight ahead of the camera, in CSS pixels; the middle of the picture by default.
63
+ * @returns The body's sight.
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * import { sightOf } from "@johnmorrisdotca/wakusei-planets/space";
68
+ *
69
+ * const right = sightOf({ front: true, x: 900, y: 300, r: 3, dx: 580, dy: 0 }, 640, 600);
70
+ * const behind = sightOf({ front: false, x: 0, y: 0, r: 0, dx: 0, dy: 1 }, 640, 600);
71
+ * console.log(right.edge, right.along, behind.edge, behind.behind);
72
+ * ```
73
+ */
74
+ export declare function sightOf(input: SightInput, width: number, height: number, axis?: {
75
+ x: number;
76
+ y: number;
77
+ }): Sight;
78
+ /**
79
+ * How far, in CSS pixels, a body out of view moves along its edge before the view tells the page again.
80
+ *
81
+ * @example
82
+ * ```ts
83
+ * import { SIGHT_STEP_PX } from "@johnmorrisdotca/wakusei-planets/space";
84
+ *
85
+ * console.log(SIGHT_STEP_PX);
86
+ * ```
87
+ */
88
+ export declare const SIGHT_STEP_PX = 8;
89
+ /**
90
+ * Whether the page should be told again: a body came into view or left it, went behind the camera or came out, changed edge, moved along its edge by `step` pixels or more since the page was last told, or the bodies
91
+ * themselves changed. A body in view that moves about in it is not a change.
92
+ *
93
+ * @param before - The sights the page was last told, by body.
94
+ * @param now - The sights now, by body.
95
+ * @param step - How far along an edge is a change, in CSS pixels.
96
+ * @returns True when the page should hear of it.
97
+ *
98
+ * @example
99
+ * ```ts
100
+ * import { sightChanged, sightOf } from "@johnmorrisdotca/wakusei-planets/space";
101
+ *
102
+ * const at = (x: number) => ({ mars: sightOf({ front: true, x, y: 300, r: 3, dx: x - 320, dy: 0 }, 640, 600) });
103
+ * console.log(sightChanged(at(700), at(702)), sightChanged(at(700), at(300)));
104
+ * ```
105
+ */
106
+ export declare function sightChanged(before: Readonly<Record<string, Sight>>, now: Readonly<Record<string, Sight>>, step?: number): boolean;
@@ -0,0 +1,84 @@
1
+ // Where a body is from the camera's point of view: inside the picture's frame, or beyond one of its four edges, and where along that edge. Pure: a place and a direction in, a side and a point out.
2
+ // The view draws no tabs from it; a page that wants them places its own.
3
+ /**
4
+ * Where a body is for a picture `width` by `height`: in view, or beyond which edge and where along it. The line to a body out of view starts at `axis`, the point of the picture straight ahead of the camera (the middle,
5
+ * unless the view is panned), kept inside the picture.
6
+ *
7
+ * @param input - The body as the camera sees it.
8
+ * @param width - The picture's width in CSS pixels.
9
+ * @param height - The picture's height in CSS pixels.
10
+ * @param axis - The point straight ahead of the camera, in CSS pixels; the middle of the picture by default.
11
+ * @returns The body's sight.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * import { sightOf } from "@johnmorrisdotca/wakusei-planets/space";
16
+ *
17
+ * const right = sightOf({ front: true, x: 900, y: 300, r: 3, dx: 580, dy: 0 }, 640, 600);
18
+ * const behind = sightOf({ front: false, x: 0, y: 0, r: 0, dx: 0, dy: 1 }, 640, 600);
19
+ * console.log(right.edge, right.along, behind.edge, behind.behind);
20
+ * ```
21
+ */
22
+ export function sightOf(input, width, height, axis = { x: width / 2, y: height / 2 }) {
23
+ const ox = Math.min(width, Math.max(0, axis.x));
24
+ const oy = Math.min(height, Math.max(0, axis.y));
25
+ const r = Math.max(0, input.r);
26
+ if (input.front && input.x + r >= 0 && input.x - r <= width && input.y + r >= 0 && input.y - r <= height) {
27
+ return { inView: true, behind: false, edge: null, x: input.x, y: input.y, along: 0, angle: angleOf(input.x - ox, input.y - oy) };
28
+ }
29
+ const dx = input.front ? input.x - ox : input.dx;
30
+ let dy = input.front ? input.y - oy : input.dy;
31
+ // straight behind the camera every way is as short: say below
32
+ if (dx === 0 && dy === 0)
33
+ dy = 1;
34
+ const tx = dx > 0 ? (width - ox) / dx : dx < 0 ? -ox / dx : Number.POSITIVE_INFINITY;
35
+ const ty = dy > 0 ? (height - oy) / dy : dy < 0 ? -oy / dy : Number.POSITIVE_INFINITY;
36
+ const t = Math.min(tx, ty);
37
+ const x = Math.min(width, Math.max(0, ox + dx * t));
38
+ const y = Math.min(height, Math.max(0, oy + dy * t));
39
+ const edge = tx < ty ? (dx > 0 ? "right" : "left") : dy > 0 ? "bottom" : "top";
40
+ return { inView: false, behind: !input.front, edge, x, y, along: edge === "top" || edge === "bottom" ? x : y, angle: angleOf(dx, dy) };
41
+ }
42
+ const angleOf = (dx, dy) => (dx === 0 && dy === 0 ? 0 : ((Math.atan2(dx, -dy) * 180) / Math.PI + 360) % 360);
43
+ /**
44
+ * How far, in CSS pixels, a body out of view moves along its edge before the view tells the page again.
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * import { SIGHT_STEP_PX } from "@johnmorrisdotca/wakusei-planets/space";
49
+ *
50
+ * console.log(SIGHT_STEP_PX);
51
+ * ```
52
+ */
53
+ export const SIGHT_STEP_PX = 8;
54
+ /**
55
+ * Whether the page should be told again: a body came into view or left it, went behind the camera or came out, changed edge, moved along its edge by `step` pixels or more since the page was last told, or the bodies
56
+ * themselves changed. A body in view that moves about in it is not a change.
57
+ *
58
+ * @param before - The sights the page was last told, by body.
59
+ * @param now - The sights now, by body.
60
+ * @param step - How far along an edge is a change, in CSS pixels.
61
+ * @returns True when the page should hear of it.
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * import { sightChanged, sightOf } from "@johnmorrisdotca/wakusei-planets/space";
66
+ *
67
+ * const at = (x: number) => ({ mars: sightOf({ front: true, x, y: 300, r: 3, dx: x - 320, dy: 0 }, 640, 600) });
68
+ * console.log(sightChanged(at(700), at(702)), sightChanged(at(700), at(300)));
69
+ * ```
70
+ */
71
+ export function sightChanged(before, now, step = SIGHT_STEP_PX) {
72
+ const ids = Object.keys(now);
73
+ if (ids.length !== Object.keys(before).length)
74
+ return true;
75
+ for (const id of ids) {
76
+ const a = before[id];
77
+ const b = now[id];
78
+ if (!a || a.inView !== b.inView || a.behind !== b.behind || a.edge !== b.edge)
79
+ return true;
80
+ if (!b.inView && Math.abs(a.along - b.along) >= step)
81
+ return true;
82
+ }
83
+ return false;
84
+ }