Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

fix(sea): re-review N1-N3 - breakers filtered by fwidth(phase), a test that tells

eyalgolan83239f8709c4
diff --git a/docs/visual-v1.md b/docs/visual-v1.md
index d1bed9cb0..88b368c33 100644
--- a/docs/visual-v1.md
+++ b/docs/visual-v1.md
@@ -995,9 +995,12 @@ Review round 1 (staff review I1-I2, M1-M8) is folded in; the values below are th
- Breakers through a `ScSurfM` 900 m zone. They are a squared triangle wave in the distance from the shore, spacing
`ScBreakerSpacingM` 300 m, rolling shoreward at `ScBreakerRate` 0.05 cycles/s (15 m/s, a quarter pixel a frame at
gameplay03 and 60 fps).
- - The breakers are box-filtered over 1.5 pixels in closed form (`ScTriSqBox`), so they never alias and tend to their
- mean (1/3) as the pixel grows. The previous `pow(tri, 5)` crest was about 21 m wide on 45 m pixels: hairline,
- dashed crests.
+ - The breakers are box-filtered in closed form (`ScTriSqBox`), so they never alias and tend to their mean (1/3) as the
+ pixel grows. The previous `pow(tri, 5)` crest was about 21 m wide on 45 m pixels: hairline, dashed crests.
+ - The box is `fwidth(phase)` x `ScBreakerFilterPx` 1.25, with the phase taken by `ScBreakers` outside the water branch
+ in both callers (re-review N2). The phase's true gradient is not 1 / 300 m: its noise term adds up to 0.0025
+ cycles/m and the 8-bit coast's slope varies up to 2x, so a width from the nominal spacing was up to 2x too narrow in
+ places.
- The 300 m break-up noise fades to its mean between 75 and 150 m pixels.
- **Beach** (`ScShore`).
- A sand strip along every sea-level coast (above -100 m): the Mediterranean and the Gulf of Eilat, both sand beaches,
@@ -1080,16 +1083,25 @@ New tests:
- Strip off: RGBA(0.716, 0.656, 0.378), luma 0.65, b/g 0.58, 0.25 over the plain.
- Strip on: RGBA(0.906, 0.771, 0.516), luma 0.78, b/g 0.67, 0.39 over the plain.
- Bounds 0.72, 0.62 and +0.32: all three fail with the strip off.
-- `TheBreakersAreFilteredToThePixel`.
- - A 960x540 render matches its 4x render's 4x4 block means: mean 0.0027, worst 0.017 (bound 0.02).
- - The surf pixel spans luma 0.647-0.781 over one breaker cycle (bound 0.03).
- - The overview is still: 0.0000 over 0.5 s (bound 0.01).
+- `TheBreakersAreFilteredToThePixel` (re-review N1).
+ - It renders at 30 km and 960x540, about 45 m a pixel: the same footprint as gameplay03's 60 km at 1920x1080. There an
+ unfiltered crest is sub-pixel but not yet faded.
+ - Each water pixel of a 49x49 patch around the surf zone (6067 pixels over four moments of a cycle) must match its 4x4
+ block in the same view rendered at 4x. Pixels touching land or markers are left out: the shoreline's step and the
+ markers' edges are not filtered by design.
+ - With the filter: p99 0.0082, worst 0.0176 (bounds 0.014 and 0.035).
+ - Red with Round 0's crest swapped back in temporarily: p99 0.0200, worst 0.0620, and the test fails.
+ - The same squared crest without its box measured p99 0.0087, worst 0.0247. At 300 m spacing it is about two pixels
+ wide by itself, so the box is what keeps it exact as the zoom changes.
+ - The surf pixel spans luma 0.642-0.788 over one breaker cycle (bound 0.03).
+ - The overview is still: 0.0036 over 0.5 s (bound 0.01).
In motion (`MapCapture.SurfFrames`, a 200x400 px coast crop at gameplay03):
- Over one second of consecutive 60 fps frames, no pixel changes more than 2/255 from one frame to the next, either
before or after the change.
- Over the whole second, the old crests moved single pixels by up to 46/255 (2.8% of the crop over 2/255). The filtered
- breakers move them by at most 9/255 (2.5%).
+ breakers move them by at most 9/255 (2.5%), the same with the `fwidth(phase)` box. The open sea's waves move them by
+ at most 14/255 (1.7%).
- A 15 s clip at `-surfDt 0.25` shows soft crests rolling in, broken along the shore.
Frame time: headless, the machine loaded by other sessions (load average 4-12), so the absolutes are inflated; read the
@@ -1129,6 +1141,14 @@ Each item below was ruled to the art track (controller, rank 10). None blocks la
biome is for the owner to judge.
6. **Overview shadow overrun.** The shadow pass overruns the budget by 0.1-0.4 ms at the overview pose; the levers are in
"Tier decisions".
+7. **Sea brightness spread (promo-sea re-review N3).** The sea's luma p90 - p10 is 0.12 at gameplay03 against promo 03's
+ 0.39, and 0.08 at diorama02 against promo 02's 0.15. Its saturation p10/p90 is narrower too (0.43/0.55 against
+ 0.29/0.58). The means match ("promo-sea", Measured). The causes:
+ - promo 03's dark tail is boat hulls and building shadows on the water. Boats are out of scope: navy ships are units,
+ and there are no civilian boats (owner).
+ - Its bright tail is foam lace and glints. The glint is held at 0.06 by the seam test (Task 7), and the surf is a
+ smooth band, not lace.
+ - diorama02's sea has a near-to-far gradient that ours keeps flat.
### Rulings digest
One line each: ruling - reason.
diff --git a/unity/Assets/Shaders/Sea.shader b/unity/Assets/Shaders/Sea.shader
index 1e287e980..003ff8c98 100644
--- a/unity/Assets/Shaders/Sea.shader
+++ b/unity/Assets/Shaders/Sea.shader
@@ -45,7 +45,7 @@ Shader "Statecraft/Sea"
float2 mapM = input.positionWS.xz - _ScMapOrigin.xz;
float coast = SAMPLE_TEXTURE2D(_ScCoastTex, sampler_ScCoastTex, saturate(mapM / _ScMap.xy)).r;
float footprintM = max(length(ddx(mapM)), length(ddy(mapM))); // as the terrain's water takes it
- float3 c = ScHazeWater(ScWaterShade(coast, 0.0, input.positionWS, mapM, footprintM), input.positionWS);
+ float3 c = ScHazeWater(ScWaterShade(coast, 0.0, input.positionWS, mapM, footprintM, ScBreakers(coast, mapM)), input.positionWS);
// The hex lattice runs on past the map's edge, exactly as on the terrain's water (review I3).
c = lerp(c, SRGBToLinear(ScGridWaterSrgb), ScGridAmount(mapM, input.positionWS) * _ScGrid.y);
return half4(c, 1.0);
diff --git a/unity/Assets/Shaders/StatecraftMap.hlsl b/unity/Assets/Shaders/StatecraftMap.hlsl
index 35918016c..395996e65 100644
--- a/unity/Assets/Shaders/StatecraftMap.hlsl
+++ b/unity/Assets/Shaders/StatecraftMap.hlsl
@@ -89,13 +89,28 @@ float ScTriSqBox(float x, float w)
return (ScTriSqIntegral(x + 0.5 * w) - ScTriSqIntegral(x - 0.5 * w)) / w;
}
+// The water's clock: Unity's time, or the pinned seconds (MapShaderGlobals.PinWaterTime).
+float ScWaterClock() { return _ScWaterTime.y > 0.5 ? _ScWaterTime.x : _Time.y; }
+
+// The breakers' phase (cycles; crests at whole numbers) and its box width: fwidth(phase) x ScBreakerFilterPx. The phase's
+// true gradient is not 1 / ScBreakerSpacingM: its noise term adds up to 0.0025 cycles/m and the 8-bit coast's slope varies
+// up to 2x, so the width comes from the phase itself (review N2). Call it in uniform control flow (outside any [branch]);
+// pass the result to ScWaterShade.
+static const float ScBreakerFilterPx = 1.25;
+float2 ScBreakers(float coast, float2 mapM)
+{
+ float offshoreM = saturate(0.5 - coast) * 2.0 * ScCoastRangeM;
+ float phase = offshoreM / ScBreakerSpacingM + ScWaterClock() * ScBreakerRate + ScNoise(mapM / 1200.0) * 2.0;
+ return float2(phase, fwidth(phase) * ScBreakerFilterPx);
+}
+
// The water's lit colour before haze (callers apply ScHazeWater and output it as is): the depth ramp above; the Dead Sea
// (below -300 m) pale turquoise with a white salt rim, the Sea of Galilee a fresher teal; a long swell and a finer chop
// (scrolling noise, the chop faded by the footprint) tilt a wave normal for Lambert from the sun (with its shadows: cliffs
// shade the water) plus sky fill and a small glint; the surf along the shore. elevM is the real elevation of the water
// surface; mapM metres east and north of the map's south-west corner; footprintM the metres one pixel covers (from fwidth,
-// taken by the caller in uniform control flow).
-float3 ScWaterShade(float coast, float elevM, float3 positionWS, float2 mapM, float footprintM)
+// taken by the caller in uniform control flow); breakers ScBreakers' (phase, box width).
+float3 ScWaterShade(float coast, float elevM, float3 positionWS, float2 mapM, float footprintM, float2 breakers)
{
if (_ScWaterOn < 0.5) return SRGBToLinear(ScFlatSeaSrgb) * 0.8;
float offshoreM = saturate(0.5 - coast) * 2.0 * ScCoastRangeM;
@@ -109,7 +124,7 @@ float3 ScWaterShade(float coast, float elevM, float3 positionWS, float2 mapM, fl
c = lerp(c, SRGBToLinear(float3(0.45, 0.78, 0.78)), salt * 0.8);
c = lerp(c, SRGBToLinear(ScGalileeSrgb), fresh * 0.7);
- float time = _ScWaterTime.y > 0.5 ? _ScWaterTime.x : _Time.y;
+ float time = ScWaterClock();
float2 p = mapM / 700.0;
float2 g = float2(ScNoise(p + float2(time * 0.05, 0.0)) - ScNoise(p + float2(0.37 + time * 0.05, 0.0)),
ScNoise(p.yx + float2(0.0, time * 0.04)) - ScNoise(p.yx + float2(0.0, 0.41 + time * 0.04)));
@@ -140,12 +155,11 @@ float3 ScWaterShade(float coast, float elevM, float3 positionWS, float2 mapM, fl
lit += pow(saturate(dot(reflect(-l, n), v)), 80.0) * 0.06 * lc * shadow;
// Surf: the swash line, then breakers (crests every ScBreakerSpacingM, rolling in: the phase grows with time towards
- // the shore; filtered over 1.5 pixels) whose crests break up along the coast (a 300 m noise, faded to its mean once a
+ // the shore; box-filtered over ScBreakers' width) whose crests break up along the coast (a 300 m noise, faded to its mean once a
// pixel covers 75-150 m). Neither on the Dead Sea, which keeps its salt rim.
float swash = 1.0 - smoothstep(ScSwashM, ScSwashM * 1.8, offshoreM);
float zone = 1.0 - smoothstep(ScSwashM, ScSurfM, offshoreM);
- float phase = offshoreM / ScBreakerSpacingM + time * ScBreakerRate + ScNoise(mapM / 1200.0) * 2.0;
- float crest = ScTriSqBox(phase, 1.5 * footprintM / ScBreakerSpacingM);
+ float crest = ScTriSqBox(breakers.x, breakers.y);
float broken = smoothstep(0.3, 0.7, ScNoise(mapM / 300.0 + float2(0.0, time * 0.02)));
broken = lerp(0.5, broken, 1.0 - smoothstep(75.0, 150.0, footprintM));
float rim = salt * (1.0 - smoothstep(0.0, 900.0, offshoreM));
diff --git a/unity/Assets/Shaders/StatecraftTerrain.hlsl b/unity/Assets/Shaders/StatecraftTerrain.hlsl
index 2c62aafdf..fe55766f0 100644
--- a/unity/Assets/Shaders/StatecraftTerrain.hlsl
+++ b/unity/Assets/Shaders/StatecraftTerrain.hlsl
@@ -27,8 +27,9 @@ void StatecraftTerrain_float(float2 UV, float3 PositionWS, float3 NormalWS, Unit
// The pixel's footprint for the water's chop and breakers, taken here in uniform control flow (derivatives are
// undefined inside the branch).
float footprintM = max(length(ddx(mapM)), length(ddy(mapM)));
+ float2 breakers = ScBreakers(coast, mapM);
float3 waterColour = 0;
- [branch] if (water > 0.0) waterColour = ScHazeWater(ScWaterShade(coast, elevM, PositionWS, mapM, footprintM), PositionWS);
+ [branch] if (water > 0.0) waterColour = ScHazeWater(ScWaterShade(coast, elevM, PositionWS, mapM, footprintM, breakers), PositionWS);
ground = ScShore(ground, coast, elevM, n, mapM); // the beach and the Dead Sea's salt flats (promo-sea)
// The hex grid (visual v1 Task 8), light and faint on land and water, thinner towards the overview: the same lines as
// Sea.shader past the edge. Computed outside the branch: its fwidth needs uniform control flow.
diff --git a/unity/Assets/Tests/EditMode/SeaTests.cs b/unity/Assets/Tests/EditMode/SeaTests.cs
index 449c66d71..392b5c581 100644
--- a/unity/Assets/Tests/EditMode/SeaTests.cs
+++ b/unity/Assets/Tests/EditMode/SeaTests.cs
@@ -184,11 +184,13 @@ public class SeaTests
var w = map.Frame.ToWorld(lon, lat, 0); w.y = map.Terrain.SampleHeight(w); return w;
}
- /// <summary>promo-sea review I2 / M5: the breakers are box-filtered over the pixel, so they never alias. In the surf
- /// zone west of Tel Aviv (about 400 m out), at the gameplay footprint (30 km at 960x540, about 45 m a pixel), each pixel
- /// matches the mean of the same view rendered at 4x (its 4x4 block), and the breakers still show (the surf pixel's
- /// luma spans a range over one breaker cycle). At the overview (250 km) they are their mean: two moments 0.5 s apart
- /// render the same. Grid off: its pixel-width lines differ between resolutions by design.</summary>
+ /// <summary>promo-sea review I2 / M5 / N1: the breakers are box-filtered over the pixel, so they never alias. Around
+ /// the surf zone west of Tel Aviv, at the gameplay footprint (30 km at 960x540: about 45 m a pixel, the same as
+ /// gameplay03's 60 km at 1920x1080), where an unfiltered crest is sub-pixel but not yet faded, every water pixel of a
+ /// 49x49 patch matches the mean of its 4x4 block in the same view rendered at 4x, at four moments of a breaker cycle.
+ /// The breakers still show (the surf pixel's luma spans a range over one cycle), and at the overview (250 km) they are
+ /// their mean: two moments 0.5 s apart render the same. Grid off: its pixel-width lines differ between resolutions by
+ /// design.</summary>
[UnityTest]
public IEnumerator TheBreakersAreFilteredToThePixel()
{
@@ -199,21 +201,33 @@ public class SeaTests
var surf = TexelWorld(map, col - 5, row);
var cam = Camera.main; var rig = cam.GetComponent<CameraRig>();
rig.LookAt = surf; rig.Distance = 30000f; rig.Yaw = 0; rig.TopInset = 0; rig.Apply();
- MapShaderGlobals.PinWaterTime(100f);
var px = new Vector2[1];
- var lo = RenderSample.Render(cam, 960, 540, new[] { surf }, px);
- int cx = (int)px[0].x, cy = (int)px[0].y;
- var hi = RenderSample.Render(cam, 3840, 2160);
- double err = 0, worst = 0; int n = 0;
- for (int y = cy - 6; y <= cy + 6; y++)
- for (int x = cx - 6; x <= cx + 6; x++)
- {
- Color sum = Color.clear;
- for (int j = 0; j < 4; j++) for (int i = 0; i < 4; i++) sum += hi.At(4 * x + i, 4 * y + j);
- double e = Mathf.Abs(Luma(lo.At(x, y)) - Luma(sum / 16f));
- err += e; worst = System.Math.Max(worst, e); n++;
- }
- err /= n;
+ var errs = new System.Collections.Generic.List<double>();
+ int cx = 0, cy = 0;
+ foreach (var t in new[] { 100f, 105f, 110f, 115f }) // four moments over one breaker cycle (20 s)
+ {
+ MapShaderGlobals.PinWaterTime(t);
+ var lo = RenderSample.Render(cam, 960, 540, new[] { surf }, px);
+ cx = (int)px[0].x; cy = (int)px[0].y;
+ var hi = RenderSample.Render(cam, 3840, 2160);
+ for (int y = cy - 24; y <= cy + 24; y++)
+ for (int x = cx - 24; x <= cx + 24; x++)
+ {
+ // Water pixels only: the shoreline's own step and the settlement markers' edges are not
+ // filtered by design, so a pixel counts only when it and all 16 of its 4x samples are water or
+ // foam (blue at least red - 0.03; sand, plain and markers are clearly red over blue).
+ Color sum = Color.clear; bool water = lo.At(x, y).b > lo.At(x, y).r - 0.03f;
+ for (int j = 0; j < 4; j++) for (int i = 0; i < 4; i++)
+ {
+ var c = hi.At(4 * x + i, 4 * y + j); sum += c; water &= c.b > c.r - 0.03f;
+ }
+ if (water) errs.Add(Mathf.Abs(Luma(lo.At(x, y)) - Luma(sum / 16f)));
+ }
+ }
+ errs.Sort();
+ double worst = errs[errs.Count - 1], p99 = errs[(int)(0.99 * (errs.Count - 1))], err = 0;
+ foreach (var e in errs) err += e;
+ err /= errs.Count;
float lMin = 1f, lMax = 0f;
for (int k = 0; k < 8; k++)
{
@@ -227,8 +241,11 @@ public class SeaTests
MapShaderGlobals.PinWaterTime(100.5f);
var b = RenderSample.Render(cam, 960, 540);
float far = Mathf.Abs(Luma(a.Mean((int)px[0].x, (int)px[0].y, 0)) - Luma(b.Mean((int)px[0].x, (int)px[0].y, 0)));
- TestContext.WriteLine($"1x against 4x over 13x13 px: mean {err:0.0000}, worst {worst:0.0000}; breaker cycle luma {lMin:0.000}..{lMax:0.000}; overview 0.5 s apart {far:0.0000}");
- Assert.Less(err, 0.02, "each pixel is its footprint's mean (no aliasing)");
+ TestContext.WriteLine($"1x against 4x, {errs.Count} water pixels of 49x49 at 4 moments: mean {err:0.0000}, p99 {p99:0.0000}, worst {worst:0.0000}; breaker cycle luma {lMin:0.000}..{lMax:0.000}; overview 0.5 s apart {far:0.0000}");
+ // Red with Round 0's unfiltered crest (pow(tri, 5), 160 m, faded only from 40 m pixels): p99 0.020, worst 0.062.
+ // Green with the box filter: p99 0.008, worst 0.018.
+ Assert.Less(p99, 0.014, "each water pixel is its footprint's mean (no aliasing)");
+ Assert.Less(worst, 0.035, "no breaker crest aliases on any water pixel");
Assert.Greater(lMax - lMin, 0.03f, "the breakers roll through at the gameplay zoom");
Assert.Less(far, 0.01f, "the overview sees the breakers' mean, not a crawl");
}