mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
feat(firms): add Navara renderer checkpoint
This commit is contained in:
1 parent
a9b3125a99
commit
5dd05a4a81
33 files changed
+2112
-285
No files matched your search
@@ -323,6 +323,10 @@
|
||||
* colorBlend?: { mode: 'mix'|'replace', amount: number }, unlit?: boolean }} [model]
|
||||
* A glTF drawn per instance in metres, at least minimumPixelSize px.
|
||||
* @property {boolean} [pickable]
|
||||
* @property {boolean} [sharpen] Sprites/points: include the glyph in the
|
||||
* sharpen/AA stage after tone mapping, for translucent fields such as FIRMS.
|
||||
* Navara defaults to the later display stage used by W1; Cesium retains its
|
||||
* native billboard stage. Does not enable sharpening or change its amount.
|
||||
*
|
||||
* @typedef {object} MoverFields
|
||||
* One instance; omitted fields keep their value (defaults: white, scale 1,
|
||||
|
||||
+6
-1
@@ -66,7 +66,7 @@ function atlasOf(atlas) {
|
||||
/**
|
||||
* The createSet options with defaults filled in, or a TypeError.
|
||||
* @returns {{ id: string, kind: string, capacity: number, order: number,
|
||||
* depthTest: 'normal'|'always'|{ disableWithinM: number }, pickable: boolean,
|
||||
* depthTest: 'normal'|'always'|{ disableWithinM: number }, pickable: boolean, sharpen: boolean,
|
||||
* cellKeys: string[], sprite: object | null, point: object | null,
|
||||
* model: object | null }}
|
||||
*/
|
||||
@@ -80,6 +80,10 @@ export function normalizeMoverSetOptions(options) {
|
||||
fail(`capacity must be a positive integer: ${capacity}`);
|
||||
const order = options.order ?? 0;
|
||||
if (!isFiniteNumber(order)) fail(`order must be a number: ${order}`);
|
||||
if (options.sharpen !== undefined && typeof options.sharpen !== 'boolean')
|
||||
fail('sharpen must be a boolean');
|
||||
if (options.sharpen && kind === 'model')
|
||||
fail('sharpen applies to sprites and points');
|
||||
const normalized = {
|
||||
id,
|
||||
kind,
|
||||
@@ -87,6 +91,7 @@ export function normalizeMoverSetOptions(options) {
|
||||
order,
|
||||
depthTest: depthTestOf(options.depthTest),
|
||||
pickable: options.pickable === true,
|
||||
sharpen: options.sharpen === true,
|
||||
cellKeys: [],
|
||||
sprite: null,
|
||||
point: null,
|
||||
|
||||
@@ -264,3 +264,32 @@ test('point outline RGBA is validated and copied independently of the caller', (
|
||||
TypeError,
|
||||
);
|
||||
});
|
||||
|
||||
test('native-stage sprite fields opt into sharpening explicitly and reject model misuse', () => {
|
||||
const base = {
|
||||
id: 'firms',
|
||||
kind: 'sprite',
|
||||
capacity: 1,
|
||||
sprite: { atlas, sizePx: 56 },
|
||||
};
|
||||
assert.equal(normalizeMoverSetOptions(base).sharpen, false);
|
||||
assert.equal(
|
||||
normalizeMoverSetOptions({ ...base, sharpen: true }).sharpen,
|
||||
true,
|
||||
);
|
||||
assert.throws(
|
||||
() => normalizeMoverSetOptions({ ...base, sharpen: 'true' }),
|
||||
TypeError,
|
||||
);
|
||||
assert.throws(
|
||||
() =>
|
||||
normalizeMoverSetOptions({
|
||||
id: 'm',
|
||||
kind: 'model',
|
||||
capacity: 1,
|
||||
model: { url: '/box.glb' },
|
||||
sharpen: true,
|
||||
}),
|
||||
TypeError,
|
||||
);
|
||||
});
|
||||
@@ -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();
|
||||
},
|
||||
};
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
@@ -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];
|
||||
|
||||
@@ -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]);
|
||||
|
||||
@@ -9,12 +9,16 @@
|
||||
// pass costs nothing while no geometry is visible. Models retain scene
|
||||
// depth; their lit display colour is resolved here so HDR effects and MSAA
|
||||
// cannot spread a tint into neighbouring pixels.
|
||||
// FIRMS opts into a separate native-stage overlay before sharpening/AA.
|
||||
// Only that stage owns an encoded-RGBA8 blend target; W1 stays in place.
|
||||
|
||||
import { EffectDesc, Pass as NavaraPass } from '@navaramap/three';
|
||||
import { GevOverlayPass } from './overlayPass.js';
|
||||
import { RGBADepthPacking, Scene } from 'three';
|
||||
|
||||
export const OVERLAY_EFFECT_KEY = 'gevOverlay';
|
||||
const PRE_SHARPEN_KEY = 'gevPreSharpenOverlay';
|
||||
const registeredViews = new WeakMap();
|
||||
|
||||
class GevOverlayEffectDesc extends EffectDesc {
|
||||
static key = OVERLAY_EFFECT_KEY;
|
||||
@@ -24,7 +28,7 @@ class GevOverlayEffectDesc extends EffectDesc {
|
||||
|
||||
constructor(view, ctx, config) {
|
||||
super(view, ctx, config);
|
||||
this.overlay = config[OVERLAY_EFFECT_KEY].overlay;
|
||||
this.overlay = config[this.constructor.key].overlay;
|
||||
this.overlay.attach(ctx);
|
||||
}
|
||||
|
||||
@@ -49,14 +53,35 @@ class GevOverlayEffectDesc extends EffectDesc {
|
||||
}
|
||||
}
|
||||
|
||||
class GevPreSharpenOverlayEffectDesc extends GevOverlayEffectDesc {
|
||||
static key = PRE_SHARPEN_KEY;
|
||||
static insertAfter = ['toneMapping'];
|
||||
static insertBefore = [
|
||||
'gevSharpen',
|
||||
'smaa',
|
||||
'fxaa',
|
||||
OVERLAY_EFFECT_KEY,
|
||||
'final',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* The overlay of one view: add(object, onFrame) puts a three object in the
|
||||
* overlay scene (onFrame runs each drawn frame before it draws);
|
||||
* registerPickable/unregisterPickable reach Navara's pick pass.
|
||||
* @param {object} view an initialised ThreeView
|
||||
*/
|
||||
export function createNavaraOverlay(view) {
|
||||
view.registerEffect(OVERLAY_EFFECT_KEY, GevOverlayEffectDesc);
|
||||
export function createNavaraOverlay(view, { beforeSharpen = false } = {}) {
|
||||
const key = beforeSharpen ? PRE_SHARPEN_KEY : OVERLAY_EFFECT_KEY;
|
||||
let registered = registeredViews.get(view);
|
||||
if (!registered) registeredViews.set(view, (registered = new Set()));
|
||||
if (!registered.has(key)) {
|
||||
view.registerEffect(
|
||||
key,
|
||||
beforeSharpen ? GevPreSharpenOverlayEffectDesc : GevOverlayEffectDesc,
|
||||
);
|
||||
registered.add(key);
|
||||
}
|
||||
const scene = new Scene();
|
||||
const frameHooks = new Map();
|
||||
const lights = new Map();
|
||||
@@ -64,9 +89,11 @@ export function createNavaraOverlay(view) {
|
||||
const pendingPicks = new Map();
|
||||
const overlay = {
|
||||
scene,
|
||||
displayBlend: beforeSharpen,
|
||||
modelTints: new Set(),
|
||||
uniforms: {
|
||||
uGevModelExposure: { value: 1 },
|
||||
uGevDisplayBlend: { value: 0 },
|
||||
tGevSceneDepth: { value: null },
|
||||
uGevResolution: { value: [0, 0] },
|
||||
},
|
||||
@@ -119,7 +146,7 @@ export function createNavaraOverlay(view) {
|
||||
},
|
||||
};
|
||||
const handle = view.addEffect({
|
||||
[OVERLAY_EFFECT_KEY]: { overlay },
|
||||
[key]: { overlay },
|
||||
visible: true,
|
||||
});
|
||||
return {
|
||||
|
||||
@@ -18,6 +18,10 @@ export const OVERLAY_DEPTH_FRAGMENT = /* glsl */ `
|
||||
uniform sampler2D tGevSceneDepth;
|
||||
uniform vec2 uGevResolution;
|
||||
uniform float uGevDepthMode;
|
||||
uniform float uGevDisplayBlend;
|
||||
vec4 gevOverlayDisplayColor(vec4 color) {
|
||||
return uGevDisplayBlend>0.5 ? sRGBTransferOETF(color) : color;
|
||||
}
|
||||
varying float vGevDepthFree;
|
||||
bool gevOverlayHidden() {
|
||||
if (uGevDepthMode < 0.5 || vGevDepthFree > 0.5 || uGevResolution.x < 1.0)
|
||||
|
||||
@@ -1,23 +1,226 @@
|
||||
import { Pass } from 'postprocessing';
|
||||
import { FullScreenQuad } from 'three/addons/postprocessing/Pass.js';
|
||||
import {
|
||||
Color,
|
||||
HalfFloatType,
|
||||
ShaderMaterial,
|
||||
WebGLRenderTarget,
|
||||
NearestFilter,
|
||||
NoColorSpace,
|
||||
SRGBColorSpace,
|
||||
} from 'three';
|
||||
|
||||
const vertexShader = `
|
||||
varying vec2 vUv;
|
||||
void main() { vUv=uv; gl_Position=vec4(position.xy,0.0,1.0); }
|
||||
`;
|
||||
const transfer = `
|
||||
vec3 gevDisplay(vec3 c) {
|
||||
return mix(12.92*c,1.055*pow(max(c,vec3(0.0)),vec3(1.0/2.4))-0.055,step(vec3(0.0031308),c));
|
||||
}
|
||||
vec3 gevLinear(vec3 c) {
|
||||
return mix(c/12.92,pow(max((c+0.055)/1.055,vec3(0.0)),vec3(2.4)),step(vec3(0.04045),c));
|
||||
}
|
||||
`;
|
||||
function displayResources(renderer) {
|
||||
// Keep only the native-stage glyphs in this target. The final composite
|
||||
// blends them over the input once, avoiding a full-screen encode copy.
|
||||
const target = new WebGLRenderTarget(1, 1, {
|
||||
type: HalfFloatType,
|
||||
minFilter: NearestFilter,
|
||||
magFilter: NearestFilter,
|
||||
depthBuffer: false,
|
||||
stencilBuffer: false,
|
||||
});
|
||||
target.texture.colorSpace = NoColorSpace;
|
||||
const composite = new ShaderMaterial({
|
||||
vertexShader,
|
||||
fragmentShader: `uniform sampler2D image; uniform sampler2D glyphs; uniform bool encodeOutput; varying vec2 vUv; ${transfer}
|
||||
void main(){vec4 bg=texture2D(image,vUv);vec4 glyph=texture2D(glyphs,vUv);if(glyph.a<=0.0){gl_FragColor=vec4(encodeOutput?gevDisplay(bg.rgb):bg.rgb,bg.a);return;}vec3 display=gevDisplay(bg.rgb)*(1.0-glyph.a)+glyph.rgb;display=floor(clamp(display,0.0,1.0)*255.0+0.5)/255.0;vec3 linear=gevLinear(display);gl_FragColor=vec4(encodeOutput?gevDisplay(linear):linear,bg.a);}`,
|
||||
uniforms: {
|
||||
image: { value: null },
|
||||
glyphs: { value: null },
|
||||
encodeOutput: { value: false },
|
||||
},
|
||||
depthTest: false,
|
||||
depthWrite: false,
|
||||
toneMapped: false,
|
||||
});
|
||||
const quad = new FullScreenQuad(composite);
|
||||
const canvas = renderer.domElement;
|
||||
const invalidate = () => {
|
||||
target.signature = null;
|
||||
};
|
||||
canvas?.addEventListener('webglcontextlost', invalidate);
|
||||
canvas?.addEventListener('webglcontextrestored', invalidate);
|
||||
return { target, composite, quad, canvas, invalidate };
|
||||
}
|
||||
|
||||
function staticSpriteSignature(scene, camera, renderer, width, height) {
|
||||
if (
|
||||
scene.children.length === 0 ||
|
||||
scene.background ||
|
||||
scene.fog ||
|
||||
scene.overrideMaterial
|
||||
)
|
||||
return null;
|
||||
// The globe render updates the shared camera before this pass. Mover origins
|
||||
// update their world matrices when placed, so a cache hit needn't traverse
|
||||
// the overlay scene just to compare its static state.
|
||||
const snapshot = [
|
||||
width,
|
||||
height,
|
||||
renderer.getPixelRatio(),
|
||||
renderer.toneMapping,
|
||||
renderer.toneMappingExposure,
|
||||
camera.layers.mask,
|
||||
...camera.matrixWorldInverse.elements,
|
||||
...camera.projectionMatrix.elements,
|
||||
];
|
||||
for (const mesh of scene.children) {
|
||||
if (!mesh.isInstancedMesh || mesh.userData.gevStaticSprite !== true)
|
||||
return null;
|
||||
const material = mesh.material,
|
||||
texture = material?.map;
|
||||
if (!texture) return null;
|
||||
snapshot.push(
|
||||
mesh.id,
|
||||
mesh.visible ? 1 : 0,
|
||||
mesh.layers.mask,
|
||||
mesh.count,
|
||||
mesh.renderOrder,
|
||||
mesh.instanceMatrix.version,
|
||||
mesh.instanceColor?.version ?? -1,
|
||||
...mesh.matrixWorld.elements,
|
||||
material.id,
|
||||
material.version,
|
||||
material.opacity,
|
||||
material.alphaTest,
|
||||
material.side,
|
||||
material.blending,
|
||||
material.transparent ? 1 : 0,
|
||||
material.toneMapped ? 1 : 0,
|
||||
material.color.r,
|
||||
material.color.g,
|
||||
material.color.b,
|
||||
texture.id,
|
||||
texture.version,
|
||||
texture.colorSpace,
|
||||
texture.format,
|
||||
texture.type,
|
||||
texture.minFilter,
|
||||
texture.magFilter,
|
||||
...texture.matrix.elements,
|
||||
mesh.geometry.drawRange.start,
|
||||
mesh.geometry.drawRange.count,
|
||||
mesh.geometry.index?.version ?? -1,
|
||||
mesh.geometry.index?.count ?? 0,
|
||||
);
|
||||
for (const [name, attribute] of Object.entries(mesh.geometry.attributes))
|
||||
snapshot.push(name, attribute.version, attribute.count);
|
||||
}
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
function signatureMatches(current, previous) {
|
||||
return (
|
||||
current &&
|
||||
previous &&
|
||||
current.length === previous.length &&
|
||||
current.every((value, index) => value === previous[index])
|
||||
);
|
||||
}
|
||||
|
||||
export class GevOverlayPass extends Pass {
|
||||
constructor(scene, camera, overlay) {
|
||||
super('GevOverlayPass', scene, camera);
|
||||
this.needsSwap = false;
|
||||
this.overlay = overlay;
|
||||
this.display = null;
|
||||
}
|
||||
render(renderer, inputBuffer) {
|
||||
render(renderer, inputBuffer, outputBuffer) {
|
||||
this.needsSwap = false;
|
||||
if (!this.overlay.hasDrawables()) return;
|
||||
const resolution = this.overlay.uniforms.uGevResolution.value;
|
||||
resolution[0] = inputBuffer?.width ?? 0;
|
||||
resolution[1] = inputBuffer?.height ?? 0;
|
||||
if (!this.overlay.displayBlend) {
|
||||
const autoClear = renderer.autoClear;
|
||||
renderer.autoClear = false;
|
||||
try {
|
||||
renderer.setRenderTarget(this.renderToScreen ? null : inputBuffer);
|
||||
renderer.render(this.scene, this.camera);
|
||||
} finally {
|
||||
renderer.autoClear = autoClear;
|
||||
}
|
||||
return;
|
||||
}
|
||||
const display = (this.display ??= displayResources(renderer));
|
||||
const signature = staticSpriteSignature(
|
||||
this.scene,
|
||||
this.camera,
|
||||
renderer,
|
||||
resolution[0],
|
||||
resolution[1],
|
||||
);
|
||||
const targetWidth = resolution[0];
|
||||
const targetHeight = resolution[1];
|
||||
if (
|
||||
display.target.width !== targetWidth ||
|
||||
display.target.height !== targetHeight
|
||||
) {
|
||||
display.target.setSize(targetWidth, targetHeight);
|
||||
display.signature = null;
|
||||
}
|
||||
const autoClear = renderer.autoClear;
|
||||
const output = this.renderToScreen ? null : outputBuffer;
|
||||
this.needsSwap = !this.renderToScreen;
|
||||
const blend = this.overlay.uniforms.uGevDisplayBlend;
|
||||
const reuseGlyphs = signatureMatches(signature, display.signature);
|
||||
const clearColor = reuseGlyphs ? null : renderer.getClearColor(new Color());
|
||||
const clearAlpha = reuseGlyphs ? null : renderer.getClearAlpha();
|
||||
renderer.autoClear = false;
|
||||
try {
|
||||
renderer.setRenderTarget(this.renderToScreen ? null : inputBuffer);
|
||||
renderer.render(this.scene, this.camera);
|
||||
if (!reuseGlyphs) {
|
||||
renderer.setClearColor(0x000000, 0);
|
||||
renderer.setRenderTarget(display.target);
|
||||
renderer.clear(true, false, false);
|
||||
blend.value = 1;
|
||||
renderer.render(this.scene, this.camera);
|
||||
blend.value = 0;
|
||||
display.signature = signature;
|
||||
}
|
||||
display.composite.uniforms.image.value = inputBuffer.texture;
|
||||
display.composite.uniforms.glyphs.value = display.target.texture;
|
||||
display.composite.uniforms.encodeOutput.value =
|
||||
this.renderToScreen && renderer.outputColorSpace === SRGBColorSpace;
|
||||
renderer.setRenderTarget(output);
|
||||
display.quad.material = display.composite;
|
||||
display.quad.render(renderer);
|
||||
} finally {
|
||||
if (!reuseGlyphs) {
|
||||
blend.value = 0;
|
||||
renderer.setClearColor(clearColor, clearAlpha);
|
||||
}
|
||||
renderer.setRenderTarget(output);
|
||||
renderer.autoClear = autoClear;
|
||||
}
|
||||
}
|
||||
dispose() {
|
||||
if (this.display) {
|
||||
this.display.canvas?.removeEventListener(
|
||||
'webglcontextlost',
|
||||
this.display.invalidate,
|
||||
);
|
||||
this.display.canvas?.removeEventListener(
|
||||
'webglcontextrestored',
|
||||
this.display.invalidate,
|
||||
);
|
||||
this.display.target.dispose();
|
||||
this.display.composite.dispose();
|
||||
this.display.quad.dispose();
|
||||
this.display = null;
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
@@ -1,43 +1,224 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { Scene, PerspectiveCamera } from 'three';
|
||||
import {
|
||||
Scene,
|
||||
PerspectiveCamera,
|
||||
HalfFloatType,
|
||||
NoColorSpace,
|
||||
SRGBColorSpace,
|
||||
InstancedMesh,
|
||||
MeshBasicMaterial,
|
||||
PlaneGeometry,
|
||||
Texture,
|
||||
} from 'three';
|
||||
import { GevOverlayPass } from './overlayPass.js';
|
||||
test('overlay draws in place only with visible objects and restores renderer state on failure', () => {
|
||||
let active = false;
|
||||
|
||||
function harness() {
|
||||
let active = false,
|
||||
target = null,
|
||||
clearColor = 0x123456,
|
||||
clearAlpha = 1;
|
||||
const overlay = {
|
||||
displayBlend: true,
|
||||
hasDrawables: () => active,
|
||||
uniforms: { uGevResolution: { value: [0, 0] } },
|
||||
uniforms: {
|
||||
uGevResolution: { value: [0, 0] },
|
||||
uGevDisplayBlend: { value: 0 },
|
||||
},
|
||||
};
|
||||
const pass = new GevOverlayPass(
|
||||
new Scene(),
|
||||
new PerspectiveCamera(),
|
||||
overlay,
|
||||
);
|
||||
const input = { width: 1280, height: 800 };
|
||||
let target,
|
||||
renders = 0;
|
||||
const scene = new Scene(),
|
||||
pass = new GevOverlayPass(scene, new PerspectiveCamera(), overlay),
|
||||
draws = [];
|
||||
const renderer = {
|
||||
autoClear: true,
|
||||
setRenderTarget: (value) => {
|
||||
outputColorSpace: SRGBColorSpace,
|
||||
getPixelRatio: () => 1,
|
||||
getClearColor(color) {
|
||||
return color.set(clearColor);
|
||||
},
|
||||
getClearAlpha() {
|
||||
return clearAlpha;
|
||||
},
|
||||
setClearColor(color, alpha) {
|
||||
clearColor = color.isColor ? color.getHex() : color;
|
||||
clearAlpha = alpha;
|
||||
},
|
||||
clear() {},
|
||||
get clearAlpha() {
|
||||
return clearAlpha;
|
||||
},
|
||||
setRenderTarget(value) {
|
||||
target = value;
|
||||
},
|
||||
render: () => {
|
||||
renders++;
|
||||
render(object) {
|
||||
draws.push({
|
||||
object,
|
||||
target,
|
||||
displayBlend: overlay.uniforms.uGevDisplayBlend.value,
|
||||
});
|
||||
},
|
||||
};
|
||||
pass.render(renderer, input);
|
||||
assert.equal(renders, 0);
|
||||
active = true;
|
||||
pass.render(renderer, input);
|
||||
assert.equal(target, input);
|
||||
assert.equal(renders, 1);
|
||||
assert.equal(pass.needsSwap, false);
|
||||
assert.equal(renderer.autoClear, true);
|
||||
assert.deepEqual(overlay.uniforms.uGevResolution.value, [1280, 800]);
|
||||
renderer.render = () => {
|
||||
throw new Error('render failed');
|
||||
return {
|
||||
overlay,
|
||||
scene,
|
||||
pass,
|
||||
draws,
|
||||
renderer,
|
||||
setActive(value) {
|
||||
active = value;
|
||||
},
|
||||
get target() {
|
||||
return target;
|
||||
},
|
||||
};
|
||||
assert.throws(() => pass.render(renderer, input), /render failed/);
|
||||
assert.equal(renderer.autoClear, true);
|
||||
pass.dispose();
|
||||
}
|
||||
test('empty overlays allocate no display target or draw calls', () => {
|
||||
const h = harness();
|
||||
h.pass.render(h.renderer, { width: 1280, height: 800, texture: {} }, {});
|
||||
assert.equal(h.pass.display, null);
|
||||
assert.deepEqual(h.draws, []);
|
||||
assert.equal(h.renderer.autoClear, true);
|
||||
h.pass.dispose();
|
||||
});
|
||||
test('display overlay composites encoded glyphs once into the next frame', () => {
|
||||
const h = harness(),
|
||||
input = {
|
||||
width: 1280,
|
||||
height: 800,
|
||||
samples: 4,
|
||||
texture: { name: 'linear frame' },
|
||||
},
|
||||
output = { texture: { name: 'next frame' } };
|
||||
h.setActive(true);
|
||||
h.pass.render(h.renderer, input, output);
|
||||
const display = h.pass.display;
|
||||
assert.equal(h.draws.length, 2);
|
||||
assert.equal(h.draws[0].object, h.scene);
|
||||
assert.equal(h.draws[0].target, display.target);
|
||||
assert.equal(h.draws[1].object, display.quad._mesh);
|
||||
assert.equal(h.draws[1].target, output);
|
||||
assert.deepEqual(
|
||||
h.draws.map((draw) => draw.displayBlend),
|
||||
[1, 0],
|
||||
);
|
||||
assert.equal(h.overlay.uniforms.uGevDisplayBlend.value, 0);
|
||||
assert.equal(
|
||||
display.target.samples,
|
||||
0,
|
||||
'native-stage alpha-masked glyphs need no second multisample raster',
|
||||
);
|
||||
assert.equal(display.target.texture.type, HalfFloatType);
|
||||
assert.equal(display.target.texture.colorSpace, NoColorSpace);
|
||||
assert.equal(display.target.depthBuffer, false);
|
||||
assert.deepEqual(h.overlay.uniforms.uGevResolution.value, [1280, 800]);
|
||||
assert.equal(h.pass.needsSwap, true);
|
||||
assert.equal(h.renderer.autoClear, true);
|
||||
assert.equal(display.composite.uniforms.image.value, input.texture);
|
||||
assert.equal(display.composite.uniforms.glyphs.value, display.target.texture);
|
||||
h.pass.render(h.renderer, input, output);
|
||||
assert.equal(h.pass.display, display);
|
||||
input.width = 640;
|
||||
h.pass.render(h.renderer, input, output);
|
||||
assert.equal(h.pass.display, display);
|
||||
assert.equal(display.target.width, 640);
|
||||
h.pass.renderToScreen = true;
|
||||
h.pass.render(h.renderer, input, output);
|
||||
assert.equal(h.target, null);
|
||||
assert.equal(display.composite.uniforms.encodeOutput.value, true);
|
||||
let sampleResets = 0;
|
||||
display.target.addEventListener('dispose', () => sampleResets++);
|
||||
input.samples = 0;
|
||||
h.pass.render(h.renderer, input, output);
|
||||
assert.equal(display.target.samples, 0);
|
||||
assert.equal(
|
||||
sampleResets,
|
||||
0,
|
||||
'changing scene raster samples leaves the glyph target unchanged',
|
||||
);
|
||||
let targetDisposals = 0,
|
||||
materialDisposals = 0;
|
||||
display.target.addEventListener('dispose', () => targetDisposals++);
|
||||
display.composite.addEventListener('dispose', () => materialDisposals++);
|
||||
h.pass.dispose();
|
||||
h.pass.dispose();
|
||||
assert.equal(targetDisposals, 1);
|
||||
assert.equal(materialDisposals, 1);
|
||||
assert.equal(h.pass.display, null);
|
||||
});
|
||||
test('overlay restores blend flag, destination and autoClear after a draw failure', () => {
|
||||
const h = harness(),
|
||||
input = { width: 1280, height: 800, texture: {} },
|
||||
output = {};
|
||||
h.setActive(true);
|
||||
const render = h.renderer.render;
|
||||
h.renderer.render = (object) => {
|
||||
if (object === h.scene) throw new Error('render failed');
|
||||
render(object);
|
||||
};
|
||||
assert.throws(
|
||||
() => h.pass.render(h.renderer, input, output),
|
||||
/render failed/,
|
||||
);
|
||||
assert.equal(h.renderer.autoClear, true);
|
||||
assert.equal(h.overlay.uniforms.uGevDisplayBlend.value, 0);
|
||||
assert.equal(h.target, output);
|
||||
assert.equal(h.renderer.clearAlpha, 1);
|
||||
h.pass.dispose();
|
||||
});
|
||||
test('static sprite glyphs reuse their target until instance data changes', () => {
|
||||
const h = harness(),
|
||||
input = { width: 1280, height: 800, texture: {} },
|
||||
output = {},
|
||||
texture = new Texture(),
|
||||
mesh = new InstancedMesh(
|
||||
new PlaneGeometry(1, 1),
|
||||
new MeshBasicMaterial({ map: texture }),
|
||||
1,
|
||||
);
|
||||
mesh.userData.gevStaticSprite = true;
|
||||
mesh.count = 1;
|
||||
mesh.instanceMatrix.needsUpdate = true;
|
||||
h.scene.add(mesh);
|
||||
h.setActive(true);
|
||||
h.pass.render(h.renderer, input, output);
|
||||
h.pass.render(h.renderer, input, output);
|
||||
assert.equal(h.draws.filter((draw) => draw.object === h.scene).length, 1);
|
||||
mesh.instanceMatrix.needsUpdate = true;
|
||||
h.pass.render(h.renderer, input, output);
|
||||
assert.equal(h.draws.filter((draw) => draw.object === h.scene).length, 2);
|
||||
h.pass.camera.position.x = 1;
|
||||
h.pass.camera.updateMatrixWorld();
|
||||
h.pass.render(h.renderer, input, output);
|
||||
assert.equal(h.draws.filter((draw) => draw.object === h.scene).length, 3);
|
||||
h.pass.dispose();
|
||||
texture.dispose();
|
||||
mesh.geometry.dispose();
|
||||
mesh.material.dispose();
|
||||
});
|
||||
|
||||
test('empty display overlay does not swap an unwritten frame buffer', () => {
|
||||
const h = harness();
|
||||
h.pass.render(h.renderer, { width: 1280, height: 800, texture: {} }, {});
|
||||
assert.equal(h.pass.needsSwap, false);
|
||||
assert.deepEqual(h.draws, []);
|
||||
h.pass.dispose();
|
||||
});
|
||||
|
||||
test('ordinary W1 overlays keep the direct pass without a display copy or target', () => {
|
||||
const h = harness(),
|
||||
input = { width: 1280, height: 800, samples: 4, texture: {} };
|
||||
h.overlay.displayBlend = false;
|
||||
h.setActive(true);
|
||||
h.pass.render(h.renderer, input);
|
||||
assert.equal(h.pass.display, null);
|
||||
assert.equal(h.draws.length, 1);
|
||||
assert.equal(h.draws[0].object, h.scene);
|
||||
assert.equal(h.target, input);
|
||||
assert.equal(h.renderer.autoClear, true);
|
||||
h.renderer.render = () => {
|
||||
throw new Error('ordinary draw failed');
|
||||
};
|
||||
assert.throws(() => h.pass.render(h.renderer, input), /ordinary draw failed/);
|
||||
assert.equal(h.renderer.autoClear, true);
|
||||
h.pass.dispose();
|
||||
});
|
||||
+17
-15
@@ -3,17 +3,11 @@
|
||||
// (src/ui/visualPresets.js SHARPEN_SHADER): a 3×3 box blur, then
|
||||
// center + (center − blur) × amount, clamped to [0, 1].
|
||||
//
|
||||
// Where it sits: right after tone mapping (after gev's overlay of sprites,
|
||||
// points and lines, which Cesium's sharpen stage also sharpens) and before
|
||||
// SMAA or FXAA (before `final` in a look without them), as Cesium runs its
|
||||
// custom stages after
|
||||
// its tone mapping and before FXAA. After tone mapping the colours are the
|
||||
// display's [0, 1] range, so highlights do not blow up; anti-aliasing after
|
||||
// it smooths the stair steps sharpening brings out. Cesium sharpens
|
||||
// gamma-encoded colours (its tone mapping ends with czm_inverseGamma) while
|
||||
// Navara's buffers stay linear until the final copy, so the mask encodes
|
||||
// with Cesium's gamma 2.2 and decodes after: the same amount sharpens the
|
||||
// same.
|
||||
// After tone mapping and native-stage fields such as FIRMS, before AA and
|
||||
// the ordinary W1 display overlay. Native SHARPEN_SHADER filters encoded
|
||||
// display RGB. Navara buffers stay linear, so use the exact sRGB transfer
|
||||
// before the box mask and its inverse afterward. A gamma-2.2 approximation
|
||||
// changes the colors of small, high-contrast glows even at the same amount.
|
||||
//
|
||||
// The sky is left alone: Navara's clouds are dithered (a fine grain at the
|
||||
// `low` quality the look uses), and the mask turned that grain into visible
|
||||
@@ -39,11 +33,19 @@ uniform bool uSkipSky;
|
||||
const float SKY_DEPTH = 0.99999;
|
||||
|
||||
vec3 encodeGamma(vec3 color) {
|
||||
return pow(max(color, vec3(0.0)), vec3(1.0 / 2.2));
|
||||
return mix(12.92*color,1.055*pow(max(color,vec3(0.0)),vec3(1.0/2.4))-0.055,step(vec3(0.0031308),color));
|
||||
}
|
||||
|
||||
vec3 decodeGamma(vec3 color) {
|
||||
return mix(color/12.92,pow(max((color+0.055)/1.055,vec3(0.0)),vec3(2.4)),step(vec3(0.04045),color));
|
||||
}
|
||||
|
||||
vec3 encoded8(vec3 color) {
|
||||
return floor(clamp(color, 0.0, 1.0) * 255.0 + 0.5) / 255.0;
|
||||
}
|
||||
|
||||
vec3 sampleGamma(vec2 uv) {
|
||||
return encodeGamma(texture2D(inputBuffer, uv).rgb);
|
||||
return encoded8(encodeGamma(texture2D(inputBuffer, uv).rgb));
|
||||
}
|
||||
|
||||
void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor) {
|
||||
@@ -53,7 +55,7 @@ void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor)
|
||||
return;
|
||||
}
|
||||
vec2 texel = texelSize;
|
||||
vec3 center = encodeGamma(inputColor.rgb);
|
||||
vec3 center = encoded8(encodeGamma(inputColor.rgb));
|
||||
vec3 blur = (
|
||||
sampleGamma(uv + vec2(-texel.x, -texel.y)) +
|
||||
sampleGamma(uv + vec2( 0.0, -texel.y)) +
|
||||
@@ -66,7 +68,7 @@ void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor)
|
||||
sampleGamma(uv + vec2( texel.x, texel.y))
|
||||
) / 9.0;
|
||||
vec3 sharpened = clamp(center + (center - blur) * amount, 0.0, 1.0);
|
||||
outputColor = vec4(pow(sharpened, vec3(2.2)), inputColor.a);
|
||||
outputColor = vec4(decodeGamma(sharpened), inputColor.a);
|
||||
}
|
||||
`;
|
||||
|
||||
|
||||
Reference in new issue
Block a user