@johnmorrisdotca/chikyu-earth 0.47.0 → 0.49.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,5 +1,7 @@
1
1
  import { MAX_SATELLITE_SPEED } from "../satellites/limits.js";
2
2
  import { satelliteEmphasis } from "./satellite-focus.js";
3
+ import { BUILT_IN_PATH_STYLES, capPaths, nearestLoop, resolvePathId, styleOf, styleTable } from "./paths.js";
4
+ import { createPathRibbons } from "./path-ribbons.js";
3
5
  /**
4
6
  * The longest a streak may be along its orbit, in Earth radii: 0.025, about 160 km, which is what a low satellite covers in a frame at 10 minutes a second (150 km). On the screen that is a share of the Earth's drawn radius, so a streak
5
7
  * is a few millimetres when the Earth fills the view, half as long when the Earth is half as large, and a dot when the Earth is tiny. A cap of so many pixels alone would leave the same length on a small Earth, where it looks long.
@@ -54,12 +56,12 @@ export function longestStreak(segments, count, earthPx) {
54
56
  }
55
57
  return longest * earthPx;
56
58
  }
57
- export const newSatelliteState = () => ({ groups: new Map(), selected: null, marked: new Set(), pinned: [], orbitTint: false, focus: null, modelOver: null, hover: { ref: null, fade: 0, enabled: true, color: "#ffffff", opacity: HOVER_PATH_OPACITY }, streakMax: DEFAULT_STREAK_MAX, drawn: { placed: 0, segments: 0, tooFast: false, dotScale: 1, longestStreakPx: 0, streakCapPx: 0, weight: 1, fade: 1, earthPx: 0, updateMs: 0, maxUpdateMs: 0, updates: 0, totalMs: 0, selectedRing: false, hoverPath: false, hoverOpacity: 0, selectedOpacity: 0 } });
59
+ export const newSatelliteState = () => ({ groups: new Map(), selected: null, marked: new Set(), pinned: [], orbitTint: false, focus: null, modelOver: null, hover: { ref: null, fade: 0, enabled: true, color: "#ffffff", opacity: HOVER_PATH_OPACITY }, paths: { requests: [], overrides: {}, selectedOn: true, version: 0, info: [], dropped: 0, unresolved: 0 }, ridden: null, streakMax: DEFAULT_STREAK_MAX, drawn: { placed: 0, segments: 0, tooFast: false, dotScale: 1, longestStreakPx: 0, streakCapPx: 0, weight: 1, fade: 1, earthPx: 0, updateMs: 0, maxUpdateMs: 0, updates: 0, totalMs: 0, selectedRing: false, hoverPath: false, hoverOpacity: 0, selectedOpacity: 0, pathsDrawn: 0, pathDraws: 0, estimated: 0 } });
58
60
  /** The most orbits a group may draw all at once, one line each. */
59
61
  export const MAX_ORBIT_GROUP = 40;
60
62
  const ORBIT_POINTS = 120;
61
63
  /** How bright the chosen satellite's orbit is. The orbit under the pointer is about a third of this, so the two are told apart at a glance. */
62
- export const SELECTED_PATH_OPACITY = 0.85;
64
+ export const SELECTED_PATH_OPACITY = BUILT_IN_PATH_STYLES["selected"].opacity;
63
65
  /** The orbit under the pointer, before it fades. A page may set another with `--chikyu-hover-path-opacity`. */
64
66
  export const HOVER_PATH_OPACITY = SELECTED_PATH_OPACITY / 3;
65
67
  /** How long the faint orbit takes to go after the pointer leaves, in milliseconds. Reduced motion takes it away at once. */
