Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Cache the low-sun fill with the ground map and test it (#351)

Nguyen Xuan Anc0af5b5aeeca
diff --git a/changes/351.size.md b/changes/351.size.md
index 54344434..5fb16e55 100644
--- a/changes/351.size.md
+++ b/changes/351.size.md
@@ -5,3 +5,4 @@
- scene:gltf-box+lazy: carries the renderer method above.
- scene:agent-scene: carries the renderer method above and the second `scene_inspect` message for a closed room lit by lamps only, which names the bake preset.
- scene:agent-scene+lazy: same as scene:agent-scene.
+- entry:ground-bounce: the low-sun fill (a constant bounce colour from the sunlit slopes and walls around a shadow) lives in the ground-bounce add-on, next to the floor and room bounce; core and lit-box do not grow.
diff --git a/src/ground-bounce.js b/src/ground-bounce.js
index ee9e1afe..2971ec38 100644
--- a/src/ground-bounce.js
+++ b/src/ground-bounce.js
@@ -320,9 +320,10 @@ function isOpenAir(parts) {
return openAir
}
-function outdoorFillEnvironment(parts, environment, ground) {
+// The fill as a `bounce` colour, or null when the scene gets none.
+function outdoorFillBounce(parts, environment, ground) {
if (environment.outdoorFill === false || !environment.keyColor || !hasKeyLight(environment) || !isOpenAir(parts)) {
- return environment
+ return null
}
const { lightDirection } = environment
const sunLength = Math.hypot(...lightDirection)
@@ -330,11 +331,10 @@ function outdoorFillEnvironment(parts, environment, ground) {
const sunOnWall = Math.hypot(lightDirection[0], lightDirection[2]) / sunLength
const lowSun = Math.max(sunOnWall - sunOnGround, 0) ** 2
if (lowSun === 0) {
- return environment
+ return null
}
const albedo = (ground?.albedo ?? [0, 0, 0]).map(channel => Math.max(channel, OUTDOOR_MINIMUM_ALBEDO))
- const bounce = albedo.map((channel, index) => (OUTDOOR_FILL_GAIN * channel * environment.keyColor[index] * (environment.keyIntensity ?? 1) * lowSun) / Math.PI)
- return { ...environment, map: { ...environment.map, bounce } }
+ return albedo.map((channel, index) => (OUTDOOR_FILL_GAIN * channel * environment.keyColor[index] * (environment.keyIntensity ?? 1) * lowSun) / Math.PI)
}
/**
@@ -355,17 +355,18 @@ export function groundBounceEnvironment(parts, environment, { automaticAlbedo =
return environment
}
const ground = groundOf(parts, automaticAlbedo)
- const fill = outdoorFillEnvironment(parts, environment, ground)
+ const fill = outdoorFillBounce(parts, environment, ground)
if (!ground) {
- return fill
+ return fill ? { ...environment, map: { ...environment.map, bounce: fill } } : environment
}
const { lightDirection } = environment
const keyColor = environment.keyColor.map(channel => channel * (environment.keyIntensity ?? 1))
- const key = JSON.stringify([ground.albedo, keyColor, lightDirection])
+ const key = JSON.stringify([ground.albedo, keyColor, lightDirection, fill])
const byInputs = cache.get(environment.map) ?? new Map()
cache.set(environment.map, byInputs)
if (!byInputs.has(key)) {
- byInputs.set(key, withGroundBounce(environment.map, { albedo: ground.albedo, keyColor, lightDirection }))
+ const bounced = withGroundBounce(environment.map, { albedo: ground.albedo, keyColor, lightDirection })
+ byInputs.set(key, fill ? { ...bounced, bounce: fill } : bounced)
}
- return { ...fill, map: { ...byInputs.get(key), ...(fill.map.bounce && { bounce: fill.map.bounce }) } }
+ return { ...environment, map: byInputs.get(key) }
}
diff --git a/tests/ground-bounce.test.js b/tests/ground-bounce.test.js
index 34abf761..dfd9e500 100644
--- a/tests/ground-bounce.test.js
+++ b/tests/ground-bounce.test.js
@@ -143,3 +143,17 @@ test('the mean of a texture is found once per image', () => {
map.image.data.fill(0);
assert.ok(findGroundSurface(openScene({ ...GRASS, map })).albedo[0] > 0.1);
});
+
+test('a low sun adds a bounce colour to the map, a high sun, no sun or outdoorFill false add none', () => {
+ const parts = openScene();
+ const lowSun = { map: uniformSky(0.5), keyColor: [3, 3, 3], lightDirection: [-5, 1, 4] };
+ const filled = groundBounceEnvironment(parts, lowSun);
+ assert.ok(filled.map.irradianceSource);
+ assert.ok(filled.map.bounce.every(channel => channel > 0));
+ assert.equal(groundBounceEnvironment(parts, lowSun).map, filled.map);
+ assert.equal(groundBounceEnvironment(parts, environmentOf(uniformSky(0.5))).map.bounce, undefined);
+ assert.equal(groundBounceEnvironment(parts, { ...lowSun, keyColor: [0, 0, 0] }).map.bounce, undefined);
+ assert.equal(groundBounceEnvironment(parts, { ...lowSun, outdoorFill: false }).map.bounce, undefined);
+ const withoutFloor = groundBounceEnvironment(parts.slice(1), lowSun);
+ assert.ok(withoutFloor.map.bounce.every(channel => channel > 0));
+});