@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 +8 -0
- package/NOTICE.md +13 -0
- package/README.md +20 -2
- package/dist/realistic/earth.d.ts +7 -0
- package/dist/realistic/earth.js +7 -0
- package/dist/realistic/element.d.ts +8 -2
- package/dist/realistic/element.js +22 -3
- package/dist/realistic/index.d.ts +2 -0
- package/dist/realistic/index.js +2 -0
- package/dist/realistic/shaders.d.ts +8 -1
- package/dist/realistic/shaders.js +30 -3
- package/dist/realistic/tile-set.d.ts +76 -0
- package/dist/realistic/tile-set.js +403 -0
- package/dist/realistic/tiles.d.ts +211 -0
- package/dist/realistic/tiles.js +266 -0
- package/package.json +4 -2
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
// The maths of the Earth's map tiles, with no browser and no three.js: which pyramid level a zoom needs, and which tiles of that level are in view. Pure.
|
|
2
|
+
//
|
|
3
|
+
// A tile set is the map cut into 512 pixel squares: level L is `1024 * 2^L` pixels across, in `2^(L+1)` columns and `2^L` rows, column 0 at longitude -180 and row 0 at
|
|
4
|
+
// latitude 90, a tile at `<layer>/<level>/<column>/<row>.webp` (`scripts/make-earth-tiles.py` makes one).
|
|
5
|
+
const RAD = Math.PI / 180;
|
|
6
|
+
/**
|
|
7
|
+
* How much larger than a device pixel a map texel may be before the next level is wanted: 1.25 is a quarter larger.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { TEXEL_SLACK } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
12
|
+
*
|
|
13
|
+
* console.log(TEXEL_SLACK);
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export const TEXEL_SLACK = 1.25;
|
|
17
|
+
/**
|
|
18
|
+
* Columns in a level.
|
|
19
|
+
*
|
|
20
|
+
* @param level - A pyramid level.
|
|
21
|
+
* @returns `2^(level + 1)`.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { tileColumns } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
26
|
+
*
|
|
27
|
+
* console.log(tileColumns(4));
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export const tileColumns = (level) => 2 ** (level + 1);
|
|
31
|
+
/**
|
|
32
|
+
* Rows in a level.
|
|
33
|
+
*
|
|
34
|
+
* @param level - A pyramid level.
|
|
35
|
+
* @returns `2^level`.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* import { tileRows } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
40
|
+
*
|
|
41
|
+
* console.log(tileRows(4));
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export const tileRows = (level) => 2 ** level;
|
|
45
|
+
/**
|
|
46
|
+
* The first level to load for a disc: the smallest one whose texels are about a device pixel on the screen, else the finest there is, and none when the whole-Earth picture
|
|
47
|
+
* (`baseWidth` across) is already that sharp.
|
|
48
|
+
*
|
|
49
|
+
* @param radiusPx - The disc's radius in device pixels.
|
|
50
|
+
* @param layer - The levels the set has for this layer.
|
|
51
|
+
* @param baseWidth - The width of the picture the tiles are laid over, 4096 for the day map.
|
|
52
|
+
* @returns A level in `layer`, or `undefined` when no tile is worth loading.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { tileLevel, type TileLayer } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
57
|
+
*
|
|
58
|
+
* const day: TileLayer = { tileSize: 512, levels: [{ level: 3, width: 8192 }, { level: 4, width: 16384 }] };
|
|
59
|
+
* console.log(tileLevel(600, day, 4096), tileLevel(1500, day, 4096), tileLevel(9000, day, 4096));
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export function tileLevel(radiusPx, layer, baseWidth) {
|
|
63
|
+
const needed = (2 * Math.PI * radiusPx) / TEXEL_SLACK;
|
|
64
|
+
if (needed <= baseWidth)
|
|
65
|
+
return undefined;
|
|
66
|
+
const finer = layer.levels.filter((entry) => entry.width > baseWidth).sort((a, b) => a.width - b.width);
|
|
67
|
+
if (finer.length === 0)
|
|
68
|
+
return undefined;
|
|
69
|
+
return (finer.find((entry) => entry.width >= needed) ?? finer[finer.length - 1]).level;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The tile of a coarser or the same level that holds a tile.
|
|
73
|
+
*
|
|
74
|
+
* @param tile - A tile.
|
|
75
|
+
* @param level - A level no finer than the tile's.
|
|
76
|
+
* @returns The tile at `level` that covers it.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* import { coveringTile } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
81
|
+
*
|
|
82
|
+
* console.log(coveringTile({ level: 4, col: 17, row: 5 }, 3));
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export function coveringTile(tile, level) {
|
|
86
|
+
const shift = Math.max(0, tile.level - level);
|
|
87
|
+
return { level: Math.min(level, tile.level), col: tile.col >> shift, row: tile.row >> shift };
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Where a tile's edges are, in longitude and latitude in radians: west, north, east and south.
|
|
91
|
+
*
|
|
92
|
+
* @param tile - A tile.
|
|
93
|
+
* @returns `[west, north, east, south]`.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* import { tileRect } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
98
|
+
*
|
|
99
|
+
* console.log(tileRect({ level: 3, col: 0, row: 0 }));
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
export function tileRect(tile) {
|
|
103
|
+
const columns = tileColumns(tile.level);
|
|
104
|
+
const rows = tileRows(tile.level);
|
|
105
|
+
return [-Math.PI + (tile.col / columns) * 2 * Math.PI, Math.PI / 2 - (tile.row / rows) * Math.PI, -Math.PI + ((tile.col + 1) / columns) * 2 * Math.PI, Math.PI / 2 - ((tile.row + 1) / rows) * Math.PI];
|
|
106
|
+
}
|
|
107
|
+
/** A place on the unit sphere turned to face the camera: +x to the right, +y up, +z toward the viewer (the realistic Earth's own turn: about y by minus the longitude, then about x by the latitude). */
|
|
108
|
+
const turned = (lon, lat, lonRad, latRad) => {
|
|
109
|
+
const cl = Math.cos(lat);
|
|
110
|
+
const x = cl * Math.sin(lon);
|
|
111
|
+
const y = Math.sin(lat);
|
|
112
|
+
const z = cl * Math.cos(lon);
|
|
113
|
+
const x1 = x * Math.cos(lonRad) - z * Math.sin(lonRad);
|
|
114
|
+
const z1 = x * Math.sin(lonRad) + z * Math.cos(lonRad);
|
|
115
|
+
return [x1, y * Math.cos(latRad) - z1 * Math.sin(latRad), y * Math.sin(latRad) + z1 * Math.cos(latRad)];
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* The longitude and latitude (degrees) under a point of the box, or `undefined` where the box shows the sky.
|
|
119
|
+
*
|
|
120
|
+
* @param camera - The camera.
|
|
121
|
+
* @param x - Across the box, CSS pixels from its left.
|
|
122
|
+
* @param y - Down the box, CSS pixels from its top.
|
|
123
|
+
* @returns The longitude and latitude in degrees under that point, or `undefined` where the box shows the sky.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```ts
|
|
127
|
+
* import { pointOnEarth } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
128
|
+
*
|
|
129
|
+
* console.log(pointOnEarth({ lon: 10, lat: 20, width: 800, height: 600, panX: 0, panY: 0, radius: 900 }, 400, 300));
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
export function pointOnEarth(camera, x, y) {
|
|
133
|
+
const px = (x - camera.width / 2 - camera.panX) / camera.radius;
|
|
134
|
+
const py = (camera.height / 2 + camera.panY - y) / camera.radius;
|
|
135
|
+
const rest = 1 - px * px - py * py;
|
|
136
|
+
if (rest < 0)
|
|
137
|
+
return undefined;
|
|
138
|
+
const pz = Math.sqrt(rest);
|
|
139
|
+
const latRad = camera.lat * RAD;
|
|
140
|
+
const lonRad = camera.lon * RAD;
|
|
141
|
+
const y1 = py * Math.cos(latRad) + pz * Math.sin(latRad);
|
|
142
|
+
const z1 = -py * Math.sin(latRad) + pz * Math.cos(latRad);
|
|
143
|
+
const gx = px * Math.cos(lonRad) + z1 * Math.sin(lonRad);
|
|
144
|
+
const gz = -px * Math.sin(lonRad) + z1 * Math.cos(lonRad);
|
|
145
|
+
return { lon: Math.atan2(gx, gz) / RAD, lat: Math.asin(Math.max(-1, Math.min(1, y1))) / RAD };
|
|
146
|
+
}
|
|
147
|
+
/** The columns and rows are counted from the corner; a longitude of exactly 180 is the last column, and a latitude of exactly -90 the last row. */
|
|
148
|
+
const tileAt = (level, lon, lat) => {
|
|
149
|
+
const columns = tileColumns(level);
|
|
150
|
+
const rows = tileRows(level);
|
|
151
|
+
const col = Math.min(columns - 1, Math.max(0, Math.floor(((lon + 180) / 360) * columns)));
|
|
152
|
+
const row = Math.min(rows - 1, Math.max(0, Math.floor(((90 - lat) / 180) * rows)));
|
|
153
|
+
return { level, col, row };
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* The tiles of a level that are in view: those with a sampled point inside the box (a little over, so a tile slides in before its edge does), and those under a grid of points
|
|
157
|
+
* across the box. The antimeridian and the poles need nothing special: a tile is tested where it is on the sphere, and a longitude of 180 is the last column.
|
|
158
|
+
*
|
|
159
|
+
* @param level - A pyramid level.
|
|
160
|
+
* @param camera - The camera.
|
|
161
|
+
* @param sun - The Sun's direction as the camera sees it (+x right, +y up, +z toward the viewer), to read each tile's `ndl`; left out, `ndl` is 1.
|
|
162
|
+
* @returns The tiles in view, the nearest to the middle of the box first.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```ts
|
|
166
|
+
* import { tilesInView } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
167
|
+
*
|
|
168
|
+
* const tiles = tilesInView(4, { lon: 179.9, lat: 89, width: 800, height: 600, panX: 0, panY: 0, radius: 3000 });
|
|
169
|
+
* console.log(tiles.length, tiles[0]);
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
export function tilesInView(level, camera, sun) {
|
|
173
|
+
const columns = tileColumns(level);
|
|
174
|
+
const rows = tileRows(level);
|
|
175
|
+
const lonRad = camera.lon * RAD;
|
|
176
|
+
const latRad = camera.lat * RAD;
|
|
177
|
+
const margin = Math.min(160, Math.min(camera.width, camera.height) / 4);
|
|
178
|
+
const cx = camera.width / 2 + camera.panX;
|
|
179
|
+
const cy = camera.height / 2 + camera.panY;
|
|
180
|
+
const found = new Map();
|
|
181
|
+
const ndlOf = (z) => (sun ? z[0] * sun[0] + z[1] * sun[1] + z[2] * sun[2] : 1);
|
|
182
|
+
for (let row = 0; row < rows; row += 1) {
|
|
183
|
+
for (let col = 0; col < columns; col += 1) {
|
|
184
|
+
const [west, north, east, south] = tileRect({ level, col, row });
|
|
185
|
+
const middle = turned((west + east) / 2, (north + south) / 2, lonRad, latRad);
|
|
186
|
+
if (middle[2] < -0.4)
|
|
187
|
+
continue;
|
|
188
|
+
let inside = false;
|
|
189
|
+
let darkest = 1;
|
|
190
|
+
for (let j = 0; j <= 2; j += 1) {
|
|
191
|
+
for (let i = 0; i <= 2; i += 1) {
|
|
192
|
+
const p = turned(west + ((east - west) * i) / 2, north + ((south - north) * j) / 2, lonRad, latRad);
|
|
193
|
+
darkest = Math.min(darkest, ndlOf(p));
|
|
194
|
+
if (p[2] < 0)
|
|
195
|
+
continue;
|
|
196
|
+
const sx = cx + p[0] * camera.radius;
|
|
197
|
+
const sy = cy - p[1] * camera.radius;
|
|
198
|
+
if (sx >= -margin && sx <= camera.width + margin && sy >= -margin && sy <= camera.height + margin)
|
|
199
|
+
inside = true;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (inside) {
|
|
203
|
+
const d = Math.hypot(cx + middle[0] * camera.radius - camera.width / 2, cy - middle[1] * camera.radius - camera.height / 2);
|
|
204
|
+
found.set(row * columns + col, { d, ndl: darkest });
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
// a tile bigger than the box can have none of its own sample points in it: the points of the box say which tile they are on
|
|
209
|
+
const step = 32;
|
|
210
|
+
for (let y = 0; y <= camera.height + step - 1; y += step) {
|
|
211
|
+
for (let x = 0; x <= camera.width + step - 1; x += step) {
|
|
212
|
+
const place = pointOnEarth(camera, Math.min(x, camera.width), Math.min(y, camera.height));
|
|
213
|
+
if (!place)
|
|
214
|
+
continue;
|
|
215
|
+
const tile = tileAt(level, place.lon, place.lat);
|
|
216
|
+
const key = tile.row * columns + tile.col;
|
|
217
|
+
if (found.has(key))
|
|
218
|
+
continue;
|
|
219
|
+
const [west, north, east, south] = tileRect(tile);
|
|
220
|
+
const p = turned((west + east) / 2, (north + south) / 2, lonRad, latRad);
|
|
221
|
+
const dark = [turned(west, north, lonRad, latRad), turned(east, north, lonRad, latRad), turned(west, south, lonRad, latRad), turned(east, south, lonRad, latRad), p].reduce((least, q) => Math.min(least, ndlOf(q)), 1);
|
|
222
|
+
found.set(key, { d: Math.hypot(cx + p[0] * camera.radius - camera.width / 2, cy - p[1] * camera.radius - camera.height / 2), ndl: dark });
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return [...found]
|
|
226
|
+
.map(([key, value]) => ({ level, col: key % columns, row: Math.floor(key / columns), ...value }))
|
|
227
|
+
.sort((a, b) => a.d - b.d);
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Read a tile set's manifest (`manifest.json`): the tile size and, for each layer, its levels. A manifest with no usable layer gives `undefined`.
|
|
231
|
+
*
|
|
232
|
+
* @param json - The parsed JSON.
|
|
233
|
+
* @returns A layer name to its levels, and the tile size; or `undefined`.
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```ts
|
|
237
|
+
* import { parseManifest } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
238
|
+
*
|
|
239
|
+
* const manifest = parseManifest({ tileSize: 512, layers: { day: { levels: [{ level: 3, width: 8192, height: 4096, columns: 16, rows: 8 }] } } });
|
|
240
|
+
* console.log(manifest?.layers["day"]?.levels);
|
|
241
|
+
* ```
|
|
242
|
+
*/
|
|
243
|
+
export function parseManifest(json) {
|
|
244
|
+
if (typeof json !== "object" || json === null)
|
|
245
|
+
return undefined;
|
|
246
|
+
const given = json;
|
|
247
|
+
if (given.tileSize !== 512 || typeof given.layers !== "object" || given.layers === null)
|
|
248
|
+
return undefined;
|
|
249
|
+
const layers = {};
|
|
250
|
+
for (const [name, layer] of Object.entries(given.layers)) {
|
|
251
|
+
if (!Array.isArray(layer?.levels))
|
|
252
|
+
continue;
|
|
253
|
+
const levels = [];
|
|
254
|
+
for (const entry of layer.levels) {
|
|
255
|
+
// the set must follow the pyramid's rule, or a tile's place on the sphere would be wrong
|
|
256
|
+
if (typeof entry?.level !== "number" || !Number.isInteger(entry.level) || entry.level < 0 || entry.level > 8)
|
|
257
|
+
continue;
|
|
258
|
+
if (entry.width !== 1024 * 2 ** entry.level || entry.columns !== tileColumns(entry.level) || entry.rows !== tileRows(entry.level))
|
|
259
|
+
continue;
|
|
260
|
+
levels.push({ level: entry.level, width: entry.width });
|
|
261
|
+
}
|
|
262
|
+
if (levels.length > 0)
|
|
263
|
+
layers[name] = { tileSize: 512, levels: levels.sort((a, b) => a.level - b.level) };
|
|
264
|
+
}
|
|
265
|
+
return Object.keys(layers).length > 0 ? { tileSize: 512, layers } : undefined;
|
|
266
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johnmorrisdotca/chikyu-earth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.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 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.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"earth",
|
|
@@ -148,6 +148,8 @@
|
|
|
148
148
|
"test:demo": "pnpm site && node scripts/docs-site.mjs build && playwright test --project=chromium-desk --project=chromium-phone --project=webkit-desk --project=webkit-phone --project=chromium-gl",
|
|
149
149
|
"screenshots": "pnpm site && node scripts/screenshots.ts",
|
|
150
150
|
"textures": "python3 scripts/make-textures.py",
|
|
151
|
+
"tiles": "python3 -I scripts/make-earth-tiles.py build",
|
|
152
|
+
"test:tiles": "python3 -I scripts/make-earth-tiles.py selftest",
|
|
151
153
|
"test:demo:one": "pnpm site && node scripts/docs-site.mjs build && playwright test",
|
|
152
154
|
"test:soak": "pnpm site && playwright test --project=soak",
|
|
153
155
|
"docs:site": "pnpm build && node scripts/docs-site.mjs build",
|
|
@@ -192,7 +194,7 @@
|
|
|
192
194
|
"@angular/core": "^22.2.4",
|
|
193
195
|
"@angular/platform-browser": "^22.2.4",
|
|
194
196
|
"@eslint/js": "^9.0.0",
|
|
195
|
-
"@johnmorrisdotca/kazoku-ui": "^0.
|
|
197
|
+
"@johnmorrisdotca/kazoku-ui": "^0.21.0",
|
|
196
198
|
"@johnmorrisdotca/wakusei-planets": "^0.5.0",
|
|
197
199
|
"@playwright/test": "1.64.0",
|
|
198
200
|
"@sveltejs/vite-plugin-svelte": "^7.3.1",
|