@johnmorrisdotca/wakusei-planets 0.71.0 → 0.73.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.
@@ -0,0 +1,293 @@
1
+ // An irregular moon the package does not draw in full: a coarse rock, sized from the catalogue, coloured from its albedo, with a shape guessed from its id.
2
+ // The same id always builds the same rock. A full moon (one in the body's table) is not one of these. Nothing here draws; the element asks.
3
+ import { BODIES } from "../facts.js";
4
+ import { ESTIMATED_MOONS } from "../data/estimated-moons.js";
5
+ import { shapeRadii } from "./shape.js";
6
+ const SIZES = ["measured", "sheet", "derived", "unknown"];
7
+ const TONES = {
8
+ grey: { colour: "#6e7278", dark: [0.28, 0.29, 0.31], light: [0.46, 0.48, 0.51] },
9
+ dark: { colour: "#3a342e", dark: [0.08, 0.07, 0.06], light: [0.2, 0.18, 0.16] },
10
+ mid: { colour: "#8a8174", dark: [0.38, 0.34, 0.28], light: [0.62, 0.58, 0.5] },
11
+ bright: { colour: "#d5d8dc", dark: [0.72, 0.74, 0.77], light: [0.94, 0.95, 0.97] },
12
+ };
13
+ /**
14
+ * One surface shader for every estimated rock. `uRockSeed`, `uRockDark` and `uRockLight` are that moon's own, set on its material, so the page compiles this once.
15
+ */
16
+ export const ESTIMATED_SURFACE = `
17
+ uniform float uRockSeed;
18
+ uniform vec3 uRockDark;
19
+ uniform vec3 uRockLight;
20
+ vec4 surface(vec3 p) {
21
+ float h1 = craters(p, 3.0, 0.85, uRockSeed);
22
+ float h2 = craters(p, 8.0, 0.55, uRockSeed + 7.0) * 0.6;
23
+ float h3 = craters(p, 18.0, 0.40, uRockSeed + 3.0) * 0.3;
24
+ float h = h1 + h2 + h3;
25
+ float hr = h1 * reliefShare(p, 3.0) + h2 * reliefShare(p, 8.0) + h3 * reliefShare(p, 18.0);
26
+ float tone = fbm(p * 2.4 + uRockSeed);
27
+ vec3 col = mix(uRockDark, uRockLight, smoothstep(0.25, 0.75, tone));
28
+ col *= 1.0 + 0.9 * h;
29
+ return vec4(col, hr);
30
+ }`;
31
+ /** The albedo class: dark under 0.08, mid under 0.25, bright from there, and grey when the albedo is unknown. */
32
+ export function albedoTone(albedo) {
33
+ if (albedo === null || !Number.isFinite(albedo))
34
+ return "grey";
35
+ if (albedo < 0.08)
36
+ return "dark";
37
+ if (albedo < 0.25)
38
+ return "mid";
39
+ return "bright";
40
+ }
41
+ const plain = (value) => value.toLowerCase().normalize("NFKD").replace(/[\u0300-\u036f]/g, "").replace(/[^a-z0-9]/g, "");
42
+ function hashId(id) {
43
+ let h = 2166136261;
44
+ for (let i = 0; i < id.length; i += 1) {
45
+ h ^= id.charCodeAt(i);
46
+ h = Math.imul(h, 16777619);
47
+ }
48
+ return h >>> 0;
49
+ }
50
+ function hash01(seed, i, salt) {
51
+ let h = Math.imul(seed ^ Math.imul(i + 1, 0x9e3779b1), 0x85ebca6b) ^ Math.imul(salt, 0xc2b2ae35);
52
+ h = Math.imul(h ^ (h >>> 16), 0x7feb352d);
53
+ h = Math.imul(h ^ (h >>> 15), 0x846ca68b);
54
+ return ((h ^ (h >>> 16)) >>> 0) / 4294967296;
55
+ }
56
+ /** A lump in about -1 to 1, the same for the same seed and direction. */
57
+ function noise(seed, x, y, z) {
58
+ let n = 0;
59
+ let w = 0;
60
+ for (let i = 0; i < 4; i += 1) {
61
+ const cx = hash01(seed, i, 1) * 2 - 1;
62
+ const cy = hash01(seed, i, 2) * 2 - 1;
63
+ const cz = hash01(seed, i, 3) * 2 - 1;
64
+ const amp = 0.55 + hash01(seed, i, 4);
65
+ n += (amp * (x * cx + y * cy + z * cz)) / (Math.hypot(cx, cy, cz) || 1);
66
+ w += amp;
67
+ }
68
+ return n / w;
69
+ }
70
+ const clampLump = (value) => Math.min(1.7, Math.max(0.45, value));
71
+ /** Turn a direction by this rock's seed, so the long axis does not point the same way on every moon. */
72
+ function spin(seed, x, y, z) {
73
+ const yaw = hash01(seed, 8, 31) * Math.PI * 2;
74
+ const pitch = (hash01(seed, 9, 32) - 0.5) * Math.PI;
75
+ const roll = hash01(seed, 10, 33) * Math.PI * 2;
76
+ const cy = Math.cos(yaw);
77
+ const sy = Math.sin(yaw);
78
+ const x1 = cy * x + sy * z;
79
+ const z1 = -sy * x + cy * z;
80
+ const cp = Math.cos(pitch);
81
+ const sp = Math.sin(pitch);
82
+ const y2 = cp * y - sp * z1;
83
+ const z2 = sp * y + cp * z1;
84
+ const cr = Math.cos(roll);
85
+ const sr = Math.sin(roll);
86
+ return [cr * x1 - sr * y2, sr * x1 + cr * y2, z2];
87
+ }
88
+ /** How round the rock is: 0 is a potato (under 100 km), 1 is a globe (200 km and up), and the sizes between blend. */
89
+ function roundness(radiusKm) {
90
+ if (radiusKm >= 200)
91
+ return 1;
92
+ if (radiusKm <= 100)
93
+ return 0;
94
+ return (radiusKm - 100) / 100;
95
+ }
96
+ /**
97
+ * The three axes, longest first. A small moon is about 1 : 0.6–0.9 : 0.5–0.8, from its seed. A large one is 1 : 1 : 1.
98
+ */
99
+ function axesOf(seed, radiusKm) {
100
+ const round = roundness(radiusKm);
101
+ const b0 = 0.6 + 0.3 * hash01(seed, 1, 21);
102
+ const c0 = 0.5 + 0.3 * hash01(seed, 2, 22);
103
+ const b = Math.max(b0, c0);
104
+ const c = Math.min(b0, c0);
105
+ return [1, b + (1 - b) * round, c + (1 - c) * round];
106
+ }
107
+ /** The radius of an ellipsoid in a direction, as a fraction of its longest axis. */
108
+ function ellipsoid(x, y, z, axes) {
109
+ const t = (x * x) / (axes[0] * axes[0]) + (y * y) / (axes[1] * axes[1]) + (z * z) / (axes[2] * axes[2]);
110
+ return t > 0 ? 1 / Math.sqrt(t) : 1;
111
+ }
112
+ /**
113
+ * A table of radii whose mean is `radiusKm`. A small moon is elongated, a large one nearly round, and both carry the same lumps. The poles are one radius each.
114
+ * The steps fit in 16 bits: the unit is chosen from twelve times the radius, which covers a potato's gap between neighbours.
115
+ */
116
+ function shapeOf(seed, radiusKm) {
117
+ const lon = 16;
118
+ const lat = 8;
119
+ const rows = lat + 1;
120
+ const axes = axesOf(seed, radiusKm);
121
+ const lump = 0.35 * (1 - roundness(radiusKm));
122
+ const raw = [];
123
+ const at = (x, y, z) => {
124
+ const [rx, ry, rz] = spin(seed, x, y, z);
125
+ return clampLump(1 + lump * noise(seed, rx, ry, rz)) * ellipsoid(rx, ry, rz, axes);
126
+ };
127
+ for (let row = 0; row < rows; row += 1) {
128
+ const phi = (Math.PI * row) / lat;
129
+ const st = Math.sin(phi);
130
+ const z = Math.cos(phi);
131
+ const pole = row === 0 || row === lat ? at(0, 0, row === 0 ? 1 : -1) : undefined;
132
+ for (let column = 0; column < lon; column += 1) {
133
+ if (pole !== undefined) {
134
+ raw.push(pole);
135
+ continue;
136
+ }
137
+ const theta = (2 * Math.PI * column) / lon;
138
+ raw.push(at(st * Math.cos(theta), st * Math.sin(theta), z));
139
+ }
140
+ }
141
+ const mean = raw.reduce((sum, value) => sum + value, 0) / raw.length;
142
+ const km = raw.map((value) => (value / mean) * radiusKm);
143
+ const unit = Math.max(1, Math.floor(30000 / (12 * radiusKm + 1)));
144
+ const whole = km.map((value) => Math.round(value * unit));
145
+ const steps = new Int16Array(lon * rows);
146
+ for (let row = 0; row < rows; row += 1) {
147
+ for (let column = 0; column < lon; column += 1) {
148
+ const i = row * lon + column;
149
+ const above = row > 0 ? whole[i - lon] : 0;
150
+ const left = column > 0 ? whole[i - 1] : 0;
151
+ const aboveLeft = row > 0 && column > 0 ? whole[i - lon - 1] : 0;
152
+ const predicted = column === 0 ? above : row === 0 ? left : left + above - aboveLeft;
153
+ steps[i] = whole[i] - predicted;
154
+ }
155
+ }
156
+ const bytes = new Uint8Array(steps.buffer, steps.byteOffset, steps.byteLength);
157
+ let binary = "";
158
+ for (const byte of bytes)
159
+ binary += String.fromCharCode(byte);
160
+ return { lon, lat, unit, steps: btoa(binary) };
161
+ }
162
+ /** How far the longest radius is past the shortest. A potato is well above 1; a globe is near 1. */
163
+ export function rockSpread(seed, radiusKm) {
164
+ const radii = shapeRadii(shapeOf(seed, radiusKm));
165
+ let low = Number.POSITIVE_INFINITY;
166
+ let high = 0;
167
+ for (const radius of radii) {
168
+ if (radius < low)
169
+ low = radius;
170
+ if (radius > high)
171
+ high = radius;
172
+ }
173
+ return high / low;
174
+ }
175
+ function notes(size) {
176
+ if (size === "derived")
177
+ return { en: "Shape estimated. Size estimated from brightness.", ja: "形は推定。大きさは明るさからの推定" };
178
+ if (size === "unknown")
179
+ return { en: "Shape estimated. Size unknown.", ja: "形は推定。大きさは不明" };
180
+ return { en: "Shape estimated.", ja: "形は推定" };
181
+ }
182
+ let byKey;
183
+ function index() {
184
+ if (byKey !== undefined)
185
+ return byKey;
186
+ byKey = new Map();
187
+ for (const row of ESTIMATED_MOONS)
188
+ byKey.set(plain(row[2]), row);
189
+ for (const row of ESTIMATED_MOONS)
190
+ byKey.set(plain(row[0]), row);
191
+ return byKey;
192
+ }
193
+ const cache = new Map();
194
+ function build(row) {
195
+ const [id, hostName, label, tenths, sizeCode, albedoThousandths] = row;
196
+ const host = hostName;
197
+ const sizeFrom = SIZES[sizeCode];
198
+ const radiusKm = tenths > 0 ? tenths / 10 : 1;
199
+ const albedo = albedoThousandths < 0 ? null : albedoThousandths / 1000;
200
+ const tone = albedoTone(albedo);
201
+ const paint = TONES[tone];
202
+ const seed = hashId(id);
203
+ const table = shapeOf(seed, radiusKm);
204
+ const facts = {
205
+ id: host,
206
+ kind: "moon",
207
+ parent: host,
208
+ name: { en: label, ja: label },
209
+ radiusKm,
210
+ flattening: 0,
211
+ tiltDeg: 0,
212
+ pole: { ra: hash01(seed, 3, 41) * 360, dec: 30 + hash01(seed, 4, 42) * 60 },
213
+ prime: { w0: hash01(seed, 5, 43) * 360, rate: 360 },
214
+ rotationHours: 24,
215
+ colour: paint.colour,
216
+ };
217
+ const look = {
218
+ id: host,
219
+ surface: ESTIMATED_SURFACE,
220
+ relief: 0.85,
221
+ ambient: 0.08,
222
+ wrap: 0.08,
223
+ detail: { amount: 0.2, stretch: 1 },
224
+ rock: { seed: (seed % 10000) / 100, dark: paint.dark, light: paint.light },
225
+ shape: () => Promise.resolve(table),
226
+ };
227
+ return { id, host, label, radiusKm, sizeFrom, albedo, tone, colour: paint.colour, seed, facts, look, note: notes(sizeFrom), line: { en: `${label}*`, ja: `${label}*` }, legend: { en: "* shape or size estimated", ja: "* 形や大きさは推定" }, table };
228
+ }
229
+ /**
230
+ * The estimated rock for a catalogue id, a name or a designation (`himalia`, `Himalia`, `S/2003 J 18`). `null` for a body the package draws in full, and for a name that is not a moon.
231
+ *
232
+ * @param query - The catalogue id, name or designation.
233
+ * @returns The rock, or `null`.
234
+ */
235
+ export function estimatedRock(query) {
236
+ const key = plain(query);
237
+ if (key === "" || key in BODIES)
238
+ return null;
239
+ const row = index().get(key);
240
+ if (row === undefined)
241
+ return null;
242
+ let rock = cache.get(row[0]);
243
+ if (rock === undefined) {
244
+ rock = build(row);
245
+ cache.set(row[0], rock);
246
+ }
247
+ return rock;
248
+ }
249
+ /**
250
+ * The line drawing of an estimated rock: a smooth outline of its guessed shape, lighter on one side. Not a body's line drawing.
251
+ *
252
+ * @param rock - The rock.
253
+ * @param size - The picture's width in pixels.
254
+ * @returns The SVG.
255
+ */
256
+ const shade = (hex, toward, amount) => {
257
+ const n = Number.parseInt(hex.slice(1), 16);
258
+ const channel = (shift) => {
259
+ const value = (n >> shift) & 255;
260
+ return Math.round(value + (toward - value) * amount);
261
+ };
262
+ return `#${((channel(16) << 16) | (channel(8) << 8) | channel(0)).toString(16).padStart(6, "0")}`;
263
+ };
264
+ export function estimatedRockLine(rock, size) {
265
+ const span = Math.max(32, Math.round(size));
266
+ const radii = shapeRadii(rock.table);
267
+ const row = rock.table.lat / 2;
268
+ const lon = rock.table.lon;
269
+ const steps = 96;
270
+ const cx = span / 2;
271
+ const cy = span / 2;
272
+ const reach = span * 0.36;
273
+ const radiusAt = (t) => {
274
+ const x = (((t % 1) + 1) % 1) * lon;
275
+ const i = Math.floor(x) % lon;
276
+ const f = x - Math.floor(x);
277
+ const smooth = f * f * (3 - 2 * f);
278
+ const a = radii[row * lon + i] / rock.radiusKm;
279
+ const b = radii[row * lon + ((i + 1) % lon)] / rock.radiusKm;
280
+ return a + (b - a) * smooth;
281
+ };
282
+ let d = "";
283
+ for (let k = 0; k < steps; k += 1) {
284
+ const t = k / steps;
285
+ const radius = radiusAt(t);
286
+ const theta = 2 * Math.PI * t;
287
+ const x = cx + Math.cos(theta) * reach * radius;
288
+ const y = cy + Math.sin(theta) * reach * radius;
289
+ d += `${k === 0 ? "M" : "L"}${x.toFixed(1)} ${y.toFixed(1)}`;
290
+ }
291
+ const id = `rock-${rock.id}`;
292
+ return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${span} ${span}" role="presentation"><defs><radialGradient id="${id}" cx="36%" cy="32%" r="78%"><stop offset="0" stop-color="${shade(rock.colour, 255, 0.45)}"/><stop offset="1" stop-color="${shade(rock.colour, 0, 0.5)}"/></radialGradient></defs><path d="${d}Z" fill="url(#${id})"/></svg>`;
293
+ }
@@ -105,6 +105,15 @@ export interface BodyLook {
105
105
  * ellipsoid of `axesKm`. Until it arrives the ellipsoid is drawn.
106
106
  */
107
107
  shape?: () => Promise<ShapeTable>;
108
+ /**
109
+ * An estimated rock shares one surface shader. These uniforms are that moon's own: a seed so the craters differ, and the dark and light colours of its albedo class.
110
+ * Only a look whose `surface` reads `uRockSeed`, `uRockDark` and `uRockLight` sets this.
111
+ */
112
+ rock?: {
113
+ seed: number;
114
+ dark: readonly [number, number, number];
115
+ light: readonly [number, number, number];
116
+ };
108
117
  /** Draw the painted map in the realistic look too: for a body with no honest drawing from numbers (the Earth's continents). */
109
118
  mapped?: boolean;
110
119
  }
@@ -29,7 +29,9 @@ export function takeRenderer(onContext) {
29
29
  listeners.add(onContext);
30
30
  shared ??= createSharedRenderer({
31
31
  load: () => loader(),
32
- stillSize: 480,
32
+ // A still is this many pixels on its long side, and the page keeps 32 MB of them. A square still is that side squared, times four bytes.
33
+ // 160 holds every moon of Saturn (293) with the planet, which a 480-pixel still cannot: those are about 0.9 MB each, and past the budget the picture is dropped and the view shows a broken image.
34
+ stillSize: 160,
33
35
  onContext: (state) => {
34
36
  for (const listener of listeners)
35
37
  listener(state);
@@ -11,7 +11,7 @@ import { type BodyMapSource } from "../door/map.ts";
11
11
  * console.log(TEXTURE_BASE);
12
12
  * ```
13
13
  */
14
- export declare const TEXTURE_BASE = "https://cdn.jsdelivr.net/npm/@johnmorrisdotca/wakusei-planets@0.54.0/dist/textures/";
14
+ export declare const TEXTURE_BASE = "https://cdn.jsdelivr.net/npm/@johnmorrisdotca/wakusei-planets@0.73.0/dist/textures/";
15
15
  /**
16
16
  * 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
17
17
  * never zooms in never fetches them. Set `texture-far-base` on an element to serve them yourself.
@@ -23,7 +23,7 @@ export declare const TEXTURE_BASE = "https://cdn.jsdelivr.net/npm/@johnmorrisdot
23
23
  * console.log(TEXTURE_FAR_BASE);
24
24
  * ```
25
25
  */
26
- export declare const TEXTURE_FAR_BASE = "https://cdn.jsdelivr.net/gh/johnmorrisdotca/wakusei-planets@v0.54.0/textures/";
26
+ export declare const TEXTURE_FAR_BASE = "https://cdn.jsdelivr.net/gh/johnmorrisdotca/wakusei-planets@v0.73.0/textures/";
27
27
  /**
28
28
  * 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:
29
29
  * 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.
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "0.54.0";
1
+ export declare const VERSION = "0.73.0";
package/dist/version.js CHANGED
@@ -1 +1,2 @@
1
- export const VERSION = "0.54.0";
1
+ // The published package version. Snippets and texture addresses name it. test/version.test.ts keeps it equal to package.json.
2
+ export const VERSION = "0.73.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/wakusei-planets",
3
- "version": "0.71.0",
3
+ "version": "0.73.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",