@johnmorrisdotca/chikyu-earth 0.47.0 → 0.48.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.
@@ -32,6 +32,9 @@ import { MAX_TEXTURE } from "../realistic/tiers.js";
32
32
  import { AU_IN_EARTH_RADII, EARTH_RADIUS_KM, frameAt } from "./ephemeris.js";
33
33
  import { MAX_ORBIT_GROUP, readStreakMax } from "./satellite-layer.js";
34
34
  import { applySatelliteFocus, satelliteEmphasis, satelliteGroupRows, shellRadiusOf, zoomForPerspectiveShell } from "./satellite-focus.js";
35
+ import { claimPress } from "../sky/claim.js";
36
+ import { bindPaths, emptyPathStats, pointerOf } from "./path-controls.js";
37
+ import { styleTable } from "./paths.js";
35
38
  import { bindSatelliteHover, hoverPathOn, hoverStyleOf, namesOnHover as namesHoverOn } from "./satellite-hover.js";
36
39
  import { clampFollow, retargetFollow } from "./follow.js";
37
40
  import { RIDE_FACES, rideTiltLimits, rideTurnDegrees } from "./ride.js";
@@ -174,7 +177,7 @@ const STYLE = `:host{display:block;aspect-ratio:1/1;position:relative;overflow:h
174
177
  * ```
175
178
  */
176
179
  export class ChikyuSpace extends HTMLElement {
177
- static observedAttributes = ["cloud-drift", "camera", "earth-size", "earth-x", "earth-y", "center", "time", "speed", "scale", "labels", "orbits", "bodies", "light", "meteors", "meteors-label", "meteors-strength", ...BODY_LABEL_ATTRIBUTES, ...SKY_ATTRIBUTES, ...CITY_NAMES_ATTRIBUTES, ...BORDER_ATTRIBUTES, ...GRID_ATTRIBUTES, ...BEAM_ATTRIBUTES, ...COUNTRY_NAMES_ATTRIBUTES, ...SEA_NAMES_ATTRIBUTES, "air-thickness", "air-label", "lang", "places", "places-look", "place-groups", "here-city", "route", "bodies-credit", "satellite-tint", "satellite-streak-max", "tiles", "tiles-finer", "tiles-finer-layer", "tiles-finer-date", "tiles-finer-timeout", "ride-viewpoint", "ride-model-size", "hover-path", "names-on-hover", "follow", "follow-ease", "dim-occluded", "ride-model-share", "ride-model-seat", "ride-zoom"];
180
+ static observedAttributes = ["cloud-drift", "camera", "earth-size", "earth-x", "earth-y", "center", "time", "speed", "scale", "labels", "orbits", "bodies", "light", "meteors", "meteors-label", "meteors-strength", ...BODY_LABEL_ATTRIBUTES, ...SKY_ATTRIBUTES, ...CITY_NAMES_ATTRIBUTES, ...BORDER_ATTRIBUTES, ...GRID_ATTRIBUTES, ...BEAM_ATTRIBUTES, ...COUNTRY_NAMES_ATTRIBUTES, ...SEA_NAMES_ATTRIBUTES, "air-thickness", "air-label", "lang", "places", "places-look", "place-groups", "here-city", "route", "bodies-credit", "satellite-tint", "satellite-streak-max", "tiles", "tiles-finer", "tiles-finer-layer", "tiles-finer-date", "tiles-finer-timeout", "ride-viewpoint", "ride-model-size", "hover-path", "names-on-hover", "selected-path", "path-hit", "follow", "follow-ease", "dim-occluded", "ride-model-share", "ride-model-seat", "ride-zoom"];
178
181
  #model = newSpaceModel();
179
182
  /** The finer imagery (NASA's tile service), one source while the attributes stay the same. Off unless `tiles-finer="gibs"`. */
180
183
  #finerHold = createFinerHolder(() => new Date(this.#model.time), () => this.#gate?.request());
@@ -219,6 +222,7 @@ export class ChikyuSpace extends HTMLElement {
219
222
  #chosenLabel;
220
223
  /** The name and the faint orbit while the pointer rests on a satellite. */
221
224
  #hover;
225
+ #pathControls;
222
226
  /** The ease of `follow` from one share to the next, if one is running. */
223
227
  #followTimer;
224
228
  #focusBeforeRide = "Earth";
@@ -407,6 +411,15 @@ export class ChikyuSpace extends HTMLElement {
407
411
  style: () => hoverStyleOf(this),
408
412
  });
409
413
  this.#hover.sync();
414
+ this.#pathControls = bindPaths({
415
+ element: this,
416
+ state: this.#model.satellites,
417
+ request: () => this.#gate?.request(),
418
+ pathAt: (x, y, radius) => this.#model.pathAt?.(x, y, radius) ?? null,
419
+ pick: (x, y) => this.#model.pick?.(x, y, 12) ?? null,
420
+ riding: () => this.#model.ride !== null,
421
+ });
422
+ this.#pathControls.sync();
410
423
  // before the shared hands (which hear it after this): a plain wheel in an immersive view zooms
