@johnmorrisdotca/wakusei-planets 0.26.0 → 0.27.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 +30 -0
- package/README.md +5 -4
- package/dist/element.d.ts +6 -2
- package/dist/element.js +75 -37
- package/dist/portrait/scene3d.js +5 -4
- package/dist/scene/body.d.ts +11 -0
- package/dist/scene/body.js +61 -8
- package/dist/scene/detail.d.ts +73 -0
- package/dist/scene/detail.js +289 -0
- package/dist/scene/extent.d.ts +10 -0
- package/dist/scene/extent.js +21 -0
- package/dist/scene/glsl.d.ts +23 -2
- package/dist/scene/glsl.js +119 -4
- package/dist/scene/look.d.ts +26 -0
- package/dist/scene/look.js +29 -0
- package/dist/scene/reach.d.ts +34 -0
- package/dist/scene/reach.js +44 -0
- package/dist/scene/textures.d.ts +16 -2
- package/dist/scene/textures.js +21 -0
- package/dist/scene/tiles.d.ts +108 -0
- package/dist/scene/tiles.js +228 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +3 -1
package/dist/scene/look.js
CHANGED
|
@@ -1,5 +1,34 @@
|
|
|
1
1
|
import { starLook } from "../star.js";
|
|
2
2
|
import { looks, stars, waiting } from "./store.js";
|
|
3
|
+
const SMOOTH = {
|
|
4
|
+
jupiter: { amount: 0.11, stretch: 7 },
|
|
5
|
+
saturn: { amount: 0.06, stretch: 12 },
|
|
6
|
+
uranus: { amount: 0.04, stretch: 8 },
|
|
7
|
+
neptune: { amount: 0.05, stretch: 8 },
|
|
8
|
+
venus: { amount: 0.07, stretch: 4 },
|
|
9
|
+
titan: { amount: 0.06, stretch: 4 },
|
|
10
|
+
earth: { amount: 0.2, stretch: 1 },
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* The strength and stretch of a body's generic fine detail: its own `detail`, else the table's for a body that is smooth (the gas giants, Venus, Titan) or the rocky default.
|
|
14
|
+
*
|
|
15
|
+
* @param look - The body's look.
|
|
16
|
+
* @returns How strong it is (0 to 1) and how much it is stretched along the lines of latitude.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { detailOf, lookOf } from "@johnmorrisdotca/wakusei-planets/look";
|
|
21
|
+
*
|
|
22
|
+
* const look = lookOf("mars");
|
|
23
|
+
* console.log(look === undefined ? "no look" : detailOf(look).amount);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function detailOf(look) {
|
|
27
|
+
if (look.emissive === true)
|
|
28
|
+
return { amount: 0, stretch: 1 };
|
|
29
|
+
const own = SMOOTH[look.id];
|
|
30
|
+
return { amount: look.detail?.amount ?? own?.amount ?? 0.32, stretch: look.detail?.stretch ?? own?.stretch ?? 1 };
|
|
31
|
+
}
|
|
3
32
|
/**
|
|
4
33
|
* Teach the package how to draw a body in three.js. Each body's entry point (`@johnmorrisdotca/wakusei-planets/jupiter`) calls this once as it loads; call it yourself to add or replace a look.
|
|
5
34
|
*
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { BodyLook } from "./look.ts";
|
|
2
|
+
import { type TileLevel } from "./tiles.ts";
|
|
3
|
+
/** CSS pixels across the picture a zoom limit is worked out for. */
|
|
4
|
+
export declare const REFERENCE_FRAME = 480;
|
|
5
|
+
/** The widest a texel of the map may be, in CSS pixels at the reference frame, while the picture still shows real detail. */
|
|
6
|
+
export declare const SHARP_TEXEL = 1.6;
|
|
7
|
+
/** How many times further than the real detail the generic detail carries a body: a rocky or icy one, a smooth one (a gas giant's bands), and a ring system (its strip is a line, so the noise only sharpens it). */
|
|
8
|
+
export declare const GENERIC_REACH: {
|
|
9
|
+
readonly rocky: 1.15;
|
|
10
|
+
readonly smooth: 2;
|
|
11
|
+
readonly rings: 1.25;
|
|
12
|
+
};
|
|
13
|
+
/** No body zooms less than this (what the shaded looks always had), and none more than this. */
|
|
14
|
+
export declare const ZOOM_FLOOR = 8;
|
|
15
|
+
export declare const ZOOM_CEILING = 1000;
|
|
16
|
+
/** The width of map a body drawn by its shader alone is counted as: its craters and bands are drawn from numbers at any size, but only so finely. */
|
|
17
|
+
export declare const SHADER_WIDTH = 4096;
|
|
18
|
+
/** The widest map a body has, in texels round the equator: the finest tile level, else its widest painted picture, else what its shader counts for. */
|
|
19
|
+
export declare function mapWidthOf(look: BodyLook | undefined, painted: boolean, levels: readonly TileLevel[] | undefined): number;
|
|
20
|
+
/**
|
|
21
|
+
* The width of map round the equator that has as many texels to a kilometre as a body's picture of its rings has along a radius: the rings are one line of samples from their inner edge to their outer, so a body that has
|
|
22
|
+
* a picture of them (Saturn) can be zoomed to where the rings' detail runs out, which is further than its globe's. `0` for a body with none.
|
|
23
|
+
*/
|
|
24
|
+
export declare function ringWidthOf(look: BodyLook | undefined, radiusKm: number): number;
|
|
25
|
+
/**
|
|
26
|
+
* The zoom limit of a body: where its real detail runs out times how far its generic detail carries it, in zoom steps of a picture `extent` radii from the middle to the edge.
|
|
27
|
+
*
|
|
28
|
+
* @param extent - Body radii from the middle of the picture to its edge at zoom 1.
|
|
29
|
+
* @param width - The widest map the body has, in texels round the equator (`mapWidthOf`).
|
|
30
|
+
* @param smooth - True for a body whose surface is soft (a gas giant, Venus, Titan): the generic detail carries it further.
|
|
31
|
+
* @param ringWidth - The equivalent width of the body's picture of its rings (`ringWidthOf`), or 0.
|
|
32
|
+
* @returns The zoom, from `ZOOM_FLOOR` to `ZOOM_CEILING`.
|
|
33
|
+
*/
|
|
34
|
+
export declare function zoomLimit(extent: number, width: number, smooth: boolean, ringWidth?: number): number;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { BASE_WIDTH } from "./tiles.js";
|
|
2
|
+
/** CSS pixels across the picture a zoom limit is worked out for. */
|
|
3
|
+
export const REFERENCE_FRAME = 480;
|
|
4
|
+
/** The widest a texel of the map may be, in CSS pixels at the reference frame, while the picture still shows real detail. */
|
|
5
|
+
export const SHARP_TEXEL = 1.6;
|
|
6
|
+
/** How many times further than the real detail the generic detail carries a body: a rocky or icy one, a smooth one (a gas giant's bands), and a ring system (its strip is a line, so the noise only sharpens it). */
|
|
7
|
+
export const GENERIC_REACH = { rocky: 1.15, smooth: 2, rings: 1.25 };
|
|
8
|
+
/** No body zooms less than this (what the shaded looks always had), and none more than this. */
|
|
9
|
+
export const ZOOM_FLOOR = 8;
|
|
10
|
+
export const ZOOM_CEILING = 1000;
|
|
11
|
+
/** The width of map a body drawn by its shader alone is counted as: its craters and bands are drawn from numbers at any size, but only so finely. */
|
|
12
|
+
export const SHADER_WIDTH = 4096;
|
|
13
|
+
/** The widest map a body has, in texels round the equator: the finest tile level, else its widest painted picture, else what its shader counts for. */
|
|
14
|
+
export function mapWidthOf(look, painted, levels) {
|
|
15
|
+
if (!painted || look?.painted === undefined)
|
|
16
|
+
return SHADER_WIDTH;
|
|
17
|
+
const own = look.painted.widths[look.painted.widths.length - 1] ?? BASE_WIDTH;
|
|
18
|
+
const finest = levels?.reduce((width, level) => Math.max(width, level.width), 0) ?? 0;
|
|
19
|
+
return Math.max(own, finest);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The width of map round the equator that has as many texels to a kilometre as a body's picture of its rings has along a radius: the rings are one line of samples from their inner edge to their outer, so a body that has
|
|
23
|
+
* a picture of them (Saturn) can be zoomed to where the rings' detail runs out, which is further than its globe's. `0` for a body with none.
|
|
24
|
+
*/
|
|
25
|
+
export function ringWidthOf(look, radiusKm) {
|
|
26
|
+
const rings = look?.painted?.rings;
|
|
27
|
+
if (rings === undefined || rings.width <= 0 || rings.to <= rings.from)
|
|
28
|
+
return 0;
|
|
29
|
+
return (2 * Math.PI * radiusKm * rings.width) / (rings.to - rings.from);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The zoom limit of a body: where its real detail runs out times how far its generic detail carries it, in zoom steps of a picture `extent` radii from the middle to the edge.
|
|
33
|
+
*
|
|
34
|
+
* @param extent - Body radii from the middle of the picture to its edge at zoom 1.
|
|
35
|
+
* @param width - The widest map the body has, in texels round the equator (`mapWidthOf`).
|
|
36
|
+
* @param smooth - True for a body whose surface is soft (a gas giant, Venus, Titan): the generic detail carries it further.
|
|
37
|
+
* @param ringWidth - The equivalent width of the body's picture of its rings (`ringWidthOf`), or 0.
|
|
38
|
+
* @returns The zoom, from `ZOOM_FLOOR` to `ZOOM_CEILING`.
|
|
39
|
+
*/
|
|
40
|
+
export function zoomLimit(extent, width, smooth, ringWidth = 0) {
|
|
41
|
+
const across = (texels) => (SHARP_TEXEL * texels) / (Math.PI * REFERENCE_FRAME);
|
|
42
|
+
const reach = Math.max(across(width) * (smooth ? GENERIC_REACH.smooth : GENERIC_REACH.rocky), across(ringWidth) * GENERIC_REACH.rings);
|
|
43
|
+
return Math.round(Math.min(ZOOM_CEILING, Math.max(ZOOM_FLOOR, reach * extent)) * 10) / 10;
|
|
44
|
+
}
|
package/dist/scene/textures.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ import type * as THREE from "three";
|
|
|
9
9
|
* console.log(TEXTURE_BASE);
|
|
10
10
|
* ```
|
|
11
11
|
*/
|
|
12
|
-
export declare const TEXTURE_BASE = "https://cdn.jsdelivr.net/npm/@johnmorrisdotca/wakusei-planets@0.
|
|
12
|
+
export declare const TEXTURE_BASE = "https://cdn.jsdelivr.net/npm/@johnmorrisdotca/wakusei-planets@0.27.0/dist/textures/";
|
|
13
13
|
/**
|
|
14
14
|
* Where the sharpest surface pictures are: the files of this exact version in the repository, on jsDelivr's GitHub address. The npm package holds only the two smaller widths, so a page that
|
|
15
15
|
* never zooms in never fetches them. Set `texture-far-base` on an element to serve them yourself.
|
|
@@ -21,8 +21,22 @@ export declare const TEXTURE_BASE = "https://cdn.jsdelivr.net/npm/@johnmorrisdot
|
|
|
21
21
|
* console.log(TEXTURE_FAR_BASE);
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
24
|
-
export declare const TEXTURE_FAR_BASE = "https://cdn.jsdelivr.net/gh/johnmorrisdotca/wakusei-planets@v0.
|
|
24
|
+
export declare const TEXTURE_FAR_BASE = "https://cdn.jsdelivr.net/gh/johnmorrisdotca/wakusei-planets@v0.27.0/textures/";
|
|
25
|
+
/**
|
|
26
|
+
* Where the finer map tiles are by default: the demo site's `tiles/` folder (GitHub Pages sends `access-control-allow-origin: *`). The tiles are made by `scripts/make-body-tiles.py` and are in no package and no repository:
|
|
27
|
+
* a page that serves its own gives the folder in the `tiles` attribute, and `tiles="off"` turns them off. A page that sets `texture-base` (it serves its own pictures) has none unless it says where they are.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts no-run
|
|
31
|
+
* import { TILES_BASE } from "@johnmorrisdotca/wakusei-planets/element";
|
|
32
|
+
*
|
|
33
|
+
* console.log(TILES_BASE);
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare const TILES_BASE = "https://johnmorrisdotca.github.io/wakusei-planets/tiles/";
|
|
25
37
|
export declare const slash: (base: string) => string;
|
|
38
|
+
/** The folder of finer map tiles for an element: its `tiles` attribute (`off` or `none` for none), else `TILES_BASE` unless the element names its own `texture-base`. */
|
|
39
|
+
export declare function tilesBase(element: Element): string | undefined;
|
|
26
40
|
export declare function texturePaths(element: Element): {
|
|
27
41
|
textureBase(): string;
|
|
28
42
|
farBase(): string;
|
package/dist/scene/textures.js
CHANGED
|
@@ -22,7 +22,28 @@ export const TEXTURE_BASE = `https://cdn.jsdelivr.net/npm/@johnmorrisdotca/wakus
|
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
24
24
|
export const TEXTURE_FAR_BASE = `https://cdn.jsdelivr.net/gh/johnmorrisdotca/wakusei-planets@v${VERSION}/textures/`;
|
|
25
|
+
/**
|
|
26
|
+
* Where the finer map tiles are by default: the demo site's `tiles/` folder (GitHub Pages sends `access-control-allow-origin: *`). The tiles are made by `scripts/make-body-tiles.py` and are in no package and no repository:
|
|
27
|
+
* a page that serves its own gives the folder in the `tiles` attribute, and `tiles="off"` turns them off. A page that sets `texture-base` (it serves its own pictures) has none unless it says where they are.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts no-run
|
|
31
|
+
* import { TILES_BASE } from "@johnmorrisdotca/wakusei-planets/element";
|
|
32
|
+
*
|
|
33
|
+
* console.log(TILES_BASE);
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export const TILES_BASE = "https://johnmorrisdotca.github.io/wakusei-planets/tiles/";
|
|
25
37
|
export const slash = (base) => (base.endsWith("/") ? base : `${base}/`);
|
|
38
|
+
/** The folder of finer map tiles for an element: its `tiles` attribute (`off` or `none` for none), else `TILES_BASE` unless the element names its own `texture-base`. */
|
|
39
|
+
export function tilesBase(element) {
|
|
40
|
+
const own = element.getAttribute("tiles");
|
|
41
|
+
if (own === "off" || own === "none")
|
|
42
|
+
return undefined;
|
|
43
|
+
if (own !== null && own !== "")
|
|
44
|
+
return slash(own);
|
|
45
|
+
return element.hasAttribute("texture-base") ? undefined : TILES_BASE;
|
|
46
|
+
}
|
|
26
47
|
export function texturePaths(element) {
|
|
27
48
|
const textureBase = () => {
|
|
28
49
|
const own = element.getAttribute("texture-base");
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { Vec3 } from "../vec.ts";
|
|
2
|
+
/** The side of a tile in pixels. */
|
|
3
|
+
export declare const TILE_SIZE = 512;
|
|
4
|
+
/** The width of the package's own map, the pyramid's level 0. */
|
|
5
|
+
export declare const BASE_WIDTH = 2048;
|
|
6
|
+
/** The most tiles in a window of picture (a 2048 pixel square, or 4096 by 1024 round a pole) and the most along one side: the graphics memory a window takes is about 22 MB. */
|
|
7
|
+
export declare const WINDOW_TILES = 16;
|
|
8
|
+
export declare const WINDOW_SIDE = 8;
|
|
9
|
+
/** How much larger than a device pixel a texel may be before the next level is wanted: 1.25 is a quarter larger. */
|
|
10
|
+
export declare const TEXEL_SLACK = 1.25;
|
|
11
|
+
/** One level of a body's pyramid, from the manifest. */
|
|
12
|
+
export interface TileLevel {
|
|
13
|
+
level: number;
|
|
14
|
+
width: number;
|
|
15
|
+
height: number;
|
|
16
|
+
columns: number;
|
|
17
|
+
rows: number;
|
|
18
|
+
}
|
|
19
|
+
/** What a tile set says it holds: the levels of each body (level 1 and up). */
|
|
20
|
+
export interface TileManifest {
|
|
21
|
+
tileSize: number;
|
|
22
|
+
bodies: Record<string, TileLevel[]>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Read a tile set's `manifest.json`. A manifest that is not the shape this reads, or whose tile size is not the one the loader cuts windows for, is `undefined`: the bodies are then drawn from their own pictures.
|
|
26
|
+
*
|
|
27
|
+
* @param value - The parsed JSON.
|
|
28
|
+
* @returns The manifest, or `undefined`.
|
|
29
|
+
*/
|
|
30
|
+
export declare function parseManifest(value: unknown): TileManifest | undefined;
|
|
31
|
+
/** The part of the surface in view: longitude from `lonMin` to `lonMax` (radians, not wrapped, so `lonMax - lonMin` is the width, at most a whole turn) and latitude from `latMin` to `latMax`. */
|
|
32
|
+
export interface Region {
|
|
33
|
+
lonMin: number;
|
|
34
|
+
lonMax: number;
|
|
35
|
+
latMin: number;
|
|
36
|
+
latMax: number;
|
|
37
|
+
}
|
|
38
|
+
/** A body's axes in the view: the three directions its east, pole and prime meridian point in (unit vectors, x right, y up, z toward the viewer), and the lengths of its semi-axes in the same order, in radii. */
|
|
39
|
+
export interface Axes {
|
|
40
|
+
east: Vec3;
|
|
41
|
+
pole: Vec3;
|
|
42
|
+
meridian: Vec3;
|
|
43
|
+
scale: Vec3;
|
|
44
|
+
}
|
|
45
|
+
/** The orthographic camera's box, in radii from the body's centre. */
|
|
46
|
+
export interface ViewBox {
|
|
47
|
+
left: number;
|
|
48
|
+
right: number;
|
|
49
|
+
bottom: number;
|
|
50
|
+
top: number;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The longitudes and latitudes of the body's surface that the camera's box shows: the front of the globe is sampled on a grid and round the limb, each point turned into the body's own frame,
|
|
54
|
+
* and the two poles are looked for separately (a pole in view means every longitude is). The region is a little too large rather than too small.
|
|
55
|
+
*
|
|
56
|
+
* @param box - The camera's box.
|
|
57
|
+
* @param axes - The body's axes in the view.
|
|
58
|
+
* @param pad - The margin added all round, as a fraction of the region (and at least two degrees when it is not 0). Default 0.06.
|
|
59
|
+
* @returns The region, or `undefined` when the box shows none of the disc.
|
|
60
|
+
*/
|
|
61
|
+
export declare function visibleRegion(box: ViewBox, axes: Axes, pad?: number): Region | undefined;
|
|
62
|
+
/** A window of picture: tiles of one level, `cols` across starting at column `col0` (the columns wrap round the far edge) and `rows` down from row `row0`. */
|
|
63
|
+
export interface TileWindow {
|
|
64
|
+
level: number;
|
|
65
|
+
col0: number;
|
|
66
|
+
cols: number;
|
|
67
|
+
row0: number;
|
|
68
|
+
rows: number;
|
|
69
|
+
columns: number;
|
|
70
|
+
rowsTotal: number;
|
|
71
|
+
}
|
|
72
|
+
/** The window of a level that holds the region, or `undefined` when it would be more tiles than a window may hold. */
|
|
73
|
+
export declare function windowOf(level: TileLevel, region: Region): TileWindow | undefined;
|
|
74
|
+
/**
|
|
75
|
+
* The window the view needs: the lowest level whose texels are no wider than a device pixel (a quarter wider, `TEXEL_SLACK`), or the finest there is, brought down to the finest level whose
|
|
76
|
+
* window fits. `undefined` when the body's own 2048 pixel map is sharp enough, or no level has a window that fits.
|
|
77
|
+
*
|
|
78
|
+
* @param region - The part of the surface in view.
|
|
79
|
+
* @param levels - The body's levels, level 1 up.
|
|
80
|
+
* @param radiansPixels - Device pixels along one radian of the body at the middle of the view.
|
|
81
|
+
* @returns The window, or `undefined`.
|
|
82
|
+
*/
|
|
83
|
+
export declare function chooseWindow(region: Region, levels: readonly TileLevel[], radiansPixels: number): TileWindow | undefined;
|
|
84
|
+
/** The window's place on the map, for the shader: `u0` the map's longitude coordinate of its west edge (0 to 1), `du` its width, `vTop` the latitude coordinate of its north edge (1 is the north pole) and `dv` its height. */
|
|
85
|
+
export declare function windowRect(window: TileWindow): {
|
|
86
|
+
u0: number;
|
|
87
|
+
du: number;
|
|
88
|
+
vTop: number;
|
|
89
|
+
dv: number;
|
|
90
|
+
};
|
|
91
|
+
/** The tiles of a window, with where each goes in the window's picture (`x`, `y` in pixels from its top left). */
|
|
92
|
+
export declare function windowTiles(window: TileWindow): {
|
|
93
|
+
level: number;
|
|
94
|
+
col: number;
|
|
95
|
+
row: number;
|
|
96
|
+
x: number;
|
|
97
|
+
y: number;
|
|
98
|
+
}[];
|
|
99
|
+
/** `layer/level/column/row` as the tile's path under the tile set (without the `.webp`). */
|
|
100
|
+
export declare const tilePath: (body: string, tile: {
|
|
101
|
+
level: number;
|
|
102
|
+
col: number;
|
|
103
|
+
row: number;
|
|
104
|
+
}) => string;
|
|
105
|
+
/** True when the region lies inside the window (the window is then still the right one for the view, whatever else changed). */
|
|
106
|
+
export declare function covers(window: TileWindow, region: Region): boolean;
|
|
107
|
+
/** The angle one texel of a level covers, in radians along the equator (`2 pi / width`). */
|
|
108
|
+
export declare const texelAngle: (width: number) => number;
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/** The side of a tile in pixels. */
|
|
2
|
+
export const TILE_SIZE = 512;
|
|
3
|
+
/** The width of the package's own map, the pyramid's level 0. */
|
|
4
|
+
export const BASE_WIDTH = 2048;
|
|
5
|
+
/** The most tiles in a window of picture (a 2048 pixel square, or 4096 by 1024 round a pole) and the most along one side: the graphics memory a window takes is about 22 MB. */
|
|
6
|
+
export const WINDOW_TILES = 16;
|
|
7
|
+
export const WINDOW_SIDE = 8;
|
|
8
|
+
/** How much larger than a device pixel a texel may be before the next level is wanted: 1.25 is a quarter larger. */
|
|
9
|
+
export const TEXEL_SLACK = 1.25;
|
|
10
|
+
const TAU = Math.PI * 2;
|
|
11
|
+
const RAD = Math.PI / 180;
|
|
12
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
13
|
+
const whole = (value, min) => typeof value === "number" && Number.isInteger(value) && value >= min;
|
|
14
|
+
/**
|
|
15
|
+
* Read a tile set's `manifest.json`. A manifest that is not the shape this reads, or whose tile size is not the one the loader cuts windows for, is `undefined`: the bodies are then drawn from their own pictures.
|
|
16
|
+
*
|
|
17
|
+
* @param value - The parsed JSON.
|
|
18
|
+
* @returns The manifest, or `undefined`.
|
|
19
|
+
*/
|
|
20
|
+
export function parseManifest(value) {
|
|
21
|
+
if (!isRecord(value) || value["tileSize"] !== TILE_SIZE || !isRecord(value["bodies"]))
|
|
22
|
+
return undefined;
|
|
23
|
+
const bodies = {};
|
|
24
|
+
for (const [id, entry] of Object.entries(value["bodies"])) {
|
|
25
|
+
if (!isRecord(entry) || !Array.isArray(entry["levels"]))
|
|
26
|
+
continue;
|
|
27
|
+
const levels = [];
|
|
28
|
+
for (const item of entry["levels"]) {
|
|
29
|
+
if (!isRecord(item))
|
|
30
|
+
continue;
|
|
31
|
+
const { level, width, height, columns, rows } = item;
|
|
32
|
+
if (!whole(level, 1) || !whole(width, TILE_SIZE) || !whole(height, TILE_SIZE) || !whole(columns, 1) || !whole(rows, 1))
|
|
33
|
+
continue;
|
|
34
|
+
if (width !== BASE_WIDTH * 2 ** level || height !== width / 2 || columns !== width / TILE_SIZE || rows !== height / TILE_SIZE)
|
|
35
|
+
continue;
|
|
36
|
+
levels.push({ level, width, height, columns, rows });
|
|
37
|
+
}
|
|
38
|
+
levels.sort((a, b) => a.level - b.level);
|
|
39
|
+
if (levels.length > 0)
|
|
40
|
+
bodies[id] = levels;
|
|
41
|
+
}
|
|
42
|
+
return { tileSize: TILE_SIZE, bodies };
|
|
43
|
+
}
|
|
44
|
+
const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
45
|
+
const wrapPi = (a) => a - TAU * Math.floor((a + Math.PI) / TAU);
|
|
46
|
+
const SAMPLES = 17;
|
|
47
|
+
/**
|
|
48
|
+
* The longitudes and latitudes of the body's surface that the camera's box shows: the front of the globe is sampled on a grid and round the limb, each point turned into the body's own frame,
|
|
49
|
+
* and the two poles are looked for separately (a pole in view means every longitude is). The region is a little too large rather than too small.
|
|
50
|
+
*
|
|
51
|
+
* @param box - The camera's box.
|
|
52
|
+
* @param axes - The body's axes in the view.
|
|
53
|
+
* @param pad - The margin added all round, as a fraction of the region (and at least two degrees when it is not 0). Default 0.06.
|
|
54
|
+
* @returns The region, or `undefined` when the box shows none of the disc.
|
|
55
|
+
*/
|
|
56
|
+
export function visibleRegion(box, axes, pad = 0.06) {
|
|
57
|
+
const lons = [];
|
|
58
|
+
const lats = [];
|
|
59
|
+
const mx = (box.left + box.right) / 2;
|
|
60
|
+
const my = (box.bottom + box.top) / 2;
|
|
61
|
+
let nearest = Infinity;
|
|
62
|
+
let ref = 0;
|
|
63
|
+
const take = (x, y) => {
|
|
64
|
+
const r2 = x * x + y * y;
|
|
65
|
+
if (r2 > 1)
|
|
66
|
+
return;
|
|
67
|
+
const w = [x, y, Math.sqrt(1 - r2)];
|
|
68
|
+
const pm = dot(axes.meridian, w) / axes.scale[2];
|
|
69
|
+
const pe = dot(axes.east, w) / axes.scale[0];
|
|
70
|
+
const pp = dot(axes.pole, w) / axes.scale[1];
|
|
71
|
+
const length = Math.hypot(pm, pe, pp) || 1;
|
|
72
|
+
const lon = Math.atan2(pe, pm);
|
|
73
|
+
// the middle longitude is the one of the sample nearest the middle of the box, so a spread across the antimeridian is measured from there
|
|
74
|
+
const d = (x - mx) * (x - mx) + (y - my) * (y - my);
|
|
75
|
+
if (d < nearest) {
|
|
76
|
+
nearest = d;
|
|
77
|
+
ref = lon;
|
|
78
|
+
}
|
|
79
|
+
lons.push(lon);
|
|
80
|
+
lats.push(Math.asin(Math.max(-1, Math.min(1, pp / length))));
|
|
81
|
+
};
|
|
82
|
+
for (let i = 0; i < SAMPLES; i += 1) {
|
|
83
|
+
for (let j = 0; j < SAMPLES; j += 1)
|
|
84
|
+
take(box.left + ((box.right - box.left) * i) / (SAMPLES - 1), box.bottom + ((box.top - box.bottom) * j) / (SAMPLES - 1));
|
|
85
|
+
}
|
|
86
|
+
// the limb: where the box cuts the edge of the disc, and the edge inside the box
|
|
87
|
+
for (let k = 0; k < 64; k += 1) {
|
|
88
|
+
const a = (k / 64) * TAU;
|
|
89
|
+
const x = Math.cos(a) * 0.999;
|
|
90
|
+
const y = Math.sin(a) * 0.999;
|
|
91
|
+
if (x >= box.left && x <= box.right && y >= box.bottom && y <= box.top)
|
|
92
|
+
take(x, y);
|
|
93
|
+
}
|
|
94
|
+
// the points where the box's own edges cross the disc (a box that cuts the disc without a grid point on it near the edge)
|
|
95
|
+
for (const x of [box.left, box.right]) {
|
|
96
|
+
const half = Math.sqrt(Math.max(0, 1 - x * x));
|
|
97
|
+
if (Math.abs(x) < 1)
|
|
98
|
+
for (const y of [-half * 0.999, half * 0.999])
|
|
99
|
+
if (y >= box.bottom && y <= box.top)
|
|
100
|
+
take(x, y);
|
|
101
|
+
}
|
|
102
|
+
for (const y of [box.bottom, box.top]) {
|
|
103
|
+
const half = Math.sqrt(Math.max(0, 1 - y * y));
|
|
104
|
+
if (Math.abs(y) < 1)
|
|
105
|
+
for (const x of [-half * 0.999, half * 0.999])
|
|
106
|
+
if (x >= box.left && x <= box.right)
|
|
107
|
+
take(x, y);
|
|
108
|
+
}
|
|
109
|
+
if (lons.length === 0)
|
|
110
|
+
return undefined;
|
|
111
|
+
let lo = Infinity;
|
|
112
|
+
let hi = -Infinity;
|
|
113
|
+
for (const lon of lons) {
|
|
114
|
+
const rel = wrapPi(lon - ref);
|
|
115
|
+
if (rel < lo)
|
|
116
|
+
lo = rel;
|
|
117
|
+
if (rel > hi)
|
|
118
|
+
hi = rel;
|
|
119
|
+
}
|
|
120
|
+
let latMin = Math.min(...lats);
|
|
121
|
+
let latMax = Math.max(...lats);
|
|
122
|
+
// a pole in the box: its point is on the front of the disc (it faces the viewer) and inside the box
|
|
123
|
+
let polar = false;
|
|
124
|
+
for (const sign of [1, -1]) {
|
|
125
|
+
const px = axes.pole[0] * axes.scale[1] * sign;
|
|
126
|
+
const py = axes.pole[1] * axes.scale[1] * sign;
|
|
127
|
+
const pz = axes.pole[2] * sign;
|
|
128
|
+
if (pz > 0 && px >= box.left && px <= box.right && py >= box.bottom && py <= box.top) {
|
|
129
|
+
polar = true;
|
|
130
|
+
if (sign > 0)
|
|
131
|
+
latMax = Math.PI / 2;
|
|
132
|
+
else
|
|
133
|
+
latMin = -Math.PI / 2;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
let lonMin = ref + lo;
|
|
137
|
+
let lonMax = ref + hi;
|
|
138
|
+
if (polar || hi - lo > TAU * 0.98) {
|
|
139
|
+
lonMin = ref - Math.PI;
|
|
140
|
+
lonMax = ref + Math.PI;
|
|
141
|
+
}
|
|
142
|
+
// a margin, so a small turn or pan does not at once leave the window
|
|
143
|
+
const padLon = pad > 0 ? Math.max(2 * RAD, (lonMax - lonMin) * pad) : 0;
|
|
144
|
+
const padLat = pad > 0 ? Math.max(2 * RAD, (latMax - latMin) * pad) : 0;
|
|
145
|
+
if (lonMax - lonMin < TAU) {
|
|
146
|
+
const spread = Math.min(TAU, lonMax - lonMin + 2 * padLon);
|
|
147
|
+
const middle = (lonMin + lonMax) / 2;
|
|
148
|
+
lonMin = middle - spread / 2;
|
|
149
|
+
lonMax = middle + spread / 2;
|
|
150
|
+
}
|
|
151
|
+
return { lonMin, lonMax, latMin: Math.max(-Math.PI / 2, latMin - padLat), latMax: Math.min(Math.PI / 2, latMax + padLat) };
|
|
152
|
+
}
|
|
153
|
+
/** The window of a level that holds the region, or `undefined` when it would be more tiles than a window may hold. */
|
|
154
|
+
export function windowOf(level, region) {
|
|
155
|
+
const { columns, rows: rowsTotal } = level;
|
|
156
|
+
const full = region.lonMax - region.lonMin >= TAU - 1e-9;
|
|
157
|
+
let col0 = 0;
|
|
158
|
+
let cols = columns;
|
|
159
|
+
if (!full) {
|
|
160
|
+
const u0 = (region.lonMin / TAU + 0.5) * columns;
|
|
161
|
+
const u1 = (region.lonMax / TAU + 0.5) * columns;
|
|
162
|
+
const first = Math.floor(u0);
|
|
163
|
+
cols = Math.min(columns, Math.ceil(u1) - first);
|
|
164
|
+
col0 = ((first % columns) + columns) % columns;
|
|
165
|
+
}
|
|
166
|
+
const r0 = Math.max(0, Math.floor((0.5 - region.latMax / Math.PI) * rowsTotal));
|
|
167
|
+
const r1 = Math.min(rowsTotal, Math.ceil((0.5 - region.latMin / Math.PI) * rowsTotal));
|
|
168
|
+
const rows = Math.max(1, r1 - r0);
|
|
169
|
+
if (cols * rows > WINDOW_TILES || cols > WINDOW_SIDE || rows > WINDOW_SIDE)
|
|
170
|
+
return undefined;
|
|
171
|
+
return { level: level.level, col0, cols, row0: Math.min(r0, rowsTotal - rows), rows, columns, rowsTotal };
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* The window the view needs: the lowest level whose texels are no wider than a device pixel (a quarter wider, `TEXEL_SLACK`), or the finest there is, brought down to the finest level whose
|
|
175
|
+
* window fits. `undefined` when the body's own 2048 pixel map is sharp enough, or no level has a window that fits.
|
|
176
|
+
*
|
|
177
|
+
* @param region - The part of the surface in view.
|
|
178
|
+
* @param levels - The body's levels, level 1 up.
|
|
179
|
+
* @param radiansPixels - Device pixels along one radian of the body at the middle of the view.
|
|
180
|
+
* @returns The window, or `undefined`.
|
|
181
|
+
*/
|
|
182
|
+
export function chooseWindow(region, levels, radiansPixels) {
|
|
183
|
+
if (levels.length === 0 || !(radiansPixels > 0))
|
|
184
|
+
return undefined;
|
|
185
|
+
const need = radiansPixels / TEXEL_SLACK;
|
|
186
|
+
if (BASE_WIDTH / TAU >= need)
|
|
187
|
+
return undefined;
|
|
188
|
+
const wanted = levels.findIndex((l) => l.width / TAU >= need);
|
|
189
|
+
for (let i = wanted === -1 ? levels.length - 1 : wanted; i >= 0; i -= 1) {
|
|
190
|
+
const window = windowOf(levels[i], region);
|
|
191
|
+
if (window !== undefined)
|
|
192
|
+
return window;
|
|
193
|
+
}
|
|
194
|
+
return undefined;
|
|
195
|
+
}
|
|
196
|
+
/** The window's place on the map, for the shader: `u0` the map's longitude coordinate of its west edge (0 to 1), `du` its width, `vTop` the latitude coordinate of its north edge (1 is the north pole) and `dv` its height. */
|
|
197
|
+
export function windowRect(window) {
|
|
198
|
+
return { u0: window.col0 / window.columns, du: window.cols / window.columns, vTop: 1 - window.row0 / window.rowsTotal, dv: window.rows / window.rowsTotal };
|
|
199
|
+
}
|
|
200
|
+
/** The tiles of a window, with where each goes in the window's picture (`x`, `y` in pixels from its top left). */
|
|
201
|
+
export function windowTiles(window) {
|
|
202
|
+
const out = [];
|
|
203
|
+
for (let j = 0; j < window.rows; j += 1) {
|
|
204
|
+
for (let i = 0; i < window.cols; i += 1)
|
|
205
|
+
out.push({ level: window.level, col: (window.col0 + i) % window.columns, row: window.row0 + j, x: i * TILE_SIZE, y: j * TILE_SIZE });
|
|
206
|
+
}
|
|
207
|
+
return out;
|
|
208
|
+
}
|
|
209
|
+
/** `layer/level/column/row` as the tile's path under the tile set (without the `.webp`). */
|
|
210
|
+
export const tilePath = (body, tile) => `${body}/${tile.level}/${tile.col}/${tile.row}`;
|
|
211
|
+
/** True when the region lies inside the window (the window is then still the right one for the view, whatever else changed). */
|
|
212
|
+
export function covers(window, region) {
|
|
213
|
+
const need = region.lonMax - region.lonMin;
|
|
214
|
+
const width = (window.cols / window.columns) * TAU;
|
|
215
|
+
if (window.cols < window.columns) {
|
|
216
|
+
const start = (window.col0 / window.columns - 0.5) * TAU;
|
|
217
|
+
const shift = ((((region.lonMin - start) % TAU) + TAU) % TAU);
|
|
218
|
+
// a region that starts a hair before the window's edge is a turn later
|
|
219
|
+
const inside = shift + need <= width + 1e-9;
|
|
220
|
+
if (!inside)
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
const north = (0.5 - window.row0 / window.rowsTotal) * Math.PI;
|
|
224
|
+
const south = (0.5 - (window.row0 + window.rows) / window.rowsTotal) * Math.PI;
|
|
225
|
+
return region.latMax <= north + 1e-9 && region.latMin >= south - 1e-9;
|
|
226
|
+
}
|
|
227
|
+
/** The angle one texel of a level covers, in radians along the equator (`2 pi / width`). */
|
|
228
|
+
export const texelAngle = (width) => TAU / width;
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const VERSION = "0.
|
|
1
|
+
export declare const VERSION = "0.27.0";
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = "0.
|
|
1
|
+
export const VERSION = "0.27.0";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johnmorrisdotca/wakusei-planets",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.27.0",
|
|
4
4
|
"description": "Wakusei Planets 惑星 — The Sun, planets and moons for the web: each as a clean line drawing, a shaded realistic drawing or a painted surface map, with real sizes, axial tilt and rotation as data, and positions of the moons for any date and time. SVG for the lines, three.js (optional) for the rest.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"planets",
|
|
@@ -330,6 +330,8 @@
|
|
|
330
330
|
"test:package": "pnpm build && node scripts/check-package.ts",
|
|
331
331
|
"check": "pnpm lint && pnpm typecheck && pnpm test && pnpm build && node scripts/check-package.ts",
|
|
332
332
|
"prepublishOnly": "pnpm check",
|
|
333
|
+
"tiles": ".cache/venv/bin/python -I scripts/make-body-tiles.py build",
|
|
334
|
+
"test:tiles": ".cache/venv/bin/python -I scripts/make-body-tiles.py selftest",
|
|
333
335
|
"family:readme": "node scripts/family-readme.mjs",
|
|
334
336
|
"readme:images": "node scripts/readme-images.mjs",
|
|
335
337
|
"site": "pnpm build && node scripts/site.ts",
|