Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Reversed depth on its own for a stretched lens, flush layers that work under it (issue #593)

Nguyen Xuan An53963f2585ed
diff --git a/changes/593.md b/changes/593.md
index 80ffb4f5..e4b25c17 100644
--- a/changes/593.md
+++ b/changes/593.md
@@ -1 +1,2 @@
-Parts that draw the same plane (a tile on a ceiling, a tread on steel) no longer flicker: the renderer now finds every flush pair when it is built, with no work-budget cutoff on big scenes, and draws the smaller part on a polygon-offset layer; `resolveCoplanar: false` turns it off (#593)
+- A film with a very long lens (far / near over 10,000, such as near 0.08 and far 9000) now switches to reversed-Z float depth on its own: `Renderer67` loads `reversed-depth.js` as a separate chunk on the first such frame and every frame after it draws with it (`await renderer.reversedDepthReady` to have it from frame one; `reversedDepth: false` keeps the 24-bit buffer). On the metro film, 24-bit depth could not tell surfaces 2 micrometres apart at 2.6 m, and stairs, canopy panels, handrails and steel flickered between frames; those pairs went from thousands of flipping pixels to under 60 (#593)
+- `resolveCoplanar` is now a function, `resolveFlushParts` from `67js/addons/coplanar-overlap`, so the plane-keyed search stays out of the core bundle. Its depth layers also work under reversed depth: the slope part of the offset is 16 times larger there, because the constant part moves a surface by under a micrometre in a float depth buffer (#593)
diff --git a/changes/593.size.md b/changes/593.size.md
index a44fb665..e3a46c81 100644
--- a/changes/593.size.md
+++ b/changes/593.size.md
@@ -1,11 +1,14 @@
-- entry:core: the renderer runs the coplanar search once at build (src/coplanar-overlap.js is now a core import, 2.5 KB gzip) so every scene with flush parts stops z-fighting without a call from the author; this is a fixed glitch (the metro film showed it on 12 part pairs per run, up to 43000 pixels).
-- entry:coplanar-overlap: plane-keyed triangle matching and the flush-layer step replace the capped triangle product.
-- entry:reversed-depth: it imports the coplanar module that the core now carries; same code, counted again.
-- scene:lit-box: the core import above.
-- scene:gltf-box: the core import above.
-- scene:gltf-box+lazy: the core import above.
-- scene:agent-scene: the core import above; the agent scene loaded the module lazily before.
-- scene:agent-scene+lazy: the same module moved from a lazy chunk into the core.
-- goal:lit-box: 2.5 KB for default z-fight repair of every scene; needs the lead's trade-off call (functional gain, not a score gain).
-- goal:gltf-box: the same 2.5 KB core import.
-- goal:agent-scene: the same core import; the lazy chunk it replaces is gone.
+- entry:core: the lens check and the lazy import of reversed depth; the coplanar search is no longer in the core.
+- entry:reversed-depth: the 16 times slope scale and the false-mode flag, about 1 KB gzip more than main with the earlier branch work counted.
+- entry:coplanar-overlap: the plane-keyed search that replaces the capped triangle product (issue #593), 1 KB gzip.
+- entry:gltf+lazy: renderer imports counted again for the depth-layer code from the earlier commits of this branch; needs the lead's trade-off call.
+- entry:replace-textured-parts: the same renderer imports; needs the lead's trade-off call.
+- entry:texture-update: the same renderer imports; needs the lead's trade-off call.
+- scene:lit-box: the same renderer imports.
+- scene:gltf-box: the same renderer imports.
+- scene:gltf-box+lazy: the same renderer imports.
+- scene:agent-scene: the same renderer imports.
+- scene:agent-scene+lazy: the same renderer imports.
+- goal:lit-box: 545 bytes for reversed depth on a stretched lens and flush-layer support; a fixed glitch, not a score gain.
+- goal:gltf-box: the same bytes.
+- goal:agent-scene: the same bytes.
diff --git a/src/renderer.js b/src/renderer.js
index 739d6f6b..65bd9a19 100644
--- a/src/renderer.js
+++ b/src/renderer.js
@@ -9,7 +9,6 @@ import { applyProjection } from './camera.js';
import { resolveEffects } from './effects.js';
import { frameDiagnostics } from './frame-diagnostics.js';
import { depthLayerOf } from './depth-layer.js';
-import { resolveFlushParts } from './coplanar-overlap.js';
import { createLightingState, updateLightingState } from './lighting.js';
import { RenderTargetLedger } from './render-target-ledger.js';
import { SHADOW_ATLAS_WIDTH, SHADOW_TILE_SIZE, SHADOW_TILES_PER_ROW, tileViewport, writeShadowTile } from './local-shadows.js';
@@ -493,7 +492,7 @@ function allFinite(values) {
}
export class Renderer67 {
- constructor(canvas, parts, { batching = true, pixelRatio = 1.5, benchmark = false, world = false, ground = true, transparent = false, samples = 4, hdr = false, depth = false, memoryBudgetMiB, environment = {}, shadowBounds = {}, shadowCascades = null, culling = true, lodPixelError = 1, createGpuTimer = null, skyOpenness = null, fogSkyVisibility = null, shading = 'full', viewTransform = 'aces', look = null, resolveCoplanar = false } = {}) {
+ constructor(canvas, parts, { batching = true, pixelRatio = 1.5, benchmark = false, world = false, ground = true, transparent = false, samples = 4, hdr = false, depth = false, memoryBudgetMiB, environment = {}, shadowBounds = {}, shadowCascades = null, culling = true, lodPixelError = 1, createGpuTimer = null, skyOpenness = null, fogSkyVisibility = null, shading = 'full', viewTransform = 'aces', look = null, resolveCoplanar = false, reversedDepth = 'auto' } = {}) {
this.canvas = canvas;
this.sceneParts = parts;
// `skyOpenness` from closed-room.js is passed in, which keeps the ray code out of scenes without volume fog.
@@ -532,6 +531,10 @@ export class Renderer67 {
this.shadowBounds = shadowBounds;
/** Set by installCoplanarDiagnostics (coplanar-overlap.js): called with the batches, returns coplanar-overlap warnings. */
this.coplanarDiagnostics = null;
+ /** `false` keeps the standard depth buffer; `'auto'` (default) loads reversed-Z once a frame's far / near passes 10,000. */
+ this.reversedDepthMode = reversedDepth;
+ /** Resolves once reversed depth is installed (set by the first frame with a stretched lens); `await` it to render with it from the start. */
+ this.reversedDepthReady = null;
/** Warnings about the last frame drawn, in the shape of `agent-diagnostics.js`; see `frame-diagnostics.js`. */
this.diagnostics = [];
this.activeLightDirection = environment.lightDirection ?? [-3, 6, 4];
@@ -572,7 +575,9 @@ export class Renderer67 {
this.windState = { time: 0, strength: 0.3 };
// The shadow map the constructor draws uses no assembly, so a first frame with none does not draw it again.
this.assemblyAmount = 0;
- if (resolveCoplanar && !benchmark) resolveFlushParts(parts);
+ // `resolveCoplanar` is resolveFlushParts from the coplanar-overlap addon, passed in so the search stays out of the core bundle.
+ if (resolveCoplanar && typeof resolveCoplanar !== 'function') throw new TypeError('resolveCoplanar is resolveFlushParts from 67js/addons/coplanar-overlap.');
+ if (resolveCoplanar && !benchmark) resolveCoplanar(parts);
const groups = groupParts(this.groundPart ? [...parts, this.groundPart] : parts, batching);
const textureLimit = this.context.getParameter(this.context.MAX_TEXTURE_SIZE);
this.hasSkins = groups.some(group => group[0].skin || group[0].crowd);
@@ -2138,7 +2143,16 @@ export class Renderer67 {
this.fogFieldOverride = field === null ? null : validateFogField(field);
}
+ // A 24-bit depth buffer cannot tell surfaces 2 micrometres apart at 2.6 m when far / near is this large (issue #593), so the
+ // first such frame loads reversed-Z (reversed-depth.js, a separate chunk) and the frames after it use a float depth buffer.
+ loadReversedDepthFor(frame) {
+ if (this.reversedDepthMode === false || this.reversedDepth !== undefined || this.reversedDepthReady) return;
+ if (!(frame.far / (frame.near ?? 0.1) > 1e4)) return;
+ this.reversedDepthReady = import('./reversed-depth.js').then(({ installReversedDepth }) => installReversedDepth(this, this.reversedDepthMode));
+ }
+
render(frame) {
+ this.loadReversedDepthFor?.(frame);
const { eye, target = [0, 1.2, 0], armorColor = [0.53, 0.39, 0.24], armorSurface = [0.88, 0.32], exposure = 1, inspection = false } = frame;
const effects = resolveEffects(frame.effects);
const environment = { lightDirection: [-3, 6, 4], fogColor: [0.35, 0.43, 0.43], fogDensity: 0.006, daylight: 1, explosionCenter: [0, 0, 0], ...this.environment, ...frame.environment };
diff --git a/src/reversed-depth.js b/src/reversed-depth.js
index 4fc6c874..17b89b2e 100644
--- a/src/reversed-depth.js
+++ b/src/reversed-depth.js
@@ -12,6 +12,10 @@ import { compileProgram, fullscreenVertex } from './gl-program.js';
import { shaderTransforms } from './shader-registry.js';
export const FAR_TO_NEAR_LIMIT = 1e4;
+// A float depth buffer resolves about 2^-23 of the value, so the constant part of a depth-layer offset moves a surface by well
+// under a micrometre, less than the rounding of a part's own matrix (issue #593: 2 um flush pairs kept flickering). The slope
+// part scales with the surface's own depth gradient, so it is the term that separates two flush faces; 16 cleared the metro film.
+const REVERSED_LAYER_SLOPE_SCALE = 16;
const ZERO_VECTOR = new Float32Array(4);
const DEFAULT_NEAR = 0.1;
@@ -148,6 +152,7 @@ function useFloatDepthTargets(renderer) {
* `renderer.reversedDepth` is true once it is on. A browser without EXT_clip_control, or a benchmark renderer, stays standard.
*/
export function installReversedDepth(renderer, mode = 'auto') {
+ if (mode === false) renderer.reversedDepth ??= false;
if (mode === false || renderer.reversedDepth !== undefined) return renderer;
const context = renderer.context;
const original = {
@@ -246,7 +251,7 @@ export function installReversedDepth(renderer, mode = 'auto') {
// The core pulls a decal toward the eye with a negative offset; reversed depth grows toward the eye, so the sign flips.
renderer.applyDepthLayer = function applyDepthLayerReversed(layer) {
original.applyDepthLayer.call(this, layer);
- if (layer && clipControl.active) context.polygonOffset(layer, layer * 4);
+ if (layer && clipControl.active) context.polygonOffset(layer * REVERSED_LAYER_SLOPE_SCALE, layer * 4);
};
renderer.resolveFrame = function resolveFrameStandard(...parameters) {
endReversed();
diff --git a/tests/reversed-depth.test.js b/tests/reversed-depth.test.js
index 6923e744..1e063d8b 100644
--- a/tests/reversed-depth.test.js
+++ b/tests/reversed-depth.test.js
@@ -80,7 +80,7 @@ test('auto mode stays off until a frame or a request asks, true forces it and fa
const off = fakeRenderer();
installReversedDepth(off.renderer, false);
off.renderer.render({ far: 1e6, near: 0.1 });
- assert.equal(off.renderer.reversedDepth, undefined);
+ assert.equal(off.renderer.reversedDepth, false);
assert.equal(off.renderer.requestReversedDepth, undefined);
});
@@ -124,3 +124,23 @@ test('the main vertex shader is compiled camera-relative only through the addon,
assert.doesNotMatch(cameraRelative, /gl_Position = viewProjection \* world;/);
assert.equal(compile(mainSource), mainSource);
});
+
+test('the renderer loads reversed depth itself for a stretched lens, once, and not when the mode is false', async () => {
+ const { Renderer67 } = await import('../src/renderer.js');
+ const stretched = { far: 9000, near: 0.08 };
+ const automatic = Object.create(Renderer67.prototype);
+ automatic.reversedDepthMode = 'auto';
+ automatic.loadReversedDepthFor({ far: 100, near: 0.1 });
+ assert.equal(automatic.reversedDepthReady ?? null, null);
+ automatic.loadReversedDepthFor(stretched);
+ assert.ok(automatic.reversedDepthReady instanceof Promise);
+ const first = automatic.reversedDepthReady;
+ automatic.loadReversedDepthFor(stretched);
+ assert.equal(automatic.reversedDepthReady, first);
+ await first.catch(() => {});
+
+ const disabled = Object.create(Renderer67.prototype);
+ disabled.reversedDepthMode = false;
+ disabled.loadReversedDepthFor(stretched);
+ assert.equal(disabled.reversedDepthReady ?? null, null);
+});