@johnmorrisdotca/wakusei-planets 0.36.0 → 0.37.1

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.
@@ -3,10 +3,12 @@ import type { Item3D } from "@johnmorrisdotca/kazoku-ui/renderer";
3
3
  import { type BodyId, type MoonId } from "../facts.ts";
4
4
  import type { ThreeParts } from "../scene/body.ts";
5
5
  import type { SourceBook } from "../door/book.ts";
6
+ import { type MoonDot } from "./all-moons.ts";
6
7
  import { type PaintLabel, type WakuseiPainter } from "../scene/painter.ts";
7
8
  import "../bodies/all.ts";
8
9
  import type { Vec3 } from "../vec.ts";
9
10
  import { type OrbitArrow } from "./arrows.ts";
11
+ import type { SightInput } from "./sight.ts";
10
12
  import { type SpaceFrame, type SpaceVec3 } from "./frame.ts";
11
13
  import { type RideLook } from "./ride.ts";
12
14
  import { type ScaleMode } from "./scale.ts";
@@ -149,6 +151,9 @@ export interface SpaceModel {
149
151
  anchor: number | null;
150
152
  orbits: boolean;
151
153
  labels: boolean;
154
+ /** The catalogue's moons to draw as dots at a moment (`all-moons`), or null for none; and how many were drawn at the last draw. */
155
+ allMoons: ((ms: number) => readonly MoonDot[]) | null;
156
+ moonDots: number;
152
157
  /** Milliseconds of game time one second of wall time covers (the ephemeris is looked up once per such span). */
153
158
  stepMs: number;
154
159
  /** Game seconds for each second of wall time, with its sign (0 when the clock holds): the places a move is worked out for are those the clock reaches by its end. */
@@ -349,6 +354,33 @@ export interface SpaceDeps {
349
354
  advance?: () => void;
350
355
  /** Where each body is on the screen, in CSS pixels in the box, or null when it is behind the camera. Called at each draw. */
351
356
  onLabels?: (places: Record<string, BodyPlace | null>) => void;
357
+ /** What the camera sees of each body at each draw, by id: the Sun, the planets and the other bodies of `SPACE_BODIES`, the moons of the body looked at, and any other moon drawn; with the picture's size and the point straight ahead of the camera. Not the body the camera rides on. */
358
+ onSight?: (bodies: Record<string, SightInput>, frame: {
359
+ width: number;
360
+ height: number;
361
+ axis: {
362
+ x: number;
363
+ y: number;
364
+ };
365
+ }) => void;
366
+ /** Whether `onSight` is wanted at this draw (left out, it always is): the page has asked for it. */
367
+ wantSight?: () => boolean;
368
+ /** The body looked at, as this draw left it, for marks laid on its surface: its centre and radius in the scene, its pole and prime meridian, the camera's place, the projection to CSS pixels and its radius on the screen. Not while the camera rides. */
369
+ onSurface?: (body: {
370
+ id: BodyId;
371
+ centre: Vec3;
372
+ radius: number;
373
+ pole: Vec3;
374
+ meridian: Vec3;
375
+ eye: Vec3;
376
+ project(point: Vec3): {
377
+ x: number;
378
+ y: number;
379
+ } | null;
380
+ width: number;
381
+ height: number;
382
+ radiusPx: number;
383
+ } | null) => void;
352
384
  /** Called at the end of a draw while the camera is moving from one ride to another, so the next frame is asked for (at the page's frame cap). */
353
385
  onMoving?: () => void;
354
386
  /** The target of the ride could not be placed at this draw. */
@@ -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";
@@ -70,6 +71,8 @@ export const newSpaceModel = () => ({
70
71
  anchor: null,
71
72
  orbits: true,
72
73
  labels: true,
74
+ allMoons: null,
75
+ moonDots: 0,
73
76
  stepMs: 1000,
74
77
  speed: 1,
75
78
  focus: "earth",
@@ -144,6 +147,10 @@ export function createSpaceItem(model, deps) {
144
147
  if (points !== undefined)
145
148
  scene.add(points.object);
146
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);
147
154
  const camera = new three.PerspectiveCamera(FOV, width / Math.max(1, height), 0.05, 1000);
148
155
  const target = new three.Vector3();
149
156
  const projected = new three.Vector3();
@@ -630,6 +637,18 @@ export function createSpaceItem(model, deps) {
630
637
  drawing.update({ toSun, ms: model.time, radiusPx: px[id], view: { camera, width: size.width, height: size.height } });
631
638
  }
632
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
+ }
633
652
  if (points !== undefined && deps.book !== undefined) {
634
653
  if (pointsVersion !== deps.book.version) {
635
654
  pointsVersion = deps.book.version;
@@ -639,6 +658,11 @@ export function createSpaceItem(model, deps) {
639
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 });
640
659
  model.door.points = points.count;
641
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
+ }
642
666
  // the orbits round the Sun, placed by the same mapping as the bodies
643
667
  const drawnRings = [];
644
668
  const hiddenRings = [];
@@ -988,6 +1012,25 @@ export function createSpaceItem(model, deps) {
988
1012
  places[id].behind = cover;
989
1013
  deps.onLabels(places);
990
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
+ }
991
1034
  if (model.rideLost)
992
1035
  deps.onRideLost?.();
993
1036
  if (model.transition)
@@ -999,6 +1042,7 @@ export function createSpaceItem(model, deps) {
999
1042
  },
1000
1043
  dispose() {
1001
1044
  points?.dispose();
1045
+ dots?.dispose();
1002
1046
  if (releaseTimer !== undefined)
1003
1047
  clearTimeout(releaseTimer);
1004
1048
  ended = true;
@@ -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
+ }
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "0.36.0";
1
+ export declare const VERSION = "0.37.1";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "0.36.0";
1
+ export const VERSION = "0.37.1";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/wakusei-planets",
3
- "version": "0.36.0",
3
+ "version": "0.37.1",
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",
@@ -538,6 +538,11 @@
538
538
  "types": "./dist/approaches.d.ts",
539
539
  "import": "./dist/approaches.js",
540
540
  "default": "./dist/approaches.js"
541
+ },
542
+ "./moon-catalogue": {
543
+ "types": "./dist/moon-catalogue.d.ts",
544
+ "import": "./dist/moon-catalogue.js",
545
+ "default": "./dist/moon-catalogue.js"
541
546
  }
542
547
  },
543
548
  "files": [
@@ -552,6 +557,7 @@
552
557
  },
553
558
  "scripts": {
554
559
  "export:data": "node scripts/export-data.ts",
560
+ "places:planned": "node scripts/check-planned.ts",
555
561
  "build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.json && node scripts/copy-textures.mjs",
556
562
  "lint": "eslint .",
557
563
  "typecheck": "tsc -p tsconfig.check.json && tsc -p tsconfig.tools.json && tsc -p tsconfig.demo.json",
@@ -616,7 +622,7 @@
616
622
  },
617
623
  "packageManager": "pnpm@10.28.0",
618
624
  "dependencies": {
619
- "@johnmorrisdotca/kazoku-ui": "^0.37.0"
625
+ "@johnmorrisdotca/kazoku-ui": "^0.41.0"
620
626
  },
621
627
  "peerDependencies": {
622
628
  "@johnmorrisdotca/chikyu-earth": ">=0.10.0",