Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Keep the refraction blur from fading the bed edge into haze (part of #349)

Nguyen Xuan An947b8fda8e43
diff --git a/changes/349.md b/changes/349.md
index 599cb8f8..db8d08f4 100644
--- a/changes/349.md
+++ b/changes/349.md
@@ -1 +1 @@
-- Water shadows follow the water (issue #349). A sun shadow on a `water` surface is faint and never darkens the reflection, and everything below the water level fades by `exp(-absorption * depth)` per colour, with shadows lifted and softened by depth times `turbidity`. Recipes take the `water` material tag with `waterPreset` (`lake`, `sea`, `river`), `absorption` (RGB per metre) and `turbidity` (0 to 1), listed in `scene_capabilities` under `water`. Blended water (opacity below one) refracts what lies below it (n = 1.33, bent by the waves) and blurs it with depth times turbidity, and submerged objects fade into the water colour. The code sits in the world material addon, so a renderer without water does not carry it, and the MCP tool count is unchanged.
+- Water shadows follow the water (issue #349). A sun shadow on a `water` surface is faint and never darkens the reflection, and everything below the water level fades by `exp(-absorption * depth)` per colour, with shadows lifted and softened by depth times `turbidity`. Recipes take the `water` material tag with `waterPreset` (`lake`, `sea`, `river`), `absorption` (RGB per metre) and `turbidity` (0 to 1), listed in `scene_capabilities` under `water`. Blended water (opacity below one) refracts what lies below it (n = 1.33, bent by the waves) and blurs it with depth times turbidity (only over what lies below the water, so the edge of a bed stays sharp instead of fading into a haze), and submerged objects fade into the water colour. The code sits in the world material addon, so a renderer without water does not carry it, and the MCP tool count is unchanged.
diff --git a/src/world-material-shader.js b/src/world-material-shader.js
index 82c5a6ea..a921eeb1 100644
--- a/src/world-material-shader.js
+++ b/src/world-material-shader.js
@@ -197,7 +197,8 @@ vec3 refractedBackground(vec3 fallback, vec3 surfacePosition, vec3 normal, vec3
seenColor += waterDecode(sampled.rgb) * below;
seen += below;
}
- return mix(fallback, seenColor / max(seen, 1.0), seen / 16.0);
+ // Average only the taps that saw something below the water: a tap past the bed edge must not pull the blur toward the water colour.
+ return seen > 0.5 ? seenColor / seen : fallback;
}
#ifndef shadowVisibility
float softShadowVisibility(vec3 normal, float radiusTexels) {
diff --git a/tests/water-shadows.test.js b/tests/water-shadows.test.js
index 610286ac..ebbc056e 100644
--- a/tests/water-shadows.test.js
+++ b/tests/water-shadows.test.js
@@ -58,3 +58,9 @@ test('the shader keeps the sun shadow off the reflection and applies the column
assert.ok(source.includes('exp(-waterAbsorption * waterDepth)'));
assert.ok(!createFragmentShader([]).includes('waterBody'));
});
+
+test('the refraction blur averages only the taps that saw the bed, so the bed edge does not fade into a haze', () => {
+ const source = createFragmentShader(['water']);
+ assert.ok(source.includes('return seen > 0.5 ? seenColor / seen : fallback;'));
+ assert.ok(!source.includes('seen / 16.0'), 'a share of missed taps must not mix the water colour into the blur');
+});