mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-18 01:02:23 +00:00
Keep GNSS history across globe changes and share the Navara ground cell pass
This commit is contained in:
1 parent
6fe5bda028
commit
1930ca247a
20 files changed
+629
-415
No files matched your search
@@ -1,291 +1,26 @@
|
||||
// FIRMS terrain/tiles classification and actual engine mover glows.
|
||||
import { Effect, EffectAttribute } from 'postprocessing';
|
||||
import { Matrix4, Uniform } from 'three';
|
||||
import { createEngineLayerView } from '../../shared/render/engineView.js';
|
||||
import { selectionDispatcherFor } from '../../../data/selectionDispatcher.js';
|
||||
import { LOD_LEVELS } from '../policy.js';
|
||||
import { createFirmsSpriteBank } from './navaraSprites.js';
|
||||
import { rectangleFrame } from './navaraGeometry.js';
|
||||
import {
|
||||
viewFrame,
|
||||
groundClipBounds,
|
||||
} from '../../shared/render/navaraGroundGeometry.js';
|
||||
import {
|
||||
createGroundGrid,
|
||||
GRID_COLS,
|
||||
GRID_ROWS,
|
||||
} from '../../shared/render/navaraGroundGrid.js';
|
||||
import {
|
||||
createGroundTexture,
|
||||
createGroundStorage,
|
||||
groundSurfaceUniforms,
|
||||
updateGroundSurface,
|
||||
} from '../../shared/render/navaraGroundSurface.js';
|
||||
const KEY = 'gevFirmsGround';
|
||||
const registeredViews = new WeakSet();
|
||||
const fragmentShader = /* glsl */ `
|
||||
uniform sampler2D tAllDepth;
|
||||
uniform sampler2D tSurfaceDepth;
|
||||
uniform sampler2D tCells;
|
||||
uniform sampler2D tGrid;
|
||||
uniform sampler2D tCandidates;
|
||||
uniform vec2 uCellSize;
|
||||
uniform vec2 uCandidateSize;
|
||||
uniform mat4 uInverseProjection;
|
||||
uniform mat4 uInverseView;
|
||||
uniform float uLogDepthFC;
|
||||
vec4 atSlot(sampler2D image,vec2 size,float slot) {
|
||||
return texture2D(image,(vec2(mod(slot,size.x),floor(slot/size.x))+0.5)/size);
|
||||
}
|
||||
vec3 toDisplay(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 toLinear(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));
|
||||
}
|
||||
vec3 pixelPlane(float distance,vec3 normal,vec3 dx,vec3 dy) {
|
||||
return vec3(distance,dot(normal,dx),dot(normal,dy));
|
||||
}
|
||||
float rectangleCoverage(vec3 west,vec3 east,vec3 south,vec3 north) {
|
||||
vec3 edges[4];
|
||||
edges[0]=west;edges[1]=east;edges[2]=south;edges[3]=north;
|
||||
float coverage=1.0;
|
||||
int partial=0;
|
||||
for(int i=0;i<4;i++) {
|
||||
float width=abs(edges[i].y)+abs(edges[i].z);
|
||||
coverage=min(coverage,clamp(edges[i].x/max(width,0.001)+0.5,0.0,1.0));
|
||||
if(coverage<=0.0) return 0.0;
|
||||
if(edges[i].x<=width*0.5) partial++;
|
||||
}
|
||||
if(partial<2) return coverage;
|
||||
// Intersect the SAME pixel square with all sides. Minimum soft-edge
|
||||
// coverage creates long miter halos at acute/polar rectangle corners.
|
||||
vec2 polygon[8],clipped[8];
|
||||
polygon[0]=vec2(-0.5,-0.5);polygon[1]=vec2(0.5,-0.5);
|
||||
polygon[2]=vec2(0.5,0.5);polygon[3]=vec2(-0.5,0.5);
|
||||
int count=4;
|
||||
for(int side=0;side<4;side++) {
|
||||
int nextCount=0;
|
||||
vec2 previous=polygon[count-1];
|
||||
float before=edges[side].x+dot(edges[side].yz,previous);
|
||||
for(int i=0;i<8;i++) {
|
||||
if(i>=count) break;
|
||||
vec2 current=polygon[i];
|
||||
float after=edges[side].x+dot(edges[side].yz,current);
|
||||
if((before>0.0&&after<0.0)||(before<0.0&&after>0.0))
|
||||
clipped[nextCount++]=mix(previous,current,before/(before-after));
|
||||
if(after>=0.0) clipped[nextCount++]=current;
|
||||
previous=current;before=after;
|
||||
}
|
||||
count=nextCount;
|
||||
if(count<3) return 0.0;
|
||||
for(int i=0;i<8;i++) { if(i>=count) break;polygon[i]=clipped[i]; }
|
||||
}
|
||||
float area=0.0;
|
||||
vec2 previous=polygon[count-1];
|
||||
for(int i=0;i<8;i++) {
|
||||
if(i>=count) break;
|
||||
area+=previous.x*polygon[i].y-previous.y*polygon[i].x;
|
||||
previous=polygon[i];
|
||||
}
|
||||
return clamp(abs(area)*0.5,0.0,1.0);
|
||||
}
|
||||
void mainImage(const in vec4 inputColor,const in vec2 uv,out vec4 outputColor) {
|
||||
outputColor=inputColor;
|
||||
float depth=unpackRGBAToDepth(texture2D(tAllDepth,uv));
|
||||
float distance=exp2(2.0*depth/uLogDepthFC)-1.0;
|
||||
vec4 ray=uInverseProjection*vec4(uv*2.0-1.0,-1.0,1.0);
|
||||
vec3 point=ray.xyz*(distance/-ray.z);
|
||||
// Evaluate derivatives before divergent candidate branches, as in quakes.
|
||||
vec3 world=(uInverseView*vec4(point,1.0)).xyz;
|
||||
vec3 dx=(uInverseView*vec4(dFdx(point),0.0)).xyz;
|
||||
vec3 dy=(uInverseView*vec4(dFdy(point),0.0)).xyz;
|
||||
// A quad crossing the sky has no surface derivative. Keep exact halfspaces
|
||||
// there; otherwise its far-plane lane spreads cell AA along the globe limb.
|
||||
float onSurface=1.0-step(1.0,depth);
|
||||
float derivativeMask=1.0-step(0.5,abs(dFdx(onSurface))+abs(dFdy(onSurface)));
|
||||
dx*=derivativeMask;
|
||||
dy*=derivativeMask;
|
||||
if(depth>=1.0) return;
|
||||
vec2 tile=clamp(floor(uv*vec2(${GRID_COLS}.0,${GRID_ROWS}.0)),vec2(0.0),vec2(${GRID_COLS - 1}.0,${GRID_ROWS - 1}.0));
|
||||
vec2 range=texture2D(tGrid,(tile+0.5)/vec2(${GRID_COLS}.0,${GRID_ROWS}.0)).xy;
|
||||
if(range.y<0.5) return;
|
||||
float surface=texture2D(tSurfaceDepth,uv).r;
|
||||
if(depth<surface-2e-7) return;
|
||||
float rho=length(world.xy);
|
||||
vec2 radial=world.xy/max(rho,0.001);
|
||||
vec3 color=toDisplay(inputColor.rgb);
|
||||
for(int k=0;k<int(range.y);k++) {
|
||||
float id=atSlot(tCandidates,uCandidateSize,range.x+float(k)).x;
|
||||
vec4 longitude=atSlot(tCells,uCellSize,id*4.0);
|
||||
vec3 south=atSlot(tCells,uCellSize,id*4.0+1.0).xyz;
|
||||
vec3 north=atSlot(tCells,uCellSize,id*4.0+2.0).xyz;
|
||||
vec3 westNormal=vec3(-longitude.x,longitude.y,0.0);
|
||||
vec3 eastNormal=vec3(longitude.z,-longitude.w,0.0);
|
||||
float coverage=rectangleCoverage(
|
||||
pixelPlane(dot(westNormal,world),westNormal,dx,dy),
|
||||
pixelPlane(dot(eastNormal,world),eastNormal,dx,dy),
|
||||
pixelPlane(world.z*south.y-rho*south.x+south.z,vec3(-south.x*radial,south.y),dx,dy),
|
||||
pixelPlane(rho*north.x-world.z*north.y-north.z,vec3(north.x*radial,-north.y),dx,dy));
|
||||
if(coverage<=0.0) continue;
|
||||
vec4 fill=atSlot(tCells,uCellSize,id*4.0+3.0);
|
||||
color=mix(color,fill.rgb,fill.a*coverage);
|
||||
}
|
||||
outputColor=vec4(toLinear(color),inputColor.a);
|
||||
}
|
||||
`;
|
||||
function buildState(cells) {
|
||||
const frames = cells.map(rectangleFrame),
|
||||
packed = createGroundStorage(cells.length * 4);
|
||||
const grid = createGroundGrid(cells.length),
|
||||
gridData = new Float32Array(GRID_COLS * GRID_ROWS * 4);
|
||||
const gridTexture = createGroundTexture(gridData, GRID_COLS, GRID_ROWS);
|
||||
let candidates = createGroundStorage(1);
|
||||
const scratch = new Float64Array(12),
|
||||
bounds = new Float64Array(4);
|
||||
const lastView = new Float64Array(16).fill(NaN),
|
||||
lastProjection = new Float64Array(16).fill(NaN);
|
||||
let lastWidth = 0,
|
||||
lastHeight = 0;
|
||||
frames.forEach((frame, index) => {
|
||||
const offset = index * 16,
|
||||
color = cells[index].color;
|
||||
packed.data.set(frame.bounds.longitude, offset);
|
||||
packed.data.set(frame.bounds.south, offset + 4);
|
||||
packed.data.set(frame.bounds.north, offset + 8);
|
||||
packed.data.set(
|
||||
[color.red, color.green, color.blue, color.alpha],
|
||||
offset + 12,
|
||||
);
|
||||
});
|
||||
return {
|
||||
count: cells.length,
|
||||
packed,
|
||||
gridTexture,
|
||||
get candidates() {
|
||||
return candidates;
|
||||
},
|
||||
update(camera, context) {
|
||||
const input = context.getInputBuffer(),
|
||||
view = camera.matrixWorldInverse.elements,
|
||||
projection = camera.projectionMatrix.elements;
|
||||
let changed = input.width !== lastWidth || input.height !== lastHeight;
|
||||
for (let i = 0; i < 16 && !changed; i++)
|
||||
changed =
|
||||
view[i] !== lastView[i] || projection[i] !== lastProjection[i];
|
||||
if (!changed) return;
|
||||
lastView.set(view);
|
||||
lastProjection.set(projection);
|
||||
lastWidth = input.width;
|
||||
lastHeight = input.height;
|
||||
frames.forEach((frame, index) => {
|
||||
viewFrame(frame, camera.matrixWorldInverse, scratch);
|
||||
groundClipBounds(frame, scratch, camera.projectionMatrix, bounds);
|
||||
if (bounds[0] <= 1) {
|
||||
bounds[0] -= 2 / Math.max(1, input.width);
|
||||
bounds[2] += 2 / Math.max(1, input.width);
|
||||
bounds[1] -= 2 / Math.max(1, input.height);
|
||||
bounds[3] += 2 / Math.max(1, input.height);
|
||||
}
|
||||
grid.bounds.set(bounds, index * 4);
|
||||
});
|
||||
const total = grid.pack();
|
||||
if (candidates.data.length / 4 < total) {
|
||||
candidates.texture.dispose();
|
||||
candidates = createGroundStorage(
|
||||
Math.max(total, candidates.data.length / 2),
|
||||
);
|
||||
}
|
||||
for (let i = 0; i < total; i++) candidates.data[i * 4] = grid.indices[i];
|
||||
for (let i = 0; i < grid.counts.length; i++) {
|
||||
gridData[i * 4] = grid.offsets[i];
|
||||
gridData[i * 4 + 1] = grid.counts[i];
|
||||
}
|
||||
gridTexture.needsUpdate = true;
|
||||
candidates.texture.needsUpdate = true;
|
||||
},
|
||||
dispose() {
|
||||
packed.texture.dispose();
|
||||
gridTexture.dispose();
|
||||
candidates.texture.dispose();
|
||||
},
|
||||
};
|
||||
}
|
||||
function groundEffectClass(EffectDesc, NavaraEffect) {
|
||||
return class FirmsGroundEffectDesc extends EffectDesc {
|
||||
static key = KEY;
|
||||
static insertAfter = [
|
||||
'gevEarthquakeGround',
|
||||
'gevRecentImagery',
|
||||
'toneMapping',
|
||||
];
|
||||
static insertBefore = [
|
||||
'gevPreSharpenOverlay',
|
||||
'gevSharpen',
|
||||
'smaa',
|
||||
'fxaa',
|
||||
'gevOverlay',
|
||||
'final',
|
||||
];
|
||||
constructor(view, context, config) {
|
||||
super(view, context, config);
|
||||
this.owner = config[KEY].owner;
|
||||
}
|
||||
createPass() {
|
||||
this.effect = new Effect('GevFirmsGround', fragmentShader, {
|
||||
attributes: EffectAttribute.CONVOLUTION,
|
||||
uniforms: new Map([
|
||||
...groundSurfaceUniforms(),
|
||||
['tCells', new Uniform(null)],
|
||||
['tGrid', new Uniform(null)],
|
||||
['tCandidates', new Uniform(null)],
|
||||
['uCellSize', new Uniform([1, 1])],
|
||||
['uCandidateSize', new Uniform([1, 1])],
|
||||
['uInverseView', new Uniform(new Matrix4())],
|
||||
]),
|
||||
});
|
||||
return new NavaraEffect(this.view.camera.raw, this.effect);
|
||||
}
|
||||
update() {
|
||||
const state = this.owner.state,
|
||||
camera = this.view.camera.raw;
|
||||
if (!this.effect || !state || !camera) return;
|
||||
updateGroundSurface(this.effect, camera, this.ctx, this.find('mrt'));
|
||||
state.update(camera, this.ctx);
|
||||
const u = this.effect.uniforms;
|
||||
u.get('tCells').value = state.packed.texture;
|
||||
u.get('uCellSize').value[0] = state.packed.width;
|
||||
u.get('uCellSize').value[1] = state.packed.height;
|
||||
u.get('tGrid').value = state.gridTexture;
|
||||
u.get('tCandidates').value = state.candidates.texture;
|
||||
u.get('uCandidateSize').value[0] = state.candidates.width;
|
||||
u.get('uCandidateSize').value[1] = state.candidates.height;
|
||||
u.get('uInverseView').value.copy(camera.matrixWorld);
|
||||
}
|
||||
onDestroy() {
|
||||
this.effect?.dispose();
|
||||
super.onDestroy();
|
||||
}
|
||||
};
|
||||
}
|
||||
import { createGroundCellDisplay } from '../../shared/render/navaraGroundCells.js';
|
||||
export function createFirmsNavaraRenderer(
|
||||
engine,
|
||||
{ view: nativeView, EffectDesc, NavaraEffect, id = 'local-firms' } = {},
|
||||
) {
|
||||
if (!engine?.movers || !nativeView?.camera || !EffectDesc || !NavaraEffect)
|
||||
throw new TypeError('FIRMS requires a Navara view');
|
||||
if (!registeredViews.has(nativeView)) {
|
||||
nativeView.registerEffect(KEY, groundEffectClass(EffectDesc, NavaraEffect));
|
||||
registeredViews.add(nativeView);
|
||||
}
|
||||
const display = createGroundCellDisplay(
|
||||
engine,
|
||||
{ view: nativeView, EffectDesc, NavaraEffect },
|
||||
1,
|
||||
);
|
||||
let destroyed = false,
|
||||
visible = false,
|
||||
cells = [],
|
||||
queued = false,
|
||||
handle = null,
|
||||
sprites = null;
|
||||
const owner = { state: buildState([]) },
|
||||
dataSource = { show: false },
|
||||
const dataSource = { show: false },
|
||||
handlers = new Set();
|
||||
const adapter = createEngineLayerView(engine, {
|
||||
layerId: id,
|
||||
@@ -293,24 +28,13 @@ export function createFirmsNavaraRenderer(
|
||||
});
|
||||
adapter.view.camera.moveEnd = engine.camera.moveEnd;
|
||||
adapter.view.scene.preRender = engine.frames.preRender;
|
||||
const sync = () => {
|
||||
const show = visible && owner.state.count > 0;
|
||||
if (show && !handle)
|
||||
handle = nativeView.addEffect({ [KEY]: { owner }, visible: true });
|
||||
else handle?.update({ visible: show });
|
||||
};
|
||||
const scheduleCells = () => {
|
||||
if (queued || destroyed) return;
|
||||
queued = true;
|
||||
queueMicrotask(() => {
|
||||
queued = false;
|
||||
if (destroyed) return;
|
||||
const next = buildState(cells),
|
||||
previous = owner.state;
|
||||
owner.state = next;
|
||||
sync();
|
||||
previous.dispose();
|
||||
if (visible) engine.render.request('firms-cells');
|
||||
display.replace(cells);
|
||||
});
|
||||
};
|
||||
const removeSprites = () => {
|
||||
@@ -366,7 +90,7 @@ export function createFirmsNavaraRenderer(
|
||||
visible = next;
|
||||
dataSource.show = next;
|
||||
sprites?.setVisible(next);
|
||||
sync();
|
||||
display.setVisible(visible);
|
||||
engine.render.request('firms-visibility');
|
||||
},
|
||||
clearCells() {
|
||||
@@ -406,8 +130,7 @@ export function createFirmsNavaraRenderer(
|
||||
cells = [];
|
||||
for (const handler of [...handlers]) handler.destroy();
|
||||
removeSprites();
|
||||
handle?.delete();
|
||||
owner.state.dispose();
|
||||
display.destroy();
|
||||
adapter.destroy();
|
||||
engine.render.request('firms-destroy');
|
||||
},
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { createGroundCellDisplay } from '../../shared/render/navaraGroundCells.js';
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createEngineEvent } from '../../../globe/contract.js';
|
||||
@@ -130,6 +131,32 @@ test('ground changes are atomic, lazy and dispose replaced and destroyed GPU tex
|
||||
second.destroy();
|
||||
assert.equal(h.registered.length, 1);
|
||||
});
|
||||
test('GNSS and FIRMS share one ground pass without hiding or destroying one another', async () => {
|
||||
const h = harness();
|
||||
const gnss = createGroundCellDisplay(h.engine, h.options, 0);
|
||||
const fire = h.renderer;
|
||||
try {
|
||||
fire.setVisible(true);
|
||||
fire.addCell(cell);
|
||||
await settle();
|
||||
gnss.replace([cell, cell]);
|
||||
gnss.setVisible(true);
|
||||
const owner = h.effects[0].config.gevFirmsGround.owner;
|
||||
assert.equal(h.effects.length, 1);
|
||||
assert.equal(owner.state.count, 3);
|
||||
gnss.setVisible(false);
|
||||
assert.equal(owner.state.count, 1);
|
||||
gnss.setVisible(true);
|
||||
fire.destroy();
|
||||
assert.equal(owner.state.count, 2);
|
||||
assert.equal(h.effects[0].deleted, 0);
|
||||
} finally {
|
||||
fire.destroy();
|
||||
gnss.destroy();
|
||||
}
|
||||
assert.equal(h.effects[0].deleted, 1);
|
||||
});
|
||||
|
||||
test('FIRMS selection uses actual engine event picks and releases its dispatcher subscriptions', () => {
|
||||
const h = harness(),
|
||||
r = h.renderer;
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
// Exact geodetic rectangle sides, including latitude parallels at any height.
|
||||
import * as geo from '../../../geo/index.js';
|
||||
const ellipsoid = geo.ellipsoid.WGS84;
|
||||
const A = ellipsoid.radii.x,
|
||||
B = ellipsoid.radii.z;
|
||||
const E2 = 1 - (B * B) / (A * A);
|
||||
export function latitudeBoundary(degrees) {
|
||||
const angle = geo.math.toRadians(degrees),
|
||||
sin = Math.sin(angle),
|
||||
cos = Math.cos(angle);
|
||||
return [sin, cos, ((E2 * A) / Math.sqrt(1 - E2 * sin * sin)) * sin * cos, 0];
|
||||
}
|
||||
export function rectangleBoundaries({ west, south, east, north }) {
|
||||
const w = geo.math.toRadians(west),
|
||||
e = geo.math.toRadians(east);
|
||||
return {
|
||||
longitude: [Math.sin(w), Math.cos(w), Math.sin(e), Math.cos(e)],
|
||||
south: latitudeBoundary(south),
|
||||
north: latitudeBoundary(north),
|
||||
};
|
||||
}
|
||||
export function rectangleDistances(bounds, point) {
|
||||
const { x, y, z } = point,
|
||||
rho = Math.hypot(x, y),
|
||||
l = bounds.longitude;
|
||||
return [
|
||||
y * l[1] - x * l[0],
|
||||
x * l[2] - y * l[3],
|
||||
z * bounds.south[1] - rho * bounds.south[0] + bounds.south[2],
|
||||
rho * bounds.north[0] - z * bounds.north[1] - bounds.north[2],
|
||||
];
|
||||
}
|
||||
export function rectangleFrame(cell) {
|
||||
let eastBound = cell.east;
|
||||
if (eastBound < cell.west) eastBound += 360;
|
||||
const lon = (cell.west + eastBound) / 2,
|
||||
lat = (cell.south + cell.north) / 2;
|
||||
const center = geo.cartesian3.fromDegrees(lon, lat);
|
||||
const up = ellipsoid.geodeticSurfaceNormal(center, geo.cartesian3.create());
|
||||
const angle = geo.math.toRadians(lon);
|
||||
const east = geo.cartesian3.create(-Math.sin(angle), Math.cos(angle), 0);
|
||||
const north = geo.cartesian3.cross(up, east, geo.cartesian3.create());
|
||||
let radius = 0;
|
||||
for (const latitude of [cell.south, lat, cell.north])
|
||||
for (const longitude of [cell.west, lon, eastBound]) {
|
||||
const p = geo.cartesian3.fromDegrees(longitude, latitude);
|
||||
const dx = p.x - center.x,
|
||||
dy = p.y - center.y,
|
||||
dz = p.z - center.z;
|
||||
radius = Math.max(
|
||||
radius,
|
||||
Math.hypot(
|
||||
dx * east.x + dy * east.y + dz * east.z,
|
||||
dx * north.x + dy * north.y + dz * north.z,
|
||||
),
|
||||
);
|
||||
}
|
||||
radius = radius * (1 + 101000 / ellipsoid.minimumRadius) + 1;
|
||||
return {
|
||||
center,
|
||||
east,
|
||||
north,
|
||||
up,
|
||||
radius,
|
||||
floor: -100000 - ((radius * radius) / (2 * ellipsoid.minimumRadius)) * 1.1,
|
||||
ceiling: 12000,
|
||||
bounds: rectangleBoundaries(cell),
|
||||
};
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
rectangleBoundaries,
|
||||
rectangleDistances,
|
||||
rectangleFrame,
|
||||
} from './navaraGeometry.js';
|
||||
} from '../../shared/render/navaraCellGeometry.js';
|
||||
const cases = [];
|
||||
for (const lat of [-90, -80, -45, 0, 45, 80, 88])
|
||||
for (const west of [-180, -123, 0, 178])
|
||||
|
||||
Reference in new issue
Block a user