@johnmorrisdotca/chikyu-earth 0.16.0 → 0.17.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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.17.0 - 2026-11-08
6
+
7
+ - **Sharp close zoom on the realistic Earth, from finer map tiles** (MODULES-645, the loader): `<chikyu-earth-realistic tiles="/tiles/">` (also `earth.tiles`) reads the tile set's `manifest.json`, picks the level from the zoom and the canvas (about one texel to a device pixel; day level 3 then 4, night level 2 then 3), loads only the tiles in view, six at a time, the middle first, and lays them over the package's pictures, which stay as the far view and the fallback. At most 48 tiles are held (about 70 MB of graphics memory); a tile that leaves view is kept 4 seconds and then freed, the oldest first when over the limit; the level drops when the tiles in view would be more. Each tile that arrives asks for one draw (30 a second at most, eight new tiles to a draw); nothing is drawn at rest or out of sight, a lost context comes back with the held tiles, and without the option, without a manifest or for a tile that fails the Earth is drawn as before. A device with no 4096 picture (`max-texture` under 4096, or 2 GB or less of memory) uses no tiles. `earth.stats.tiles` and the `chikyu-tiles` event say what it is doing. New pure parts in `/realistic`: `tileLevel`, `tilesInView`, `coveringTile`, `pointOnEarth`, `tileRect`, `tileColumns`, `tileRows`, `parseManifest`, `TEXEL_SLACK`, `TILE_LIMIT`, `TILE_KEEP_MS`. **The demo's zoom limit on the realistic look goes from 5 to 20** when its tiles are found (the finest tile level is 4 times the widest picture) and stays 5 when they are not. Tests: the level and tile maths (the antimeridian, the poles, a tile bigger than the box) in `test/tiles.test.ts`; a made-up tile set in `e2e/tiles.gl.ts` (requests, drawing over the base, freeing after zoom-out, night tiles, failed tiles, a lost context, batching); a tiles round in `pnpm test:freeze` (GPU objects flat over six zoom rounds). Guide in English and Japanese: `docs/guides/earth-tiles.md`.
8
+ - The demo moves to kazoku-ui ^0.21.0, whose default zoom range is 0.1x to 50x; the flat globe and the embed page keep their 1x to 5x with `zoom-min` and `zoom-max`.
9
+ - **Earth tile pyramid, the data** (MODULES-645, first phase): `scripts/make-earth-tiles.py` (`pnpm tiles`, `pnpm test:tiles`) cuts NASA's Blue Marble Next Generation (September 2004, with bathymetry, 21600 x 10800) and Black Marble 2016 (3 km, 13500 x 6750) into 512 px WebP tiles, day to level 4 (16384 x 8192), night to level 3 (8192 x 4096): 852 tiles, 10.3 MB. Seamless at the antimeridian, tested on a made-up map in CI. Built by `pages.yml` and served at `/tiles/` beside the demo; never in git, never in the npm package. Guide in English and Japanese (`docs/guides/earth-tiles.md`); sources and terms in NOTICE.md.
10
+ - **Why two packages** (MODULES-521): a guide in English and Japanese (`docs/guides/two-packages.md`), a README section and a Guides row say that chikyu-earth is for the Earth only, that the solar system is wakusei-planets', and that the dependency runs one way. Two tested examples: the two packages on one page (`examples/together/`, built and opened by `pnpm test:usage`) and every entry point bundled with wakusei-planets missing.
11
+ - Tests: the ride's glide is measured on the page's own clock, and the Auto-turn step of the drag-rule test turns by an angle read from the controller, not by a 1.5 s stopwatch; the docs site's browser test is the family's newer one (WebKit on a Mac is pressed with Alt+Tab) (MODULES-629).
12
+
5
13
  ## 0.16.0 - 2026-11-08
6
14
 
7
15
  - **Space Shuttle flights and the Hubble visits** (MODULES-623), in `@johnmorrisdotca/chikyu-earth/satellites`: `missions()` (the 135 flights, STS-1 to STS-135, with orbiter, liftoff and landing in UTC, the people NASA lists, the main payload in English and Japanese, the catalogue number, and `hubble` on STS-31, 61, 82, 103, 109 and 125), `missionOf(id)` (`"STS-31"`, `"31"`, `31`), `missionElements(file)` and `missionPosition(sets, ms, flight?)` (latitude, longitude, height and speed of the craft at a moment, from the element set nearest in time, by the same SGP4 as `createSatellites`; nothing before liftoff or after landing; nothing from elements over two days away). New types `Mission`, `MissionPosition`, `MissionFile`, `Orbiter` and `HubbleVisit`. Facts: NASA's mission pages (public domain, read 2026-11-08, listed in NOTICE.md).
package/NOTICE.md CHANGED
@@ -33,6 +33,19 @@ material, so no other licence applies. This package is not endorsed by NASA and
33
33
  The credit names are NASA's usual lines for these series; the Visible Earth pages (now at science.nasa.gov/earth/earth-observatory) did not show a credit line in the
34
34
  text read on 2026-11-02, so check them if you need the exact wording for print.
35
35
 
