@johnmorrisdotca/chikyu-earth 0.33.2 → 0.34.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.
@@ -8,7 +8,9 @@ import { type ScaleMode } from "./scale.ts";
8
8
  import type { SatelliteRef } from "../satellites/types.ts";
9
9
  import { type RideLook } from "./ride.ts";
10
10
  import type { SkyFrame } from "../sky/frame.ts";
11
- import { type SatelliteState } from "./satellite-layer.ts";
11
+ import type { PlaceView } from "../markers/places.ts";
12
+ import type { CityFrame } from "../cities/geometry.ts";
13
+ import { type SatelliteScreen, type SatelliteState } from "./satellite-layer.ts";
12
14
  import { type BodyLabelState } from "../realistic/body-label.ts";
13
15
  import { type MeteorState } from "../meteors/layer.ts";
14
16
  type Three = ThreeParts;
@@ -186,6 +188,14 @@ export interface SpaceModel {
186
188
  rideDistance: number | null;
187
189
  /** The satellite at a point of the view (CSS pixels), within a radius, if a draw has been made and one is there. */
188
190
  pick: ((x: number, y: number, radius: number) => SatelliteRef | null) | null;
191
+ /** The followed satellite's model, when there is one drawn: where its middle is on the screen (CSS pixels), as of the last draw. */
192
+ followModel: {
193
+ x: number;
194
+ y: number;
195
+ shown: boolean;
196
+ } | null;
197
+ /** Where a satellite of a shown group is on the screen as of the last draw (`placeSatellite`), or `null`. */
198
+ placeSatellite: ((ref: SatelliteRef) => SatelliteScreen | null) | null;
189
199
  ratio(): number;
190
200
  }
191
201
  export declare const newSpaceModel: () => SpaceModel;
@@ -220,6 +230,23 @@ export interface BodyDrawing {
220
230
  billboard?: boolean;
221
231
  dispose(): void;
222
232
  }
233
+ /**
234
+ * The seam for drawing a followed satellite as a 3D model: the package ships no model, the page supplies them. `has` says whether there is a model for a satellite (by NORAD number: `25544` is the ISS); `make` makes it as a drawing of radius 1
235
+ * (its middle at the origin, its long axis along +x, its top, the side that faces space, along +z, and its left along +y), at once or later (a promise); the view places it where the satellite is, points it the way it flies, sizes it to read and
236
+ * gives it the Sun's direction (`light`, `update`). `null` from `make` is no model: the satellite stays a dot.
237
+ *
238
+ * @example
239
+ * ```ts no-run
240
+ * import type { SatellitePainter } from "@johnmorrisdotca/chikyu-earth/space";
241
+ *
242
+ * const painter: SatellitePainter = { has: (norad) => norad === 25544, make: async () => null };
243
+ * console.log(painter.has(25544));
244
+ * ```
245
+ */
246
+ export interface SatellitePainter {
247
+ has(noradId: number): boolean;
248
+ make(three: Three, noradId: number, context: PaintContext): BodyDrawing | null | Promise<BodyDrawing | null>;
249
+ }
223
250
  /**
224
251
  * What the scene tells a painter when it makes a body.
225
252
  *
@@ -290,6 +317,10 @@ export interface SpaceDeps {
290
317
  onLabels?: (places: Record<BodyName, BodyPlace | null>) => void;
291
318
  /** Where the sky falls on the screen at this draw, and the bodies that cover it (the names of the stars and the constellation lines are laid over it). Called at each draw. */
292
319
  onSky?: (frame: SkyFrame) => void;
320
+ /** Where points of the Earth's surface fall on the screen at this draw, for the places drawn on it. Called at each draw. */
321
+ onPlaces?: (view: PlaceView) => void;
322
+ /** Where the Earth is on the screen and how to find a place on it, for the names of cities. Called at each draw, with `null` while no Earth is shown. */
323
+ onCities?: (frame: CityFrame | null) => void;
293
324
  /** The name written on the Earth could not be made. */
294
325
  onLabelError?: (error: unknown) => void;
