@johnmorrisdotca/chikyu-earth 0.16.0 → 0.18.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 +12 -0
- package/NOTICE.md +13 -0
- package/README.md +44 -14
- package/dist/element.d.ts +4 -0
- package/dist/element.js +26 -2
- package/dist/realistic/earth.d.ts +7 -0
- package/dist/realistic/earth.js +7 -0
- package/dist/realistic/element.d.ts +11 -2
- package/dist/realistic/element.js +48 -4
- 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/dist/space/element.d.ts +5 -0
- package/dist/space/element.js +37 -0
- package/package.json +5 -6
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One tile of the map pyramid: a level, a column counted east from longitude -180 and a row counted south from latitude 90.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import type { TileId } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
7
|
+
*
|
|
8
|
+
* const tile: TileId = { level: 4, col: 17, row: 5 };
|
|
9
|
+
* console.log(tile);
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export interface TileId {
|
|
13
|
+
level: number;
|
|
14
|
+
col: number;
|
|
15
|
+
row: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* What a layer of a tile set holds, from its manifest: the pyramid's levels, each as wide as it is, and the tile size.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import type { TileLayer } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
23
|
+
*
|
|
24
|
+
* const day: TileLayer = { tileSize: 512, levels: [{ level: 3, width: 8192 }, { level: 4, width: 16384 }] };
|
|
25
|
+
* console.log(day.levels.length);
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export interface TileLayer {
|
|
29
|
+
tileSize: number;
|
|
30
|
+
levels: {
|
|
31
|
+
level: number;
|
|
32
|
+
width: number;
|
|
33
|
+
}[];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Where the camera is, as the realistic Earth draws it: the place in the middle of the disc, the box in CSS pixels, the pan, and the disc's radius in CSS pixels.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* import type { TileCamera } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
41
|
+
*
|
|
42
|
+
* const camera: TileCamera = { lon: 10, lat: 20, width: 800, height: 600, panX: 0, panY: 0, radius: 900 };
|
|
43
|
+
* console.log(camera.radius);
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export interface TileCamera {
|
|
47
|
+
lon: number;
|
|
48
|
+
lat: number;
|
|
49
|
+
width: number;
|
|
50
|
+
height: number;
|
|
51
|
+
panX: number;
|
|
52
|
+
panY: number;
|
|
53
|
+
radius: number;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* A tile that is in view: where, how far its middle is from the middle of the box (CSS pixels, for loading the middle first) and how dark its darkest sampled point is (`ndl`, the
|
|
57
|
+
* cosine of the angle to the Sun, only when a Sun was given).
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* import type { VisibleTile } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
62
|
+
*
|
|
63
|
+
* const tile: VisibleTile = { level: 3, col: 0, row: 0, d: 120, ndl: -0.4 };
|
|
64
|
+
* console.log(tile.d);
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export interface VisibleTile extends TileId {
|
|
68
|
+
d: number;
|
|
69
|
+
ndl: number;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* How much larger than a device pixel a map texel may be before the next level is wanted: 1.25 is a quarter larger.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* import { TEXEL_SLACK } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
77
|
+
*
|
|
78
|
+
* console.log(TEXEL_SLACK);
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export declare const TEXEL_SLACK = 1.25;
|
|
82
|
+
/**
|
|
83
|
+
* Columns in a level.
|
|
84
|
+
*
|
|
85
|
+
* @param level - A pyramid level.
|
|
86
|
+
* @returns `2^(level + 1)`.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* import { tileColumns } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
91
|
+
*
|
|
92
|
+
* console.log(tileColumns(4));
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export declare const tileColumns: (level: number) => number;
|
|
96
|
+
/**
|
|
97
|
+
* Rows in a level.
|
|
98
|
+
*
|
|
99
|
+
* @param level - A pyramid level.
|
|
100
|
+
* @returns `2^level`.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* import { tileRows } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
105
|
+
*
|
|
106
|
+
* console.log(tileRows(4));
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export declare const tileRows: (level: number) => number;
|
|
110
|
+
/**
|
|
111
|
+
* 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
|
|
112
|
+
* (`baseWidth` across) is already that sharp.
|
|
113
|
+
*
|
|
114
|
+
* @param radiusPx - The disc's radius in device pixels.
|
|
115
|
+
* @param layer - The levels the set has for this layer.
|
|
116
|
+
* @param baseWidth - The width of the picture the tiles are laid over, 4096 for the day map.
|
|
117
|
+
* @returns A level in `layer`, or `undefined` when no tile is worth loading.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* import { tileLevel, type TileLayer } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
122
|
+
*
|
|
123
|
+
* const day: TileLayer = { tileSize: 512, levels: [{ level: 3, width: 8192 }, { level: 4, width: 16384 }] };
|
|
124
|
+
* console.log(tileLevel(600, day, 4096), tileLevel(1500, day, 4096), tileLevel(9000, day, 4096));
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
export declare function tileLevel(radiusPx: number, layer: TileLayer, baseWidth: number): number | undefined;
|
|
128
|
+
/**
|
|
129
|
+
* The tile of a coarser or the same level that holds a tile.
|
|
130
|
+
*
|
|
131
|
+
* @param tile - A tile.
|
|
132
|
+
* @param level - A level no finer than the tile's.
|
|
133
|
+
* @returns The tile at `level` that covers it.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```ts
|
|
137
|
+
* import { coveringTile } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
138
|
+
*
|
|
139
|
+
* console.log(coveringTile({ level: 4, col: 17, row: 5 }, 3));
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
export declare function coveringTile(tile: TileId, level: number): TileId;
|
|
143
|
+
/**
|
|
144
|
+
* Where a tile's edges are, in longitude and latitude in radians: west, north, east and south.
|
|
145
|
+
*
|
|
146
|
+
* @param tile - A tile.
|
|
147
|
+
* @returns `[west, north, east, south]`.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```ts
|
|
151
|
+
* import { tileRect } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
152
|
+
*
|
|
153
|
+
* console.log(tileRect({ level: 3, col: 0, row: 0 }));
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
export declare function tileRect(tile: TileId): [number, number, number, number];
|
|
157
|
+
/**
|
|
158
|
+
* The longitude and latitude (degrees) under a point of the box, or `undefined` where the box shows the sky.
|
|
159
|
+
*
|
|
160
|
+
* @param camera - The camera.
|
|
161
|
+
* @param x - Across the box, CSS pixels from its left.
|
|
162
|
+
* @param y - Down the box, CSS pixels from its top.
|
|
163
|
+
* @returns The longitude and latitude in degrees under that point, or `undefined` where the box shows the sky.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```ts
|
|
167
|
+
* import { pointOnEarth } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
168
|
+
*
|
|
169
|
+
* console.log(pointOnEarth({ lon: 10, lat: 20, width: 800, height: 600, panX: 0, panY: 0, radius: 900 }, 400, 300));
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
export declare function pointOnEarth(camera: TileCamera, x: number, y: number): {
|
|
173
|
+
lon: number;
|
|
174
|
+
lat: number;
|
|
175
|
+
} | undefined;
|
|
176
|
+
/**
|
|
177
|
+
* 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
|
|
178
|
+
* 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.
|
|
179
|
+
*
|
|
180
|
+
* @param level - A pyramid level.
|
|
181
|
+
* @param camera - The camera.
|
|
182
|
+
* @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.
|
|
183
|
+
* @returns The tiles in view, the nearest to the middle of the box first.
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```ts
|
|
187
|
+
* import { tilesInView } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
188
|
+
*
|
|
189
|
+
* const tiles = tilesInView(4, { lon: 179.9, lat: 89, width: 800, height: 600, panX: 0, panY: 0, radius: 3000 });
|
|
190
|
+
* console.log(tiles.length, tiles[0]);
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
export declare function tilesInView(level: number, camera: TileCamera, sun?: readonly [number, number, number]): VisibleTile[];
|
|
194
|
+
/**
|
|
195
|
+
* 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`.
|
|
196
|
+
*
|
|
197
|
+
* @param json - The parsed JSON.
|
|
198
|
+
* @returns A layer name to its levels, and the tile size; or `undefined`.
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```ts
|
|
202
|
+
* import { parseManifest } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
203
|
+
*
|
|
204
|
+
* const manifest = parseManifest({ tileSize: 512, layers: { day: { levels: [{ level: 3, width: 8192, height: 4096, columns: 16, rows: 8 }] } } });
|
|
205
|
+
* console.log(manifest?.layers["day"]?.levels);
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
export declare function parseManifest(json: unknown): {
|
|
209
|
+
tileSize: number;
|
|
210
|
+
layers: Record<string, TileLayer>;
|
|
211
|
+
} | undefined;
|
|
@@ -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/dist/space/element.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type ViewController } from "@johnmorrisdotca/kazoku-ui/view-controls";
|
|
1
2
|
import type { GeoPoint } from "../types.ts";
|
|
2
3
|
import { setThreeLoader } from "../realistic/shared.ts";
|
|
3
4
|
import type { LightMode } from "../realistic/earth.ts";
|
|
@@ -117,6 +118,8 @@ type SatelliteStats = {
|
|
|
117
118
|
/**
|
|
118
119
|
* The `<chikyu-space>` custom element: the Earth, the Moon, the Sun and the planets where they are at a date and time, drawn with three.js through the shared renderer. It takes `center`, `time` (`now` or an ISO date), `speed` (1 is real time, 0 holds, 86400 is a day a second, negative runs backwards), `scale` (`compressed` or `true`), `labels` and `orbits` (`off` hides them); positions are computed in the browser by astronomy-engine, an optional peer dependency. It draws only when something changes, and nothing while it is out of sight.
|
|
119
120
|
*
|
|
121
|
+
* It carries kazoku-ui's controls itself: a drag turns the view, two fingers pinch, Ctrl and the wheel zoom (a plain wheel at Fit scrolls the page) and the arrow keys, + and - work once it is focused. The zoom runs from a millionth of Fit (the whole solar system) to 2.5 times, and from 0.5 to 6 times while riding from a body. `controls="none"` turns the hands off; `zoom-min` and `zoom-max` change the range. A `<kazoku-view-controls for="...">` on the element takes the hands over, and the page applies its events with `setView`.
|
|
122
|
+
*
|
|
120
123
|
* @example
|
|
121
124
|
* ```ts no-run
|
|
122
125
|
* import type { ChikyuSpace } from "@johnmorrisdotca/chikyu-earth/space/element";
|
|
@@ -130,6 +133,8 @@ type SatelliteStats = {
|
|
|
130
133
|
export declare class ChikyuSpace extends HTMLElement {
|
|
131
134
|
#private;
|
|
132
135
|
static observedAttributes: string[];
|
|
136
|
+
/** The controller behind the element's own hands (`set`, `fit`, `state`), while it is in a page. */
|
|
137
|
+
get controller(): ViewController | undefined;
|
|
133
138
|
connectedCallback(): void;
|
|
134
139
|
disconnectedCallback(): void;
|
|
135
140
|
attributeChangedCallback(name: string, _old: string | null, value: string | null): void;
|
package/dist/space/element.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { carryControls } from "@johnmorrisdotca/kazoku-ui/view-controls";
|
|
1
2
|
import { adoptStyles } from "../adopt.js";
|
|
2
3
|
import { createGate, frameCap } from "../realistic/gate.js";
|
|
3
4
|
import { setThreeLoader, takeRenderer } from "../realistic/shared.js";
|
|
@@ -38,6 +39,12 @@ const EARTH_RATE = 360 / 86_164.0905;
|
|
|
38
39
|
const SATELLITE_RATE = 360 / (88 * 60);
|
|
39
40
|
const SATELLITE_COLORS = ["#ffd54f", "#7fd4ff", "#ff8fa3", "#9ef0a8"];
|
|
40
41
|
const REAL_TIME_TICK_MS = 60_000;
|
|
42
|
+
/**
|
|
43
|
+
* Tilt to the poles. The zoom is the camera's distance: a millionth of Fit takes in the whole solar system (the views of the planets and their distances need that much), and 2.5 times is as close as the Earth's 4096 pixel picture and the body textures stay sharp.
|
|
44
|
+
* Riding at a body looking at the Earth, the zoom only narrows the camera's field a little either way: 0.5 to 6.
|
|
45
|
+
*/
|
|
46
|
+
const LIMITS = { tiltMax: 90, zoomMin: 0.000001, zoomMax: 2.5 };
|
|
47
|
+
const RIDE_ZOOM = { "zoom-min": "0.5", "zoom-max": "6" };
|
|
41
48
|
const readCentre = (value) => {
|
|
42
49
|
if (!value)
|
|
43
50
|
return null;
|
|
@@ -56,6 +63,8 @@ const STYLE = `:host{display:block;aspect-ratio:1/1;position:relative;overflow:h
|
|
|
56
63
|
/**
|
|
57
64
|
* The `<chikyu-space>` custom element: the Earth, the Moon, the Sun and the planets where they are at a date and time, drawn with three.js through the shared renderer. It takes `center`, `time` (`now` or an ISO date), `speed` (1 is real time, 0 holds, 86400 is a day a second, negative runs backwards), `scale` (`compressed` or `true`), `labels` and `orbits` (`off` hides them); positions are computed in the browser by astronomy-engine, an optional peer dependency. It draws only when something changes, and nothing while it is out of sight.
|
|
58
65
|
*
|
|
66
|
+
* It carries kazoku-ui's controls itself: a drag turns the view, two fingers pinch, Ctrl and the wheel zoom (a plain wheel at Fit scrolls the page) and the arrow keys, + and - work once it is focused. The zoom runs from a millionth of Fit (the whole solar system) to 2.5 times, and from 0.5 to 6 times while riding from a body. `controls="none"` turns the hands off; `zoom-min` and `zoom-max` change the range. A `<kazoku-view-controls for="...">` on the element takes the hands over, and the page applies its events with `setView`.
|
|
67
|
+
*
|
|
59
68
|
* @example
|
|
60
69
|
* ```ts no-run
|
|
61
70
|
* import type { ChikyuSpace } from "@johnmorrisdotca/chikyu-earth/space/element";
|
|
@@ -87,6 +96,12 @@ export class ChikyuSpace extends HTMLElement {
|
|
|
87
96
|
#names = {};
|
|
88
97
|
#hint = "choose to look at it";
|
|
89
98
|
#tiers = {};
|
|
99
|
+
#hands;
|
|
100
|
+
#ridden = {};
|
|
101
|
+
/** The controller behind the element's own hands (`set`, `fit`, `state`), while it is in a page. */
|
|
102
|
+
get controller() {
|
|
103
|
+
return this.#hands?.controller;
|
|
104
|
+
}
|
|
90
105
|
connectedCallback() {
|
|
91
106
|
this.#model.centre = readCentre(this.getAttribute("center")) ?? this.#model.centre;
|
|
92
107
|
this.#mode = readTime(this.getAttribute("time"));
|
|
@@ -148,10 +163,14 @@ export class ChikyuSpace extends HTMLElement {
|
|
|
148
163
|
this.#last = performance.now();
|
|
149
164
|
this.#schedule();
|
|
150
165
|
this.addEventListener("pointerdown", this.#down);
|
|
166
|
+
this.#hands = carryControls(this, { initial: this.#model.view, limits: LIMITS, onChange: (state, info) => this.setView(state, info.final) });
|
|
151
167
|
this.#labelBox.addEventListener("click", this.#labelClick);
|
|
152
168
|
this.#labelBox.addEventListener("focusin", this.#labelFocus);
|
|
153
169
|
}
|
|
154
170
|
disconnectedCallback() {
|
|
171
|
+
this.#hands?.unbind();
|
|
172
|
+
this.#hands = undefined;
|
|
173
|
+
this.#zoomRange(false);
|
|
155
174
|
this.removeEventListener("pointerdown", this.#down);
|
|
156
175
|
window.removeEventListener("pointerup", this.#up, true);
|
|
157
176
|
clearTimeout(this.#timer);
|
|
@@ -320,6 +339,7 @@ export class ChikyuSpace extends HTMLElement {
|
|
|
320
339
|
else if (wasRiding)
|
|
321
340
|
model.focus = this.#focusBeforeRide;
|
|
322
341
|
}
|
|
342
|
+
this.#zoomRange(model.ride?.view === "from");
|
|
323
343
|
this.#lostTold = false;
|
|
324
344
|
const pose = model.pose;
|
|
325
345
|
model.transition = (options.smooth ?? true) && !reduced && pose.valid ? { start: performance.now(), duration: 1200, from: { p: [pose.p[0], pose.p[1], pose.p[2]], q: [pose.q[0], pose.q[1], pose.q[2], pose.q[3]], fov: pose.fov } } : null;
|
|
@@ -328,6 +348,23 @@ export class ChikyuSpace extends HTMLElement {
|
|
|
328
348
|
this.#gate?.request();
|
|
329
349
|
this.dispatchEvent(new CustomEvent("chikyu-ride", { detail: this.riding }));
|
|
330
350
|
}
|
|
351
|
+
/** From a body the zoom only narrows the field a little, so the range is 0.5 to 6: the `zoom-min` and `zoom-max` attributes, unless the page set them itself. */
|
|
352
|
+
#zoomRange(from) {
|
|
353
|
+
for (const name of ["zoom-min", "zoom-max"]) {
|
|
354
|
+
const mine = this.#ridden[name];
|
|
355
|
+
if (this.hasAttribute(name) && this.getAttribute(name) !== mine)
|
|
356
|
+
continue;
|
|
357
|
+
if (from) {
|
|
358
|
+
this.#ridden[name] = RIDE_ZOOM[name];
|
|
359
|
+
this.setAttribute(name, RIDE_ZOOM[name]);
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
if (mine !== undefined)
|
|
363
|
+
this.removeAttribute(name);
|
|
364
|
+
delete this.#ridden[name];
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
331
368
|
/** What the camera is riding along with now, or null. */
|
|
332
369
|
get riding() {
|
|
333
370
|
const ride = this.#model.ride;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johnmorrisdotca/chikyu-earth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.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",
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"starlink",
|
|
40
40
|
"celestrak",
|
|
41
41
|
"spaceprogramx",
|
|
42
|
+
"space-program-x",
|
|
42
43
|
"spx",
|
|
43
44
|
"space",
|
|
44
45
|
"satellite",
|
|
@@ -148,6 +149,8 @@
|
|
|
148
149
|
"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
150
|
"screenshots": "pnpm site && node scripts/screenshots.ts",
|
|
150
151
|
"textures": "python3 scripts/make-textures.py",
|
|
152
|
+
"tiles": "python3 -I scripts/make-earth-tiles.py build",
|
|
153
|
+
"test:tiles": "python3 -I scripts/make-earth-tiles.py selftest",
|
|
151
154
|
"test:demo:one": "pnpm site && node scripts/docs-site.mjs build && playwright test",
|
|
152
155
|
"test:soak": "pnpm site && playwright test --project=soak",
|
|
153
156
|
"docs:site": "pnpm build && node scripts/docs-site.mjs build",
|
|
@@ -160,19 +163,16 @@
|
|
|
160
163
|
},
|
|
161
164
|
"dependencies": {
|
|
162
165
|
"@johnmorrisdotca/chizu": "^1.6.0",
|
|
166
|
+
"@johnmorrisdotca/kazoku-ui": "^0.21.1",
|
|
163
167
|
"d3-geo": "^3.1.1"
|
|
164
168
|
},
|
|
165
169
|
"peerDependencies": {
|
|
166
|
-
"@johnmorrisdotca/kazoku-ui": ">=0.9.0",
|
|
167
170
|
"three": ">=0.160.0",
|
|
168
171
|
"astronomy-engine": ">=2.1.0",
|
|
169
172
|
"satellite.js": ">=6.0.0",
|
|
170
173
|
"@johnmorrisdotca/wakusei-planets": ">=0.5.0"
|
|
171
174
|
},
|
|
172
175
|
"peerDependenciesMeta": {
|
|
173
|
-
"@johnmorrisdotca/kazoku-ui": {
|
|
174
|
-
"optional": true
|
|
175
|
-
},
|
|
176
176
|
"three": {
|
|
177
177
|
"optional": true
|
|
178
178
|
},
|
|
@@ -192,7 +192,6 @@
|
|
|
192
192
|
"@angular/core": "^22.2.4",
|
|
193
193
|
"@angular/platform-browser": "^22.2.4",
|
|
194
194
|
"@eslint/js": "^9.0.0",
|
|
195
|
-
"@johnmorrisdotca/kazoku-ui": "^0.20.0",
|
|
196
195
|
"@johnmorrisdotca/wakusei-planets": "^0.5.0",
|
|
197
196
|
"@playwright/test": "1.64.0",
|
|
198
197
|
"@sveltejs/vite-plugin-svelte": "^7.3.1",
|