@@ -99,14 +101,19 @@ void main() {
99
101
  float d = length(gl_PointCoord - vec2(0.5));
100
102
  if (d > 0.5) discard;
101
103
  vec3 ink = byClass > 0.5 ? vCls : tint;
104
+ // a dim of 1 is an old element set; above 1 is an estimate (estimate-far): lighter, and fainter than an old one, so the two are told apart
105
+ float estimated = step(1.5, vDim);
106
+ float old = min(vDim, 1.0);
107
+ ink = mix(ink, vec3(1.0), 0.5 * estimated);
108
+ float weak = (1.0 - 0.7 * old) * (1.0 - 0.45 * estimated);
102
109
  if (ring > 0.5) {
103
110
  float a = smoothstep(0.30, 0.38, d) * (1.0 - smoothstep(0.44, 0.5, d));
104
- gl_FragColor = vec4(ink, a * (1.0 - 0.7 * vDim));
111
+ gl_FragColor = vec4(ink, a * weak);
105
112
  } else {
106
113
  float rimEdge = smoothstep(0.28, 0.42, d);
107
- vec3 colour = mix(ink, vec3(0.04, 0.05, 0.08), rimEdge * rim);
114
+ vec3 colour = mix(ink, vec3(0.04, 0.05, 0.08), rimEdge * rim * (1.0 - 0.6 * estimated));
108
115
  float a = smoothstep(0.50, 0.40, d);
109
- gl_FragColor = vec4(colour, a * (1.0 - 0.7 * vDim) * fade);
116
+ gl_FragColor = vec4(colour, a * weak * fade);
110
117
  }
111
118
  }`;
112
119
  /** How long, in milliseconds of wall time, a satellite's place is kept before SGP4 places it again when the clock is too fast to interpolate: about once a second. */
@@ -247,15 +254,15 @@ export function createSatelliteLayer(three, scene, state, ratio) {
247
254
  hoverLoop.renderOrder = 4;
248
255
  scene.add(hoverLoop);
249
256
  let hoverKey = "";
250
- const selectedLine = { positions: new Float32Array(ORBIT_POINTS * 3), geometry: new three.BufferGeometry(), material: new three.LineBasicMaterial({ color: "#ffffff", transparent: true, opacity: SELECTED_PATH_OPACITY }) };
251
- selectedLine.geometry.setAttribute("position", new three.BufferAttribute(selectedLine.positions, 3));
252
- const selectedLoop = new three.LineLoop(selectedLine.geometry, selectedLine.material);
253
- selectedLoop.frustumCulled = false;
254
- selectedLoop.visible = false;
255
- scene.add(selectedLoop);
256
- let selectedKey = "";
257
- let selectedAt = 0;
258
- let selectedWall = 0;
257
+ // The page's paths and the chosen satellite's own orbit: one mesh, one draw call, buffers made once (see path-ribbons.ts).
258
+ const ribbons = createPathRibbons(three, scene);
259
+ let slots = [];
260
+ let slotsSignature = "";
261
+ let resolvedFor = [];
262
+ let resolvedVersion = -1;
263
+ let resolved = [];
264
+ let pathDrawn = false;
265
+ let table = styleTable({});
259
266
  const drop = (entry) => {
260
267
  scene.remove(entry.points);
261
268
  entry.geometry.dispose();
@@ -306,8 +313,99 @@ export function createSatelliteLayer(three, scene, state, ratio) {
306
313
  entry.linesWall = wall;
307
314
  };
308
315
  const find = (ref) => state.groups.get(ref.group)?.satellites;
316
+ const refKey = (ref) => `${ref.group}:${ref.index}`;
317
+ const cleanRef = (ref) => {
318
+ if (!ref)
319
+ return null;
320
+ const group = state.groups.get(ref.group);
321
+ return group && ref.index >= 0 && ref.index < group.satellites.count && satelliteEmphasis(ref.group, group.visible, state.focus).drawn ? ref : null;
322
+ };
323
+ /**
324
+ * The paths to draw now: the page's (the last 16 that match a satellite, in order), then the chosen satellite's own orbit and the ridden one's in the `selected` look, last, so they lie over the rest. The orbits are
325
+ * computed once for a satellite and again only when the clock has moved half an hour and a second has passed, as the chosen orbit always was. The buffers are rewritten only when the list, a look or an orbit changed.
326
+ */
327
+ const syncPaths = (time, wall, chosen) => {
328
+ const pathState = state.paths;
329
+ const sources = [...state.groups.values()].map((group) => group.satellites);
330
+ const same = sources.length === resolvedFor.length && sources.every((source, i) => source === resolvedFor[i]);
331
+ if (!same || resolvedVersion !== pathState.version) {
332
+ resolvedFor = sources;
333
+ resolvedVersion = pathState.version;
334
+ table = styleTable(pathState.overrides);
335
+ resolved = pathState.requests.map((request) => ({ request, ref: resolvePathId(request.id, state.groups) }));
336
+ }
337
+ // a satellite asked for twice is drawn once, in the place and look of the later request
338
+ const byRef = new Map();
339
+ for (const one of resolved) {
340
+ if (!one.ref)
341
+ continue;
342
+ byRef.delete(refKey(one.ref));
343
+ byRef.set(refKey(one.ref), { request: one.request, ref: one.ref });
344
+ }
345
+ const matched = [...byRef.values()];
346
+ const capped = capPaths(matched);
347
+ const wanted = [];
348
+ for (const one of capped.kept) {
349
+ const ref = cleanRef(one.ref);
350
+ if (!ref)
351
+ continue;
352
+ const look = styleOf(one.request, table);
353
+ wanted.push({ id: one.request.id, ref, name: look.name, style: look.style });
354
+ }
355
+ if (pathState.selectedOn) {
356
+ const own = [chosen, state.ridden].map(cleanRef);
357
+ const seen = new Set();
358
+ for (const ref of own) {
359
+ if (!ref || seen.has(refKey(ref)))
360
+ continue;
361
+ seen.add(refKey(ref));
362
+ const norad = find(ref)?.sets[ref.index]?.NORAD_CAT_ID;
363
+ wanted.push({ id: norad ?? refKey(ref), ref, name: "selected", style: table["selected"] });
364
+ }
365
+ }
366
+ const signature = wanted.map((w) => `${refKey(w.ref)}|${w.name}|${w.style.color}|${w.style.width}|${w.style.opacity}|${w.style.dash ? w.style.dash.join(",") : "-"}`).join(";");
367
+ let dirty = signature !== slotsSignature;
368
+ const old = new Map(slots.map((slot) => [slot.key, slot]));
369
+ let empty = 0;
370
+ const next = [];
371
+ for (const w of wanted) {
372
+ const key = `${refKey(w.ref)}|${w.name}`;
373
+ let slot = old.get(key);
374
+ const set = find(w.ref);
375
+ if (!slot) {
376
+ slot = { key, info: { id: w.id, ref: w.ref, style: w.name }, style: w.style, positions: new Float32Array(ORBIT_POINTS * 3), screen: new Float32Array(ORBIT_POINTS * 2), seen: new Uint8Array(ORBIT_POINTS), at: Number.NaN, wall: 0, empty: false };
377
+ dirty = true;
378
+ }
379
+ old.delete(key);
380
+ slot.info = { id: w.id, ref: w.ref, style: w.name };
381
+ slot.style = w.style;
382
+ if (Number.isNaN(slot.at) || (Math.abs(time - slot.at) > ORBIT_REFRESH_GAME_MS && wall - slot.wall > ORBIT_REFRESH_WALL_MS)) {
383
+ slot.at = time;
384
+ slot.wall = wall;
385
+ slot.positions.set(set.orbit(w.ref.index, time, ORBIT_POINTS).subarray(0, ORBIT_POINTS * 3));
386
+ slot.empty = slot.positions.every((v) => v === 0);
387
+ dirty = true;
388
+ }
389
+ if (slot.empty)
390
+ empty += 1;
391
+ next.push(slot);
392
+ }
393
+ slots = next;
394
+ if (dirty) {
395
+ slotsSignature = signature;
396
+ ribbons.set(slots.filter((slot) => !slot.empty).map((slot) => ({ positions: slot.positions, style: slot.style })));
397
+ }
398
+ const own = wanted.filter((w) => w.name === "selected" && pathState.selectedOn);
399
+ pathState.info = slots.filter((slot) => !slot.empty).map((slot) => ({ ...slot.info }));
400
+ pathState.dropped = capped.dropped;
401
+ pathState.unresolved = resolved.length - matched.length + empty;
402
+ pathDrawn = ribbons.mesh.visible;
403
+ state.drawn.pathsDrawn = ribbons.count;
404
+ state.drawn.pathDraws = pathDrawn ? 1 : 0;
405
+ state.drawn.selectedOpacity = own.length > 0 ? own[0].style.opacity : 0;
406
+ };
309
407
  return {
310
- update(time, speed, earthPx = 0) {
408
+ update(time, speed, earthPx = 0, size) {
311
409
  const started = performance.now();
312
410
  const tooFast = Math.abs(speed) > MAX_SATELLITE_SPEED;
313
411
  const step = Math.min(60_000, Math.max(1000, Math.abs(speed) * 1000));
@@ -471,6 +569,8 @@ export function createSatelliteLayer(three, scene, state, ratio) {
471
569
  line.loop.material.opacity = opacity;
472
570
  }
473
571
  }
572
+ if (size)
573
+ ribbons.frame(size.width, size.height, radius);
474
574
  const chosen = state.selected;
475
575
  const chosenGroup = chosen ? state.groups.get(chosen.group) : undefined;
476
576
  const chosenDrawn = chosen !== null && chosenGroup !== undefined && satelliteEmphasis(chosen.group, chosenGroup.visible, state.focus).drawn;
@@ -483,25 +583,13 @@ export function createSatelliteLayer(three, scene, state, ratio) {
483
583
  const over = state.modelOver !== null && state.modelOver.group === chosen.group && state.modelOver.index === chosen.index;
484
584
  selectedPoints.visible = !over;
485
585
  state.drawn.selectedRing = !over;
486
- const key = `${chosen.group}:${chosen.index}`;
487
- if (key !== selectedKey || (Math.abs(time - selectedAt) > ORBIT_REFRESH_GAME_MS && started - selectedWall > ORBIT_REFRESH_WALL_MS)) {
488
- selectedKey = key;
489
- selectedAt = time;
490
- selectedWall = started;
491
- selectedLine.positions.set(set.orbit(chosen.index, time, ORBIT_POINTS).subarray(0, ORBIT_POINTS * 3));
492
- selectedLine.geometry.getAttribute("position").needsUpdate = true;
493
- }
494
- selectedLoop.visible = true;
495
586
  selectedMaterial.uniforms["size"].value = 14 * ratio();
496
- state.drawn.selectedOpacity = selectedLine.material.opacity;
497
587
  }
498
588
  else {
499
589
  selectedPoints.visible = false;
500
590
  state.drawn.selectedRing = false;
501
- selectedLoop.visible = false;
502
- selectedKey = "";
503
- state.drawn.selectedOpacity = 0;
504
591
  }
592
+ syncPaths(time, started, chosen !== null && set !== undefined && chosenDrawn ? chosen : null);
505
593
  // The faint orbit: one path at a time, computed once from the elements already loaded, hidden while it is the chosen satellite's own orbit.
506
594
  const hover = state.hover;
507
595
  const hoverRef = hover.enabled && hover.fade > 0 ? hover.ref : null;
@@ -527,6 +615,11 @@ export function createSatelliteLayer(three, scene, state, ratio) {
527
615
  state.drawn.hoverPath = hoverLoop.visible;
528
616
  state.drawn.hoverOpacity = hoverLoop.visible ? hoverLine.material.opacity : 0;
529
617
  state.drawn.placed = placed;
618
+ let estimated = 0;
619
+ for (const group of state.groups.values())
620
+ if (group.visible)
621
+ estimated += group.satellites.estimatedCount;
622
+ state.drawn.estimated = estimated;
530
623
  state.drawn.segments = segments;
531
624
  state.drawn.longestStreakPx = longest;
532
625
  state.drawn.streakCapPx = capLength * radius;
@@ -633,7 +726,42 @@ export function createSatelliteLayer(three, scene, state, ratio) {
633
726
  else
634
727
  hidden = earthHides(c, world);
635
728
  }
636
- return { x, y, visible: !behind && !hidden && edge === null, heightKm, edge, behind, occluded: hidden };
729
+ return { x, y, visible: !behind && !hidden && edge === null, heightKm, edge, behind, occluded: hidden, estimated: find(ref)?.estimated(ref.index) ?? false };
730
+ },
731
+ pathAt(x, y, camera, size, radius) {
732
+ if (!(radius > 0))
733
+ return null;
734
+ const c = camera.position;
735
+ const ortho = camera.isOrthographicCamera === true;
736
+ const centreDepth = ortho ? view.set(0, 0, 0).applyMatrix4(camera.matrixWorldInverse).z : 0;
737
+ const loops = [];
738
+ const drawn = slots.filter((slot) => !slot.empty);
739
+ // the topmost look is the last drawn, so on a tie the later slot wins: walk them in reverse
740
+ const order = drawn.map((slot, i) => ({ slot, i })).reverse();
741
+ for (const { slot } of order) {
742
+ for (let i = 0; i < ORBIT_POINTS; i += 1) {
743
+ world.set(slot.positions[i * 3], slot.positions[i * 3 + 1], slot.positions[i * 3 + 2]).applyMatrix4(scene.matrixWorld);
744
+ projected.copy(world).project(camera);
745
+ slot.screen[i * 2] = ((projected.x + 1) / 2) * size.width;
746
+ slot.screen[i * 2 + 1] = ((1 - projected.y) / 2) * size.height;
747
+ let seen = projected.z <= 1 && projected.z >= -1 && Number.isFinite(projected.x) && Number.isFinite(projected.y);
748
+ if (seen) {
749
+ if (ortho) {
750
+ view.copy(world).applyMatrix4(camera.matrixWorldInverse);
751
+ seen = !earthHidesDisc(view.x, view.y, view.z, centreDepth);
752
+ }
753
+ else
754
+ seen = !earthHides(c, world);
755
+ }
756
+ slot.seen[i] = seen ? 1 : 0;
757
+ }
758
+ loops.push({ xy: slot.screen, ok: slot.seen, count: ORBIT_POINTS });
759
+ }
760
+ const best = nearestLoop(x, y, loops, radius);
761
+ if (!best)
762
+ return null;
763
+ const slot = order[best.index].slot;
764
+ return { ...slot.info, ref: { ...slot.info.ref }, x: best.x, y: best.y, distancePx: best.distance };
637
765
  },
638
766
  place(camera, size) {
639
767
  if (!selectedPoints.visible)
@@ -650,11 +778,10 @@ export function createSatelliteLayer(three, scene, state, ratio) {
650
778
  for (const entry of drawn.values())
651
779
  drop(entry);
652
780
  drawn.clear();
653
- scene.remove(selectedPoints, selectedLoop, hoverLoop);
781
+ scene.remove(selectedPoints, hoverLoop);
782
+ ribbons.dispose();
654
783
  selectedGeometry.dispose();
655
784
  selectedMaterial.dispose();
656
- selectedLine.geometry.dispose();
657
- selectedLine.material.dispose();
658
785
  hoverLine.geometry.dispose();
659
786
  hoverLine.material.dispose();
660
787
  },
@@ -13,6 +13,7 @@ import type { PlaceView } from "../markers/places.ts";
13
13
  import { type PlaceSpriteData } from "../markers/place-sprites.ts";
14
14
  import type { CountryFrame } from "../country-names/geometry.ts";
15
15
  import type { GeoPoint } from "../types.ts";
16
+ import type { PathHit } from "./paths.ts";
16
17
  import { type SatelliteScreen, type SatelliteState } from "./satellite-layer.ts";
17
18
  import { type BodyLabelState } from "../realistic/body-label.ts";
18
19
  import { type MeteorState } from "../meteors/layer.ts";
@@ -253,6 +254,8 @@ export interface SpaceModel {
253
254
  tall: number;
254
255
  } | null;
255
256
  };
257
+ /** The page's path under a point of the view (CSS pixels), within a radius (`paths`, and the chosen satellite's own orbit), if a draw has been made and one is there. */
258
+ pathAt: ((x: number, y: number, radius: number) => Omit<PathHit, "pointer"> | null) | null;
256
259
  /** The satellite at a point of the view (CSS pixels), within a radius, if a draw has been made and one is there. */
257
260
  pick: ((x: number, y: number, radius: number) => SatelliteRef | null) | null;
258
261
  /** The followed satellite's model, when there is one drawn: where its middle is on the screen (CSS pixels), as of the last draw. */
@@ -138,6 +138,7 @@ export const newSpaceModel = () => ({
138
138
  beam: newBeamState(),
139
139
  route: newRouteState(),
140
140
  pick: null,
141
+ pathAt: null,
141
142
  placeSatellite: null,
142
143
  followModel: null,
143
144
  rideFrame: { share: null, seat: null, fresh: true, ms: 400, moving: false, share_: newEase(), seat_: newEase(), weight_: newEase(), groundKm: null },
@@ -572,9 +573,11 @@ export function createSpaceItem(model, deps) {
572
573
  over = riding.target;
573
574
  }
574
575
  model.satellites.modelOver = over;
576
+ model.satellites.ridden = riding && typeof riding.target === "object" ? riding.target : null;
575
577
  }
576
- layer?.update(model.time, model.speed, model.drawn?.radiusPx ?? 0);
578
+ layer?.update(model.time, model.speed, model.drawn?.radiusPx ?? 0, size);
577
579
  model.pick = layer ? (x, y, radius) => layer.pick(x, y, camera, size, radius) : null;
580
+ model.pathAt = layer ? (x, y, radius) => layer.pathAt(x, y, camera, size, radius) : null;
578
581
  // the camera: looks at the focus body from the direction the sky was in when it was last moved, turned by yaw and tilt, as far as the zoom says; or, riding along, sits at the
579
582
  // body and looks at the Earth
580
583
  let focus = model.focus;
@@ -1248,6 +1251,7 @@ export function createSpaceItem(model, deps) {
1248
1251
  dropModel();
1249
1252
  layer?.dispose();
1250
1253
  model.pick = null;
1254
+ model.pathAt = null;
1251
1255
  model.placeSatellite = null;
1252
1256
  model.tiles = null;
1253
1257
  tiles.dispose();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/chikyu-earth",
3
- "version": "0.47.0",
3
+ "version": "0.49.0",
4
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",