295
326
  /** Where the chosen satellite is on the screen, or null. Called at each draw while a satellite is chosen (and once when it is let go). */
@@ -297,6 +328,14 @@ export interface SpaceDeps {
297
328
  x: number;
298
329
  y: number;
299
330
  } | null) => void;
331
+ /** Where each member of a labelled group is on the screen at this draw (the groups of `model.satellites.marked`); an empty list once when the last group is let go. */
332
+ onSatelliteMarks?: (marks: {
333
+ ref: SatelliteRef;
334
+ x: number;
335
+ y: number;
336
+ visible: boolean;
337
+ heightKm: number;
338
+ }[]) => void;
300
339
  /** 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). */
301
340
  onMoving?: () => void;
302
341
  /** The target of the ride could not be placed at this draw. */
@@ -305,6 +344,8 @@ export interface SpaceDeps {
305
344
  onSatellitesError?: (error: unknown) => void;
306
345
  /** How the Sun, the planets and their moons are drawn; left out, plain spheres in the right colours (see `BodyPainter`). */
307
346
  painter?: BodyPainter;
347
+ /** The models of large satellites, for a followed satellite (see `SatellitePainter`); none, and a satellite is a dot. */
348
+ satellitePainter?: () => SatellitePainter | undefined;
308
349
  /** The painter could not make a body (the three.js parts handed over lack something it needs); the body is drawn plain instead. */
309
350
  onPainterError?: (error: unknown) => void;
310
351
  }
@@ -6,10 +6,13 @@ import { createTrack, orbitPath, PLANETS } from "./ephemeris.js";
6
6
  import { placeAround, placeMoon, placeSun, RADIUS } from "./scale.js";
7
7
  import { ridePose } from "./ride.js";
8
8
  import { coverBody, covers } from "../sky/cover.js";
9
+ import { MODEL_RADIUS_SHARE, modelMatrix, modelRadius, satelliteAxes } from "./satellite-model.js";
9
10
  import { createSatelliteLayer, newSatelliteState } from "./satellite-layer.js";
10
11
  import { createBodyLabelLayer, newBodyLabelState } from "../realistic/body-label.js";
11
12
  import { createMeteorLayer, newMeteorState } from "../meteors/layer.js";
12
13
  import { subsolarPoint, unitVector } from "../realistic/sun.js";
14
+ /** The most satellites ringed and named at once, over all the labelled groups. */
15
+ const MAX_MARKED = 80;
13
16
  const RAD = Math.PI / 180;
14
17
  const FOV = 35;
15
18
  /** How much of the view's shorter side the Earth's disc fills at zoom 1 when the page does not say: 0.9, so the atmosphere's glow (8% beyond the disc) is inside the view and never cut. */
@@ -87,6 +90,8 @@ export const newSpaceModel = () => ({
87
90
  meteors: newMeteorState(),
88
91
  bodyLabel: newBodyLabelState(),
89
92
  pick: null,
93
+ placeSatellite: null,
94
+ followModel: null,
90
95
  ratio: () => Math.min(globalThis.devicePixelRatio || 1, 2),
91
96
  });
92
97
  /** An inertial direction from the Earth's centre, in scene axes, for right ascension and declination in degrees. */
