@johnmorrisdotca/chikyu-earth 0.46.1 → 0.46.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/NOTICE.md +10 -0
- package/README.md +2 -2
- package/dist/door/gibs.d.ts +198 -0
- package/dist/door/gibs.js +403 -0
- package/dist/door/grid.d.ts +201 -0
- package/dist/door/grid.js +251 -0
- package/dist/door/index.d.ts +2 -0
- package/dist/door/index.js +2 -0
- package/dist/door/map.d.ts +14 -0
- package/dist/realistic/element-base.d.ts +6 -0
- package/dist/realistic/element-base.js +52 -7
- package/dist/realistic/index.d.ts +1 -1
- package/dist/realistic/index.js +1 -1
- package/dist/realistic/shaders.js +5 -3
- package/dist/realistic/tile-set.d.ts +11 -1
- package/dist/realistic/tile-set.js +148 -19
- package/dist/realistic/tiles.d.ts +20 -0
- package/dist/realistic/tiles.js +94 -1
- package/dist/space/element-base.d.ts +5 -0
- package/dist/space/element-base.js +41 -5
- package/dist/space/scene.d.ts +2 -0
- package/dist/space/scene.js +1 -0
- package/package.json +1 -1
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { gridOnEarth } from "../door/grid.js";
|
|
1
2
|
import { DEFAULT_LEVELS, tileKeyParts, tilePlace } from "../door/map.js";
|
|
2
3
|
import { TILE_FRAG, TILE_VERT } from "./shaders.js";
|
|
3
4
|
import { MAX_TEXTURE, TIERS } from "./tiers.js";
|
|
4
|
-
import { coveringTile, tileColumns, tileLevel, tileRect, tileRows, tilesInView } from "./tiles.js";
|
|
5
|
+
import { coveringTile, gridTilesInView, TEXEL_SLACK, tileColumns, tileLevel, tileRect, tileRows, tilesInView } from "./tiles.js";
|
|
5
6
|
/**
|
|
6
7
|
* The most tiles held at once, day and night together, about a megabyte and a third of graphics memory each (a 512 pixel square with its mipmaps).
|
|
7
8
|
*
|
|
@@ -38,6 +39,11 @@ export const TILE_KEEP_MS = 4000;
|
|
|
38
39
|
export const TILE_BUDGET = 72 * 1024 * 1024;
|
|
39
40
|
const IN_FLIGHT = 6;
|
|
40
41
|
const NEW_PER_DRAW = 8;
|
|
42
|
+
/** A finer tile fades in over this many milliseconds, then the loop stops. A reader who asked for less motion skips it. */
|
|
43
|
+
const FADE_MS = 300;
|
|
44
|
+
/** The package's own day pyramid, at level 5. A finer source is not asked until the view wants more than the tiles underneath, and never less than this when those tiles are not loaded yet. */
|
|
45
|
+
const OWN_SERVED = 32768;
|
|
46
|
+
const RAD = Math.PI / 180;
|
|
41
47
|
// the day mix reaches full light at 0.22: a tile whose darkest point is brighter than that never shows the night picture
|
|
42
48
|
const NIGHT_BELOW = 0.25;
|
|
43
49
|
const GRID = 16;
|
|
@@ -108,6 +114,10 @@ export function createTileSet(three, parent, shared, deps) {
|
|
|
108
114
|
const failed = new Map();
|
|
109
115
|
let queue = [];
|
|
110
116
|
let wantedKeys = new Set();
|
|
117
|
+
/** Grid tiles in view this frame, by the key the source named. */
|
|
118
|
+
let gridSeen = new Set();
|
|
119
|
+
let finerLevel = null;
|
|
120
|
+
let fading = false;
|
|
111
121
|
/** The sources the levels were read for. */
|
|
112
122
|
let current = {};
|
|
113
123
|
/** Counts the times the sources changed: an answer for an earlier set of them is let go. */
|
|
@@ -126,8 +136,9 @@ export function createTileSet(three, parent, shared, deps) {
|
|
|
126
136
|
let disposed = false;
|
|
127
137
|
const baseDay = TIERS.day[TIERS.day.length - 1];
|
|
128
138
|
const baseNight = TIERS.night[TIERS.night.length - 1];
|
|
139
|
+
const reduced = () => (deps.reducedMotion ?? (() => typeof globalThis.matchMedia === "function" && globalThis.matchMedia("(prefers-reduced-motion: reduce)").matches))();
|
|
129
140
|
const accountOf = (source, key) => `chikyu-earth:tile:${tagOf(source)}:${key}`;
|
|
130
|
-
const take = (layer, id, source) => deps.assets.acquire(accountOf(source, keyOf(layer, id)), () => {
|
|
141
|
+
const take = (layer, id, source, knownKey) => deps.assets.acquire(accountOf(source, knownKey ?? keyOf(layer === "finer" ? "day" : layer, id)), () => {
|
|
131
142
|
const texture = new three.Texture();
|
|
132
143
|
texture.wrapS = three.ClampToEdgeWrapping;
|
|
133
144
|
texture.wrapT = three.ClampToEdgeWrapping;
|
|
@@ -139,7 +150,7 @@ export function createTileSet(three, parent, shared, deps) {
|
|
|
139
150
|
made.ready = Promise.resolve()
|
|
140
151
|
.then(() => {
|
|
141
152
|
// the source names its tiles: the one for this place is the key of the pyramid's tile that matches
|
|
142
|
-
const key = source.need({ ...tilePlace(id), layer }, 1024 * 2 ** id.level).find((one) => {
|
|
153
|
+
const key = knownKey ?? source.need({ ...tilePlace(id), layer: layer === "finer" ? "day" : layer }, 1024 * 2 ** id.level).find((one) => {
|
|
143
154
|
const parts = tileKeyParts(one);
|
|
144
155
|
return parts !== undefined && parts.level === id.level && parts.col === id.col && parts.row === id.row;
|
|
145
156
|
});
|
|
@@ -237,13 +248,13 @@ export function createTileSet(three, parent, shared, deps) {
|
|
|
237
248
|
const pump = () => {
|
|
238
249
|
while (inFlight < IN_FLIGHT && queue.length > 0) {
|
|
239
250
|
const next = queue.shift();
|
|
240
|
-
const key =
|
|
251
|
+
const key = next.key;
|
|
241
252
|
if (entries.has(key))
|
|
242
253
|
continue;
|
|
243
|
-
const source = current[next.layer];
|
|
254
|
+
const source = next.layer === "finer" ? current.finer : current[next.layer];
|
|
244
255
|
if (source === undefined)
|
|
245
256
|
continue;
|
|
246
|
-
const entry = { key, layer: next.layer, id: next.id, source, asset: take(next.layer, next.id, source), ready: false, integrated: false, lastWanted: clock() };
|
|
257
|
+
const entry = { key, layer: next.layer, id: next.id, source, ...(next.grid ? { gridLevel: next.grid } : {}), asset: take(next.layer, next.id, source, next.grid ? key : undefined), ready: false, integrated: false, lastWanted: clock() };
|
|
247
258
|
entries.set(key, entry);
|
|
248
259
|
inFlight += 1;
|
|
249
260
|
requested += 1;
|
|
@@ -314,9 +325,18 @@ export function createTileSet(three, parent, shared, deps) {
|
|
|
314
325
|
}
|
|
315
326
|
return { level: null, day: [], night: [] };
|
|
316
327
|
};
|
|
328
|
+
const gridRect = (entry) => {
|
|
329
|
+
const grid = entry.source.grid;
|
|
330
|
+
const level = entry.gridLevel;
|
|
331
|
+
if (!grid || !level)
|
|
332
|
+
return tileRect(entry.id);
|
|
333
|
+
const span = gridOnEarth(level, entry.id.col, entry.id.row, grid.extent) ?? { lonMin: -180, lonMax: 180, latMin: -90, latMax: 90 };
|
|
334
|
+
return [span.lonMin * RAD, span.latMax * RAD, span.lonMax * RAD, span.latMin * RAD];
|
|
335
|
+
};
|
|
317
336
|
const makeMesh = (entry) => {
|
|
318
337
|
const nightRect = { value: [0, 0, 1, 1] };
|
|
319
338
|
const nightMap = { value: shared.night.value };
|
|
339
|
+
const cover = { value: 1 };
|
|
320
340
|
const material = new three.ShaderMaterial({
|
|
321
341
|
vertexShader: TILE_VERT,
|
|
322
342
|
fragmentShader: TILE_FRAG,
|
|
@@ -330,14 +350,22 @@ export function createTileSet(three, parent, shared, deps) {
|
|
|
330
350
|
ortho: shared.ortho,
|
|
331
351
|
...shared.extra,
|
|
332
352
|
nightRect,
|
|
333
|
-
|
|
334
|
-
//
|
|
335
|
-
|
|
353
|
+
tileAlpha: cover,
|
|
354
|
+
// the source's look: each colour multiplied, and clear below a luminance (the defaults leave a tile as it is: the clear range, in 0 to 1, lies below black)
|
|
355
|
+
tileGain: { value: entry.source.look?.gain ?? [1, 1, 1] },
|
|
356
|
+
tileClear: { value: entry.source.look ? [entry.source.look.clear[0] / 255, entry.source.look.clear[1] / 255] : [-2, -1] },
|
|
357
|
+
rect: { value: gridRect(entry) },
|
|
358
|
+
// a finer level sits a little higher, so two levels at once never fight for the same depth; a grid tile sits above the package's finest. All sit above the whole-Earth sphere's flat faces
|
|
359
|
+
radius: { value: entry.gridLevel ? 1.0008 : 1.0004 + 0.0001 * (entry.id.level - 3) },
|
|
336
360
|
},
|
|
337
361
|
polygonOffset: true,
|
|
338
362
|
polygonOffsetFactor: -1,
|
|
339
363
|
polygonOffsetUnits: -1,
|
|
364
|
+
// a source whose tiles have holes (no data, which the source made transparent) is drawn blended, so the picture under it shows there
|
|
365
|
+
transparent: entry.source.holes === true,
|
|
366
|
+
depthWrite: entry.source.holes !== true,
|
|
340
367
|
});
|
|
368
|
+
entry.cover = cover;
|
|
341
369
|
const mesh = new three.Mesh(geometry, material);
|
|
342
370
|
mesh.frustumCulled = false;
|
|
343
371
|
mesh.visible = false;
|
|
@@ -350,7 +378,7 @@ export function createTileSet(three, parent, shared, deps) {
|
|
|
350
378
|
const retarget = () => {
|
|
351
379
|
const night = nightLevels();
|
|
352
380
|
for (const entry of entries.values()) {
|
|
353
|
-
if (!entry.mesh || !entry.nightMap || !entry.nightRect)
|
|
381
|
+
if (!entry.mesh || !entry.nightMap || !entry.nightRect || entry.gridLevel)
|
|
354
382
|
continue;
|
|
355
383
|
let texture = null;
|
|
356
384
|
let rect = [0, 0, 1, 1];
|
|
@@ -380,6 +408,14 @@ export function createTileSet(three, parent, shared, deps) {
|
|
|
380
408
|
for (const entry of entries.values()) {
|
|
381
409
|
if (!entry.mesh)
|
|
382
410
|
continue;
|
|
411
|
+
if (entry.gridLevel) {
|
|
412
|
+
entry.mesh.visible = gridSeen.has(entry.key);
|
|
413
|
+
if (entry.mesh.visible) {
|
|
414
|
+
shown += 1;
|
|
415
|
+
used.set(entry.source.info.id, (used.get(entry.source.info.id) ?? 0) + 1);
|
|
416
|
+
}
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
383
419
|
let set = visible.get(entry.id.level);
|
|
384
420
|
if (!set) {
|
|
385
421
|
set = new Set(tilesInView(entry.id.level, camera).map((tile) => tile.row * tileColumns(tile.level) + tile.col));
|
|
@@ -444,25 +480,89 @@ export function createTileSet(three, parent, shared, deps) {
|
|
|
444
480
|
heldBytes: heldBytes(),
|
|
445
481
|
refused,
|
|
446
482
|
used: Object.fromEntries(used),
|
|
483
|
+
finer: finerLevel,
|
|
484
|
+
fading,
|
|
447
485
|
};
|
|
448
486
|
},
|
|
449
487
|
sync(camera, sun, night, ratio) {
|
|
450
488
|
if (disposed)
|
|
451
489
|
return;
|
|
452
490
|
const now = deps.sources();
|
|
453
|
-
if (now.day !== current.day || now.night !== current.night) {
|
|
491
|
+
if (now.day !== current.day || now.night !== current.night || now.finer !== current.finer) {
|
|
454
492
|
reset();
|
|
455
|
-
current = { ...(now.day ? { day: now.day } : {}), ...(now.night ? { night: now.night } : {}) };
|
|
456
|
-
if (current.day !== undefined)
|
|
493
|
+
current = { ...(now.day ? { day: now.day } : {}), ...(now.night ? { night: now.night } : {}), ...(now.finer ? { finer: now.finer } : {}) };
|
|
494
|
+
if (current.day !== undefined && !current.day.grid)
|
|
457
495
|
readManifest(current);
|
|
496
|
+
else if (current.day?.grid)
|
|
497
|
+
manifest = { layers: {} };
|
|
458
498
|
}
|
|
459
499
|
last = { camera };
|
|
460
|
-
|
|
500
|
+
const pyramidReady = current.day !== undefined && !current.day.grid && manifest != null && manifest.layers["day"] !== undefined;
|
|
501
|
+
if (current.day === undefined && current.finer === undefined)
|
|
502
|
+
return;
|
|
503
|
+
if (current.day !== undefined && !current.day.grid && manifest === undefined && current.finer === undefined)
|
|
461
504
|
return;
|
|
462
505
|
const at = clock();
|
|
463
|
-
const wanted = choose(camera, sun, night, ratio);
|
|
506
|
+
const wanted = pyramidReady ? choose(camera, sun, night, ratio) : { level: null, day: [], night: [] };
|
|
464
507
|
wantedLevel = wanted.level;
|
|
465
|
-
|
|
508
|
+
const needed = (2 * Math.PI * camera.radius * ratio) / TEXEL_SLACK;
|
|
509
|
+
const served = () => {
|
|
510
|
+
if (current.day?.grid)
|
|
511
|
+
return Math.max(...current.day.grid.levels.map((level) => level.width));
|
|
512
|
+
if (current.day === undefined)
|
|
513
|
+
return OWN_SERVED;
|
|
514
|
+
if (manifest === undefined)
|
|
515
|
+
return undefined;
|
|
516
|
+
const widths = manifest?.layers["day"]?.levels.map((level) => level.width) ?? [];
|
|
517
|
+
return widths.length > 0 ? Math.max(...widths) : OWN_SERVED;
|
|
518
|
+
};
|
|
519
|
+
const jobs = [];
|
|
520
|
+
const addGrid = (source, layer, minWidth, taken) => {
|
|
521
|
+
const grid = source.grid;
|
|
522
|
+
if (!grid)
|
|
523
|
+
return null;
|
|
524
|
+
const levels = grid.levels.filter((level) => level.width > minWidth).sort((a, b) => a.width - b.width);
|
|
525
|
+
if (levels.length === 0 || !(needed > minWidth))
|
|
526
|
+
return null;
|
|
527
|
+
let index = levels.findIndex((level) => level.width >= needed);
|
|
528
|
+
if (index < 0)
|
|
529
|
+
index = levels.length - 1;
|
|
530
|
+
while (index >= 0) {
|
|
531
|
+
const level = levels[index];
|
|
532
|
+
const tiles = gridTilesInView(level, grid.extent, camera, sun);
|
|
533
|
+
if (taken + tiles.length <= limit) {
|
|
534
|
+
for (const tile of tiles) {
|
|
535
|
+
const place = gridOnEarth(level, tile.col, tile.row, grid.extent);
|
|
536
|
+
if (!place)
|
|
537
|
+
continue;
|
|
538
|
+
const inset = {
|
|
539
|
+
lonMin: place.lonMin + (place.lonMax - place.lonMin) * 0.25,
|
|
540
|
+
lonMax: place.lonMax - (place.lonMax - place.lonMin) * 0.25,
|
|
541
|
+
latMin: place.latMin + (place.latMax - place.latMin) * 0.25,
|
|
542
|
+
latMax: place.latMax - (place.latMax - place.latMin) * 0.25,
|
|
543
|
+
layer: "day",
|
|
544
|
+
};
|
|
545
|
+
const key = source.need(inset, level.width).find((one) => {
|
|
546
|
+
const parts = tileKeyParts(one);
|
|
547
|
+
return parts !== undefined && parts.level === tile.level && parts.col === tile.col && parts.row === tile.row;
|
|
548
|
+
});
|
|
549
|
+
if (key)
|
|
550
|
+
jobs.push({ layer, id: { level: tile.level, col: tile.col, row: tile.row }, key, grid: level });
|
|
551
|
+
}
|
|
552
|
+
return level.level;
|
|
553
|
+
}
|
|
554
|
+
index -= 1;
|
|
555
|
+
}
|
|
556
|
+
return null;
|
|
557
|
+
};
|
|
558
|
+
finerLevel = null;
|
|
559
|
+
if (current.day?.grid)
|
|
560
|
+
wantedLevel = addGrid(current.day, "day", baseDay, 0);
|
|
561
|
+
const floor = served();
|
|
562
|
+
if (current.finer?.grid && floor !== undefined)
|
|
563
|
+
finerLevel = addGrid(current.finer, "finer", floor, wanted.day.length + wanted.night.length + jobs.length);
|
|
564
|
+
gridSeen = new Set(jobs.map((job) => job.key));
|
|
565
|
+
wantedKeys = new Set([...wanted.day.map((tile) => keyOf("day", tile)), ...wanted.night.map((id) => keyOf("night", id)), ...jobs.map((job) => job.key)]);
|
|
466
566
|
// a tile that failed is asked for again only after it has left the view and come back (the view changed), never on a timer and never while it stays in view: at rest the door sends nothing
|
|
467
567
|
for (const key of [...failed.keys()])
|
|
468
568
|
if (!wantedKeys.has(key))
|
|
@@ -472,7 +572,11 @@ export function createTileSet(three, parent, shared, deps) {
|
|
|
472
572
|
if (entry)
|
|
473
573
|
entry.lastWanted = at;
|
|
474
574
|
}
|
|
475
|
-
queue = [
|
|
575
|
+
queue = [
|
|
576
|
+
...wanted.day.map((tile) => ({ layer: "day", id: { level: tile.level, col: tile.col, row: tile.row }, key: keyOf("day", tile) })),
|
|
577
|
+
...wanted.night.map((id) => ({ layer: "night", id, key: keyOf("night", id) })),
|
|
578
|
+
...jobs.map((job) => ({ layer: job.layer, id: job.id, key: job.key, grid: job.grid })),
|
|
579
|
+
].filter((item) => !failed.has(item.key) && !entries.has(item.key));
|
|
476
580
|
pump();
|
|
477
581
|
let fresh = 0;
|
|
478
582
|
let waiting = false;
|
|
@@ -485,8 +589,18 @@ export function createTileSet(three, parent, shared, deps) {
|
|
|
485
589
|
}
|
|
486
590
|
fresh += 1;
|
|
487
591
|
entry.integrated = true;
|
|
488
|
-
if (entry.layer === "day")
|
|
592
|
+
if (entry.layer === "day" || entry.layer === "finer")
|
|
489
593
|
makeMesh(entry);
|
|
594
|
+
if (entry.gridLevel && entry.cover) {
|
|
595
|
+
entry.born = at;
|
|
596
|
+
const full = reduced();
|
|
597
|
+
entry.cover.value = full ? 1 : 0;
|
|
598
|
+
const material = entry.mesh?.material;
|
|
599
|
+
if (material && !full) {
|
|
600
|
+
material.transparent = true;
|
|
601
|
+
material.depthWrite = false;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
490
604
|
}
|
|
491
605
|
sweep();
|
|
492
606
|
retarget();
|
|
@@ -494,7 +608,22 @@ export function createTileSet(three, parent, shared, deps) {
|
|
|
494
608
|
if (wanted.level !== null)
|
|
495
609
|
visible.set(wanted.level, new Set(wanted.day.map((tile) => tile.row * tileColumns(tile.level) + tile.col)));
|
|
496
610
|
place(camera, visible);
|
|
497
|
-
|
|
611
|
+
fading = false;
|
|
612
|
+
for (const entry of entries.values()) {
|
|
613
|
+
if (!entry.gridLevel || !entry.cover || !entry.integrated)
|
|
614
|
+
continue;
|
|
615
|
+
const t = reduced() ? 1 : Math.min(1, (clock() - (entry.born ?? at)) / FADE_MS);
|
|
616
|
+
entry.cover.value = t;
|
|
617
|
+
const material = entry.mesh?.material;
|
|
618
|
+
if (material) {
|
|
619
|
+
const holes = entry.source.holes === true;
|
|
620
|
+
material.transparent = t < 1 || holes;
|
|
621
|
+
material.depthWrite = t >= 1 && !holes;
|
|
622
|
+
}
|
|
623
|
+
if (t < 1)
|
|
624
|
+
fading = true;
|
|
625
|
+
}
|
|
626
|
+
if (waiting || fading)
|
|
498
627
|
deps.invalidate();
|
|
499
628
|
arm();
|
|
500
629
|
},
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type MapGrid, type MapGridLevel } from "../door/grid.ts";
|
|
1
2
|
/**
|
|
2
3
|
* One tile of the map pyramid: a level, a column counted east from longitude -180 and a row counted south from latitude 90.
|
|
3
4
|
*
|
|
@@ -233,3 +234,22 @@ export declare function parseManifest(json: unknown): {
|
|
|
233
234
|
tileSize: number;
|
|
234
235
|
layers: Record<string, TileLayer>;
|
|
235
236
|
} | undefined;
|
|
237
|
+
/**
|
|
238
|
+
* The tiles of one level of a source's own grid that are in view. Only the columns and rows under the box are tested, so a fine matrix (hundreds of columns) is not walked. A tile that misses the Earth is left out.
|
|
239
|
+
*
|
|
240
|
+
* @param level - A level of the source's grid.
|
|
241
|
+
* @param extent - The Earth's extent, from the same grid.
|
|
242
|
+
* @param camera - The camera.
|
|
243
|
+
* @param sun - The Sun's direction as the camera sees it, for each tile's `ndl`; left out, `ndl` is 1.
|
|
244
|
+
* @returns The tiles in view, the nearest to the middle of the box first.
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* ```ts
|
|
248
|
+
* import { gibsGrid } from "@johnmorrisdotca/chikyu-earth/door";
|
|
249
|
+
* import { gridTilesInView } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
250
|
+
*
|
|
251
|
+
* const grid = gibsGrid("250m");
|
|
252
|
+
* console.log(gridTilesInView(grid.levels[6]!, grid.extent, { lon: 0, lat: 0, width: 800, height: 600, panX: 0, panY: 0, radius: 8000 }).length > 0);
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
255
|
+
export declare function gridTilesInView(level: MapGridLevel, extent: MapGrid["extent"], camera: TileCamera, sun?: readonly [number, number, number]): VisibleTile[];
|
package/dist/realistic/tiles.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// The maths of the Earth's map tiles, with no browser and no three.js: which pyramid level a zoom needs, and which tiles of that level are in view. Pure.
|
|
2
2
|
//
|
|
3
3
|
// A tile set is the map cut into 512 pixel squares: level L is `1024 * 2^L` pixels across, in `2^(L+1)` columns and `2^L` rows, column 0 at longitude -180 and row 0 at
|
|
4
|
-
// latitude 90, a tile at `<layer>/<level>/<column>/<row>.webp` (`scripts/make-earth-tiles.py` makes one).
|
|
4
|
+
// latitude 90, a tile at `<layer>/<level>/<column>/<row>.webp` (`scripts/make-earth-tiles.py` makes one). A source may declare a different grid; `gridTilesInView` walks only the columns and rows the view can see.
|
|
5
|
+
import { gridOnEarth, gridSpan, gridTileAt } from "../door/grid.js";
|
|
5
6
|
const RAD = Math.PI / 180;
|
|
6
7
|
/**
|
|
7
8
|
* How much larger than a device pixel a map texel may be before the next level is wanted: 1.25 is a quarter larger.
|
|
@@ -282,3 +283,95 @@ export function parseManifest(json) {
|
|
|
282
283
|
}
|
|
283
284
|
return Object.keys(layers).length > 0 ? { tileSize: 512, layers } : undefined;
|
|
284
285
|
}
|
|
286
|
+
/**
|
|
287
|
+
* The tiles of one level of a source's own grid that are in view. Only the columns and rows under the box are tested, so a fine matrix (hundreds of columns) is not walked. A tile that misses the Earth is left out.
|
|
288
|
+
*
|
|
289
|
+
* @param level - A level of the source's grid.
|
|
290
|
+
* @param extent - The Earth's extent, from the same grid.
|
|
291
|
+
* @param camera - The camera.
|
|
292
|
+
* @param sun - The Sun's direction as the camera sees it, for each tile's `ndl`; left out, `ndl` is 1.
|
|
293
|
+
* @returns The tiles in view, the nearest to the middle of the box first.
|
|
294
|
+
*
|
|
295
|
+
* @example
|
|
296
|
+
* ```ts
|
|
297
|
+
* import { gibsGrid } from "@johnmorrisdotca/chikyu-earth/door";
|
|
298
|
+
* import { gridTilesInView } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
299
|
+
*
|
|
300
|
+
* const grid = gibsGrid("250m");
|
|
301
|
+
* console.log(gridTilesInView(grid.levels[6]!, grid.extent, { lon: 0, lat: 0, width: 800, height: 600, panX: 0, panY: 0, radius: 8000 }).length > 0);
|
|
302
|
+
* ```
|
|
303
|
+
*/
|
|
304
|
+
export function gridTilesInView(level, extent, camera, sun) {
|
|
305
|
+
const lonRad = camera.lon * RAD;
|
|
306
|
+
const latRad = camera.lat * RAD;
|
|
307
|
+
const look = camera.perspective;
|
|
308
|
+
const margin = Math.min(160, Math.min(camera.width, camera.height) / 4);
|
|
309
|
+
const cx = camera.width / 2 + camera.panX;
|
|
310
|
+
const cy = camera.height / 2 + camera.panY;
|
|
311
|
+
const eyeLength = look ? Math.hypot(...look.eye) : 1;
|
|
312
|
+
const ndlOf = (z) => (sun ? z[0] * sun[0] + z[1] * sun[1] + z[2] * sun[2] : 1);
|
|
313
|
+
const sample = (lon, lat) => {
|
|
314
|
+
if (!look) {
|
|
315
|
+
const n = turned(lon * RAD, lat * RAD, lonRad, latRad);
|
|
316
|
+
const at = { x: cx + n[0] * camera.radius, y: cy - n[1] * camera.radius };
|
|
317
|
+
return { n, screen: n[2] < 0 ? null : at, flat: at, toward: n[2] };
|
|
318
|
+
}
|
|
319
|
+
const cl = Math.cos(lat * RAD);
|
|
320
|
+
const n = [cl * Math.sin(lon * RAD), Math.sin(lat * RAD), cl * Math.cos(lon * RAD)];
|
|
321
|
+
const along = n[0] * look.eye[0] + n[1] * look.eye[1] + n[2] * look.eye[2];
|
|
322
|
+
const screen = along < 1 ? null : look.project(n);
|
|
323
|
+
return { n, screen, flat: screen, toward: along / eyeLength };
|
|
324
|
+
};
|
|
325
|
+
const candidates = new Set();
|
|
326
|
+
const step = 24;
|
|
327
|
+
for (let y = 0; y <= camera.height + step - 1; y += step) {
|
|
328
|
+
for (let x = 0; x <= camera.width + step - 1; x += step) {
|
|
329
|
+
const place = pointOnEarth(camera, Math.min(x, camera.width), Math.min(y, camera.height));
|
|
330
|
+
if (!place)
|
|
331
|
+
continue;
|
|
332
|
+
const at = gridTileAt(level, place.lon, place.lat, extent);
|
|
333
|
+
if (!at)
|
|
334
|
+
continue;
|
|
335
|
+
for (let dr = -1; dr <= 1; dr += 1) {
|
|
336
|
+
for (let dc = -1; dc <= 1; dc += 1) {
|
|
337
|
+
const col = at.col + dc;
|
|
338
|
+
const row = at.row + dr;
|
|
339
|
+
if (col < 0 || row < 0 || col >= level.columns || row >= level.rows)
|
|
340
|
+
continue;
|
|
341
|
+
if (!gridOnEarth(level, col, row, extent))
|
|
342
|
+
continue;
|
|
343
|
+
candidates.add(row * level.columns + col);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
const found = [];
|
|
349
|
+
for (const key of candidates) {
|
|
350
|
+
const col = key % level.columns;
|
|
351
|
+
const row = Math.floor(key / level.columns);
|
|
352
|
+
const span = gridSpan(level, col, row);
|
|
353
|
+
const west = Math.max(span.lonMin, extent.west);
|
|
354
|
+
const east = Math.min(span.lonMax, extent.east);
|
|
355
|
+
const north = Math.min(span.latMax, extent.north);
|
|
356
|
+
const south = Math.max(span.latMin, extent.south);
|
|
357
|
+
const middle = sample((west + east) / 2, (north + south) / 2);
|
|
358
|
+
if (middle.toward < -0.4)
|
|
359
|
+
continue;
|
|
360
|
+
let inside = false;
|
|
361
|
+
let darkest = 1;
|
|
362
|
+
for (let j = 0; j <= 2; j += 1) {
|
|
363
|
+
for (let i = 0; i <= 2; i += 1) {
|
|
364
|
+
const p = sample(west + ((east - west) * i) / 2, north + ((south - north) * j) / 2);
|
|
365
|
+
darkest = Math.min(darkest, ndlOf(p.n));
|
|
366
|
+
if (p.screen && p.screen.x >= -margin && p.screen.x <= camera.width + margin && p.screen.y >= -margin && p.screen.y <= camera.height + margin)
|
|
367
|
+
inside = true;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
if (!inside)
|
|
371
|
+
continue;
|
|
372
|
+
const d = middle.flat ? Math.hypot(middle.flat.x - camera.width / 2, middle.flat.y - camera.height / 2) : Math.hypot(camera.width, camera.height);
|
|
373
|
+
found.push({ level: level.level, col, row, d, ndl: darkest });
|
|
374
|
+
}
|
|
375
|
+
found.sort((a, b) => a.d - b.d);
|
|
376
|
+
return found;
|
|
377
|
+
}
|
|
@@ -210,6 +210,11 @@ export declare class ChikyuSpace extends HTMLElement {
|
|
|
210
210
|
/** Where the finer map tiles are served from (a folder with `manifest.json`), or `null` for none; the attribute `tiles`. The Earth lays only the tiles in view over its own picture, at the level the camera's height needs, so a ride a few hundred kilometres above the ground sees sharp ground. */
|
|
211
211
|
get tiles(): string | null;
|
|
212
212
|
set tiles(value: string | null);
|
|
213
|
+
/**
|
|
214
|
+
* Finer imagery than the package's own tiles, read by the browser from NASA's imagery service. `gibs` turns it on. See `<chikyu-earth-realistic>` for the layer and the day.
|
|
215
|
+
*/
|
|
216
|
+
get tilesFiner(): string | null;
|
|
217
|
+
set tilesFiner(value: string | null);
|
|
213
218
|
/**
|
|
214
219
|
* True for the full-screen mode (the `immersive` attribute): the element fills its box in any shape (the Earth fits the shorter side with its glow inside it, `earth-size`), a plain wheel zooms instead of scrolling
|
|
215
220
|
* the page, a drag anywhere on it turns the view, and a touch drag turns it instead of scrolling. A notch of the wheel is about 1.2 times; out far enough the Moon, the Sun and the planets come into view.
|
|
@@ -20,6 +20,7 @@ import { COUNTRY_NAMES_STYLE, createCountryHost } from "../country-names/host.js
|
|
|
20
20
|
import { SEA_NAMES_ATTRIBUTES, readSeaNames } from "../sea-names/geometry.js";
|
|
21
21
|
import { SEA_NAMES_STYLE, createSeaHost } from "../sea-names/host.js";
|
|
22
22
|
import { createGate, frameCap } from "../realistic/gate.js";
|
|
23
|
+
import { createFinerHolder, GIBS_ID } from "../door/gibs.js";
|
|
23
24
|
import { folderSource } from "../door/map.js";
|
|
24
25
|
import { setThreeLoader, takeRenderer } from "../realistic/shared.js";
|
|
25
26
|
import { NOTES_STYLE, setAirNote } from "../realistic/air-note.js";
|
|
@@ -156,8 +157,11 @@ const STYLE = `:host{display:block;aspect-ratio:1/1;position:relative;overflow:h
|
|
|
156
157
|
* ```
|
|
157
158
|
*/
|
|
158
159
|
export class ChikyuSpace extends HTMLElement {
|
|
159
|
-
static observedAttributes = ["cloud-drift", "camera", "earth-size", "earth-x", "earth-y", "center", "time", "speed", "scale", "labels", "orbits", "bodies", "light", "meteors", "meteors-label", "meteors-strength", ...BODY_LABEL_ATTRIBUTES, ...SKY_ATTRIBUTES, ...CITY_NAMES_ATTRIBUTES, ...BORDER_ATTRIBUTES, ...GRID_ATTRIBUTES, ...COUNTRY_NAMES_ATTRIBUTES, ...SEA_NAMES_ATTRIBUTES, "air-thickness", "air-label", "lang", "places", "places-look", "place-groups", "here-city", "route", "bodies-credit", "satellite-tint", "satellite-streak-max", "tiles", "ride-viewpoint"];
|
|
160
|
+
static observedAttributes = ["cloud-drift", "camera", "earth-size", "earth-x", "earth-y", "center", "time", "speed", "scale", "labels", "orbits", "bodies", "light", "meteors", "meteors-label", "meteors-strength", ...BODY_LABEL_ATTRIBUTES, ...SKY_ATTRIBUTES, ...CITY_NAMES_ATTRIBUTES, ...BORDER_ATTRIBUTES, ...GRID_ATTRIBUTES, ...COUNTRY_NAMES_ATTRIBUTES, ...SEA_NAMES_ATTRIBUTES, "air-thickness", "air-label", "lang", "places", "places-look", "place-groups", "here-city", "route", "bodies-credit", "satellite-tint", "satellite-streak-max", "tiles", "tiles-finer", "tiles-finer-layer", "tiles-finer-date", "tiles-finer-timeout", "ride-viewpoint"];
|
|
160
161
|
#model = newSpaceModel();
|
|
162
|
+
/** The finer imagery (NASA's tile service), one source while the attributes stay the same. Off unless `tiles-finer="gibs"`. */
|
|
163
|
+
#finerHold = createFinerHolder(() => new Date(this.#model.time), () => this.#gate?.request());
|
|
164
|
+
#finerNote;
|
|
161
165
|
#paint = createPaintState();
|
|
162
166
|
#mode = "now";
|
|
163
167
|
#speed = 1;
|
|
@@ -244,13 +248,14 @@ export class ChikyuSpace extends HTMLElement {
|
|
|
244
248
|
if (!this.hasAttribute("aria-label"))
|
|
245
249
|
this.setAttribute("aria-label", "The Earth, the Moon, the Sun and the planets in their places for the date shown");
|
|
246
250
|
const root = this.shadowRoot ?? this.attachShadow({ mode: "open" });
|
|
247
|
-
root.innerHTML = `<div id="stage"></div><div id="places"></div><div id="labels" aria-hidden="true"></div><div id="notes"><p id="air-note" hidden></p><p id="meteor-note" hidden></p><p id="bodies-note" hidden></p></div>`;
|
|
251
|
+
root.innerHTML = `<div id="stage"></div><div id="places"></div><div id="labels" aria-hidden="true"></div><div id="notes"><p id="air-note" hidden></p><p id="meteor-note" hidden></p><p id="bodies-note" hidden></p><p id="finer-credit" hidden></p></div>`;
|
|
248
252
|
adoptStyles(root, STYLE, this.nonce);
|
|
249
253
|
this.#stage = root.getElementById("stage");
|
|
250
254
|
this.#labelBox = root.getElementById("labels");
|
|
251
255
|
this.#note = root.getElementById("meteor-note");
|
|
252
256
|
this.#airNote = root.getElementById("air-note");
|
|
253
257
|
this.#bodiesNote = root.getElementById("bodies-note");
|
|
258
|
+
this.#finerNote = root.getElementById("finer-credit");
|
|
254
259
|
this.#sky = createSkyHost({
|
|
255
260
|
root,
|
|
256
261
|
attribute: (name) => this.getAttribute(name),
|
|
@@ -347,6 +352,7 @@ export class ChikyuSpace extends HTMLElement {
|
|
|
347
352
|
onPainterError: (error) => this.dispatchEvent(new CustomEvent("chikyu-bodies-unavailable", { detail: { reason: error instanceof Error ? error.message : String(error) } })),
|
|
348
353
|
sources: () => this.#tileSources(),
|
|
349
354
|
onTiles: (layers) => this.dispatchEvent(new CustomEvent("chikyu-tiles", { detail: { layers } })),
|
|
355
|
+
onTilesDrawn: () => this.#showFinerCredit(),
|
|
350
356
|
});
|
|
351
357
|
this.#view = taken.renderer.view(this.#stage, item, { still: null });
|
|
352
358
|
this.#set("loading");
|
|
@@ -503,18 +509,36 @@ export class ChikyuSpace extends HTMLElement {
|
|
|
503
509
|
this.#model.satellites.streakMax = readStreakMax(value);
|
|
504
510
|
this.#gate?.request();
|
|
505
511
|
}
|
|
506
|
-
else if (name === "tiles")
|
|
512
|
+
else if (name === "tiles" || name === "tiles-finer" || name === "tiles-finer-layer" || name === "tiles-finer-date" || name === "tiles-finer-timeout")
|
|
507
513
|
this.#gate?.request();
|
|
508
514
|
if (name === "air-thickness" || name === "air-label" || name === "lang")
|
|
509
515
|
setAirNote(this.#airNote, this, this.#model.airThickness);
|
|
510
516
|
if (name === "bodies-credit" || name === "lang")
|
|
511
517
|
this.#showBodiesCredit();
|
|
518
|
+
if (name === "lang")
|
|
519
|
+
this.#showFinerCredit();
|
|
512
520
|
}
|
|
513
521
|
#wheel = (event) => immersiveWheel(this, event);
|
|
514
522
|
/** The map source of each layer now: the folder of the `tiles` attribute, or none. */
|
|
523
|
+
#showFinerCredit() {
|
|
524
|
+
const note = this.#finerNote;
|
|
525
|
+
if (!note)
|
|
526
|
+
return;
|
|
527
|
+
const count = this.#model.tiles?.stats.used[GIBS_ID] ?? 0;
|
|
528
|
+
const lang = (this.closest("[lang]")?.getAttribute("lang") ?? "").toLowerCase().startsWith("ja") ? "ja" : "en";
|
|
529
|
+
const finer = this.#finerHold.source((attr) => this.getAttribute(attr), false);
|
|
530
|
+
const text = count > 0 && finer ? finer.info.credit[lang] : "";
|
|
531
|
+
if (note.textContent !== text)
|
|
532
|
+
note.textContent = text;
|
|
533
|
+
if (note.lang !== lang)
|
|
534
|
+
note.lang = lang;
|
|
535
|
+
if (note.hidden !== (text === ""))
|
|
536
|
+
note.hidden = text === "";
|
|
537
|
+
}
|
|
515
538
|
#tileSources() {
|
|
516
539
|
const folder = this.tiles === null ? undefined : folderSource(this.tiles);
|
|
517
|
-
|
|
540
|
+
const finer = this.#finerHold.source((attr) => this.getAttribute(attr), false);
|
|
541
|
+
return { ...(folder ? { day: folder, night: folder } : {}), ...(finer ? { finer } : {}) };
|
|
518
542
|
}
|
|
519
543
|
/** Where the finer map tiles are served from (a folder with `manifest.json`), or `null` for none; the attribute `tiles`. The Earth lays only the tiles in view over its own picture, at the level the camera's height needs, so a ride a few hundred kilometres above the ground sees sharp ground. */
|
|
520
544
|
get tiles() {
|
|
@@ -529,6 +553,18 @@ export class ChikyuSpace extends HTMLElement {
|
|
|
529
553
|
else
|
|
530
554
|
this.setAttribute("tiles", value);
|
|
531
555
|
}
|
|
556
|
+
/**
|
|
557
|
+
* Finer imagery than the package's own tiles, read by the browser from NASA's imagery service. `gibs` turns it on. See `<chikyu-earth-realistic>` for the layer and the day.
|
|
558
|
+
*/
|
|
559
|
+
get tilesFiner() {
|
|
560
|
+
return this.getAttribute("tiles-finer") === "gibs" ? "gibs" : null;
|
|
561
|
+
}
|
|
562
|
+
set tilesFiner(value) {
|
|
563
|
+
if (value === "gibs")
|
|
564
|
+
this.setAttribute("tiles-finer", "gibs");
|
|
565
|
+
else
|
|
566
|
+
this.removeAttribute("tiles-finer");
|
|
567
|
+
}
|
|
532
568
|
/** The body painter's credit under the picture, in the element's or the page's language: shown while the painter draws the bodies (not `plain`), unless `bodies-credit` is `off`. */
|
|
533
569
|
/** The painter's credit as last told to the page (`chikyu-bodies-credit`): both languages, or empty for none. */
|
|
534
570
|
#creditKey = "";
|
|
@@ -1729,7 +1765,7 @@ export class ChikyuSpace extends HTMLElement {
|
|
|
1729
1765
|
bodyLabel: { on: this.#model.bodyLabel.settings !== null, text: this.#model.bodyLabel.text, ...this.#model.bodyLabel.drawn },
|
|
1730
1766
|
sky: this.#sky?.stats ?? { on: false, loaded: false, names: 0, figures: 0, picked: null, pick: null, hover: null, constellations: 0, culture: "iau", renders: 0, lang: "", stars: [] },
|
|
1731
1767
|
followModel: this.#model.followModel,
|
|
1732
|
-
tiles: this.#model.tiles?.stats ?? { ready: false, level: null, held: 0, shown: 0, loading: 0, failed: 0, requested: 0, heldBytes: 0, refused: 0, used: {} },
|
|
1768
|
+
tiles: this.#model.tiles?.stats ?? { ready: false, level: null, held: 0, shown: 0, loading: 0, failed: 0, requested: 0, heldBytes: 0, refused: 0, used: {}, finer: null, fading: false },
|
|
1733
1769
|
places: { ...(this.#placeLayer?.stats ?? { places: 0, shown: 0, labels: 0, renders: 0, rings: 0, satelliteNames: 0, marks: 0, groups: 0, sprites: 0, dots: 0, omitted: 0 }) },
|
|
1734
1770
|
hereCity: this.#here?.stats ?? { on: false, maxKm: 0, loaded: false, pending: false },
|
|
1735
1771
|
cityNames: this.#cities?.stats ?? { on: false, max: 0, radius: 0, tiers: [], cities: 0, names: 0, renders: 0 },
|
package/dist/space/scene.d.ts
CHANGED
|
@@ -447,6 +447,8 @@ export interface SpaceDeps {
|
|
|
447
447
|
sources?: () => TileSources;
|
|
448
448
|
/** Told once when the tile set has been read: the width in pixels of the finest level of each of its layers. */
|
|
449
449
|
onTiles?: (layers: Record<string, number>) => void;
|
|
450
|
+
/** Told at the end of a draw, after the map tiles are placed, so a credit line can follow which tiles are on screen. */
|
|
451
|
+
onTilesDrawn?: () => void;
|
|
450
452
|
}
|
|
451
453
|
/**
|
|
452
454
|
* The default look: a sphere in the planet's colour, lit by the Sun, and for the Sun a glowing disc.
|
package/dist/space/scene.js
CHANGED
|
@@ -755,6 +755,7 @@ export function createSpaceItem(model, deps) {
|
|
|
755
755
|
},
|
|
756
756
|
};
|
|
757
757
|
tiles.sync({ lon: 0, lat: 0, width: size.width, height: size.height, panX: 0, panY: 0, radius: focal / away, perspective: look }, [sunLocal.value.x, sunLocal.value.y, sunLocal.value.z], true, model.ratio());
|
|
758
|
+
deps.onTilesDrawn?.();
|
|
758
759
|
}
|
|
759
760
|
let paintedBodies = sunSlot.drawing ? 1 : 0;
|
|
760
761
|
for (const p of planets.values())
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johnmorrisdotca/chikyu-earth",
|
|
3
|
-
"version": "0.46.
|
|
3
|
+
"version": "0.46.4",
|
|
4
4
|
"description": "Chikyu Earth 地球 — A line-drawn globe for the web: country outlines that turn at the Earth's own rate, centred on the place you give it, with an optional realistic Earth from space (three.js), the Sun, Moon and planets, satellites on their orbits, and markers for any place. A web component and an SVG string.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"earth",
|