@johnmorrisdotca/chikyu-earth 0.37.0 → 0.39.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 +15 -0
- package/NOTICE.md +2 -1
- package/README.md +2 -1
- package/dist/markers/place-layer.d.ts +12 -1
- package/dist/markers/place-layer.js +88 -18
- package/dist/markers/place-sprites.d.ts +5 -0
- package/dist/markers/place-sprites.js +8 -0
- package/dist/paint/buffer.d.ts +58 -0
- package/dist/paint/buffer.js +321 -0
- package/dist/paint/math.d.ts +24 -0
- package/dist/paint/math.js +94 -0
- package/dist/paint/texture.d.ts +34 -0
- package/dist/paint/texture.js +52 -0
- package/dist/realistic/earth.d.ts +9 -0
- package/dist/realistic/earth.js +19 -10
- package/dist/realistic/element-base.d.ts +437 -0
- package/dist/realistic/element-base.js +1209 -0
- package/dist/realistic/element.d.ts +2 -406
- package/dist/realistic/element.js +3 -1151
- package/dist/realistic/index.d.ts +1 -1
- package/dist/realistic/no-cultures.d.ts +1 -0
- package/dist/realistic/no-cultures.js +2 -0
- package/dist/realistic/shaders.js +13 -0
- package/dist/realistic/tiles.d.ts +24 -0
- package/dist/realistic/tiles.js +30 -12
- package/dist/satellites/track.js +26 -11
- package/dist/satellites/types.d.ts +5 -2
- package/dist/sky/culture-source.d.ts +15 -0
- package/dist/sky/culture-source.js +15 -0
- package/dist/sky/cultures.d.ts +1 -0
- package/dist/sky/cultures.js +8 -0
- package/dist/sky/host.js +144 -18
- package/dist/space/element-base.d.ts +624 -0
- package/dist/space/element-base.js +1627 -0
- package/dist/space/element.d.ts +2 -587
- package/dist/space/element.js +3 -1546
- package/dist/space/no-cultures.d.ts +1 -0
- package/dist/space/no-cultures.js +2 -0
- package/dist/space/satellite-layer.d.ts +24 -0
- package/dist/space/satellite-layer.js +45 -5
- package/dist/space/scene.d.ts +16 -0
- package/dist/space/scene.js +56 -9
- package/package.json +23 -3
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { coverageAngleRad, groundUnder, PAINT_H, PAINT_W, paintPixel, splitDateLine, trackSteps } from "./math.js";
|
|
2
|
+
const MINUTE = 60_000;
|
|
3
|
+
const DAY = 86_400_000;
|
|
4
|
+
const LENGTH_MS = { "1d": DAY, "1w": 7 * DAY, "1m": 30 * DAY };
|
|
5
|
+
export const paintLengthOf = (length) => (length === "1w" || length === "1m" ? length : "1d");
|
|
6
|
+
export const paintSwathOf = (swath) => (swath === "coverage" ? "coverage" : "track");
|
|
7
|
+
/** Minutes since the epoch, in 16 bits: the trail is at most a month, and 16 bits hold 45 days. */
|
|
8
|
+
export const paintMinute = (ms) => Math.round(ms / MINUTE) & 0xffff;
|
|
9
|
+
const write = (data, x, y, minute) => {
|
|
10
|
+
if (x < 0 || y < 0 || x >= PAINT_W || y >= PAINT_H)
|
|
11
|
+
return false;
|
|
12
|
+
const i = (y * PAINT_W + x) * 4;
|
|
13
|
+
const fresh = data[i + 3] === 0;
|
|
14
|
+
data[i] = (minute >> 8) & 255;
|
|
15
|
+
data[i + 1] = minute & 255;
|
|
16
|
+
data[i + 2] = 255;
|
|
17
|
+
data[i + 3] = 255;
|
|
18
|
+
return fresh;
|
|
19
|
+
};
|
|
20
|
+
const stampDot = (data, point, minute, radius) => {
|
|
21
|
+
const at = paintPixel(point.lon, point.lat);
|
|
22
|
+
let added = 0;
|
|
23
|
+
for (let dy = -radius; dy <= radius; dy += 1) {
|
|
24
|
+
for (let dx = -radius; dx <= radius; dx += 1) {
|
|
25
|
+
if (dx * dx + dy * dy > radius * radius)
|
|
26
|
+
continue;
|
|
27
|
+
if (write(data, at.x + dx, at.y + dy, minute))
|
|
28
|
+
added += 1;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return added;
|
|
32
|
+
};
|
|
33
|
+
const stampLine = (data, a, b, minute) => {
|
|
34
|
+
const from = paintPixel(a.lon, a.lat);
|
|
35
|
+
const to = paintPixel(b.lon, b.lat);
|
|
36
|
+
if (Math.abs(to.x - from.x) > PAINT_W / 2)
|
|
37
|
+
return 0;
|
|
38
|
+
let added = 0;
|
|
39
|
+
let x = from.x;
|
|
40
|
+
let y = from.y;
|
|
41
|
+
const dx = Math.abs(to.x - x);
|
|
42
|
+
const dy = Math.abs(to.y - y);
|
|
43
|
+
const sx = x < to.x ? 1 : -1;
|
|
44
|
+
const sy = y < to.y ? 1 : -1;
|
|
45
|
+
let err = dx - dy;
|
|
46
|
+
for (;;) {
|
|
47
|
+
if (write(data, x, y, minute))
|
|
48
|
+
added += 1;
|
|
49
|
+
if (write(data, x + 1, y, minute))
|
|
50
|
+
added += 1;
|
|
51
|
+
if (x === to.x && y === to.y)
|
|
52
|
+
break;
|
|
53
|
+
const e2 = 2 * err;
|
|
54
|
+
if (e2 > -dy) {
|
|
55
|
+
err -= dy;
|
|
56
|
+
x += sx;
|
|
57
|
+
}
|
|
58
|
+
if (e2 < dx) {
|
|
59
|
+
err += dx;
|
|
60
|
+
y += sy;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return added;
|
|
64
|
+
};
|
|
65
|
+
const RAD = Math.PI / 180;
|
|
66
|
+
/** 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. */
|
|
67
|
+
const stampCap = (data, point, theta, minute) => {
|
|
68
|
+
if (!(theta > 0))
|
|
69
|
+
return stampDot(data, point, minute, 1);
|
|
70
|
+
const deg = theta / RAD;
|
|
71
|
+
const sin1 = Math.sin(point.lat * RAD);
|
|
72
|
+
const cos1 = Math.cos(point.lat * RAD);
|
|
73
|
+
const cosTheta = Math.cos(theta);
|
|
74
|
+
const y0 = paintPixel(0, Math.min(90, point.lat + deg)).y;
|
|
75
|
+
const y1 = paintPixel(0, Math.max(-90, point.lat - deg)).y;
|
|
76
|
+
let added = 0;
|
|
77
|
+
for (let y = y0; y <= y1; y += 1) {
|
|
78
|
+
const lat = (90 - ((y + 0.5) / PAINT_H) * 180) * RAD;
|
|
79
|
+
const denominator = cos1 * Math.cos(lat);
|
|
80
|
+
const need = cosTheta - sin1 * Math.sin(lat);
|
|
81
|
+
let half;
|
|
82
|
+
if (denominator < 1e-9) {
|
|
83
|
+
if (need > 0)
|
|
84
|
+
continue;
|
|
85
|
+
half = 180;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
const ratio = need / denominator;
|
|
89
|
+
if (ratio > 1)
|
|
90
|
+
continue;
|
|
91
|
+
half = ratio <= -1 ? 180 : Math.acos(ratio) / RAD;
|
|
92
|
+
}
|
|
93
|
+
const first = Math.floor(((point.lon - half + 180) / 360) * PAINT_W);
|
|
94
|
+
const last = Math.floor(((point.lon + half + 180) / 360) * PAINT_W);
|
|
95
|
+
if (last - first >= PAINT_W - 1) {
|
|
96
|
+
for (let x = 0; x < PAINT_W; x += 1)
|
|
97
|
+
if (write(data, x, y, minute))
|
|
98
|
+
added += 1;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
for (let x = first; x <= last; x += 1)
|
|
102
|
+
if (write(data, ((x % PAINT_W) + PAINT_W) % PAINT_W, y, minute))
|
|
103
|
+
added += 1;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return added;
|
|
107
|
+
};
|
|
108
|
+
const stampGround = (data, swath, ground, previous, minute) => {
|
|
109
|
+
const theta = swath === "coverage" ? coverageAngleRad(ground.heightKm) : 0;
|
|
110
|
+
if (swath === "coverage" && previous === null)
|
|
111
|
+
return { added: stampCap(data, ground, theta, minute), at: ground };
|
|
112
|
+
if (swath === "track" && previous === null)
|
|
113
|
+
return { added: stampDot(data, ground, minute, 1), at: ground };
|
|
114
|
+
// a line is made of steps 0.4 of a degree long; a swath of caps that overlap, a step of about seven tenths of the cap's radius
|
|
115
|
+
const points = trackSteps(previous, ground, swath === "coverage" ? Math.max(1, (theta / RAD) * 0.7) : 0.4);
|
|
116
|
+
let added = 0;
|
|
117
|
+
let cursor = previous;
|
|
118
|
+
for (const point of points) {
|
|
119
|
+
if (swath === "coverage")
|
|
120
|
+
added += stampCap(data, point, theta, minute);
|
|
121
|
+
else
|
|
122
|
+
for (const [from, to] of splitDateLine(cursor, point))
|
|
123
|
+
added += stampLine(data, from, to, minute);
|
|
124
|
+
cursor = point;
|
|
125
|
+
}
|
|
126
|
+
return { added, at: cursor };
|
|
127
|
+
};
|
|
128
|
+
/** A stamp's age in minutes, from the 16 bits it holds. */
|
|
129
|
+
const ageOf = (then, now) => {
|
|
130
|
+
let age = now - then;
|
|
131
|
+
if (age < -32768)
|
|
132
|
+
age += 65536;
|
|
133
|
+
if (age > 32767)
|
|
134
|
+
age -= 65536;
|
|
135
|
+
return age;
|
|
136
|
+
};
|
|
137
|
+
/** The longest gap between two samples that a line may leap: a twentieth of a trip, and between 30 seconds and four minutes. The samples are never more than a third of a trip apart, however many that takes. */
|
|
138
|
+
const MAX_GAP = 240_000;
|
|
139
|
+
const MIN_GAP = 30_000;
|
|
140
|
+
const FRAME_SAMPLES = 600;
|
|
141
|
+
const REBUILD_SAMPLES = { track: 4000, coverage: 1500 };
|
|
142
|
+
const gapOf = (periodMs) => (periodMs !== undefined && periodMs > 0 ? Math.max(MIN_GAP, Math.min(MAX_GAP, periodMs / 20)) : 20_000);
|
|
143
|
+
export function createPaintState() {
|
|
144
|
+
// the two megabytes are taken when the first satellite is painted and given back by `clear`, not made with the view
|
|
145
|
+
let buffer = null;
|
|
146
|
+
const grab = () => (buffer ??= new Uint8Array(PAINT_W * PAINT_H * 4));
|
|
147
|
+
let spec = null;
|
|
148
|
+
let pixels = 0;
|
|
149
|
+
let lastMs = Number.NaN;
|
|
150
|
+
/** The last point stamped and the way the clock was going then, so a line carries on from it. */
|
|
151
|
+
let previous = null;
|
|
152
|
+
let heading = 0;
|
|
153
|
+
/** The buffer was emptied since the last step: the picture must be sent even if nothing new was stamped. */
|
|
154
|
+
let stale = false;
|
|
155
|
+
const reset = () => {
|
|
156
|
+
buffer?.fill(0);
|
|
157
|
+
pixels = 0;
|
|
158
|
+
lastMs = Number.NaN;
|
|
159
|
+
previous = null;
|
|
160
|
+
heading = 0;
|
|
161
|
+
stale = true;
|
|
162
|
+
};
|
|
163
|
+
/** Take out what has fallen out of the length (or is later than the clock now says). */
|
|
164
|
+
const dropOld = (data, now, span) => {
|
|
165
|
+
let dropped = false;
|
|
166
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
167
|
+
if (data[i + 3] === 0)
|
|
168
|
+
continue;
|
|
169
|
+
const then = ((data[i] ?? 0) << 8) | (data[i + 1] ?? 0);
|
|
170
|
+
const age = ageOf(then, now);
|
|
171
|
+
// a minute of slack at each end, for the rounding of the stamp's minute
|
|
172
|
+
if (age > span + 1 || age < -1) {
|
|
173
|
+
data[i] = 0;
|
|
174
|
+
data[i + 1] = 0;
|
|
175
|
+
data[i + 2] = 0;
|
|
176
|
+
data[i + 3] = 0;
|
|
177
|
+
pixels -= 1;
|
|
178
|
+
dropped = true;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return dropped;
|
|
182
|
+
};
|
|
183
|
+
const self = {
|
|
184
|
+
get data() {
|
|
185
|
+
return grab();
|
|
186
|
+
},
|
|
187
|
+
width: PAINT_W,
|
|
188
|
+
height: PAINT_H,
|
|
189
|
+
get pixels() {
|
|
190
|
+
return pixels;
|
|
191
|
+
},
|
|
192
|
+
get on() {
|
|
193
|
+
return spec !== null;
|
|
194
|
+
},
|
|
195
|
+
get length() {
|
|
196
|
+
return spec?.length ?? "1d";
|
|
197
|
+
},
|
|
198
|
+
get swath() {
|
|
199
|
+
return spec?.swath ?? "track";
|
|
200
|
+
},
|
|
201
|
+
get id() {
|
|
202
|
+
return spec?.id ?? "";
|
|
203
|
+
},
|
|
204
|
+
get spanMinutes() {
|
|
205
|
+
return LENGTH_MS[spec?.length ?? "1d"] / MINUTE;
|
|
206
|
+
},
|
|
207
|
+
set(next) {
|
|
208
|
+
if (!next) {
|
|
209
|
+
self.clear();
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
const id = String(next.id);
|
|
213
|
+
const length = paintLengthOf(next.length);
|
|
214
|
+
const swath = paintSwathOf(next.swath);
|
|
215
|
+
if (spec && spec.id === id && spec.length === length && spec.swath === swath)
|
|
216
|
+
return;
|
|
217
|
+
reset();
|
|
218
|
+
spec = { id, length, swath };
|
|
219
|
+
},
|
|
220
|
+
clear() {
|
|
221
|
+
reset();
|
|
222
|
+
spec = null;
|
|
223
|
+
buffer = null;
|
|
224
|
+
},
|
|
225
|
+
step(ms, sample, periodMs) {
|
|
226
|
+
if (!spec) {
|
|
227
|
+
const upload = stale;
|
|
228
|
+
stale = false;
|
|
229
|
+
return upload;
|
|
230
|
+
}
|
|
231
|
+
if (!Number.isFinite(ms))
|
|
232
|
+
return false;
|
|
233
|
+
if (Number.isFinite(lastMs) && ms === lastMs) {
|
|
234
|
+
const upload = stale;
|
|
235
|
+
stale = false;
|
|
236
|
+
return upload;
|
|
237
|
+
}
|
|
238
|
+
const data = grab();
|
|
239
|
+
const length = LENGTH_MS[spec.length];
|
|
240
|
+
const gap = gapOf(periodMs);
|
|
241
|
+
let dirty = stale;
|
|
242
|
+
stale = false;
|
|
243
|
+
/** Stamp the satellite's place at each of `times`, one line from the one before. */
|
|
244
|
+
const lay = (times, way) => {
|
|
245
|
+
if (way !== heading)
|
|
246
|
+
previous = null;
|
|
247
|
+
heading = way;
|
|
248
|
+
for (const at of times) {
|
|
249
|
+
const ground = sample(at);
|
|
250
|
+
if (!ground) {
|
|
251
|
+
previous = null;
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
const stamped = stampGround(data, spec.swath, ground, previous, paintMinute(at));
|
|
255
|
+
pixels += stamped.added;
|
|
256
|
+
previous = stamped.at;
|
|
257
|
+
dirty = true;
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
const between = (from, to, cap) => {
|
|
261
|
+
const span = Math.abs(to - from);
|
|
262
|
+
const count = Math.max(Math.min(cap, Math.max(1, Math.ceil(span / gap))), periodMs === undefined ? 1 : Math.ceil(span / (periodMs / 3)));
|
|
263
|
+
return Array.from({ length: count + 1 }, (_, i) => from + ((to - from) * i) / count);
|
|
264
|
+
};
|
|
265
|
+
if (!Number.isFinite(lastMs) || Math.abs(ms - lastMs) > length) {
|
|
266
|
+
// the first moment, or a jump: the whole length is laid down again, oldest first
|
|
267
|
+
data.fill(0);
|
|
268
|
+
pixels = 0;
|
|
269
|
+
previous = null;
|
|
270
|
+
heading = 1;
|
|
271
|
+
lay(between(ms - length, ms, REBUILD_SAMPLES[spec.swath]), 1);
|
|
272
|
+
}
|
|
273
|
+
else if (ms > lastMs) {
|
|
274
|
+
lay(between(lastMs, ms, FRAME_SAMPLES), 1);
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
// backwards: the newer end falls away below, and the older end, which the clock has just uncovered, is laid down
|
|
278
|
+
lay(between(lastMs - length, ms - length, FRAME_SAMPLES), -1);
|
|
279
|
+
}
|
|
280
|
+
if (dropOld(data, paintMinute(ms), length / MINUTE))
|
|
281
|
+
dirty = true;
|
|
282
|
+
lastMs = ms;
|
|
283
|
+
return dirty;
|
|
284
|
+
},
|
|
285
|
+
};
|
|
286
|
+
return self;
|
|
287
|
+
}
|
|
288
|
+
/** The satellite `id` names, in the groups now set: a NORAD number, or `group:index`. */
|
|
289
|
+
export function findPainted(groups, id) {
|
|
290
|
+
const cut = id.indexOf(":");
|
|
291
|
+
if (cut > 0) {
|
|
292
|
+
for (const [name, group] of groups) {
|
|
293
|
+
const index = Number(id.slice(cut + 1));
|
|
294
|
+
if (name === id.slice(0, cut) && index >= 0 && index < group.satellites.count)
|
|
295
|
+
return { satellites: group.satellites, index };
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
const norad = Number(id);
|
|
299
|
+
if (!Number.isFinite(norad))
|
|
300
|
+
return null;
|
|
301
|
+
for (const [, group] of groups) {
|
|
302
|
+
const index = group.satellites.sets.findIndex((set) => set.NORAD_CAT_ID === norad);
|
|
303
|
+
if (index >= 0)
|
|
304
|
+
return { satellites: group.satellites, index };
|
|
305
|
+
}
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
const scratch = new Float64Array(6);
|
|
309
|
+
/** The ground under the painted satellite at `ms`, or null when it cannot be placed. */
|
|
310
|
+
export function paintedGround(groups, id, ms) {
|
|
311
|
+
const found = findPainted(groups, id);
|
|
312
|
+
if (!found || !found.satellites.exact(found.index, ms, scratch))
|
|
313
|
+
return null;
|
|
314
|
+
return groundUnder(scratch[0], scratch[1], scratch[2], ms);
|
|
315
|
+
}
|
|
316
|
+
/** One trip of the painted satellite round the Earth, in milliseconds, from its mean motion; undefined when it is not found. */
|
|
317
|
+
export function paintedPeriod(groups, id) {
|
|
318
|
+
const found = findPainted(groups, id);
|
|
319
|
+
const motion = found?.satellites.sets[found.index]?.MEAN_MOTION;
|
|
320
|
+
return motion !== undefined && motion > 0 ? DAY / motion : undefined;
|
|
321
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Mean Earth radius, kilometres, the same unit the satellite positions use. */
|
|
2
|
+
export declare const PAINT_EARTH_KM = 6371;
|
|
3
|
+
/** The painted texture: equirectangular, north at the first row, longitude −180 at the first column. */
|
|
4
|
+
export declare const PAINT_W = 1024;
|
|
5
|
+
export declare const PAINT_H = 512;
|
|
6
|
+
export interface LonLat {
|
|
7
|
+
lon: number;
|
|
8
|
+
lat: number;
|
|
9
|
+
}
|
|
10
|
+
/** Earth-central angle, in radians, where a satellite `heightKm` up is at least `elevationDeg` above the horizon (10 when left out). 0 when it never is. */
|
|
11
|
+
export declare function coverageAngleRad(heightKm: number, elevationDeg?: number): number;
|
|
12
|
+
/** The ground under a satellite. `x, y, z` are what `Satellites.exact` writes (Earth radii, x to the vernal equinox, y north). `ms` turns the Earth under that place. */
|
|
13
|
+
export declare function groundUnder(x: number, y: number, z: number, ms: number): LonLat & {
|
|
14
|
+
heightKm: number;
|
|
15
|
+
};
|
|
16
|
+
/** Points from `a` to `b` along the great circle, about `stepDeg` apart, including both ends. A fast clock stays a continuous line, and a pole or the date line is many short steps. */
|
|
17
|
+
export declare function trackSteps(a: LonLat, b: LonLat, stepDeg: number): LonLat[];
|
|
18
|
+
/** One segment, or two when the short way from `a` to `b` crosses the date line, so an equirectangular line does not cross the whole map. Longitude is degrees, −180 to 180. */
|
|
19
|
+
export declare function splitDateLine(a: LonLat, b: LonLat): [LonLat, LonLat][];
|
|
20
|
+
/** Where a longitude and latitude fall on the painted texture, column and row. */
|
|
21
|
+
export declare function paintPixel(lon: number, lat: number): {
|
|
22
|
+
x: number;
|
|
23
|
+
y: number;
|
|
24
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// Pure geometry for a satellite's painted ground track: the point under it, the date line, and the circle where it stands 10° up.
|
|
2
|
+
import { greenwichSidereal } from "../realistic/sun.js";
|
|
3
|
+
const RAD = Math.PI / 180;
|
|
4
|
+
/** Mean Earth radius, kilometres, the same unit the satellite positions use. */
|
|
5
|
+
export const PAINT_EARTH_KM = 6371;
|
|
6
|
+
/** The painted texture: equirectangular, north at the first row, longitude −180 at the first column. */
|
|
7
|
+
export const PAINT_W = 1024;
|
|
8
|
+
export const PAINT_H = 512;
|
|
9
|
+
/** Earth-central angle, in radians, where a satellite `heightKm` up is at least `elevationDeg` above the horizon (10 when left out). 0 when it never is. */
|
|
10
|
+
export function coverageAngleRad(heightKm, elevationDeg = 10) {
|
|
11
|
+
const r = PAINT_EARTH_KM + heightKm;
|
|
12
|
+
if (!(r > PAINT_EARTH_KM))
|
|
13
|
+
return 0;
|
|
14
|
+
const elevation = elevationDeg * RAD;
|
|
15
|
+
const cosAlpha = (PAINT_EARTH_KM / r) * Math.cos(elevation);
|
|
16
|
+
if (!(cosAlpha < 1))
|
|
17
|
+
return 0;
|
|
18
|
+
const alpha = Math.acos(Math.max(-1, cosAlpha));
|
|
19
|
+
return Math.max(0, alpha - elevation);
|
|
20
|
+
}
|
|
21
|
+
/** A unit vector for a longitude and latitude, in the Earth's own axes (+z at longitude 0, +x east, +y north). */
|
|
22
|
+
const unitOf = (point) => {
|
|
23
|
+
const lon = point.lon * RAD;
|
|
24
|
+
const lat = point.lat * RAD;
|
|
25
|
+
const c = Math.cos(lat);
|
|
26
|
+
return [c * Math.sin(lon), Math.sin(lat), c * Math.cos(lon)];
|
|
27
|
+
};
|
|
28
|
+
const fromUnit = (x, y, z) => {
|
|
29
|
+
const length = Math.hypot(x, y, z) || 1;
|
|
30
|
+
return { lon: Math.atan2(x, z) / RAD, lat: Math.asin(Math.max(-1, Math.min(1, y / length))) / RAD };
|
|
31
|
+
};
|
|
32
|
+
/** The ground under a satellite. `x, y, z` are what `Satellites.exact` writes (Earth radii, x to the vernal equinox, y north). `ms` turns the Earth under that place. */
|
|
33
|
+
export function groundUnder(x, y, z, ms) {
|
|
34
|
+
const theta = greenwichSidereal(ms) * RAD;
|
|
35
|
+
const cos = Math.cos(theta);
|
|
36
|
+
const sin = Math.sin(theta);
|
|
37
|
+
const temeX = x;
|
|
38
|
+
const temeY = -z;
|
|
39
|
+
const temeZ = y;
|
|
40
|
+
const east = -temeX * sin + temeY * cos;
|
|
41
|
+
const north = temeZ;
|
|
42
|
+
const prime = temeX * cos + temeY * sin;
|
|
43
|
+
const ground = fromUnit(east, north, prime);
|
|
44
|
+
return { ...ground, heightKm: (Math.hypot(x, y, z) - 1) * PAINT_EARTH_KM };
|
|
45
|
+
}
|
|
46
|
+
/** Points from `a` to `b` along the great circle, about `stepDeg` apart, including both ends. A fast clock stays a continuous line, and a pole or the date line is many short steps. */
|
|
47
|
+
export function trackSteps(a, b, stepDeg) {
|
|
48
|
+
const u = unitOf(a);
|
|
49
|
+
const v = unitOf(b);
|
|
50
|
+
const dot = Math.max(-1, Math.min(1, u[0] * v[0] + u[1] * v[1] + u[2] * v[2]));
|
|
51
|
+
const angle = Math.acos(dot);
|
|
52
|
+
const step = Math.max(0.25, stepDeg) * RAD;
|
|
53
|
+
if (!(angle > step))
|
|
54
|
+
return [a, b];
|
|
55
|
+
const count = Math.ceil(angle / step);
|
|
56
|
+
const sin = Math.sin(angle) || 1;
|
|
57
|
+
const out = [];
|
|
58
|
+
for (let i = 0; i <= count; i += 1) {
|
|
59
|
+
const t = i / count;
|
|
60
|
+
const left = Math.sin((1 - t) * angle) / sin;
|
|
61
|
+
const right = Math.sin(t * angle) / sin;
|
|
62
|
+
out.push(fromUnit(u[0] * left + v[0] * right, u[1] * left + v[1] * right, u[2] * left + v[2] * right));
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
/** One segment, or two when the short way from `a` to `b` crosses the date line, so an equirectangular line does not cross the whole map. Longitude is degrees, −180 to 180. */
|
|
67
|
+
export function splitDateLine(a, b) {
|
|
68
|
+
let delta = b.lon - a.lon;
|
|
69
|
+
if (delta > 180)
|
|
70
|
+
delta -= 360;
|
|
71
|
+
if (delta < -180)
|
|
72
|
+
delta += 360;
|
|
73
|
+
const end = a.lon + delta;
|
|
74
|
+
if (end <= 180 && end >= -180)
|
|
75
|
+
return [[a, { lon: end, lat: b.lat }]];
|
|
76
|
+
const edge = end > 180 ? 180 : -180;
|
|
77
|
+
const span = end - a.lon;
|
|
78
|
+
const t = span === 0 ? 0 : (edge - a.lon) / span;
|
|
79
|
+
const lat = a.lat + (b.lat - a.lat) * t;
|
|
80
|
+
const other = edge === 180 ? -180 : 180;
|
|
81
|
+
const rest = end > 180 ? end - 360 : end + 360;
|
|
82
|
+
return [
|
|
83
|
+
[a, { lon: edge, lat }],
|
|
84
|
+
[{ lon: other, lat }, { lon: rest, lat: b.lat }],
|
|
85
|
+
];
|
|
86
|
+
}
|
|
87
|
+
/** Where a longitude and latitude fall on the painted texture, column and row. */
|
|
88
|
+
export function paintPixel(lon, lat) {
|
|
89
|
+
const u = lon / 360 + 0.5;
|
|
90
|
+
const v = 0.5 - lat / 180;
|
|
91
|
+
const x = Math.max(0, Math.min(PAINT_W - 1, Math.floor(u * PAINT_W)));
|
|
92
|
+
const y = Math.max(0, Math.min(PAINT_H - 1, Math.floor(v * PAINT_H)));
|
|
93
|
+
return { x, y };
|
|
94
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type * as THREE from "three";
|
|
2
|
+
import type { ThreeParts } from "../realistic/textures.ts";
|
|
3
|
+
/** Where the trail's picture comes from: its pixels are asked for only while it is on, so a page that paints nothing never makes them. */
|
|
4
|
+
export interface PaintSource {
|
|
5
|
+
data(): Uint8Array;
|
|
6
|
+
width: number;
|
|
7
|
+
height: number;
|
|
8
|
+
}
|
|
9
|
+
export interface PaintTexture {
|
|
10
|
+
uniforms: {
|
|
11
|
+
paintMap: {
|
|
12
|
+
value: THREE.Texture | null;
|
|
13
|
+
};
|
|
14
|
+
paintNow: {
|
|
15
|
+
value: number;
|
|
16
|
+
};
|
|
17
|
+
paintSpan: {
|
|
18
|
+
value: number;
|
|
19
|
+
};
|
|
20
|
+
paintOn: {
|
|
21
|
+
value: number;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
/** Whether the picture is on the graphics card now. */
|
|
25
|
+
readonly held: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Make the picture when painting goes on (and send it), send it again when `changed`, and give it back when painting is off.
|
|
28
|
+
* With painting off nothing is made and nothing is held.
|
|
29
|
+
*/
|
|
30
|
+
sync(on: boolean, changed: boolean): void;
|
|
31
|
+
dispose(): void;
|
|
32
|
+
}
|
|
33
|
+
/** A texture over the trail (equirectangular, north in the first row), made on the first paint and freed on clear. The same uniforms go on the sphere and on any tile laid over it. */
|
|
34
|
+
export declare function createPaintTexture(three: ThreeParts, source: PaintSource): PaintTexture;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/** A texture over the trail (equirectangular, north in the first row), made on the first paint and freed on clear. The same uniforms go on the sphere and on any tile laid over it. */
|
|
2
|
+
export function createPaintTexture(three, source) {
|
|
3
|
+
const DataTexture = three.DataTexture;
|
|
4
|
+
const uniforms = {
|
|
5
|
+
paintMap: { value: null },
|
|
6
|
+
paintNow: { value: 0 },
|
|
7
|
+
paintSpan: { value: 1440 },
|
|
8
|
+
paintOn: { value: 0 },
|
|
9
|
+
};
|
|
10
|
+
let texture = null;
|
|
11
|
+
const make = () => {
|
|
12
|
+
const data = source.data();
|
|
13
|
+
const made = DataTexture ? new DataTexture(data, source.width, source.height) : new three.Texture();
|
|
14
|
+
if (!DataTexture)
|
|
15
|
+
made.image = { data, width: source.width, height: source.height };
|
|
16
|
+
made.generateMipmaps = false;
|
|
17
|
+
made.flipY = false;
|
|
18
|
+
made.magFilter = three.LinearFilter;
|
|
19
|
+
made.minFilter = three.LinearFilter;
|
|
20
|
+
made.wrapS = three.ClampToEdgeWrapping;
|
|
21
|
+
made.wrapT = three.ClampToEdgeWrapping;
|
|
22
|
+
const space = three.NoColorSpace;
|
|
23
|
+
if (space)
|
|
24
|
+
made.colorSpace = space;
|
|
25
|
+
made.needsUpdate = true;
|
|
26
|
+
return made;
|
|
27
|
+
};
|
|
28
|
+
const free = () => {
|
|
29
|
+
texture?.dispose();
|
|
30
|
+
texture = null;
|
|
31
|
+
uniforms.paintMap.value = null;
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
uniforms,
|
|
35
|
+
get held() {
|
|
36
|
+
return texture !== null;
|
|
37
|
+
},
|
|
38
|
+
sync(on, changed) {
|
|
39
|
+
if (!on) {
|
|
40
|
+
free();
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (texture === null) {
|
|
44
|
+
texture = make();
|
|
45
|
+
uniforms.paintMap.value = texture;
|
|
46
|
+
}
|
|
47
|
+
else if (changed)
|
|
48
|
+
texture.needsUpdate = true;
|
|
49
|
+
},
|
|
50
|
+
dispose: free,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
@@ -136,6 +136,15 @@ export interface EarthDeps {
|
|
|
136
136
|
placeSprites?: () => PlaceSpriteData | null;
|
|
137
137
|
/** Told once, when the rings of the places that are not markers of their own were first wanted: whether the picture can draw them (false when the three.js parts handed over lack `Points`). */
|
|
138
138
|
onPlaceRings?: (drawn: boolean) => void;
|
|
139
|
+
/** The painted satellite trail, when the element has one. Read at every draw; nothing is uploaded while `step` returns false. */
|
|
140
|
+
paint?: {
|
|
141
|
+
data(): Uint8Array;
|
|
142
|
+
width: number;
|
|
143
|
+
height: number;
|
|
144
|
+
step(ms: number): boolean;
|
|
145
|
+
span(): number;
|
|
146
|
+
on(): boolean;
|
|
147
|
+
};
|
|
139
148
|
}
|
|
140
149
|
export declare const newModel: () => EarthModel;
|
|
141
150
|
/**
|
package/dist/realistic/earth.js
CHANGED
|
@@ -6,11 +6,13 @@ import { createSatelliteLayer, newSatelliteState } from "../space/satellite-laye
|
|
|
6
6
|
import { createTextureSet } from "./textures.js";
|
|
7
7
|
import { createTileSet } from "./tile-set.js";
|
|
8
8
|
import { createBodyLabelLayer, newBodyLabelState } from "./body-label.js";
|
|
9
|
-
import { createPlaceSprites } from "../markers/place-sprites.js";
|
|
9
|
+
import { beginPlaceSprites, createPlaceSprites } from "../markers/place-sprites.js";
|
|
10
10
|
import { createMeteorLayer, newMeteorState } from "../meteors/layer.js";
|
|
11
11
|
import { createBorderLayer, newBorderState } from "../borders/layer.js";
|
|
12
12
|
import { createGridLayer, newGridState } from "../grid/layer.js";
|
|
13
13
|
import { pointOnEarth } from "./tiles.js";
|
|
14
|
+
import { paintMinute } from "../paint/buffer.js";
|
|
15
|
+
import { createPaintTexture } from "../paint/texture.js";
|
|
14
16
|
const RAD = Math.PI / 180;
|
|
15
17
|
/**
|
|
16
18
|
* The glow reaches this many radii from the centre; the globe at 1x is drawn to fit inside it.
|
|
@@ -149,6 +151,8 @@ export function createEarthItem(model, deps) {
|
|
|
149
151
|
fragmentShader: SURFACE_FRAG,
|
|
150
152
|
uniforms: { dayMap: uniform.day, nightMap: uniform.night, waterMap: uniform.water, cloudMap: uniform.clouds, sunDir, sunLocal, cloudShift, lights: { value: 1 }, boost: { value: 0 }, ortho: { value: 1 } },
|
|
151
153
|
});
|
|
154
|
+
const paint = createPaintTexture(three, { data: () => deps.paint?.data() ?? new Uint8Array(4), width: deps.paint?.width ?? 1, height: deps.paint?.height ?? 1 });
|
|
155
|
+
Object.assign(surface.uniforms, paint.uniforms);
|
|
152
156
|
const cloudMaterial = new three.ShaderMaterial({
|
|
153
157
|
vertexShader: SURFACE_VERT,
|
|
154
158
|
fragmentShader: CLOUD_FRAG,
|
|
@@ -167,7 +171,7 @@ export function createEarthItem(model, deps) {
|
|
|
167
171
|
});
|
|
168
172
|
const turned = new three.Group();
|
|
169
173
|
const earth = new three.Mesh(grid, surface);
|
|
170
|
-
const tiles = createTileSet(three, turned, { sunDir, lights: surface.uniforms["lights"], boost: surface.uniforms["boost"], ortho: surface.uniforms["ortho"], water: uniform.water, night: uniform.night, extra: { cloudMap: uniform.clouds, sunLocal, cloudShift } }, { assets: deps.assets, sources: () => deps.sources?.() ?? {}, cap: deps.cap, invalidate: deps.invalidate, ...(deps.onTiles ? { onManifest: deps.onTiles } : {}) });
|
|
174
|
+
const tiles = createTileSet(three, turned, { sunDir, lights: surface.uniforms["lights"], boost: surface.uniforms["boost"], ortho: surface.uniforms["ortho"], water: uniform.water, night: uniform.night, extra: { cloudMap: uniform.clouds, sunLocal, cloudShift, ...paint.uniforms } }, { assets: deps.assets, sources: () => deps.sources?.() ?? {}, cap: deps.cap, invalidate: deps.invalidate, ...(deps.onTiles ? { onManifest: deps.onTiles } : {}) });
|
|
171
175
|
model.tiles = tiles;
|
|
172
176
|
const clouds = new three.Mesh(grid, cloudMaterial);
|
|
173
177
|
clouds.scale.setScalar(CLOUD_RADIUS);
|
|
@@ -193,8 +197,7 @@ export function createEarthItem(model, deps) {
|
|
|
193
197
|
let meteorTimer;
|
|
194
198
|
let nameOn = null;
|
|
195
199
|
let borders = null;
|
|
196
|
-
|
|
197
|
-
let placeSpritesTried = false;
|
|
200
|
+
const placeRings = { sprites: null, tried: false };
|
|
198
201
|
let gridLayer = null;
|
|
199
202
|
return {
|
|
200
203
|
scene,
|
|
@@ -289,13 +292,11 @@ export function createEarthItem(model, deps) {
|
|
|
289
292
|
borders.sync({ points: points.length > 0 ? points : [look], angle: Math.asin(Math.min(1, (1.4 * Math.hypot(size.width, size.height)) / 8 / radius)) / RAD, radius });
|
|
290
293
|
}
|
|
291
294
|
const spriteData = deps.placeSprites?.() ?? null;
|
|
292
|
-
if (spriteData
|
|
293
|
-
placeSprites = createPlaceSprites(three, turned, sunDir);
|
|
294
|
-
placeSpritesTried = true;
|
|
295
|
+
if (beginPlaceSprites(placeRings, spriteData, () => createPlaceSprites(three, turned, sunDir))) {
|
|
295
296
|
// told once: without `Points` the page's layer draws these rings itself
|
|
296
|
-
deps.onPlaceRings?.(
|
|
297
|
+
deps.onPlaceRings?.(placeRings.sprites !== null);
|
|
297
298
|
}
|
|
298
|
-
|
|
299
|
+
placeRings.sprites?.sync(spriteData, model.ratio(), size.width, size.height, model.light === "lit");
|
|
299
300
|
// the grid: made the first time it is switched on, over the ground and under the clouds
|
|
300
301
|
if (model.grid.on && !gridLayer && !model.grid.stats.failed) {
|
|
301
302
|
try {
|
|
@@ -307,6 +308,13 @@ export function createEarthItem(model, deps) {
|
|
|
307
308
|
}
|
|
308
309
|
}
|
|
309
310
|
gridLayer?.sync(radius);
|
|
311
|
+
if (deps.paint) {
|
|
312
|
+
const changed = deps.paint.step(model.time);
|
|
313
|
+
paint.sync(deps.paint.on(), changed);
|
|
314
|
+
paint.uniforms.paintNow.value = paintMinute(model.time);
|
|
315
|
+
paint.uniforms.paintSpan.value = deps.paint.span();
|
|
316
|
+
paint.uniforms.paintOn.value = deps.paint.on() ? 1 : 0;
|
|
317
|
+
}
|
|
310
318
|
if (model.meteors.on && !meteors)
|
|
311
319
|
meteors = createMeteorLayer(three, turned, model.meteors);
|
|
312
320
|
if (meteors) {
|
|
@@ -326,12 +334,13 @@ export function createEarthItem(model, deps) {
|
|
|
326
334
|
meteors?.dispose();
|
|
327
335
|
nameOn?.dispose();
|
|
328
336
|
borders?.dispose();
|
|
329
|
-
|
|
337
|
+
placeRings.sprites?.dispose();
|
|
330
338
|
gridLayer?.dispose();
|
|
331
339
|
model.tiles = null;
|
|
332
340
|
tiles.dispose();
|
|
333
341
|
textures.dispose();
|
|
334
342
|
grid.dispose();
|
|
343
|
+
paint.dispose();
|
|
335
344
|
surface.dispose();
|
|
336
345
|
cloudMaterial.dispose();
|
|
337
346
|
atmosphere.dispose();
|