Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Water look: ripple facets in the glint, streaked foam, foam test

Nguyen Xuan And1b8de62d1c8
diff --git a/changes/819.md b/changes/819.md
index 199336cb..542d610b 100644
--- a/changes/819.md
+++ b/changes/819.md
@@ -1 +1 @@
-- The `water` tag reads the sea's foam, mirrors the sky with a fresnel term, shows a sun glint 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 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)
diff --git a/src/mesh-ocean-modifier.js b/src/mesh-ocean-modifier.js
index e2f720d9..3b0f30f8 100644
--- a/src/mesh-ocean-modifier.js
+++ b/src/mesh-ocean-modifier.js
@@ -14,8 +14,8 @@ export const MAX_OCEAN_RESOLUTION = 16;
export const MAX_OCEAN_WORK = 50_000_000;
const GRAVITY = 9.81;
const SIGNIFICANT_HEIGHT_PER_WIND_SQUARED = 0.21;
-const FOAM_START = 0.5;
-const FOAM_FULL = 0;
+const FOAM_START = 0.75;
+const FOAM_FULL = 0.25;
const CHANNEL_GAUSSIAN_RADIUS = 40;
const CHANNEL_GAUSSIAN_ANGLE = 41;
const CHANNEL_PHASE = 42;
diff --git a/src/world-material-shader.js b/src/world-material-shader.js
index fe1ae378..4f41fab4 100644
--- a/src/world-material-shader.js
+++ b/src/world-material-shader.js
@@ -367,13 +367,13 @@ void mossMasks(vec3 normal, float amount, out float sharpMask, out float softMas
vec3 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: the mask is broken up by noise stretched along the crest, so a patch has lacy edges and holes rather than a soft blob.
+ // 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.
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;
- float foamMask = clamp(1.0 - vertexSkyVisibility, 0.0, 1.0);
- waterFoam = smoothstep(0.55, 0.72, foamMask * 1.1 + (streaks - 0.5) * 0.8);
+ 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 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);
diff --git a/tests/water-ocean-look.test.js b/tests/water-ocean-look.test.js
index 13c9e4d2..793a6819 100644
--- a/tests/water-ocean-look.test.js
+++ b/tests/water-ocean-look.test.js
@@ -49,8 +49,24 @@ test('ocean output smooths over 180 degrees unless the recipe says otherwise', (
test('the water shader reads the foam mask, a fresnel sky and a sun glint', () => {
const shader = createFragmentShader(['water', 'vertexColors']);
- assert.match(shader, /waterFoam = smoothstep\(0\.15, 0\.85, clamp\(1\.0 - vertexSkyVisibility/);
+ assert.match(shader, /float foamMask = clamp\(2\.0 \* \(1\.0 - vertexSkyVisibility\)/);
assert.match(shader, /float reflectivity = \(0\.02 \+ 0\.98 \* pow\(1\.0 - cosine, 5\.0\)\)/);
- assert.match(shader, /pow\(max\(dot\(normal, halfVector\), 0\.0\), 900\.0\)/);
+ assert.match(shader, /pow\(max\(dot\(normal, halfVector\), 0\.0\), glintSharpness\)/);
assert.match(shader, /normal = normalize\(normal \+ rippleTilt\)/);
});
+
+test('the ocean foam of a rough sea reaches the water material as 1 - foam', () => {
+ const water = createAgentScene(seaRecipe({ tag: 'water' })).parts[0].geometry;
+ const foam = water.vertexGroups.foam;
+ const whitecaps = foam.filter(weight => weight > 0.05).length;
+ assert.ok(whitecaps > 0, 'a rough sea has vertices with foam');
+ assert.ok(Math.min(...water.skyVisibility) < 0.95, 'the foam reaches the shader as a lower sky visibility');
+ assert.equal(water.skyVisibility.filter(value => value < 0.95).length, whitecaps);
+});
+
+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, /rippleTilt\.xz \+= detailSlope/);
+ assert.match(shader, /waterFoam = smoothstep\(0\.4, 0\.58, foamMask/);
+});