@@ -269,6 +274,16 @@ export function createSpaceItem(model, deps) {
269
274
  let meteors = null;
270
275
  let meteorTimer;
271
276
  let nameOn = null;
277
+ let marksTold = false;
278
+ // the followed satellite's model: which satellite it is for, the drawing once it is made, and a count so that a model made for a satellite no longer followed is let go
279
+ const modelSlot = { key: "", drawing: null, turn: 0 };
280
+ const dropModel = () => {
281
+ if (!modelSlot.drawing)
282
+ return;
283
+ scene.remove(modelSlot.drawing.object);
284
+ modelSlot.drawing.dispose();
285
+ modelSlot.drawing = null;
286
+ };
272
287
  let layer;
273
288
  const rideState = new Float64Array(6);
274
289
  let selectedShown = false;
@@ -383,6 +398,7 @@ export function createSpaceItem(model, deps) {
383
398
  let focusPos = pos[focus];
384
399
  let focusRadius = radiusOf(focus);
385
400
  let rideFrom = null;
401
+ let followed = null;
386
402
  model.rideLost = false;
387
403
  model.rideMotion = 0;
388
404
  model.rideDistance = null;
@@ -407,6 +423,7 @@ export function createSpaceItem(model, deps) {
407
423
  if (ride.view === "follow") {
408
424
  focusPos = place;
409
425
  focusRadius = Math.max(1e-4, ride.distanceKm / 6371 / fit);
426
+ followed = { place, velocity: [rideState[3], rideState[4], rideState[5]], ref: ride.target };
410
427
  }
411
428
  else
412
429
  rideFrom = { position: place, velocity: [rideState[3], rideState[4], rideState[5]], hidden: null, gameSeconds: 0 };
@@ -527,6 +544,51 @@ export function createSpaceItem(model, deps) {
527
544
  if (held.slot.drawing?.object.visible)
528
545
  paintedMoons += 1;
529
546
  model.painted = { bodies: paintedBodies, moons: paintedMoons };
547
+ // the followed satellite as a model, when the page has one for it: made once, placed where the satellite is, pointed the way it flies, sized to read and lit by the Sun
548
+ const satellitePainter = deps.satellitePainter?.();
549
+ const group = followed ? model.satellites.groups.get(followed.ref.group) : undefined;
550
+ const norad = followed && group ? group.satellites.sets[followed.ref.index]?.NORAD_CAT_ID : undefined;
551
+ const modelKey = norad !== undefined && satellitePainter?.has(norad) ? String(norad) : "";
552
+ if (modelKey !== modelSlot.key) {
553
+ dropModel();
554
+ modelSlot.key = modelKey;
555
+ const turn = (modelSlot.turn += 1);
556
+ if (modelKey && satellitePainter && norad !== undefined) {
557
+ void Promise.resolve(satellitePainter.make(three, norad, { look: "", invalidate: () => deps.invalidate(), ratio: model.ratio(), maxTexture: deps.cap() }))
558
+ .then((made) => {
559
+ if (!made)
560
+ return;
561
+ if (ended || modelSlot.turn !== turn) {
562
+ made.dispose();
563
+ return;
564
+ }
565
+ made.object.matrixAutoUpdate = false;
566
+ made.object.visible = false;
567
+ scene.add(made.object);
568
+ modelSlot.drawing = made;
569
+ deps.invalidate();
570
+ })
571
+ .catch((error) => deps.onPainterError?.(error));
572
+ }
573
+ }
574
+ const axes = followed ? satelliteAxes(followed.place, followed.velocity) : null;
575
+ if (modelSlot.drawing && followed && axes) {
576
+ const object = modelSlot.drawing.object;
577
+ const away = Math.hypot(followed.place[0] - camera.position.x, followed.place[1] - camera.position.y, followed.place[2] - camera.position.z);
578
+ object.matrix.set(...modelMatrix(followed.place, axes, modelRadius(away, half, MODEL_RADIUS_SHARE, 1e-9)));
579
+ object.matrixWorldNeedsUpdate = true;
580
+ object.visible = true;
581
+ const toSunNow = unit(frame.sun);
582
+ modelSlot.drawing.light?.(toSunNow);
583
+ modelSlot.drawing.update?.({ toSun: toSunNow, ms: model.time, radiusPx: size.height * MODEL_RADIUS_SHARE });
584
+ projected.set(followed.place[0], followed.place[1], followed.place[2]).project(camera);
585
+ model.followModel = { x: ((projected.x + 1) / 2) * size.width, y: ((1 - projected.y) / 2) * size.height, shown: true };
586
+ }
587
+ else {
588
+ if (modelSlot.drawing)
589
+ modelSlot.drawing.object.visible = false;
590
+ model.followModel = null;
591
+ }
530
592
  const sunToSun = unit([-frame.sun[0], -frame.sun[1], -frame.sun[2]]);
531
593
  sunDrawing?.update?.({ toSun: sunToSun, ms: model.time, radiusPx: px.Sun });
532
594
  for (const name of PLANETS) {
@@ -595,6 +657,65 @@ export function createSpaceItem(model, deps) {
595
657
  }
596
658
  deps.onLabels(places);
597
659
  }
660
+ // the ringed satellites first, so their names are laid out with the places' in this same draw
661
+ model.placeSatellite = layer ? (ref) => layer.placeOf(ref, camera, size) : null;
662
+ if (deps.onSatelliteMarks) {
663
+ if (layer && model.satellites.marked.size > 0) {
664
+ const marks = [];
665
+ for (const name of model.satellites.marked) {
666
+ const group = model.satellites.groups.get(name);
667
+ if (!group)
668
+ continue;
669
+ for (let index = 0; index < group.satellites.count && marks.length < MAX_MARKED; index += 1) {
670
+ const ref = { group: name, index };
671
+ const at = layer.placeOf(ref, camera, size);
672
+ if (at)
673
+ marks.push({ ref, ...at });
674
+ }
675
+ }
676
+ marksTold = true;
677
+ deps.onSatelliteMarks(marks);
678
+ }
679
+ else if (marksTold) {
680
+ marksTold = false;
681
+ deps.onSatelliteMarks([]);
682
+ }
683
+ }
684
+ if (deps.onPlaces) {
685
+ // a place on the ground, turned with the Earth into the scene's axes, seen through the camera: where it falls, which way its upright points on the screen, and whether it faces the camera
686
+ const turn = earth.rotation.y;
687
+ const cos = Math.cos(turn);
688
+ const sin = Math.sin(turn);
689
+ const eye = camera.position;
690
+ deps.onPlaces({
691
+ width: size.width,
692
+ height: size.height,
693
+ at(lon, lat, out) {
694
+ const la = lat * RAD;
695
+ const lo = lon * RAD;
696
+ const lx = Math.cos(la) * Math.sin(lo);
697
+ const lz = Math.cos(la) * Math.cos(lo);
698
+ const wx = lx * cos + lz * sin;
699
+ const wy = Math.sin(la);
700
+ const wz = -lx * sin + lz * cos;
701
+ const away = Math.hypot(eye.x - wx, eye.y - wy, eye.z - wz) || 1;
702
+ out.facing = (wx * (eye.x - wx) + wy * (eye.y - wy) + wz * (eye.z - wz)) / away;
703
+ projected.set(wx, wy, wz).project(camera);
704
+ if (projected.z > 1 || projected.z < -1)
705
+ return false;
706
+ out.x = ((projected.x + 1) / 2) * size.width;
707
+ out.y = ((1 - projected.y) / 2) * size.height;
708
+ projected.set(wx * 1.001, wy * 1.001, wz * 1.001).project(camera);
709
+ const dx = ((projected.x + 1) / 2) * size.width - out.x;
710
+ const dy = ((1 - projected.y) / 2) * size.height - out.y;
711
+ const seen = Math.hypot(dx, dy);
712
+ const side = Math.sqrt(Math.max(0, 1 - out.facing * out.facing));
713
+ out.upX = seen > 0 ? (dx / seen) * side : 0;
714
+ out.upY = seen > 0 ? (dy / seen) * side : 0;
715
+ return true;
716
+ },
717
+ });
718
+ }
598
719
  // the name written on the Earth: made the first time the settings ask for it; none while a ride along a body hides the Earth
599
720
  if (model.bodyLabel.settings && !nameOn)
600
721
  nameOn = createBodyLabelLayer(three, earth, model.bodyLabel, { invalidate: () => deps.invalidate(), ...(deps.onLabelError ? { onError: deps.onLabelError } : {}) });
@@ -636,6 +757,34 @@ export function createSpaceItem(model, deps) {
636
757
  },
637
758
  });
638
759
  }
760
+ if (deps.onCities) {
761
+ if (hidden === "Earth")
762
+ deps.onCities(null);
763
+ else {
764
+ // a place on the ground, turned with the Earth (the Earth is at the middle, one unit in radius): in sight when the viewer is above its horizon, found through the camera
765
+ const ground = new three.Vector3();
766
+ const eye = camera.position;
767
+ const turned = earth.quaternion;
768
+ deps.onCities({
769
+ width: size.width,
770
+ height: size.height,
771
+ radius: Math.min(px.Earth, 1e5),
772
+ project(lon, lat, out) {
773
+ const phi = lat * RAD;
774
+ const lambda = lon * RAD;
775
+ ground.set(Math.cos(phi) * Math.sin(lambda), Math.sin(phi), Math.cos(phi) * Math.cos(lambda)).applyQuaternion(turned);
776
+ const dx = eye.x - ground.x;
777
+ const dy = eye.y - ground.y;
778
+ const dz = eye.z - ground.z;
779
+ const facing = (ground.x * dx + ground.y * dy + ground.z * dz) / (Math.hypot(dx, dy, dz) || 1);
780
+ ground.project(camera);
781
+ out.x = ((ground.x + 1) / 2) * size.width;
782
+ out.y = ((1 - ground.y) / 2) * size.height;
783
+ return ground.z > 1 || ground.z < -1 ? -1 : facing;
784
+ },
785
+ });
786
+ }
787
+ }
639
788
  if (layer && deps.onSelected) {
640
789
  const at = model.satellites.selected ? layer.place(camera, size) : null;
641
790
  if (at || selectedShown)
@@ -672,8 +821,11 @@ export function createSpaceItem(model, deps) {
672
821
  clearTimeout(meteorTimer);
673
822
  meteors?.dispose();
674
823
  nameOn?.dispose();
824
+ modelSlot.turn += 1;
825
+ dropModel();
675
826
  layer?.dispose();
676
827
  model.pick = null;
828
+ model.placeSatellite = null;
677
829
  textures.dispose();
678
830
  ended = true;
679
831
  empty(sunSlot);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/chikyu-earth",
3
- "version": "0.33.2",
4
- "description": "Chikyu Earth 地球 — A line-drawn globe for the web: country outlines that turn at the Earth's own rate, centred on where you are, with an optional realistic Earth from space (three.js), the Sun, Moon and planets, satellites on their orbits, and markers for any place. A web component and an SVG string.",
3
+ "version": "0.34.0",
4
+ "description": "Chikyu Earth 地球 — A line-drawn globe for the web: country outlines that turn at the Earth's own rate, centred on the place you give it, with an optional realistic Earth from space (three.js), the Sun, Moon and planets, satellites on their orbits, and markers for any place. A web component and an SVG string.",
5
5
  "keywords": [
6
6
  "earth",
7
7
  "globe",
@@ -200,6 +200,7 @@
200
200
  },
201
201
  "peerDependencies": {
202
202
  "@johnmorrisdotca/hoshi-stars": ">=0.30.0",
203
+ "@johnmorrisdotca/kuni": ">=1.6.1",
203
204
  "@johnmorrisdotca/wakusei-planets": ">=0.5.0",
204
205
  "astronomy-engine": ">=2.1.0",
205
206
  "satellite.js": ">=6.0.0",
@@ -220,6 +221,9 @@
220
221
  },
221
222
  "@johnmorrisdotca/hoshi-stars": {
222
223
  "optional": true
224
+ },
225
+ "@johnmorrisdotca/kuni": {
226
+ "optional": true
223
227
  }
224
228
  },
225
229
  "devDependencies": {
@@ -230,7 +234,7 @@
230
234
  "@eslint/js": "^9.0.0",
231
235
  "@johnmorrisdotca/hata": "^1.8.0",
232
236
  "@johnmorrisdotca/hoshi-stars": "^0.30.0",
233
- "@johnmorrisdotca/kuni": "^1.5.0",
237
+ "@johnmorrisdotca/kuni": "^1.6.1",
234
238
  "@johnmorrisdotca/wakusei-planets": "^0.5.0",
235
239
  "@playwright/test": "1.64.0",
236
240
  "@sveltejs/vite-plugin-svelte": "^7.3.1",