Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Water look round 4: haze under the horizon, warped ripple phases, narrower glint widening

Nguyen Xuan An927d59e2d436
diff --git a/changes/819.md b/changes/819.md
index 7768cabb..deb6d2e2 100644
--- a/changes/819.md
+++ b/changes/819.md
@@ -1 +1 @@
-- The `water` tag reads the sea's foam as lace (a bright core that breaks into veins, soft and antialiased edges), mirrors the sky with a fresnel term over a deep blue-green body, breaks the sun glint into facets with eight non-repeating wind ripple bands, and an `ocean` mesh smooths over 180 degrees by default; the physical sky draws its sun as a round disc on a 1024 by 512 map (#819)
+- The `water` tag reads the sea's foam as lace (a bright core that breaks into veins, soft and antialiased edges), mirrors the sky with a fresnel term over a deep blue-green body, breaks the sun glint into facets with eight wind ripple bands with warped phases, and an `ocean` mesh smooths over 180 degrees by default; the physical sky draws its sun as a round disc on a 1024 by 512 map and fades the ground into haze over the first 7 degrees below the horizon, so a finite sea ends in haze, not a tan band (#819)
diff --git a/changes/819.size.md b/changes/819.size.md
index 901dd55f..b517ea27 100644
--- a/changes/819.size.md
+++ b/changes/819.size.md
@@ -7,3 +7,4 @@
- entry:nature: same shared world-material block as entry:terrain, now with eight-band ripple detail and lace foam (about 700 bytes gzip more); it is what turns the glint into facets and the foam into streaks in #819.
- entry:vegetation: same shared world-material block as entry:terrain.
- entry:physical-sky: the sky map grows from 128 by 64 to 1024 by 512 texels and a soft-rimmed disc profile, so the sun is a round disc instead of the texel grid (#819).
+- entry:physical-sky: the haze blend below the horizon is a few lines of map code, about 100 bytes gzip; it removes the tan band between a finite sea's edge and the horizon.
diff --git a/src/physical-sky.js b/src/physical-sky.js
index e618ba59..704e7ddd 100644
--- a/src/physical-sky.js
+++ b/src/physical-sky.js
@@ -157,6 +157,18 @@ function sunDiscWeight(sunCosine) {
return edge * edge * (3 - 2 * edge) * SUN_DISC_STRENGTH;
}
+// Haze fills the first degrees under the horizon, so a finite sea or plane whose far edge ends short of the true horizon meets
+// haze the colour of the sky, not a flat band of ground colour with a hard step at the horizon line.
+const HORIZON_HAZE_SINE = 0.12;
+
+function belowHorizonRadiance(sky, color, zenith, sun, direction) {
+ const horizonSky = skyRadiance(sky, [direction[0], 0, direction[2]], sun);
+ const ground = groundRadiance(sky, color, zenith, sun).map((component, channel) => component + horizonSky[channel] * 0.35 * (1 - sky.groundAlbedo));
+ const depth = Math.min(-direction[1] / HORIZON_HAZE_SINE, 1);
+ const groundShare = depth * depth * (3 - 2 * depth);
+ return ground.map((component, channel) => horizonSky[channel] + (component - horizonSky[channel]) * groundShare);
+}
+
function buildMapData(sky) {
const { sun, color, zenith } = colorAtZenithAndHorizon(sky);
const drawDisc = sky.sunDisc !== false && sky.sunElevation > -1.5;
@@ -172,7 +184,7 @@ function buildMapData(sky) {
: 0;
const radiance = direction[1] >= 0
? skyRadiance(sky, direction, sun).map((component, channel) => component + color[channel] * discWeight)
- : groundRadiance(sky, color, zenith, sun).map((component, channel) => component + skyRadiance(sky, [direction[0], 0, direction[2]], sun)[channel] * 0.35 * (1 - sky.groundAlbedo));
+ : belowHorizonRadiance(sky, color, zenith, sun, direction);
data.set(radiance, (row * MAP_WIDTH + column) * 3);
}
}
diff --git a/src/world-material-shader.js b/src/world-material-shader.js
index 4c5940ae..f7dddbf8 100644
--- a/src/world-material-shader.js
+++ b/src/world-material-shader.js
@@ -330,16 +330,16 @@ vec2 waterDetailSlope(vec2 position, float footprint, out float lostVariance) {
float wavenumber = 2.1;
float angle = 0.4;
for (int index = 0; index < 8; index++) {
- float visible = 1.0 - smoothstep(0.08, 0.25, footprint * wavenumber / 6.2831853);
+ float visible = 1.0 - smoothstep(0.06, 0.2, footprint * wavenumber / 6.2831853);
vec2 direction = vec2(cos(angle), sin(angle));
- float drift = terrainNoise(position * 0.21 + float(index) * 9.17).x;
- float strength = 0.35 + 1.3 * terrainNoise(position * 0.13 + float(index) * 23.9 + 4.0).x;
- float phase = dot(position, direction) * wavenumber + time * sqrt(9.81 * wavenumber) + float(index) * 1.7 + drift * 0.5;
+ float drift = terrainNoise(position * wavenumber * 0.09 + float(index) * 9.17).x - 0.5;
+ float strength = 0.15 + 1.7 * terrainNoise(position * wavenumber * 0.06 + float(index) * 23.9 + 4.0).x;
+ float phase = dot(position, direction) * wavenumber + time * sqrt(9.81 * wavenumber) + float(index) * 1.7 + drift * 5.0;
float amplitude = 0.085 * pow(wavenumber / 2.1, -0.3) * strength;
slope += direction * cos(phase) * amplitude * visible;
lostVariance += 0.5 * amplitude * amplitude * (1.0 - visible);
wavenumber *= 1.7320508;
- angle += 2.399963 + 0.08 * drift;
+ angle += 2.399963;
}
return slope;
}
@@ -458,7 +458,7 @@ void mossMasks(vec3 normal, float amount, out float sharpMask, out float softMas
radiance = mix(radiance * 0.6 + color * ambientIntensity * daylight * 1.7, skyColor * ambientIntensity, clamp(reflectivity, 0.0, 1.0));
// Sun glint: a narrow lobe around the mirror direction, the width of open water slopes, bright enough to bloom.
vec3 halfVector = normalize(normalize(lightDirection) + viewDirection);
- float glintSharpness = 1.0 / (1.0 / 500.0 + waterLostVariance);
+ float glintSharpness = 1.0 / (1.0 / 500.0 + 0.35 * waterLostVariance);
float glintShape = pow(max(dot(normal, halfVector), 0.0), glintSharpness);
float glintFresnel = 0.02 + 0.98 * pow(1.0 - max(dot(halfVector, viewDirection), 0.0), 5.0);
radiance += vec3(1.0, 0.9, 0.7) * glintShape * (0.5 + 40.0 * glintFresnel) * 6.0 * keyResponse * (1.0 - waterFoam);
diff --git a/tests/physical-sky.test.js b/tests/physical-sky.test.js
index 9702a962..872f1f8d 100644
--- a/tests/physical-sky.test.js
+++ b/tests/physical-sky.test.js
@@ -118,3 +118,15 @@ test('environment motion tracks animate physical sky sunElevation, sunRotation a
motion: { duration: 1, tracks: [{ kind: 'environment', property: 'sunElevation', keyframes: [{ time: 0, value: 45 }] }] },
}), /environment\.sky/);
});
+
+test('the sky map has no step at the horizon: haze fills the first degrees below it', () => {
+ const map = agentEnvironmentMap(skyEnvironment({ sunElevation: 9, turbidity: 2 }));
+ const column = Math.floor(map.width / 2);
+ const radianceAt = row => map.data[(row * map.width + column) * 3 + 1];
+ const horizonRow = map.height / 2;
+ const above = radianceAt(horizonRow - 1);
+ const below = radianceAt(horizonRow);
+ assert.ok(Math.abs(above - below) / above < 0.03, `radiance steps from ${above} to ${below} across the horizon`);
+ const deepBelow = radianceAt(horizonRow + Math.round(map.height / 12));
+ assert.ok(Math.abs(deepBelow - below) / below > 0.1, 'well below the horizon the ground colour takes over');
+});
diff --git a/tests/water-ocean-look.test.js b/tests/water-ocean-look.test.js
index aaa8498a..b80e77ac 100644
--- a/tests/water-ocean-look.test.js
+++ b/tests/water-ocean-look.test.js
@@ -69,5 +69,7 @@ test('wind ripples break the glint into facets and the foam into streaks', () =>
assert.match(shader, /vec2 waterDetailSlope\(/);
assert.match(shader, /float foamBand = 0\.13 \+ 1\.5 \* fwidth\(foamDensity\)/);
assert.match(shader, /rippleTilt\.xz \+= detailSlope/);
+ assert.match(shader, /terrainNoise\(position \* wavenumber \* 0\.09/);
+ assert.match(shader, /1\.0 \/ \(1\.0 \/ 500\.0 \+ 0\.35 \* waterLostVariance\)/);
assert.match(shader, /terrainNoise\(crestPosition \* vec2\(3\.1, 14\.0\)/);
});