Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Leave exactly flush pairs to the depth buffer and settle layer chains by relaxation (issue #593)

Nguyen Xuan An7029e3955f81
diff --git a/changes/593-false-pairs.md b/changes/593-false-pairs.md
new file mode 100644
index 00000000..ba65b498
--- /dev/null
+++ b/changes/593-false-pairs.md
@@ -0,0 +1 @@
+- resolveCoplanar: resolveFlushParts no longer layers pairs that lie on one plane to within a micrometre (the depth buffer already settles them) and settles chains of equal-sized parts, so it stops adding flicker on dense scenes (#593)
diff --git a/src/coplanar-overlap.js b/src/coplanar-overlap.js
index 0043cc19..68388a21 100644
--- a/src/coplanar-overlap.js
+++ b/src/coplanar-overlap.js
@@ -11,6 +11,8 @@ import { depthLayerOf, MAXIMUM_DEPTH_LAYER, setFlushDepthLayers } from './depth-
export const COPLANAR_TOLERANCE = 0.001;
/** Overlaps smaller than this (square metres) are edge contact, not a fight. */
export const COPLANAR_MINIMUM_AREA = 1e-4;
+/** Pairs on one plane to within this distance (metres) are left to the depth buffer by resolveFlushParts. */
+export const EXACT_FLUSH_DISTANCE = 1e-6;
/** The distance suggested in the fix text, in metres. */
export const SUGGESTED_OFFSET = 0.002;
const DEFAULT_WORK_BUDGET = 1_000_000;
@@ -452,23 +454,32 @@ export function coplanarOverlapDiagnostics({ overlaps, truncated }) {
/**
* The layer each smaller part of a pair should draw on, as a Map from part to layer. A part that is the smaller one against
- * a part already on layer n gets n + 1, up to MAXIMUM_DEPTH_LAYER. Parts that set their own layer are left alone.
+ * a part already on layer n gets n + 1. Parts that set their own layer are left alone.
+ * A chain deeper than MAXIMUM_DEPTH_LAYER would pile parts on the top layer, where they fight each other again at the largest
+ * offset, so a part that cannot clear its larger part stays where it is. Layers are settled by repeating until nothing moves,
+ * because parts of equal size have no order that a single sweep could follow.
+ * A pair closer than `minimumGap` (metres, default 0: every pair counts) is skipped. resolveFlushParts passes
+ * EXACT_FLUSH_DISTANCE: such a pair lies on one plane to within rounding, which a float depth buffer (reversed depth, the
+ * default) already breaks the same way every frame, while an offset moves the whole part and made the metro film flicker far
+ * more (issue #593: 5 to 7633 flagged pixels), so only pairs with a real gap get a layer there.
*/
-export function coplanarLayers(overlaps) {
+export function coplanarLayers(overlaps, { minimumGap = 0 } = {}) {
const layers = new Map();
- const ordered = [...overlaps].sort((first, second) => diagonalOfPart(second.larger) - diagonalOfPart(first.larger));
- for (const { smaller, larger } of ordered) {
- if (smaller.decal !== undefined || smaller.depthLayer !== undefined) continue;
- const base = layers.get(larger) ?? depthLayerOf(larger);
- layers.set(smaller, Math.min(MAXIMUM_DEPTH_LAYER, Math.max(layers.get(smaller) ?? 0, base + 1)));
+ for (let pass = 0; pass <= MAXIMUM_DEPTH_LAYER + 1; pass++) {
+ let moved = false;
+ for (const { smaller, larger, distance } of overlaps) {
+ if (distance < minimumGap) continue;
+ if (smaller.decal !== undefined || smaller.depthLayer !== undefined) continue;
+ const wanted = (layers.get(larger) ?? depthLayerOf(larger)) + 1;
+ if (wanted > MAXIMUM_DEPTH_LAYER || wanted <= (layers.get(smaller) ?? 0)) continue;
+ layers.set(smaller, wanted);
+ moved = true;
+ }
+ if (!moved) break;
}
return layers;
}
-function diagonalOfPart(part) {
- return diagonal(worldBounds(part));
-}
-
/**
* Adds the coplanar-overlap warning to `renderer.diagnostics`. The check is bounded (see findCoplanarOverlaps) and runs once
* per layout, when the renderer's batches change, not per frame, so parts that move later are not rechecked.
@@ -493,7 +504,7 @@ export function installCoplanarDiagnostics(renderer, onOverlaps = null) {
* for the parts of one scene. The renderer calls it once when it is built; a part that sets `decal` or `depthLayer` keeps its own.
*/
export function resolveFlushParts(parts) {
- const layers = coplanarLayers(findCoplanarOverlaps(parts).overlaps);
+ const layers = coplanarLayers(findCoplanarOverlaps(parts).overlaps, { minimumGap: EXACT_FLUSH_DISTANCE });
setFlushDepthLayers(layers);
return layers.size;
}
diff --git a/tests/coplanar-overlap.test.js b/tests/coplanar-overlap.test.js
index 8346a712..48d8b2c7 100644
--- a/tests/coplanar-overlap.test.js
+++ b/tests/coplanar-overlap.test.js
@@ -1,8 +1,8 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { createAgentScene } from '../src/agent-scene.js';
-import { findCoplanarOverlaps, coplanarOverlapDiagnostics, installCoplanarDiagnostics, resolveCoplanarParts, resolveFlushParts } from '../src/coplanar-overlap.js';
-import { depthLayerOf } from '../src/depth-layer.js';
+import { coplanarLayers, findCoplanarOverlaps, coplanarOverlapDiagnostics, installCoplanarDiagnostics, resolveCoplanarParts, resolveFlushParts } from '../src/coplanar-overlap.js';
+import { depthLayerOf, MAXIMUM_DEPTH_LAYER } from '../src/depth-layer.js';
import { loadAgentSceneModules } from '../src/agent-scene-modules.js';
import { frameDiagnostics } from '../src/frame-diagnostics.js';
import { groupParts, Renderer67 } from '../src/renderer.js';
@@ -182,7 +182,7 @@ test('hundreds of copies parked at one spot cost two parts, not every pair among
test('flush parts get a layer beside the part, so the part objects and updateParts keep working', () => {
const road = gridPart('roadTwo', 4, 4, 0);
- const marking = gridPart('markingTwo', 2, 2, 0);
+ const marking = gridPart('markingTwo', 2, 2, 0.00005);
const far = gridPart('farAway', 4, 4, 30);
assert.equal(resolveFlushParts([road, marking, far]), 1);
assert.equal(depthLayerOf(marking), 1);
@@ -190,3 +190,41 @@ test('flush parts get a layer beside the part, so the part objects and updatePar
assert.equal(depthLayerOf(far), 0);
assert.equal(marking.depthLayer, undefined);
});
+
+test('faces that share only an edge or a seam are not pairs, and a sliver under the minimum area is ignored', () => {
+ const left = gridPart('left', 2, 2, 0);
+ const right = movedPart(gridPart('right', 2, 2, 0), 'right', 2, 0);
+ assert.deepEqual(findCoplanarOverlaps([left, right]).overlaps, []);
+ const cornerOnly = movedPart(gridPart('cornerOnly', 2, 2, 0), 'cornerOnly', 2, 2);
+ assert.deepEqual(findCoplanarOverlaps([left, cornerOnly]).overlaps, []);
+ const sliver = movedPart(gridPart('sliver', 2, 2, 0), 'sliver', 1.99999, 0);
+ assert.deepEqual(findCoplanarOverlaps([left, sliver]).overlaps, []);
+ assert.equal(resolveFlushParts([left, right, cornerOnly]), 0);
+});
+
+test('two parts that really share area are a pair with that area', () => {
+ const base = gridPart('base', 4, 1, 0);
+ const overlapping = movedPart(gridPart('overlapping', 4, 1, 0), 'overlapping', 3, 0);
+ const { overlaps } = findCoplanarOverlaps([base, overlapping]);
+ assert.equal(overlaps.length, 1);
+ assert.ok(Math.abs(overlaps[0].area - 4) < 1e-6);
+});
+
+test('resolveFlushParts leaves exactly flush pairs to the depth buffer; coplanarLayers still layers them', () => {
+ const road = gridPart('roadExact', 4, 4, 0);
+ const marking = gridPart('markingExact', 2, 2, 0);
+ assert.equal(resolveFlushParts([road, marking]), 0);
+ assert.equal(coplanarLayers(findCoplanarOverlaps([road, marking]).overlaps).get(marking), 1);
+});
+
+test('layers settle in chains and never pile up on the top layer', () => {
+ const big = gridPart('chainBig', 10, 1, 0);
+ const middle = gridPart('chainMiddle', 5, 1, 0);
+ const small = gridPart('chainSmall', 2, 1, 0);
+ const layers = coplanarLayers(findCoplanarOverlaps([small, middle, big]).overlaps);
+ assert.equal(layers.get(middle), 1);
+ assert.equal(layers.get(small), 2);
+ const stack = Array.from({ length: 8 }, (unused, index) => gridPart(`stack${index}`, 10 - index, 1, 0));
+ const stackLayers = coplanarLayers(findCoplanarOverlaps(stack).overlaps);
+ for (const layer of stackLayers.values()) assert.ok(layer <= MAXIMUM_DEPTH_LAYER);
+});