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): review round 1 - turquoise shallows, filtered breakers, footprint-eased waves
eyalgolanaf6e3cc0139e
diff --git a/unity/Assets/Editor/MapCapture.cs b/unity/Assets/Editor/MapCapture.csindex e71272c51..d070cf40b 100644--- a/unity/Assets/Editor/MapCapture.cs+++ b/unity/Assets/Editor/MapCapture.cs@@ -103,6 +103,33 @@ public static class MapCapture}}+ /// <summary>The surf in motion (promo-sea review I2): 60 frames, consecutive at 60 fps unless -surfDt gives the seconds+ /// between them (the water's clock pinned through MapShaderGlobals.PinWaterTime) of the gameplay03 pose on the web tier (quality level 0), 1920x1080, into+ /// <-captureDir> (default captures/visual/surf) as surf-NNN.png; encode or diff them to judge crawl and shimmer,+ /// which stills cannot show.</summary>+ public static void SurfFrames()+ {+ var args = System.Environment.GetCommandLineArgs();+ int at = System.Array.IndexOf(args, "-captureDir");+ var dir = at >= 0 && at + 1 < args.Length ? args[at + 1] : Path.Combine(CaptureDir, "visual", "surf");+ int dtAt = System.Array.IndexOf(args, "-surfDt"); // seconds between frames (default 1/60: consecutive frames)+ float dt = dtAt >= 0 && dtAt + 1 < args.Length ? float.Parse(args[dtAt + 1], System.Globalization.CultureInfo.InvariantCulture) : 1f / 60f;+ var map = OpenAndBuild();+ var rig = Camera.main.GetComponent<CameraRig>();+ int saved = QualitySettings.GetQualityLevel();+ try+ {+ QualitySettings.SetQualityLevel(0, true);+ PromoPose.Gameplay03.Apply(rig, map.Frame);+ for (int i = 0; i < 60; i++)+ {+ MapShaderGlobals.PinWaterTime(100f + i * dt);+ Save(Camera.main, 1920, 1080, Path.Combine(dir, $"surf-{i:D3}.png"));+ }+ }+ finally { MapShaderGlobals.PinWaterTime(null); QualitySettings.SetQualityLevel(saved, true); }+ }+/// <summary>Renders each promo pose (PromoPose.All) at 1920x1080 once per quality tier (0 = Mobile, the WebGL tier;/// 1 = PC) into <-captureDir> (default captures/visual/editor) as <tier>-<pose>.png. Editor renders show no UI/// Toolkit labels; tools/art_checkpoint.sh pairs them with the WebGL screenshots, which do.</summary>diff --git a/unity/Assets/Scripts/Map/MapShaderGlobals.cs b/unity/Assets/Scripts/Map/MapShaderGlobals.csindex 8efa6c5d0..643df83b9 100644--- a/unity/Assets/Scripts/Map/MapShaderGlobals.cs+++ b/unity/Assets/Scripts/Map/MapShaderGlobals.cs@@ -10,7 +10,13 @@ namespace Statecraft.Game.Map{public static readonly int Map = Shader.PropertyToID("_ScMap"), MapOrigin = Shader.PropertyToID("_ScMapOrigin"),Biome = Shader.PropertyToID("_ScBiomeTex"), Haze = Shader.PropertyToID("_ScHaze"), HazeWater = Shader.PropertyToID("_ScHazeWater"),- HazeRange = Shader.PropertyToID("_ScHazeRange"), WaterOn = Shader.PropertyToID("_ScWaterOn"), Grid = Shader.PropertyToID("_ScGrid");+ HazeRange = Shader.PropertyToID("_ScHazeRange"), WaterOn = Shader.PropertyToID("_ScWaterOn"), Grid = Shader.PropertyToID("_ScGrid"),+ WaterTime = Shader.PropertyToID("_ScWaterTime");++ /// <summary>Pins the water's clock (waves, surf) to the given seconds, or back to Unity's time with null: for tests and+ /// frame captures that must render chosen moments (promo-sea review I2, M5).</summary>+ public static void PinWaterTime(float? seconds) =>+ Shader.SetGlobalVector(WaterTime, seconds.HasValue ? new Vector4(seconds.Value, 1f, 0f, 0f) : Vector4.zero);/// <summary>A warm, dusty haze over land (sRGB): warmer than the ground it covers (R - B 0.26), so the far land reads/// as dust, not as a cool milky veil (Task 6 review I1).</summary>diff --git a/unity/Assets/Shaders/StatecraftMap.hlsl b/unity/Assets/Shaders/StatecraftMap.hlslindex e2ed69f19..35918016c 100644--- a/unity/Assets/Shaders/StatecraftMap.hlsl+++ b/unity/Assets/Shaders/StatecraftMap.hlsl@@ -48,22 +48,46 @@ float3 ScHazeWater(float3 colour, float3 positionWS)float ScWater(float coast) { return 1.0 - smoothstep(0.497, 0.503, coast); }static const float ScCoastRangeM = 12000.0; // tools/terrain_bake.py COAST_RANGE_M-float _ScWaterOn; // 1 = shaded water, 0 = the flat sea colour (the "water" toggle)+float _ScWaterOn; // 1 = shaded water and shore, 0 = the flat sea colour, no shore (the "water" toggle)+float4 _ScWaterTime; // y = 1: the water's clock is x seconds (tests and frame captures); else _Time.y// Depth from the shore (promo-sea). The map has no bathymetry: the heightmap is Copernicus GLO-30, a land model whose sea// texels are 0 m, so the water's depth is read from the distance to the shore (coast.png). To first order that is the// Israeli shelf: sandy shallows for the first kilometre or two, the 100 m isobath some 10-20 km out. Turquoise shallows// ScShallowM wide (the edge wanders +-35% with a 3 km noise, so it never reads as a ruled stripe), teal, then the open-// sea's muted steel blue towards ScCoastRangeM. sRGB, before the grade; chosen so the graded sea lands near the promo's-// (promo 03 sea pixels: saturation 0.49; Task 7's ramp graded to 0.81, open item 3).+// sea's steel blue towards ScCoastRangeM. sRGB, before the grade: a clearly turquoise band (linear weight, so it reads at+// its width) over a muted deep blue, so the graded sea matches the promo in its spread, not only its mean (promo 03 sea+// pixels: saturation 0.49; Task 7's ramp graded to 0.81, open item 3).static const float ScShallowM = 2600.0;-static const float3 ScShallowSrgb = float3(0.46, 0.68, 0.64);-static const float3 ScMidSrgb = float3(0.29, 0.46, 0.53);-static const float3 ScDeepSrgb = float3(0.22, 0.34, 0.47);+static const float3 ScShallowSrgb = float3(0.40, 0.72, 0.70);+static const float3 ScMidSrgb = float3(0.29, 0.50, 0.59);+static const float3 ScDeepSrgb = float3(0.27, 0.40, 0.54);+static const float3 ScFlatSeaSrgb = float3(0.20, 0.36, 0.47); // the "water" toggle's flat sea (x 0.8)+static const float3 ScGalileeSrgb = float3(0.20, 0.45, 0.55); // fresh water (-300..-100 m): the Sea of Galilee+static const float ScChopGain = 1.4; // the chop's share of the wave tilt against the swell's+static const float ScWaveTilt = 0.6; // wave normal tilt at the gameplay zoom (Task 7: 0.15)// The surf: a swash line along the sand (full to ScSwashM) and breakers rolling shoreward through the surf zone-// (ScSurfM), broken along the shore by noise. Where a pixel's footprint nears the breakers' spacing they fade to their-// mean, so the overview never shimmers.-static const float ScSwashM = 180.0, ScSurfM = 900.0, ScBreakerSpacingM = 160.0;+// (ScSurfM), broken along the shore by noise. The breakers are a squared triangle wave in the distance from the shore,+// box-filtered over the pixel's footprint in closed form, so they never alias and tend to their mean (1/3) as the pixel+// grows; ScBreakerRate cycles per second roll them in at ScBreakerSpacingM x ScBreakerRate = 15 m/s (a quarter pixel per+// frame at the gameplay pose's ~45 m pixels and 60 fps). coast.png's 94 m levels are interpolated linearly between+// texels, so a crest can sit up to ~47 m off its true place: about one gameplay pixel, under the phase noise.+static const float ScSwashM = 180.0, ScSurfM = 900.0, ScBreakerSpacingM = 300.0, ScBreakerRate = 0.05;++// Antiderivative of tri(x)^2, tri(x) = 1 - |2 frac(x) - 1| (period 1, mean 1/3).+float ScTriSqIntegral(float x)+{+ float t = frac(x);+ float g = t < 0.5 ? 4.0 * t * t * t / 3.0 : 1.0 / 3.0 - 4.0 * (1.0 - t) * (1.0 - t) * (1.0 - t) / 3.0;+ return floor(x) / 3.0 + g;+}++// tri(x)^2 averaged over [x - w/2, x + w/2].+float ScTriSqBox(float x, float w)+{+ w = max(w, 1e-3);+ return (ScTriSqIntegral(x + 0.5 * w) - ScTriSqIntegral(x - 0.5 * w)) / w;+}// 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@@ -73,19 +97,19 @@ static const float ScSwashM = 180.0, ScSurfM = 900.0, ScBreakerSpacingM = 160.0;// taken by the caller in uniform control flow).float3 ScWaterShade(float coast, float elevM, float3 positionWS, float2 mapM, float footprintM){- if (_ScWaterOn < 0.5) return SRGBToLinear(float3(0.20, 0.36, 0.47)) * 0.8;+ if (_ScWaterOn < 0.5) return SRGBToLinear(ScFlatSeaSrgb) * 0.8;float offshoreM = saturate(0.5 - coast) * 2.0 * ScCoastRangeM;float shallowEdge = ScShallowM * (0.65 + 0.7 * ScNoise(mapM / 3000.0));float shallow = 1.0 - smoothstep(0.0, shallowEdge, offshoreM);float deepT = pow(smoothstep(0.0, ScCoastRangeM, offshoreM), 0.6);float3 c = lerp(SRGBToLinear(ScMidSrgb), SRGBToLinear(ScDeepSrgb), deepT);- c = lerp(c, SRGBToLinear(ScShallowSrgb), shallow * (0.4 + 0.6 * shallow));+ c = lerp(c, SRGBToLinear(ScShallowSrgb), shallow);float salt = step(elevM, -300.0);float fresh = step(elevM, -100.0) * (1.0 - salt);c = lerp(c, SRGBToLinear(float3(0.45, 0.78, 0.78)), salt * 0.8);- c = lerp(c, SRGBToLinear(float3(0.20, 0.45, 0.55)), fresh * 0.7);+ c = lerp(c, SRGBToLinear(ScGalileeSrgb), fresh * 0.7);- float time = _Time.y;+ float time = _ScWaterTime.y > 0.5 ? _ScWaterTime.x : _Time.y;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)));@@ -93,9 +117,13 @@ float3 ScWaterShade(float coast, float elevM, float3 positionWS, float2 mapM, fl// sees it, the overview averages it away).float chop = 1.0 - smoothstep(40.0, 170.0, footprintM);float2 q = mapM / 170.0;- g += 1.4 * chop * float2(ScNoise(q - float2(time * 0.11, 0.0)) - ScNoise(q + float2(0.29 - time * 0.11, 0.0)),+ g += ScChopGain * chop * float2(ScNoise(q - float2(time * 0.11, 0.0)) - ScNoise(q + float2(0.29 - time * 0.11, 0.0)),ScNoise(q.yx - float2(0.0, time * 0.09)) - ScNoise(q.yx + float2(0.0, 0.33 - time * 0.09)));- float3 n = normalize(float3(g.x * 0.28, 1.0, g.y * 0.28));+ // The wave tilt: ScWaveTilt near (pixels under 50 m: the gameplay zoom shows the chop and sparkle), easing to Task 7's+ // 0.15 by 120 m, where a pixel spans a good part of a swell cell: the far sea reads as a calm surface, and patch means+ // either side of the map's edge stay within the seam test's 0.012 (at 0.6 a single 7x7 patch at 120 km carried 0.03).+ float tilt = lerp(ScWaveTilt, 0.15, smoothstep(50.0, 120.0, footprintM));+ float3 n = normalize(float3(g.x * tilt, 1.0, g.y * tilt));#if defined(UNIVERSAL_REALTIME_LIGHTS_INCLUDED) && !defined(SHADERGRAPH_PREVIEW)Light sun = GetMainLight(TransformWorldToShadowCoord(positionWS)); // review I6: the water receives the sun's shadowsfloat3 l = sun.direction, lc = sun.color;@@ -111,32 +139,36 @@ float3 ScWaterShade(float coast, float elevM, float3 positionWS, float2 mapM, fl// view-dependent gradient that read as a seam between samples 5 km apart either side of the map's edge.lit += pow(saturate(dot(reflect(-l, n), v)), 80.0) * 0.06 * lc * shadow;- // Surf: the swash line, then breakers (sharp crests every ScBreakerSpacingM, rolling in: the phase grows with time- // towards the shore) whose crests break up along the coast. Neither on the Dead Sea, which keeps its salt rim.+ // 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+ // 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 * 0.18 + ScNoise(mapM / 1200.0) * 2.0;- float crest = pow(1.0 - abs(frac(phase) * 2.0 - 1.0), 5.0);- crest = lerp(1.0 / 6.0, crest, 1.0 - smoothstep(ScBreakerSpacingM * 0.25, ScBreakerSpacingM * 0.6, footprintM));+ float phase = offshoreM / ScBreakerSpacingM + time * ScBreakerRate + ScNoise(mapM / 1200.0) * 2.0;+ float crest = ScTriSqBox(phase, 1.5 * footprintM / ScBreakerSpacingM);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));float foam = saturate((swash * (0.8 + 0.2 * broken) + zone * crest * broken) * (1.0 - salt) + rim);return lerp(lit, SRGBToLinear(float3(0.93, 0.95, 0.94)), foam * 0.85);}-// The shore on land (promo-sea): a sand strip along the Mediterranean (above -100 m, so not the rift lakes), wet and-// darker where the swash reaches, ScBeachM wide on average (+-40% with a 4 km noise) - symbolic width, as the promo's+// The shore on land (promo-sea): a sand strip along every sea-level coast (above -100 m: the Mediterranean and the Gulf+// of Eilat, both sand beaches; not the rift lakes), wet and darker where the swash reaches, ScBeachM wide on average (+-40% with a 4 km noise) - symbolic width, as the promo's// beach, wider than the real 30-100 m strip but within the coastal dune belt's kilometres; white salt flats along the-// Dead Sea (below -300 m). Steep faces (the Carmel's and the kurkar cliffs) keep their rock.+// Dead Sea (below -300 m). Both thresholds are 20 m ramps, so a shore slope crossing one leaves no hard contour. Steep+// faces (the Carmel's and the kurkar cliffs) keep their rock. Off with the "water" toggle (so its cost is in the+// toggle's).static const float ScBeachM = 650.0;static const float3 ScSandSrgb = float3(0.88, 0.76, 0.56), ScSaltSrgb = float3(0.92, 0.90, 0.85);float3 ScShore(float3 ground, float coast, float elevM, float3 n, float2 mapM){+ if (_ScWaterOn < 0.5) return ground;float landM = saturate(coast - 0.5) * 2.0 * ScCoastRangeM;float width = ScBeachM * (0.6 + 0.8 * ScNoise(mapM / 4000.0 + 7.3));float strip = (1.0 - smoothstep(width * 0.55, width, landM)) * (1.0 - smoothstep(0.25, 0.45, 1.0 - saturate(n.y)));- float salt = step(elevM, -300.0);- float med = step(-100.0, elevM);+ float salt = 1.0 - smoothstep(-310.0, -290.0, elevM);+ float med = smoothstep(-110.0, -90.0, elevM);float3 sand = SRGBToLinear(ScSandSrgb) * lerp(0.94, 1.04, ScNoise(mapM / 350.0));sand = lerp(sand, sand * 0.78, 1.0 - smoothstep(60.0, 160.0, landM)); // wet sand at the waterlinefloat3 shore = lerp(sand, SRGBToLinear(ScSaltSrgb), salt);diff --git a/unity/Assets/Tests/EditMode/SeaTests.cs b/unity/Assets/Tests/EditMode/SeaTests.csindex d5b127a7b..449c66d71 100644--- a/unity/Assets/Tests/EditMode/SeaTests.cs+++ b/unity/Assets/Tests/EditMode/SeaTests.cs@@ -14,7 +14,7 @@ public class SeaTestsconst string SeaMatPath = "Assets/Materials/Sea.mat";[TearDown]- public void Reset() { RenderSample.TiltShift(true); EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); VisualToggles.Clear(); }+ public void Reset() { MapShaderGlobals.PinWaterTime(null); RenderSample.TiltShift(true); EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); VisualToggles.Clear(); }static MapScene OpenMap(){@@ -95,8 +95,9 @@ public class SeaTestsstatic float Luma(Color c) => RenderSample.Luma(c);- /// <summary>Rendered west of Tel Aviv: just offshore (inside the 350 m foam band) is brighter than 1.2 km out, which is- /// lighter and greener than the open sea 24 km out. The shore is found in coast.png itself.</summary>+ /// <summary>Rendered west of Tel Aviv: just offshore (about 120-200 m out, on the swash line: full to 180 m, gone by+ /// 324 m) is brighter than 1.2 km out (inside the turquoise shallows), which is lighter and greener than the open sea+ /// 24 km out. The shore is found in coast.png itself.</summary>[UnityTest]public IEnumerator ShallowsAreLighterThanTheOpenSeaAndFoamLinesTheShore(){@@ -160,11 +161,78 @@ public class SeaTestsTestContext.WriteLine($"beach {b}, plain {l}");// Without the strip the same pixel is the plain's pale coastal edge, RGBA(0.716, 0.656, 0.378): luma 0.65, b/g 0.58.// With it RGBA(0.906, 0.771, 0.516): luma 0.78, b/g 0.67.- Assert.Greater(Luma(b), Luma(l) + 0.2f, "the sand is lighter than the plain");+ Assert.Greater(Luma(b), Luma(l) + 0.32f, "the sand is lighter than the plain (0.25 without the strip)");Assert.Greater(Luma(b), 0.72f, "pale sand");Assert.Greater(b.b / b.g, 0.62f, "sand, not the plain's yellow-green");}+ /// <summary>The coast.png column of the first water texel west of (34.95 E, lat) and its row; row 0 = south.</summary>+ static (int col, int row) ShoreTexel(float lat)+ {+ var coast = new Texture2D(2, 2);+ coast.LoadImage(File.ReadAllBytes(MapTexturePaths.Coast)); // readable copy+ int row = Mathf.FloorToInt((lat - 29.45f) / (33.35f - 29.45f) * coast.height);+ int col = Mathf.FloorToInt((34.95f - 34.2f) / (35.9f - 34.2f) * coast.width);+ while (col > 0 && coast.GetPixel(col, row).r >= 0.5f) col--;+ Object.DestroyImmediate(coast);+ return (col, row);+ }++ static Vector3 TexelWorld(MapScene map, int col, int row)+ {+ double lon = 34.2 + (col + 0.5) / 2048.0 * (35.9 - 34.2), lat = 29.45 + (row + 0.5) / 2048.0 * (33.35 - 29.45);+ 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>+ [UnityTest]+ public IEnumerator TheBreakersAreFilteredToThePixel()+ {+ var map = OpenMap();+ yield return null;+ VisualToggles.Set("grid", false);+ var (col, row) = ShoreTexel(32.08f);+ 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;+ float lMin = 1f, lMax = 0f;+ for (int k = 0; k < 8; k++)+ {+ MapShaderGlobals.PinWaterTime(100f + k * 2.5f); // one breaker cycle is 1 / ScBreakerRate = 20 s+ var r = RenderSample.Render(cam, 960, 540);+ float l = Luma(r.Mean(cx, cy, 0)); lMin = Mathf.Min(lMin, l); lMax = Mathf.Max(lMax, l);+ }+ rig.Distance = 250000f; rig.Apply();+ MapShaderGlobals.PinWaterTime(100f);+ var a = RenderSample.Render(cam, 960, 540, new[] { surf }, px);+ 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)");+ 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");+ }+/// <summary>Rendered: the Dead Sea is pale turquoise, much lighter than the open Mediterranean.</summary>[UnityTest]public IEnumerator TheDeadSeaIsPalerThanTheOpenSea()