Merge branch 'german/refactor-e4-model-retry' into german/navara-refactor

This commit is contained in:
German Khokhlov committed 2026-11-10 16:04:06 +04:00
commit 75cf0aeaf9
22 files changed
+1039 -470

No files matched your search

+12 -2
View File
@@ -51,14 +51,22 @@ class GevMoverSetDesc extends withFrameUpdates(MeshDesc) {
const group = new Group();
group.frustumCulled = false;
// Meshes arrive with the glTF; they join the group (and the pick pass).
mover.group.ready.then(() => {
mover.group.onLoad = () => {
if (this.destroyedByGev) return;
for (const { key, wrapper } of this.wrappers) {
this.ctx.unregisterPickableMesh(key);
batchIds.release(wrapper);
}
this.wrappers.length = 0;
this.gev.clearPickable();
group.clear();
mover.group.meshes.forEach((mesh, index) => {
group.add(mesh);
register(mover.group.pickMeshes[index], `${this.id}#${index}`);
});
this.view.forceUpdate();
});
};
if (mover.group.meshes.length) mover.group.onLoad();
return group;
}
@@ -68,6 +76,7 @@ class GevMoverSetDesc extends withFrameUpdates(MeshDesc) {
onDestroy() {
this.destroyedByGev = true;
this.gev.mover.group.onLoad = null;
for (const { key } of this.wrappers) this.ctx.unregisterPickableMesh(key);
this.gev.mover.dispose();
for (const { wrapper } of this.wrappers) this.gev.batchIds.release(wrapper);
@@ -163,6 +172,7 @@ export function createNavaraMovers(view, { overlay, aces = false }) {
kind: options.kind,
pickable: options.pickable,
batchIds,
clearPickable: () => pickIndex.remove(options.id),
onPickable: (wrapper) =>
pickIndex.add(options.id, wrapper, (index) => store.pickIds[index]),
},
+143 -89
View File
@@ -1,4 +1,4 @@
// The three side of a Navara model set: the glTF loaded once, one
// The three side of a Navara model set: the glTF shared by the set, one
// InstancedMesh per mesh in it (all instances share the model's meshes),
// each instance placed by position, orientation and scale relative to an
// origin near the camera. Cesium's minimumPixelSize is kept on the CPU once
@@ -17,6 +17,7 @@ import {
Sphere,
Vector3,
} from 'three';
import { createMoverRetry } from '../../movers.js';
import { footprintPerDepth, originFor } from './pack.js';
import { createModelTint } from './modelTint.js';
@@ -188,94 +189,133 @@ export function createModelMover(
for (const { mesh, tint } of parts) {
if (tint) {
tint.count = store.count;
tint.visible = store.count > 0;
tint.visible = loaded && store.count > 0;
}
mesh.count = store.count;
mesh.visible = store.count > 0;
mesh.visible = loaded && store.count > 0;
mesh.instanceMatrix.needsUpdate = true;
}
for (const attribute of colorAttributes) attribute.needsUpdate = true;
syncTransparency();
};
const group = { meshes: [], pickMeshes: [], ready: null };
let failed = false;
const asset = load(model.url).catch(() => {
// A missing asset keeps the layer's glyph fallback. Do not leak the
// rejected loader promise (or its URL) through descriptor creation.
if (!disposed) {
failed = true;
console.warn('[globe] Model asset unavailable');
const group = { meshes: [], pickMeshes: [], ready: null, onLoad: null };
const pickIds = new Map();
const current = (index) => pickIds.get(index) === store.pickIds[index];
let generation = 0;
let finish;
group.ready = new Promise((resolve) => (finish = resolve));
const releaseParts = () => {
for (const { mesh, tint } of parts) {
if (tint) {
overlay.remove(tint);
tint.material.dispose();
tint.dispose();
}
mesh.removeFromParent();
mesh.geometry.dispose();
mesh.material.dispose();
mesh.dispose?.();
}
return null;
});
group.ready = asset.then((gltf) => {
if (!gltf) return;
const root = gltf.scene;
root.traverse((child) => {
parts.length = materials.length = colorAttributes.length = 0;
group.meshes.length = group.pickMeshes.length = 0;
disposeTextures();
};
const releaseAsset = (gltf, releaseTexture) => {
gltf.scene.traverse((child) => {
child.geometry?.dispose();
for (const material of Array.isArray(child.material)
? child.material
: [child.material])
if (material)
: [child.material]) {
if (!material) continue;
if (releaseTexture)
for (const value of Object.values(material))
if (value?.isTexture) textures.add(value);
if (value?.isTexture) value.dispose();
material.dispose();
}
});
if (disposed) {
disposeTextures();
return;
}
root.updateMatrixWorld(true);
const sphere = new Box3()
.setFromObject(root)
.getBoundingSphere(new Sphere());
radius = Math.max(sphere.radius, 1e-3);
root.traverse((child) => {
if (!child.isMesh) return;
let material = child.material;
if (model.unlit)
material = new MeshBasicMaterial({
map: material.map ?? null,
color: material.color,
};
const retry = createMoverRetry(options.retryAfterMs, () => {
const version = generation;
load(model.url)
.then((gltf) => {
if (disposed || version !== generation) {
releaseAsset(gltf, true);
return;
}
releaseParts();
const root = gltf.scene;
root.traverse((child) => {
for (const material of Array.isArray(child.material)
? child.material
: [child.material])
if (material)
for (const value of Object.values(material))
if (value?.isTexture) textures.add(value);
});
else material = material.clone();
blendInstanceColor(material, uniforms, Boolean(overlay));
const geometry = child.geometry.clone();
const color = new InstancedBufferAttribute(
new Float32Array(capacity * 4).fill(1),
4,
);
color.setUsage(DynamicDrawUsage);
geometry.setAttribute('gevColor', color);
colorAttributes.push(color);
const mesh = new InstancedMesh(geometry, material, capacity);
mesh.instanceMatrix.setUsage(DynamicDrawUsage);
mesh.frustumCulled = false;
mesh.renderOrder = options.order;
mesh.count = 0;
mesh.visible = false;
const tint = overlay
? createModelTint(mesh, uniforms, overlay.uniforms)
: null;
if (tint) {
// Still present in the scene depth and GPU pick registry. Only colour
// moves to the display pass, where it is toned before raster resolve.
material.colorWrite = false;
overlay.add(tint);
}
materials.push(material);
parts.push({ mesh, tint, local: child.matrixWorld.clone() });
group.meshes.push(mesh);
group.pickMeshes.push(tint ?? mesh);
});
loaded = true;
if (origin)
for (const { mesh, tint } of parts) {
mesh.position.set(origin.x, origin.y, origin.z);
tint?.position.copy(mesh.position);
}
write(0, store.count);
onLoad(group.meshes);
root.updateMatrixWorld(true);
const sphere = new Box3()
.setFromObject(root)
.getBoundingSphere(new Sphere());
radius = Math.max(sphere.radius, 1e-3);
root.traverse((child) => {
if (!child.isMesh) return;
let material = child.material;
if (model.unlit)
material = new MeshBasicMaterial({
map: material.map ?? null,
color: material.color,
});
else material = material.clone();
blendInstanceColor(material, uniforms, Boolean(overlay));
const geometry = child.geometry.clone();
const color = new InstancedBufferAttribute(
new Float32Array(capacity * 4).fill(1),
4,
);
color.setUsage(DynamicDrawUsage);
geometry.setAttribute('gevColor', color);
colorAttributes.push(color);
const mesh = new InstancedMesh(geometry, material, capacity);
mesh.instanceMatrix.setUsage(DynamicDrawUsage);
mesh.frustumCulled = false;
mesh.renderOrder = options.order;
mesh.count = 0;
mesh.visible = false;
const tint = overlay
? createModelTint(mesh, uniforms, overlay.uniforms)
: null;
if (tint) {
// Still present in the scene depth and GPU pick registry. Only colour
// moves to the display pass, where it is toned before raster resolve.
material.colorWrite = false;
overlay.add(tint);
}
materials.push(material);
parts.push({ mesh, tint, local: child.matrixWorld.clone() });
group.meshes.push(mesh);
group.pickMeshes.push(tint ?? mesh);
});
loaded = true;
retry.succeed();
releaseAsset(gltf, false);
if (origin)
for (const { mesh, tint } of parts) {
mesh.position.set(origin.x, origin.y, origin.z);
tint?.position.copy(mesh.position);
}
write(0, store.count);
group.onLoad?.();
finish();
onLoad(group.meshes);
})
.catch(() => {
if (disposed || version !== generation) return;
retry.fail();
console.warn('[globe] Model asset unavailable');
});
});
retry.start();
const placeAt = (next) => {
origin = { x: next.x, y: next.y, z: next.z };
@@ -290,8 +330,19 @@ export function createModelMover(
return {
group,
uniforms,
ready: () => loaded,
failed: () => failed,
ready(index) {
if (disposed) return false;
if (index !== undefined) return loaded && current(index);
for (let at = 0; at < store.count; at++)
if (!loaded || !current(at)) return false;
return true;
},
failed(index) {
if (disposed || !retry.failed()) return false;
if (index !== undefined) return current(index);
for (let at = 0; at < store.count; at++) if (current(at)) return true;
return false;
},
origin: () => origin,
/**
* The drawn radius (px) of an instance at its last write, or null when
@@ -303,6 +354,17 @@ export function createModelMover(
? drawnPx[index] / 2
: null,
commit(range) {
let changed = false;
for (let index = 0; index < store.count; index++) {
if (pickIds.has(index) ? !current(index) : retry.failed())
changed = true;
pickIds.set(index, store.pickIds[index]);
}
if (changed) {
generation++;
loaded = false;
retry.start();
}
if (!origin && store.count > 0)
placeAt({
x: store.positions[0],
@@ -313,11 +375,11 @@ export function createModelMover(
for (const { mesh, tint } of parts) {
if (tint) {
tint.count = store.count;
tint.visible = store.count > 0;
tint.visible = loaded && store.count > 0;
}
mesh.count = store.count;
// Retained empty banks must also skip three's material/uniform work.
mesh.visible = store.count > 0;
mesh.visible = loaded && store.count > 0;
}
},
/** Each drawn frame: pixel limits need every instance rewritten. */
@@ -340,17 +402,9 @@ export function createModelMover(
dispose() {
if (disposed) return;
disposed = true;
for (const { mesh, tint } of parts) {
if (tint) {
overlay.remove(tint);
tint.material.dispose();
tint.dispose();
}
mesh.geometry.dispose();
mesh.material.dispose();
mesh.dispose?.();
}
disposeTextures();
retry.destroy();
releaseParts();
finish();
},
};
}
+100 -2
View File
@@ -197,12 +197,14 @@ test('a model removed before its load completes releases the arriving textures',
test('a failed model load stays not-ready without an unhandled rejection or leaking the URL', async (t) => {
const warnings = [];
t.mock.method(console, 'warn', (message) => warnings.push(message));
const mover = createModelMover(options, createMoverStore(options), {
const store = createMoverStore(options);
store.setCount(1);
const mover = createModelMover(options, store, {
load: async () => {
throw new Error('unavailable asset URL');
},
});
await mover.group.ready;
await new Promise(setImmediate);
assert.equal(mover.ready(), false);
assert.equal(mover.failed(), true);
assert.equal(mover.group.meshes.length, 0);
@@ -333,3 +335,99 @@ test('the display pass preserves unlit model colour without a lighting tone curv
);
mover.dispose();
});
for (const failures of [1, 2, 3])
test(`model retries ${failures} failures with one budget and releases its timer`, async (t) => {
t.mock.method(console, 'warn', () => {});
t.mock.timers.enable({ apis: ['setTimeout'] });
const store = createMoverStore(options);
let loads = 0;
let added = 0;
const mover = createModelMover({ ...options, retryAfterMs: 20 }, store, {
load: async () => {
if (++loads <= failures) throw new Error('503');
return fakeGltf();
},
});
mover.group.onLoad = () => added++;
t.after(() => mover.dispose());
store.set(0, { position: { x: 6378137, y: 0, z: 0 }, pickId: 'a' });
store.setCount(1);
mover.commit(store.dirty());
await new Promise(setImmediate);
assert.equal(mover.failed(0), true);
for (let attempt = 0; attempt < 2; attempt++) {
t.mock.timers.tick(19);
assert.equal(loads, attempt + 1);
t.mock.timers.tick(1);
await new Promise(setImmediate);
if (mover.ready()) break;
}
assert.equal(loads, Math.min(failures + 1, 3));
assert.equal(mover.ready(), failures < 3);
assert.equal(mover.failed(), failures === 3);
t.mock.timers.tick(10000);
assert.equal(loads, Math.min(failures + 1, 3));
store.set(0, { pickId: 'b' });
assert.equal(
mover.failed(),
false,
'the replacement has no failed attempt',
);
mover.commit(store.dirty());
assert.equal(mover.failed(0), false);
await new Promise(setImmediate);
assert.equal(mover.ready(0), true);
assert.equal(added, failures < 3 ? 2 : 1);
assert.equal(
mover.group.meshes.length,
1,
'replacement does not accumulate meshes',
);
mover.dispose();
t.mock.timers.tick(10000);
assert.equal(loads, Math.min(failures + 1, 3) + 1);
});
test('a pending obsolete identity releases its asset without overwriting the replacement', async (t) => {
const store = createMoverStore(options);
const pending = [];
const mover = createModelMover(options, store, {
load: () => new Promise((resolve) => pending.push(resolve)),
});
t.after(() => mover.dispose());
store.set(0, { pickId: 'a' });
store.setCount(1);
mover.commit(store.dirty());
store.set(0, { pickId: 'b' });
mover.commit(store.dirty());
const stale = fakeGltf();
let released = 0;
stale.scene.children[0].geometry.addEventListener(
'dispose',
() => released++,
);
pending[0](stale);
await new Promise(setImmediate);
assert.equal(released, 1);
assert.equal(mover.ready(), false);
pending[1](fakeGltf());
await mover.group.ready;
assert.equal(mover.ready(), true);
});
test('destroying a failed model cancels its pending retry', async (t) => {
t.mock.method(console, 'warn', () => {});
t.mock.timers.enable({ apis: ['setTimeout'] });
let loads = 0;
const mover = createModelMover(options, createMoverStore(options), {
load: async () => {
loads++;
throw new Error('503');
},
});
await new Promise(setImmediate);
mover.dispose();
t.mock.timers.tick(10000);
assert.equal(loads, 1);
});
+19 -15
View File
@@ -31,6 +31,7 @@ import {
OVERLAY_DEPTH_VERTEX,
} from '../overlayDepth.js';
import { DETACHED_SONAR, SONAR_VERTEX } from '../sonar.js';
import { createMoverRetry } from '../../movers.js';
const VERTEX_HEAD = /* glsl */ `
attribute vec4 gevStyle;
@@ -317,22 +318,9 @@ export function createMoverMesh(
};
// A canvas atlas is there at once; a URL one once its image loads.
let textureReady = point || Boolean(options.sprite.atlas.canvas);
let textureFailed = false;
const encodedAtlas = !point && options.sharpen === true;
const material = new MeshBasicMaterial({
map: point
? null
: atlasTexture(
options.sprite.atlas,
encodedAtlas,
() => {
textureReady = true;
onTextureLoad();
},
() => {
textureFailed = true;
},
),
map: null,
transparent: true,
alphaTest: point || encodedAtlas ? 0.005 : 0.02,
// Drawn after tone mapping, with no depth buffer: the shader tests the
@@ -341,6 +329,21 @@ export function createMoverMesh(
depthWrite: false,
side: FrontSide,
});
const retry = createMoverRetry(options.retryAfterMs, () => {
material.map?.dispose();
material.map = atlasTexture(
options.sprite.atlas,
encodedAtlas,
() => {
textureReady = true;
retry.succeed();
onTextureLoad();
},
() => retry.fail(),
);
material.needsUpdate = true;
});
if (!point) retry.start();
material.onBeforeCompile = (shader) =>
patchMoverShader(shader, uniforms, {
point,
@@ -394,7 +397,7 @@ export function createMoverMesh(
mesh,
uniforms,
ready: () => textureReady,
failed: () => textureFailed,
failed: retry.failed,
origin: () => origin,
/** Writes the changed instances; the origin starts at the first one. */
commit(range) {
@@ -455,6 +458,7 @@ export function createMoverMesh(
return true;
},
dispose() {
retry.destroy();
geometry.dispose();
material.map?.dispose();
material.dispose();
@@ -348,3 +348,51 @@ test('native-stage glow atlas filters encoded colors and keeps the native faint-
glow.dispose();
ordinary.dispose();
});
test('a URL atlas retries on fresh images, stops after three failures and cancels on dispose', (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const previous = globalThis.Image;
const images = [];
globalThis.Image = class {
constructor() {
images.push(this);
}
};
t.after(() => {
if (previous) globalThis.Image = previous;
else delete globalThis.Image;
});
const options = normalizeMoverSetOptions({
...spriteOptions,
retryAfterMs: 20,
sprite: {
sizePx: 8,
atlas: { url: '/atlas.png', cells: { a: [0, 0, 1, 1] } },
},
});
const mover = createMoverMesh(options, createMoverStore(options));
images[0].onerror();
assert.equal(mover.failed(), true);
t.mock.timers.tick(19);
assert.equal(images.length, 1);
t.mock.timers.tick(1);
assert.equal(mover.failed(), true);
images[1].onload();
assert.equal(mover.ready(), true);
assert.equal(mover.failed(), false);
assert.equal(images[0].onload, null);
mover.dispose();
const permanent = createMoverMesh(options, createMoverStore(options));
for (let i = 2; i < 5; i++) {
images[i].onerror();
t.mock.timers.tick(20);
}
assert.equal(images.length, 5);
assert.equal(permanent.failed(), true);
permanent.dispose();
const cancelled = createMoverMesh(options, createMoverStore(options));
images[5].onerror();
cancelled.dispose();
t.mock.timers.tick(10000);
assert.equal(images.length, 6);
});