@johnmorrisdotca/wakusei-planets 0.39.0 → 0.41.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 +17 -0
- package/NOTICE.md +1 -1
- package/README.md +3 -3
- package/dist/element.d.ts +1 -1
- package/dist/element.js +1 -1
- package/dist/ground-track.d.ts +80 -0
- package/dist/ground-track.js +137 -0
- package/dist/paint/trail.d.ts +80 -0
- package/dist/paint/trail.js +322 -0
- package/dist/scene/body.d.ts +1 -0
- package/dist/scene/body.js +2 -2
- package/dist/scene/glsl.d.ts +2 -2
- package/dist/scene/glsl.js +4 -2
- package/dist/scene/name-label.d.ts +56 -3
- package/dist/scene/name-label.js +72 -7
- package/dist/scene/painter.d.ts +2 -1
- package/dist/scene/painter.js +5 -3
- package/dist/scene/textures.d.ts +2 -2
- package/dist/space/element.d.ts +24 -0
- package/dist/space/element.js +51 -6
- package/dist/space/index.d.ts +1 -1
- package/dist/space/index.js +1 -1
- package/dist/space/labels.d.ts +32 -0
- package/dist/space/labels.js +39 -0
- package/dist/space/paint-overlay.d.ts +11 -0
- package/dist/space/paint-overlay.js +84 -0
- package/dist/space/scene.d.ts +6 -0
- package/dist/space/scene.js +25 -0
- package/dist/textures/pluto-1024.webp +0 -0
- package/dist/textures/pluto-512.webp +0 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +6 -1
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
// One fixed equirectangular buffer for a craft's ground track. A frame stamps only the ground just crossed.
|
|
2
|
+
// The picture fades a stamp by the minute stored in it. Nothing is rebuilt at rest and the buffer never grows.
|
|
3
|
+
import { BODIES } from "../facts.js";
|
|
4
|
+
import { coverageAngleRad, splitTrail, trackSteps } from "../ground-track.js";
|
|
5
|
+
import { orientation } from "../scene/orient.js";
|
|
6
|
+
import { eclipticToScene } from "../space/frame.js";
|
|
7
|
+
import { cross, dot } from "../vec.js";
|
|
8
|
+
const DAY = 86_400_000;
|
|
9
|
+
const LENGTH_MS = { "1d": DAY, "1w": 7 * DAY, "1m": 30 * DAY };
|
|
10
|
+
/** Two minutes a tick, so a month (21,600 ticks) fits in a signed 16-bit age. */
|
|
11
|
+
const TICK = 120_000;
|
|
12
|
+
/** Equirectangular, north at the first row, longitude −180 at the first column. */
|
|
13
|
+
export const PAINT_W = 1024;
|
|
14
|
+
export const PAINT_H = 512;
|
|
15
|
+
export const paintLengthOf = (length) => (length === "1w" || length === "1m" ? length : "1d");
|
|
16
|
+
export const paintSwathOf = (swath) => (swath === "coverage" ? "coverage" : "track");
|
|
17
|
+
/** Ticks since the epoch, in 16 bits. One tick is two minutes, so a month fits in a signed 16-bit age. */
|
|
18
|
+
export const paintMinute = (ms) => Math.round(ms / TICK) & 0xffff;
|
|
19
|
+
/**
|
|
20
|
+
* Where a craft is on a body's surface. `eclipticKm` is from the body's centre, ecliptic J2000, the same axes `/orbiters` uses.
|
|
21
|
+
*
|
|
22
|
+
* @param body - The body the craft goes round.
|
|
23
|
+
* @param eclipticKm - The place, in kilometres from the body's centre.
|
|
24
|
+
* @param ms - The instant, so the body has turned under the craft.
|
|
25
|
+
* @returns Longitude, latitude and height.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { orientation } from "@johnmorrisdotca/wakusei-planets/painter";
|
|
30
|
+
*
|
|
31
|
+
* console.log(orientation("moon", Date.UTC(2026, 10, 11)).pole.length === 3);
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export function groundUnder(body, eclipticKm, ms) {
|
|
35
|
+
const scene = eclipticToScene(eclipticKm);
|
|
36
|
+
const { pole, meridian } = orientation(body, ms);
|
|
37
|
+
const east = cross(pole, meridian);
|
|
38
|
+
const x = dot(scene, east);
|
|
39
|
+
const y = dot(scene, pole);
|
|
40
|
+
const z = dot(scene, meridian);
|
|
41
|
+
const radius = Math.hypot(x, y, z) || 1;
|
|
42
|
+
return {
|
|
43
|
+
lon: (Math.atan2(x, z) * 180) / Math.PI,
|
|
44
|
+
lat: (Math.asin(Math.max(-1, Math.min(1, y / radius))) * 180) / Math.PI,
|
|
45
|
+
heightKm: radius - BODIES[body].radiusKm,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
const paintPixel = (lon, lat) => {
|
|
49
|
+
const u = lon / 360 + 0.5;
|
|
50
|
+
const v = 0.5 - lat / 180;
|
|
51
|
+
return { x: Math.max(0, Math.min(PAINT_W - 1, Math.floor(u * PAINT_W))), y: Math.max(0, Math.min(PAINT_H - 1, Math.floor(v * PAINT_H))) };
|
|
52
|
+
};
|
|
53
|
+
const write = (data, x, y, minute) => {
|
|
54
|
+
if (x < 0 || y < 0 || x >= PAINT_W || y >= PAINT_H)
|
|
55
|
+
return false;
|
|
56
|
+
const i = (y * PAINT_W + x) * 4;
|
|
57
|
+
const fresh = data[i + 3] === 0;
|
|
58
|
+
data[i] = (minute >> 8) & 255;
|
|
59
|
+
data[i + 1] = minute & 255;
|
|
60
|
+
data[i + 2] = 255;
|
|
61
|
+
data[i + 3] = 255;
|
|
62
|
+
return fresh;
|
|
63
|
+
};
|
|
64
|
+
const stampDot = (data, point, minute) => {
|
|
65
|
+
const at = paintPixel(point.lon, point.lat);
|
|
66
|
+
return write(data, at.x, at.y, minute) ? 1 : 0;
|
|
67
|
+
};
|
|
68
|
+
const stampLine = (data, a, b, minute) => {
|
|
69
|
+
const from = paintPixel(a.lon, a.lat);
|
|
70
|
+
const to = paintPixel(b.lon, b.lat);
|
|
71
|
+
if (Math.abs(to.x - from.x) > PAINT_W / 2)
|
|
72
|
+
return 0;
|
|
73
|
+
let added = 0;
|
|
74
|
+
let x = from.x;
|
|
75
|
+
let y = from.y;
|
|
76
|
+
const dx = Math.abs(to.x - x);
|
|
77
|
+
const dy = Math.abs(to.y - y);
|
|
78
|
+
const sx = x < to.x ? 1 : -1;
|
|
79
|
+
const sy = y < to.y ? 1 : -1;
|
|
80
|
+
let err = dx - dy;
|
|
81
|
+
for (;;) {
|
|
82
|
+
if (write(data, x, y, minute))
|
|
83
|
+
added += 1;
|
|
84
|
+
if (x === to.x && y === to.y)
|
|
85
|
+
break;
|
|
86
|
+
const e2 = 2 * err;
|
|
87
|
+
if (e2 > -dy) {
|
|
88
|
+
err -= dy;
|
|
89
|
+
x += sx;
|
|
90
|
+
}
|
|
91
|
+
if (e2 < dx) {
|
|
92
|
+
err += dx;
|
|
93
|
+
y += sy;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return added;
|
|
97
|
+
};
|
|
98
|
+
const RAD = Math.PI / 180;
|
|
99
|
+
/** Every pixel within `theta` radians of `point`, row by row: each row's half-width in longitude is worked out, and a cap over the date line wraps to the other edge. */
|
|
100
|
+
const stampCap = (data, point, theta, minute) => {
|
|
101
|
+
if (!(theta > 0))
|
|
102
|
+
return stampDot(data, point, minute);
|
|
103
|
+
const deg = theta / RAD;
|
|
104
|
+
const sin1 = Math.sin(point.lat * RAD);
|
|
105
|
+
const cos1 = Math.cos(point.lat * RAD);
|
|
106
|
+
const cosTheta = Math.cos(theta);
|
|
107
|
+
const y0 = paintPixel(0, Math.min(90, point.lat + deg)).y;
|
|
108
|
+
const y1 = paintPixel(0, Math.max(-90, point.lat - deg)).y;
|
|
109
|
+
let added = 0;
|
|
110
|
+
for (let y = y0; y <= y1; y += 1) {
|
|
111
|
+
const lat = (90 - ((y + 0.5) / PAINT_H) * 180) * RAD;
|
|
112
|
+
const denominator = cos1 * Math.cos(lat);
|
|
113
|
+
const need = cosTheta - sin1 * Math.sin(lat);
|
|
114
|
+
let half;
|
|
115
|
+
if (denominator < 1e-9) {
|
|
116
|
+
if (need > 0)
|
|
117
|
+
continue;
|
|
118
|
+
half = 180;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
const ratio = need / denominator;
|
|
122
|
+
if (ratio > 1)
|
|
123
|
+
continue;
|
|
124
|
+
half = ratio <= -1 ? 180 : Math.acos(ratio) / RAD;
|
|
125
|
+
}
|
|
126
|
+
const first = Math.floor(((point.lon - half + 180) / 360) * PAINT_W);
|
|
127
|
+
const last = Math.floor(((point.lon + half + 180) / 360) * PAINT_W);
|
|
128
|
+
if (last - first >= PAINT_W - 1) {
|
|
129
|
+
for (let x = 0; x < PAINT_W; x += 1)
|
|
130
|
+
if (write(data, x, y, minute))
|
|
131
|
+
added += 1;
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
for (let x = first; x <= last; x += 1)
|
|
135
|
+
if (write(data, ((x % PAINT_W) + PAINT_W) % PAINT_W, y, minute))
|
|
136
|
+
added += 1;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return added;
|
|
140
|
+
};
|
|
141
|
+
const stampGround = (data, swath, ground, radiusKm, previous, minute) => {
|
|
142
|
+
const theta = swath === "coverage" ? coverageAngleRad(ground.heightKm, radiusKm) : 0;
|
|
143
|
+
// a line is made of steps a third of a degree long; a swath of caps that overlap, a step of about seven tenths of the cap's radius
|
|
144
|
+
const points = previous === null ? [ground] : trackSteps(previous, ground, swath === "coverage" ? Math.max(1, (theta / RAD) * 0.7) : 0.35);
|
|
145
|
+
let added = 0;
|
|
146
|
+
let cursor = previous;
|
|
147
|
+
for (const point of points) {
|
|
148
|
+
if (swath === "coverage")
|
|
149
|
+
added += stampCap(data, point, theta, minute);
|
|
150
|
+
else if (cursor === null)
|
|
151
|
+
added += stampDot(data, point, minute);
|
|
152
|
+
else
|
|
153
|
+
for (const [from, to] of splitTrail(cursor, point))
|
|
154
|
+
added += stampLine(data, from, to, minute);
|
|
155
|
+
cursor = point;
|
|
156
|
+
}
|
|
157
|
+
return { added, at: ground };
|
|
158
|
+
};
|
|
159
|
+
const ageOf = (then, now) => {
|
|
160
|
+
let age = now - then;
|
|
161
|
+
if (age < -32768)
|
|
162
|
+
age += 65536;
|
|
163
|
+
if (age > 32768)
|
|
164
|
+
age -= 65536;
|
|
165
|
+
return age;
|
|
166
|
+
};
|
|
167
|
+
/** The longest gap between two samples of the craft's place, so a line between them is the short way round the same great circle: a twentieth of a trip, and no more than four minutes (the body turns under it). */
|
|
168
|
+
const MAX_GAP = 240_000;
|
|
169
|
+
const MIN_GAP = 30_000;
|
|
170
|
+
/** Samples taken at most in one frame, and at most to lay down a whole length at once (a swath costs more a sample than a line). A gap is never more than a third of a trip, however many that takes. */
|
|
171
|
+
const FRAME_SAMPLES = 600;
|
|
172
|
+
const REBUILD_SAMPLES = { track: 4000, coverage: 1500 };
|
|
173
|
+
const gapOf = (periodMs) => (periodMs !== undefined && periodMs > 0 ? Math.max(MIN_GAP, Math.min(MAX_GAP, periodMs / 20)) : TICK);
|
|
174
|
+
/**
|
|
175
|
+
* The trail buffer. One for a view. `set` with another craft clears what was painted.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* import { splitDateLine } from "@johnmorrisdotca/wakusei-planets/ground-track";
|
|
180
|
+
*
|
|
181
|
+
* console.log(splitDateLine({ lon: 170, lat: 0 }, { lon: -170, lat: 4 }).length === 2);
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
export function createPaintTrail() {
|
|
185
|
+
// the two megabytes are taken when the first craft is painted, not when the view is made
|
|
186
|
+
let buffer = null;
|
|
187
|
+
const grab = () => (buffer ??= new Uint8Array(PAINT_W * PAINT_H * 4));
|
|
188
|
+
let spec = null;
|
|
189
|
+
let pixels = 0;
|
|
190
|
+
let lastMs = Number.NaN;
|
|
191
|
+
/** The last point stamped and the way the clock was going then, so a line carries on from it. */
|
|
192
|
+
let previous = null;
|
|
193
|
+
let heading = 0;
|
|
194
|
+
/** The buffer was emptied since the last step: the picture must be sent even if nothing new was stamped. */
|
|
195
|
+
let stale = false;
|
|
196
|
+
const reset = () => {
|
|
197
|
+
buffer?.fill(0);
|
|
198
|
+
pixels = 0;
|
|
199
|
+
lastMs = Number.NaN;
|
|
200
|
+
previous = null;
|
|
201
|
+
heading = 0;
|
|
202
|
+
stale = true;
|
|
203
|
+
};
|
|
204
|
+
const dropOld = (data, now, span) => {
|
|
205
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
206
|
+
if (data[i + 3] === 0)
|
|
207
|
+
continue;
|
|
208
|
+
const then = ((data[i] ?? 0) << 8) | (data[i + 1] ?? 0);
|
|
209
|
+
const age = ageOf(then, now);
|
|
210
|
+
// a tick of slack at each end, for the rounding of the stamp's minute
|
|
211
|
+
if (age > span + 1 || age < -1) {
|
|
212
|
+
data[i] = 0;
|
|
213
|
+
data[i + 1] = 0;
|
|
214
|
+
data[i + 2] = 0;
|
|
215
|
+
data[i + 3] = 0;
|
|
216
|
+
pixels -= 1;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
const self = {
|
|
221
|
+
get data() {
|
|
222
|
+
return grab();
|
|
223
|
+
},
|
|
224
|
+
width: PAINT_W,
|
|
225
|
+
height: PAINT_H,
|
|
226
|
+
get pixels() {
|
|
227
|
+
return pixels;
|
|
228
|
+
},
|
|
229
|
+
get on() {
|
|
230
|
+
return spec !== null;
|
|
231
|
+
},
|
|
232
|
+
get length() {
|
|
233
|
+
return spec?.length ?? "1d";
|
|
234
|
+
},
|
|
235
|
+
get swath() {
|
|
236
|
+
return spec?.swath ?? "track";
|
|
237
|
+
},
|
|
238
|
+
get id() {
|
|
239
|
+
return spec?.id ?? "";
|
|
240
|
+
},
|
|
241
|
+
get spanMinutes() {
|
|
242
|
+
return LENGTH_MS[spec?.length ?? "1d"] / TICK;
|
|
243
|
+
},
|
|
244
|
+
set(next) {
|
|
245
|
+
if (!next) {
|
|
246
|
+
self.clear();
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
const id = String(next.id);
|
|
250
|
+
const length = paintLengthOf(next.length);
|
|
251
|
+
const swath = paintSwathOf(next.swath);
|
|
252
|
+
if (spec && spec.id === id && spec.length === length && spec.swath === swath)
|
|
253
|
+
return;
|
|
254
|
+
reset();
|
|
255
|
+
spec = { id, length, swath };
|
|
256
|
+
},
|
|
257
|
+
clear() {
|
|
258
|
+
reset();
|
|
259
|
+
spec = null;
|
|
260
|
+
},
|
|
261
|
+
step(ms, radiusKm, sample, periodMs) {
|
|
262
|
+
if (!spec) {
|
|
263
|
+
const upload = stale;
|
|
264
|
+
stale = false;
|
|
265
|
+
return upload;
|
|
266
|
+
}
|
|
267
|
+
if (!Number.isFinite(ms))
|
|
268
|
+
return false;
|
|
269
|
+
if (Number.isFinite(lastMs) && ms === lastMs) {
|
|
270
|
+
const upload = stale;
|
|
271
|
+
stale = false;
|
|
272
|
+
return upload;
|
|
273
|
+
}
|
|
274
|
+
const data = grab();
|
|
275
|
+
const length = LENGTH_MS[spec.length];
|
|
276
|
+
const gap = gapOf(periodMs);
|
|
277
|
+
let dirty = stale;
|
|
278
|
+
stale = false;
|
|
279
|
+
/** Stamp the craft's place at each of `times`, one line from the one before. */
|
|
280
|
+
const lay = (times, way) => {
|
|
281
|
+
if (way !== heading)
|
|
282
|
+
previous = null;
|
|
283
|
+
heading = way;
|
|
284
|
+
for (const at of times) {
|
|
285
|
+
const ground = sample(at);
|
|
286
|
+
if (!ground) {
|
|
287
|
+
previous = null;
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
const stamped = stampGround(data, spec.swath, ground, radiusKm, previous, paintMinute(at));
|
|
291
|
+
pixels += stamped.added;
|
|
292
|
+
previous = stamped.at;
|
|
293
|
+
dirty = true;
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
const between = (from, to, cap) => {
|
|
297
|
+
const span = Math.abs(to - from);
|
|
298
|
+
const count = Math.max(Math.min(cap, Math.max(1, Math.ceil(span / gap))), periodMs === undefined ? 1 : Math.ceil(span / (periodMs / 3)));
|
|
299
|
+
return Array.from({ length: count + 1 }, (_, i) => from + ((to - from) * i) / count);
|
|
300
|
+
};
|
|
301
|
+
if (!Number.isFinite(lastMs) || Math.abs(ms - lastMs) > length) {
|
|
302
|
+
// the first moment, or a jump: the whole length is laid down again, oldest first
|
|
303
|
+
data.fill(0);
|
|
304
|
+
pixels = 0;
|
|
305
|
+
previous = null;
|
|
306
|
+
heading = 1;
|
|
307
|
+
lay(between(ms - length, ms, REBUILD_SAMPLES[spec.swath]), 1);
|
|
308
|
+
}
|
|
309
|
+
else if (ms > lastMs) {
|
|
310
|
+
lay(between(lastMs, ms, FRAME_SAMPLES), 1);
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
// backwards: the newer end falls away below, and the older end, which the clock has just uncovered, is laid down
|
|
314
|
+
lay(between(lastMs - length, ms - length, FRAME_SAMPLES), -1);
|
|
315
|
+
}
|
|
316
|
+
dropOld(data, paintMinute(ms), length / TICK);
|
|
317
|
+
lastMs = ms;
|
|
318
|
+
return dirty || pixels > 0;
|
|
319
|
+
},
|
|
320
|
+
};
|
|
321
|
+
return self;
|
|
322
|
+
}
|
package/dist/scene/body.d.ts
CHANGED
package/dist/scene/body.js
CHANGED
|
@@ -371,7 +371,7 @@ export function createBodyItem(options) {
|
|
|
371
371
|
return;
|
|
372
372
|
}
|
|
373
373
|
if (label === undefined) {
|
|
374
|
-
label = createNameLabel(T, state.label.text, state.label, look.emissive === true, () => options.redraw());
|
|
374
|
+
label = createNameLabel(T, state.label.text, state.label, look.emissive === true, () => options.redraw(), facts.colour);
|
|
375
375
|
mesh.add(label.object);
|
|
376
376
|
}
|
|
377
377
|
else {
|
|
@@ -453,7 +453,7 @@ export function createBodyItem(options) {
|
|
|
453
453
|
},
|
|
454
454
|
get label() {
|
|
455
455
|
const inner = label?.object.children[0];
|
|
456
|
-
return label === undefined || latest.label === undefined || latest.label === null ? null : { shown: label.ready && inner !== undefined && inner.visible, text: latest.label.text, turn: inner?.rotation.y ?? 0 };
|
|
456
|
+
return label === undefined || latest.label === undefined || latest.label === null ? null : { shown: label.ready && inner !== undefined && inner.visible, text: latest.label.text, turn: inner?.rotation.y ?? 0, look: label.look };
|
|
457
457
|
},
|
|
458
458
|
resize(w, h) {
|
|
459
459
|
viewWidth = w;
|
package/dist/scene/glsl.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare const VERTEX = "\nvarying vec3 vObj;\nvarying vec3 vN;\nvarying vec3 vW;\nvoid main() {\n vObj = vec3(position.z, position.x, position.y);\n vN = normalize(normalMatrix * normal);\n vec4 w = modelMatrix * vec4(position, 1.0);\n vW = w.xyz;\n gl_Position = projectionMatrix * viewMatrix * w;\n}";
|
|
2
2
|
/** Noise, craters and the band palette, for the surface code of a body to call. */
|
|
3
|
-
export declare const PRELUDE = "\nfloat hash13(vec3 p) { p = fract(p * 0.1031); p += dot(p, p.zyx + 31.32); return fract((p.x + p.y) * p.z); }\nvec3 hash33(vec3 p) { p = fract(p * vec3(0.1031, 0.1030, 0.0973)); p += dot(p, p.yxz + 33.33); return fract((p.xxy + p.yxx) * p.zyx); }\nfloat vnoise(vec3 p) {\n vec3 i = floor(p); vec3 f = fract(p); f = f * f * (3.0 - 2.0 * f);\n return mix(mix(mix(hash13(i), hash13(i + vec3(1,0,0)), f.x), mix(hash13(i + vec3(0,1,0)), hash13(i + vec3(1,1,0)), f.x), f.y),\n mix(mix(hash13(i + vec3(0,0,1)), hash13(i + vec3(1,0,1)), f.x), mix(hash13(i + vec3(0,1,1)), hash13(i + vec3(1,1,1)), f.x), f.y), f.z);\n}\n// Noise that knows how big a pixel is: an octave finer than the pixel can show is replaced by its average (0.5) instead of being sampled, so a small body is smooth and not a spray of pixel-sized specks.\nfloat fbm(vec3 p) { float fw = length(fwidth(p)); float a = 0.5; float s = 0.0; for (int i = 0; i < 5; i++) { s += a * mix(0.5, vnoise(p), 1.0 - smoothstep(0.3, 0.7, fw)); p = p * 2.03 + vec3(1.7, 9.2, 3.1); fw *= 2.03; a *= 0.5; } return s; }\n// Craters: a bowl and a raised rim in each cell that has one. Returns a height, negative in a bowl. A crater smaller than about two pixels fades out (it would only be noise).\nfloat craters(vec3 p, float freq, float amp, float seed) {\n vec3 q = p * freq + seed; vec3 ip = floor(q); float h = 0.0;\n float fw = length(fwidth(q));\n for (int x = -1; x <= 1; x++) for (int y = -1; y <= 1; y++) for (int z = -1; z <= 1; z++) {\n vec3 c = ip + vec3(float(x), float(y), float(z));\n float on = step(0.52, hash13(c + 3.7));\n vec3 ctr = c + hash33(c);\n float rad = mix(0.12, 0.36, hash13(c + 17.3));\n float d = length(q - ctr) / rad;\n float bowl = d < 1.0 ? (d * d - 1.0) : 0.0;\n float rim = d >= 1.0 ? exp(-pow((d - 1.0) * 5.0, 2.0)) * 0.3 * smoothstep(1.0, 1.08, d) : 0.0;\n h += on * amp * rad * (bowl + rim) * (1.0 - smoothstep(0.
|
|
3
|
+
export declare const PRELUDE = "\nfloat hash13(vec3 p) { p = fract(p * 0.1031); p += dot(p, p.zyx + 31.32); return fract((p.x + p.y) * p.z); }\nvec3 hash33(vec3 p) { p = fract(p * vec3(0.1031, 0.1030, 0.0973)); p += dot(p, p.yxz + 33.33); return fract((p.xxy + p.yxx) * p.zyx); }\nfloat vnoise(vec3 p) {\n vec3 i = floor(p); vec3 f = fract(p); f = f * f * (3.0 - 2.0 * f);\n return mix(mix(mix(hash13(i), hash13(i + vec3(1,0,0)), f.x), mix(hash13(i + vec3(0,1,0)), hash13(i + vec3(1,1,0)), f.x), f.y),\n mix(mix(hash13(i + vec3(0,0,1)), hash13(i + vec3(1,0,1)), f.x), mix(hash13(i + vec3(0,1,1)), hash13(i + vec3(1,1,1)), f.x), f.y), f.z);\n}\n// Noise that knows how big a pixel is: an octave finer than the pixel can show is replaced by its average (0.5) instead of being sampled, so a small body is smooth and not a spray of pixel-sized specks.\nfloat fbm(vec3 p) { float fw = length(fwidth(p)); float a = 0.5; float s = 0.0; for (int i = 0; i < 5; i++) { s += a * mix(0.5, vnoise(p), 1.0 - smoothstep(0.3, 0.7, fw)); p = p * 2.03 + vec3(1.7, 9.2, 3.1); fw *= 2.03; a *= 0.5; } return s; }\n// Craters: a bowl and a raised rim in each cell that has one. Returns a height, negative in a bowl. A crater smaller than about two pixels fades out (it would only be noise).\nfloat craters(vec3 p, float freq, float amp, float seed) {\n vec3 q = p * freq + seed; vec3 ip = floor(q); float h = 0.0;\n float fw = length(fwidth(q));\n for (int x = -1; x <= 1; x++) for (int y = -1; y <= 1; y++) for (int z = -1; z <= 1; z++) {\n vec3 c = ip + vec3(float(x), float(y), float(z));\n float on = step(0.52, hash13(c + 3.7));\n vec3 ctr = c + hash33(c);\n float rad = mix(0.12, 0.36, hash13(c + 17.3));\n float d = length(q - ctr) / rad;\n float bowl = d < 1.0 ? (d * d - 1.0) : 0.0;\n float rim = d >= 1.0 ? exp(-pow((d - 1.0) * 5.0, 2.0)) * 0.3 * smoothstep(1.0, 1.08, d) : 0.0;\n h += on * amp * rad * (bowl + rim) * (1.0 - smoothstep(0.14, 0.34, fw / rad));\n }\n return h;\n}\nfloat lon(vec3 p) { return atan(p.y, p.x); }\nfloat lat(vec3 p) { return asin(clamp(p.z, -1.0, 1.0)); }\nfloat wrapPi(float a) { return a - 6.2831853 * floor((a + 3.1415927) / 6.2831853); }\nvec3 dirOf(float lonDeg, float latDeg) { float lo = radians(lonDeg); float la = radians(latDeg); return vec3(cos(la) * cos(lo), cos(la) * sin(lo), sin(la)); }\n// One big round bowl with a rim, by longitude, latitude and angular radius in degrees. Negative inside.\nfloat bowl(vec3 p, float lonDeg, float latDeg, float radDeg, float depth) {\n float d = acos(clamp(dot(p, dirOf(lonDeg, latDeg)), -1.0, 1.0)) / radians(radDeg);\n return d < 1.0 ? depth * (d * d - 1.0) : depth * exp(-pow((d - 1.0) * 5.0, 2.0)) * 0.3 * smoothstep(1.0, 1.08, d);\n}\n// A soft oval on the body, 1 inside and 0 outside, by longitude and latitude in degrees.\nfloat oval(vec3 p, float lon0, float lat0, float rx, float ry, float soft) {\n float dx = wrapPi(lon(p) - radians(lon0)) * cos(radians(lat0)) / radians(rx);\n float dy = (lat(p) - radians(lat0)) / radians(ry);\n return 1.0 - smoothstep(1.0 - soft, 1.0, sqrt(dx * dx + dy * dy));\n}";
|
|
4
4
|
/**
|
|
5
5
|
* The generic fine detail of a close view, for the fragment shader of a body (`bodyFragment` with `detail`). It is not a measured feature of the body: it is noise, put on so that a body never goes smooth when the
|
|
6
6
|
* zoom takes it past the sharpness of its map. `gTexel` is the angle one texel of the picture being read covers (radians); where a pixel is smaller than a texel (the map is enlarged more than about one and a
|
|
@@ -45,4 +45,4 @@ export declare const GLOW_FRAGMENT = "\nprecision highp float;\nvarying vec2 vP;
|
|
|
45
45
|
*/
|
|
46
46
|
export declare const STAR_GLOW_FRAGMENT = "\nprecision highp float;\nvarying vec2 vP;\nuniform vec3 uColour;\nuniform float uStrength;\nuniform float uSpread;\nuniform float uRim;\nuniform float uProm;\nuniform float uSeed;\nuniform float uTime;\nfloat h1(float n) { return fract(sin(n * 127.1 + uSeed * 91.7) * 43758.5453); }\nfloat n1(float x) { float i = floor(x); float f = fract(x); f = f * f * (3.0 - 2.0 * f); return mix(h1(i), h1(i + 1.0), f); }\nvoid main() {\n float r = length(vP);\n if (r < 0.98) discard;\n float x = r - 1.0;\n float fade = smoothstep(3.0, 1.6, r);\n float halo = exp(-x * uSpread) * uStrength;\n float tight = exp(-x * 14.0) * uStrength * 0.9;\n float rim = exp(-x * 60.0) * uRim;\n vec3 col = uColour * (halo + tight) + mix(uColour, vec3(1.0), 0.4) * rim;\n float a = halo + tight + rim;\n if (uProm > 0.0) {\n float ang = atan(vP.y, vP.x);\n float site = smoothstep(0.55, 0.85, n1(ang * 2.6 + uSeed * 20.0));\n float drift = 0.75 + 0.25 * sin(uTime * 0.5 + ang * 4.0);\n float height = site * uProm * 0.3 * drift;\n float strands = 0.65 + 0.35 * n1(ang * 70.0 + uTime * 0.3);\n float pr = step(0.0, x) * pow(smoothstep(height, 0.0, x), 1.6) * site * strands * 0.9;\n vec3 pc = mix(vec3(1.0, 0.42, 0.26), uColour, 0.2);\n float a0 = uSeed * 6.2831853 + uTime * 0.02;\n float da = atan(sin(ang - a0), cos(ang - a0));\n float burst = pow(max(0.0, sin(uTime * 0.9 + uSeed * 40.0)), 24.0) * uProm;\n float fl = exp(-da * da * 14.0) * exp(-x * 6.0) * burst * 1.6;\n col += pc * pr + vec3(1.0, 0.92, 0.75) * fl;\n a += pr + fl;\n }\n // additive blending multiplies by alpha, so the root of the light is the alpha and the colour carries the rest: the sum arrives as written\n float s = sqrt(min(a * fade, 1.0));\n gl_FragColor = vec4(col / max(a, 1e-4) * s, s);\n}";
|
|
47
47
|
/** A cheap lit ball for a moon seen small beside its planet. */
|
|
48
|
-
export declare const BALL_FRAGMENT = "\nprecision highp float;\nuniform vec3 uLight;\nuniform vec3 uColour;\nvarying vec3 vObj;\nvarying vec3 vN;\nvarying vec3 vW;\n\nfloat hash13(vec3 p) { p = fract(p * 0.1031); p += dot(p, p.zyx + 31.32); return fract((p.x + p.y) * p.z); }\nvec3 hash33(vec3 p) { p = fract(p * vec3(0.1031, 0.1030, 0.0973)); p += dot(p, p.yxz + 33.33); return fract((p.xxy + p.yxx) * p.zyx); }\nfloat vnoise(vec3 p) {\n vec3 i = floor(p); vec3 f = fract(p); f = f * f * (3.0 - 2.0 * f);\n return mix(mix(mix(hash13(i), hash13(i + vec3(1,0,0)), f.x), mix(hash13(i + vec3(0,1,0)), hash13(i + vec3(1,1,0)), f.x), f.y),\n mix(mix(hash13(i + vec3(0,0,1)), hash13(i + vec3(1,0,1)), f.x), mix(hash13(i + vec3(0,1,1)), hash13(i + vec3(1,1,1)), f.x), f.y), f.z);\n}\n// Noise that knows how big a pixel is: an octave finer than the pixel can show is replaced by its average (0.5) instead of being sampled, so a small body is smooth and not a spray of pixel-sized specks.\nfloat fbm(vec3 p) { float fw = length(fwidth(p)); float a = 0.5; float s = 0.0; for (int i = 0; i < 5; i++) { s += a * mix(0.5, vnoise(p), 1.0 - smoothstep(0.3, 0.7, fw)); p = p * 2.03 + vec3(1.7, 9.2, 3.1); fw *= 2.03; a *= 0.5; } return s; }\n// Craters: a bowl and a raised rim in each cell that has one. Returns a height, negative in a bowl. A crater smaller than about two pixels fades out (it would only be noise).\nfloat craters(vec3 p, float freq, float amp, float seed) {\n vec3 q = p * freq + seed; vec3 ip = floor(q); float h = 0.0;\n float fw = length(fwidth(q));\n for (int x = -1; x <= 1; x++) for (int y = -1; y <= 1; y++) for (int z = -1; z <= 1; z++) {\n vec3 c = ip + vec3(float(x), float(y), float(z));\n float on = step(0.52, hash13(c + 3.7));\n vec3 ctr = c + hash33(c);\n float rad = mix(0.12, 0.36, hash13(c + 17.3));\n float d = length(q - ctr) / rad;\n float bowl = d < 1.0 ? (d * d - 1.0) : 0.0;\n float rim = d >= 1.0 ? exp(-pow((d - 1.0) * 5.0, 2.0)) * 0.3 * smoothstep(1.0, 1.08, d) : 0.0;\n h += on * amp * rad * (bowl + rim) * (1.0 - smoothstep(0.
|
|
48
|
+
export declare const BALL_FRAGMENT = "\nprecision highp float;\nuniform vec3 uLight;\nuniform vec3 uColour;\nvarying vec3 vObj;\nvarying vec3 vN;\nvarying vec3 vW;\n\nfloat hash13(vec3 p) { p = fract(p * 0.1031); p += dot(p, p.zyx + 31.32); return fract((p.x + p.y) * p.z); }\nvec3 hash33(vec3 p) { p = fract(p * vec3(0.1031, 0.1030, 0.0973)); p += dot(p, p.yxz + 33.33); return fract((p.xxy + p.yxx) * p.zyx); }\nfloat vnoise(vec3 p) {\n vec3 i = floor(p); vec3 f = fract(p); f = f * f * (3.0 - 2.0 * f);\n return mix(mix(mix(hash13(i), hash13(i + vec3(1,0,0)), f.x), mix(hash13(i + vec3(0,1,0)), hash13(i + vec3(1,1,0)), f.x), f.y),\n mix(mix(hash13(i + vec3(0,0,1)), hash13(i + vec3(1,0,1)), f.x), mix(hash13(i + vec3(0,1,1)), hash13(i + vec3(1,1,1)), f.x), f.y), f.z);\n}\n// Noise that knows how big a pixel is: an octave finer than the pixel can show is replaced by its average (0.5) instead of being sampled, so a small body is smooth and not a spray of pixel-sized specks.\nfloat fbm(vec3 p) { float fw = length(fwidth(p)); float a = 0.5; float s = 0.0; for (int i = 0; i < 5; i++) { s += a * mix(0.5, vnoise(p), 1.0 - smoothstep(0.3, 0.7, fw)); p = p * 2.03 + vec3(1.7, 9.2, 3.1); fw *= 2.03; a *= 0.5; } return s; }\n// Craters: a bowl and a raised rim in each cell that has one. Returns a height, negative in a bowl. A crater smaller than about two pixels fades out (it would only be noise).\nfloat craters(vec3 p, float freq, float amp, float seed) {\n vec3 q = p * freq + seed; vec3 ip = floor(q); float h = 0.0;\n float fw = length(fwidth(q));\n for (int x = -1; x <= 1; x++) for (int y = -1; y <= 1; y++) for (int z = -1; z <= 1; z++) {\n vec3 c = ip + vec3(float(x), float(y), float(z));\n float on = step(0.52, hash13(c + 3.7));\n vec3 ctr = c + hash33(c);\n float rad = mix(0.12, 0.36, hash13(c + 17.3));\n float d = length(q - ctr) / rad;\n float bowl = d < 1.0 ? (d * d - 1.0) : 0.0;\n float rim = d >= 1.0 ? exp(-pow((d - 1.0) * 5.0, 2.0)) * 0.3 * smoothstep(1.0, 1.08, d) : 0.0;\n h += on * amp * rad * (bowl + rim) * (1.0 - smoothstep(0.14, 0.34, fw / rad));\n }\n return h;\n}\nfloat lon(vec3 p) { return atan(p.y, p.x); }\nfloat lat(vec3 p) { return asin(clamp(p.z, -1.0, 1.0)); }\nfloat wrapPi(float a) { return a - 6.2831853 * floor((a + 3.1415927) / 6.2831853); }\nvec3 dirOf(float lonDeg, float latDeg) { float lo = radians(lonDeg); float la = radians(latDeg); return vec3(cos(la) * cos(lo), cos(la) * sin(lo), sin(la)); }\n// One big round bowl with a rim, by longitude, latitude and angular radius in degrees. Negative inside.\nfloat bowl(vec3 p, float lonDeg, float latDeg, float radDeg, float depth) {\n float d = acos(clamp(dot(p, dirOf(lonDeg, latDeg)), -1.0, 1.0)) / radians(radDeg);\n return d < 1.0 ? depth * (d * d - 1.0) : depth * exp(-pow((d - 1.0) * 5.0, 2.0)) * 0.3 * smoothstep(1.0, 1.08, d);\n}\n// A soft oval on the body, 1 inside and 0 outside, by longitude and latitude in degrees.\nfloat oval(vec3 p, float lon0, float lat0, float rx, float ry, float soft) {\n float dx = wrapPi(lon(p) - radians(lon0)) * cos(radians(lat0)) / radians(rx);\n float dy = (lat(p) - radians(lat0)) / radians(ry);\n return 1.0 - smoothstep(1.0 - soft, 1.0, sqrt(dx * dx + dy * dy));\n}\nvoid main() {\n vec3 n = normalize(vN);\n float shade = 0.82 + 0.3 * (fbm(normalize(vObj) * 4.0) - 0.5);\n float diff = max(dot(n, uLight), 0.0);\n vec3 c = pow(uColour * shade, vec3(2.2)) * (0.04 + 1.1 * diff);\n gl_FragColor = vec4(pow(c, vec3(1.0 / 2.2)), 1.0);\n}";
|
package/dist/scene/glsl.js
CHANGED
|
@@ -33,7 +33,7 @@ float craters(vec3 p, float freq, float amp, float seed) {
|
|
|
33
33
|
float d = length(q - ctr) / rad;
|
|
34
34
|
float bowl = d < 1.0 ? (d * d - 1.0) : 0.0;
|
|
35
35
|
float rim = d >= 1.0 ? exp(-pow((d - 1.0) * 5.0, 2.0)) * 0.3 * smoothstep(1.0, 1.08, d) : 0.0;
|
|
36
|
-
h += on * amp * rad * (bowl + rim) * (1.0 - smoothstep(0.
|
|
36
|
+
h += on * amp * rad * (bowl + rim) * (1.0 - smoothstep(0.14, 0.34, fw / rad));
|
|
37
37
|
}
|
|
38
38
|
return h;
|
|
39
39
|
}
|
|
@@ -130,7 +130,9 @@ ${detail ? " s.rgb *= microDetail(p);\n" : ""}
|
|
|
130
130
|
float det = dot(dpx, cross(dpy, n));
|
|
131
131
|
// relief is worked out from differences between neighbouring pixels, which cannot show a bump only a few pixels wide: on a small disc it fades out and the colour alone carries the craters
|
|
132
132
|
float reliefLod = 1.0 - smoothstep(0.008, 0.024, length(fwidth(p)));
|
|
133
|
-
|
|
133
|
+
// a wall steeper than about 50 degrees is not tipped further: the tilt of two pixels' difference on a steep bowl would turn the surface away from the light and draw a hard black ring
|
|
134
|
+
vec3 tilt = sign(det) * uRelief * reliefLod * grad * 22.0;
|
|
135
|
+
vec3 nb = normalize(abs(det) * n - tilt * min(1.0, 1.2 * abs(det) / max(length(tilt), 1e-20)));
|
|
134
136
|
float d = dot(nb, uLight);
|
|
135
137
|
float diff = clamp((d + uWrap) / (1.0 + uWrap), 0.0, 1.0) * smoothstep(-0.02 - uWrap * 0.6, 0.1 + uWrap * 0.8, dg);
|
|
136
138
|
if (uRingOn > 0.5) {
|
|
@@ -33,11 +33,28 @@ export interface NameLabelOptions {
|
|
|
33
33
|
size?: BodyLabelSize;
|
|
34
34
|
/** No label while the body is drawn smaller than this many CSS pixels in radius. Default 24. */
|
|
35
35
|
minRadiusPx?: number;
|
|
36
|
+
/**
|
|
37
|
+
* The tone of the letters, so the name reads on every body: `auto` (the default) takes dark letters on a light body and pale letters on a dark one, from the body's own colour (`nameTone`); `light` and
|
|
38
|
+
* `dark` force one. Dark letters are the `burned` look, pale ones the `painted` look. `auto` changes only a `painted` look; the look a page names (`etched`, `outline`, `burned`) is drawn as asked.
|
|
39
|
+
*/
|
|
40
|
+
tone?: NameLabelTone;
|
|
36
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* The tone of a name's letters: `auto` (from the body's colour), `light` or `dark`.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* import type { NameLabelTone } from "@johnmorrisdotca/wakusei-planets/painter";
|
|
48
|
+
*
|
|
49
|
+
* const tone: NameLabelTone = "dark";
|
|
50
|
+
* console.log(tone);
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export type NameLabelTone = "auto" | "light" | "dark";
|
|
37
54
|
/** The family's standard attribute names, as `readNameLabel` reads them. */
|
|
38
|
-
export declare const NAME_LABEL_ATTRIBUTES: readonly ["body-label", "body-label-strength", "body-label-repeat", "body-label-latitude", "body-label-facing", "body-label-font", "body-label-fallback-font"];
|
|
55
|
+
export declare const NAME_LABEL_ATTRIBUTES: readonly ["body-label", "body-label-strength", "body-label-repeat", "body-label-latitude", "body-label-facing", "body-label-font", "body-label-fallback-font", "body-label-tone"];
|
|
39
56
|
/**
|
|
40
|
-
* The label the family's attributes ask for (`body-label="painted"` and the
|
|
57
|
+
* The label the family's attributes ask for (`body-label="painted"` and the seven beside it), or `null` when `body-label` is absent or not a look.
|
|
41
58
|
*
|
|
42
59
|
* @param el - Anything with `getAttribute` and `hasAttribute`: the element.
|
|
43
60
|
* @returns The options, or `null` for no label.
|
|
@@ -55,6 +72,39 @@ export declare function readNameLabel(el: {
|
|
|
55
72
|
getAttribute(name: string): string | null;
|
|
56
73
|
hasAttribute(name: string): boolean;
|
|
57
74
|
}): NameLabelOptions | null;
|
|
75
|
+
/**
|
|
76
|
+
* The tone of letters that read on a body of this colour: `dark` when the body is light, `light` when it is dark. The two letters are the family's pale `#f4efe2` and dark `#1c140c`; laid over a surface at any
|
|
77
|
+
* strength, the pale ones stand out more on a surface darker than about half way between them and the dark ones on a lighter one, which is a relative luminance of 0.5 in the body's mean colour (`BODIES[id].colour`):
|
|
78
|
+
* Venus, Saturn, Uranus, the Sun, Io and the ice moons get dark letters, and Mars, the Moon, the Earth and Neptune pale ones.
|
|
79
|
+
*
|
|
80
|
+
* @param colour - The body's mean colour, `#rrggbb`.
|
|
81
|
+
* @returns `dark` or `light`.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* import { BODIES } from "@johnmorrisdotca/wakusei-planets";
|
|
86
|
+
* import { nameTone } from "@johnmorrisdotca/wakusei-planets/painter";
|
|
87
|
+
*
|
|
88
|
+
* console.log(nameTone(BODIES.uranus.colour), nameTone(BODIES.mars.colour));
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export declare function nameTone(colour: string): "light" | "dark";
|
|
92
|
+
/**
|
|
93
|
+
* The look a name is drawn in once its tone is settled. A tone the page forces (`light` or `dark`) wins: `dark` makes `painted` and `etched` the dark `burned` look, and `light` makes `burned` the pale `painted` look. With `auto` (or no
|
|
94
|
+
* tone) only a `painted` look is changed, to `burned` on a light body (`nameTone`); `etched`, `burned` and `outline` are drawn as the page asked. `outline` has one tone and is never changed.
|
|
95
|
+
*
|
|
96
|
+
* @param options - The label's look and tone.
|
|
97
|
+
* @param colour - The body's mean colour, `#rrggbb`; left out, `auto` leaves the look as it is.
|
|
98
|
+
* @returns The look to draw.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* import { nameLabelLook } from "@johnmorrisdotca/wakusei-planets/painter";
|
|
103
|
+
*
|
|
104
|
+
* console.log(nameLabelLook({ look: "painted" }, "#a9e0e5"), nameLabelLook({ look: "painted", tone: "light" }, "#a9e0e5"), nameLabelLook({ look: "etched" }, "#a9e0e5"));
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export declare function nameLabelLook(options: Pick<NameLabelOptions, "look" | "tone">, colour?: string): BodyLabelLook;
|
|
58
108
|
/**
|
|
59
109
|
* The text written for a body: its English name in capitals (the family's look), or its Japanese name as it is.
|
|
60
110
|
*
|
|
@@ -82,6 +132,8 @@ export type LabelParts = Pick<typeof THREE, "CanvasTexture" | "SRGBColorSpace" |
|
|
|
82
132
|
export interface NameLabel {
|
|
83
133
|
/** Add it to the body's turning mesh (a radius-1 sphere whose +z is longitude 0). */
|
|
84
134
|
readonly object: THREE.Group;
|
|
135
|
+
/** The look the name is drawn in now, its tone settled: `painted` (pale letters), `etched`, `outline` or `burned` (dark ones). */
|
|
136
|
+
readonly look: BodyLabelLook;
|
|
85
137
|
/** True once the name is drawn in its font: the host draws its view again when `onReady` is called. */
|
|
86
138
|
readonly ready: boolean;
|
|
87
139
|
/** Before each draw: the camera (for `facing`; left out, the name faces +z), the body's radius on the screen in CSS pixels, and the direction to the light in the scene's axes. Returns true when it hid, showed or turned the name. */
|
|
@@ -112,5 +164,6 @@ export declare function setBodyLabelFontUrl(url: string | URL): void;
|
|
|
112
164
|
* @param options - The look and the rest.
|
|
113
165
|
* @param emissive - The body glows (the Sun): the name is lit fully, from no direction.
|
|
114
166
|
* @param onReady - Called when the name is drawn, and again when a new font is in.
|
|
167
|
+
* @param colour - The body's mean colour, `#rrggbb`: with `tone: "auto"` the letters are dark on a light body and pale on a dark one. Left out, they are the look's own.
|
|
115
168
|
*/
|
|
116
|
-
export declare function createNameLabel(T: ThreeParts, text: string, options: NameLabelOptions, emissive: boolean, onReady: () => void): NameLabel;
|
|
169
|
+
export declare function createNameLabel(T: ThreeParts, text: string, options: NameLabelOptions, emissive: boolean, onReady: () => void, colour?: string): NameLabel;
|
package/dist/scene/name-label.js
CHANGED
|
@@ -3,11 +3,12 @@
|
|
|
3
3
|
// bodies have no three.js lights (each is lit by the `uLight` of its own material). Nothing here draws: the host calls `update` before each draw and `set` when a setting changed.
|
|
4
4
|
import { BODY_LABEL_FONT, createBodyLabel, loadBodyLabelFont } from "@johnmorrisdotca/kazoku-ui/body-label";
|
|
5
5
|
/** The family's standard attribute names, as `readNameLabel` reads them. */
|
|
6
|
-
export const NAME_LABEL_ATTRIBUTES = ["body-label", "body-label-strength", "body-label-repeat", "body-label-latitude", "body-label-facing", "body-label-font", "body-label-fallback-font"];
|
|
6
|
+
export const NAME_LABEL_ATTRIBUTES = ["body-label", "body-label-strength", "body-label-repeat", "body-label-latitude", "body-label-facing", "body-label-font", "body-label-fallback-font", "body-label-tone"];
|
|
7
7
|
const LOOKS = ["painted", "etched", "outline", "burned"];
|
|
8
8
|
const REPEATS = ["once", "both", "around"];
|
|
9
|
+
const TONES = ["auto", "light", "dark"];
|
|
9
10
|
/**
|
|
10
|
-
* The label the family's attributes ask for (`body-label="painted"` and the
|
|
11
|
+
* The label the family's attributes ask for (`body-label="painted"` and the seven beside it), or `null` when `body-label` is absent or not a look.
|
|
11
12
|
*
|
|
12
13
|
* @param el - Anything with `getAttribute` and `hasAttribute`: the element.
|
|
13
14
|
* @returns The options, or `null` for no label.
|
|
@@ -43,8 +44,62 @@ export function readNameLabel(el) {
|
|
|
43
44
|
const fallback = el.getAttribute("body-label-fallback-font");
|
|
44
45
|
if (fallback !== null && fallback.trim() !== "")
|
|
45
46
|
out.fallbackFont = fallback.trim();
|
|
47
|
+
const tone = el.getAttribute("body-label-tone");
|
|
48
|
+
if (tone !== null && TONES.includes(tone))
|
|
49
|
+
out.tone = tone;
|
|
46
50
|
return out;
|
|
47
51
|
}
|
|
52
|
+
/** The relative luminance (0 to 1, as WCAG defines it) of a `#rrggbb` colour. */
|
|
53
|
+
const luminanceOf = (hex) => {
|
|
54
|
+
const [r, g, b] = [1, 3, 5].map((i) => {
|
|
55
|
+
const c = parseInt(hex.slice(i, i + 2), 16) / 255;
|
|
56
|
+
return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
|
|
57
|
+
});
|
|
58
|
+
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* The tone of letters that read on a body of this colour: `dark` when the body is light, `light` when it is dark. The two letters are the family's pale `#f4efe2` and dark `#1c140c`; laid over a surface at any
|
|
62
|
+
* strength, the pale ones stand out more on a surface darker than about half way between them and the dark ones on a lighter one, which is a relative luminance of 0.5 in the body's mean colour (`BODIES[id].colour`):
|
|
63
|
+
* Venus, Saturn, Uranus, the Sun, Io and the ice moons get dark letters, and Mars, the Moon, the Earth and Neptune pale ones.
|
|
64
|
+
*
|
|
65
|
+
* @param colour - The body's mean colour, `#rrggbb`.
|
|
66
|
+
* @returns `dark` or `light`.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* import { BODIES } from "@johnmorrisdotca/wakusei-planets";
|
|
71
|
+
* import { nameTone } from "@johnmorrisdotca/wakusei-planets/painter";
|
|
72
|
+
*
|
|
73
|
+
* console.log(nameTone(BODIES.uranus.colour), nameTone(BODIES.mars.colour));
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export function nameTone(colour) {
|
|
77
|
+
return luminanceOf(colour) > 0.5 ? "dark" : "light";
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The look a name is drawn in once its tone is settled. A tone the page forces (`light` or `dark`) wins: `dark` makes `painted` and `etched` the dark `burned` look, and `light` makes `burned` the pale `painted` look. With `auto` (or no
|
|
81
|
+
* tone) only a `painted` look is changed, to `burned` on a light body (`nameTone`); `etched`, `burned` and `outline` are drawn as the page asked. `outline` has one tone and is never changed.
|
|
82
|
+
*
|
|
83
|
+
* @param options - The label's look and tone.
|
|
84
|
+
* @param colour - The body's mean colour, `#rrggbb`; left out, `auto` leaves the look as it is.
|
|
85
|
+
* @returns The look to draw.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* import { nameLabelLook } from "@johnmorrisdotca/wakusei-planets/painter";
|
|
90
|
+
*
|
|
91
|
+
* console.log(nameLabelLook({ look: "painted" }, "#a9e0e5"), nameLabelLook({ look: "painted", tone: "light" }, "#a9e0e5"), nameLabelLook({ look: "etched" }, "#a9e0e5"));
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
export function nameLabelLook(options, colour) {
|
|
95
|
+
if (options.look === "outline")
|
|
96
|
+
return "outline";
|
|
97
|
+
if (options.tone === "dark")
|
|
98
|
+
return "burned";
|
|
99
|
+
if (options.tone === "light")
|
|
100
|
+
return options.look === "burned" ? "painted" : options.look;
|
|
101
|
+
return options.look === "painted" && colour !== undefined && nameTone(colour) === "dark" ? "burned" : options.look;
|
|
102
|
+
}
|
|
48
103
|
/**
|
|
49
104
|
* The text written for a body: its English name in capitals (the family's look), or its Japanese name as it is.
|
|
50
105
|
*
|
|
@@ -68,9 +123,9 @@ export function nameLabelKey(text, options) {
|
|
|
68
123
|
if (!options)
|
|
69
124
|
return "";
|
|
70
125
|
const o = { ...DEFAULTS, ...options };
|
|
71
|
-
return `${text}|${o.look}|${o.strength}|${o.repeat}|${o.latitude}|${o.facing}|${o.font}|${o.fallbackFont}|${o.size}|${o.minRadiusPx}`;
|
|
126
|
+
return `${text}|${o.look}|${o.tone}|${o.strength}|${o.repeat}|${o.latitude}|${o.facing}|${o.font}|${o.fallbackFont}|${o.size}|${o.minRadiusPx}`;
|
|
72
127
|
}
|
|
73
|
-
const DEFAULTS = { strength: 0.5, repeat: "once", latitude: 0, facing: false, font: BODY_LABEL_FONT, fallbackFont: "", size: "medium", minRadiusPx: 24 };
|
|
128
|
+
const DEFAULTS = { tone: "auto", strength: 0.5, repeat: "once", latitude: 0, facing: false, font: BODY_LABEL_FONT, fallbackFont: "", size: "medium", minRadiusPx: 24 };
|
|
74
129
|
const hasLabelParts = (T) => {
|
|
75
130
|
const parts = T;
|
|
76
131
|
return typeof parts.CanvasTexture === "function" && typeof parts.SRGBColorSpace === "string" && typeof parts.NormalBlending === "number";
|
|
@@ -149,15 +204,22 @@ const ensureFont = (font) => (font === BODY_LABEL_FONT && canDraw() ? (fontLoad
|
|
|
149
204
|
* @param options - The look and the rest.
|
|
150
205
|
* @param emissive - The body glows (the Sun): the name is lit fully, from no direction.
|
|
151
206
|
* @param onReady - Called when the name is drawn, and again when a new font is in.
|
|
207
|
+
* @param colour - The body's mean colour, `#rrggbb`: with `tone: "auto"` the letters are dark on a light body and pale on a dark one. Left out, they are the look's own.
|
|
152
208
|
*/
|
|
153
|
-
export function createNameLabel(T, text, options, emissive, onReady) {
|
|
209
|
+
export function createNameLabel(T, text, options, emissive, onReady, colour) {
|
|
154
210
|
const group = new T.Group();
|
|
155
211
|
group.name = "name-label";
|
|
156
212
|
// the shared label's frame has longitude 0 along +x and east towards -z; the bodies' has longitude 0 along +z and east along +x: a quarter turn about the pole maps one onto the other
|
|
157
213
|
group.rotation.y = -Math.PI / 2;
|
|
158
214
|
let label;
|
|
159
215
|
let materials = [];
|
|
160
|
-
|
|
216
|
+
// what the shared label is told: the look with its tone settled (`nameLabelLook`), and no tone of its own
|
|
217
|
+
const shared = (o) => {
|
|
218
|
+
const rest = { ...DEFAULTS, ...withoutUndefined(o) };
|
|
219
|
+
delete rest.tone;
|
|
220
|
+
return { ...rest, look: nameLabelLook(o, colour) };
|
|
221
|
+
};
|
|
222
|
+
let wanted = { text, options: shared(options) };
|
|
161
223
|
let disposed = false;
|
|
162
224
|
let ready = false;
|
|
163
225
|
const light = new T.Vector3(0, 0, 1);
|
|
@@ -189,6 +251,9 @@ export function createNameLabel(T, text, options, emissive, onReady) {
|
|
|
189
251
|
get ready() {
|
|
190
252
|
return ready;
|
|
191
253
|
},
|
|
254
|
+
get look() {
|
|
255
|
+
return wanted.options.look;
|
|
256
|
+
},
|
|
192
257
|
update(camera, radiusPx, toLight) {
|
|
193
258
|
if (label === undefined)
|
|
194
259
|
return false;
|
|
@@ -201,7 +266,7 @@ export function createNameLabel(T, text, options, emissive, onReady) {
|
|
|
201
266
|
return label.update((camera ?? FAR_EYE), radiusPx);
|
|
202
267
|
},
|
|
203
268
|
set(nextText, next) {
|
|
204
|
-
wanted = { text: nextText, options:
|
|
269
|
+
wanted = { text: nextText, options: shared(next) };
|
|
205
270
|
if (label === undefined)
|
|
206
271
|
return;
|
|
207
272
|
const fontChanged = wanted.options.font !== label.options.font;
|
package/dist/scene/painter.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { type TileLevel } from "./tiles.ts";
|
|
|
7
7
|
import { moonOffset, orientation } from "./orient.ts";
|
|
8
8
|
import { type NameLabelOptions } from "./name-label.ts";
|
|
9
9
|
export { moonOffset, orientation };
|
|
10
|
-
export { nameLabelText, readNameLabel, setBodyLabelFontUrl, type NameLabelOptions } from "./name-label.ts";
|
|
10
|
+
export { nameLabelLook, nameLabelText, nameTone, readNameLabel, setBodyLabelFontUrl, type NameLabelOptions, type NameLabelTone } from "./name-label.ts";
|
|
11
11
|
export { mercuryTilesCredit, PICTURE_CREDITS, picturesCredit, type PictureCredit } from "../picture-credits.ts";
|
|
12
12
|
/**
|
|
13
13
|
* The looks the painter draws: `realistic` (shaded from the body's real colours and features) and `painted` (a public-domain surface map, with the shaded look until it arrives).
|
|
@@ -148,6 +148,7 @@ export interface Painting {
|
|
|
148
148
|
shown: boolean;
|
|
149
149
|
text: string;
|
|
150
150
|
turn: number;
|
|
151
|
+
look: string;
|
|
151
152
|
} | null;
|
|
152
153
|
dispose(): void;
|
|
153
154
|
}
|