@johnmorrisdotca/wakusei-planets 0.45.0 → 0.46.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 +11 -0
- package/NOTICE.md +11 -0
- package/README.md +4 -3
- package/dist/scene/textures.d.ts +2 -2
- package/dist/space/element.d.ts +7 -0
- package/dist/space/element.js +12 -0
- package/dist/surface/detail.d.ts +169 -0
- package/dist/surface/detail.js +292 -0
- package/dist/surface/element.d.ts +90 -0
- package/dist/surface/element.js +482 -0
- package/dist/surface/glsl.d.ts +20 -0
- package/dist/surface/glsl.js +403 -0
- package/dist/surface/height.d.ts +257 -0
- package/dist/surface/height.js +232 -0
- package/dist/surface/index.d.ts +8 -0
- package/dist/surface/index.js +8 -0
- package/dist/surface/land.d.ts +111 -0
- package/dist/surface/land.js +261 -0
- package/dist/surface/load.d.ts +133 -0
- package/dist/surface/load.js +183 -0
- package/dist/surface/rocks.d.ts +45 -0
- package/dist/surface/rocks.js +166 -0
- package/dist/surface/scene.d.ts +64 -0
- package/dist/surface/scene.js +323 -0
- package/dist/surface/site.d.ts +58 -0
- package/dist/surface/site.js +38 -0
- package/dist/surface/sky.d.ts +81 -0
- package/dist/surface/sky.js +60 -0
- package/dist/surface/view.d.ts +129 -0
- package/dist/surface/view.js +139 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +23 -1
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One level of a site's pyramid, as the manifest says it.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import { type HeightLevelInfo } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
7
|
+
*
|
|
8
|
+
* const info: HeightLevelInfo = { id: "h5", res: 5, tiles: 4, half: 512 };
|
|
9
|
+
* console.log(`${info.id}: ${info.tiles} x ${info.tiles} tiles, ${info.res} m apart, reaching ${info.half} m each way`);
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export interface HeightLevelInfo {
|
|
13
|
+
/** The level's name: `h5`, `h20`, `h80`, `far`. */
|
|
14
|
+
id: string;
|
|
15
|
+
/** Metres between two samples. */
|
|
16
|
+
res: number;
|
|
17
|
+
/** Tiles across (and down). */
|
|
18
|
+
tiles: number;
|
|
19
|
+
/** Half the level's width in metres: the square reaches this far east, west, north and south of the site. */
|
|
20
|
+
half: number;
|
|
21
|
+
/** The range the level's heights are encoded over, where it is not the manifest's (the coarse level reaches over mountains and basins the site's range cannot hold). */
|
|
22
|
+
height?: {
|
|
23
|
+
min: number;
|
|
24
|
+
range: number;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* What a site's `manifest.json` holds.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* import { type TerrainManifest } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
33
|
+
*
|
|
34
|
+
* const manifest: TerrainManifest = {
|
|
35
|
+
* version: 1, body: "moon", site: "apollo-17", name: { en: "Apollo 17", ja: "アポロ17号" },
|
|
36
|
+
* lat: 20.1911, lon: 30.7723, radius: 1_737_400, tile: 256, height: { min: -2000, range: 4000 }, ground: -1930,
|
|
37
|
+
* levels: [{ id: "h5", res: 5, tiles: 4, half: 512 }], credit: "made for this example", accuracy: "to the metre",
|
|
38
|
+
* };
|
|
39
|
+
* console.log(`${manifest.name.en}: ${manifest.levels.length} level, ${manifest.height.range} m of range`);
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export interface TerrainManifest {
|
|
43
|
+
version: 1;
|
|
44
|
+
body: "moon";
|
|
45
|
+
site: string;
|
|
46
|
+
name: {
|
|
47
|
+
en: string;
|
|
48
|
+
ja: string;
|
|
49
|
+
};
|
|
50
|
+
/** Where the viewer stands: planetocentric degrees. */
|
|
51
|
+
lat: number;
|
|
52
|
+
lon: number;
|
|
53
|
+
/** The body's radius in metres, the sphere the heights are measured from. */
|
|
54
|
+
radius: number;
|
|
55
|
+
/** Samples across a tile. */
|
|
56
|
+
tile: number;
|
|
57
|
+
/** A height is `min + value / 65535 * range`, in metres from the sphere. */
|
|
58
|
+
height: {
|
|
59
|
+
min: number;
|
|
60
|
+
range: number;
|
|
61
|
+
};
|
|
62
|
+
/** The height of the ground at the site, in metres. */
|
|
63
|
+
ground: number;
|
|
64
|
+
levels: readonly HeightLevelInfo[];
|
|
65
|
+
/** The ground picture, where the site has one: levels on the same grids as the first of the height levels, its credit and the Sun the mosaic was lit by (fitted to the heights; divided out). */
|
|
66
|
+
picture?: {
|
|
67
|
+
levels: readonly HeightLevelInfo[];
|
|
68
|
+
credit: string;
|
|
69
|
+
sun: {
|
|
70
|
+
azimuth: number;
|
|
71
|
+
elevation: number;
|
|
72
|
+
correlation: number;
|
|
73
|
+
};
|
|
74
|
+
} | null;
|
|
75
|
+
/** The measured ground's shadows, baked: for each of the first three levels a picture of `cells` x `cells` cells by `sectors / 3` slabs side by side, three directions to a slab (red, green, blue), the elevation angle of the horizon in each, 8 bits over `from` to `to` degrees. */
|
|
76
|
+
horizon?: {
|
|
77
|
+
cells: number;
|
|
78
|
+
sectors: number;
|
|
79
|
+
from: number;
|
|
80
|
+
to: number;
|
|
81
|
+
levels: readonly {
|
|
82
|
+
id: string;
|
|
83
|
+
file: string;
|
|
84
|
+
}[];
|
|
85
|
+
} | null;
|
|
86
|
+
/** The tiles' file type: `png` where it is left out (the made-up sites of the tests), `webp` (lossless, bit for bit the same heights in a third less) for the real ones. */
|
|
87
|
+
format?: "png" | "webp";
|
|
88
|
+
/** The credit the page says beside the picture. */
|
|
89
|
+
credit: string;
|
|
90
|
+
/** How well the heights are known, in words, for the line the page shows. */
|
|
91
|
+
accuracy: string;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* The bytes of one level: `size` x `size` samples of four bytes (red high, green low), row 0 at the north edge.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* import { type HeightLevel } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
99
|
+
*
|
|
100
|
+
* const level: HeightLevel = { id: "h5", res: 5, tiles: 1, half: 10, size: 4, data: new Uint8Array(4 * 4 * 4) };
|
|
101
|
+
* console.log(`${level.size} x ${level.size} samples, ${level.data.length} bytes`);
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
export interface HeightLevel extends HeightLevelInfo {
|
|
105
|
+
size: number;
|
|
106
|
+
data: Uint8Array;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The address of a tile: `<base><site>/terrain/<level>/<column>/<row>.webp` (`.png` where the manifest has no `format`), with `base` ending in a slash and the site's body in front (`moon/apollo-17`).
|
|
110
|
+
*
|
|
111
|
+
* @param base - The folder the tiles are served from.
|
|
112
|
+
* @param manifest - The site's manifest.
|
|
113
|
+
* @param level - A level's name.
|
|
114
|
+
* @param column - From the west edge, 0 first.
|
|
115
|
+
* @param row - From the north edge, 0 first.
|
|
116
|
+
* @returns The address.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* import { tileAddress, type TerrainManifest } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
121
|
+
*
|
|
122
|
+
* const manifest = { body: "moon", site: "apollo-17" } as TerrainManifest;
|
|
123
|
+
* console.log(tileAddress("tiles/", manifest, "h5", 3, 4));
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
export declare function tileAddress(base: string, manifest: Pick<TerrainManifest, "body" | "site" | "format">, level: string, column: number, row: number): string;
|
|
127
|
+
/**
|
|
128
|
+
* An empty level, ready to be filled tile by tile: every height the lowest the encoding holds until a tile says otherwise.
|
|
129
|
+
*
|
|
130
|
+
* @param info - The level as the manifest says it.
|
|
131
|
+
* @param tile - Samples across a tile.
|
|
132
|
+
* @returns The level with its bytes all zero.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* import { emptyLevel } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
137
|
+
*
|
|
138
|
+
* const level = emptyLevel({ id: "h5", res: 5, tiles: 2, half: 20 }, 4);
|
|
139
|
+
* console.log(`${level.size} x ${level.size} samples, ${level.data.length} bytes`);
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
export declare function emptyLevel(info: HeightLevelInfo, tile: number): HeightLevel;
|
|
143
|
+
/**
|
|
144
|
+
* Put one decoded tile (RGBA, `tile` x `tile`) into its place in a level.
|
|
145
|
+
*
|
|
146
|
+
* @param level - The level being filled.
|
|
147
|
+
* @param tile - Samples across a tile.
|
|
148
|
+
* @param column - The tile's column from the west edge.
|
|
149
|
+
* @param row - The tile's row from the north edge.
|
|
150
|
+
* @param rgba - The tile's bytes, row by row.
|
|
151
|
+
*/
|
|
152
|
+
export declare function placeTile(level: HeightLevel, tile: number, column: number, row: number, rgba: Uint8Array | Uint8ClampedArray): void;
|
|
153
|
+
/**
|
|
154
|
+
* A height in metres from the two bytes the tiles hold.
|
|
155
|
+
*
|
|
156
|
+
* @param high - The high byte, from the red of a pixel.
|
|
157
|
+
* @param low - The low byte, from the green of a pixel.
|
|
158
|
+
* @param height - The manifest's height encoding.
|
|
159
|
+
* @returns The height in metres from the sphere.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* import { decodeHeight } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
164
|
+
*
|
|
165
|
+
* console.log(decodeHeight(128, 0, { min: -2000, range: 4000 }).toFixed(1));
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
export declare const decodeHeight: (high: number, low: number, height: TerrainManifest["height"]) => number;
|
|
169
|
+
/**
|
|
170
|
+
* One level's height at a point in metres east and north of the site, bilinear, or `undefined` where the point is outside the level.
|
|
171
|
+
*
|
|
172
|
+
* @param level - The level to read.
|
|
173
|
+
* @param height - The manifest's height encoding (the level's own, where it has one, is used instead).
|
|
174
|
+
* @param x - Metres east of the site.
|
|
175
|
+
* @param y - Metres north of the site.
|
|
176
|
+
* @returns The height in metres from the sphere, or `undefined` outside the level.
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```ts
|
|
180
|
+
* import { sampleLevel } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
181
|
+
*
|
|
182
|
+
* const height = { min: -2000, range: 4000 };
|
|
183
|
+
* const level = { id: "h5", res: 5, tiles: 1, half: 10, size: 4, data: new Uint8Array(4 * 4 * 4) };
|
|
184
|
+
* for (let i = 0; i < 16; i += 1) level.data[i * 4] = 128; // every sample 128 * 256 / 65535 of the range up
|
|
185
|
+
*
|
|
186
|
+
* console.log(sampleLevel(level, height, 0, 0)?.toFixed(2), sampleLevel(level, height, 100, 0));
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
export declare function sampleLevel(level: HeightLevel, height: TerrainManifest["height"], x: number, y: number): number | undefined;
|
|
190
|
+
/**
|
|
191
|
+
* The ground's height at a point, from the finest level that holds it.
|
|
192
|
+
*
|
|
193
|
+
* @param levels - The levels, finest first.
|
|
194
|
+
* @param height - The manifest's height encoding.
|
|
195
|
+
* @param x - Metres east of the site.
|
|
196
|
+
* @param y - Metres north of the site.
|
|
197
|
+
* @returns The height in metres from the sphere, or `undefined` outside every level.
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```ts
|
|
201
|
+
* import { groundAt } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
202
|
+
*
|
|
203
|
+
* const height = { min: -2000, range: 4000 };
|
|
204
|
+
* const level = { id: "h5", res: 5, tiles: 1, half: 10, size: 4, data: new Uint8Array(4 * 4 * 4) };
|
|
205
|
+
* for (let i = 0; i < 16; i += 1) level.data[i * 4] = 128;
|
|
206
|
+
*
|
|
207
|
+
* console.log(groundAt([level], height, 0, 0)?.toFixed(2), groundAt([level], height, 100, 0));
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
export declare function groundAt(levels: readonly HeightLevel[], height: TerrainManifest["height"], x: number, y: number): number | undefined;
|
|
211
|
+
/**
|
|
212
|
+
* How far the ground at `distance` metres falls away from a plane tangent at the viewer's feet: the Moon is round.
|
|
213
|
+
*
|
|
214
|
+
* @param distance - Metres from the viewer.
|
|
215
|
+
* @param radius - The body's radius in metres.
|
|
216
|
+
* @returns The fall in metres.
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```ts
|
|
220
|
+
* import { drop } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
221
|
+
*
|
|
222
|
+
* console.log(drop(1000, 1_737_400).toFixed(3));
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
export declare const drop: (distance: number, radius: number) => number;
|
|
226
|
+
/**
|
|
227
|
+
* Fill the tiles of a level that have not arrived yet from a coarser level, so the ground is whole at once and gets finer as the real tiles come: heights are resampled (bilinear) from the coarser
|
|
228
|
+
* level's samples and written in this level's own encoding; a picture is resampled from its grey. The tiles named by `skip` (those that have arrived) are left alone.
|
|
229
|
+
*
|
|
230
|
+
* @param level - The finer level, filled in place.
|
|
231
|
+
* @param coarse - A coarser level, centred on the same site and holding every point of `level`.
|
|
232
|
+
* @param tile - Samples across a tile.
|
|
233
|
+
* @param skip - True for a tile that is already real: its column and row.
|
|
234
|
+
* @param encoding - For heights, the encodings (the manifest's height or a level's own) of the coarse level and of this one; for a picture, `"grey"`.
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```ts
|
|
238
|
+
* import { emptyLevel, fillFromCoarser } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
239
|
+
*
|
|
240
|
+
* const encoding = { min: 0, range: 1000 };
|
|
241
|
+
* const coarse = emptyLevel({ id: "h20", res: 20, tiles: 1, half: 40 }, 4);
|
|
242
|
+
* for (let i = 0; i < 16; i += 1) coarse.data[i * 4] = 128; // every sample about half way up the range
|
|
243
|
+
* const fine = emptyLevel({ id: "h5", res: 5, tiles: 1, half: 10 }, 4);
|
|
244
|
+
* fillFromCoarser(fine, coarse, 4, () => false, { from: encoding, to: encoding });
|
|
245
|
+
* console.log(fine.data[0], fine.data[3]);
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
export declare function fillFromCoarser(level: HeightLevel, coarse: HeightLevel, tile: number, skip: (column: number, row: number) => boolean, encoding: {
|
|
249
|
+
from: {
|
|
250
|
+
min: number;
|
|
251
|
+
range: number;
|
|
252
|
+
};
|
|
253
|
+
to: {
|
|
254
|
+
min: number;
|
|
255
|
+
range: number;
|
|
256
|
+
};
|
|
257
|
+
} | "grey"): void;
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
// The Moon's measured heights at a site, as the tile pyramid `scripts/make-moon-terrain.py` makes: square levels centred on the landing point, each a grid of 16 bit heights, the finest
|
|
2
|
+
// first. Pure: no DOM; the decoding of a picture into bytes is the loader's (`load.ts`).
|
|
3
|
+
/**
|
|
4
|
+
* The address of a tile: `<base><site>/terrain/<level>/<column>/<row>.webp` (`.png` where the manifest has no `format`), with `base` ending in a slash and the site's body in front (`moon/apollo-17`).
|
|
5
|
+
*
|
|
6
|
+
* @param base - The folder the tiles are served from.
|
|
7
|
+
* @param manifest - The site's manifest.
|
|
8
|
+
* @param level - A level's name.
|
|
9
|
+
* @param column - From the west edge, 0 first.
|
|
10
|
+
* @param row - From the north edge, 0 first.
|
|
11
|
+
* @returns The address.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { tileAddress, type TerrainManifest } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
16
|
+
*
|
|
17
|
+
* const manifest = { body: "moon", site: "apollo-17" } as TerrainManifest;
|
|
18
|
+
* console.log(tileAddress("tiles/", manifest, "h5", 3, 4));
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export function tileAddress(base, manifest, level, column, row) {
|
|
22
|
+
return `${base.endsWith("/") ? base : `${base}/`}${manifest.body}/${manifest.site}/terrain/${level}/${column}/${row}.${manifest.format ?? "png"}`;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* An empty level, ready to be filled tile by tile: every height the lowest the encoding holds until a tile says otherwise.
|
|
26
|
+
*
|
|
27
|
+
* @param info - The level as the manifest says it.
|
|
28
|
+
* @param tile - Samples across a tile.
|
|
29
|
+
* @returns The level with its bytes all zero.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { emptyLevel } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
34
|
+
*
|
|
35
|
+
* const level = emptyLevel({ id: "h5", res: 5, tiles: 2, half: 20 }, 4);
|
|
36
|
+
* console.log(`${level.size} x ${level.size} samples, ${level.data.length} bytes`);
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export function emptyLevel(info, tile) {
|
|
40
|
+
const size = info.tiles * tile;
|
|
41
|
+
return { ...info, size, data: new Uint8Array(size * size * 4) };
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Put one decoded tile (RGBA, `tile` x `tile`) into its place in a level.
|
|
45
|
+
*
|
|
46
|
+
* @param level - The level being filled.
|
|
47
|
+
* @param tile - Samples across a tile.
|
|
48
|
+
* @param column - The tile's column from the west edge.
|
|
49
|
+
* @param row - The tile's row from the north edge.
|
|
50
|
+
* @param rgba - The tile's bytes, row by row.
|
|
51
|
+
*/
|
|
52
|
+
export function placeTile(level, tile, column, row, rgba) {
|
|
53
|
+
for (let y = 0; y < tile; y += 1) {
|
|
54
|
+
const from = y * tile * 4;
|
|
55
|
+
level.data.set(rgba.subarray(from, from + tile * 4), ((row * tile + y) * level.size + column * tile) * 4);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* A height in metres from the two bytes the tiles hold.
|
|
60
|
+
*
|
|
61
|
+
* @param high - The high byte, from the red of a pixel.
|
|
62
|
+
* @param low - The low byte, from the green of a pixel.
|
|
63
|
+
* @param height - The manifest's height encoding.
|
|
64
|
+
* @returns The height in metres from the sphere.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* import { decodeHeight } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
69
|
+
*
|
|
70
|
+
* console.log(decodeHeight(128, 0, { min: -2000, range: 4000 }).toFixed(1));
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export const decodeHeight = (high, low, height) => height.min + ((high * 256 + low) / 65535) * height.range;
|
|
74
|
+
/**
|
|
75
|
+
* One level's height at a point in metres east and north of the site, bilinear, or `undefined` where the point is outside the level.
|
|
76
|
+
*
|
|
77
|
+
* @param level - The level to read.
|
|
78
|
+
* @param height - The manifest's height encoding (the level's own, where it has one, is used instead).
|
|
79
|
+
* @param x - Metres east of the site.
|
|
80
|
+
* @param y - Metres north of the site.
|
|
81
|
+
* @returns The height in metres from the sphere, or `undefined` outside the level.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* import { sampleLevel } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
86
|
+
*
|
|
87
|
+
* const height = { min: -2000, range: 4000 };
|
|
88
|
+
* const level = { id: "h5", res: 5, tiles: 1, half: 10, size: 4, data: new Uint8Array(4 * 4 * 4) };
|
|
89
|
+
* for (let i = 0; i < 16; i += 1) level.data[i * 4] = 128; // every sample 128 * 256 / 65535 of the range up
|
|
90
|
+
*
|
|
91
|
+
* console.log(sampleLevel(level, height, 0, 0)?.toFixed(2), sampleLevel(level, height, 100, 0));
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
export function sampleLevel(level, height, x, y) {
|
|
95
|
+
const fx = (x + level.half) / level.res - 0.5;
|
|
96
|
+
const fy = (level.half - y) / level.res - 0.5;
|
|
97
|
+
if (fx < 0 || fy < 0 || fx > level.size - 1.001 || fy > level.size - 1.001)
|
|
98
|
+
return undefined;
|
|
99
|
+
const x0 = Math.floor(fx);
|
|
100
|
+
const y0 = Math.floor(fy);
|
|
101
|
+
const tx = fx - x0;
|
|
102
|
+
const ty = fy - y0;
|
|
103
|
+
const at = (ix, iy) => {
|
|
104
|
+
const i = (iy * level.size + ix) * 4;
|
|
105
|
+
return decodeHeight(level.data[i], level.data[i + 1], level.height ?? height);
|
|
106
|
+
};
|
|
107
|
+
const top = at(x0, y0) * (1 - tx) + at(x0 + 1, y0) * tx;
|
|
108
|
+
const bottom = at(x0, y0 + 1) * (1 - tx) + at(x0 + 1, y0 + 1) * tx;
|
|
109
|
+
return top * (1 - ty) + bottom * ty;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The ground's height at a point, from the finest level that holds it.
|
|
113
|
+
*
|
|
114
|
+
* @param levels - The levels, finest first.
|
|
115
|
+
* @param height - The manifest's height encoding.
|
|
116
|
+
* @param x - Metres east of the site.
|
|
117
|
+
* @param y - Metres north of the site.
|
|
118
|
+
* @returns The height in metres from the sphere, or `undefined` outside every level.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```ts
|
|
122
|
+
* import { groundAt } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
123
|
+
*
|
|
124
|
+
* const height = { min: -2000, range: 4000 };
|
|
125
|
+
* const level = { id: "h5", res: 5, tiles: 1, half: 10, size: 4, data: new Uint8Array(4 * 4 * 4) };
|
|
126
|
+
* for (let i = 0; i < 16; i += 1) level.data[i * 4] = 128;
|
|
127
|
+
*
|
|
128
|
+
* console.log(groundAt([level], height, 0, 0)?.toFixed(2), groundAt([level], height, 100, 0));
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
export function groundAt(levels, height, x, y) {
|
|
132
|
+
for (const level of levels) {
|
|
133
|
+
const h = sampleLevel(level, height, x, y);
|
|
134
|
+
if (h !== undefined)
|
|
135
|
+
return h;
|
|
136
|
+
}
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* How far the ground at `distance` metres falls away from a plane tangent at the viewer's feet: the Moon is round.
|
|
141
|
+
*
|
|
142
|
+
* @param distance - Metres from the viewer.
|
|
143
|
+
* @param radius - The body's radius in metres.
|
|
144
|
+
* @returns The fall in metres.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* import { drop } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
149
|
+
*
|
|
150
|
+
* console.log(drop(1000, 1_737_400).toFixed(3));
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
export const drop = (distance, radius) => (distance * distance) / (2 * radius);
|
|
154
|
+
/**
|
|
155
|
+
* Fill the tiles of a level that have not arrived yet from a coarser level, so the ground is whole at once and gets finer as the real tiles come: heights are resampled (bilinear) from the coarser
|
|
156
|
+
* level's samples and written in this level's own encoding; a picture is resampled from its grey. The tiles named by `skip` (those that have arrived) are left alone.
|
|
157
|
+
*
|
|
158
|
+
* @param level - The finer level, filled in place.
|
|
159
|
+
* @param coarse - A coarser level, centred on the same site and holding every point of `level`.
|
|
160
|
+
* @param tile - Samples across a tile.
|
|
161
|
+
* @param skip - True for a tile that is already real: its column and row.
|
|
162
|
+
* @param encoding - For heights, the encodings (the manifest's height or a level's own) of the coarse level and of this one; for a picture, `"grey"`.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```ts
|
|
166
|
+
* import { emptyLevel, fillFromCoarser } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
167
|
+
*
|
|
168
|
+
* const encoding = { min: 0, range: 1000 };
|
|
169
|
+
* const coarse = emptyLevel({ id: "h20", res: 20, tiles: 1, half: 40 }, 4);
|
|
170
|
+
* for (let i = 0; i < 16; i += 1) coarse.data[i * 4] = 128; // every sample about half way up the range
|
|
171
|
+
* const fine = emptyLevel({ id: "h5", res: 5, tiles: 1, half: 10 }, 4);
|
|
172
|
+
* fillFromCoarser(fine, coarse, 4, () => false, { from: encoding, to: encoding });
|
|
173
|
+
* console.log(fine.data[0], fine.data[3]);
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
export function fillFromCoarser(level, coarse, tile, skip, encoding) {
|
|
177
|
+
const n = coarse.size;
|
|
178
|
+
const colCell = new Int32Array(level.size);
|
|
179
|
+
const colFrac = new Float32Array(level.size);
|
|
180
|
+
const rowCell = new Int32Array(level.size);
|
|
181
|
+
const rowFrac = new Float32Array(level.size);
|
|
182
|
+
for (let i = 0; i < level.size; i += 1) {
|
|
183
|
+
const x = (i + 0.5) * level.res - level.half;
|
|
184
|
+
const fx = Math.min(Math.max((x + coarse.half) / coarse.res - 0.5, 0), n - 1.001);
|
|
185
|
+
colCell[i] = Math.floor(fx);
|
|
186
|
+
colFrac[i] = fx - colCell[i];
|
|
187
|
+
const fy = Math.min(Math.max((coarse.half - (level.half - (i + 0.5) * level.res)) / coarse.res - 0.5, 0), n - 1.001);
|
|
188
|
+
rowCell[i] = Math.floor(fy);
|
|
189
|
+
rowFrac[i] = fy - rowCell[i];
|
|
190
|
+
}
|
|
191
|
+
const grey = encoding === "grey";
|
|
192
|
+
const fromMin = grey ? 0 : encoding.from.min;
|
|
193
|
+
const fromSpan = grey ? 0 : encoding.from.range / 65535;
|
|
194
|
+
const toMin = grey ? 0 : encoding.to.min;
|
|
195
|
+
const toScale = grey ? 0 : 65535 / encoding.to.range;
|
|
196
|
+
const src = coarse.data;
|
|
197
|
+
const value = (ix, iy) => {
|
|
198
|
+
const i = (iy * n + ix) * 4;
|
|
199
|
+
return grey ? src[i] : fromMin + (src[i] * 256 + src[i + 1]) * fromSpan;
|
|
200
|
+
};
|
|
201
|
+
for (let row = 0; row < level.tiles; row += 1) {
|
|
202
|
+
for (let column = 0; column < level.tiles; column += 1) {
|
|
203
|
+
if (skip(column, row))
|
|
204
|
+
continue;
|
|
205
|
+
for (let y = row * tile; y < (row + 1) * tile; y += 1) {
|
|
206
|
+
const y0 = rowCell[y];
|
|
207
|
+
const ty = rowFrac[y];
|
|
208
|
+
for (let x = column * tile; x < (column + 1) * tile; x += 1) {
|
|
209
|
+
const x0 = colCell[x];
|
|
210
|
+
const tx = colFrac[x];
|
|
211
|
+
const top = value(x0, y0) * (1 - tx) + value(x0 + 1, y0) * tx;
|
|
212
|
+
const bottom = value(x0, y0 + 1) * (1 - tx) + value(x0 + 1, y0 + 1) * tx;
|
|
213
|
+
const v = top * (1 - ty) + bottom * ty;
|
|
214
|
+
const o = (y * level.size + x) * 4;
|
|
215
|
+
if (grey) {
|
|
216
|
+
const g = Math.round(v);
|
|
217
|
+
level.data[o] = g;
|
|
218
|
+
level.data[o + 1] = g;
|
|
219
|
+
level.data[o + 2] = g;
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
const code = Math.min(65535, Math.max(0, Math.round((v - toMin) * toScale)));
|
|
223
|
+
level.data[o] = code >> 8;
|
|
224
|
+
level.data[o + 1] = code & 255;
|
|
225
|
+
level.data[o + 2] = 0;
|
|
226
|
+
}
|
|
227
|
+
level.data[o + 3] = 255;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { moonSky, localFrame, type MoonSky, type SkyDirection } from "./sky.ts";
|
|
2
|
+
export { SURFACE_SITES, siteById, type SurfaceSite } from "./site.ts";
|
|
3
|
+
export { EYE_HEIGHT, FOV_LIMITS, WALK_SPEED, clampLook, forward, stepOver, viewProjection, type Look } from "./view.ts";
|
|
4
|
+
export { decodeHeight, drop, emptyLevel, fillFromCoarser, groundAt, sampleLevel, tileAddress, type HeightLevel, type HeightLevelInfo, type TerrainManifest } from "./height.ts";
|
|
5
|
+
export { loadPlan, loadTerrain, type LoadJob, type LoadOptions, type Terrain, type TerrainProgress } from "./load.ts";
|
|
6
|
+
export type { SurfaceState } from "./element.ts";
|
|
7
|
+
export { craterIn, craterProfile, madeGround, rimBoost, rocksAround, standingPlace, type Crater, type Rock } from "./detail.ts";
|
|
8
|
+
export { rockMesh, type RockMesh } from "./rocks.ts";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// The standing view: the Moon's measured ground, the Sun and the Earth as a person standing there sees them. `import "@johnmorrisdotca/wakusei-planets/surface/element"` registers `<wakusei-surface>`.
|
|
2
|
+
export { moonSky, localFrame } from "./sky.js";
|
|
3
|
+
export { SURFACE_SITES, siteById } from "./site.js";
|
|
4
|
+
export { EYE_HEIGHT, FOV_LIMITS, WALK_SPEED, clampLook, forward, stepOver, viewProjection } from "./view.js";
|
|
5
|
+
export { decodeHeight, drop, emptyLevel, fillFromCoarser, groundAt, sampleLevel, tileAddress } from "./height.js";
|
|
6
|
+
export { loadPlan, loadTerrain } from "./load.js";
|
|
7
|
+
export { craterIn, craterProfile, madeGround, rimBoost, rocksAround, standingPlace } from "./detail.js";
|
|
8
|
+
export { rockMesh } from "./rocks.js";
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { WakuseiSpace } from "../space/element.ts";
|
|
2
|
+
/**
|
|
3
|
+
* What `land` and `leave` can be told.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import type { LandOptions } from "@johnmorrisdotca/wakusei-planets/surface/land";
|
|
8
|
+
*
|
|
9
|
+
* const options: LandOptions = { tiles: "https://johnmorrisdotca.github.io/wakusei-planets/tiles/", time: "1972-12-11T19:54Z" };
|
|
10
|
+
* console.log(Object.keys(options).join(", "));
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export interface LandOptions {
|
|
14
|
+
/** Where the height tiles are served from (the standing view's `tiles` attribute); its own default when left out. */
|
|
15
|
+
tiles?: string;
|
|
16
|
+
/** The moment on the ground: an ISO date and time, milliseconds since 1970, or `"landing"` for the site's own moment (the landing itself). Left out, it is the orbit view's own moment at the time of the call, so the ground's Sun and Earth are the ones the view above it shows; the standing view then keeps that moment (it runs no clock of its own). */
|
|
17
|
+
time?: number | string;
|
|
18
|
+
/** Where to look first: the site's own look when left out. */
|
|
19
|
+
look?: {
|
|
20
|
+
heading?: number;
|
|
21
|
+
pitch?: number;
|
|
22
|
+
fov?: number;
|
|
23
|
+
};
|
|
24
|
+
/** False to cut straight to the ground with no turn, no zoom and no fade (reduced motion does the same). */
|
|
25
|
+
smooth?: boolean;
|
|
26
|
+
/** Milliseconds to wait for the standing view to be live before giving up (20,000 by default). */
|
|
27
|
+
timeout?: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The site the person is standing at over this orbit view, or `null` while in orbit (and while still coming down).
|
|
31
|
+
*
|
|
32
|
+
* @param space - The orbit view.
|
|
33
|
+
* @returns The site's name, as in `SURFACE_SITES`; `null` while in orbit or still coming down.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { landedAt } from "@johnmorrisdotca/wakusei-planets/surface/land";
|
|
38
|
+
*
|
|
39
|
+
* console.log(landedAt({} as never));
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function landedAt(space: WakuseiSpace): string | null;
|
|
43
|
+
/**
|
|
44
|
+
* The standing view put over the orbit view, while coming down or standing, or `null`: for a page that sets its attributes (the Sun's moment, where to look) or listens to it.
|
|
45
|
+
*
|
|
46
|
+
* @param space - The orbit view.
|
|
47
|
+
* @returns The `<wakusei-surface>` element; `null` when no landing is under way or done.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { surfaceOf } from "@johnmorrisdotca/wakusei-planets/surface/land";
|
|
52
|
+
*
|
|
53
|
+
* console.log(surfaceOf({} as never));
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function surfaceOf(space: WakuseiSpace): HTMLElement | null;
|
|
57
|
+
/**
|
|
58
|
+
* Come down to a site from the orbit view in one move: the camera goes to the Moon if it is not there, turns over the site and comes in to the Moon's closest zoom, the standing view is made and its ground
|
|
59
|
+
* loaded meanwhile, and it fades in over the picture. `wakusei-landing` is sent when it starts and `wakusei-landed` (with the site) when the person is standing. Resolves the standing view; rejects when there is
|
|
60
|
+
* no such site, the view cannot be drawn, the ground does not come in time, or `leave` is called first.
|
|
61
|
+
*
|
|
62
|
+
* @param space - The orbit view, with the Moon in it.
|
|
63
|
+
* @param siteId - A site's name from `SURFACE_SITES`.
|
|
64
|
+
* @param options - Where the tiles are, the moment, where to look first, and whether to move smoothly.
|
|
65
|
+
* @returns The `<wakusei-surface>` element, standing.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts no-run
|
|
69
|
+
* import "@johnmorrisdotca/wakusei-planets/space/element";
|
|
70
|
+
* import { land, leave } from "@johnmorrisdotca/wakusei-planets/surface/land";
|
|
71
|
+
*
|
|
72
|
+
* const space = document.querySelector("wakusei-space")!;
|
|
73
|
+
* space.addEventListener("wakusei-landed", (event) => console.log("down at", (event as CustomEvent).detail.site));
|
|
74
|
+
* await land(space, "apollo-17");
|
|
75
|
+
* await leave(space);
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare function land(space: WakuseiSpace, siteId: string, options?: LandOptions): Promise<HTMLElement>;
|
|
79
|
+
/**
|
|
80
|
+
* Go back up: the standing view fades out and the orbit view's camera rises to the whole Moon. Sends `wakusei-left`. Does nothing when nobody has landed; a landing that is still coming down is called off (its
|
|
81
|
+
* `land` rejects).
|
|
82
|
+
*
|
|
83
|
+
* @param space - The orbit view.
|
|
84
|
+
* @param options - `smooth`: false to cut.
|
|
85
|
+
* @returns Resolves when the orbit view is showing again.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts no-run
|
|
89
|
+
* import { leave } from "@johnmorrisdotca/wakusei-planets/surface/land";
|
|
90
|
+
*
|
|
91
|
+
* const space = document.querySelector("wakusei-space")!;
|
|
92
|
+
* await leave(space);
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export declare function leave(space: WakuseiSpace, options?: {
|
|
96
|
+
smooth?: boolean;
|
|
97
|
+
}): Promise<void>;
|
|
98
|
+
declare module "../space/element.ts" {
|
|
99
|
+
interface WakuseiSpace {
|
|
100
|
+
/** Come down to a site on the Moon from this view in one move; see `land` in `/surface/land`. */
|
|
101
|
+
land(siteId: string, options?: LandOptions): Promise<HTMLElement>;
|
|
102
|
+
/** Go back up from a site; see `leave` in `/surface/land`. */
|
|
103
|
+
leave(options?: {
|
|
104
|
+
smooth?: boolean;
|
|
105
|
+
}): Promise<void>;
|
|
106
|
+
/** The site stood at, or null. */
|
|
107
|
+
readonly landed: string | null;
|
|
108
|
+
/** The standing view over this one, or null. */
|
|
109
|
+
readonly standing: HTMLElement | null;
|
|
110
|
+
}
|
|
111
|
+
}
|