411
424
  this.addEventListener("wheel", this.#wheel, { capture: true, passive: false });
412
425
  this.addEventListener("keydown", this.#faceKey);
@@ -426,6 +439,8 @@ export class ChikyuSpace extends HTMLElement {
426
439
  this.#zoomRange(false);
427
440
  this.#hover?.stop();
428
441
  this.#hover = undefined;
442
+ this.#pathControls?.stop();
443
+ this.#pathControls = undefined;
429
444
  this.removeEventListener("pointerdown", this.#down);
430
445
  this.removeEventListener("wheel", this.#wheel, { capture: true });
431
446
  this.removeEventListener("keydown", this.#faceKey);
@@ -563,6 +578,11 @@ export class ChikyuSpace extends HTMLElement {
563
578
  }
564
579
  else if (name === "hover-path" || name === "names-on-hover")
565
580
  this.#hover?.sync();
581
+ else if (name === "selected-path")
582
+ this.#pathControls?.sync();
583
+ else if (name === "path-hit") {
584
+ /* read at each press and move */
585
+ }
566
586
  else if (name === "follow")
567
587
  this.#easeFollow(clampFollow(Number(value)));
568
588
  else if (name === "satellite-streak-max") {
@@ -1993,6 +2013,102 @@ export class ChikyuSpace extends HTMLElement {
1993
2013
  const chosen = this.#model.satellites.selected;
1994
2014
  return chosen ? { ...chosen } : null;
1995
2015
  }
2016
+ // --- paths -------------------------------------------------------------------------------------------------------------------------------------
2017
+ /**
2018
+ * Draw these satellites' orbits at once, each in a look from the table (`pathStyles`), without a satellite being chosen. One draw call for all of them and the chosen satellite's own orbit, and nothing is drawn at rest. At most 16 are kept, the last of the list.
2019
+ * `id` is a NORAD catalogue number or `group:index` (`stations:0`); an id that matches no satellite is skipped (`stats.paths.unresolved`). A satellite asked for twice is drawn once, in the later look. `style` is the name of a look (`selected`, `hovered`, `favourite`, `path`, or one of the page's own)
2020
+ * or a partial look over `path`. `[]` takes them all away. Where the pointer is on one, `chikyu-path-hover` and `chikyu-path-click` say which (`path-hit`).
2021
+ *
2022
+ * @param list - The paths, oldest first.
2023
+ *
2024
+ * @example
2025
+ * ```ts
2026
+ * import type { ChikyuSpace } from "@johnmorrisdotca/chikyu-earth/space";
2027
+ *
2028
+ * const show = (space: ChikyuSpace): void => space.setPaths([{ id: 25544, style: "favourite" }, { id: "stations:1", style: { color: "#7fdbff", dash: [8, 4] } }]);
2029
+ * console.log(typeof show);
2030
+ * ```
2031
+ */
2032
+ setPaths(list) {
2033
+ this.#pathControls?.setPaths(list);
2034
+ }
2035
+ /** The paths in use, as set (the last 16). Setting it is `setPaths`. */
2036
+ get paths() {
2037
+ return this.#pathControls?.paths ?? [];
2038
+ }
2039
+ set paths(list) {
2040
+ this.setPaths(list);
2041
+ }
2042
+ /**
2043
+ * Change the looks the paths are drawn in, by name. Each entry is a partial look over the built-in one of that name (or over `path`, for a name of the page's own); `null` takes the page's entry away. Without `replace` the entries are added to those already set.
2044
+ * The built-in looks differ by width and dash as well as colour: `selected` (white, 2 px, solid, also the look of the chosen satellite's own orbit), `hovered` (white, 1.5 px, dashed), `favourite` (amber, 1.75 px, dotted) and `path` (white, 1.5 px, solid).
2045
+ * A look is `{ color, width (0.5 to 8 CSS pixels), opacity (0 to 1), dash ([dash, gap] in CSS pixels, or null) }`.
2046
+ *
2047
+ * @param table - The entries by name.
2048
+ * @param replace - `true` to drop the entries set before.
2049
+ *
2050
+ * @example
2051
+ * ```ts
2052
+ * import type { ChikyuSpace } from "@johnmorrisdotca/chikyu-earth/space";
2053
+ *
2054
+ * const quiet = (space: ChikyuSpace): void => space.setPathStyles({ selected: { color: "#ffd166", width: 3, dash: null } });
2055
+ * console.log(typeof quiet);
2056
+ * ```
2057
+ */
2058
+ setPathStyles(table, replace = false) {
2059
+ this.#pathControls?.setStyles(table, replace);
2060
+ }
2061
+ /** The table of looks in use, whole: the built-in ones with the page's own over them. A copy. */
2062
+ get pathStyles() {
2063
+ return this.#pathControls?.styles ?? styleTable({});
2064
+ }
2065
+ set pathStyles(table) {
2066
+ this.setPathStyles(table, true);
2067
+ }
2068
+ /**
2069
+ * Draw the chosen satellite's own orbit (the attribute `selected-path`; on unless `off`). Off, `selectSatellite` still rings and names the satellite and a ride still follows it, but no line is drawn: the page draws it, with `setPaths` and the `selected` look, or its own. The look of the line when it is on is the `selected` entry of `pathStyles`.
2070
+ */
2071
+ get selectedPath() {
2072
+ return this.getAttribute("selected-path") !== "off";
2073
+ }
2074
+ set selectedPath(value) {
2075
+ if (value)
2076
+ this.removeAttribute("selected-path");
2077
+ else
2078
+ this.setAttribute("selected-path", "off");
2079
+ }
2080
+ /**
2081
+ * How near, in CSS pixels, the pointer has to be to a path for the pick and hover events to report it (the attribute `path-hit`; 8 when left out, 0 to 40; `0` or `off` turns the test off). A touch is given twice that, and at least 16.
2082
+ * The test covers the page's paths and the chosen satellite's own orbit, not the part of an orbit the Earth hides, and a satellite's dot under the pointer comes first. In a ride from a satellite the test is off unless the attribute is set.
2083
+ */
2084
+ get pathHit() {
2085
+ return this.#pathControls?.hit ?? 8;
2086
+ }
2087
+ set pathHit(value) {
2088
+ if (Number.isFinite(value) && value >= 0)
2089
+ this.setAttribute("path-hit", String(value));
2090
+ else
2091
+ this.removeAttribute("path-hit");
2092
+ }
2093
+ /**
2094
+ * The path under a point of the element, from the last draw: its id as given, the satellite (`ref`, the same shape as in `chikyu-satellite-click`), the name of its look, the nearest point on it and the distance to it, or `null`.
2095
+ *
2096
+ * @param x - CSS pixels from the element's left.
2097
+ * @param y - CSS pixels from its top.
2098
+ * @param radiusPx - How near the point must be, 0 to 200; `path-hit` when left out.
2099
+ * @returns The path, or `null` when none is within reach.
2100
+ *
2101
+ * @example
2102
+ * ```ts
2103
+ * import type { ChikyuSpace } from "@johnmorrisdotca/chikyu-earth/space";
2104
+ *
2105
+ * const name = (space: ChikyuSpace, x: number, y: number): string => space.pathAt(x, y, 10)?.style ?? "none";
2106
+ * console.log(typeof name);
2107
+ * ```
2108
+ */
2109
+ pathAt(x, y, radiusPx) {
2110
+ return this.#pathControls?.at(x, y, radiusPx) ?? null;
2111
+ }
1996
2112
  /**
1997
2113
  * Ring and name every satellite of a group (or stop): a ring round each and its name beside it, in the page's language (the ISS, Tiangong and Hubble by their short names, the rest as their elements name them, or as `satelliteNames` says), never over another name, a place's or a ring's.
1998
2114
  * Names and rings are drawn with the places, only for satellites that are in the picture and not behind the Earth, and nothing is drawn at rest. `chikyu-satellite-visibility` is sent with `detail: { ref, visible }` when a labelled satellite comes into view or goes out of it (not at every frame).
@@ -2154,6 +2270,7 @@ export class ChikyuSpace extends HTMLElement {
2154
2270
  time: this.#model.time,
2155
2271
  lookups: this.#model.lookups,
2156
2272
  satellites: this.#satelliteStats(),
2273
+ paths: this.#pathControls?.stats() ?? emptyPathStats(),
2157
2274
  ride: { riding: this.#model.ride !== null, lost: this.#model.rideLost, gliding: this.#model.transition !== null || this.#model.rideFrame.moving, facing: this.facing, groundKm: this.#model.rideFrame.groundKm },
2158
2275
  camera: { position: [this.#model.pose.p[0], this.#model.pose.p[1], this.#model.pose.p[2]], quaternion: [this.#model.pose.q[0], this.#model.pose.q[1], this.#model.pose.q[2], this.#model.pose.q[3]], fov: this.#model.pose.fov },
2159
2276
  bodies: { look: this.#model.bodies || painter?.looks?.[0] || "plain", painted: this.#model.painted.bodies, moons: this.#model.painted.moons, held: this.#model.painted.held, moon: this.#model.painted.moon, tiles: this.#model.painted.tiles, mapped: this.#model.painted.mapped, pending: this.#model.painted.pending },
@@ -2315,9 +2432,14 @@ export class ChikyuSpace extends HTMLElement {
2315
2432
  const y = event.clientY - rect.top;
2316
2433
  const satellite = this.#model.pick?.(x, y, 12);
2317
2434
  if (satellite) {
2435
+ claimPress(event);
2318
2436
  this.dispatchEvent(new CustomEvent("chikyu-satellite-click", { detail: { ...satellite, info: this.satelliteInfo(satellite) }, bubbles: true, composed: true }));
2319
2437
  return;
2320
2438
  }
2439
+ if (this.#pathControls?.press(x, y, pointerOf(event))) {
2440
+ claimPress(event);
2441
+ return;
2442
+ }
2321
2443
  if (!this.#places)
2322
2444
  return;
2323
2445
  let best;
@@ -0,0 +1,56 @@
1
+ import type { SatelliteRef } from "../satellites/types.ts";
2
+ import type { SatelliteState } from "./satellite-layer.ts";
3
+ import { MAX_PATHS, type PathHit, type PathRequest, type PathStyle } from "./paths.ts";
4
+ /** The attributes the paths add to an element. */
5
+ export declare const PATH_ATTRIBUTES: readonly ["selected-path", "path-hit"];
6
+ /** What the element hands the paths. */
7
+ export interface PathHost {
8
+ element: HTMLElement;
9
+ state: SatelliteState;
10
+ /** Ask for a draw. */
11
+ request(): void;
12
+ /** The path under a point of the view within a radius, from the last draw. */
13
+ pathAt(x: number, y: number, radius: number): Omit<PathHit, "pointer"> | null;
14
+ /** The satellite whose dot is under a point, which a path never takes a press from. */
15
+ pick(x: number, y: number): SatelliteRef | null;
16
+ /** The view is a ride from a satellite: the hit test is off there unless `path-hit` is set. */
17
+ riding(): boolean;
18
+ }
19
+ export interface PathStats {
20
+ /** Paths the page asked for. */
21
+ requested: number;
22
+ /** Paths in the one draw (the page's and the chosen satellite's own), as of the last draw. */
23
+ drawn: number;
24
+ /** Asked for beyond {@link MAX_PATHS}, so left out (the earliest). */
25
+ dropped: number;
26
+ /** Asked for but matching no satellite, or with no place. */
27
+ unresolved: number;
28
+ /** The chosen satellite's own orbit is drawn. */
29
+ selectedDrawn: boolean;
30
+ /** Draw calls the paths took at the last draw: 0 or 1. */
31
+ draws: number;
32
+ /** The path the mouse is on, by the id given, or `null`. */
33
+ hover: number | string | null;
34
+ /** `chikyu-path-click` events sent so far. */
35
+ hits: number;
36
+ }
37
+ export interface PathControls {
38
+ setPaths(list: readonly PathRequest[]): void;
39
+ readonly paths: PathRequest[];
40
+ setStyles(table: Readonly<Record<string, Partial<PathStyle> | null>>, replace?: boolean): void;
41
+ readonly styles: Record<string, PathStyle>;
42
+ /** Read `selected-path` and `path-hit` again. */
43
+ sync(): void;
44
+ readonly selectedOn: boolean;
45
+ readonly hit: number;
46
+ at(x: number, y: number, radiusPx?: number, pointer?: PathHit["pointer"]): PathHit | null;
47
+ /** A press that was not a drag, at a point of the element: sends `chikyu-path-click` and answers `true` when a path was under it and no dot was. */
48
+ press(x: number, y: number, pointer: PathHit["pointer"]): boolean;
49
+ stats(): PathStats;
50
+ stop(): void;
51
+ }
52
+ export declare const pointerOf: (event: PointerEvent) => PathHit["pointer"];
53
+ export declare function bindPaths(host: PathHost): PathControls;
54
+ export { MAX_PATHS };
55
+ /** The paths' numbers before the element has bound them. */
56
+ export declare const emptyPathStats: () => PathStats;
@@ -0,0 +1,121 @@
1
+ import { capPaths, hitRadius, MAX_PATHS, readPathHit, styleTable } from "./paths.js";
2
+ /** The attributes the paths add to an element. */
3
+ export const PATH_ATTRIBUTES = ["selected-path", "path-hit"];
4
+ export const pointerOf = (event) => (event.pointerType === "touch" ? "touch" : event.pointerType === "pen" ? "pen" : "mouse");
5
+ export function bindPaths(host) {
6
+ const { element, state } = host;
7
+ let hits = 0;
8
+ let hovered = null;
9
+ let queued = false;
10
+ let point = null;
11
+ const radiusFor = (pointer) => {
12
+ if (host.riding() && !element.hasAttribute("path-hit"))
13
+ return 0;
14
+ return hitRadius(readPathHit(element.getAttribute("path-hit")), pointer);
15
+ };
16
+ const at = (x, y, radiusPx, pointer = "mouse") => {
17
+ const radius = radiusPx === undefined ? radiusFor(pointer) : Math.max(0, Math.min(radiusPx, 200));
18
+ const found = host.pathAt(x, y, radius);
19
+ return found ? { ...found, pointer } : null;
20
+ };
21
+ const detail = (hit) => ({ ...hit, ref: { ...hit.ref } });
22
+ const sameHit = (a, b) => (a === null && b === null) || (a !== null && b !== null && a.id === b.id && a.style === b.style && a.ref.group === b.ref.group && a.ref.index === b.ref.index);
23
+ const apply = () => {
24
+ queued = false;
25
+ if (!point)
26
+ return;
27
+ const dot = host.pick(point.x, point.y);
28
+ const hit = dot ? null : at(point.x, point.y, undefined, "mouse");
29
+ if (sameHit(hit, hovered)) {
30
+ hovered = hit;
31
+ return;
32
+ }
33
+ hovered = hit;
34
+ element.dispatchEvent(new CustomEvent("chikyu-path-hover", { detail: hit ? detail(hit) : null, bubbles: true, composed: true }));
35
+ };
36
+ const move = (event) => {
37
+ if (event.pointerType === "touch" || event.buttons !== 0)
38
+ return;
39
+ if (state.paths.info.length === 0 && hovered === null)
40
+ return;
41
+ const rect = element.getBoundingClientRect();
42
+ point = { x: event.clientX - rect.left, y: event.clientY - rect.top };
43
+ if (queued)
44
+ return;
45
+ queued = true;
46
+ element.ownerDocument.defaultView?.requestAnimationFrame(apply);
47
+ };
48
+ const leave = () => {
49
+ point = null;
50
+ if (hovered === null)
51
+ return;
52
+ hovered = null;
53
+ element.dispatchEvent(new CustomEvent("chikyu-path-hover", { detail: null, bubbles: true, composed: true }));
54
+ };
55
+ element.addEventListener("pointermove", move);
56
+ element.addEventListener("pointerleave", leave);
57
+ element.addEventListener("pointercancel", leave);
58
+ return {
59
+ setPaths(list) {
60
+ state.paths.requests = list.filter((one) => one && (typeof one.id === "number" || typeof one.id === "string")).map((one) => ({ id: one.id, ...(one.style === undefined ? {} : { style: typeof one.style === "string" ? one.style : { ...one.style } }) }));
61
+ state.paths.version += 1;
62
+ host.request();
63
+ },
64
+ get paths() {
65
+ return capPaths(state.paths.requests).kept.map((one) => ({ ...one }));
66
+ },
67
+ setStyles(table, replace = false) {
68
+ const next = replace ? {} : { ...state.paths.overrides };
69
+ for (const [name, style] of Object.entries(table)) {
70
+ if (style === null)
71
+ delete next[name];
72
+ else
73
+ next[name] = { ...style };
74
+ }
75
+ state.paths.overrides = next;
76
+ state.paths.version += 1;
77
+ host.request();
78
+ },
79
+ get styles() {
80
+ return styleTable(state.paths.overrides);
81
+ },
82
+ sync() {
83
+ const on = element.getAttribute("selected-path") !== "off";
84
+ if (on === state.paths.selectedOn)
85
+ return;
86
+ state.paths.selectedOn = on;
87
+ state.paths.version += 1;
88
+ host.request();
89
+ },
90
+ get selectedOn() {
91
+ return state.paths.selectedOn;
92
+ },
93
+ get hit() {
94
+ return readPathHit(element.getAttribute("path-hit"));
95
+ },
96
+ at,
97
+ press(x, y, pointer) {
98
+ if (host.pick(x, y))
99
+ return false;
100
+ const hit = at(x, y, undefined, pointer);
101
+ if (!hit)
102
+ return false;
103
+ hits += 1;
104
+ element.dispatchEvent(new CustomEvent("chikyu-path-click", { detail: detail(hit), bubbles: true, composed: true }));
105
+ return true;
106
+ },
107
+ stats() {
108
+ const info = state.paths;
109
+ return { requested: info.requests.length, drawn: state.drawn.pathsDrawn, dropped: info.dropped, unresolved: info.unresolved, selectedDrawn: state.drawn.selectedOpacity > 0, draws: state.drawn.pathDraws, hover: hovered ? hovered.id : null, hits };
110
+ },
111
+ stop() {
112
+ element.removeEventListener("pointermove", move);
113
+ element.removeEventListener("pointerleave", leave);
114
+ element.removeEventListener("pointercancel", leave);
115
+ hovered = null;
116
+ },
117
+ };
118
+ }
119
+ export { MAX_PATHS };
120
+ /** The paths' numbers before the element has bound them. */
121
+ export const emptyPathStats = () => ({ requested: 0, drawn: 0, dropped: 0, unresolved: 0, selectedDrawn: false, draws: 0, hover: null, hits: 0 });
@@ -0,0 +1,25 @@
1
+ import type * as THREE from "three";
2
+ import type { ThreeParts } from "../realistic/textures.ts";
3
+ import { type PathStyle } from "./paths.ts";
4
+ type Three = ThreeParts;
5
+ /** Points on each orbit loop. The same as the chosen orbit has always had. */
6
+ export declare const PATH_POINTS = 120;
7
+ /** The paths the mesh can hold: those of the page and the chosen satellite's own. */
8
+ export declare const PATH_SLOTS: number;
9
+ /** What one slot is told: the loop's points (3 numbers each, {@link PATH_POINTS} of them) and its look. */
10
+ export interface RibbonPath {
11
+ positions: Float32Array;
12
+ style: PathStyle;
13
+ }
14
+ export interface PathRibbons {
15
+ /** Write the paths into the buffers, none for an empty list. Costs nothing on the GPU until the next draw, and nothing is drawn while there are no paths. */
16
+ set(paths: readonly RibbonPath[]): void;
17
+ /** The size of the picture in CSS pixels and the Earth's radius on it, which a dash is measured against. */
18
+ frame(width: number, height: number, earthPx: number): void;
19
+ /** How many paths are in the buffers. */
20
+ readonly count: number;
21
+ readonly mesh: THREE.Mesh;
22
+ dispose(): void;
23
+ }
24
+ export declare function createPathRibbons(three: Three, scene: THREE.Object3D): PathRibbons;
25
+ export {};
@@ -0,0 +1,165 @@
1
+ import { MAX_PATHS } from "./paths.js";
2
+ /** Points on each orbit loop. The same as the chosen orbit has always had. */
3
+ export const PATH_POINTS = 120;
4
+ /** The paths the mesh can hold: those of the page and the chosen satellite's own. */
5
+ export const PATH_SLOTS = MAX_PATHS + 1;
6
+ const VERT = /* glsl */ `
7
+ uniform vec2 res;
8
+ uniform float dashScale;
9
+ attribute vec3 other;
10
+ attribute vec2 corner;
11
+ attribute vec4 ink;
12
+ attribute vec4 look;
13
+ attribute float span;
14
+ varying vec4 vInk;
15
+ varying float vEdge;
16
+ varying float vHalf;
17
+ varying float vAlong;
18
+ varying vec2 vDash;
19
+ void main() {
20
+ mat4 m = projectionMatrix * modelViewMatrix;
21
+ vec4 a = m * vec4(position, 1.0);
22
+ vec4 b = m * vec4(other, 1.0);
23
+ float near = 0.01;
24
+ if (a.w < near && b.w < near) {
25
+ gl_Position = vec4(2.0, 2.0, 2.0, 1.0);
26
+ return;
27
+ }
28
+ if (a.w < near) a = mix(a, b, (near - a.w) / (b.w - a.w));
29
+ else if (b.w < near) b = mix(b, a, (near - b.w) / (a.w - b.w));
30
+ vec2 hres = res * 0.5;
31
+ vec2 sa = a.xy / a.w * hres;
32
+ vec2 sb = b.xy / b.w * hres;
33
+ vec2 d = sb - sa;
34
+ float len = length(d);
35
+ vec2 dir = len > 1e-4 ? d / len : vec2(1.0, 0.0);
36
+ vec2 normal = vec2(-dir.y, dir.x);
37
+ bool atB = corner.x > 0.5;
38
+ vec4 p = atB ? b : a;
39
+ vec2 s = atB ? sb : sa;
40
+ float reach = look.x * 0.5 + 0.5;
41
+ vec2 at = s + normal * corner.y * reach;
42
+ gl_Position = vec4(at / hres * p.w, p.z, p.w);
43
+ vInk = ink;
44
+ vEdge = corner.y * reach;
45
+ vHalf = look.x * 0.5;
46
+ vAlong = (look.w + (atB ? span : 0.0)) * dashScale;
47
+ vDash = look.yz;
48
+ }`;
49
+ const FRAG = /* glsl */ `
50
+ varying vec4 vInk;
51
+ varying float vEdge;
52
+ varying float vHalf;
53
+ varying float vAlong;
54
+ varying vec2 vDash;
55
+ void main() {
56
+ float cover = clamp(vHalf + 0.5 - abs(vEdge), 0.0, 1.0);
57
+ if (vDash.x > 0.0) {
58
+ float period = vDash.x + vDash.y;
59
+ float m = mod(vAlong, period);
60
+ float inside = m < vDash.x ? min(m, vDash.x - m) : -min(m - vDash.x, period - m);
61
+ cover *= clamp(inside + 0.5, 0.0, 1.0);
62
+ }
63
+ if (cover <= 0.0) discard;
64
+ gl_FragColor = vec4(vInk.rgb, vInk.a * cover);
65
+ #include <colorspace_fragment>
66
+ }`;
67
+ export function createPathRibbons(three, scene) {
68
+ // The quads face either way (a segment's left and right turn with its direction on the screen), so neither side is culled.
69
+ const verts = PATH_SLOTS * PATH_POINTS * 4;
70
+ const position = new Float32Array(verts * 3);
71
+ const other = new Float32Array(verts * 3);
72
+ const corner = new Float32Array(verts * 2);
73
+ const ink = new Float32Array(verts * 4);
74
+ const look = new Float32Array(verts * 4);
75
+ const span = new Float32Array(verts);
76
+ const index = new Uint16Array(PATH_SLOTS * PATH_POINTS * 6);
77
+ for (let q = 0; q < PATH_SLOTS * PATH_POINTS; q += 1) {
78
+ const v = q * 4;
79
+ // the quad's corners: start-left, start-right, end-left, end-right
80
+ corner.set([0, -1, 0, 1, 1, -1, 1, 1], v * 2);
81
+ index.set([v, v + 1, v + 2, v + 2, v + 1, v + 3], q * 6);
82
+ }
83
+ const geometry = new three.BufferGeometry();
84
+ const attributes = {
85
+ position: new three.BufferAttribute(position, 3),
86
+ other: new three.BufferAttribute(other, 3),
87
+ ink: new three.BufferAttribute(ink, 4),
88
+ look: new three.BufferAttribute(look, 4),
89
+ span: new three.BufferAttribute(span, 1),
90
+ };
91
+ for (const [name, attribute] of Object.entries(attributes))
92
+ geometry.setAttribute(name, attribute);
93
+ geometry.setAttribute("corner", new three.BufferAttribute(corner, 2));
94
+ geometry.setIndex(new three.BufferAttribute(index, 1));
95
+ geometry.setDrawRange(0, 0);
96
+ const material = new three.ShaderMaterial({ vertexShader: VERT, fragmentShader: FRAG, uniforms: { res: { value: [1, 1] }, dashScale: { value: 1 } }, transparent: true, depthWrite: false, side: three.DoubleSide });
97
+ const mesh = new three.Mesh(geometry, material);
98
+ mesh.name = "paths";
99
+ mesh.frustumCulled = false;
100
+ mesh.renderOrder = 4;
101
+ mesh.visible = false;
102
+ scene.add(mesh);
103
+ const colour = new three.Color();
104
+ let count = 0;
105
+ return {
106
+ set(paths) {
107
+ count = Math.min(PATH_SLOTS, paths.length);
108
+ for (let p = 0; p < count; p += 1) {
109
+ const { positions, style } = paths[p];
110
+ colour.set(style.color);
111
+ // the arc length at each point, in Earth radii, for the dashes
112
+ let along = 0;
113
+ for (let i = 0; i < PATH_POINTS; i += 1) {
114
+ const j = (i + 1) % PATH_POINTS;
115
+ const ax = positions[i * 3];
116
+ const ay = positions[i * 3 + 1];
117
+ const az = positions[i * 3 + 2];
118
+ const bx = positions[j * 3];
119
+ const by = positions[j * 3 + 1];
120
+ const bz = positions[j * 3 + 2];
121
+ const length = Math.hypot(bx - ax, by - ay, bz - az);
122
+ const q = p * PATH_POINTS + i;
123
+ for (let k = 0; k < 4; k += 1) {
124
+ const v = q * 4 + k;
125
+ position[v * 3] = ax;
126
+ position[v * 3 + 1] = ay;
127
+ position[v * 3 + 2] = az;
128
+ other[v * 3] = bx;
129
+ other[v * 3 + 1] = by;
130
+ other[v * 3 + 2] = bz;
131
+ ink[v * 4] = colour.r;
132
+ ink[v * 4 + 1] = colour.g;
133
+ ink[v * 4 + 2] = colour.b;
134
+ ink[v * 4 + 3] = style.opacity;
135
+ look[v * 4] = style.width;
136
+ look[v * 4 + 1] = style.dash ? style.dash[0] : 0;
137
+ look[v * 4 + 2] = style.dash ? style.dash[1] : 0;
138
+ look[v * 4 + 3] = along;
139
+ span[v] = length;
140
+ }
141
+ along += length;
142
+ }
143
+ }
144
+ for (const attribute of Object.values(attributes))
145
+ attribute.needsUpdate = true;
146
+ geometry.setDrawRange(0, count * PATH_POINTS * 6);
147
+ mesh.visible = count > 0;
148
+ },
149
+ frame(width, height, earthPx) {
150
+ const value = material.uniforms["res"].value;
151
+ value[0] = Math.max(1, width);
152
+ value[1] = Math.max(1, height);
153
+ material.uniforms["dashScale"].value = Math.max(1, earthPx);
154
+ },
155
+ get count() {
156
+ return count;
157
+ },
158
+ mesh,
159
+ dispose() {
160
+ scene.remove(mesh);
161
+ geometry.dispose();
162
+ material.dispose();
163
+ },
164
+ };
165
+ }