@johnmorrisdotca/wakusei-planets 0.31.0 → 0.32.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.
@@ -1,56 +1,68 @@
1
- import { chooseWindow, covers, parseManifest, tilePath, visibleRegion, windowRect, windowTiles, BASE_WIDTH, TEXEL_SLACK, TILE_SIZE, texelAngle } from "./tiles.js";
1
+ import { accountOf } from "../door/book.js";
2
+ import { DEFAULT_LEVELS, forgetManifests as forgetFolders, tileKeyParts } from "../door/map.js";
3
+ import { chooseWindow, covers, visibleRegion, windowRect, windowTiles, BASE_WIDTH, TEXEL_SLACK, TILE_SIZE, texelAngle } from "./tiles.js";
2
4
  /** Milliseconds a window that the view no longer needs is kept (the view may come straight back) before its texture is freed. */
3
5
  export const DETAIL_KEEP_MS = 4000;
4
6
  /** Milliseconds a changed view must stay changed before a new window is fetched, so a drag or a pinch does not fetch what it passes. */
5
7
  export const DETAIL_SETTLE_MS = 140;
6
- /** Decoded tiles kept (about a megabyte each), those of the window first. */
7
- export const DETAIL_TILES_KEPT = 48;
8
+ /** Decoded tiles a view's store keeps by default (about a megabyte and a half each), those of the window first: the view's 48 MB budget (`TILE_BUDGET_BYTES`). */
9
+ export const DETAIL_TILES_KEPT = 34;
10
+ /** Milliseconds a levels read that failed waits before it is asked again, and only when a view wants more than the body's own picture. */
8
11
  const RETRY_MS = 30_000;
9
- const IN_FLIGHT = 6;
10
- // One manifest read for each tile set of the page, shared by every element.
11
- const manifests = new Map();
12
- function readManifest(base, now) {
13
- const held = manifests.get(base);
14
- if (held !== undefined && now - held.at < RETRY_MS)
15
- return held.value;
16
- const value = fetch(`${base}manifest.json`, { mode: "cors" }).then(async (response) => (response.ok ? parseManifest(await response.json().catch(() => undefined)) : undefined), () => undefined);
17
- manifests.set(base, { at: now, value });
18
- return value;
19
- }
20
- /** Forget what was read of the tile sets (for a test that serves different ones). */
21
- export function forgetManifests() {
22
- manifests.clear();
23
- }
12
+ /**
13
+ * Forget what was read of the tile folders (for a test that serves different ones).
14
+ *
15
+ * @example
16
+ * ```ts no-run
17
+ * import { forgetManifests } from "@johnmorrisdotca/wakusei-planets/door";
18
+ *
19
+ * forgetManifests();
20
+ * ```
21
+ */
22
+ export const forgetManifests = forgetFolders;
24
23
  const keyOf = (w) => `${w.level}:${w.col0}:${w.cols}:${w.row0}:${w.rows}`;
24
+ /** The window as a place on the map, in degrees, pulled in a hair so that a tile's own edge does not count as the next tile. */
25
+ function placeOf(window) {
26
+ const lonMin = (window.col0 / window.columns - 0.5) * 360;
27
+ const latMax = 90 - (window.row0 / window.rowsTotal) * 180;
28
+ const inset = 1e-6;
29
+ return { lonMin: lonMin + inset, lonMax: lonMin + (window.cols / window.columns) * 360 - inset, latMax: latMax - inset, latMin: latMax - (window.rows / window.rowsTotal) * 180 + inset };
30
+ }
25
31
  /** Make the window of finer map for one body. */
