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.

feat(sea): promo water - muted depth ramp, surf, chop and a beach strip

eyalgolan45a357e8872f
diff --git a/unity/Assets/Shaders/Sea.shader b/unity/Assets/Shaders/Sea.shader
index 67a2b9321..1e287e980 100644
--- a/unity/Assets/Shaders/Sea.shader
+++ b/unity/Assets/Shaders/Sea.shader
@@ -44,7 +44,8 @@ Shader "Statecraft/Sea"
{
float2 mapM = input.positionWS.xz - _ScMapOrigin.xz;
float coast = SAMPLE_TEXTURE2D(_ScCoastTex, sampler_ScCoastTex, saturate(mapM / _ScMap.xy)).r;
- float3 c = ScHazeWater(ScWaterShade(coast, 0.0, input.positionWS, mapM), input.positionWS);
+ 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);
// 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 6c8b40e54..e2ed69f19 100644
--- a/unity/Assets/Shaders/StatecraftMap.hlsl
+++ b/unity/Assets/Shaders/StatecraftMap.hlsl
@@ -50,30 +50,52 @@ 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)
-// The water's lit colour before haze (callers apply ScHazeWater and output it as is): a turquoise -> teal -> navy ramp with
-// distance from the shore; the Dead Sea (below -300 m) pale turquoise with a white salt rim, the Sea of Galilee a fresher
-// teal; two scrolling noise layers tilt a wave normal for Lambert from the sun (with its shadows: cliffs shade the water)
-// plus sky fill and a small glint; animated foam bands within 350 m of the shore. elevM is the real elevation of the water
-// surface; mapM metres east and north of the map's south-west corner.
-float3 ScWaterShade(float coast, float elevM, float3 positionWS, float2 mapM)
+// 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).
+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);
+// 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;
+
+// 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)
{
- if (_ScWaterOn < 0.5) return SRGBToLinear(float3(0.16, 0.42, 0.55)) * 0.8;
+ if (_ScWaterOn < 0.5) return SRGBToLinear(float3(0.20, 0.36, 0.47)) * 0.8;
float offshoreM = saturate(0.5 - coast) * 2.0 * ScCoastRangeM;
- float t = pow(saturate(offshoreM / ScCoastRangeM), 0.55);
- float3 shallow = SRGBToLinear(float3(0.38, 0.74, 0.72));
- float3 mid = SRGBToLinear(float3(0.13, 0.47, 0.58));
- float3 deep = SRGBToLinear(float3(0.06, 0.24, 0.40));
- float3 c = t < 0.5 ? lerp(shallow, mid, t * 2.0) : lerp(mid, deep, t * 2.0 - 1.0);
+ 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));
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.15, 0.48, 0.60)), fresh * 0.7);
+ c = lerp(c, SRGBToLinear(float3(0.20, 0.45, 0.55)), fresh * 0.7);
float time = _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)));
- float3 n = normalize(float3(g.x * 0.15, 1.0, g.y * 0.15));
+ // The chop: a 170 m layer moving the other way, full while a pixel covers under 40 m, gone by 170 m (the gameplay pose
+ // 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)),
+ 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));
#if defined(UNIVERSAL_REALTIME_LIGHTS_INCLUDED) && !defined(SHADERGRAPH_PREVIEW)
Light sun = GetMainLight(TransformWorldToShadowCoord(positionWS)); // review I6: the water receives the sun's shadows
float3 l = sun.direction, lc = sun.color;
@@ -89,13 +111,38 @@ float3 ScWaterShade(float coast, float elevM, float3 positionWS, float2 mapM)
// 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;
- float band = 1.0 - smoothstep(0.0, 350.0, offshoreM);
- float waves = 0.7 + 0.3 * sin(offshoreM / 90.0 - time * 1.3);
+ // 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.
+ 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 broken = smoothstep(0.3, 0.7, ScNoise(mapM / 300.0 + float2(0.0, time * 0.02)));
float rim = salt * (1.0 - smoothstep(0.0, 900.0, offshoreM));
- float foam = saturate(band * waves * (1.0 - salt) + rim);
+ 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
+// 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.
+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)
+{
+ 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);
+ 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 waterline
+ float3 shore = lerp(sand, SRGBToLinear(ScSaltSrgb), salt);
+ return lerp(ground, shore, strip * saturate(med + salt));
+}
+
// The painted ground: biome colour, warmer stone high up, a dusty rift floor, rock on steep slopes, two octaves of
// breakup, curvature (ridges light, hollows dark) and the baked occlusion (bake: r AO from ao.png, g curvature, b sky-view).
float3 ScGround(float2 uv, float2 mapM, float3 n, float3 positionWS, float4 bake)
diff --git a/unity/Assets/Shaders/StatecraftTerrain.hlsl b/unity/Assets/Shaders/StatecraftTerrain.hlsl
index 0e99a6ebd..2c62aafdf 100644
--- a/unity/Assets/Shaders/StatecraftTerrain.hlsl
+++ b/unity/Assets/Shaders/StatecraftTerrain.hlsl
@@ -24,8 +24,12 @@ void StatecraftTerrain_float(float2 UV, float3 PositionWS, float3 NormalWS, Unit
float elevM = (PositionWS.y - _ScMap.z) / _ScMap.w;
// Land pixels (water 0, spatially coherent) skip the water shading: its noise and second shadow lookup would only be
// multiplied away (Task 7 fix round 1, M3).
+ // 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)));
float3 waterColour = 0;
- [branch] if (water > 0.0) waterColour = ScHazeWater(ScWaterShade(coast, elevM, PositionWS, mapM), PositionWS);
+ [branch] if (water > 0.0) waterColour = ScHazeWater(ScWaterShade(coast, elevM, PositionWS, mapM, footprintM), 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.
float gridLine = ScGridAmount(mapM, PositionWS);
diff --git a/unity/Assets/Tests/EditMode/SeaTests.cs b/unity/Assets/Tests/EditMode/SeaTests.cs
index ef94ed3d3..d5b127a7b 100644
--- a/unity/Assets/Tests/EditMode/SeaTests.cs
+++ b/unity/Assets/Tests/EditMode/SeaTests.cs
@@ -126,6 +126,43 @@ public class SeaTests
Assert.Greater(Luma(f), Luma(s) + 0.02f, "foam at the shore");
Assert.Greater(Luma(s), Luma(d) + 0.05f, "shallows lighter");
Assert.Greater(s.g / s.b, d.g / d.b, "shallows greener");
+ // promo-sea (open item 3, issue #38): the open sea is the promo's muted steel blue, not a saturated navy. HSV
+ // saturation before the grade: 0.91 with Task 7's ramp, 0.58 now (the grade lifts it: promo 03's sea measures 0.49).
+ Color.RGBToHSV(d, out _, out float deepSat, out _);
+ Assert.Less(deepSat, 0.7f, "the open sea is muted");
+ }
+
+ /// <summary>promo-sea: a sand strip lines the Mediterranean shore (promo 03's beach). Rendered on the same row west of
+ /// Tel Aviv: about 200 m inland of the shore the ground is pale sand, lighter than the plain 5 km inland and less
+ /// yellow-green than the bare coastal edge it covers.</summary>
+ [UnityTest]
+ public IEnumerator ASandStripLinesTheMediterraneanShore()
+ {
+ var map = OpenMap();
+ yield return null;
+ var coast = new Texture2D(2, 2);
+ coast.LoadImage(File.ReadAllBytes(MapTexturePaths.Coast)); // readable copy; row 0 = south
+ int row = Mathf.FloorToInt((32.08f - 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--; // walk west to the first water texel
+ Object.DestroyImmediate(coast);
+ Vector3 At(int c)
+ {
+ double lon = 34.2 + (c + 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;
+ }
+ Vector3 beach = At(col + 3), inland = At(col + 60);
+ var cam = Camera.main; var rig = cam.GetComponent<CameraRig>();
+ rig.LookAt = Vector3.Lerp(beach, inland, 0.5f); rig.Distance = 30000f; rig.Yaw = 0; rig.TopInset = 0; rig.Apply();
+ var px = new Vector2[2];
+ var r = RenderSample.Render(cam, 960, 540, new[] { beach, inland }, px);
+ Color b = r.Mean((int)px[0].x, (int)px[0].y, 1), l = r.Mean((int)px[1].x, (int)px[1].y, 2);
+ TestContext.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), 0.72f, "pale sand");
+ Assert.Greater(b.b / b.g, 0.62f, "sand, not the plain's yellow-green");
}
/// <summary>Rendered: the Dead Sea is pale turquoise, much lighter than the open Mediterranean.</summary>