3D engine that AI agents drive
Water look round 3: lacy antialiased foam, blue-green body, round sun disc, non-repeating ripple bands
Nguyen Xuan An02dcf302e374
diff --git a/changes/819.md b/changes/819.mdindex 542d610b..7768cabb 100644--- a/changes/819.md+++ b/changes/819.md@@ -1 +1 @@-- The `water` tag reads the sea's foam as streaks along the crests, mirrors the sky with a fresnel term, breaks the sun glint into facets with wind ripples and goes deeper at grazing angles, and an `ocean` mesh smooths over 180 degrees by default (#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 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)diff --git a/changes/819.size.md b/changes/819.size.mdindex 1131fb05..901dd55f 100644--- a/changes/819.size.md+++ b/changes/819.size.md@@ -4,5 +4,6 @@- entry:world-material-shader: fresnel sky reflection, a narrow sun glint, a grazing-angle body colour and foam are the look #819 asks for; 230 bytes gzip of GLSL.- scene:agent-scene+lazy: the shader block plus the foam and smoothAngle helpers in the lazy mesh chunk (414 bytes gzip).- goal:agent-scene: 24 bytes gzip, one guarded call that hands a water part its foam; the foam helper itself lives in the lazy mesh chunk.-- entry:nature: same shared world-material block as entry:terrain, now with six-wave ripple detail and streaked foam (about 400 bytes gzip more); it is what turns the glint into facets and the foam into streaks in #819.+- 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).diff --git a/src/physical-sky.js b/src/physical-sky.jsindex 31bacd09..e618ba59 100644--- a/src/physical-sky.js+++ b/src/physical-sky.js@@ -17,8 +17,8 @@ const LIMITS = Object.freeze({ozone: [0, 10, 'ozone absorption from 0 (none) to 10, such as 1'],});-const MAP_WIDTH = 128;-const MAP_HEIGHT = 64;+const MAP_WIDTH = 1024;+const MAP_HEIGHT = 512;// Per-channel (red, green, blue) optical depth of the air at the zenith, the aerosol depth per unit turbidity, and the// Chappuis-band ozone absorption depth per unit ozone (strong in orange-green, weak in blue).const RAYLEIGH_DEPTH = [0.04, 0.1, 0.24];@@ -147,6 +147,16 @@ export function applyPhysicalSky(input, environment, fail) {const mapsByKey = new Map();const MAXIMUM_CACHED_MAPS = 8;+/**+ * A round disc with a flat bright top and a soft rim, by the angle from the sun's centre. A squared falloff over a map of a few+ * degrees per texel drew the sun as the texel grid (a rounded rectangle); the finer map and this profile keep it a circle.+ */+function sunDiscWeight(sunCosine) {+ const rim = (sunCosine - SUN_DISC_COSINE) / (1 - SUN_DISC_COSINE);+ const edge = Math.min(Math.max(rim / 0.64, 0), 1);+ return edge * edge * (3 - 2 * edge) * SUN_DISC_STRENGTH;+}+function buildMapData(sky) {const { sun, color, zenith } = colorAtZenithAndHorizon(sky);const drawDisc = sky.sunDisc !== false && sky.sunElevation > -1.5;@@ -158,7 +168,7 @@ function buildMapData(sky) {const direction = [Math.sin(polar) * Math.sin(azimuth), Math.cos(polar), -Math.sin(polar) * Math.cos(azimuth)];const sunCosine = direction[0] * sun[0] + direction[1] * sun[1] + direction[2] * sun[2];const discWeight = drawDisc && direction[1] > 0 && sunCosine > SUN_DISC_COSINE- ? ((sunCosine - SUN_DISC_COSINE) / (1 - SUN_DISC_COSINE)) ** 2 * SUN_DISC_STRENGTH+ ? sunDiscWeight(sunCosine): 0;const radiance = direction[1] >= 0? skyRadiance(sky, direction, sun).map((component, channel) => component + color[channel] * discWeight)diff --git a/src/world-material-shader.js b/src/world-material-shader.jsindex 4f41fab4..4c5940ae 100644--- a/src/world-material-shader.js+++ b/src/world-material-shader.js@@ -319,22 +319,27 @@ vec3 terrainNoise(vec2 position) {(upper - lower) * derivative.y);}-// Wind ripples the mesh is too coarse to carry: six travelling waves from 2.6 m down to 0.2 m, about 0.12 rms slope in all, which-// is what breaks a sun glint into facets. A wave fades out once a pixel is wider than a quarter of its length, so far water does-// not shimmer; the slope variance the faded waves took with them comes back as lostVariance, to widen the glint instead.+// Wind ripples the mesh is too coarse to carry: eight travelling waves from 3 m down to 0.15 m, about 0.15 rms slope in all, which+// is what breaks a sun glint into facets. The wavelengths step by an irrational ratio and the directions by the golden angle, and+// each wave's phase and strength drift with slow noise, so no pair of waves repeats the same blob pattern over the water. A wave+// fades out once a pixel is wider than a quarter of its length, so far water does not shimmer; the slope variance the faded waves+// took with them comes back as lostVariance, to widen the glint instead.vec2 waterDetailSlope(vec2 position, float footprint, out float lostVariance) {vec2 slope = vec2(0.0);lostVariance = 0.0;- float wavenumber = 2.4;+ float wavenumber = 2.1;float angle = 0.4;- for (int index = 0; index < 6; index++) {+ for (int index = 0; index < 8; index++) {float visible = 1.0 - smoothstep(0.08, 0.25, footprint * wavenumber / 6.2831853);- float phase = dot(position, vec2(cos(angle), sin(angle))) * wavenumber + time * sqrt(9.81 * wavenumber) + float(index) * 1.7;- float amplitude = 0.11 * pow(wavenumber / 2.4, -0.5);- slope += vec2(cos(angle), sin(angle)) * cos(phase) * amplitude * visible;+ 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 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.65;- angle += 2.399963;+ wavenumber *= 1.7320508;+ angle += 2.399963 + 0.08 * drift;}return slope;}@@ -367,16 +372,25 @@ void mossMasks(vec3 normal, float amount, out float sharpMask, out float softMasvec3 crestNormal = normal;normal = normalize(normal + rippleTilt);// The body is deeper at grazing angles; the foam mask (the ocean's, stored as 1 - foam in the sky visibility channel) turns it white.- // Streaks: only the heaviest half of the ocean's foam counts (about 5% of a rough sea), and noise breaks it up by noise stretched along the crest, so a patch has lacy edges and holes rather than a soft blob.+ // Foam: the mask is the heavy half of the ocean's foam (about 5% of a rough sea). Three octaves of noise stretched along the crest+ // make lace: the core stays bright and solid, the edge dissolves into holes and thin veins, and it grows less opaque outwards.+ // The threshold has a soft band at least one pixel wide (fwidth), so the edge is smooth and does not follow the mesh triangles.vec2 crestAcross = normalize(crestNormal.xz + vec2(0.0001));vec2 crestAlong = vec2(-crestAcross.y, crestAcross.x);- vec2 streakPosition = vec2(dot(worldPosition.xz, crestAlong) * 0.6, dot(worldPosition.xz, crestAcross) * 3.2);- float streaks = terrainNoise(streakPosition).x * 0.6 + terrainNoise(streakPosition * 2.9 + 7.3).x * 0.4;+ vec2 crestPosition = vec2(dot(worldPosition.xz, crestAlong), dot(worldPosition.xz, crestAcross));+ float broadLace = terrainNoise(crestPosition * vec2(0.45, 2.4)).x;+ float midLace = terrainNoise(crestPosition * vec2(1.3, 6.5) + 7.3).x;+ float fineLace = terrainNoise(crestPosition * vec2(3.1, 14.0) + 3.9).x;+ float lace = broadLace * 0.5 + midLace * 0.3 + fineLace * 0.2;float foamMask = clamp(2.0 * (1.0 - vertexSkyVisibility) - 1.0, 0.0, 1.0);- waterFoam = smoothstep(0.4, 0.58, foamMask * 1.25 + (streaks - 0.5) * 0.7);+ float foamDensity = foamMask * 1.25 + (lace - 0.5) * 0.9;+ float foamBand = 0.13 + 1.5 * fwidth(foamDensity);+ float foamBody = smoothstep(0.5 - foamBand, 0.5 + foamBand, foamDensity) * smoothstep(0.03, 0.25, foamMask);+ float foamVeins = pow(1.0 - abs(2.0 * midLace - 1.0), 3.0) * smoothstep(0.45, 0.9, foamMask) * (1.0 - foamBody) * 0.32;+ waterFoam = max(foamBody * mix(0.5, 1.0, smoothstep(0.3, 0.95, foamMask)) * (0.78 + 0.22 * smoothstep(0.25, 0.75, fineLace * 0.6 + midLace * 0.4)), foamVeins);float grazing = pow(1.0 - max(dot(normal, viewDirection), 0.0), 4.0);- color = mix(color, vec3(0.04, 0.12, 0.16), grazing * 0.85);- color = mix(color, vec3(0.92, 0.95, 0.97), waterFoam);+ color = mix(color, vec3(0.0137, 0.147, 0.195), grazing * 0.85);+ color = mix(color, vec3(0.92, 0.95, 0.97) * (0.68 + 0.32 * (fineLace * 0.5 + midLace * 0.5)), waterFoam);roughness = mix(0.16, 0.8, waterFoam);}if (CANOPY_TAG) {@@ -441,11 +455,11 @@ void mossMasks(vec3 normal, float amount, out float sharpMask, out float softMasvec3 mirrorDirection = reflect(-viewDirection, normal);mirrorDirection.y = max(mirrorDirection.y, 0.02);vec3 skyColor = ENVIRONMENT_ENABLED ? surroundingsColor(mirrorDirection, 0.05) : mix(vec3(0.12, 0.24, 0.28), vec3(0.40, 0.63, 0.78), clamp(mirrorDirection.y, 0.0, 1.0)) * daylight;- radiance = mix(radiance * 0.6, skyColor * ambientIntensity, clamp(reflectivity, 0.0, 1.0));+ 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 / 900.0 + waterLostVariance);- float glintShape = pow(max(dot(normal, halfVector), 0.0), glintSharpness) * (glintSharpness + 2.0) / 902.0;+ float glintSharpness = 1.0 / (1.0 / 500.0 + 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);radiance += vec3(0.85, 0.9, 0.95) * waterFoam * (max(dot(normal, normalize(lightDirection)), 0.0) * keyResponse + 0.35 * daylight * ambientIntensity);diff --git a/tests/physical-sky.test.js b/tests/physical-sky.test.jsindex c9d039d7..9702a962 100644--- a/tests/physical-sky.test.js+++ b/tests/physical-sky.test.js@@ -86,7 +86,7 @@ test('ozone deepens twilight blue and sunDisc adds the solar disc to the sky mapconst withDisc = agentEnvironmentMap(skyEnvironment({ sunElevation: 30, sunDisc: true }));const withoutDisc = agentEnvironmentMap(skyEnvironment({ sunElevation: 30, sunDisc: false }));- const peak = values => Math.max(...values);+ const peak = values => values.reduce((largest, value) => Math.max(largest, value), 0);assert.ok(peak(withDisc.data) > peak(withoutDisc.data) * 1.2);assert.throws(() => skyEnvironment({ ozone: -1 }), /environment\.sky\.ozone/);assert.throws(() => skyEnvironment({ sunDisc: 1 }), /environment\.sky\.sunDisc/);diff --git a/tests/water-ocean-look.test.js b/tests/water-ocean-look.test.jsindex 793a6819..aaa8498a 100644--- a/tests/water-ocean-look.test.js+++ b/tests/water-ocean-look.test.js@@ -67,6 +67,7 @@ test('the ocean foam of a rough sea reaches the water material as 1 - foam', ()test('wind ripples break the glint into facets and the foam into streaks', () => {const shader = createFragmentShader(['water', 'vertexColors']);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, /waterFoam = smoothstep\(0\.4, 0\.58, foamMask/);+ assert.match(shader, /terrainNoise\(crestPosition \* vec2\(3\.1, 14\.0\)/);});