Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Light the face of a leaf card the viewer sees (part of #394)

Nguyen Xuan Ance5889d9c94b
diff --git a/changes/394.md b/changes/394.md
index e20a68bd..48a5cec1 100644
--- a/changes/394.md
+++ b/changes/394.md
@@ -1 +1,2 @@
- Lower tree levels no longer draw near black under sky light: levels 1 to 3 read the sky visibility field measured on level 0 and keep its mean, so every level sees about as much sky as level 0 (it was 0.04 to 0.06 against 0.08 to 0.12). Part of #394.
+- Leaves and grass blades now let sun through to the face you see. Before, a tree's flat lower-level cards seen from below took transmitted light from the wrong side and lost the rest to their own shadow, so they read dark under the sun against level 0 (about 0.3 to 0.6 of its brightness on the leaf pixels, now about 0.5 to 0.9). Part of #394.
diff --git a/src/world-material-shader.js b/src/world-material-shader.js
index 45fd4277..91c98012 100644
--- a/src/world-material-shader.js
+++ b/src/world-material-shader.js
@@ -367,7 +367,7 @@ void mossMasks(vec3 normal, float amount, out float sharpMask, out float softMas
metallic = mix(metallic, 0.0, mossCover);
}
`,
- worldLight: ` if (CANOPY_TAG) radiance += color * max(dot(-normal, normalize(lightDirection)), 0.0) * 0.3 * keyResponse * visibility;
+ worldLight: ` if (CANOPY_TAG) radiance += color * max(dot(gl_FrontFacing ? -normal : normal, normalize(lightDirection)), 0.0) * 0.5 * keyResponse * mix(visibility, 1.0, 0.7);
if (mossCover > 0.001) {
float sheen = pow(1.0 - max(dot(normal, viewDirection), 0.0), 4.0);
float wrap = max(dot(normal, normalize(lightDirection)) + 0.5, 0.0) / 1.5;
diff --git a/tests/foliage-transmission.test.js b/tests/foliage-transmission.test.js
new file mode 100644
index 00000000..c9d21a08
--- /dev/null
+++ b/tests/foliage-transmission.test.js
@@ -0,0 +1,17 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+import { worldMaterialSlots } from '../src/world-material-shader.js';
+
+// Issue #394: the lower tree levels are a few big, flat cards. Seen from below, a card shows its back face, and light
+// from above reaches it only by passing through the leaf. The transmission term must follow the face the viewer sees,
+// and the shadow test must not remove it entirely, because the shadow on a card's underside is the card's own.
+
+test('foliage transmission lights the face the viewer sees', () => {
+ const { worldLight } = worldMaterialSlots;
+ assert.match(worldLight, /CANOPY_TAG\) radiance \+= .*gl_FrontFacing \? -normal : normal/);
+});
+
+test('foliage transmission keeps most of its light under a shadow', () => {
+ const { worldLight } = worldMaterialSlots;
+ assert.match(worldLight, /mix\(visibility, 1\.0, 0\.7\)/);
+});