Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Count true visibility flips in the flicker probe with an exact ground truth (#751)

Nguyen Xuan An5dc49629bd38
diff --git a/bench/flicker/ground-truth.js b/bench/flicker/ground-truth.js
index d4850c8c..bcab9e63 100644
--- a/bench/flicker/ground-truth.js
+++ b/bench/flicker/ground-truth.js
@@ -14,7 +14,8 @@ import { castPixelOwners, crossProduct, normalized, subtract, NO_TRIANGLE, RIVAL
export { castPixelOwners, NO_TRIANGLE };
const NORMAL_KEY_SCALE = 100;
-const COLOUR_KEY_SCALE = 255;
+// Fine enough that triangle ids painted as 24-bit colours stay apart as keys, however a frame encodes them.
+const COLOUR_KEY_SCALE = 1e7;
export const DEFAULT_COLOUR_THRESHOLD = 40;
const MINIMUM_REFERENCE_PIXELS = 3;
@@ -51,6 +52,7 @@ function colourKey(part, geometry, firstVertex, vertexCount, facing) {
export function collectWorldTriangles(parts, flushOrderOf) {
const cornerChunks = [];
const keyIds = new Map();
+ const keyColours = [];
const keyOf = [];
const rankList = [];
const doubleSidedList = [];
@@ -70,7 +72,10 @@ export function collectWorldTriangles(parts, flushOrderOf) {
const edgeOne = subtract([corners[base + 3], corners[base + 4], corners[base + 5]], first);
const edgeTwo = subtract([corners[base + 6], corners[base + 7], corners[base + 8]], first);
const key = colourKey(part, geometry, indices[triangle * 3], vertexCount, normalized(crossProduct(edgeOne, edgeTwo)));
- if (!keyIds.has(key)) keyIds.set(key, keyIds.size);
+ if (!keyIds.has(key)) {
+ keyIds.set(key, keyIds.size);
+ keyColours.push(key.split('|')[0].split(',').map(Number));
+ }
keyOf.push(keyIds.get(key));
rankList.push(depthLayerOf(part) * RANKS_PER_LAYER + (ranks ? ranks[triangle] : 0));
doubleSidedList.push(part.material?.doubleSided === true ? 1 : 0);
@@ -84,7 +89,7 @@ export function collectWorldTriangles(parts, flushOrderOf) {
corners.set(chunk, offset);
offset += chunk.length;
}
- return { corners, keyOf: Int32Array.from(keyOf), ranks: Uint8Array.from(rankList), doubleSided: Uint8Array.from(doubleSidedList), keyCount: keyIds.size, count: keyOf.length };
+ return { corners, keyOf: Int32Array.from(keyOf), ranks: Uint8Array.from(rankList), doubleSided: Uint8Array.from(doubleSidedList), keyCount: keyIds.size, keyColours, count: keyOf.length };
}
function channelDistance(pixels, index, reference) {
@@ -109,6 +114,37 @@ function hasUniformSurroundings(keyAt, width, height, x, y) {
return true;
}
+// The rank of the nearest face of colour key `key` on the ray of `pixel`, which is the face a swapped pixel shows.
+function shownRank(world, cast, pixel, key) {
+ for (let slot = 0; slot < cast.hitsPerPixel; slot++) {
+ const triangle = cast.hitTriangles[pixel * cast.hitsPerPixel + slot];
+ if (triangle === NO_TRIANGLE) break;
+ if (world.keyOf[triangle] === key) return world.ranks[triangle];
+ }
+ return -1;
+}
+
+// Says what a wrong pixel of an exact frame shows. The colour names a face: when that face lies on the ray (one of its six nearest
+// hits) the pixel is swapped, and it is flipped when the face has no higher rank than the one the ray cast puts in front. A face
+// that is not on the ray is the rasteriser and the ray cast disagreeing about a pixel centre (offRay), and a colour that names no
+// face is a blend of two (blended). Neither is depth fighting.
+function classifyExactMismatch(result, mismatchMask, { pixels, pixel, world, cast, keyByColour }) {
+ const shownKey = keyByColour.get(`${pixels[pixel * 4]},${pixels[pixel * 4 + 1]},${pixels[pixel * 4 + 2]}`);
+ if (shownKey === undefined) {
+ result.blended++;
+ return;
+ }
+ const rank = shownRank(world, cast, pixel, shownKey);
+ if (rank < 0) {
+ result.offRay++;
+ return;
+ }
+ result.swapped++;
+ mismatchMask[pixel] = 2;
+ if (rank > world.ranks[cast.owner[pixel]]) result.ranked++;
+ else result.flipped++;
+}
+
/**
* Compares a rendered frame (RGBA bytes, rows from the top) with the ray cast of `castPixelOwners`.
* Returns `{ contested, wrong, swapped, tied, unjudged, edge }`:
@@ -117,15 +153,24 @@ function hasUniformSurroundings(keyAt, width, height, x, y) {
* - `swapped`: wrong pixels that sit within `threshold` of the colour of a face behind, a fight and not shading;
* - `tied`: contested pixels where the scene itself does not say which face is in front (the flush pass off, or a seam); left out of the other counts;
* - `unjudged`: contested pixels whose face has too few clean pixels to know its colour;
+ * - `offRay` and `blended`: with `exact`, wrong pixels that show a face not on the ray, or no single face (see classifyExactMismatch);
+ * - `flipped`: with `exact`, the swapped pixels where the face shown has no higher rank than the face the ray cast puts in front, so
+ * a face behind covers one in front and nothing in the scene asked for it: a true visibility flip. The rest of `swapped` is
+ * `ranked`, a face that the flush pass deliberately lifts over the nearer one inside its bias, which is stable and not flicker;
* - `edge`: pixels left out for having another colour next to them.
+ * With `exact`, a function from the colour of a face (0 to 1 per channel) to the bytes the frame shows for it, the frame holds an
+ * unshaded colour per face (an unlit scene painted with one colour per triangle): the colour a face must show is that, not a median
+ * of its pixels, and `threshold` is 0, so a swapped pixel is a true visibility
+ * flip (the raster shows another face's id where the ray cast says this face is in front) and no shading step can pass for one.
* `mismatchMask` marks the wrong pixels for a picture: 1 for a wrong pixel, 2 for a swapped one.
*/
-export function compareWithGroundTruth({ pixels, world, cast, width, height, threshold = DEFAULT_COLOUR_THRESHOLD }) {
+export function compareWithGroundTruth({ pixels, world, cast, width, height, exact = null, threshold = exact ? 0 : DEFAULT_COLOUR_THRESHOLD }) {
const keyAt = new Int32Array(width * height);
for (let pixel = 0; pixel < keyAt.length; pixel++) keyAt[pixel] = cast.owner[pixel] === NO_TRIANGLE ? -1 : world.keyOf[cast.owner[pixel]];
const references = new Array(world.keyCount);
- const samples = Array.from({ length: world.keyCount }, () => ({ clean: [[], [], []], any: [[], [], []], cleanCount: 0 }));
- for (let y = 1; y < height - 1; y++) {
+ const samples = Array.from({ length: exact ? 0 : world.keyCount }, () => ({ clean: [[], [], []], any: [[], [], []], cleanCount: 0 }));
+ if (exact) world.keyColours.forEach((colour, key) => { references[key] = exact(colour.map(channel => channel / COLOUR_KEY_SCALE)); });
+ for (let y = 1; y < height - 1 && !exact; y++) {
for (let x = 1; x < width - 1; x++) {
if (!hasUniformSurroundings(keyAt, width, height, x, y)) continue;
const pixel = y * width + x;
@@ -142,7 +187,9 @@ export function compareWithGroundTruth({ pixels, world, cast, width, height, thr
const source = bucket.cleanCount >= MINIMUM_REFERENCE_PIXELS ? bucket.clean : bucket.any;
if (source[0].length >= MINIMUM_REFERENCE_PIXELS) references[key] = source.map(median);
});
- const result = { contested: 0, wrong: 0, swapped: 0, tied: 0, unjudged: 0, edge: 0 };
+ const result = { contested: 0, wrong: 0, swapped: 0, tied: 0, unjudged: 0, edge: 0, flipped: 0, ranked: 0, offRay: 0, blended: 0 };
+ const keyByColour = new Map();
+ references.forEach((reference, key) => { if (exact) keyByColour.set(reference.join(','), key); });
const mismatchMask = new Uint8Array(width * height);
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
@@ -165,6 +212,10 @@ export function compareWithGroundTruth({ pixels, world, cast, width, height, thr
if (channelDistance(pixels, pixel * 4, reference) <= threshold) continue;
result.wrong++;
mismatchMask[pixel] = 1;
+ if (exact) {
+ classifyExactMismatch(result, mismatchMask, { pixels, pixel, world, cast, keyByColour });
+ continue;
+ }
for (let slot = 0; slot < RIVALS_PER_PIXEL; slot++) {
const rival = references[cast.rivals[pixel * RIVALS_PER_PIXEL + slot]];
if (rival && channelDistance(pixels, pixel * 4, rival) <= threshold) {
diff --git a/bench/flicker/pixel-cast.js b/bench/flicker/pixel-cast.js
index 173c3f09..70e8dd4e 100644
--- a/bench/flicker/pixel-cast.js
+++ b/bench/flicker/pixel-cast.js
@@ -165,5 +165,5 @@ export function castPixelOwners(world, camera, width, height) {
if (!known) rivals[pixel * RIVALS_PER_PIXEL + stored++] = key;
}
}
- return { owner, rivals, tied, distance };
+ return { owner, rivals, tied, distance, hitTriangles, hitDistances, hitsPerPixel: HITS_PER_PIXEL };
}
diff --git a/bench/flicker/probe.mjs b/bench/flicker/probe.mjs
index a6f2c4de..86b5968b 100644
--- a/bench/flicker/probe.mjs
+++ b/bench/flicker/probe.mjs
@@ -8,6 +8,11 @@
// cannot z-fight. `swapped` is the count that matters (a pixel showing the colour of a face behind the visible one), `tied` the
// pixels where the scene itself names no winner (exact coplanar faces with the flush pass off, `offset=0`), and `wrong` also holds
// shading steps within a face. A glTF needs `ids=1` (flat colour per part) or `facets=1` (per triangle) for the truth to read colours.
+// `facets=2` is the exact form: every triangle is drawn unlit in a colour that is its own number, so the frame names the face shown at
+// each pixel and no shading can pass for a swap. Then `flipped` is the count of true visibility flips (a face behind covers one in
+// front that is not ranked below it), `ranked` the swaps the flush pass asked for, `offRay` pixels showing a face the ray through the
+// pixel centre misses (the rasteriser and the ray cast disagree on an edge) and `blended` pixels no face explains.
+// --timeout <ms> sets the wait for a page step (default 240000).
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import { chromium } from 'playwright';
@@ -32,7 +37,7 @@ const browser = await chromium.launch({
args: ['--use-angle=d3d11', '--enable-gpu', '--ignore-gpu-blocklist', '--enable-webgl', '--disable-renderer-backgrounding'],
});
const page = await browser.newPage({ viewport: { width, height }, deviceScaleFactor: 1 });
-page.setDefaultTimeout(240000);
+page.setDefaultTimeout(Number(argument("timeout", "240000")));
page.on('pageerror', error => console.error('pageerror', error.message));
page.on('console', message => { if (message.type() === 'error') console.error('console', message.text()); });
const results = [];
diff --git a/bench/flicker/results/README.md b/bench/flicker/results/README.md
index eb8eadf4..85bcb036 100644
--- a/bench/flicker/results/README.md
+++ b/bench/flicker/results/README.md
@@ -4,3 +4,10 @@
configs `reversed=1;reversed=0;reversed=1,offset=0;reversed=0,offset=0` (reversed-Z float depth and 24-bit depth, flush pass on and
off). It needs a GPU browser, so it is a local run, not part of CI; rerun it after a change to the flush pass or the depth path.
`--assert` exits 1 if any trap shows a swapped pixel with the flush pass on.
+
+`melbourne-drape-1280x720.json` is the exact ground truth (`facets=2`, see `probe.mjs`) of the Melbourne drape GLB at 12 camera
+spots (roofs, bridge, river, overview and 8 glass spots), with the flush pass on and off (`offset=0`), reversed-Z float depth, shadows off.
+Summed over the 12 spots: 8 true visibility flips (`flipped`) with the flush pass on against 109,522 with it off. The 8 are single
+pixels on a triangle edge, where the ray cast and the rasteriser disagree about the pixel centre, not coplanar fights. `offRay` (pixels
+showing a face the pixel-centre ray misses) is the rasteriser and the ray cast differing at edges and is not depth fighting. A render
+pass (albedo, object ID) cannot stand in for this: its target has a 24-bit depth buffer, which fights far more than the beauty path.
diff --git a/bench/flicker/results/melbourne-drape-1280x720.json b/bench/flicker/results/melbourne-drape-1280x720.json
new file mode 100644
index 00000000..8d88b988
--- /dev/null
+++ b/bench/flicker/results/melbourne-drape-1280x720.json
@@ -0,0 +1,290 @@
+[
+ {
+ "spot": "roofs",
+ "flushPass": true,
+ "swapped": 0,
+ "flipped": 0,
+ "ranked": 0,
+ "tied": 0,
+ "offRay": 1052,
+ "blended": 1,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "bridge",
+ "flushPass": true,
+ "swapped": 0,
+ "flipped": 0,
+ "ranked": 0,
+ "tied": 0,
+ "offRay": 502,
+ "blended": 0,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "river",
+ "flushPass": true,
+ "swapped": 0,
+ "flipped": 0,
+ "ranked": 0,
+ "tied": 0,
+ "offRay": 1593,
+ "blended": 1,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "overview",
+ "flushPass": true,
+ "swapped": 0,
+ "flipped": 0,
+ "ranked": 0,
+ "tied": 0,
+ "offRay": 568,
+ "blended": 0,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "glass0",
+ "flushPass": true,
+ "swapped": 0,
+ "flipped": 0,
+ "ranked": 0,
+ "tied": 0,
+ "offRay": 2441,
+ "blended": 0,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "glass1",
+ "flushPass": true,
+ "swapped": 0,
+ "flipped": 0,
+ "ranked": 0,
+ "tied": 16,
+ "offRay": 2233,
+ "blended": 1,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "glass2",
+ "flushPass": true,
+ "swapped": 0,
+ "flipped": 0,
+ "ranked": 0,
+ "tied": 0,
+ "offRay": 1982,
+ "blended": 0,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "glass3",
+ "flushPass": true,
+ "swapped": 0,
+ "flipped": 0,
+ "ranked": 0,
+ "tied": 0,
+ "offRay": 1039,
+ "blended": 0,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "glass4",
+ "flushPass": true,
+ "swapped": 1,
+ "flipped": 1,
+ "ranked": 0,
+ "tied": 0,
+ "offRay": 2920,
+ "blended": 0,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "glass5",
+ "flushPass": true,
+ "swapped": 0,
+ "flipped": 0,
+ "ranked": 0,
+ "tied": 206,
+ "offRay": 2156,
+ "blended": 0,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "glass6",
+ "flushPass": true,
+ "swapped": 0,
+ "flipped": 0,
+ "ranked": 0,
+ "tied": 0,
+ "offRay": 633,
+ "blended": 0,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "glass7",
+ "flushPass": true,
+ "swapped": 7,
+ "flipped": 7,
+ "ranked": 0,
+ "tied": 0,
+ "offRay": 1471,
+ "blended": 1,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "roofs",
+ "flushPass": false,
+ "swapped": 11329,
+ "flipped": 11329,
+ "ranked": 0,
+ "tied": 1114,
+ "offRay": 4982,
+ "blended": 1,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "bridge",
+ "flushPass": false,
+ "swapped": 3271,
+ "flipped": 3271,
+ "ranked": 0,
+ "tied": 0,
+ "offRay": 1030,
+ "blended": 0,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "river",
+ "flushPass": false,
+ "swapped": 1973,
+ "flipped": 1973,
+ "ranked": 0,
+ "tied": 133,
+ "offRay": 2906,
+ "blended": 1,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "overview",
+ "flushPass": false,
+ "swapped": 54,
+ "flipped": 54,
+ "ranked": 0,
+ "tied": 44,
+ "offRay": 659,
+ "blended": 0,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "glass0",
+ "flushPass": false,
+ "swapped": 23328,
+ "flipped": 23328,
+ "ranked": 0,
+ "tied": 119,
+ "offRay": 5211,
+ "blended": 0,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "glass1",
+ "flushPass": false,
+ "swapped": 1371,
+ "flipped": 1371,
+ "ranked": 0,
+ "tied": 16,
+ "offRay": 2905,
+ "blended": 1,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "glass2",
+ "flushPass": false,
+ "swapped": 42069,
+ "flipped": 42069,
+ "ranked": 0,
+ "tied": 2477,
+ "offRay": 6704,
+ "blended": 0,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "glass3",
+ "flushPass": false,
+ "swapped": 6376,
+ "flipped": 6376,
+ "ranked": 0,
+ "tied": 128,
+ "offRay": 5763,
+ "blended": 0,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "glass4",
+ "flushPass": false,
+ "swapped": 5141,
+ "flipped": 5141,
+ "ranked": 0,
+ "tied": 236,
+ "offRay": 4179,
+ "blended": 0,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "glass5",
+ "flushPass": false,
+ "swapped": 1586,
+ "flipped": 1586,
+ "ranked": 0,
+ "tied": 318,
+ "offRay": 3215,
+ "blended": 0,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "glass6",
+ "flushPass": false,
+ "swapped": 212,
+ "flipped": 212,
+ "ranked": 0,
+ "tied": 0,
+ "offRay": 668,
+ "blended": 0,
+ "width": 1280,
+ "height": 720
+ },
+ {
+ "spot": "glass7",
+ "flushPass": false,
+ "swapped": 12812,
+ "flipped": 12812,
+ "ranked": 0,
+ "tied": 0,
+ "offRay": 4367,
+ "blended": 1,
+ "width": 1280,
+ "height": 720
+ }
+]
diff --git a/bench/flicker/viewer.js b/bench/flicker/viewer.js
index 74100779..ba8eac7e 100644
--- a/bench/flicker/viewer.js
+++ b/bench/flicker/viewer.js
@@ -18,9 +18,13 @@ function paintPartsByOwner(parts) {
});
}
+// An unlit surface leaves the renderer as pow(colour, 1 / 2.2) when the frame is not HDR.
+const DISPLAY_GAMMA = 2.2;
+
// facets=1: one colour per triangle instead of per part. Two faces of one part that meet at a crease then differ in colour, so the
// probe reads the crease as an edge and stops counting its ordinary anti-aliasing as flicker. Fighting triangles still differ.
function paintTrianglesApart(parts) {
+ let triangleNumber = 0;
for (const part of parts) {
const { positions, normals, indices } = part.geometry;
const triangleCount = indices.length / 3;
@@ -28,8 +32,11 @@ function paintTrianglesApart(parts) {
const flatNormals = new Float32Array(triangleCount * 9);
const colors = new Float32Array(triangleCount * 9);
for (let triangle = 0; triangle < triangleCount; triangle++) {
- const hue = (((triangle + 1) * 0.618034) % 1);
- const colour = [0, 1 / 3, 2 / 3].map(offset => 0.35 + 0.65 * (0.5 + 0.5 * Math.cos(2 * Math.PI * (hue + offset))));
+ triangleNumber++;
+ const hue = ((triangleNumber * 0.618034) % 1);
+ const colour = options.triangleIds
+ ? [triangleNumber & 255, (triangleNumber >> 8) & 255, (triangleNumber >> 16) & 255].map(byte => (byte / 255) ** DISPLAY_GAMMA)
+ : [0, 1 / 3, 2 / 3].map(offset => 0.35 + 0.65 * (0.5 + 0.5 * Math.cos(2 * Math.PI * (hue + offset))));
for (let corner = 0; corner < 3; corner++) {
const source = indices[triangle * 3 + corner] * 3;
const target = (triangle * 3 + corner) * 3;
@@ -40,7 +47,7 @@ function paintTrianglesApart(parts) {
}
part.geometry = { positions: flatPositions, normals: flatNormals, colors, indices: Uint32Array.from({ length: triangleCount * 3 }, (_, index) => index) };
const { map, ...material } = part.material;
- part.material = { ...material, color: [1, 1, 1], metallic: 0, roughness: 1 };
+ part.material = { ...material, color: [1, 1, 1], metallic: 0, roughness: 1, ...(options.triangleIds ? { tag: 'unlit' } : {}) };
}
}
@@ -86,7 +93,9 @@ const options = {
ids: parameters.get('ids') === '1',
// shadows=0 turns off casting and receiving, to tell shadow-edge aliasing apart from depth fighting.
shadows: parameters.get('shadows') !== '0',
- facets: parameters.get('facets') === '1',
+ facets: parameters.get('facets') === '1' || parameters.get('facets') === '2',
+ // facets=2: the colour of every triangle is its own number, drawn unlit, so the frame tells exactly which face is shown at a pixel.
+ triangleIds: parameters.get('facets') === '2',
};
const trapName = parameters.get('trap');
@@ -188,7 +197,7 @@ async function build() {
const size = bounds.maximum.map((value, axis) => value - bounds.minimum[axis]);
extent = Math.max(...size);
renderer = new Renderer67(canvas, loaded.parts, {
- ground: false, hdr: true, samples: 4, pixelRatio: Math.min(devicePixelRatio, 1.5), memoryBudgetMiB: 1024,
+ ground: false, hdr: !options.triangleIds, samples: 4, pixelRatio: Math.min(devicePixelRatio, 1.5), memoryBudgetMiB: 1024,
reversedDepth: options.reversed ? 'auto' : false,
resolveCoplanar: options.offset ? resolveAndReport : false,
});
@@ -238,6 +247,9 @@ function readFrame(context) {
// `totalChanged` counts every pixel whose owner changed, edges included. `clearOfEdges` is stricter than `changed`: an anti-aliased
// edge blends two owners into a colour that is close to both, which the four-neighbour test reads as no edge, so this count also
// leaves out pixels with a colour step anywhere within two pixels.
+// The bytes an unlit face of this colour shows: the triangle number that painted it, back from the gamma curve.
+const shownByte = colour => colour.map(channel => Math.round(255 * channel ** (1 / DISPLAY_GAMMA)));
+
// The ground-truth probe (ground-truth.js): one frame, compared with a float64 ray cast that cannot z-fight. Needs flat colours
// per part (ids=1) or per triangle (facets=1); the trap scenes bring their own. Returns the counts of compareWithGroundTruth
// and the milliseconds the ray cast took.
@@ -253,7 +265,7 @@ async function truth() {
const started = performance.now();
const world = collectWorldTriangles(window.flickerParts, flushOrderOf);
const cast = castPixelOwners(world, frameFor(0), width, height);
- const { mismatchMask, ...counts } = compareWithGroundTruth({ pixels, world, cast, width, height });
+ const { mismatchMask, ...counts } = compareWithGroundTruth({ pixels, world, cast, width, height, exact: options.triangleIds ? shownByte : null });
renderer.render(frameFor(0));
return { ...counts, triangles: world.count, castMilliseconds: Math.round(performance.now() - started), width, height };
}
@@ -307,11 +319,11 @@ function swapDetails(limit = 12) {
const pixels = new Uint8ClampedArray(readFrame(context));
const world = collectWorldTriangles(window.flickerParts, flushOrderOf);
const cast = castPixelOwners(world, frameFor(0), width, height);
- const { mismatchMask } = compareWithGroundTruth({ pixels, world, cast, width, height });
+ const { mismatchMask } = compareWithGroundTruth({ pixels, world, cast, width, height, exact: options.triangleIds ? shownByte : null });
const groups = new Map();
for (let pixel = 0; pixel < mismatchMask.length; pixel++) {
if (mismatchMask[pixel] !== 2) continue;
- const group = groups.get(cast.owner[pixel]) ?? { triangle: cast.owner[pixel], pixels: 0, distances: [] };
+ const group = groups.get(cast.owner[pixel]) ?? { triangle: cast.owner[pixel], pixels: 0, distances: [], sample: pixel };
group.pixels++;
const neighbours = [-1, 1, -width, width, -width - 1, -width + 1, width - 1, width + 1];
if (neighbours.some(step => cast.owner[pixel + step] !== cast.owner[pixel])) group.onTriangleEdge = (group.onTriangleEdge ?? 0) + 1;
@@ -324,6 +336,12 @@ function swapDetails(limit = 12) {
behind: trianglesBehind(world, group.triangle),
distance: group.distances.sort((first, second) => first - second)[group.distances.length >> 1],
corners: Array.from(world.corners.subarray(group.triangle * 9, group.triangle * 9 + 9)),
+ sample: {
+ x: group.sample % width, y: Math.floor(group.sample / width),
+ drawnColour: [...pixels.subarray(group.sample * 4, group.sample * 4 + 3)],
+ hits: Array.from({ length: cast.hitsPerPixel }, (_, slot) => cast.hitTriangles[group.sample * cast.hitsPerPixel + slot]).filter(triangle => triangle >= 0)
+ .map((triangle, slot) => ({ triangle, distance: cast.hitDistances[group.sample * cast.hitsPerPixel + slot], rank: world.ranks[triangle], colour: world.keyColours[world.keyOf[triangle]] })),
+ },
}));
}
diff --git a/changes/751-ground-truth.md b/changes/751-ground-truth.md
new file mode 100644
index 00000000..be5f9cb2
--- /dev/null
+++ b/changes/751-ground-truth.md
@@ -0,0 +1 @@
+- The flicker probe has an exact ground truth: every triangle is drawn unlit as its own number and compared with a float64 ray cast, so it counts true visibility flips and not colour-test false positives (#751)
diff --git a/tests/flicker-ground-truth.test.js b/tests/flicker-ground-truth.test.js
index dde8abd8..2026a2ba 100644
--- a/tests/flicker-ground-truth.test.js
+++ b/tests/flicker-ground-truth.test.js
@@ -204,3 +204,64 @@ test('failingTraps flags a swapped pixel with the flush pass on and ignores runs
{ trap: 'missing', configuration: 'reversed0', swapped: null },
]);
});
+
+// The bytes a face of this colour shows in an unlit frame painted one colour per face.
+const exactBytes = colour => colour.map(channel => Math.round(channel * 255));
+
+// A frame that shows exactly the face the ray cast names, except where `override` gives other bytes.
+function paintExactFrame(cast, override = () => null) {
+ const pixels = new Uint8ClampedArray(WIDTH * HEIGHT * 4);
+ for (let pixel = 0; pixel < WIDTH * HEIGHT; pixel++) {
+ const owner = cast.owner[pixel];
+ const truth = owner === NO_TRIANGLE ? [0, 0, 0] : owner >= 2 ? [0, 0, 255] : [255, 0, 0];
+ pixels.set([...(override(pixel) ?? truth), 255], pixel * 4);
+ }
+ return pixels;
+}
+
+function compareExact(pixels, world, cast) {
+ return compareWithGroundTruth({ pixels, world, cast, width: WIDTH, height: HEIGHT, exact: exactBytes });
+}
+
+test('an exact frame with the right face everywhere has no wrong pixel', () => {
+ const { world, cast } = castWallWithPlaque(0.01);
+ assert.deepEqual(world.keyColours, [[1e7, 0, 0], [0, 0, 1e7]]);
+ const outcome = compareExact(paintExactFrame(cast), world, cast);
+ assert.equal(outcome.wrong, 0);
+ assert.ok(outcome.contested > 0);
+});
+
+test('an exact frame showing the wall through the plaque counts one true visibility flip', () => {
+ const { world, cast } = castWallWithPlaque(0.01);
+ const outcome = compareExact(paintExactFrame(cast, pixel => (pixel === CENTRE ? [255, 0, 0] : null)), world, cast);
+ assert.equal(outcome.wrong, 1);
+ assert.equal(outcome.swapped, 1);
+ assert.equal(outcome.flipped, 1);
+ assert.equal(outcome.ranked, 0);
+ assert.equal(outcome.mismatchMask[CENTRE], 2);
+});
+
+test('a face lifted over a nearer one by its rank is swapped but not a flip', () => {
+ const { world, cast } = castWallWithPlaque(-0.0005);
+ world.ranks[0] = 0;
+ world.ranks[1] = 0;
+ world.ranks[2] = 1;
+ world.ranks[3] = 1;
+ const outcome = compareExact(paintExactFrame(cast, pixel => (pixel === CENTRE ? [0, 0, 255] : null)), world, cast);
+ assert.equal(outcome.swapped, 1);
+ assert.equal(outcome.ranked, 1);
+ assert.equal(outcome.flipped, 0);
+});
+
+test('an exact frame showing a face off the ray, or a colour no face has, is not depth fighting', () => {
+ const parts = [...wallWithPlaque(0.01), wallPart('far-side', 2, 2, 0, [0, 1, 0])];
+ parts[2].matrix = new Float32Array([...IDENTITY.slice(0, 12), 500, 0, 0, 1]);
+ const world = collectWorldTriangles(parts, () => null);
+ const cast = castPixelOwners(world, CAMERA, WIDTH, HEIGHT);
+ const outcome = compareExact(paintExactFrame(cast, pixel => (pixel === CENTRE ? [0, 255, 0] : pixel === CENTRE + 1 ? [7, 7, 7] : null)), world, cast);
+ assert.equal(outcome.wrong, 2);
+ assert.equal(outcome.offRay, 1);
+ assert.equal(outcome.blended, 1);
+ assert.equal(outcome.swapped, 0);
+ assert.equal(outcome.flipped, 0);
+});
diff --git a/tests/flush-resolve.test.js b/tests/flush-resolve.test.js
index a5d26fc1..92ac883c 100644
--- a/tests/flush-resolve.test.js
+++ b/tests/flush-resolve.test.js
@@ -278,3 +278,19 @@ test('the bias moves depth to where the point would be that much closer to the e
assert.match(source, /uniform vec3 flushBias;\s+uniform vec4 clipPerMetre;/);
assert.match(source, /gl_Position = viewProjection \* world;\s+\/\/[^\n]*\n[^\n]*\n\s+if \(flushBias != vec3\(0\.0\)\) \{\s+vec4 lifted = gl_Position - \(flushBias\.x \+ flushBias\.y \* gl_Position\.w\) \* clipPerMetre;\s+gl_Position\.z = \(lifted\.z \/ lifted\.w \+ flushBias\.z\) \* gl_Position\.w;/);
});
+
+test('glass a centimetre in front of nested facades is ranked above them with no rank-overflow', () => {
+ // Melbourne's towers: facades of stacked blocks a few millimetres apart, 17 deep, with a glass skin 1 cm out from the outermost.
+ const facade = (index, offset) => {
+ const height = 40 + index * 5;
+ return [0, 0, offset, 30, 0, offset, 30, height, offset, 0, height, offset];
+ };
+ const walls = partOf('walls', Array.from({ length: 17 }, (unused, index) => facade(index, index * 0.005)));
+ const glass = partOf('glass', Array.from({ length: 3 }, (unused, index) => facade(index * 8, 17 * 0.005 + 0.01)), { material: { alphaMode: 'blend' } });
+ assert.deepEqual(resolveFlush([walls, glass]), { ranked: 2, leftovers: [] });
+ assert.ok(Math.abs(drawnArea(walls) - 30 * 120) < 1, 'the walls draw the union of their facades once');
+ const wallRanks = ranksOf(walls);
+ const glassRanks = ranksOf(glass);
+ assert.ok(Math.max(...wallRanks) < Math.min(...glassRanks), 'the glass is above every wall it covers');
+ assert.ok(Math.max(...glassRanks) <= MAXIMUM_FLUSH_RANK);
+});