3D engine that AI agents drive
Fill sun shadows under a low sun with bounced light from the surroundings (#351)
Nguyen Xuan Anf85338504120
diff --git a/bench/reference/__pycache__/luminance-ratio.cpython-312.pyc b/bench/reference/__pycache__/luminance-ratio.cpython-312.pycnew file mode 100644index 00000000..3c9b3ac1Binary files /dev/null and b/bench/reference/__pycache__/luminance-ratio.cpython-312.pyc differdiff --git a/changes/351.md b/changes/351.mdindex d8cdd481..a3faba43 100644--- a/changes/351.md+++ b/changes/351.md@@ -1 +1,2 @@- Add a bounced-light estimate for a closed room lit by point, spot or area lights without a bake (issue #351). `groundBounceEnvironment` gives the environment map a `bounce` colour, flux * rho / (pi * area * (1 - rho)) per colour channel, which the renderer adds to the diffuse light at no cost per frame; `environment.roomBounce: false` turns it off. A room with a sun is left alone. `scene_inspect` says so when a closed room is lit by lamps only and names the bake preset.+- Under a low sun (sun elevation near the horizon) the environment map also gets a `bounce` colour from the sunlit slopes and walls around a shadow: `0.4 * albedo * sun / pi * (wall - ground)^2`, free per frame. `mountain-valley` and `megacity-dusk` shadow p20 against Cycles go from 0.60 and 0.59 to 0.79 and 0.85 with a better FLIP; scenes with a high sun, `temple-hall` and `dark-shaft` do not change. `environment.outdoorFill: false` turns it off. `bench/reference/render-gpu.mjs` takes `--cpu` for SwiftShader.diff --git a/docs/lighting.md b/docs/lighting.mdindex eaec7d7d..cc5029c7 100644--- a/docs/lighting.md+++ b/docs/lighting.md@@ -126,7 +126,22 @@ Luminance of the 67js picture over Cycles (linear, whole frame and 20th percenti| `brick-room` | 0.57 | 0.86 | 0.37 | 0.95 || `temple-hall` | 0.97 | 0.97 | 1.62 | 1.64 |-The two `temple-hall` p20 values differ only by the rescaling of the picture (the scene has a sun, so no bounce is added). The outdoor shadow fill of the issue is not done.+The two `temple-hall` p20 values differ only by the rescaling of the picture (the scene has a sun, so no bounce is added).++## Sun shadows fill under a low sun++A scene with a sun that is not a closed room or a bake gets one more constant diffuse light under a low sun, the same `bounce` colour the room uses, so it costs nothing per frame and changes no shader. Slopes, walls and roofs that face a low sun send its light back into the shadows, and the ground term does not see them (`mountain-valley` has no level ground at all). The fill is `0.4 * albedo * sun colour / pi * (wall - ground)^2`, where `ground` is the sun's vertical share on a level surface, `wall` its horizontal share on a vertical one and `albedo` the floor's (at least 0.2). A high sun gives zero, so the studio scenes and `long-street` are unchanged, and so are `temple-hall` and `dark-shaft`. `environment.outdoorFill: false` turns it off.++Measured on SwiftShader against the committed Cycles panels (960 x 540 picture scaled to the panel, FLIP of the panel pair):++| Scene | Mean before | Mean after | p20 before | p20 after | FLIP before | FLIP after |+| --- | --- | --- | --- | --- | --- | --- |+| `mountain-valley` | 0.90 | 0.99 | 0.60 | 0.79 | 0.1699 | 0.1259 |+| `megacity-dusk` | 0.93 | 0.97 | 0.59 | 0.85 | 0.1686 | 0.1431 |+| `fog-valley` | 1.07 | 1.09 | 0.86 | 0.92 | 0.1906 | 0.1810 |+| `temple-hall`, `dark-shaft` | | | | unchanged | | unchanged |++The other 19 reference scenes drew the same pixels (mean, p20 and FLIP identical).## Local shadowsdiff --git a/src/ground-bounce.js b/src/ground-bounce.jsindex a4db1654..ee9e1afe 100644--- a/src/ground-bounce.js+++ b/src/ground-bounce.js@@ -298,6 +298,45 @@ function hasLocalLights(environment) {return [environment.pointLights, environment.spotLights, environment.areaLights].some(lights => lights?.length > 0)}+// Under a low sun a shadow is lit by more than the sky and the level ground: slopes, walls and roofs that face the sun send+// it back too, and the ground term above does not see them (`mountain-valley` has no level ground at all). The sky's own light+// is already in the map. The fill is a constant diffuse light, a `bounce` colour like the room's: the sun on a surface of the+// ground's albedo (at least 0.2) over pi, times a fitted gain, and times how much more a vertical face gets than the ground,+// squared: nothing for a high sun, whose ground bounce is the whole story (the studio scenes, `long-street`), most for a sun+// near the horizon (`mountain-valley`, `megacity-dusk`). A closed room, a bake or no sun gets none, so `temple-hall` and+// `dark-shaft` keep their shadows.+const OUTDOOR_FILL_GAIN = 0.4+const OUTDOOR_MINIMUM_ALBEDO = 0.2++const openAirOfParts = new WeakMap()++function isOpenAir(parts) {+ const remembered = openAirOfParts.get(parts)+ if (remembered?.length === parts.length) {+ return remembered.openAir+ }+ const openAir = !isDrawnWithBakedLight(parts) && !isClosedRoom(parts)+ openAirOfParts.set(parts, { length: parts.length, openAir })+ return openAir+}++function outdoorFillEnvironment(parts, environment, ground) {+ if (environment.outdoorFill === false || !environment.keyColor || !hasKeyLight(environment) || !isOpenAir(parts)) {+ return environment+ }+ const { lightDirection } = environment+ const sunLength = Math.hypot(...lightDirection)+ const sunOnGround = Math.max(lightDirection[1] / sunLength, 0)+ const sunOnWall = Math.hypot(lightDirection[0], lightDirection[2]) / sunLength+ const lowSun = Math.max(sunOnWall - sunOnGround, 0) ** 2+ if (lowSun === 0) {+ return environment+ }+ 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 } }+}+/*** The environment with the ground's bounce in its map, found from the parts: the same environment when the scene has no* plain floor, a baked part (its light map already holds the bounce), a closed room (a bake or the room's own walls@@ -316,8 +355,9 @@ export function groundBounceEnvironment(parts, environment, { automaticAlbedo =return environment}const ground = groundOf(parts, automaticAlbedo)+ const fill = outdoorFillEnvironment(parts, environment, ground)if (!ground) {- return environment+ return fill}const { lightDirection } = environmentconst keyColor = environment.keyColor.map(channel => channel * (environment.keyIntensity ?? 1))@@ -327,5 +367,5 @@ export function groundBounceEnvironment(parts, environment, { automaticAlbedo =if (!byInputs.has(key)) {byInputs.set(key, withGroundBounce(environment.map, { albedo: ground.albedo, keyColor, lightDirection }))}- return { ...environment, map: byInputs.get(key) }+ return { ...fill, map: { ...byInputs.get(key), ...(fill.map.bounce && { bounce: fill.map.bounce }) } }}