@johnmorrisdotca/chikyu-earth 0.16.0 → 0.18.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 +12 -0
- package/NOTICE.md +13 -0
- package/README.md +44 -14
- package/dist/element.d.ts +4 -0
- package/dist/element.js +26 -2
- package/dist/realistic/earth.d.ts +7 -0
- package/dist/realistic/earth.js +7 -0
- package/dist/realistic/element.d.ts +11 -2
- package/dist/realistic/element.js +48 -4
- package/dist/realistic/index.d.ts +2 -0
- package/dist/realistic/index.js +2 -0
- package/dist/realistic/shaders.d.ts +8 -1
- package/dist/realistic/shaders.js +30 -3
- package/dist/realistic/tile-set.d.ts +76 -0
- package/dist/realistic/tile-set.js +403 -0
- package/dist/realistic/tiles.d.ts +211 -0
- package/dist/realistic/tiles.js +266 -0
- package/dist/space/element.d.ts +5 -0
- package/dist/space/element.js +37 -0
- package/package.json +5 -6
|
@@ -10,7 +10,8 @@ void main() {
|
|
|
10
10
|
vWorld = (modelMatrix * vec4(position, 1.0)).xyz;
|
|
11
11
|
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
|
|
12
12
|
}`;
|
|
13
|
-
|
|
13
|
+
/** The surface program. `tile` is the program of a map tile laid over the whole-Earth pictures: the day picture is read at the tile's own coordinates, the rest at the whole Earth's. */
|
|
14
|
+
const surfaceFrag = (tile) => /* glsl */ `
|
|
14
15
|
uniform sampler2D dayMap;
|
|
15
16
|
uniform sampler2D nightMap;
|
|
16
17
|
uniform sampler2D waterMap;
|
|
@@ -18,6 +19,7 @@ uniform vec3 sunDir;
|
|
|
18
19
|
uniform float lights;
|
|
19
20
|
uniform float boost;
|
|
20
21
|
uniform float ortho;
|
|
22
|
+
${tile ? "uniform vec4 nightRect;\nvarying vec2 vUvG;" : ""}
|
|
21
23
|
varying vec2 vUv;
|
|
22
24
|
varying vec3 vNormal;
|
|
23
25
|
varying vec3 vWorld;
|
|
@@ -27,7 +29,7 @@ void main() {
|
|
|
27
29
|
float ndl = dot(n, sunDir);
|
|
28
30
|
float day = smoothstep(-0.10, 0.22, ndl);
|
|
29
31
|
vec3 albedo = texture2D(dayMap, vUv).rgb;
|
|
30
|
-
float water = texture2D(waterMap, vUv).r;
|
|
32
|
+
float water = texture2D(waterMap, ${tile ? "vUvG" : "vUv"}).r;
|
|
31
33
|
|
|
32
34
|
vec3 lit = albedo * (0.07 + 1.0 * pow(max(ndl, 0.0), 0.5));
|
|
33
35
|
float dusk = smoothstep(-0.14, 0.0, ndl) * (1.0 - smoothstep(0.0, 0.30, ndl));
|
|
@@ -37,7 +39,7 @@ void main() {
|
|
|
37
39
|
float glint = pow(max(dot(n, h), 0.0), 220.0) * water * day;
|
|
38
40
|
lit += vec3(1.0, 0.93, 0.80) * glint * 0.7;
|
|
39
41
|
|
|
40
|
-
vec3 night = texture2D(nightMap, vUv).rgb;
|
|
42
|
+
vec3 night = texture2D(nightMap, ${tile ? "(vUvG - nightRect.xy) / nightRect.zw" : "vUv"}).rgb;
|
|
41
43
|
night = night * night * vec3(1.6, 1.5, 1.3) * lights + night * 0.25;
|
|
42
44
|
vec3 col = mix(night, lit, day);
|
|
43
45
|
col += pow(albedo, vec3(0.75)) * boost * 0.65 * (1.0 - day);
|
|
@@ -47,6 +49,31 @@ void main() {
|
|
|
47
49
|
col += vec3(0.20, 0.45, 1.0) * rim * sunSide * 0.55;
|
|
48
50
|
gl_FragColor = vec4(col, 1.0);
|
|
49
51
|
}`;
|
|
52
|
+
export const SURFACE_FRAG = surfaceFrag(false);
|
|
53
|
+
/** The same surface for a map tile: the tile's own program. */
|
|
54
|
+
export const TILE_FRAG = surfaceFrag(true);
|
|
55
|
+
/**
|
|
56
|
+
* A map tile's corners: one 0 to 1 grid for every tile, put on the sphere here from the tile's longitude and latitude range (`rect`: west, north, east, south, in radians) at `radius`.
|
|
57
|
+
* `vUvG` is where the point is on the whole-Earth pictures.
|
|
58
|
+
*/
|
|
59
|
+
export const TILE_VERT = /* glsl */ `
|
|
60
|
+
uniform vec4 rect;
|
|
61
|
+
uniform float radius;
|
|
62
|
+
varying vec2 vUv;
|
|
63
|
+
varying vec2 vUvG;
|
|
64
|
+
varying vec3 vNormal;
|
|
65
|
+
varying vec3 vWorld;
|
|
66
|
+
void main() {
|
|
67
|
+
vUv = uv;
|
|
68
|
+
float lon = mix(rect.x, rect.z, uv.x);
|
|
69
|
+
float lat = mix(rect.y, rect.w, uv.y);
|
|
70
|
+
vec3 unit = vec3(cos(lat) * sin(lon), sin(lat), cos(lat) * cos(lon));
|
|
71
|
+
vUvG = vec2((lon + 3.14159265358979) / 6.28318530717959, 0.5 - lat / 3.14159265358979);
|
|
72
|
+
vNormal = normalize(mat3(modelMatrix) * unit);
|
|
73
|
+
vec4 world = modelMatrix * vec4(unit * radius, 1.0);
|
|
74
|
+
vWorld = world.xyz;
|
|
75
|
+
gl_Position = projectionMatrix * viewMatrix * world;
|
|
76
|
+
}`;
|
|
50
77
|
export const CLOUD_FRAG = /* glsl */ `
|
|
51
78
|
uniform sampler2D cloudMap;
|
|
52
79
|
uniform vec3 sunDir;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type * as THREE from "three";
|
|
2
|
+
import type { AssetCache } from "@johnmorrisdotca/kazoku-ui/renderer";
|
|
3
|
+
import type { ThreeParts } from "./textures.ts";
|
|
4
|
+
import { type TileCamera } from "./tiles.ts";
|
|
5
|
+
/**
|
|
6
|
+
* 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
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { TILE_LIMIT } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
11
|
+
*
|
|
12
|
+
* console.log(TILE_LIMIT);
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare const TILE_LIMIT = 48;
|
|
16
|
+
/**
|
|
17
|
+
* Milliseconds a tile that is no longer wanted is kept (the view may come straight back) before it is freed.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { TILE_KEEP_MS } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
22
|
+
*
|
|
23
|
+
* console.log(TILE_KEEP_MS);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare const TILE_KEEP_MS = 4000;
|
|
27
|
+
type Three = ThreeParts;
|
|
28
|
+
type Uniform<T> = {
|
|
29
|
+
value: T;
|
|
30
|
+
};
|
|
31
|
+
/** What the tiles share with the surface they are laid over. */
|
|
32
|
+
export interface TileShared {
|
|
33
|
+
sunDir: Uniform<THREE.Vector3>;
|
|
34
|
+
lights: Uniform<number>;
|
|
35
|
+
boost: Uniform<number>;
|
|
36
|
+
ortho: Uniform<number>;
|
|
37
|
+
/** The whole-Earth water and night pictures' uniforms: their value is the picture now held. */
|
|
38
|
+
water: Uniform<THREE.Texture | null>;
|
|
39
|
+
night: Uniform<THREE.Texture | null>;
|
|
40
|
+
}
|
|
41
|
+
export interface TileSetDeps {
|
|
42
|
+
assets: AssetCache;
|
|
43
|
+
/** Where the tile set is, ending with a slash, or `undefined` for none. Read at every draw. */
|
|
44
|
+
base: () => string | undefined;
|
|
45
|
+
/** The widest picture this device holds: tiles are only used where the day picture reaches 4096. */
|
|
46
|
+
cap: () => number;
|
|
47
|
+
/** Ask for one draw. */
|
|
48
|
+
invalidate: () => void;
|
|
49
|
+
/** Told once when a tile set's manifest has been read: the finest width of each layer. */
|
|
50
|
+
onManifest?: (layers: Record<string, number>) => void;
|
|
51
|
+
limit?: number;
|
|
52
|
+
now?: () => number;
|
|
53
|
+
}
|
|
54
|
+
/** What the tile set is doing, for a page or a test. */
|
|
55
|
+
export interface TileStats {
|
|
56
|
+
/** A readable manifest is in hand. */
|
|
57
|
+
ready: boolean;
|
|
58
|
+
/** The level now wanted for the day picture, `null` when the pictures of the package are sharp enough. */
|
|
59
|
+
level: number | null;
|
|
60
|
+
/** Tiles held (loading, loaded, on the sphere or not), and those on the sphere in view. */
|
|
61
|
+
held: number;
|
|
62
|
+
shown: number;
|
|
63
|
+
loading: number;
|
|
64
|
+
/** Tiles that could not be fetched and are not asked for again for a while. */
|
|
65
|
+
failed: number;
|
|
66
|
+
/** Tile fetches made. */
|
|
67
|
+
requested: number;
|
|
68
|
+
}
|
|
69
|
+
export interface TileSet {
|
|
70
|
+
sync(camera: TileCamera, sun: readonly [number, number, number], night: boolean, ratio: number): void;
|
|
71
|
+
readonly stats: TileStats;
|
|
72
|
+
dispose(): void;
|
|
73
|
+
}
|
|
74
|
+
/** Make the set that puts tiles on `parent`, a group that turns with the Earth. */
|
|
75
|
+
export declare function createTileSet(three: Three, parent: THREE.Group, shared: TileShared, deps: TileSetDeps): TileSet;
|
|
76
|
+
export {};
|
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import { TILE_FRAG, TILE_VERT } from "./shaders.js";
|
|
2
|
+
import { MAX_TEXTURE, TIERS } from "./tiers.js";
|
|
3
|
+
import { coveringTile, parseManifest, tileColumns, tileLevel, tileRect, tileRows, tilesInView } from "./tiles.js";
|
|
4
|
+
/**
|
|
5
|
+
* 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).
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { TILE_LIMIT } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
10
|
+
*
|
|
11
|
+
* console.log(TILE_LIMIT);
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export const TILE_LIMIT = 48;
|
|
15
|
+
/**
|
|
16
|
+
* Milliseconds a tile that is no longer wanted is kept (the view may come straight back) before it is freed.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { TILE_KEEP_MS } from "@johnmorrisdotca/chikyu-earth/realistic";
|
|
21
|
+
*
|
|
22
|
+
* console.log(TILE_KEEP_MS);
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export const TILE_KEEP_MS = 4000;
|
|
26
|
+
const IN_FLIGHT = 6;
|
|
27
|
+
const NEW_PER_DRAW = 8;
|
|
28
|
+
const RETRY_MS = 30_000;
|
|
29
|
+
// the day mix reaches full light at 0.22: a tile whose darkest point is brighter than that never shows the night picture
|
|
30
|
+
const NIGHT_BELOW = 0.25;
|
|
31
|
+
const GRID = 16;
|
|
32
|
+
const keyOf = (layer, id) => `${layer}/${id.level}/${id.col}/${id.row}`;
|
|
33
|
+
/** A 0 to 1 grid, the same for every tile: the vertex shader puts it on the sphere. */
|
|
34
|
+
function patch(three) {
|
|
35
|
+
const positions = new Float32Array((GRID + 1) * (GRID + 1) * 3);
|
|
36
|
+
const uvs = new Float32Array((GRID + 1) * (GRID + 1) * 2);
|
|
37
|
+
let p = 0;
|
|
38
|
+
let q = 0;
|
|
39
|
+
for (let j = 0; j <= GRID; j += 1) {
|
|
40
|
+
for (let i = 0; i <= GRID; i += 1) {
|
|
41
|
+
uvs[q++] = i / GRID;
|
|
42
|
+
uvs[q++] = j / GRID;
|
|
43
|
+
positions[p++] = i / GRID;
|
|
44
|
+
positions[p++] = j / GRID;
|
|
45
|
+
positions[p++] = 0;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const index = [];
|
|
49
|
+
for (let j = 0; j < GRID; j += 1) {
|
|
50
|
+
for (let i = 0; i < GRID; i += 1) {
|
|
51
|
+
const a = j * (GRID + 1) + i;
|
|
52
|
+
const b = a + GRID + 1;
|
|
53
|
+
index.push(a, b, a + 1, b, b + 1, a + 1);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const geometry = new three.BufferGeometry();
|
|
57
|
+
geometry.setAttribute("position", new three.BufferAttribute(positions, 3));
|
|
58
|
+
geometry.setAttribute("uv", new three.BufferAttribute(uvs, 2));
|
|
59
|
+
geometry.setIndex(index);
|
|
60
|
+
return geometry;
|
|
61
|
+
}
|
|
62
|
+
/** Make the set that puts tiles on `parent`, a group that turns with the Earth. */
|
|
63
|
+
export function createTileSet(three, parent, shared, deps) {
|
|
64
|
+
const limit = deps.limit ?? TILE_LIMIT;
|
|
65
|
+
const clock = deps.now ?? (() => performance.now());
|
|
66
|
+
const geometry = patch(three);
|
|
67
|
+
const entries = new Map();
|
|
68
|
+
const failed = new Map();
|
|
69
|
+
let queue = [];
|
|
70
|
+
let wantedKeys = new Set();
|
|
71
|
+
let current;
|
|
72
|
+
let manifest;
|
|
73
|
+
let manifestFetch;
|
|
74
|
+
let inFlight = 0;
|
|
75
|
+
let requested = 0;
|
|
76
|
+
let wantedLevel = null;
|
|
77
|
+
let shown = 0;
|
|
78
|
+
let timer;
|
|
79
|
+
let last;
|
|
80
|
+
let disposed = false;
|
|
81
|
+
const baseDay = TIERS.day[TIERS.day.length - 1];
|
|
82
|
+
const baseNight = TIERS.night[TIERS.night.length - 1];
|
|
83
|
+
const take = (layer, id, base) => deps.assets.acquire(`chikyu-earth:tile:${base}${keyOf(layer, id)}`, () => {
|
|
84
|
+
const texture = new three.Texture();
|
|
85
|
+
texture.wrapS = three.ClampToEdgeWrapping;
|
|
86
|
+
texture.wrapT = three.ClampToEdgeWrapping;
|
|
87
|
+
texture.minFilter = three.LinearMipmapLinearFilter;
|
|
88
|
+
texture.magFilter = three.LinearFilter;
|
|
89
|
+
texture.anisotropy = 8;
|
|
90
|
+
const controller = new AbortController();
|
|
91
|
+
const made = { texture, closed: false, ready: Promise.resolve(), release: () => undefined };
|
|
92
|
+
made.ready = fetch(`${base}${keyOf(layer, id)}.webp`, { signal: controller.signal })
|
|
93
|
+
.then((response) => {
|
|
94
|
+
if (!response.ok)
|
|
95
|
+
throw new Error(`${keyOf(layer, id)}: ${response.status}`);
|
|
96
|
+
return response.blob();
|
|
97
|
+
})
|
|
98
|
+
.then((blob) => createImageBitmap(blob, { premultiplyAlpha: "none", colorSpaceConversion: "none" }))
|
|
99
|
+
.then((bitmap) => {
|
|
100
|
+
if (made.closed)
|
|
101
|
+
bitmap.close();
|
|
102
|
+
else {
|
|
103
|
+
texture.image = bitmap;
|
|
104
|
+
texture.needsUpdate = true;
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
made.ready.catch(() => undefined);
|
|
108
|
+
made.release = () => {
|
|
109
|
+
made.closed = true;
|
|
110
|
+
controller.abort();
|
|
111
|
+
texture.dispose();
|
|
112
|
+
texture.image?.close?.();
|
|
113
|
+
};
|
|
114
|
+
return made;
|
|
115
|
+
}, (value) => value.release());
|
|
116
|
+
const drop = (entry) => {
|
|
117
|
+
entries.delete(entry.key);
|
|
118
|
+
if (entry.mesh) {
|
|
119
|
+
parent.remove(entry.mesh);
|
|
120
|
+
entry.mesh.material.dispose();
|
|
121
|
+
}
|
|
122
|
+
if (current !== undefined)
|
|
123
|
+
deps.assets.release(`chikyu-earth:tile:${current}${entry.key}`);
|
|
124
|
+
};
|
|
125
|
+
const reset = () => {
|
|
126
|
+
for (const entry of [...entries.values()])
|
|
127
|
+
drop(entry);
|
|
128
|
+
queue = [];
|
|
129
|
+
wantedKeys = new Set();
|
|
130
|
+
failed.clear();
|
|
131
|
+
manifestFetch?.abort();
|
|
132
|
+
manifestFetch = undefined;
|
|
133
|
+
manifest = undefined;
|
|
134
|
+
wantedLevel = null;
|
|
135
|
+
shown = 0;
|
|
136
|
+
};
|
|
137
|
+
const readManifest = (base) => {
|
|
138
|
+
manifest = undefined;
|
|
139
|
+
const controller = new AbortController();
|
|
140
|
+
manifestFetch = controller;
|
|
141
|
+
fetch(`${base}manifest.json`, { signal: controller.signal })
|
|
142
|
+
.then((response) => (response.ok ? response.json() : undefined))
|
|
143
|
+
.then((json) => {
|
|
144
|
+
if (disposed || current !== base || manifestFetch !== controller)
|
|
145
|
+
return;
|
|
146
|
+
const read = parseManifest(json);
|
|
147
|
+
manifest = read ?? null;
|
|
148
|
+
if (read) {
|
|
149
|
+
deps.onManifest?.(Object.fromEntries(Object.entries(read.layers).map(([name, layer]) => [name, layer.levels[layer.levels.length - 1].width])));
|
|
150
|
+
deps.invalidate();
|
|
151
|
+
}
|
|
152
|
+
}, () => {
|
|
153
|
+
if (!disposed && current === base && manifestFetch === controller)
|
|
154
|
+
manifest = null;
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
const pump = () => {
|
|
158
|
+
const base = current;
|
|
159
|
+
if (base === undefined)
|
|
160
|
+
return;
|
|
161
|
+
while (inFlight < IN_FLIGHT && queue.length > 0) {
|
|
162
|
+
const next = queue.shift();
|
|
163
|
+
const key = keyOf(next.layer, next.id);
|
|
164
|
+
if (entries.has(key))
|
|
165
|
+
continue;
|
|
166
|
+
const entry = { key, layer: next.layer, id: next.id, asset: take(next.layer, next.id, base), ready: false, integrated: false, lastWanted: clock() };
|
|
167
|
+
entries.set(key, entry);
|
|
168
|
+
inFlight += 1;
|
|
169
|
+
requested += 1;
|
|
170
|
+
const settled = () => {
|
|
171
|
+
inFlight -= 1;
|
|
172
|
+
pump();
|
|
173
|
+
};
|
|
174
|
+
entry.asset.ready.then(() => {
|
|
175
|
+
if (entries.get(key) === entry) {
|
|
176
|
+
entry.ready = true;
|
|
177
|
+
deps.invalidate();
|
|
178
|
+
}
|
|
179
|
+
settled();
|
|
180
|
+
}, () => {
|
|
181
|
+
if (entries.get(key) === entry) {
|
|
182
|
+
failed.set(key, clock());
|
|
183
|
+
drop(entry);
|
|
184
|
+
}
|
|
185
|
+
settled();
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
const nightLevels = () => {
|
|
190
|
+
const layer = manifest?.layers["night"];
|
|
191
|
+
const finer = layer?.levels.filter((entry) => entry.width > baseNight);
|
|
192
|
+
return layer && finer && finer.length > 0 ? { tileSize: layer.tileSize, levels: finer } : undefined;
|
|
193
|
+
};
|
|
194
|
+
/** The night tile that goes with a day tile: the same level, or the finest the night picture has. */
|
|
195
|
+
const nightFor = (tile, night) => {
|
|
196
|
+
const level = night.levels.filter((entry) => entry.level <= tile.level).reduce((top, entry) => Math.max(top, entry.level), night.levels[0].level);
|
|
197
|
+
return coveringTile(tile, level);
|
|
198
|
+
};
|
|
199
|
+
const choose = (camera, sun, night, ratio) => {
|
|
200
|
+
const day = manifest?.layers["day"];
|
|
201
|
+
if (!day || deps.cap() < MAX_TEXTURE)
|
|
202
|
+
return { level: null, day: [], night: [] };
|
|
203
|
+
const nightLayer = night ? nightLevels() : undefined;
|
|
204
|
+
let level = tileLevel(camera.radius * ratio, day, baseDay);
|
|
205
|
+
while (level !== undefined) {
|
|
206
|
+
const tiles = tilesInView(level, camera, sun);
|
|
207
|
+
const seen = new Set();
|
|
208
|
+
const dark = [];
|
|
209
|
+
if (nightLayer) {
|
|
210
|
+
for (const tile of tiles) {
|
|
211
|
+
if (tile.ndl >= NIGHT_BELOW)
|
|
212
|
+
continue;
|
|
213
|
+
const id = nightFor(tile, nightLayer);
|
|
214
|
+
const key = keyOf("night", id);
|
|
215
|
+
if (!seen.has(key)) {
|
|
216
|
+
seen.add(key);
|
|
217
|
+
dark.push(id);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (tiles.length + dark.length <= limit)
|
|
222
|
+
return { level, day: tiles, night: dark };
|
|
223
|
+
const lower = day.levels.filter((entry) => entry.width > baseDay && entry.level < level).pop();
|
|
224
|
+
level = lower?.level;
|
|
225
|
+
}
|
|
226
|
+
return { level: null, day: [], night: [] };
|
|
227
|
+
};
|
|
228
|
+
const makeMesh = (entry) => {
|
|
229
|
+
const nightRect = { value: [0, 0, 1, 1] };
|
|
230
|
+
const nightMap = { value: shared.night.value };
|
|
231
|
+
const material = new three.ShaderMaterial({
|
|
232
|
+
vertexShader: TILE_VERT,
|
|
233
|
+
fragmentShader: TILE_FRAG,
|
|
234
|
+
uniforms: {
|
|
235
|
+
dayMap: { value: entry.asset.texture },
|
|
236
|
+
nightMap,
|
|
237
|
+
waterMap: shared.water,
|
|
238
|
+
sunDir: shared.sunDir,
|
|
239
|
+
lights: shared.lights,
|
|
240
|
+
boost: shared.boost,
|
|
241
|
+
ortho: shared.ortho,
|
|
242
|
+
nightRect,
|
|
243
|
+
rect: { value: tileRect(entry.id) },
|
|
244
|
+
// a finer level sits a little higher, so two levels at once never fight for the same depth; all sit above the whole-Earth sphere's flat faces
|
|
245
|
+
radius: { value: 1.0004 + 0.0001 * (entry.id.level - 3) },
|
|
246
|
+
},
|
|
247
|
+
polygonOffset: true,
|
|
248
|
+
polygonOffsetFactor: -1,
|
|
249
|
+
polygonOffsetUnits: -1,
|
|
250
|
+
});
|
|
251
|
+
const mesh = new three.Mesh(geometry, material);
|
|
252
|
+
mesh.frustumCulled = false;
|
|
253
|
+
mesh.visible = false;
|
|
254
|
+
parent.add(mesh);
|
|
255
|
+
entry.mesh = mesh;
|
|
256
|
+
entry.nightRect = nightRect;
|
|
257
|
+
entry.nightMap = nightMap;
|
|
258
|
+
};
|
|
259
|
+
/** Each day tile reads the finest night tile it has, else the whole-Earth night picture. */
|
|
260
|
+
const retarget = () => {
|
|
261
|
+
const night = nightLevels();
|
|
262
|
+
for (const entry of entries.values()) {
|
|
263
|
+
if (!entry.mesh || !entry.nightMap || !entry.nightRect)
|
|
264
|
+
continue;
|
|
265
|
+
let texture = null;
|
|
266
|
+
let rect = [0, 0, 1, 1];
|
|
267
|
+
if (night) {
|
|
268
|
+
for (let level = entry.id.level; level >= night.levels[0].level && !texture; level -= 1) {
|
|
269
|
+
if (!night.levels.some((l) => l.level === level))
|
|
270
|
+
continue;
|
|
271
|
+
const id = coveringTile(entry.id, level);
|
|
272
|
+
const held = entries.get(keyOf("night", id));
|
|
273
|
+
if (held?.integrated && held.ready) {
|
|
274
|
+
texture = held.asset.texture;
|
|
275
|
+
rect = [id.col / tileColumns(id.level), id.row / tileRows(id.level), 1 / tileColumns(id.level), 1 / tileRows(id.level)];
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
entry.nightMap.value = texture ?? shared.night.value;
|
|
280
|
+
entry.nightRect.value = rect;
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
const place = (camera, visible) => {
|
|
284
|
+
shown = 0;
|
|
285
|
+
for (const entry of entries.values()) {
|
|
286
|
+
if (!entry.mesh)
|
|
287
|
+
continue;
|
|
288
|
+
let set = visible.get(entry.id.level);
|
|
289
|
+
if (!set) {
|
|
290
|
+
set = new Set(tilesInView(entry.id.level, camera).map((tile) => tile.row * tileColumns(tile.level) + tile.col));
|
|
291
|
+
visible.set(entry.id.level, set);
|
|
292
|
+
}
|
|
293
|
+
entry.mesh.visible = set.has(entry.id.row * tileColumns(entry.id.level) + entry.id.col);
|
|
294
|
+
if (entry.mesh.visible)
|
|
295
|
+
shown += 1;
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
/** Free what is no longer wanted: past its time, or the oldest first when over the limit. True when something on the sphere went. */
|
|
299
|
+
const sweep = () => {
|
|
300
|
+
const now = clock();
|
|
301
|
+
let changed = false;
|
|
302
|
+
const unwanted = [...entries.values()].filter((entry) => !wantedKeys.has(entry.key)).sort((a, b) => a.lastWanted - b.lastWanted);
|
|
303
|
+
for (const entry of unwanted) {
|
|
304
|
+
if (now - entry.lastWanted < TILE_KEEP_MS && entries.size <= limit)
|
|
305
|
+
continue;
|
|
306
|
+
if (entry.mesh?.visible || entry.integrated)
|
|
307
|
+
changed = true;
|
|
308
|
+
drop(entry);
|
|
309
|
+
}
|
|
310
|
+
if (changed)
|
|
311
|
+
retarget();
|
|
312
|
+
return changed;
|
|
313
|
+
};
|
|
314
|
+
const arm = () => {
|
|
315
|
+
if (timer !== undefined || disposed)
|
|
316
|
+
return;
|
|
317
|
+
const unwanted = [...entries.values()].filter((entry) => !wantedKeys.has(entry.key));
|
|
318
|
+
if (unwanted.length === 0)
|
|
319
|
+
return;
|
|
320
|
+
const oldest = Math.min(...unwanted.map((entry) => entry.lastWanted));
|
|
321
|
+
timer = setTimeout(() => {
|
|
322
|
+
timer = undefined;
|
|
323
|
+
if (sweep()) {
|
|
324
|
+
if (last)
|
|
325
|
+
place(last.camera, new Map());
|
|
326
|
+
deps.invalidate();
|
|
327
|
+
}
|
|
328
|
+
arm();
|
|
329
|
+
}, Math.max(50, oldest + TILE_KEEP_MS - clock() + 20));
|
|
330
|
+
};
|
|
331
|
+
return {
|
|
332
|
+
get stats() {
|
|
333
|
+
return {
|
|
334
|
+
ready: manifest != null,
|
|
335
|
+
level: wantedLevel,
|
|
336
|
+
held: entries.size,
|
|
337
|
+
shown,
|
|
338
|
+
loading: [...entries.values()].filter((entry) => !entry.ready).length,
|
|
339
|
+
failed: failed.size,
|
|
340
|
+
requested,
|
|
341
|
+
};
|
|
342
|
+
},
|
|
343
|
+
sync(camera, sun, night, ratio) {
|
|
344
|
+
if (disposed)
|
|
345
|
+
return;
|
|
346
|
+
const base = deps.base();
|
|
347
|
+
if (base !== current) {
|
|
348
|
+
reset();
|
|
349
|
+
current = base;
|
|
350
|
+
if (base !== undefined)
|
|
351
|
+
readManifest(base);
|
|
352
|
+
}
|
|
353
|
+
last = { camera };
|
|
354
|
+
if (current === undefined || !manifest)
|
|
355
|
+
return;
|
|
356
|
+
const now = clock();
|
|
357
|
+
const wanted = choose(camera, sun, night, ratio);
|
|
358
|
+
wantedLevel = wanted.level;
|
|
359
|
+
wantedKeys = new Set([...wanted.day.map((tile) => keyOf("day", tile)), ...wanted.night.map((id) => keyOf("night", id))]);
|
|
360
|
+
for (const [key, when] of failed)
|
|
361
|
+
if (now - when >= RETRY_MS)
|
|
362
|
+
failed.delete(key);
|
|
363
|
+
for (const key of wantedKeys) {
|
|
364
|
+
const entry = entries.get(key);
|
|
365
|
+
if (entry)
|
|
366
|
+
entry.lastWanted = now;
|
|
367
|
+
}
|
|
368
|
+
queue = [...wanted.day.map((tile) => ({ layer: "day", id: { level: tile.level, col: tile.col, row: tile.row } })), ...wanted.night.map((id) => ({ layer: "night", id }))].filter((item) => !failed.has(keyOf(item.layer, item.id)) && !entries.has(keyOf(item.layer, item.id)));
|
|
369
|
+
pump();
|
|
370
|
+
let fresh = 0;
|
|
371
|
+
let waiting = false;
|
|
372
|
+
for (const entry of entries.values()) {
|
|
373
|
+
if (entry.integrated || !entry.ready || !wantedKeys.has(entry.key))
|
|
374
|
+
continue;
|
|
375
|
+
if (fresh >= NEW_PER_DRAW) {
|
|
376
|
+
waiting = true;
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
fresh += 1;
|
|
380
|
+
entry.integrated = true;
|
|
381
|
+
if (entry.layer === "day")
|
|
382
|
+
makeMesh(entry);
|
|
383
|
+
}
|
|
384
|
+
sweep();
|
|
385
|
+
retarget();
|
|
386
|
+
const visible = new Map();
|
|
387
|
+
if (wanted.level !== null)
|
|
388
|
+
visible.set(wanted.level, new Set(wanted.day.map((tile) => tile.row * tileColumns(tile.level) + tile.col)));
|
|
389
|
+
place(camera, visible);
|
|
390
|
+
if (waiting)
|
|
391
|
+
deps.invalidate();
|
|
392
|
+
arm();
|
|
393
|
+
},
|
|
394
|
+
dispose() {
|
|
395
|
+
disposed = true;
|
|
396
|
+
clearTimeout(timer);
|
|
397
|
+
timer = undefined;
|
|
398
|
+
reset();
|
|
399
|
+
current = undefined;
|
|
400
|
+
geometry.dispose();
|
|
401
|
+
},
|
|
402
|
+
};
|
|
403
|
+
}
|