mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 08:42:21 +00:00
In a dense downtown view from about 400 m up, the occlusion margin of max(3 m, 2% of the range) came to 14 to 20 m, so a wall, awning, tree or lower facade standing 10 to 20 m in front of a street was read as the road itself: the vehicle's dot was hidden by the scene, but its bracket and callout were still drawn. Checked against scene.pickPosition in one such view, 18 of 470 vehicles in frame fell in that band. The margin now depends on how steeply the camera looks down on the contact. A point on the line of sight s metres in front of a vehicle stands s * sin(elevation) above it, so a surface there hides the vehicle once that rise passes 2 m. The margin stays at least 3 m and at most 2% of the range, so grazing and street-level views are unchanged. In the same views no vehicle with the drawn surface within 3 m in front of it (its own road) is newly hidden. From the review of the first change: - A contact back in view after longer than a retest interval is held back and retested like a new one, instead of being drawn from an answer given before it left the view. It gets a fresh record, so a test still in flight from before cannot end the hold-back either. - The hold-back for a contact's first answer stretches with the retest pace when a crowded view slows it. - A batch whose control point failed no longer counts as a first answer. - An unknown log-depth flag reads as "no depth" rather than as standard depth, which would have put every surface in front of every contact. - A shader that fails to build leaves no shader behind and is not built again on the same context.
352 lines
11 KiB
JavaScript
352 lines
11 KiB
JavaScript
/**
|
|
* A stand-in scene and WebGL context for tests of the occlusion probe: just
|
|
* the members `sceneOcclusionProbe.js` uses, with depth images the test
|
|
* controls.
|
|
*
|
|
* The fake context cannot run GLSL. When the probe draws a point inside a
|
|
* query, the fake decides whether the point draws by applying the same rule
|
|
* to the same inputs the vertex shader gets (the uploaded attributes, the
|
|
* bound depth textures and the uniforms), using the CPU decode from
|
|
* `detectionOcclusion.js`. That checks everything around the shader; the
|
|
* shader itself is checked against `scene.pickPosition` in a real browser.
|
|
*/
|
|
import * as Cesium from 'cesium';
|
|
import { depthToViewDistance, unpackDepth } from '../detectionOcclusion.js';
|
|
|
|
/** Inverse of `unpackDepth`: four base-255 digits, most significant first. */
|
|
export function packDepth(depth) {
|
|
const bytes = [];
|
|
let rest = Math.min(Math.max(depth, 0), 1);
|
|
for (let i = 0; i < 4; i++) {
|
|
rest *= 255;
|
|
const digit = Math.min(255, Math.floor(rest));
|
|
bytes.push(digit);
|
|
rest -= digit;
|
|
}
|
|
return bytes;
|
|
}
|
|
|
|
/** Inverse of `depthToViewDistance`. */
|
|
export function viewDistanceToDepth(distance, near, far, logDepth) {
|
|
if (logDepth)
|
|
return Math.log2(distance - near + 1) / Math.log2(far - near + 1);
|
|
return (far * (distance - near)) / (distance * (far - near));
|
|
}
|
|
|
|
/**
|
|
* An RGBA8 depth image filled from a function of the texel.
|
|
* @param {number} width
|
|
* @param {number} height
|
|
* @param {(column: number, row: number) => number} depthAt Window depth; rows
|
|
* count from the bottom.
|
|
*/
|
|
export function depthImage(width, height, depthAt) {
|
|
const data = new Uint8Array(width * height * 4);
|
|
for (let row = 0; row < height; row++)
|
|
for (let column = 0; column < width; column++)
|
|
data.set(packDepth(depthAt(column, row)), (row * width + column) * 4);
|
|
return { width, height, data };
|
|
}
|
|
|
|
const GL = {
|
|
FRAMEBUFFER: 0x8d40,
|
|
READ_FRAMEBUFFER: 0x8ca8,
|
|
DRAW_FRAMEBUFFER: 0x8ca9,
|
|
FRAMEBUFFER_BINDING: 0x8ca6,
|
|
DRAW_FRAMEBUFFER_BINDING: 0x8ca6,
|
|
READ_FRAMEBUFFER_BINDING: 0x8caa,
|
|
TEXTURE_BINDING_2D: 0x8069,
|
|
TEXTURE_2D: 0x0de1,
|
|
TEXTURE0: 0x84c0,
|
|
ACTIVE_TEXTURE: 0x84e0,
|
|
COLOR_ATTACHMENT0: 0x8ce0,
|
|
FRAMEBUFFER_COMPLETE: 0x8cd5,
|
|
ARRAY_BUFFER: 0x8892,
|
|
ARRAY_BUFFER_BINDING: 0x8894,
|
|
VERTEX_ARRAY_BINDING: 0x85b5,
|
|
CURRENT_PROGRAM: 0x8b8d,
|
|
STREAM_DRAW: 0x88e0,
|
|
SCISSOR_TEST: 0x0c11,
|
|
VIEWPORT: 0x0ba2,
|
|
POINTS: 0x0000,
|
|
FLOAT: 0x1406,
|
|
RGBA: 0x1908,
|
|
RGBA8: 0x8058,
|
|
UNSIGNED_BYTE: 0x1401,
|
|
VERTEX_SHADER: 0x8b31,
|
|
FRAGMENT_SHADER: 0x8b30,
|
|
COMPILE_STATUS: 0x8b81,
|
|
LINK_STATUS: 0x8b82,
|
|
ANY_SAMPLES_PASSED_CONSERVATIVE: 0x8d6a,
|
|
QUERY_RESULT: 0x8866,
|
|
QUERY_RESULT_AVAILABLE: 0x8867,
|
|
};
|
|
|
|
/**
|
|
* A recording WebGL context. `webgl2: false` leaves out queries, vertex
|
|
* arrays and immutable textures, as a WebGL 1 context does. Query results
|
|
* stay unavailable until `finishGpu()`.
|
|
*/
|
|
export function createFakeGl({ webgl2 = true } = {}) {
|
|
const state = {
|
|
read: null,
|
|
draw: null,
|
|
units: [null, null, null, null, null, null, null, null],
|
|
unit: 0,
|
|
array: null,
|
|
vertexArray: null,
|
|
program: null,
|
|
scissor: false,
|
|
viewport: [0, 0, 1920, 1080],
|
|
lost: false,
|
|
/** Whether a point that should draw does (false: a broken draw path). */
|
|
drawsLand: true,
|
|
/** Whether shaders compile: a boolean, or a function of the shader type. */
|
|
compiles: true,
|
|
};
|
|
const counts = {
|
|
readPixels: 0,
|
|
queries: 0,
|
|
draws: 0,
|
|
resultReads: 0,
|
|
deleted: 0,
|
|
};
|
|
const liveQueries = new Set();
|
|
const liveShaders = new Set();
|
|
let activeQuery = null;
|
|
const gl = {
|
|
...GL,
|
|
state,
|
|
counts,
|
|
liveQueries,
|
|
liveShaders,
|
|
isContextLost: () => state.lost,
|
|
getParameter(name) {
|
|
if (name === GL.READ_FRAMEBUFFER_BINDING) return state.read;
|
|
if (name === GL.DRAW_FRAMEBUFFER_BINDING) return state.draw;
|
|
if (name === GL.TEXTURE_BINDING_2D) return state.units[state.unit];
|
|
if (name === GL.ACTIVE_TEXTURE) return GL.TEXTURE0 + state.unit;
|
|
if (name === GL.ARRAY_BUFFER_BINDING) return state.array;
|
|
if (name === GL.VERTEX_ARRAY_BINDING) return state.vertexArray;
|
|
if (name === GL.CURRENT_PROGRAM) return state.program;
|
|
if (name === GL.VIEWPORT) return Int32Array.from(state.viewport);
|
|
return null;
|
|
},
|
|
isEnabled: (cap) => cap === GL.SCISSOR_TEST && state.scissor,
|
|
enable(cap) {
|
|
if (cap === GL.SCISSOR_TEST) state.scissor = true;
|
|
},
|
|
disable(cap) {
|
|
if (cap === GL.SCISSOR_TEST) state.scissor = false;
|
|
},
|
|
createFramebuffer: () => ({ image: null }),
|
|
createTexture: () => ({ image: null }),
|
|
createBuffer: () => ({ data: null }),
|
|
deleteFramebuffer() {
|
|
counts.deleted++;
|
|
},
|
|
deleteTexture() {
|
|
counts.deleted++;
|
|
},
|
|
deleteBuffer() {
|
|
counts.deleted++;
|
|
},
|
|
activeTexture(unit) {
|
|
state.unit = unit - GL.TEXTURE0;
|
|
},
|
|
bindTexture(_target, texture) {
|
|
state.units[state.unit] = texture;
|
|
},
|
|
bindFramebuffer(target, framebuffer) {
|
|
if (target !== GL.DRAW_FRAMEBUFFER) state.read = framebuffer;
|
|
if (target !== GL.READ_FRAMEBUFFER) state.draw = framebuffer;
|
|
},
|
|
bindBuffer(_target, buffer) {
|
|
state.array = buffer;
|
|
},
|
|
readPixels(x, y, width, height, _format, _type, destination) {
|
|
counts.readPixels++;
|
|
const image = state.read.image;
|
|
for (let row = 0; row < height; row++) {
|
|
const from = ((y + row) * image.width + x) * 4;
|
|
destination.set(
|
|
image.data.subarray(from, from + width * 4),
|
|
row * width * 4,
|
|
);
|
|
}
|
|
},
|
|
};
|
|
if (webgl2) {
|
|
Object.assign(gl, {
|
|
texStorage2D(_target, _levels, _format, width, height) {
|
|
state.units[state.unit].image = {
|
|
width,
|
|
height,
|
|
data: new Uint8Array(width * height * 4),
|
|
};
|
|
},
|
|
framebufferTexture2D(_target, _attachment, _kind, texture) {
|
|
state.draw.image = texture.image;
|
|
},
|
|
checkFramebufferStatus: () => GL.FRAMEBUFFER_COMPLETE,
|
|
createShader(type) {
|
|
const shader = { type };
|
|
liveShaders.add(shader);
|
|
return shader;
|
|
},
|
|
shaderSource() {},
|
|
compileShader() {},
|
|
getShaderParameter: (shader) =>
|
|
typeof state.compiles === 'function'
|
|
? state.compiles(shader.type)
|
|
: state.compiles,
|
|
deleteShader(shader) {
|
|
liveShaders.delete(shader);
|
|
},
|
|
createProgram: () => ({ uniforms: {} }),
|
|
attachShader() {},
|
|
linkProgram() {},
|
|
getProgramParameter: () => true,
|
|
deleteProgram() {
|
|
counts.deleted++;
|
|
},
|
|
getUniformLocation: (_program, name) => name,
|
|
useProgram(program) {
|
|
state.program = program;
|
|
},
|
|
viewport(x, y, width, height) {
|
|
state.viewport = [x, y, width, height];
|
|
},
|
|
uniform1i(name, value) {
|
|
state.program.uniforms[name] = value;
|
|
},
|
|
uniform4f(name, x, y, z, w) {
|
|
state.program.uniforms[name] = [x, y, z, w];
|
|
},
|
|
createVertexArray: () => ({ buffer: null }),
|
|
deleteVertexArray() {
|
|
counts.deleted++;
|
|
},
|
|
bindVertexArray(vertexArray) {
|
|
state.vertexArray = vertexArray;
|
|
},
|
|
enableVertexAttribArray() {},
|
|
vertexAttribPointer() {
|
|
state.vertexArray.buffer = state.array;
|
|
},
|
|
bufferData(_target, data) {
|
|
state.array.data = Float32Array.from(data);
|
|
},
|
|
createQuery() {
|
|
const query = { drew: false, available: false, ended: false };
|
|
liveQueries.add(query);
|
|
return query;
|
|
},
|
|
deleteQuery(query) {
|
|
liveQueries.delete(query);
|
|
},
|
|
beginQuery(_target, query) {
|
|
counts.queries++;
|
|
query.drew = false;
|
|
query.available = false;
|
|
query.ended = false;
|
|
activeQuery = query;
|
|
},
|
|
endQuery() {
|
|
activeQuery.ended = true;
|
|
activeQuery = null;
|
|
},
|
|
/** The vertex shader's rule, applied to what the shader would be given. */
|
|
drawArrays(_mode, first) {
|
|
counts.draws++;
|
|
const uniforms = state.program.uniforms;
|
|
const data = state.vertexArray.buffer.data;
|
|
const u = data[first * 3];
|
|
const v = data[first * 3 + 1];
|
|
const threshold = data[first * 3 + 2];
|
|
let surface = -1;
|
|
for (let i = 0; i < uniforms.u_count && surface < 0; i++) {
|
|
const image = state.units[uniforms[`u_depth${i}`]].image;
|
|
const column = Math.min(image.width - 1, Math.floor(u * image.width));
|
|
const row = Math.min(image.height - 1, Math.floor(v * image.height));
|
|
const distance = depthToViewDistance(
|
|
unpackDepth(image.data, (row * image.width + column) * 4),
|
|
uniforms.u_near[i],
|
|
uniforms.u_far[i],
|
|
uniforms.u_logDepth === 1,
|
|
);
|
|
if (distance === distance) surface = distance;
|
|
}
|
|
const hidden = surface > 0 && surface < threshold;
|
|
// A point at the middle of the viewport lands only if that is the
|
|
// target's one pixel and the scissor test is not in the way. A
|
|
// sampler left on a unit with no texture fails the whole draw.
|
|
const samplersBound = [0, 1, 2, 3].every(
|
|
(i) => state.units[uniforms[`u_depth${i}`]]?.image,
|
|
);
|
|
const lands =
|
|
state.drawsLand &&
|
|
samplersBound &&
|
|
!state.scissor &&
|
|
state.draw?.image?.width === 1 &&
|
|
state.viewport.join() === '0,0,1,1';
|
|
if (activeQuery && !hidden && lands) activeQuery.drew = true;
|
|
},
|
|
getQueryParameter(query, name) {
|
|
if (name === GL.QUERY_RESULT_AVAILABLE) return query.available;
|
|
counts.resultReads++;
|
|
return query.available && query.drew ? 1 : 0;
|
|
},
|
|
});
|
|
}
|
|
/** Let every query issued so far finish. */
|
|
gl.finishGpu = () => {
|
|
for (const query of liveQueries) if (query.ended) query.available = true;
|
|
};
|
|
return gl;
|
|
}
|
|
|
|
/**
|
|
* A scene drawn in the given depth ranges, nearest first.
|
|
* @param {{
|
|
* gl?: object,
|
|
* ranges: Array<{near: number, far: number, image: object}>,
|
|
* logDepth?: boolean, fovy?: number, aspectRatio?: number,
|
|
* viewMatrix?: Cesium.Matrix4,
|
|
* }} options
|
|
*/
|
|
export function createFakeDepthScene({
|
|
gl = createFakeGl(),
|
|
ranges,
|
|
logDepth = true,
|
|
fovy = Math.PI / 2,
|
|
aspectRatio = 1,
|
|
viewMatrix = Cesium.Matrix4.clone(Cesium.Matrix4.IDENTITY),
|
|
}) {
|
|
return {
|
|
mode: Cesium.SceneMode.SCENE3D,
|
|
useDepthPicking: true,
|
|
opaqueFrustumNearOffset: 0.9999,
|
|
context: { _gl: gl, depthTexture: true, webgl2: !!gl.createQuery },
|
|
_environmentState: { useGlobeDepthFramebuffer: true },
|
|
frameState: { useLogDepth: logDepth },
|
|
camera: { frustum: { fovy, aspectRatio }, viewMatrix },
|
|
view: {
|
|
frustumCommandsList: ranges.map(({ near, far }) => ({ near, far })),
|
|
pickDepths: ranges.map(({ image }) => {
|
|
const texture = { image };
|
|
return {
|
|
framebuffer: {
|
|
_framebuffer: { image },
|
|
getColorTexture: () => ({
|
|
width: image.width,
|
|
height: image.height,
|
|
_texture: texture,
|
|
}),
|
|
},
|
|
};
|
|
}),
|
|
},
|
|
};
|
|
}
|