mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 00:43:15 +00:00
feat(atlas): a regions view, and visited regions that stand out
A traveller who visited only Alberta saw all of Canada colored on the zoomed-out globe. A new layer switch, Regions instead of countries, keeps the coarse regions of every country with a visited region on screen at every zoom: the engine loads and holds them (setPinnedRegions), the renderer draws them at full strength with faint inner borders, and the country itself only tints faintly beneath them once they are drawn. The switch is remembered per device, on the desktop layers dialog and the phone sheet, in all 28 languages. Also without the switch, a country with visited regions now steps far back once its regions fade in (to about a fifth instead of two thirds), so the regions really visited stand out from the rest of the country.
This commit is contained in:
@@ -518,6 +518,29 @@ describe('GlobeEngine regions in view', () => {
|
||||
expect(s.worldKey).toBe('l2');
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-ENGINE-114: pinned countries load and draw their coarse regions on the whole globe', async () => {
|
||||
const ctx = await start({ initial: { lon: 2, lat: 46, zoom: 1 } });
|
||||
await boot(ctx);
|
||||
ctx.engine.setPinnedRegions(['FR']);
|
||||
frames(1);
|
||||
await flush();
|
||||
expect(fetched(ctx, 'regions/FR.r0.json')).toBe(1);
|
||||
expect(ctx.renderer.touchRegion.mock.calls.map(([k]) => k)).toContain('FR:r0');
|
||||
ctx.worker.answer();
|
||||
await flush();
|
||||
const inUse = ctx.renderer.setRegion.mock.calls[0][2];
|
||||
expect(inUse?.has('FR:r0')).toBe(true);
|
||||
frames(1);
|
||||
const pinned = lastDraw(ctx.renderer);
|
||||
expect(pinned.regionAlpha).toBe(0);
|
||||
expect(pinned.regionKeys).toEqual(['FR:r0']);
|
||||
expect(pinned.pinnedRegions?.has('FR')).toBe(true);
|
||||
|
||||
ctx.engine.setPinnedRegions([]);
|
||||
frames(1);
|
||||
expect(lastDraw(ctx.renderer).regionKeys).toEqual([]);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-ENGINE-021: the view is scanned at most every 0.2 s and a loaded region is not fetched twice', async () => {
|
||||
const ctx = await start({ initial: { lon: 2, lat: 46, zoom: 6 } });
|
||||
await boot(ctx);
|
||||
|
||||
@@ -150,6 +150,8 @@ export class GlobeEngine {
|
||||
private regionFailedAt = new Map<string, number>();
|
||||
private regionScanAt = -1;
|
||||
private inViewRegionKeys: ReadonlySet<string> = new Set();
|
||||
/** Countries whose regions show at every zoom, not only once zoomed in (the regions view). */
|
||||
private pinned: ReadonlySet<string> = new Set();
|
||||
private prefetchQueue: string[] = [];
|
||||
private prefetchTimer = 0;
|
||||
private nextRegionId = FIRST_REGION_ID;
|
||||
@@ -252,6 +254,22 @@ export class GlobeEngine {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows these countries' regions at every zoom instead of only once zoomed
|
||||
* in, so a country with a few visited regions reads as those regions rather
|
||||
* than as a whole visited country. An empty list goes back to the usual fade.
|
||||
*/
|
||||
setPinnedRegions(a2s: readonly string[]): void {
|
||||
this.pinned = new Set(a2s);
|
||||
this.kick();
|
||||
}
|
||||
|
||||
/** The region layers that must stay on the GPU: those in view and the pinned countries' coarse ones. */
|
||||
private regionsInUse(): ReadonlySet<string> {
|
||||
if (!this.pinned.size) return this.inViewRegionKeys;
|
||||
return new Set([...this.inViewRegionKeys, ...[...this.pinned].map((a2) => `${a2}:r0`)]);
|
||||
}
|
||||
|
||||
async loadRegionIndex(a2: string): Promise<RegionIndex | null> {
|
||||
const have = this.regionIndex.get(a2);
|
||||
if (have) return have;
|
||||
@@ -281,7 +299,7 @@ export class GlobeEngine {
|
||||
const topo = (await this.opts.fetchJson(idx.files[lod])) as Topology;
|
||||
const layer = await this.build(topo, 'regions', MAX_EDGE[lod], base);
|
||||
if (!this.alive) return;
|
||||
this.renderer.setRegion(key, layer, this.inViewRegionKeys);
|
||||
this.renderer.setRegion(key, layer, this.regionsInUse());
|
||||
this.onRegionLoaded?.(a2);
|
||||
this.kick();
|
||||
} catch {
|
||||
@@ -736,6 +754,12 @@ export class GlobeEngine {
|
||||
if (fine) void this.loadRegion(a2, 'r1');
|
||||
}
|
||||
}
|
||||
// The pinned countries' coarse regions, wherever the camera is.
|
||||
for (const a2 of this.pinned) {
|
||||
if (!this.manifest?.regions[a2]) continue;
|
||||
this.renderer.touchRegion(`${a2}:r0`);
|
||||
void this.loadRegion(a2, 'r0');
|
||||
}
|
||||
this.regionKeys = this.collectRegionKeys();
|
||||
|
||||
const time = this.now();
|
||||
@@ -768,6 +792,7 @@ export class GlobeEngine {
|
||||
selected: this.selected,
|
||||
ink,
|
||||
regionAlpha: this.regionAlpha,
|
||||
pinnedRegions: this.pinned,
|
||||
theme: this.theme,
|
||||
arcs: this.arcs,
|
||||
arcProgress,
|
||||
@@ -791,9 +816,13 @@ export class GlobeEngine {
|
||||
};
|
||||
|
||||
private collectRegionKeys(): string[] {
|
||||
if (this.regionAlpha <= 0) return [];
|
||||
const fine = this.cam.zoom >= REGION_FINE_ZOOM;
|
||||
const out: string[] = [];
|
||||
// Pinned countries draw their regions before the zoom brings the rest in.
|
||||
for (const a2 of this.pinned) {
|
||||
if (this.regionAlpha <= 0 && this.renderer.hasRegion(`${a2}:r0`)) out.push(`${a2}:r0`);
|
||||
}
|
||||
if (this.regionAlpha <= 0) return out;
|
||||
const fine = this.cam.zoom >= REGION_FINE_ZOOM;
|
||||
for (const a2 of this.regionIndex.keys()) {
|
||||
const k1 = `${a2}:r1`;
|
||||
const k0 = `${a2}:r0`;
|
||||
|
||||
@@ -448,6 +448,17 @@ describe('GlobeRenderer region layers', () => {
|
||||
expect(DARK.regionBorder[3]).toBe(0.5);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-RENDER-049: a pinned country draws its regions in full while zoomed out, with faint inner borders', () => {
|
||||
const { fake, r } = setup();
|
||||
r.setRegion('DE:r0', layer(4, 1, 5));
|
||||
r.draw(state({ regionAlpha: 0, regionKeys: ['DE:r0'], pinnedRegions: new Set(['DE']) }));
|
||||
const [region] = fillDraws(fake, 12);
|
||||
expect(region.values.u_mode).toBe(2);
|
||||
expect(region.values.u_layerAlpha).toBe(1);
|
||||
const [borders] = lineDraws(fake, 5);
|
||||
expect((borders.values.u_color as number[])[3]).toBeCloseTo(DARK.regionBorder[3] * 0.45);
|
||||
});
|
||||
|
||||
it('FE-ATLAS2-RENDER-029: does not draw regions faded out below the threshold', () => {
|
||||
const { fake, r } = setup();
|
||||
r.setRegion('DE', layer(4, 1, 5));
|
||||
|
||||
@@ -49,6 +49,8 @@ export interface DrawState {
|
||||
selected: number;
|
||||
ink: { id: number; lon: number; lat: number; radius: number } | null;
|
||||
regionAlpha: number;
|
||||
/** Countries whose regions show at full strength at every zoom (the regions view). */
|
||||
pinnedRegions?: ReadonlySet<string>;
|
||||
theme: GlobeTheme;
|
||||
arcs: ArcLayer[];
|
||||
arcProgress: number;
|
||||
@@ -199,7 +201,7 @@ export class GlobeRenderer {
|
||||
return vao;
|
||||
}
|
||||
|
||||
/** rgba: colour + fill strength. fx: hatch, reveal order (0..255), outline, spare. */
|
||||
/** rgba: colour + fill strength. fx: hatch, reveal order (0..255), regions loaded, visited regions. */
|
||||
setStatus(
|
||||
id: number,
|
||||
rgba: [number, number, number, number],
|
||||
@@ -454,7 +456,10 @@ export class GlobeRenderer {
|
||||
gl.bindVertexArray(this.ocean.vao);
|
||||
gl.drawElements(gl.TRIANGLES, this.ocean.count, gl.UNSIGNED_INT, 0);
|
||||
}
|
||||
const regionsOn = s.regionAlpha > 0.001 && s.regionKeys.some((k) => this.regions.has(k));
|
||||
// A pinned country's regions show whatever the zoom; the rest fade in with it.
|
||||
const pinnedKey = (k: string) => !!s.pinnedRegions?.has(k.slice(0, k.indexOf(':')));
|
||||
const fillAlpha = (k: string) => (pinnedKey(k) ? 1 : s.regionAlpha);
|
||||
const regionsOn = s.regionKeys.some((k) => this.regions.has(k) && fillAlpha(k) > 0.001);
|
||||
if (this.world) {
|
||||
gl.uniform1i(p.u.u_mode, 0);
|
||||
gl.uniform1f(p.u.u_dimBase, 1);
|
||||
@@ -464,10 +469,10 @@ export class GlobeRenderer {
|
||||
}
|
||||
if (regionsOn) {
|
||||
gl.uniform1i(p.u.u_mode, 2);
|
||||
gl.uniform1f(p.u.u_layerAlpha, s.regionAlpha);
|
||||
for (const k of s.regionKeys) {
|
||||
const r = this.regions.get(k);
|
||||
if (!r) continue;
|
||||
gl.uniform1f(p.u.u_layerAlpha, fillAlpha(k));
|
||||
gl.bindVertexArray(r.fill.vao);
|
||||
gl.drawElements(gl.TRIANGLES, r.fill.count, gl.UNSIGNED_INT, 0);
|
||||
}
|
||||
@@ -490,11 +495,13 @@ export class GlobeRenderer {
|
||||
gl.drawArraysInstanced(gl.TRIANGLES, 0, 6, g.count);
|
||||
};
|
||||
if (regionsOn) {
|
||||
const c = [...th.regionBorder];
|
||||
c[3] *= s.regionAlpha;
|
||||
for (const k of s.regionKeys) {
|
||||
const r = this.regions.get(k);
|
||||
if (r) lines(r.borders, c, 0.7);
|
||||
if (!r) continue;
|
||||
// Zoomed out, a pinned country's inner borders stay faint, so it does not turn into a web.
|
||||
const c = [...th.regionBorder];
|
||||
c[3] *= pinnedKey(k) ? Math.max(s.regionAlpha, 0.45) : s.regionAlpha;
|
||||
lines(r.borders, c, 0.7);
|
||||
}
|
||||
}
|
||||
if (this.world) {
|
||||
|
||||
@@ -84,7 +84,7 @@ in float v_shade;
|
||||
in vec2 v_ll;
|
||||
flat in int v_fid;
|
||||
uniform sampler2D u_status; // rgb colour, a strength
|
||||
uniform sampler2D u_fx; // r hatch, g reveal order, b outline flag
|
||||
uniform sampler2D u_fx; // r hatch, g reveal order, b regions loaded, a visited regions
|
||||
uniform int u_mode; // 0 land, 1 ocean, 2 region layer
|
||||
uniform vec3 u_land;
|
||||
uniform vec3 u_ocean;
|
||||
@@ -136,8 +136,9 @@ void main() {
|
||||
float d = acos(clamp(dot(p1, p2), -1.0, 1.0));
|
||||
a *= 1.0 - smoothstep(u_inkR - 0.01, u_inkR, d);
|
||||
}
|
||||
// A country whose regions are on screen steps back so its regions read.
|
||||
if (u_mode == 0 && fx.b > 0.5) a *= 1.0 - 0.32 * u_regionFade;
|
||||
// A country whose regions are on screen steps back so its regions read; one with
|
||||
// visited regions (fx.a) steps far back, so the regions really visited stand out.
|
||||
if (u_mode == 0 && fx.b > 0.5) a *= 1.0 - (fx.a > 0.5 ? 0.82 : 0.32) * u_regionFade;
|
||||
vec3 base = u_land * (u_mode == 2 ? 1.0 : u_dimBase);
|
||||
vec3 col = mix(base, st.rgb, a);
|
||||
if (fx.r > 0.5) {
|
||||
|
||||
Reference in New Issue
Block a user