3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Refuse non-finite sun bounce strength and water mirror options (#2555)
Nguyen Xuan An5b64dfcccec6
diff --git a/changes/2555.md b/changes/2555.mdnew file mode 100644index 00000000..9aeb838a--- /dev/null+++ b/changes/2555.md@@ -0,0 +1 @@+- `withSunBounce` and `attachWaterReflection` refuse a NaN or out-of-range `environment.sunBounce`, mirror `scale` or `updateEvery` with an error that names the range, instead of NaN lights, a 1 pixel or giant mirror target, or a mirror that never redraws (#2555, #2556)diff --git a/docs/lighting.md b/docs/lighting.mdindex d299408b..c62fb6ad 100644--- a/docs/lighting.md+++ b/docs/lighting.md@@ -142,7 +142,7 @@ A closed room a sun lights gets the light its sunlit patches send on, so the floThe room test is `fittedClosedRoomShare` (`src/fitted-closed-room.js`), not the core `closedRoomShare`: the core test shoots its rays from the middle of the scene's bounds, and a lawn and a hedge outside a window moved that point out of a reading-corner room (share 0.50, 0.90 needed), so no patch was found and `sunBounce` 1 and 2 drew the same picture. The fitted test also tries the middle of the parts near the rest (share 0.98 there), leaves out the heaviest parts when the scene has more than 150 000 triangles (scanned props no longer switch the test off), and still calls open ground and a lone solid open. The inspect diagnostic `closed-room-without-bake` keeps the core test. A ray origin inside a part (a mug, a table) tests that part, not the room, so the core test starts from the first point of the bounds, the middle first and then points lifted and offset within them, that lies inside no part's bounds, and gives no answer when every candidate is inside a part (#1197).-`environment.sunBounce` is a number of 0 or more: 0 is off (the look before sun bounce existed, byte for byte: the pixels of all 6 scenes below match a tree without it), 1 is the default, fitted to Cycles on the reference halls (the tables below), above 1 is stronger. The 0.3 is fitted: at 0.6 `dark-shaft` came out brighter than Cycles around its patch (mean 1.19 times) and FLIP got worse by 0.01.+`environment.sunBounce` is a number of 0 or more (`withSunBounce` refuses NaN, Infinity and anything above 100000): 0 is off (the look before sun bounce existed, byte for byte: the pixels of all 6 scenes below match a tree without it), 1 is the default, fitted to Cycles on the reference halls (the tables below), above 1 is stronger. The 0.3 is fitted: at 0.6 `dark-shaft` came out brighter than Cycles around its patch (mean 1.19 times) and FLIP got worse by 0.01.Luminance of the Crucible picture over Cycles in the ring around the lit patch (mean and 20th percentile, linear, 480 x 270 panels of the committed Cycles sheets, patch = Cycles pixels above 0.35 of its 99.5th percentile, ring from 3 to 25 pixels away), and FLIP of the whole panel against Cycles (RTX 3060, `bench/reference/render-gpu.mjs --sun-bounce <n>`, scored on the 960 x 540 draws scaled to the panel):diff --git a/docs/terrain.md b/docs/terrain.mdindex d121708b..f91eb709 100644--- a/docs/terrain.md+++ b/docs/terrain.md@@ -92,7 +92,7 @@ In a recipe, the same ground is the `terrain` geometry (`size`, `resolution`, `s## Terrain near water-A terrain near water needs nothing set. When `applyWaterReflection` finds a water surface in the scene, every terrain part it is given builds up to two extra coarse meshes per chunk (as many halvings as the resolution allows) that only the water reflection draws; `attachWaterReflection` hides them from every other draw. This is one build of about 130 ms in `applyWaterReflection`, before the renderer exists, so no frame waits for it. The first call with water builds the coarse levels (about 0.1 s and 22 MB), so call it during setup, before the animation starts. Terrain in a scene without water builds nothing extra: its memory and build time are what they were. Pass `mirrorLevels` to choose the count yourself; `mirrorLevels: 0` keeps the plain terrain even next to water, and a number you set is built when the terrain is created, as before. Measured on a 900 m terrain at resolution 128 with 8 by 8 chunks: the levels add about 22 MB of mesh data (the finest level is 61 MB). In return the mirror frame of the water benchmark (960 by 540, RTX 3060, median of 240 samples) costs +25% over reflection off instead of +40%.+A terrain near water needs nothing set. When `applyWaterReflection` finds a water surface in the scene, every terrain part it is given builds up to two extra coarse meshes per chunk (as many halvings as the resolution allows) that only the water reflection draws; `attachWaterReflection` takes `scale` (0.05 to 1 of the canvas, default 0.75) and `updateEvery` (0 to 1000 lit frames) and refuses other values; it hides them from every other draw. This is one build of about 130 ms in `applyWaterReflection`, before the renderer exists, so no frame waits for it. The first call with water builds the coarse levels (about 0.1 s and 22 MB), so call it during setup, before the animation starts. Terrain in a scene without water builds nothing extra: its memory and build time are what they were. Pass `mirrorLevels` to choose the count yourself; `mirrorLevels: 0` keeps the plain terrain even next to water, and a number you set is built when the terrain is created, as before. Measured on a 900 m terrain at resolution 128 with 8 by 8 chunks: the levels add about 22 MB of mesh data (the finest level is 61 MB). In return the mirror frame of the water benchmark (960 by 540, RTX 3060, median of 240 samples) costs +25% over reflection off instead of +40%.## Heightfield from rastersdiff --git a/src/sun-bounce.js b/src/sun-bounce.jsindex de2e6ef5..fe3d7068 100644--- a/src/sun-bounce.js+++ b/src/sun-bounce.js@@ -4,6 +4,7 @@ import { CLOSED_ROOM_SHARE, partMatrixLookup } from './closed-room.js';import { fittedClosedRoomShare } from './fitted-closed-room.js';import { partDiffuseAlbedo } from './ground-bounce.js';import { MAX_POINT_LIGHTS } from './light-limits.js';+import { requireFiniteInRange } from './modeling-limits.js';// Light a sunlit patch sends on to the rest of a closed room. The patches are found once, when the scene is prepared: surfaces// of the room are sampled, the samples the sun reaches (a shadow ray) and that sit inside the room (most of a short cosine ray@@ -31,6 +32,8 @@ export const DEFAULT_SUN_BOUNCE = 1;// A light closer than this share of the scene to a surface blows up as 1/d^2 into a hard speck, so a patch with less free space in front is left out.const MINIMUM_LIFT_SHARE = 0.03;const KEY_THRESHOLD = 1e-3;+// A light's intensity is capped at this, so a larger strength only blows the frame out.+const MAXIMUM_STRENGTH = 1e5;const EXCLUDED_TAGS = new Set(['liquid', 'foam', 'unlit', 'fire', 'smoke']);function isBounceSurface(part) {@@ -192,6 +195,7 @@ function lightOfPatch(group, strength, tracer, probeOffset, sceneSize) {* `maximumLights` caps how many patches there are.*/export function findSunBounceLights(parts, environment, { strength = 1, maximumLights = SUN_BOUNCE_PATCH_LIGHTS } = {}) {+ requireFiniteInRange('findSunBounceLights', 'strength', strength, 0, MAXIMUM_STRENGTH);if (strength <= 0 || maximumLights <= 0 || !hasSun(environment) || parts.some(part => part.material?.lightMap || part.material?.irradianceGrid)) return [];const share = fittedClosedRoomShare(parts, partMatrixLookup(parts));if (share === null || share < CLOSED_ROOM_SHARE) return [];@@ -216,7 +220,7 @@ const lightsOfParts = new WeakMap();* them; 0 returns the environment itself. The patches are found once for these parts and this sun and kept.*/export function withSunBounce(parts, environment) {- const strength = environment.sunBounce ?? DEFAULT_SUN_BOUNCE;+ const strength = requireFiniteInRange('withSunBounce', 'environment.sunBounce', environment.sunBounce ?? DEFAULT_SUN_BOUNCE, 0, MAXIMUM_STRENGTH);if (strength <= 0 || !hasSun(environment)) return environment;const existing = environment.pointLights ?? [];const room = Math.min(SUN_BOUNCE_PATCH_LIGHTS, MAX_POINT_LIGHTS - existing.length);diff --git a/src/water-reflection.js b/src/water-reflection.jsindex 7b21b144..01ae13c6 100644--- a/src/water-reflection.js+++ b/src/water-reflection.js@@ -1,5 +1,6 @@import { layerTextureSupport, liquidWaterSupport, registerShaderFeatureCode } from './shader-registry.js';import { worldBounds } from './probe-trace.js';+import { requireFiniteInRange } from './modeling-limits.js';import { WATER_MAXIMUM_ROUGHNESS, WATER_BLUR_LIMIT_ROUGHNESS, WATER_MAXIMUM_BLUR_LEVEL, WATER_FADE_START_ROUGHNESS, waterMirrorUniformsGlsl, waterMirrorFunctionsGlsl } from './water-mirror-glsl.js';export { WATER_MAXIMUM_ROUGHNESS, WATER_BLUR_LIMIT_ROUGHNESS, WATER_MAXIMUM_BLUR_LEVEL, WATER_FADE_START_ROUGHNESS };@@ -373,6 +374,8 @@ function hideMirrorOnlyLevels(batch) {}export function attachWaterReflection(renderer, surface, { scale = WATER_REFLECTION_SCALE, updateEvery = 1 } = {}) {+ requireFiniteInRange('attachWaterReflection', 'scale', scale, 0.05, 1);+ requireFiniteInRange('attachWaterReflection', 'updateEvery', updateEvery, 0, 1000);if (!surface) {return () => {};}diff --git a/tests/sun-bounce.test.js b/tests/sun-bounce.test.jsindex cafb8cf2..0bffec11 100644--- a/tests/sun-bounce.test.js+++ b/tests/sun-bounce.test.js@@ -91,3 +91,13 @@ test('environment.sunBounce that is not a number of 0 or more is refused', () =>}assert.equal(createAgentScene(roomWithOpening({ sunBounce: 2 })).environment.sunBounce, 2);});++test('a sun bounce strength that is not a finite number refuses instead of making NaN lights', () => {+ const { parts, environment } = createAgentScene(roomWithOpening());+ for (const sunBounce of [NaN, Infinity, 'strong']) {+ assert.throws(() => withSunBounce(parts, { ...environment, sunBounce }), /environment\.sunBounce was .*pass a number from 0 to 100000/);+ }+ assert.throws(() => findSunBounceLights(parts, environment, { strength: NaN }), /strength was NaN/);+ const lights = withSunBounce(parts, { ...environment, sunBounce: 2 }).pointLights;+ assert.ok(lights.length > 0 && lights.every(light => Number.isFinite(light.intensity)));+});diff --git a/tests/water-reflection.test.js b/tests/water-reflection.test.jsindex 396b2e36..6ce7f68d 100644--- a/tests/water-reflection.test.js+++ b/tests/water-reflection.test.js@@ -406,3 +406,15 @@ test('the liquid shader reads the mirror picture the lit shader reads, and clipsassert.match(liquidFragmentShader, /if \(waterClip\.y > 0\.5 && worldPosition\.y < waterClip\.x/);assert.ok(createFragmentShader(['studioLighting', 'waterReflection']).includes('vec3 waterMirrorColor(vec3 position, float roughness)'));});++test('attachWaterReflection refuses a mirror scale or update interval that would size or stall the target wrongly', () => {+ const renderer = { render() {}, dispose() {} };+ const surface = findWaterSurface(lakeScene());+ for (const scale of [NaN, 0, 1000, Infinity]) {+ assert.throws(() => attachWaterReflection(renderer, surface, { scale }), /scale was .*pass a number from 0.05 to 1/);+ }+ for (const updateEvery of [NaN, Infinity, -1]) {+ assert.throws(() => attachWaterReflection(renderer, surface, { updateEvery }), /updateEvery was .*pass a number from 0 to 1000/);+ }+ attachWaterReflection(renderer, surface, { scale: 0.5, updateEvery: 2 })();+});