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(map): SRP-Batcher-compatible selection depth pass; the skirt follows every terrain vertex (fix round 3, m-2 m-4)

eyalgolandf297d34b951
diff --git a/unity/Assets/Scripts/Map/MapBuilder.cs b/unity/Assets/Scripts/Map/MapBuilder.cs
index 00d67a1ca..620b4b305 100644
--- a/unity/Assets/Scripts/Map/MapBuilder.cs
+++ b/unity/Assets/Scripts/Map/MapBuilder.cs
@@ -155,8 +155,6 @@ namespace Statecraft.Game.Map
public static readonly float[] SkirtRings = { 0f, 4000f, 20000f, 60000f, 600000f };
/// <summary>Share of the fall to sea level reached at each ring (land samples only).</summary>
static readonly float[] SkirtFall = { 0f, 0.4f, 0.85f, 1f, 1f };
- /// <summary>Edge samples per side of the border skirt (the heightmap has 2048 intervals per side).</summary>
- public const int SkirtSamplesPerEdge = 1024;
/// <summary>A heightmap sample this close to 0 m is sea (tools/dem_to_heightmap.py writes open sea as exactly 0 m).</summary>
public const float SeaToleranceM = 0.5f;
@@ -169,6 +167,9 @@ namespace Statecraft.Game.Map
{
var size = data.size;
float seaY = (0f - elevMinM) * VerticalExaggeration;
+ // One spoke per terrain vertex along each edge (the heightmap's own resolution), so the skirt's inner edge follows
+ // the terrain's edge exactly; sampling every second vertex left slivers up to 36 m (fix round 3, m-4).
+ int samples = data.heightmapResolution - 1;
var verts = new List<Vector3>(); var sea = new List<int>(); var land = new List<int>();
bool IsLand(float h) => Mathf.Abs(h / VerticalExaggeration + elevMinM) > SeaToleranceM;
// One ring of vertices outward from an edge point; returns the index of its first vertex.
@@ -211,9 +212,9 @@ namespace Statecraft.Game.Map
foreach (var (a, b, n) in edges)
{
int prev = -1; bool prevLand = false;
- for (int i = 0; i <= SkirtSamplesPerEdge; i++)
+ for (int i = 0; i <= samples; i++)
{
- var p = Vector2.Lerp(a, b, (float)i / SkirtSamplesPerEdge);
+ var p = Vector2.Lerp(a, b, (float)i / samples);
bool isLand = IsLand(Height(p));
int spoke = Spoke(p, n, isLand);
if (prev >= 0) Quad(prevLand || isLand ? land : sea, prev, spoke);
diff --git a/unity/Assets/Shaders/HexSelection.shader b/unity/Assets/Shaders/HexSelection.shader
index d9f1a2391..8a47be363 100644
--- a/unity/Assets/Shaders/HexSelection.shader
+++ b/unity/Assets/Shaders/HexSelection.shader
@@ -51,6 +51,10 @@ Shader "Statecraft/HexSelection"
#pragma vertex Vert
#pragma fragment Frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
+ // The same per-material layout as the colour pass, as the SRP Batcher requires of every pass (fix round 3, m-2).
+ CBUFFER_START(UnityPerMaterial)
+ half4 _BaseColor;
+ CBUFFER_END
struct Attributes { float4 positionOS : POSITION; };
struct Varyings { float4 positionCS : SV_POSITION; };
Varyings Vert(Attributes input)
diff --git a/unity/Assets/Tests/EditMode/SettlementTests.cs b/unity/Assets/Tests/EditMode/SettlementTests.cs
index f9347e6ca..470e9ed0f 100644
--- a/unity/Assets/Tests/EditMode/SettlementTests.cs
+++ b/unity/Assets/Tests/EditMode/SettlementTests.cs
@@ -224,11 +224,34 @@ public class MapSceneTests
Assert.IsFalse(c.x > 1f && c.x < size.x - 1f && c.z > 1f && c.z < size.z - 1f, $"skirt triangle over the terrain at {c}");
}
}
- // Its inner edge meets the terrain's edge (no gap, no step).
- var edge = System.Array.FindAll(verts, v => Mathf.Abs(v.x - size.x) < 0.5f && v.z > 1000f && v.z < size.z - 1000f);
- Assert.Greater(edge.Length, 100);
- foreach (var v in edge)
- Assert.AreEqual(map.Terrain.SampleHeight(new Vector3(v.x - 0.5f, 0, v.z)), v.y, 2f, $"east edge at z={v.z}");
+ // Fix round 3 (m-4): on all four edges, at every heightmap sample (the terrain's own vertices), the skirt's inner edge
+ // is at the terrain's height: no sliver between them. The skirt's inner height between its ring-0 vertices is linear.
+ var data = map.Terrain.terrainData;
+ int res = data.heightmapResolution;
+ var ring0 = System.Array.FindAll(verts, v => v.x <= 0.5f || v.x >= size.x - 0.5f || v.z <= 0.5f || v.z >= size.z - 0.5f);
+ foreach (var (name, onEdge, along, at) in new (string, System.Func<Vector3, bool>, System.Func<Vector3, float>, System.Func<float, Vector2>)[] {
+ ("south", v => Mathf.Abs(v.z) < 0.5f, v => v.x, t => new Vector2(t, 0)),
+ ("north", v => Mathf.Abs(v.z - size.z) < 0.5f, v => v.x, t => new Vector2(t, size.z)),
+ ("west", v => Mathf.Abs(v.x) < 0.5f, v => v.z, t => new Vector2(0, t)),
+ ("east", v => Mathf.Abs(v.x - size.x) < 0.5f, v => v.z, t => new Vector2(size.x, t)) })
+ {
+ var line = System.Array.FindAll(ring0, v => onEdge(v)); System.Array.Sort(line, (p, q) => along(p).CompareTo(along(q)));
+ float worst = 0; float worstAt = 0;
+ for (int i = 0; i < res; i++)
+ {
+ float t = (name == "south" || name == "north" ? size.x : size.z) * i / (res - 1);
+ int k = System.Array.FindLastIndex(line, v => along(v) <= t + 0.01f);
+ if (k < 0 || k >= line.Length - 1) continue;
+ var a0 = line[k]; var a1 = line[k + 1];
+ float f = (t - along(a0)) / Mathf.Max(1e-3f, along(a1) - along(a0));
+ var p2 = at(t);
+ float terrain = data.GetInterpolatedHeight(p2.x / size.x, p2.y / size.z);
+ float d = Mathf.Abs(Mathf.Lerp(a0.y, a1.y, f) - terrain);
+ if (d > worst) { worst = d; worstAt = t; }
+ }
+ TestContext.WriteLine($"{name} edge: largest gap to the terrain {worst:0.00} m (at {worstAt:0} m)");
+ Assert.Less(worst, 0.5f, $"{name} edge: a sliver of {worst:0.00} m at {worstAt:0} m along it");
+ }
// Facing up (seen from the camera's side).
var used = new HashSet<int>();
for (int sm = 0; sm < mesh.subMeshCount; sm++) used.UnionWith(mesh.GetTriangles(sm));
@@ -414,6 +437,13 @@ public class MapSceneTests
Assert.AreEqual("Statecraft/HexSelection", selection.shader.name);
StringAssert.IsMatch(@"(?m)^\s*Offset\s+-\d+\s*,\s*-\d+", File.ReadAllText(AssetDatabase.GetAssetPath(selection.shader)));
Assert.Greater(selection.renderQueue, Load(OverlayMatPath).renderQueue, "drawn after the overlay lines");
+ // Fix round 3 (m-2): every pass declares the same UnityPerMaterial CBUFFER, as the SRP Batcher requires.
+ var source = File.ReadAllText(AssetDatabase.GetAssetPath(selection.shader));
+ var buffers = System.Text.RegularExpressions.Regex.Matches(source, @"CBUFFER_START\(UnityPerMaterial\)(.*?)CBUFFER_END", System.Text.RegularExpressions.RegexOptions.Singleline);
+ var passes = System.Text.RegularExpressions.Regex.Matches(source, @"HLSLPROGRAM").Count;
+ Assert.AreEqual(passes, buffers.Count, "a UnityPerMaterial CBUFFER in every pass");
+ foreach (System.Text.RegularExpressions.Match b in buffers)
+ Assert.AreEqual(System.Text.RegularExpressions.Regex.Replace(buffers[0].Groups[1].Value, @"\s+", " "), System.Text.RegularExpressions.Regex.Replace(b.Groups[1].Value, @"\s+", " "), "the same layout in every pass");
// Fix round 2 (N6): the band is in the depth prepass / camera depth texture too, so depth of field sees its depth.
Assert.GreaterOrEqual(selection.FindPass("DepthOnly"), 0, "a DepthOnly pass");
Assert.AreEqual("DepthOnly", selection.shader.FindPassTagValue(selection.FindPass("DepthOnly"), new UnityEngine.Rendering.ShaderTagId("LightMode")).name);