36
+ ## The Earth tiles
37
+
38
+ `scripts/make-earth-tiles.py` cuts two bigger NASA maps into a pyramid of 512 px WebP tiles (docs/guides/earth-tiles.md). The tiles are built in CI and served by GitHub Pages beside the demo at `/tiles/`; they are not in git and not in the npm package. Terms, read on 2026-11-08 at https://www.nasa.gov/nasa-brand-center/images-and-media/ (NASA Images and Media Usage Guidelines): "NASA content – images, audio, video, and media files used in the rendition of 3-dimensional models, such as texture maps and polygon data in any format – generally are not subject to copyright in the United States." and "NASA should be acknowledged as the source of the material." The same page says third-party material NASA uses with permission is marked as copyright protected with the holder's name; neither source below is marked so. Not endorsed by NASA; no NASA insignia or logotype is used.
39
+
40
+ | Layer | Source | Credit |
41
+ |---|---|---|
42
+ | `day` (levels 0 to 4, up to 16384 x 8192) | Blue Marble: Next Generation with topography and bathymetry, September 2004, 21600 x 10800 (https://assets.science.nasa.gov/content/dam/science/esd/eo/images/bmng/bmng-topography-bathymetry/september/world.topo.bathy.200409.3x21600x10800.jpg, SHA-256 863036b622ba3dab4eb4ffb7149730b02cfa433ce49672e9b7267022051e04a2); the same month as `day-*.webp` | "NASA Earth Observatory": the page https://science.nasa.gov/earth/earth-observatory/blue-marble-next-generation/ (read 2026-11-08) says "Anyone using or republishing Blue Marble: Next Generation please credit “NASA Earth Observatory.”" It was produced by Reto Stöckli, NASA Earth Observatory (NASA Goddard Space Flight Center). |
43
+ | `night` (levels 0 to 3, up to 8192 x 4096) | Earth at Night, Black Marble 2016 colour map at 3 km, 13500 x 6750 (https://assets.science.nasa.gov/content/dam/science/esd/eo/images/imagerecords/144000/144898/BlackMarble_2016_3km.jpg, SHA-256 230aac448ae68c358be433dd518888cccb3a85ccf66f7b44326441c324ad6725; page https://science.nasa.gov/earth/earth-observatory/earth-at-night/maps/, read 2026-11-08) | NASA Earth Observatory, Suomi NPP VIIRS data (the page names no further credit line in its text; check it for print) |
44
+
45
+ The bathymetry-free Blue Marble maps (base map, and base map with topography, on the same Blue Marble page) fall under the same NASA terms; the tiles use the one with bathymetry, as `day-*.webp` does. The clouds are not tiled: NASA's sharper cloud files (Visible Earth 57747, once at eoimages.gsfc.nasa.gov) are no longer served, and the Blue Marble 2002 page (https://science.nasa.gov/earth/earth-observatory/the-blue-marble-true-color-global-imagery-at-1km-resolution/) offers only the 2048 picture already in `clouds-2048.webp`.
46
+
47
+ Where the pyramid is hosted: GitHub Pages of this public repository, which costs nothing (docs/guides/earth-tiles.md). It adds 852 files and 10.3 MB to the site.
48
+
36
49
  ## The Moon's picture
37
50
 
38
51
  `moon-*.webp` is made by `scripts/make-textures.py` from NASA's Scientific Visualization Studio item 4720, "Moon: LROC colour mosaic" (https://svs.gsfc.nasa.gov/4720/,
package/README.md CHANGED
@@ -131,6 +131,22 @@ export function ChikyuEarth({ center = "139.7,35.7", look = "thick" }: { center?
131
131
 
132
132
  The guides are also in Japanese (日本語), one language to a page: [docs/guides/ja/](docs/guides/ja/getting-started.md).
133
133
 
134
+ ## Two packages: the Earth here, the rest in wakusei-planets
135
+
136
+ Chikyu Earth is for someone who cares about the Earth only: the countries, day and night, city lights, clouds, markers and the satellites round it. The Sun, the other planets and the moons, where each one is on a date, and the solar system as a scene are [wakusei-planets](https://github.com/johnmorrisdotca/wakusei-planets). The dependency runs one way: wakusei-planets may use chikyu-earth, and chikyu-earth never needs wakusei-planets. A page about a city or a flight installs only this one, a page about Saturn only the other, and a page about the Earth in the solar system both; with both, wakusei-planets' line Earth is this package's globe with the countries:
137
+
138
+ ```sh
139
+ npm install @johnmorrisdotca/chikyu-earth @johnmorrisdotca/wakusei-planets
140
+ ```
141
+
142
+ ```ts no-run
143
+ import "@johnmorrisdotca/chikyu-earth/element";
144
+ import "@johnmorrisdotca/wakusei-planets/element";
145
+ import "@johnmorrisdotca/wakusei-planets/earth";
146
+ ```
147
+
148
+ The full page, the rule and what lives where, each example run by `pnpm test:usage`: [Why two packages](docs/guides/two-packages.md).
149
+
134
150
  ## The Earth from space
135
151
 
136
152
  An optional second look, for a page with room for a real picture: day and night by the real Sun, cities' lights, a sun glint on the sea, a cloud layer and the
@@ -146,14 +162,15 @@ atmosphere at the rim, drawn with three.js through kazoku-ui's shared renderer.
146
162
  ```
147
163
 
148
164
  Needs `three` and `@johnmorrisdotca/kazoku-ui` (both optional peers) and the pictures (1.6 MB, from NASA, in `dist/textures/`). It draws nothing while nothing changes,
149
- at most 30 times a second while it does, in one WebGL context, with textures that grow as you zoom and are freed after. All of it, the budget and the tests:
165
+ at most 30 times a second while it does, in one WebGL context, with textures that grow as you zoom and are freed after. With `tiles="/tiles/"` (a folder of NASA map
166
+ tiles, [how](docs/guides/earth-tiles.md)) it loads only the finer tiles in view, so you can zoom in to 20 times and stay sharp. All of it, the budget and the tests:
150
167
  [docs/realistic.md](docs/realistic.md).
151
168
 
152
169
  ## The Sun, the Moon and the planets
153
170
 
154
171
  `<chikyu-space>` (`@johnmorrisdotca/chikyu-earth/space/element`) places the Earth, the Moon, the Sun, the planets and Pluto (the real ones, with their moons, when [wakusei-planets](https://github.com/johnmorrisdotca/wakusei-planets) is installed: [guide](docs/guides/the-real-planets.md)) where they are at a date and time, with the Moon's phase and the Earth's day side
155
172
  from the light, a compressed scale you can read (and a true one that shows how empty it is), time that runs forwards or backwards, and a zoom from the Earth to the solar system. Positions are
156
- computed in the browser by astronomy-engine (MIT), nothing is fetched. [docs/space.md](docs/space.md).
173
+ computed in the browser by astronomy-engine (MIT), nothing is fetched. [docs/space.md](docs/space.md). The solar system is wakusei-planets' to own, so this view is moving there; this package keeps the Earth and its satellites ([why two packages](docs/guides/two-packages.md)).
157
174
 
158
175
  <p align="center"><img alt="The solar system on 2 November 2026 in the compressed scale: orbits round the Sun, the planets named" src="docs/images/space-solar.png" width="420"></p>
159
176
 
@@ -358,6 +375,7 @@ Each guide has a Japanese twin in [docs/guides/ja](docs/guides/ja/getting-starte
358
375
  | [The realistic Earth and markers](docs/guides/realistic-and-markers.md) | The Earth from space, markers |
359
376
  | [Satellites](docs/guides/satellites.md) | SGP4 and the space view |
360
377
  | [Ride along](docs/guides/ride-along.md) | A camera on a moving body |
378
+ | [Why two packages](docs/guides/two-packages.md) | This package and wakusei-planets: which to install, alone and together |
361
379
  | [How it stays light](docs/guides/how-it-works.md) | The budget |
362
380
  | [Architecture](docs/guides/architecture.md) | How the layers fit |
363
381
  | [Troubleshooting](docs/guides/troubleshooting.md) | Something does not appear |
@@ -4,6 +4,7 @@ import type { GeoPoint, GlobeView } from "../types.ts";
4
4
  import { lookAt } from "../view.ts";
5
5
  import type { Layer } from "./tiers.ts";
6
6
  import { type ThreeParts } from "./textures.ts";
7
+ import { type TileSet } from "./tile-set.ts";
7
8
  export type { ThreeParts };
8
9
  type Three = ThreeParts;
9
10
  /**
@@ -48,6 +49,8 @@ export interface EarthModel {
48
49
  radius: number;
49
50
  centre: GeoPoint;
50
51
  } | null;
52
+ /** The map tiles laid over the pictures while the scene is built: what they are doing, for a page or a test. */
53
+ tiles: TileSet | null;
51
54
  /** The pixel ratio the textures are chosen for. */
52
55
  ratio(): number;
53
56
  }
@@ -57,6 +60,10 @@ export interface EarthDeps {
57
60
  textureBase: () => string;
58
61
  /** The widest texture, 4096 at most. */
59
62
  cap: () => number;
63
+ /** Where the finer map tiles are (a URL ending with a slash), or `undefined`: the Earth then keeps the package's own pictures. */
64
+ tiles?: () => string | undefined;
65
+ /** Told once when the tile set has been read: the width in pixels of the finest level of each of its layers. */
66
+ onTiles?: (layers: Record<string, number>) => void;
60
67
  /** Clouds' eastward drift against the ground, degrees an hour. */
61
68
  drift: number;
62
69
  invalidate: () => void;
@@ -2,6 +2,7 @@ import { HOME_VIEW, lookAt } from "../view.js";
2
2
  import { ATMOSPHERE_FRAG, ATMOSPHERE_VERT, CLOUD_FRAG, SURFACE_FRAG, SURFACE_VERT } from "./shaders.js";
3
3
  import { subsolarPoint, unitVector } from "./sun.js";
4
4
  import { createTextureSet } from "./textures.js";
5
+ import { createTileSet } from "./tile-set.js";
5
6
  const RAD = Math.PI / 180;
6
7
  /**
7
8
  * The glow reaches this many radii from the centre; the globe at 1x is drawn to fit inside it.
@@ -24,6 +25,7 @@ export const newModel = () => ({
24
25
  lights: true,
25
26
  light: "sun",
26
27
  drawn: null,
28
+ tiles: null,
27
29
  ratio: () => Math.min(globalThis.devicePixelRatio || 1, 2),
28
30
  });
29
31
  /**
@@ -113,6 +115,8 @@ export function createEarthItem(model, deps) {
113
115
  });
114
116
  const turned = new three.Group();
115
117
  const earth = new three.Mesh(grid, surface);
118
+ const tiles = createTileSet(three, turned, { sunDir, lights: surface.uniforms["lights"], boost: surface.uniforms["boost"], ortho: surface.uniforms["ortho"], water: uniform.water, night: uniform.night }, { assets: deps.assets, base: () => deps.tiles?.(), cap: deps.cap, invalidate: deps.invalidate, ...(deps.onTiles ? { onManifest: deps.onTiles } : {}) });
119
+ model.tiles = tiles;
116
120
  const clouds = new three.Mesh(grid, cloudMaterial);
117
121
  clouds.scale.setScalar(CLOUD_RADIUS);
118
122
  clouds.renderOrder = 2;
@@ -148,6 +152,7 @@ export function createEarthItem(model, deps) {
148
152
  camera.updateProjectionMatrix();
149
153
  model.drawn = { view: { ...view }, time: model.time, radius, centre: { ...model.centre } };
150
154
  textures.sync(radius * model.ratio());
155
+ tiles.sync({ lon: look.lon, lat: look.lat, width: size.width, height: size.height, panX: view.panX, panY: view.panY, radius }, [sunDir.value.x, sunDir.value.y, sunDir.value.z], true, model.ratio());
151
156
  deps.onPose?.({ lon: look.lon, lat: look.lat, radius, cx: size.width / 2 + view.panX, cy: size.height / 2 + view.panY, width: size.width, height: size.height });
152
157
  return false;
153
158
  },
@@ -155,6 +160,8 @@ export function createEarthItem(model, deps) {
155
160
  size = { width: next, height: nextHeight };
156
161
  },
157
162
  dispose() {
163
+ model.tiles = null;
164
+ tiles.dispose();
158
165
  textures.dispose();
159
166
  grid.dispose();
160
167
  surface.dispose();
@@ -4,6 +4,7 @@ import { setSharedRenderer, setThreeLoader } from "./shared.ts";
4
4
  export { setSharedRenderer, setThreeLoader };
5
5
  import type { OverlayHost } from "../markers/layer.ts";
6
6
  import { type Layer } from "./tiers.ts";
7
+ import type { TileStats } from "./tile-set.ts";
7
8
  type EarthLayer = Extract<Layer, "day" | "night" | "clouds" | "water">;
8
9
  /**
9
10
  * What the realistic Earth is doing: `loading` its pictures, `live` and drawing, `unavailable` (no WebGL), `lost` (the GPU took the context; it comes back by itself) or `idle` (another view has the page's one WebGL context; press or focus it to take it).
@@ -32,7 +33,7 @@ export type EarthState = "loading" | "live" | "unavailable" | "lost" | "idle";
32
33
  */
33
34
  export declare const readTime: (value: string | null) => number | "now";
34
35
  /**
35
- * The `<chikyu-earth-realistic>` custom element: the Earth from space, drawn with three.js through kazoku-ui's shared renderer (one WebGL context for the page). It takes `center` (`"lon,lat"`), `time` (`now` or an ISO date; the real Sun at that moment), `clouds="off"`, `lights="off"`, `live-seconds`, `texture-base` (where the pictures are served from) and `max-ratio` (the most device pixels to a CSS pixel the canvas is drawn at, 2 at most, and `0.5` draws at half the size and scales it up: for a background; read when the element connects, and it is the shared renderer's cap for the whole page), and a shared view controller's camera through `setView`. It draws only when something changes, at most 30 times a second, and nothing while it is out of sight or the tab is hidden.
36
+ * The `<chikyu-earth-realistic>` custom element: the Earth from space, drawn with three.js through kazoku-ui's shared renderer (one WebGL context for the page). It takes `center` (`"lon,lat"`), `time` (`now` or an ISO date; the real Sun at that moment), `clouds="off"`, `lights="off"`, `live-seconds`, `texture-base` (where the pictures are served from), `tiles` (where a set of finer map tiles is served from, see the Earth tiles guide: only the tiles in view are loaded, over the package's own pictures, which stay as the far view and the fallback; without it, or when a tile cannot be fetched, the Earth is drawn as before) and `max-ratio` (the most device pixels to a CSS pixel the canvas is drawn at, 2 at most, and `0.5` draws at half the size and scales it up: for a background; read when the element connects, and it is the shared renderer's cap for the whole page), and a shared view controller's camera through `setView`. It draws only when something changes, at most 30 times a second, and nothing while it is out of sight or the tab is hidden.
36
37
  *
37
38
  * @example
38
39
  * ```ts no-run
@@ -42,6 +43,7 @@ export declare const readTime: (value: string | null) => number | "now";
42
43
  * earth.setAttribute("center", "139.7,35.7");
43
44
  * earth.setAttribute("time", "2026-06-21T03:00Z");
44
45
  * earth.setAttribute("texture-base", "/textures/");
46
+ * earth.setAttribute("tiles", "/tiles/");
45
47
  * document.body.append(earth);
46
48
  * ```
47
49
  */
@@ -51,6 +53,9 @@ export declare class ChikyuEarthRealistic extends HTMLElement {
51
53
  connectedCallback(): void;
52
54
  disconnectedCallback(): void;
53
55
  attributeChangedCallback(name: string, _old: string | null, value: string | null): void;
56
+ /** Where the finer map tiles are served from (a folder with `manifest.json`), or `null` for none; the attribute `tiles`. The Earth loads only the tiles in view, over its own pictures. */
57
+ get tiles(): string | null;
58
+ set tiles(value: string | null);
54
59
  /** A box laid over the Earth for markers (see `@johnmorrisdotca/chikyu-earth/markers`): where it is, the pose of the last draw, and a way to hear the next. */
55
60
  get overlay(): OverlayHost;
56
61
  /** Where the view starts: longitude and latitude; the shared controls' yaw and tilt turn it from there. */
@@ -66,10 +71,11 @@ export declare class ChikyuEarthRealistic extends HTMLElement {
66
71
  set light(value: LightMode);
67
72
  get lights(): boolean;
68
73
  set lights(value: boolean);
69
- /** What the element is doing, for a page or a test: its state, the texture widths held, the draws made. */
74
+ /** What the element is doing, for a page or a test: its state, the texture widths held, the map tiles held, the draws made. */
70
75
  get stats(): {
71
76
  state: EarthState;
72
77
  tiers: Record<EarthLayer, number>;
78
+ tiles: TileStats;
73
79
  draws: number;
74
80
  contexts: number;
75
81
  canvas: {
@@ -46,7 +46,7 @@ export const readTime = (value) => {
46
46
  return Number.isFinite(ms) ? ms : "now";
47
47
  };
48
48
  /**
49
- * The `<chikyu-earth-realistic>` custom element: the Earth from space, drawn with three.js through kazoku-ui's shared renderer (one WebGL context for the page). It takes `center` (`"lon,lat"`), `time` (`now` or an ISO date; the real Sun at that moment), `clouds="off"`, `lights="off"`, `live-seconds`, `texture-base` (where the pictures are served from) and `max-ratio` (the most device pixels to a CSS pixel the canvas is drawn at, 2 at most, and `0.5` draws at half the size and scales it up: for a background; read when the element connects, and it is the shared renderer's cap for the whole page), and a shared view controller's camera through `setView`. It draws only when something changes, at most 30 times a second, and nothing while it is out of sight or the tab is hidden.
49
+ * The `<chikyu-earth-realistic>` custom element: the Earth from space, drawn with three.js through kazoku-ui's shared renderer (one WebGL context for the page). It takes `center` (`"lon,lat"`), `time` (`now` or an ISO date; the real Sun at that moment), `clouds="off"`, `lights="off"`, `live-seconds`, `texture-base` (where the pictures are served from), `tiles` (where a set of finer map tiles is served from, see the Earth tiles guide: only the tiles in view are loaded, over the package's own pictures, which stay as the far view and the fallback; without it, or when a tile cannot be fetched, the Earth is drawn as before) and `max-ratio` (the most device pixels to a CSS pixel the canvas is drawn at, 2 at most, and `0.5` draws at half the size and scales it up: for a background; read when the element connects, and it is the shared renderer's cap for the whole page), and a shared view controller's camera through `setView`. It draws only when something changes, at most 30 times a second, and nothing while it is out of sight or the tab is hidden.
50
50
  *
51
51
  * @example
52
52
  * ```ts no-run
@@ -56,11 +56,12 @@ export const readTime = (value) => {
56
56
  * earth.setAttribute("center", "139.7,35.7");
57
57
  * earth.setAttribute("time", "2026-06-21T03:00Z");
58
58
  * earth.setAttribute("texture-base", "/textures/");
59
+ * earth.setAttribute("tiles", "/tiles/");
59
60
  * document.body.append(earth);
60
61
  * ```
61
62
  */
62
63
  export class ChikyuEarthRealistic extends HTMLElement {
63
- static observedAttributes = ["center", "time", "clouds", "lights", "light", "live-seconds"];
64
+ static observedAttributes = ["center", "time", "clouds", "lights", "light", "live-seconds", "tiles"];
64
65
  #model = newModel();
65
66
  #mode = "now";
66
67
  #view;
@@ -114,6 +115,8 @@ export class ChikyuEarthRealistic extends HTMLElement {
114
115
  assets: taken.renderer.assets,
115
116
  textureBase: () => this.#textureBase(),
116
117
  cap: () => this.#textureCap(),
118
+ tiles: () => this.tiles ?? undefined,
119
+ onTiles: (layers) => this.dispatchEvent(new CustomEvent("chikyu-tiles", { detail: { layers } })),
117
120
  drift: 1.5,
118
121
  invalidate: () => this.#gate?.soon(),
119
122
  onPose: (pose) => {
@@ -167,6 +170,21 @@ export class ChikyuEarthRealistic extends HTMLElement {
167
170
  this.light = readLight(value);
168
171
  else if (name === "live-seconds")
169
172
  this.#startTick();
173
+ else if (name === "tiles")
174
+ this.#gate?.soon();
175
+ }
176
+ /** Where the finer map tiles are served from (a folder with `manifest.json`), or `null` for none; the attribute `tiles`. The Earth loads only the tiles in view, over its own pictures. */
177
+ get tiles() {
178
+ const given = this.getAttribute("tiles");
179
+ if (!given)
180
+ return null;
181
+ return given.endsWith("/") ? given : `${given}/`;
182
+ }
183
+ set tiles(value) {
184
+ if (value === null || value === "")
185
+ this.removeAttribute("tiles");
186
+ else
187
+ this.setAttribute("tiles", value);
170
188
  }
171
189
  /** A box laid over the Earth for markers (see `@johnmorrisdotca/chikyu-earth/markers`): where it is, the pose of the last draw, and a way to hear the next. */
172
190
  get overlay() {
@@ -224,12 +242,13 @@ export class ChikyuEarthRealistic extends HTMLElement {
224
242
  this.#model.lights = value;
225
243
  this.#gate?.soon();
226
244
  }
227
- /** What the element is doing, for a page or a test: its state, the texture widths held, the draws made. */
245
+ /** What the element is doing, for a page or a test: its state, the texture widths held, the map tiles held, the draws made. */
228
246
  get stats() {
229
247
  const stats = this.#taken?.renderer.stats;
230
248
  return {
231
249
  state: this.#state,
232
250
  tiers: { ...this.#tiers },
251
+ tiles: this.#model.tiles?.stats ?? { ready: false, level: null, held: 0, shown: 0, loading: 0, failed: 0, requested: 0 },
233
252
  draws: stats?.draws ?? 0,
234
253
  contexts: stats?.contexts ?? 0,
235
254
  canvas: stats?.canvas ?? { width: 0, height: 0, pixels: 0 },
@@ -3,3 +3,5 @@ export { chooseTier, textureBytes, textureCap, MAX_TEXTURE, TIERS, TEXELS_PER_RA
3
3
  export { createGate, frameCap, type Gate } from "./gate.ts";
4
4
  export { lookAt, discRadius, ATMOSPHERE_OUTER, type LightMode } from "./earth.ts";
5
5
  export type { ThreeParts } from "./earth.ts";
6
+ export { TEXEL_SLACK, coveringTile, parseManifest, pointOnEarth, tileColumns, tileLevel, tileRect, tileRows, tilesInView, type TileCamera, type TileId, type TileLayer, type VisibleTile } from "./tiles.ts";
7
+ export { TILE_KEEP_MS, TILE_LIMIT } from "./tile-set.ts";
@@ -4,3 +4,5 @@ export { subsolarPoint, unitVector, julianDay } from "./sun.js";
4
4
  export { chooseTier, textureBytes, textureCap, MAX_TEXTURE, TIERS, TEXELS_PER_RADIUS_PIXEL } from "./tiers.js";
5
5
  export { createGate, frameCap } from "./gate.js";
6
6
  export { lookAt, discRadius, ATMOSPHERE_OUTER } from "./earth.js";
7
+ export { TEXEL_SLACK, coveringTile, parseManifest, pointOnEarth, tileColumns, tileLevel, tileRect, tileRows, tilesInView } from "./tiles.js";
8
+ export { TILE_KEEP_MS, TILE_LIMIT } from "./tile-set.js";
@@ -1,5 +1,12 @@
1
1
  export declare const SURFACE_VERT = "\nvarying vec2 vUv;\nvarying vec3 vNormal;\nvarying vec3 vWorld;\nvoid main() {\n vUv = uv;\n vNormal = normalize(mat3(modelMatrix) * position);\n vWorld = (modelMatrix * vec4(position, 1.0)).xyz;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);\n}";
2
- export declare const SURFACE_FRAG = "\nuniform sampler2D dayMap;\nuniform sampler2D nightMap;\nuniform sampler2D waterMap;\nuniform vec3 sunDir;\nuniform float lights;\nuniform float boost;\nuniform float ortho;\nvarying vec2 vUv;\nvarying vec3 vNormal;\nvarying vec3 vWorld;\nvoid main() {\n vec3 n = normalize(vNormal);\n vec3 toEye = ortho > 0.5 ? vec3(0.0, 0.0, 1.0) : normalize(cameraPosition - vWorld);\n float ndl = dot(n, sunDir);\n float day = smoothstep(-0.10, 0.22, ndl);\n vec3 albedo = texture2D(dayMap, vUv).rgb;\n float water = texture2D(waterMap, vUv).r;\n\n vec3 lit = albedo * (0.07 + 1.0 * pow(max(ndl, 0.0), 0.5));\n float dusk = smoothstep(-0.14, 0.0, ndl) * (1.0 - smoothstep(0.0, 0.30, ndl));\n lit += vec3(0.55, 0.28, 0.14) * dusk * 0.30 * (0.35 + 0.65 * albedo.g);\n\n vec3 h = normalize(sunDir + toEye);\n float glint = pow(max(dot(n, h), 0.0), 220.0) * water * day;\n lit += vec3(1.0, 0.93, 0.80) * glint * 0.7;\n\n vec3 night = texture2D(nightMap, vUv).rgb;\n night = night * night * vec3(1.6, 1.5, 1.3) * lights + night * 0.25;\n vec3 col = mix(night, lit, day);\n col += pow(albedo, vec3(0.75)) * boost * 0.65 * (1.0 - day);\n\n float rim = pow(1.0 - clamp(dot(n, toEye), 0.0, 1.0), 3.0);\n float sunSide = 0.12 + 0.88 * smoothstep(-0.25, 0.5, ndl);\n col += vec3(0.20, 0.45, 1.0) * rim * sunSide * 0.55;\n gl_FragColor = vec4(col, 1.0);\n}";
2
+ export declare const SURFACE_FRAG: string;
3
+ /** The same surface for a map tile: the tile's own program. */
4
+ export declare const TILE_FRAG: string;
5
+ /**
6
+ * A map tile's corners: one 0 to 1 grid for every tile, put on the sphere here from the tile's longitude and latitude range (`rect`: west, north, east, south, in radians) at `radius`.
7
+ * `vUvG` is where the point is on the whole-Earth pictures.
8
+ */
9
+ export declare const TILE_VERT = "\nuniform vec4 rect;\nuniform float radius;\nvarying vec2 vUv;\nvarying vec2 vUvG;\nvarying vec3 vNormal;\nvarying vec3 vWorld;\nvoid main() {\n vUv = uv;\n float lon = mix(rect.x, rect.z, uv.x);\n float lat = mix(rect.y, rect.w, uv.y);\n vec3 unit = vec3(cos(lat) * sin(lon), sin(lat), cos(lat) * cos(lon));\n vUvG = vec2((lon + 3.14159265358979) / 6.28318530717959, 0.5 - lat / 3.14159265358979);\n vNormal = normalize(mat3(modelMatrix) * unit);\n vec4 world = modelMatrix * vec4(unit * radius, 1.0);\n vWorld = world.xyz;\n gl_Position = projectionMatrix * viewMatrix * world;\n}";
3
10
  export declare const CLOUD_FRAG = "\nuniform sampler2D cloudMap;\nuniform vec3 sunDir;\nuniform float shift;\nuniform float opacity;\nvarying vec2 vUv;\nvarying vec3 vNormal;\nvoid main() {\n vec3 n = normalize(vNormal);\n float ndl = dot(n, sunDir);\n float c = texture2D(cloudMap, vec2(vUv.x + shift, vUv.y)).r;\n float a = smoothstep(0.28, 0.88, c) * opacity;\n float day = smoothstep(-0.10, 0.25, ndl);\n vec3 col = mix(vec3(0.015, 0.02, 0.04), vec3(1.0), day * (0.18 + 0.82 * pow(max(ndl, 0.0), 0.45)));\n col += vec3(0.55, 0.28, 0.14) * smoothstep(-0.14, 0.0, ndl) * (1.0 - smoothstep(0.0, 0.25, ndl)) * 0.35;\n gl_FragColor = vec4(col, a);\n}";
4
11
  export declare const ATMOSPHERE_VERT = "\nvarying vec3 vPosView;\nvarying vec3 vNormalView;\nvoid main() {\n vec4 view = modelViewMatrix * vec4(position, 1.0);\n vPosView = view.xyz;\n vNormalView = normalize(normalMatrix * position);\n gl_Position = projectionMatrix * view;\n}";
5
12
  export declare const ATMOSPHERE_FRAG = "\nuniform vec3 sunView;\nuniform float outer;\nuniform float ortho;\nvarying vec3 vPosView;\nvarying vec3 vNormalView;\nvoid main() {\n vec3 n = normalize(vNormalView);\n vec3 v = ortho > 0.5 ? vec3(0.0, 0.0, 1.0) : normalize(-vPosView);\n float nz = clamp(dot(n, v), -1.0, 1.0);\n float rho = outer * sqrt(max(0.0, 1.0 - nz * nz));\n float t = clamp((rho - 1.0) / (outer - 1.0), 0.0, 1.0);\n vec2 dir = normalize(n.xy + vec2(1e-5, 0.0));\n float side = smoothstep(-0.40, 0.55, dot(vec3(dir, 0.0), sunView));\n float glow = pow(1.0 - t, 3.0) * (0.10 + 0.90 * side);\n vec3 col = mix(vec3(0.10, 0.22, 0.60), vec3(0.42, 0.68, 1.0), side);\n gl_FragColor = vec4(col, glow * 0.9);\n}";
@@ -10,7 +10,8 @@ void main() {
10
10
  vWorld = (modelMatrix * vec4(position, 1.0)).xyz;
11
11
  gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
12
12
  }`;
13
- export const SURFACE_FRAG = /* glsl */ `
13
+ /** The surface program. `tile` is the program of a map tile laid over the whole-Earth pictures: the day picture is read at the tile's own coordinates, the rest at the whole Earth's. */
14
+ const surfaceFrag = (tile) => /* glsl */ `
14
15
  uniform sampler2D dayMap;
15
16
  uniform sampler2D nightMap;
16
17
  uniform sampler2D waterMap;
@@ -18,6 +19,7 @@ uniform vec3 sunDir;
18
19
  uniform float lights;
19
20
  uniform float boost;
20
21
  uniform float ortho;
22
+ ${tile ? "uniform vec4 nightRect;\nvarying vec2 vUvG;" : ""}
21
23
  varying vec2 vUv;
22
24
  varying vec3 vNormal;
23
25
  varying vec3 vWorld;
@@ -27,7 +29,7 @@ void main() {
27
29
  float ndl = dot(n, sunDir);
28
30
  float day = smoothstep(-0.10, 0.22, ndl);
29
31
  vec3 albedo = texture2D(dayMap, vUv).rgb;
30
- float water = texture2D(waterMap, vUv).r;
32
+ float water = texture2D(waterMap, ${tile ? "vUvG" : "vUv"}).r;
31
33
 
32
34
  vec3 lit = albedo * (0.07 + 1.0 * pow(max(ndl, 0.0), 0.5));
33
35
  float dusk = smoothstep(-0.14, 0.0, ndl) * (1.0 - smoothstep(0.0, 0.30, ndl));
@@ -37,7 +39,7 @@ void main() {
37
39
  float glint = pow(max(dot(n, h), 0.0), 220.0) * water * day;
38
40
  lit += vec3(1.0, 0.93, 0.80) * glint * 0.7;
39
41
 
40
- vec3 night = texture2D(nightMap, vUv).rgb;
42
+ vec3 night = texture2D(nightMap, ${tile ? "(vUvG - nightRect.xy) / nightRect.zw" : "vUv"}).rgb;
41
43
  night = night * night * vec3(1.6, 1.5, 1.3) * lights + night * 0.25;
42
44
  vec3 col = mix(night, lit, day);
43
45
  col += pow(albedo, vec3(0.75)) * boost * 0.65 * (1.0 - day);
@@ -47,6 +49,31 @@ void main() {
47
49
  col += vec3(0.20, 0.45, 1.0) * rim * sunSide * 0.55;
48
50
  gl_FragColor = vec4(col, 1.0);
49
51
  }`;
52
+ export const SURFACE_FRAG = surfaceFrag(false);
53
+ /** The same surface for a map tile: the tile's own program. */
54
+ export const TILE_FRAG = surfaceFrag(true);
55
+ /**
56
+ * A map tile's corners: one 0 to 1 grid for every tile, put on the sphere here from the tile's longitude and latitude range (`rect`: west, north, east, south, in radians) at `radius`.
57
+ * `vUvG` is where the point is on the whole-Earth pictures.
58
+ */
59
+ export const TILE_VERT = /* glsl */ `
60
+ uniform vec4 rect;
61
+ uniform float radius;
62
+ varying vec2 vUv;
63
+ varying vec2 vUvG;
64
+ varying vec3 vNormal;
65
+ varying vec3 vWorld;
66
+ void main() {
67
+ vUv = uv;
68
+ float lon = mix(rect.x, rect.z, uv.x);
69
+ float lat = mix(rect.y, rect.w, uv.y);
70
+ vec3 unit = vec3(cos(lat) * sin(lon), sin(lat), cos(lat) * cos(lon));
71
+ vUvG = vec2((lon + 3.14159265358979) / 6.28318530717959, 0.5 - lat / 3.14159265358979);
72
+ vNormal = normalize(mat3(modelMatrix) * unit);
73
+ vec4 world = modelMatrix * vec4(unit * radius, 1.0);
74
+ vWorld = world.xyz;
75
+ gl_Position = projectionMatrix * viewMatrix * world;
76
+ }`;
50
77
  export const CLOUD_FRAG = /* glsl */ `
51
78
  uniform sampler2D cloudMap;
52
79
  uniform vec3 sunDir;
@@ -0,0 +1,76 @@
1
+ import type * as THREE from "three";
2
+ import type { AssetCache } from "@johnmorrisdotca/kazoku-ui/renderer";
3
+ import type { ThreeParts } from "./textures.ts";
4
+ import { type TileCamera } from "./tiles.ts";
5
+ /**
6
+ * The most tiles held at once, day and night together, about a megabyte and a third of graphics memory each (a 512 pixel square with its mipmaps).
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { TILE_LIMIT } from "@johnmorrisdotca/chikyu-earth/realistic";
11
+ *
12
+ * console.log(TILE_LIMIT);
13
+ * ```
14
+ */
15
+ export declare const TILE_LIMIT = 48;
16
+ /**
17
+ * Milliseconds a tile that is no longer wanted is kept (the view may come straight back) before it is freed.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * import { TILE_KEEP_MS } from "@johnmorrisdotca/chikyu-earth/realistic";
22
+ *
23
+ * console.log(TILE_KEEP_MS);
24
+ * ```
25
+ */
26
+ export declare const TILE_KEEP_MS = 4000;
27
+ type Three = ThreeParts;
28
+ type Uniform<T> = {
29
+ value: T;
30
+ };
31
+ /** What the tiles share with the surface they are laid over. */
32
+ export interface TileShared {
33
+ sunDir: Uniform<THREE.Vector3>;
34
+ lights: Uniform<number>;
35
+ boost: Uniform<number>;
36
+ ortho: Uniform<number>;
37
+ /** The whole-Earth water and night pictures' uniforms: their value is the picture now held. */
38
+ water: Uniform<THREE.Texture | null>;
39
+ night: Uniform<THREE.Texture | null>;
40
+ }
41
+ export interface TileSetDeps {
42
+ assets: AssetCache;
43
+ /** Where the tile set is, ending with a slash, or `undefined` for none. Read at every draw. */
44
+ base: () => string | undefined;
45
+ /** The widest picture this device holds: tiles are only used where the day picture reaches 4096. */
46
+ cap: () => number;
47
+ /** Ask for one draw. */
48
+ invalidate: () => void;
49
+ /** Told once when a tile set's manifest has been read: the finest width of each layer. */
50
+ onManifest?: (layers: Record<string, number>) => void;
51
+ limit?: number;
52
+ now?: () => number;
53
+ }
54
+ /** What the tile set is doing, for a page or a test. */
55
+ export interface TileStats {
56
+ /** A readable manifest is in hand. */
57
+ ready: boolean;
58
+ /** The level now wanted for the day picture, `null` when the pictures of the package are sharp enough. */
59
+ level: number | null;
60
+ /** Tiles held (loading, loaded, on the sphere or not), and those on the sphere in view. */
61
+ held: number;
62
+ shown: number;
63
+ loading: number;
64
+ /** Tiles that could not be fetched and are not asked for again for a while. */
65
+ failed: number;
66
+ /** Tile fetches made. */
67
+ requested: number;
68
+ }
69
+ export interface TileSet {
70
+ sync(camera: TileCamera, sun: readonly [number, number, number], night: boolean, ratio: number): void;
71
+ readonly stats: TileStats;
72
+ dispose(): void;
73
+ }
74
+ /** Make the set that puts tiles on `parent`, a group that turns with the Earth. */
75
+ export declare function createTileSet(three: Three, parent: THREE.Group, shared: TileShared, deps: TileSetDeps): TileSet;
76
+ export {};