3D engine that AI agents drive
Fit the default fog to the scene size
Nguyen Xuan Anfeb985bc35df
diff --git a/bench/size/budgets.json b/bench/size/budgets.jsonindex 3176ea0d..d5d63315 100644--- a/bench/size/budgets.json+++ b/bench/size/budgets.json@@ -6,13 +6,13 @@"agent-scene": 100000},"budgets": {- "entry:core": 87300,+ "entry:core": 87540,"entry:model": 14400,"entry:nature": 13200,"entry:motion": 13600,"entry:canvas2d": 9400,- "entry:gltf": 54120,- "entry:gltf+lazy": 57450,+ "entry:gltf": 54590,+ "entry:gltf+lazy": 57920,"entry:modeling": 8700,"entry:sculpt": 2500,"entry:surface": 10100,@@ -34,9 +34,9 @@"entry:ground-bounce": 9460,"entry:path-tracer": 14600,"entry:texture-set": 900,- "entry:agent-scene": 78540,+ "entry:agent-scene": 78550,"entry:agent-scene+lazy": 261000,- "entry:agent-asset": 109890,+ "entry:agent-asset": 109900,"entry:agent-asset+lazy": 272400,"entry:hdri": 3300,"entry:camera-effects": 8600,@@ -45,10 +45,10 @@"entry:material-layer-shaders": 21360,"entry:light-linking": 19680,"entry:cascaded-shadows": 28480,- "scene:lit-box": 64650,- "scene:gltf-box": 85680,- "scene:gltf-box+lazy": 89010,- "scene:agent-scene": 106460,- "scene:agent-scene+lazy": 288870+ "scene:lit-box": 64890,+ "scene:gltf-box": 86430,+ "scene:gltf-box+lazy": 89760,+ "scene:agent-scene": 106710,+ "scene:agent-scene+lazy": 289120}}diff --git a/changes/263.md b/changes/263.mdnew file mode 100644index 00000000..17ea04c6--- /dev/null+++ b/changes/263.md@@ -0,0 +1 @@+- Fit the default fog to the scene size (issue #263). A scene that sets no `fogDensity` now gets 0.006, or 1.2 divided by the scene diagonal when that is smaller, so a 900 m scene keeps 30 percent of its contrast at the far side instead of 0.4 percent. Scenes up to about 200 m are unchanged. `normalizeAgentLook` now returns `fogDensity: null` for "fit to the scene"; set a number, or 0 for none, to override.diff --git a/docs/agent-look.md b/docs/agent-look.mdindex a73b1b4e..66ec6c47 100644--- a/docs/agent-look.md+++ b/docs/agent-look.md@@ -121,7 +121,7 @@ Sky colors use display RGB, matching the renderer's existing environment contrac| `skyTop` | `[0.2, 0.38, 0.52]` | Components from `0` to `1` || `skyBottom` | `[0.62, 0.7, 0.67]` | Components from `0` to `1` || `fogColor` | `[0.35, 0.43, 0.43]` | Components from `0` to `1` |-| `fogDensity` | `0.006` | `0` to `10` |+| `fogDensity` | `null` | `0` to `10`. `null` fits the density to the scene: `0.006`, or `1.2` divided by the scene diagonal in metres when that is smaller, so a 900 m scene keeps 30 percent of its contrast at the far side. Set `0` for no fog || `volumeFog` | Complete defaults below | One bounded world volume |Directional key radiance equals `keyColor * keyIntensity`.diff --git a/docs/guide.md b/docs/guide.mdindex 6ad15119..e5c28aba 100644--- a/docs/guide.md+++ b/docs/guide.md@@ -380,7 +380,7 @@ Each effect compiles and allocates its targets on the first frame that turns itPasses draw without multisampling into their own target, and `renderer.passObjectNames()` lists the part behind each object ID.The pass branch of the main shader compiles on the first pass request. MCP render jobs write passes to files; see [agent render jobs](agent-render-jobs.md#render-passes).-Outdoor `environment` accepts `skyTop`, `skyBottom` (display RGB), `fogColor` (linear RGB), `fogDensity`, `lightDirection` and `daylight`. Set `world: true` and `ground: false`. Frames can override environment and supply `time`, `wind`, `waterRipple` and `rippleTime`.+Outdoor `environment` accepts `skyTop`, `skyBottom` (display RGB), `fogColor` (linear RGB), `fogDensity` (unset, it is 0.006, thinned to 1.2 divided by the scene diagonal for scenes wider than about 200 m), `lightDirection` and `daylight`. Set `world: true` and `ground: false`. Frames can override environment and supply `time`, `wind`, `waterRipple` and `rippleTime`.## Local lights and volume fogdiff --git a/src/agent-look.js b/src/agent-look.jsindex 351ec037..26489943 100644--- a/src/agent-look.js+++ b/src/agent-look.js@@ -252,7 +252,7 @@ function environmentFrom(input = {}) {skyTop: vectorInRange(fallback(input.skyTop, defaults.skyTop), 0, 1, 'skyTop'),skyBottom: vectorInRange(fallback(input.skyBottom, defaults.skyBottom), 0, 1, 'skyBottom'),fogColor: vectorInRange(fallback(input.fogColor, [0.35, 0.43, 0.43]), 0, 1, 'fogColor'),- fogDensity: numberInRange(fallback(input.fogDensity, 0.006), 0, 10, 'fogDensity'),+ fogDensity: input.fogDensity === undefined || input.fogDensity === null ? null : numberInRange(input.fogDensity, 0, 10, 'fogDensity'),volumeFog: volumeFogFrom(input.volumeFog),};// Optional so recipes without these fields keep their exported shape.diff --git a/src/fog-fit.js b/src/fog-fit.jsnew file mode 100644index 00000000..101a6f15--- /dev/null+++ b/src/fog-fit.js@@ -0,0 +1,41 @@+import { geometryBoundingSphere } from './render-culling.js';++export const DEFAULT_FOG_DENSITY = 0.006;++// Optical depth across the whole scene. 1.2 is what the old fixed density gave a 200 m scene, so smaller scenes keep+// their look and larger ones get a thinner fog that leaves 30 percent of the contrast at the far side.+const SCENE_OPTICAL_DEPTH = 1.2;++function largestAxisScale(matrix) {+ let largest = 0;+ for (let column = 0; column < 3; column++) {+ largest = Math.max(largest, Math.hypot(matrix[column * 4], matrix[column * 4 + 1], matrix[column * 4 + 2]));+ }+ return largest;+}++/** Diagonal of the box around every part's bounding sphere, in world units. Zero when no part has geometry. */+export function sceneDiagonal(parts) {+ const minimum = [Infinity, Infinity, Infinity];+ const maximum = [-Infinity, -Infinity, -Infinity];+ for (const part of parts) {+ if (!part.geometry?.positions?.length || !part.matrix) continue;+ const sphere = geometryBoundingSphere(part.geometry);+ const matrix = part.matrix;+ const radius = sphere.radius * largestAxisScale(matrix);+ for (let axis = 0; axis < 3; axis++) {+ const center = matrix[axis] * sphere.center[0] + matrix[4 + axis] * sphere.center[1] + matrix[8 + axis] * sphere.center[2] + matrix[12 + axis];+ minimum[axis] = Math.min(minimum[axis], center - radius);+ maximum[axis] = Math.max(maximum[axis], center + radius);+ }+ }+ if (!Number.isFinite(minimum[0])) return 0;+ return Math.hypot(maximum[0] - minimum[0], maximum[1] - minimum[1], maximum[2] - minimum[2]);+}++/** The fog density a scene gets when it sets none: the old default, thinned for scenes wider than about 200 m. */+export function fittedFogDensity(parts) {+ const diagonal = sceneDiagonal(parts);+ if (!(diagonal > 0)) return DEFAULT_FOG_DENSITY;+ return Math.min(DEFAULT_FOG_DENSITY, SCENE_OPTICAL_DEPTH / diagonal);+}diff --git a/src/renderer.js b/src/renderer.jsindex 1303eb0c..0a7bf999 100644--- a/src/renderer.js+++ b/src/renderer.js@@ -14,6 +14,7 @@ import { assertEnvironmentMap, createEnvironmentResources, environmentBackgroundimport { validateSkinGeometry, validateSkinPose, skinTextureLayout, packSkinInfluences } from './skinning.js';import { validateSplatMaterial, splatBatchState, splatTextureBytes, resolveSplatSize, packSplatParameters, buildSplatPixels } from './splat.js';import { materialLayerState, materialLayerValue } from './material-layers.js';+import { fittedFogDensity } from './fog-fit.js';import { InstanceSelection, extractFrustumPlanes, geometryBoundingSphere, selectInstances } from './render-culling.js';import { resolveShadowLight, shadowBiasPerSlope } from './shadow-light.js';import { validateMorphGeometry, validateMorphPose, validateMorphTransform, packMorphTargets } from './morph.js';@@ -446,6 +447,7 @@ export class Renderer67 {this.depth = Boolean(depth && !benchmark);this.world = world;this.environment = environment;+ this.fittedFogDensity = world ? fittedFogDensity(parts) : undefined;this.shadowBounds = shadowBounds;/** Warnings about the last frame drawn, in the shape of `agent-diagnostics.js`; see `frame-diagnostics.js`. */this.diagnostics = [];@@ -2016,6 +2018,7 @@ export class Renderer67 {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 };+ environment.fogDensity ??= this.fittedFogDensity ?? 0.006;const volume = resolveVolumeFog(environment.volumeFog);if (volume.density > 0) {if (!this.depth || !this.hdr || this.transparent || this.benchmark) throw new Error('Volume fog requires depth:true, active HDR and an opaque canvas');diff --git a/tests/agent-look.test.js b/tests/agent-look.test.jsindex 7d6b7514..c6a1323c 100644--- a/tests/agent-look.test.js+++ b/tests/agent-look.test.js@@ -22,7 +22,7 @@ test('default look is complete, detached JSON and idempotent', () => {skyTop: [0.2, 0.38, 0.52],skyBottom: [0.62, 0.7, 0.67],fogColor: [0.35, 0.43, 0.43],- fogDensity: 0.006,+ fogDensity: null,volumeFog: resolveVolumeFog(),});assert.deepEqual(look.effects, {diff --git a/tests/fog-fit.test.js b/tests/fog-fit.test.jsnew file mode 100644index 00000000..6001b396--- /dev/null+++ b/tests/fog-fit.test.js@@ -0,0 +1,31 @@+import assert from 'node:assert/strict';+import test from 'node:test';+import { DEFAULT_FOG_DENSITY, fittedFogDensity, sceneDiagonal } from '../src/fog-fit.js';++function slabPart(sizeX, sizeZ, offsetX = 0) {+ const halfX = sizeX / 2;+ const halfZ = sizeZ / 2;+ const positions = new Float32Array([-halfX, 0, -halfZ, halfX, 0, -halfZ, halfX, 0, halfZ, -halfX, 0, halfZ]);+ const matrix = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, offsetX, 0, 0, 1]);+ return { geometry: { positions }, matrix };+}++test('a small scene keeps the default fog density', () => {+ assert.equal(fittedFogDensity([slabPart(20, 20)]), DEFAULT_FOG_DENSITY);+ assert.equal(fittedFogDensity([]), DEFAULT_FOG_DENSITY);+});++test('a 900 m scene gets a thinner fog that leaves its far side readable', () => {+ const parts = [slabPart(900, 900)];+ const density = fittedFogDensity(parts);+ const farSideDistance = sceneDiagonal(parts);+ assert.ok(density < DEFAULT_FOG_DENSITY / 4);+ assert.ok(Math.exp(-density * farSideDistance) >= 0.29, 'at least 29 percent of the contrast survives the full diagonal');+ assert.ok(Math.exp(-DEFAULT_FOG_DENSITY * farSideDistance) < 0.01, 'the fixed default hid the far side');+});++test('part transforms count toward the scene size', () => {+ const near = slabPart(10, 10);+ const far = slabPart(10, 10, 800);+ assert.ok(sceneDiagonal([near, far]) > 800);+});