26
32
  export function createDetail(T, deps) {
27
33
  const clock = deps.now ?? (() => performance.now());
28
- const cache = new Map();
29
- const inFlight = new Map();
34
+ const store = deps.book.store;
35
+ /** This drawing of the body: its tiles are held under its own name in the store. */
36
+ const owner = {};
30
37
  const failed = new Map();
31
- let manifest;
32
- let manifestFor = "";
33
- let manifestAsked = false;
34
- let askedAt = 0;
38
+ /** The bitmaps this body has seen come out of the store, to close the ones the store has let go. */
39
+ const seen = new Map();
40
+ let active;
35
41
  let levels;
42
+ let levelsRead = false;
43
+ let levelsAsked = false;
44
+ let askedAt = 0;
36
45
  let held;
37
46
  let loading;
38
47
  let pending;
39
48
  let freeTimer;
40
49
  let wantLevel = null;
50
+ let wantKey = "";
41
51
  let requested = 0;
42
52
  let gone = false;
53
+ let lastKeys;
54
+ let watching = false;
43
55
  // The Texture class of the three.js module the page handed over: `DataTexture` extends it, and a page's subset of three need not name it.
44
56
  const TextureClass = Object.getPrototypeOf(T.DataTexture);
45
- const forgetTiles = (keep) => {
46
- if (cache.size <= DETAIL_TILES_KEPT)
47
- return;
48
- const order = [...cache.entries()].filter(([k]) => !keep.has(k)).sort((a, b) => a[1].used - b[1].used);
49
- for (const [k, entry] of order) {
50
- if (cache.size <= DETAIL_TILES_KEPT)
51
- break;
52
- entry.bitmap.close();
53
- cache.delete(k);
57
+ const heldTiles = () => (lastKeys === undefined ? 0 : lastKeys.keys.filter((key) => store.get(lastKeys.account, key) !== undefined).length);
58
+ /** Closes the bitmaps the store no longer holds. */
59
+ const prune = () => {
60
+ for (const [key, one] of seen) {
61
+ const at = key.indexOf("\u0000");
62
+ if (store.get(one.account, key.slice(at + 1)) !== undefined)
63
+ continue;
64
+ one.image.close?.();
65
+ seen.delete(key);
54
66
  }
55
67
  };
56
68
  const release = () => {
@@ -66,25 +78,44 @@ export function createDetail(T, deps) {
66
78
  clearTimeout(pending.timer);
67
79
  pending = undefined;
68
80
  };
81
+ /** Stops the window being fetched: its loads are aborted (the tiles already here stay in the store until it needs the room). */
69
82
  const cancelLoading = () => {
70
- for (const controller of inFlight.values())
71
- controller.abort();
72
- inFlight.clear();
83
+ if (loading !== undefined) {
84
+ const at = accountOf(loading.source, deps.body, owner);
85
+ if (lastKeys?.account === at)
86
+ store.want(deps.book.loader(loading.source, deps.body, owner), []);
87
+ }
73
88
  loading = undefined;
74
89
  };
75
- const install = (window) => {
76
- const tiles = windowTiles(window);
90
+ /** Frees every tile of this body held in the store, and the bitmaps. */
91
+ const dropTiles = () => {
92
+ if (lastKeys !== undefined)
93
+ deps.book.dropAccount(lastKeys.source, deps.body, owner);
94
+ for (const one of seen.values())
95
+ one.image.close?.();
96
+ seen.clear();
97
+ lastKeys = undefined;
98
+ };
99
+ const install = (window, source, tiles) => {
100
+ const placed = windowTiles(window);
77
101
  const canvas = document.createElement("canvas");
78
102
  canvas.width = window.cols * TILE_SIZE;
79
103
  canvas.height = window.rows * TILE_SIZE;
80
104
  const context = canvas.getContext("2d");
81
105
  if (context === null)
82
- return;
83
- for (const tile of tiles) {
84
- const entry = cache.get(tilePath(deps.body, tile));
85
- if (entry === undefined)
86
- return;
87
- context.drawImage(entry.bitmap, tile.x, tile.y);
106
+ return false;
107
+ const account = accountOf(source, deps.body, owner);
108
+ for (const tile of placed) {
109
+ const key = tiles.get(`${tile.level}:${tile.col}:${tile.row}`);
110
+ const got = key === undefined ? undefined : store.get(account, key);
111
+ if (got === undefined)
112
+ return false;
113
+ try {
114
+ context.drawImage(got.image, tile.x, tile.y, TILE_SIZE, TILE_SIZE);
115
+ }
116
+ catch {
117
+ return false;
118
+ }
88
119
  }
89
120
  const texture = new TextureClass(canvas);
90
121
  texture.flipY = false;
@@ -95,7 +126,7 @@ export function createDetail(T, deps) {
95
126
  texture.anisotropy = 4;
96
127
  texture.needsUpdate = true;
97
128
  const old = held;
98
- held = { key: keyOf(window), window, texture, canvas };
129
+ held = { key: keyOf(window), window, texture, canvas, source: source.info, tiles: placed.length };
99
130
  const rect = windowRect(window);
100
131
  const full = window.cols >= window.columns;
101
132
  // a window that reaches a pole has no edge there: its rectangle goes a little past the pole and the picture is clamped
@@ -115,77 +146,82 @@ export function createDetail(T, deps) {
115
146
  old.canvas.width = 0;
116
147
  old.canvas.height = 0;
117
148
  }
118
- forgetTiles(new Set(tiles.map((t) => tilePath(deps.body, t))));
119
149
  deps.redraw();
150
+ return true;
151
+ };
152
+ /** Looks at the window being fetched: whole, it goes on the body; if nothing more is coming and it is not whole, it failed. */
153
+ const check = () => {
154
+ if (gone || loading === undefined)
155
+ return;
156
+ const now = loading;
157
+ const loader = deps.book.loader(now.source, deps.body, owner);
158
+ const got = store.want(loader, now.keys);
159
+ const account = accountOf(now.source, deps.body, owner);
160
+ for (const [key, tile] of got)
161
+ seen.set(`${account}\u0000${key}`, { image: tile.image, account });
162
+ if (now.keys.every((key) => got.has(key))) {
163
+ loading = undefined;
164
+ if (!install(now.window, now.source, now.tiles))
165
+ failed.set(now.key, clock());
166
+ prune();
167
+ // a window that arrives when the view no longer wants one is let go after the usual while: no draw may come to say so
168
+ if (wantLevel === null)
169
+ scheduleFree();
170
+ return;
171
+ }
172
+ const stats = store.stats();
173
+ if (stats.inFlight === 0 && stats.queued === 0) {
174
+ // a tile that cannot be had spoils the window: leave the picture as it is, and try again only when the view has changed
175
+ failed.set(now.key, clock());
176
+ cancelLoading();
177
+ prune();
178
+ return;
179
+ }
180
+ // the store says a tile has failed before it has let go of it: look again when nothing is running
181
+ if (!watching) {
182
+ watching = true;
183
+ void store.idle().then(() => {
184
+ watching = false;
185
+ check();
186
+ });
187
+ }
120
188
  };
121
- const load = (window) => {
189
+ const unwatch = deps.book.onChange(check);
190
+ const load = (window, source) => {
122
191
  const key = keyOf(window);
123
192
  if (loading?.key === key)
124
193
  return;
125
194
  cancelLoading();
126
- loading = { key, window };
127
- const base = deps.base();
128
- if (base === undefined)
195
+ let keys;
196
+ try {
197
+ const wanted = source.need({ ...placeOf(window), body: deps.body }, window.columns * TILE_SIZE);
198
+ keys = Array.isArray(wanted) ? wanted.filter((one) => typeof one === "string") : [];
199
+ }
200
+ catch {
201
+ failed.set(key, clock());
129
202
  return;
130
- const tiles = windowTiles(window).map((t) => ({ t, path: tilePath(deps.body, t) }));
131
- const todo = tiles.filter(({ path }) => !cache.has(path));
132
- for (const { path } of tiles) {
133
- const entry = cache.get(path);
134
- if (entry !== undefined)
135
- entry.used = clock();
136
203
  }
137
- const done = () => {
138
- if (gone || loading?.key !== key)
139
- return;
140
- if (tiles.every(({ path }) => cache.has(path))) {
141
- loading = undefined;
142
- install(window);
143
- }
144
- };
145
- if (todo.length === 0) {
146
- done();
204
+ const tiles = new Map();
205
+ for (const one of keys) {
206
+ const at = tileKeyParts(one);
207
+ if (at !== undefined)
208
+ tiles.set(`${at.level}:${at.col}:${at.row}`, one);
209
+ }
210
+ const need = windowTiles(window);
211
+ if (!need.every((tile) => tiles.has(`${tile.level}:${tile.col}:${tile.row}`))) {
212
+ failed.set(key, clock());
147
213
  return;
148
214
  }
149
- let next = 0;
150
- const start = () => {
151
- while (inFlight.size < IN_FLIGHT && next < todo.length) {
152
- const { path } = todo[next];
153
- next += 1;
154
- const controller = new AbortController();
155
- inFlight.set(path, controller);
156
- requested += 1;
157
- void fetch(`${base}${path}.webp`, { mode: "cors", signal: controller.signal })
158
- .then((response) => (response.ok ? response.blob() : Promise.reject(new Error(String(response.status)))))
159
- .then((blob) => createImageBitmap(blob))
160
- .then((bitmap) => {
161
- inFlight.delete(path);
162
- if (gone || loading?.key !== key) {
163
- bitmap.close();
164
- return;
165
- }
166
- cache.set(path, { bitmap, used: clock() });
167
- start();
168
- done();
169
- }, (error) => {
170
- inFlight.delete(path);
171
- if (gone || (error instanceof DOMException && error.name === "AbortError"))
172
- return;
173
- // a tile that cannot be had spoils the window: leave the picture as it is and try again after a while
174
- failed.set(key, clock());
175
- cancelLoading();
176
- });
177
- }
178
- };
179
- start();
180
- };
181
- const dropTiles = () => {
182
- for (const entry of cache.values())
183
- entry.bitmap.close();
184
- cache.clear();
215
+ const wanted = need.map((tile) => tiles.get(`${tile.level}:${tile.col}:${tile.row}`));
216
+ loading = { key, window, keys: wanted, tiles, source };
217
+ lastKeys = { source, account: accountOf(source, deps.body, owner), keys: wanted };
218
+ // the store starts what is not held and aborts what is no longer wanted; what is held comes back at once
219
+ store.want(deps.book.loader(source, deps.body, owner, () => (requested += 1)), wanted);
220
+ check();
185
221
  };
186
222
  // not wanted any more (zoomed out, or out of sight): after a few seconds the picture and every decoded tile are let go
187
223
  const scheduleFree = () => {
188
- if ((held === undefined && cache.size === 0) || freeTimer !== undefined)
224
+ if ((held === undefined && lastKeys === undefined) || freeTimer !== undefined)
189
225
  return;
190
226
  freeTimer = setTimeout(() => {
191
227
  freeTimer = undefined;
@@ -205,29 +241,67 @@ export function createDetail(T, deps) {
205
241
  clearTimeout(freeTimer);
206
242
  freeTimer = undefined;
207
243
  };
208
- const ask = (base) => {
209
- if (manifestAsked && manifestFor === base && (manifest !== undefined || clock() - askedAt < RETRY_MS))
244
+ const ask = (source) => {
245
+ if (levelsAsked && (levelsRead || clock() - askedAt < RETRY_MS))
210
246
  return;
211
- manifestAsked = true;
212
- manifestFor = base;
247
+ levelsAsked = true;
213
248
  askedAt = clock();
214
- void readManifest(base, clock()).then((read) => {
215
- if (gone || manifestFor !== base)
249
+ const controller = new AbortController();
250
+ // read once the view has settled, and only if the body is still drawn from this source: a page that changes `tiles` just after the element starts is not asked for a manifest it has just said it does not want
251
+ setTimeout(() => {
252
+ if (gone || active !== source || deps.source() !== source) {
253
+ // not wanted now: ask again for whatever the view is drawn from, at the draw this asks for
254
+ if (!gone && active === source) {
255
+ levelsAsked = false;
256
+ deps.redraw();
257
+ }
216
258
  return;
217
- manifest = read;
218
- levels = read?.bodies[deps.body];
219
- deps.onLevels?.(levels);
220
- if (levels !== undefined)
221
- deps.redraw();
222
- });
259
+ }
260
+ void Promise.resolve()
261
+ .then(() => (source.levels === undefined ? DEFAULT_LEVELS : source.levels(deps.body, controller.signal)))
262
+ .then((read) => {
263
+ if (gone || active !== source)
264
+ return;
265
+ levelsRead = read !== undefined;
266
+ levels = read !== undefined && read.length > 0 ? read : undefined;
267
+ deps.onLevels?.(levels);
268
+ // a draw tells this the view as it is now (never as it was): the window is chosen then
269
+ if (levels !== undefined)
270
+ deps.redraw();
271
+ }, () => undefined);
272
+ }, DETAIL_SETTLE_MS);
223
273
  };
224
274
  const idle = () => {
225
275
  wantLevel = null;
226
276
  cancelPending();
227
277
  scheduleFree();
228
278
  };
279
+ /** The source for this draw. A change of source (one added, removed or switched off) lets go of the window of the old one and starts again with the new. */
280
+ const take = () => {
281
+ const source = deps.source();
282
+ if (source === active)
283
+ return source;
284
+ // the new source is the active one before anything is told to draw, so a draw that comes back here finds nothing more to change
285
+ active = source;
286
+ cancelPending();
287
+ cancelLoading();
288
+ dropTiles();
289
+ failed.clear();
290
+ wantKey = "";
291
+ levels = undefined;
292
+ levelsRead = false;
293
+ levelsAsked = false;
294
+ if (held !== undefined) {
295
+ release();
296
+ deps.apply(null);
297
+ deps.redraw();
298
+ }
299
+ if (source === undefined)
300
+ deps.onLevels?.(undefined);
301
+ return source;
302
+ };
229
303
  // what the view needs, whoever works out the region: the window, loaded after the view has settled
230
- const track = (region, inside, radiansPixels) => {
304
+ const track = (source, region, inside, radiansPixels) => {
231
305
  const want = region === undefined ? undefined : chooseWindow(region, levels, radiansPixels);
232
306
  wantLevel = want?.level ?? null;
233
307
  if (want === undefined) {
@@ -243,16 +317,21 @@ export function createDetail(T, deps) {
243
317
  const key = keyOf(want);
244
318
  if (held?.key === key || loading?.key === key || pending?.key === key)
245
319
  return;
246
- const lastFailed = failed.get(key);
247
- if (lastFailed !== undefined && clock() - lastFailed < RETRY_MS)
320
+ // a window that failed is not asked for again while the view wants the same one; a changed view clears it
321
+ if (key !== wantKey) {
322
+ if (wantKey !== "")
323
+ failed.clear();
324
+ wantKey = key;
325
+ }
326
+ if (failed.has(key))
248
327
  return;
249
328
  cancelPending();
250
329
  pending = {
251
330
  key,
252
331
  timer: setTimeout(() => {
253
332
  pending = undefined;
254
- if (!gone)
255
- load(want);
333
+ if (!gone && active === source)
334
+ load(want, source);
256
335
  }, DETAIL_SETTLE_MS),
257
336
  };
258
337
  };
@@ -260,34 +339,34 @@ export function createDetail(T, deps) {
260
339
  sync(box, axes, viewWidth) {
261
340
  if (gone)
262
341
  return;
263
- const base = deps.base();
264
- if (base === undefined || !deps.allowed()) {
342
+ const source = take();
343
+ if (source === undefined || !deps.allowed()) {
265
344
  idle();
266
345
  return;
267
346
  }
268
- ask(base);
347
+ ask(source);
269
348
  if (levels === undefined)
270
349
  return;
271
350
  const region = visibleRegion(box, axes);
272
- track(region, region === undefined ? undefined : visibleRegion(box, axes, 0), (deps.ratio() * viewWidth) / Math.max(1e-9, box.right - box.left));
351
+ track(source, region, region === undefined ? undefined : visibleRegion(box, axes, 0), (deps.ratio() * viewWidth) / Math.max(1e-9, box.right - box.left));
273
352
  },
274
- follow(seen) {
353
+ follow(seen_) {
275
354
  if (gone)
276
355
  return;
277
- const base = deps.base();
278
- if (base === undefined || !deps.allowed()) {
356
+ const source = take();
357
+ if (source === undefined || !deps.allowed()) {
279
358
  idle();
280
359
  return;
281
360
  }
282
- // the tile set is read when the view wants more than the 2048 pixel map gives, not before: a page that never gets that close fetches nothing
283
- const radiansPixels = seen === undefined ? 0 : seen.radiansPixels * deps.ratio();
284
- if (seen !== undefined && radiansPixels / TEXEL_SLACK > BASE_WIDTH / (2 * Math.PI))
285
- ask(base);
361
+ // the levels are read when the view wants more than the 2048 pixel map gives, not before: a page that never gets that close fetches nothing
362
+ const radiansPixels = seen_ === undefined ? 0 : seen_.radiansPixels * deps.ratio();
363
+ if (seen_ !== undefined && radiansPixels / TEXEL_SLACK > BASE_WIDTH / (2 * Math.PI))
364
+ ask(source);
286
365
  if (levels === undefined) {
287
366
  idle();
288
367
  return;
289
368
  }
290
- track(seen?.region, seen?.inside, radiansPixels);
369
+ track(source, seen_?.region, seen_?.inside, radiansPixels);
291
370
  },
292
371
  rest() {
293
372
  if (!gone)
@@ -295,18 +374,21 @@ export function createDetail(T, deps) {
295
374
  },
296
375
  get stats() {
297
376
  return {
298
- ready: manifest !== undefined,
377
+ ready: levelsRead,
299
378
  level: wantLevel,
300
379
  shown: held?.window.level ?? null,
301
- held: cache.size,
302
- loading: inFlight.size,
380
+ held: heldTiles(),
381
+ loading: loading === undefined ? 0 : loading.keys.filter((key) => store.get(accountOf(loading.source, deps.body, owner), key) === undefined).length,
303
382
  failed: failed.size,
304
383
  requested,
305
384
  pixels: held === undefined ? 0 : held.window.cols * TILE_SIZE * held.window.rows * TILE_SIZE,
385
+ source: held?.source,
386
+ used: held?.tiles ?? 0,
306
387
  };
307
388
  },
308
389
  dispose() {
309
390
  gone = true;
391
+ unwatch();
310
392
  cancelPending();
311
393
  cancelLoading();
312
394
  cancelFree();
@@ -1,5 +1,6 @@
1
1
  import type * as THREE from "three";
2
2
  import "../bodies/all.ts";
3
+ import { type SourceBook } from "../door/book.ts";
3
4
  import { type Vec3 } from "../vec.ts";
4
5
  import { type DetailStats } from "./detail.ts";
5
6
  import { type TileLevel } from "./tiles.ts";
@@ -153,6 +154,8 @@ export interface PainterOptions {
153
154
  farBase?: string;
154
155
  /** Where the finer map tiles are (a folder address that ends in a slash; `off` for none). Left out, the demo site's (`TILES_BASE`) unless `textureBase` is given, in which case there are none. They are laid over a body that is big in the view, when the caller gives `PaintInfo.view`. */
155
156
  tiles?: string;
157
+ /** The sources of the view that owns the painter (the data door): a map source in it takes the place of the folder of tiles for its body. Left out, the painter has a book of its own and draws from the folder alone. */
158
+ book?: SourceBook;
156
159
  }
157
160
  /**
158
161
  * A painter: makes the bodies as three.js objects (`make`) and tells where a planet's moons are (`moons`). Its shape is chikyu-earth's `BodyPainter`, so `setBodyPainter(painter)` is all the wiring.
@@ -1,7 +1,8 @@
1
1
  import "../bodies/all.js";
2
2
  import { BODIES, MOONS_OF } from "../facts.js";
3
3
  import { writeBasis } from "./basis.js";
4
- import { TEXTURE_BASE, TEXTURE_FAR_BASE, TILES_BASE, slash, loadImage, tilesAllowed } from "./textures.js";
4
+ import { TEXTURE_BASE, TEXTURE_FAR_BASE, TILES_BASE, slash, loadImage, mapSourceFor, tilesAllowed } from "./textures.js";
5
+ import { createSourceBook } from "../door/book.js";
5
6
  import { cross } from "../vec.js";
6
7
  import { bodyFragment, GLOW_FRAGMENT, GLOW_VERTEX, PAINTED_SURFACE, PAINTED_SURFACE_WINDOWED, RING_FRAGMENT_WORLD, RING_VERTEX_WORLD, VERTEX_WORLD } from "./glsl.js";
7
8
  import { fallbackLook } from "./fallback.js";
@@ -305,11 +306,12 @@ function paint(T, id, context, bases) {
305
306
  tooBigSince = 0;
306
307
  };
307
308
  // the window of finer map tiles for a close view, laid over the painted map by the painted material (see `scene/detail.ts`); only for a body that is told which camera sees it
308
- const windowUniforms = wantPainted && paintedMaterial !== undefined && generic && bases.tiles !== undefined && look.painted !== undefined ? paintedMaterial.uniforms : undefined;
309
+ const windowUniforms = wantPainted && paintedMaterial !== undefined && generic && (bases.tiles !== undefined || bases.shared) && look.painted !== undefined ? paintedMaterial.uniforms : undefined;
309
310
  const detail = windowUniforms === undefined
310
311
  ? undefined
311
312
  : createDetail(T, {
312
- base: () => bases.tiles,
313
+ source: () => mapSourceFor(bases.book, bases.tiles, look.painted.map),
314
+ book: bases.book,
313
315
  body: look.painted.map,
314
316
  ratio: () => ratio,
315
317
  allowed: tilesAllowed,
@@ -413,9 +415,10 @@ export function createPainter(options = {}) {
413
415
  const far = slash(options.farBase ?? (options.textureBase !== undefined ? options.textureBase : TEXTURE_FAR_BASE));
414
416
  const own = options.tiles;
415
417
  const tiles = own === "off" || own === "none" || own === "" ? undefined : own !== undefined ? slash(own) : options.textureBase !== undefined ? undefined : TILES_BASE;
418
+ const book = options.book ?? createSourceBook({ kinds: ["tiles"] });
416
419
  return {
417
420
  looks: LOOKS,
418
- make: (three, name, context = {}) => paint(three, idOf(name), context, { near, far, tiles }),
421
+ make: (three, name, context = {}) => paint(three, idOf(name), context, { near, far, tiles, book, shared: options.book !== undefined }),
419
422
  moons(planet, ms) {
420
423
  const radius = BODIES[idOf(planet)]?.radiusKm;
421
424
  if (radius === undefined)
@@ -1,4 +1,6 @@
1
1
  import type * as THREE from "three";
2
+ import type { SourceBook } from "../door/book.ts";
3
+ import { type BodyMapSource } from "../door/map.ts";
2
4
  /**
3
5
  * The default home of the surface pictures: the files of this exact version on jsDelivr. Set `texture-base` on an element to serve them yourself.
4
6
  *
@@ -9,7 +11,7 @@ import type * as THREE from "three";
9
11
  * console.log(TEXTURE_BASE);
10
12
  * ```
11
13
  */
12
- export declare const TEXTURE_BASE = "https://cdn.jsdelivr.net/npm/@johnmorrisdotca/wakusei-planets@0.31.0/dist/textures/";
14
+ export declare const TEXTURE_BASE = "https://cdn.jsdelivr.net/npm/@johnmorrisdotca/wakusei-planets@0.32.0/dist/textures/";
13
15
  /**
14
16
  * Where the sharpest surface pictures are: the files of this exact version in the repository, on jsDelivr's GitHub address. The npm package holds only the two smaller widths, so a page that
15
17
  * never zooms in never fetches them. Set `texture-far-base` on an element to serve them yourself.
@@ -21,7 +23,7 @@ export declare const TEXTURE_BASE = "https://cdn.jsdelivr.net/npm/@johnmorrisdot
21
23
  * console.log(TEXTURE_FAR_BASE);
22
24
  * ```
23
25
  */
24
- export declare const TEXTURE_FAR_BASE = "https://cdn.jsdelivr.net/gh/johnmorrisdotca/wakusei-planets@v0.31.0/textures/";
26
+ export declare const TEXTURE_FAR_BASE = "https://cdn.jsdelivr.net/gh/johnmorrisdotca/wakusei-planets@v0.32.0/textures/";
25
27
  /**
26
28
  * Where the finer map tiles are by default: the demo site's `tiles/` folder (GitHub Pages sends `access-control-allow-origin: *`). The tiles are made by `scripts/make-body-tiles.py` and are in no package and no repository:
27
29
  * a page that serves its own gives the folder in the `tiles` attribute, and `tiles="off"` turns them off. A page that sets `texture-base` (it serves its own pictures) has none unless it says where they are.
@@ -37,6 +39,11 @@ export declare const TILES_BASE = "https://johnmorrisdotca.github.io/wakusei-pla
37
39
  export declare const slash: (base: string) => string;
38
40
  /** The folder of finer map tiles for an element: its `tiles` attribute (`off` or `none` for none), else `TILES_BASE` unless the element names its own `texture-base`. */
39
41
  export declare function tilesBase(element: Element): string | undefined;
42
+ /**
43
+ * The map source a body is drawn from: the last colour source a page added for it (`addSource`), else the package's own folder of tiles at `base` (the `tiles` attribute), else `undefined`, and the body keeps its own
44
+ * 2048 pixel picture. One folder source is made for each address, so every element of a page shares it.
45
+ */
46
+ export declare function mapSourceFor(book: SourceBook, base: string | undefined, body: string): BodyMapSource | undefined;
40
47
  export declare function texturePaths(element: Element): {
41
48
  textureBase(): string;
42
49
  farBase(): string;
@@ -1,3 +1,4 @@
1
+ import { tilesSource } from "../door/map.js";
1
2
  import { VERSION } from "../version.js";
2
3
  /**
3
4
  * The default home of the surface pictures: the files of this exact version on jsDelivr. Set `texture-base` on an element to serve them yourself.
@@ -44,6 +45,22 @@ export function tilesBase(element) {
44
45
  return slash(own);
45
46
  return element.hasAttribute("texture-base") ? undefined : TILES_BASE;
46
47
  }
48
+ const folders = new Map();
49
+ /**
50
+ * The map source a body is drawn from: the last colour source a page added for it (`addSource`), else the package's own folder of tiles at `base` (the `tiles` attribute), else `undefined`, and the body keeps its own
51
+ * 2048 pixel picture. One folder source is made for each address, so every element of a page shares it.
52
+ */
53
+ export function mapSourceFor(book, base, body) {
54
+ const added = book.mapSources(body, "colour")[0];
55
+ if (added !== undefined)
56
+ return added;
57
+ if (base === undefined)
58
+ return undefined;
59
+ let own = folders.get(base);
60
+ if (own === undefined)
61
+ folders.set(base, (own = tilesSource(base)));
62
+ return own;
63
+ }
47
64
  export function texturePaths(element) {
48
65
  const textureBase = () => {
49
66
  const own = element.getAttribute("texture-base");
@@ -1,6 +1,8 @@
1
1
  import { type ViewController } from "@johnmorrisdotca/kazoku-ui/view-controls";
2
2
  import { type BodyId } from "../facts.ts";
3
3
  import { type SpaceKind } from "./bodies.ts";
4
+ import type { Credit, DataSource } from "../data-door.ts";
5
+ import { type DoorHost } from "../door/host.ts";
4
6
  import { setThreeLoader } from "../scene/shared.ts";
5
7
  import { type SpacePlanet } from "./frame.ts";
6
8
  import { type ScaleMode } from "./scale.ts";
@@ -233,6 +235,23 @@ export declare class WakuseiSpace extends HTMLElement {
233
235
  get riding(): Riding | null;
234
236
  /** The zoom that shows a part of the sky: the Earth alone, the Moon's orbit, the Sun, the planets out to Mars, all of them, or a planet's nearer moons, at this scale and moment. */
235
237
  zoomFor(named: SpacePreset | string): number;
238
+ /**
239
+ * The outside sources of this view (the data door): maps of a body, which take the place of the folder of tiles for it, and records of small bodies, which are drawn as points in one instanced draw. Each carries its own
240
+ * licence and credit. Assign a list to replace them; a source `validateSource` refuses throws a `TypeError` and changes nothing. A property, not an attribute: data is not text.
241
+ */
242
+ get sources(): readonly DataSource[];
243
+ set sources(list: readonly DataSource[]);
244
+ /** Adds one source (checked at once: a `TypeError` for an empty credit, no licence, a clashing id or a map source with no body). Returns the function that removes it. */
245
+ addSource(source: DataSource): () => void;
246
+ /** Removes a source by id: its loads are aborted, its tiles and records freed, its points and window taken off the picture, and its credit goes. Returns whether there was one. */
247
+ removeSource(id: string): boolean;
248
+ /** Switches a source off (`false`) or on, for a layers panel. */
249
+ enableSource(id: string, on: boolean): void;
250
+ /**
251
+ * The sources whose data was in the last drawn frame, the package's own first: the surface pictures and the positions of the bodies, the finer map tiles where a window of them is on a body, and each source of yours that
252
+ * gave points or tiles. `creditLine(credits, "en" | "ja")` from `@johnmorrisdotca/wakusei-planets/door` writes them with the licences. `wakusei-creditschange` fires when this set changes (not at each frame), with `detail: { credits }`.
253
+ */
254
+ credits(): Credit[];
236
255
  /** What the element is doing, for a page or a test. */
237
256
  get stats(): {
238
257
  state: SpaceState;
@@ -276,6 +295,11 @@ export declare class WakuseiSpace extends HTMLElement {
276
295
  lost: boolean;
277
296
  gliding: boolean;
278
297
  };
298
+ /** The data door: the small bodies in the one instanced draw, and what the sources have done (tile loads started, failed and refused, bytes held, records read). */
299
+ door: {
300
+ points: number;
301
+ sources: ReturnType<DoorHost["book"]["stats"]>;
302
+ };
279
303
  /** The orbit lines drawn at the last draw (`earth`, the planets, `moon`), and how far each body is from its own line as a fraction of the line's radius: a body on its line reads under 0.01. */
280
304
  orbits: {
281
305
  drawn: string[];