feat(firms): add Navara renderer checkpoint

This commit is contained in:
German Khokhlov committed 2026-10-28 09:36:50 +04:00
1 parent a9b3125a99
commit 5dd05a4a81
33 files changed
+2112 -285

No files matched your search

+20 -7
View File
@@ -20,6 +20,7 @@ import { createModelMover } from './modelMover.js';
import { createMoverMesh } from './moverMesh.js';
import { createPickIndex } from './pickIds.js';
import { createMoverFrameCamera } from './frameCamera.js';
import { createNavaraOverlay } from '../overlay.js';
const DESC_NAME = 'gevMoverSet';
const registered = new WeakSet();
@@ -94,24 +95,32 @@ export function createNavaraMovers(view, { overlay, aces = false }) {
loader ??= createGltfLoader();
return loader.loadAsync(url);
};
let preSharpenOverlay = null,
preSharpenUsers = 0;
const request = () => view.forceUpdate();
const overlaySet = (options, store) => {
const targetOverlay = options.sharpen
? (preSharpenOverlay ??= createNavaraOverlay(view, {
beforeSharpen: true,
}))
: overlay;
if (options.sharpen) preSharpenUsers++;
const mover = createMoverMesh(options, store, {
onTextureLoad: request,
overlayUniforms: overlay.uniforms,
overlayUniforms: targetOverlay.uniforms,
});
const key = `gevMovers:${options.id}`;
if (options.pickable) {
const wrapper = new PickableInstancedMeshWrapper(
mover.mesh,
options.capacity,
overlay.ctx,
targetOverlay.ctx,
);
overlay.registerPickable(key, wrapper);
targetOverlay.registerPickable(key, wrapper);
pickIndex.add(options.id, wrapper, (index) => store.pickIds[index]);
}
overlay.add(mover.mesh, (time) =>
updateMover(readFrameCamera, overlay.ctx, mover, time),
targetOverlay.add(mover.mesh, (time) =>
updateMover(readFrameCamera, targetOverlay.ctx, mover, time),
);
return {
ready: mover.ready,
@@ -121,9 +130,13 @@ export function createNavaraMovers(view, { overlay, aces = false }) {
},
destroy() {
pickIndex.remove(options.id);
if (options.pickable) overlay.unregisterPickable(key);
overlay.remove(mover.mesh);
if (options.pickable) targetOverlay.unregisterPickable(key);
targetOverlay.remove(mover.mesh);
mover.dispose();
if (options.sharpen && --preSharpenUsers === 0) {
preSharpenOverlay.destroy();
preSharpenOverlay = null;
}
request();
},
};
+75 -20
View File
@@ -8,14 +8,15 @@
import {
CanvasTexture,
DoubleSide,
DynamicDrawUsage,
FrontSide,
InstancedBufferAttribute,
InstancedMesh,
LinearFilter,
MeshBasicMaterial,
PlaneGeometry,
SRGBColorSpace,
NoColorSpace,
Texture,
} from 'three';
import {
@@ -78,19 +79,19 @@ gevSizePx = gevOutlinedSize + 3.0 / uGevPixelRatio;
vGevPixelDistance = 2.0 / max(gevSizePx * uGevPixelRatio, 1e-3);
if (gevStyle.y <= 0.0) gevSizePx = 0.0;
#endif
float gevCos = cos(gevStyle.x);
float gevSin = sin(gevStyle.x);
vec2 gevCorner = position.xy;
vec2 gevTurned = vec2(
gevCorner.x * gevCos - gevCorner.y * gevSin,
gevCorner.x * gevSin + gevCorner.y * gevCos
gevCorner.x * gevStyle.z - gevCorner.y * gevStyle.w,
gevCorner.x * gevStyle.w + gevCorner.y * gevStyle.z
);
vec4 mvPosition = mvCenter;
mvPosition.xy += gevTurned * gevSizePx * uGevFootprint * max(-mvCenter.z, 0.0);
gl_Position = projectionMatrix * mvPosition;
vGevColor = gevColor;
vGevLocal = gevCorner;
#ifndef GEV_ALWAYS_VISIBLE
vGevDepthFree = gevDistance < uGevDisableWithinM ? 1.0 : 0.0;
#endif
`;
const VERTEX_UV = /* glsl */ `
@@ -138,10 +139,15 @@ diffuseColor *= sRGBTransferEOTF(vGevColor);
const DETACHED_OVERLAY = Object.freeze({
tGevSceneDepth: { value: null },
uGevResolution: { value: [0, 0] },
uGevDisplayBlend: { value: 0 },
});
/** Patches three's MeshBasicMaterial shaders into the screen-space quad. */
export function patchMoverShader(shader, uniforms, { point = false } = {}) {
export function patchMoverShader(
shader,
uniforms,
{ point = false, encodedAtlas = false, alwaysVisible = false } = {},
) {
Object.assign(shader.uniforms, uniforms);
const replace = (source, from, to) => {
if (!source.includes(from))
@@ -151,7 +157,7 @@ export function patchMoverShader(shader, uniforms, { point = false } = {}) {
let vertex = replace(
shader.vertexShader,
'void main() {',
`${point ? '#define GEV_POINT\n' : ''}${VERTEX_HEAD}\nvoid main() {`,
`${point ? '#define GEV_POINT\n' : ''}${alwaysVisible ? '#define GEV_ALWAYS_VISIBLE\n' : ''}${VERTEX_HEAD}\nvoid main() {`,
);
vertex = replace(vertex, '#include <project_vertex>', VERTEX_PROJECT);
vertex = replace(vertex, '#include <uv_vertex>', VERTEX_UV);
@@ -160,20 +166,34 @@ export function patchMoverShader(shader, uniforms, { point = false } = {}) {
'void main() {',
`${FRAGMENT_HEAD}\nvoid main() {`,
);
let colorFragment = fragmentColor(point);
if (alwaysVisible)
colorFragment = replace(
colorFragment,
'if (gevOverlayHidden()) discard;',
'',
);
fragment = replace(fragment, '#include <color_fragment>', colorFragment);
fragment = replace(
fragment,
'#include <color_fragment>',
fragmentColor(point),
'#include <colorspace_fragment>',
'gl_FragColor=gevOverlayDisplayColor(gl_FragColor);\n#include <colorspace_fragment>',
);
if (encodedAtlas)
fragment = replace(
fragment,
'#include <map_fragment>',
'#include <map_fragment>\ndiffuseColor.rgb = sRGBTransferEOTF(vec4(diffuseColor.rgb, 1.0)).rgb;',
);
shader.vertexShader = vertex;
shader.fragmentShader = fragment;
return shader;
}
// Sampled as Cesium samples its billboard atlas: linear, no mipmaps (a
// mipmapped atlas greys small glyphs that Cesium draws at full colour).
function cesiumSampling(texture) {
texture.colorSpace = SRGBColorSpace;
function cesiumSampling(texture, encoded = false) {
// Native FIRMS filters its original encoded PNG colors before display blending.
texture.colorSpace = encoded ? NoColorSpace : SRGBColorSpace;
texture.generateMipmaps = false;
texture.minFilter = LinearFilter;
texture.magFilter = LinearFilter;
@@ -181,9 +201,10 @@ function cesiumSampling(texture) {
}
/** The atlas as a three texture (a canvas at once, a URL once loaded). */
function atlasTexture(atlas, onLoad) {
if (atlas.canvas) return cesiumSampling(new CanvasTexture(atlas.canvas));
const texture = cesiumSampling(new Texture());
function atlasTexture(atlas, encoded, onLoad) {
if (atlas.canvas)
return cesiumSampling(new CanvasTexture(atlas.canvas), encoded);
const texture = cesiumSampling(new Texture(), encoded);
const image = new Image();
image.crossOrigin = 'anonymous';
image.onload = () => {
@@ -247,28 +268,41 @@ export function createMoverMesh(
// Shared by every overlay: the scene depth and the target's size.
tGevSceneDepth: overlayUniforms.tGevSceneDepth,
uGevResolution: overlayUniforms.uGevResolution,
uGevDisplayBlend:
overlayUniforms.uGevDisplayBlend ?? DETACHED_OVERLAY.uGevDisplayBlend,
};
// A canvas atlas is there at once; a URL one once its image loads.
let textureReady = point || Boolean(options.sprite.atlas.canvas);
const encodedAtlas = !point && options.sharpen === true;
const material = new MeshBasicMaterial({
map: point
? null
: atlasTexture(options.sprite.atlas, () => {
: atlasTexture(options.sprite.atlas, encodedAtlas, () => {
textureReady = true;
onTextureLoad();
}),
transparent: true,
alphaTest: point ? 0.005 : 0.02,
alphaTest: point || encodedAtlas ? 0.005 : 0.02,
// Drawn after tone mapping, with no depth buffer: the shader tests the
// scene depth itself (overlayDepth.js).
depthTest: false,
depthWrite: false,
side: DoubleSide,
side: FrontSide,
});
material.onBeforeCompile = (shader) =>
patchMoverShader(shader, uniforms, { point });
material.customProgramCacheKey = () => `gev-mover-${kind}`;
patchMoverShader(shader, uniforms, {
point,
encodedAtlas,
alwaysVisible: depthTest === 'always',
});
material.customProgramCacheKey = () =>
`gev-mover-${kind}${encodedAtlas ? '-encoded-atlas' : ''}`;
const mesh = new InstancedMesh(geometry, material, capacity);
mesh.userData.gevStaticSprite =
kind === 'sprite' &&
options.sharpen &&
depthTest === 'always' &&
!style.scaleByDistance;
mesh.instanceMatrix.setUsage(DynamicDrawUsage);
mesh.frustumCulled = false;
mesh.renderOrder = options.order;
@@ -301,6 +335,7 @@ export function createMoverMesh(
mesh.updateMatrix();
mesh.updateMatrixWorld(true);
};
let previousCamera = null;
return {
mesh,
uniforms,
@@ -332,6 +367,26 @@ export function createMoverMesh(
viewportHeightPx,
pixelRatio = 1,
}) {
if (
previousCamera &&
previousCamera.x === cameraPosition.x &&
previousCamera.y === cameraPosition.y &&
previousCamera.z === cameraPosition.z &&
previousCamera.height === cameraHeight &&
previousCamera.fov === fovDeg &&
previousCamera.viewportHeight === viewportHeightPx &&
previousCamera.pixelRatio === pixelRatio
)
return false;
previousCamera = {
x: cameraPosition.x,
y: cameraPosition.y,
z: cameraPosition.z,
height: cameraHeight,
fov: fovDeg,
viewportHeight: viewportHeightPx,
pixelRatio,
};
uniforms.uGevPixelRatio.value = pixelRatio;
uniforms.uGevFootprint.value = footprintPerDepth(
fovDeg,
+82 -1
View File
@@ -1,6 +1,12 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { ShaderLib, UniformsUtils } from 'three';
import {
ShaderLib,
UniformsUtils,
FrontSide,
NoColorSpace,
SRGBColorSpace,
} from 'three';
import { createMoverStore, normalizeMoverSetOptions } from '../../movers.js';
import { createMoverMesh, patchMoverShader } from './moverMesh.js';
@@ -27,6 +33,16 @@ test('the shader patch sizes the quad in pixels and colours it per instance', ()
const shader = patchMoverShader(basicShader(), { uGevSizePx: { value: 9 } });
assert.equal(shader.uniforms.uGevSizePx.value, 9);
assert.match(shader.vertexShader, /attribute vec4 gevStyle;/);
assert.match(shader.vertexShader, /gevCorner\.x \* gevStyle\.z/);
assert.doesNotMatch(
shader.vertexShader,
/cos\(gevStyle\.x\)|sin\(gevStyle\.x\)/,
);
assert.ok(
shader.vertexShader.indexOf('float gevDistance = length(mvCenter.xyz);') <
shader.vertexShader.indexOf('if (uGevHasScaleByDistance > 0.5)'),
'depth testing uses the distance outside the optional scaling block',
);
assert.match(
shader.vertexShader,
/uGevFootprint \* max\(-mvCenter\.z, 0\.0\)/,
@@ -89,6 +105,13 @@ test('update sets the pixel footprint and moves the origin to a far camera', ()
};
assert.equal(mover.update(camera), false, 'close: the origin stays');
assert.ok(Math.abs(mover.uniforms.uGevFootprint.value - 2 / 800) < 1e-12);
const matrixVersion = mover.mesh.instanceMatrix.version;
assert.equal(
mover.update(camera),
false,
'unchanged camera reuses its snapshot',
);
assert.equal(mover.mesh.instanceMatrix.version, matrixVersion);
camera.cameraPosition = { x: 6_378_137 + 5_000, y: 0, z: 0 };
assert.equal(mover.update(camera), true);
assert.equal(mover.origin().x, 6_378_137 + 5_000);
@@ -108,11 +131,22 @@ test('points have no atlas and draw a disc', () => {
const mover = createMoverMesh(options, createMoverStore(options));
assert.equal(mover.mesh.material.map, null);
assert.equal(mover.mesh.material.depthTest, false);
assert.equal(mover.mesh.material.side, FrontSide);
assert.equal(mover.uniforms.uGevDepthMode.value, 0, "'always': no test");
assert.equal(mover.uniforms.uGevOutlinePx.value, 1);
mover.dispose();
});
test('always-visible movers omit scene-depth sampling from the shader', () => {
const shader = patchMoverShader(basicShader(), {}, { alwaysVisible: true });
assert.match(shader.vertexShader, /#define GEV_ALWAYS_VISIBLE/);
assert.match(
shader.vertexShader,
/#ifndef GEV_ALWAYS_VISIBLE\nvGevDepthFree =[^\n]+\n#endif/,
);
assert.doesNotMatch(shader.fragmentShader, /gevOverlayHidden\(\)\) discard/);
});
test('an origin move uploads every instance even when a commit follows in the same frame', () => {
const store = createMoverStore(spriteOptions);
const mover = createMoverMesh(spriteOptions, store);
@@ -144,3 +178,50 @@ test('an origin move uploads every instance even when a commit follows in the sa
for (let i = 0; i < 4 * 16; i++)
assert.ok(covered.has(i), `instance float ${i} is uploaded`);
});
test('sprites share the live display-blend uniform of their overlay target', () => {
const flag = { value: 0 };
const mover = createMoverMesh(
spriteOptions,
createMoverStore(spriteOptions),
{
overlayUniforms: {
tGevSceneDepth: { value: null },
uGevResolution: { value: [1280, 800] },
uGevDisplayBlend: flag,
},
},
);
const shader = basicShader();
mover.mesh.material.onBeforeCompile(shader);
assert.equal(shader.uniforms.uGevDisplayBlend, flag);
flag.value = 1;
assert.equal(shader.uniforms.uGevDisplayBlend.value, 1);
assert.match(
shader.fragmentShader,
/gl_FragColor=gevOverlayDisplayColor\(gl_FragColor\)/,
);
mover.dispose();
});
test('native-stage glow atlas filters encoded colors and keeps the native faint-edge cutoff', () => {
const options = { ...spriteOptions, sharpen: true };
const glow = createMoverMesh(options, createMoverStore(options));
const ordinary = createMoverMesh(
spriteOptions,
createMoverStore(spriteOptions),
);
assert.equal(glow.mesh.material.map.colorSpace, NoColorSpace);
assert.equal(glow.mesh.material.alphaTest, 0.005);
assert.equal(ordinary.mesh.material.map.colorSpace, SRGBColorSpace);
assert.equal(ordinary.mesh.material.alphaTest, 0.02);
assert.notEqual(
glow.mesh.material.customProgramCacheKey(),
ordinary.mesh.material.customProgramCacheKey(),
);
const shader = basicShader();
glow.mesh.material.onBeforeCompile(shader);
assert.match(shader.fragmentShader, /diffuseColor.rgb = sRGBTransferEOTF/);
glow.dispose();
ordinary.dispose();
});
+5 -17
View File
@@ -42,7 +42,7 @@ export function originFor(cameraPosition, origin, cameraHeight) {
/**
* Writes instances [from, to) of a mover store into the upload arrays:
* `matrices` (identity with the offset from `origin` as translation),
* `style` (rotation, scale or 0 when hidden, two spare lanes), `colors`
* `style` (rotation, scale or 0 when hidden, and cached cosine/sine), `colors`
* (rgba) and `uvs` (the instance's atlas rect, from `uvRects`).
*/
export function packInstances(store, from, to, origin, uvRects, out) {
@@ -50,27 +50,15 @@ export function packInstances(store, from, to, origin, uvRects, out) {
const { matrices, style, colors: colorOut, uvs } = out;
for (let index = from; index < to; index++) {
const m = index * 16;
matrices[m] = 1;
matrices[m + 1] = 0;
matrices[m + 2] = 0;
matrices[m + 3] = 0;
matrices[m + 4] = 0;
matrices[m + 5] = 1;
matrices[m + 6] = 0;
matrices[m + 7] = 0;
matrices[m + 8] = 0;
matrices[m + 9] = 0;
matrices[m + 10] = 1;
matrices[m + 11] = 0;
matrices[m + 12] = positions[index * 3] - origin.x;
matrices[m + 13] = positions[index * 3 + 1] - origin.y;
matrices[m + 14] = positions[index * 3 + 2] - origin.z;
matrices[m + 15] = 1;
const s = index * 4;
style[s] = rotations[index];
const rotation = rotations[index];
style[s] = rotation;
style[s + 1] = shows[index] === 1 ? scales[index] : 0;
style[s + 2] = 0;
style[s + 3] = 0;
style[s + 2] = Math.cos(rotation);
style[s + 3] = Math.sin(rotation);
colorOut[s] = colors[s];
colorOut[s + 1] = colors[s + 1];
colorOut[s + 2] = colors[s + 2];
+11 -3
View File
@@ -61,6 +61,12 @@ test('instances pack as offsets from the origin, style, colour and uv', () => {
colors: new Float32Array(12),
uvs: new Float32Array(12),
};
for (let index = 0; index < 3; index++) {
out.matrices[index * 16] = 1;
out.matrices[index * 16 + 5] = 1;
out.matrices[index * 16 + 10] = 1;
out.matrices[index * 16 + 15] = 1;
}
const uvRects = atlasUvRects(options.sprite.atlas.cells, options.cellKeys);
packInstances(store, 0, 2, { x: 1000, y: 2000, z: 3000 }, uvRects, out);
// Identity matrix with the offset as translation.
@@ -69,9 +75,11 @@ test('instances pack as offsets from the origin, style, colour and uv', () => {
[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0.5, 0, 0, 1],
);
assert.deepEqual([...out.matrices.subarray(28, 31)], [1, 1, 1]);
// rotation, scale (0 when hidden), and two spare lanes.
assert.deepEqual([...out.style.subarray(0, 4)], [1, 2, 0, 0]);
assert.deepEqual([...out.style.subarray(4, 8)], [0, 0, 0, 0]);
// rotation, scale (0 when hidden), and cached cosine/sine.
assert.deepEqual([...out.style.subarray(0, 2)], [1, 2]);
assert.ok(Math.abs(out.style[2] - Math.cos(1)) < 1e-7);
assert.ok(Math.abs(out.style[3] - Math.sin(1)) < 1e-7);
assert.deepEqual([...out.style.subarray(4, 8)], [0, 0, 1, 0]);
assert.deepEqual([...out.colors.subarray(4, 8)], [1, 0, 0, 0.5]);
assert.deepEqual([...out.uvs.subarray(0, 4)], [0, 0, 1, 1]);
assert.deepEqual([...out.uvs.subarray(4, 8)], [0, 0.5, 0.5, 1]);