A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Units: cap a badge's on-screen size up close (owner: "Shrink up close") (#2077)
Eyal Golana256c49ce7ac
diff --git a/changelog.d/unit-badge-screen-cap.md b/changelog.d/unit-badge-screen-cap.mdnew file mode 100644index 000000000..2f9433348--- /dev/null+++ b/changelog.d/unit-badge-screen-cap.md@@ -0,0 +1,7 @@+### Changed++- A unit's badge no longer fills the screen up close (owner, 31 October 2026: "Shrink up close (Recommended)"). From+ about 30 km out it keeps its size; a nearer camera draws it no taller than it is there (`UnitBadge.MaxScreenShare` of+ the screen's height), shrinking about its lower edge so it still clears the flag and the figures. At 12 km over Gaza+ the Egyptian force's badge was about 700 of 1080 pixels tall. A click on the badge and the map's names follow the+ badge as drawn.diff --git a/docs/units.md b/docs/units.mdindex 5f118f5c7..dd8327c3a 100644--- a/docs/units.md+++ b/docs/units.md@@ -388,6 +388,9 @@ checks every anchor and logs each spot and its distance.camera before the names are placed, and each token's keep-clear boxes follow (`UnitLayer.KeepClear`). A fixed box roundevery place the camera's yaw could take the badge, about 3.7 km across, pushed Tel Aviv's name off its kit at gameplay03(`MapLabelTests.PillsKeepClearOfTheUnitTokens`; test `TheKeepClearBoxesFollowTheBadgeAndTheFlagAsTheCameraTurns`).+ Up close the badge is capped on the screen (owner, 31 October 2026: "Shrink up close (Recommended)"): from about 30 km+ out it keeps its 1.4 km width, and a nearer camera draws it no taller than `UnitBadge.MaxScreenShare` of the screen's+ height, shrinking about its lower edge, which stays clear of the flag (test `UnitBadgeScreenCapTests`).- **Ring:** the white ring appears only on selection.- **Base and flag (demo2 T29, refs #197).** An army token's figures stand shoulder to shoulder on one round earth baseringed by a bright white rim (`UnitPlinth`), and the side's flag flies on a pole at the base's back edge (`UnitFlags`).diff --git a/unity/Assets/Scripts/Map/Units/UnitBadge.cs b/unity/Assets/Scripts/Map/Units/UnitBadge.csindex d6e9824ce..a27b9ea48 100644--- a/unity/Assets/Scripts/Map/Units/UnitBadge.cs+++ b/unity/Assets/Scripts/Map/Units/UnitBadge.cs@@ -12,6 +12,11 @@ namespace Statecraft.Game.Map.Units{/// <summary>The plate's width at map scale (m): about a third of a figure's height, as in the promo.</summary>public const float SizeM = 1400f;+ /// <summary>The largest share of the screen's height a plate is drawn at (owner, 31 October 2026, "Shrink up close+ /// (Recommended)"): a badge as big as it is in a 30 km view keeps <see cref="SizeM"/>; a nearer camera draws it smaller,+ /// so it never grows past this. Just over the largest a plate is drawn from 30 km out (0.142, 154 of 1080 pixels, at the+ /// view's lower edge; UnitBadgeScreenCapTests), so every view from there out is unchanged.</summary>+ public const float MaxScreenShare = 0.15f;/// <summary>Clearance (m) between the token's top and the plate's lower edge.</summary>public const float LiftM = 1300f;/// <summary>A rifleman's height (m, real) and the corvette's mast top (m, real): the token's top before scaling.</summary>@@ -21,6 +26,14 @@ namespace Statecraft.Game.Map.Units/// <summary>The promo badge's dark navy and its ivory glyph (the selection ring's colour).</summary>public static readonly Color PlateColour = new Color(0.10f, 0.13f, 0.20f), GlyphColour = MapScene.SelectionColour;+ /// <summary>The plate's width (m) for a perspective camera whose projection scales y by <paramref name="m11"/>+ /// (Camera.projectionMatrix[1, 1]) and that sees the plate <paramref name="depth"/> metres ahead: a plate w wide spans+ /// w m11 / (2 depth) of the screen's height, so <see cref="SizeM"/>, or less to keep that within+ /// <see cref="MaxScreenShare"/>. SizeM for an orthographic camera (m11 is then not a perspective scale) or a plate+ /// behind the camera.</summary>+ public static float SizeFor(float depth, float m11, bool orthographic) =>+ orthographic || depth <= 0f || m11 <= 0f ? SizeM : Mathf.Min(SizeM, 2f * MaxScreenShare * depth / m11);+/// <summary>A rounded square, 1 x 1 centred on the origin, corner radius 0.22.</summary>public static Mesh PlateMesh(){diff --git a/unity/Assets/Scripts/Map/Units/UnitLayer.cs b/unity/Assets/Scripts/Map/Units/UnitLayer.csindex 97cc9ebc1..3741bf975 100644--- a/unity/Assets/Scripts/Map/Units/UnitLayer.cs+++ b/unity/Assets/Scripts/Map/Units/UnitLayer.cs@@ -274,6 +274,7 @@ namespace Statecraft.Game.Map.Unitsvar eye = cam.transform.position; var rotation = cam.transform.rotation;if (facedFor is { } last && last.eye == eye && last.rotation == rotation) return; // nothing moved since the last turnfacedFor = (eye, rotation);+ Vector3 forward = cam.transform.forward, up = cam.transform.up; float m11 = cam.projectionMatrix[1, 1];var away = cam.transform.forward; away.y = 0f;away = away.sqrMagnitude > 1e-6f ? away.normalized : Vector3.forward;var right = cam.transform.right; right.y = 0f;@@ -313,9 +314,15 @@ namespace Statecraft.Game.Map.Unitsif (figures.Length > 0) pole = turning * pole; // where the pole stands now, for the badge's sidevar aside = pole == Vector3.zero ? Vector3.zero : (Vector3.Dot(pole, right) > 0f ? -right : right) * BadgeAsideM;var at = home + away * back + aside;+ // Owner, 31 October 2026 ("Shrink up close"): a near camera draws the plate no taller than a 30 km view does.+ // It shrinks about its lower edge (the plate faces the camera, so its edges run along the camera's up), which+ // stays where the full plate's is, clear of the flag and the figures; that move keeps the plate's depth.+ float size = UnitBadge.SizeFor(Vector3.Dot(at - eye, forward), m11, cam.orthographic);+ at -= up * (0.5f * (UnitBadge.SizeM - size));b.SetPositionAndRotation(at, rotation);+ b.localScale = Vector3.one * size;var boxes = keepClear[i];- boxes[0] = new Bounds(at, Vector3.one * UnitBadge.SizeM);+ boxes[0] = new Bounds(at, Vector3.one * size); // the click and the names follow the plate as drawnif ((flag != null || meshes.Length > 0) && standing is Bounds s){if (flag != null)diff --git a/unity/Assets/Tests/EditMode/UnitBadgeScreenCapTests.cs b/unity/Assets/Tests/EditMode/UnitBadgeScreenCapTests.csnew file mode 100644index 000000000..2947229aa--- /dev/null+++ b/unity/Assets/Tests/EditMode/UnitBadgeScreenCapTests.cs@@ -0,0 +1,137 @@+using System.Collections.Generic;+using System.IO;+using System.Linq;+using NUnit.Framework;+using Statecraft.Game;+using Statecraft.Game.Map;+using Statecraft.Game.Map.Units;+using Statecraft.Sim;+using UnityEditor.SceneManagement;+using UnityEngine;++/// <summary>The owner, 31 October 2026, asked "Should badges shrink when you're close?": "Shrink up close (Recommended)". A+/// unit badge's plate keeps its world size (UnitBadge.SizeM) from a 30 km view out, and a nearer camera draws it no taller+/// than UnitBadge.MaxScreenShare of the screen (at 12 km over Gaza the Egyptian force's plate was about 700 of 1080 px).+/// Each view renders the units of the first turn at 1920x1080 with no top bar (the inset only moves the view's lower edge+/// further out, where badges are smaller), the look-at point swept from the token past the view's lower edge.</summary>+public class UnitBadgeScreenCapTests+{+ static readonly Content C = ContentLoader.Load(Paths.Content);+ const string Egypt = "egyptian_expeditionary_force";+ static readonly float[] Yaws = { 0f, 90f, 180f, 270f };+ /// <summary>How far (m) the look-at point sits beyond each token, along the view: from a little behind it (the token+ /// high on the screen) to past the view's lower edge (its badge leaves the screen at the bottom).</summary>+ static readonly float[] Ahead = { -4000f, 0f, 2000f, 4000f, 6000f, 8000f, 10000f, 12000f, 14000f, 16000f, 18000f, 20000f };++ [Test]+ public void BadgesKeepTheir30KmSizeAndNearerViewsCapTheirHeightOnScreen()+ {+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);+ var map = Object.FindFirstObjectByType<MapScene>();+ map.Build();+ var cam = Camera.main; var rig = cam.GetComponent<CameraRig>();+ var rt = new RenderTexture(1920, 1080, 24);+ // An Arab force's token waits for its Collection figure, which edit mode never streams: answer at once with a+ // stand-in, as CollectionOtherLeaveTests does (the badge's place comes from the figure's set height, not its mesh).+ var standIn = GameObject.CreatePrimitive(PrimitiveType.Cube); standIn.transform.position = Vector3.one * -100000f;+ UnitFigures.Source = (_, __, done) => done(standIn); UnitFigures.Forget();+ try+ {+ Assert.AreEqual(CameraRig.Fov, cam.fieldOfView, 1e-3f, "the game camera's field of view");+ cam.targetTexture = rt; rig.TopInset = 0f;+ var units = UnitData.Parse(File.ReadAllText(Path.Combine(Paths.Content, "map", UnitData.DefaultFileName)));+ var layer = UnitLayer.Create(map, units, C.Hexes, still: true);+ layer.Show(C, Simulation.NewGame(C, GoldenRun.Seed, Mode.Historical));+ var tokens = map.Terrain.transform.Find(UnitLayer.RootName).Find("Tokens");+ var badges = layer.Tokens.Select(t => tokens.Find(t.Token.Unit.Id).Find("Badge")).ToList();+ Assert.That(badges.Count, Is.GreaterThan(5), "the first turn's tokens");+ // Where each token stands (its root sits at the origin: the pieces are placed in world space).+ var spots = badges.Select(b => { var p = b.position; p.y = 0f; return p; }).ToList();+ int egypt = layer.Tokens.ToList().FindIndex(t => t.Token.Unit.Id == Egypt);+ Assert.That(egypt, Is.GreaterThanOrEqualTo(0), $"the Egyptian force stands at Gaza on the first turn (drawn: {string.Join(", ", layer.Tokens.Select(t => t.Token.Unit.Id))})");++ // The plate's height on the screen (a share of it) and whether all of it is on the screen.+ (float share, bool shown) Drawn(Transform badge)+ {+ float half = 0.5f * badge.lossyScale.y;+ var corners = new[] { new Vector3(-1, -1), new Vector3(1, -1), new Vector3(-1, 1), new Vector3(1, 1) }+ .Select(c => cam.WorldToViewportPoint(badge.position + badge.rotation * (c * half))).ToList();+ bool shown = corners.All(v => v.z > 0f && v.x >= 0f && v.x <= 1f && v.y >= 0f && v.y <= 1f);+ return (corners.Max(v => v.y) - corners.Min(v => v.y), shown);+ }+ // The look-at point (on y = 0, as the game's and the captures' are) the given distance beyond a token.+ void Look(int token, float distance, float yaw, float ahead)+ {+ rig.LookAt = spots[token] + Quaternion.Euler(0f, yaw, 0f) * Vector3.forward * ahead; rig.Distance = distance; rig.Yaw = yaw; rig.Apply();+ layer.FaceCamera(cam);+ }+ Vector3 LowerEdge(Transform badge) => badge.position - badge.up * (0.5f * badge.lossyScale.y);++ // From 30 km out every plate on the screen is today's: SizeM wide, and so no taller than the cap.+ float largest = 0f; int seen = 0; var changed = new List<string>();+ var lowerAt30 = new Dictionary<(int, float), Vector3>();+ foreach (var distance in new[] { 30000f, 45000f, 60000f })+ foreach (var yaw in Yaws)+ for (int i = 0; i < badges.Count; i++)+ foreach (var ahead in Ahead)+ {+ Look(i, distance, yaw, ahead);+ if (distance == 30000f && ahead == 0f) lowerAt30[(i, yaw)] = LowerEdge(badges[i]);+ for (int j = 0; j < badges.Count; j++)+ {+ var (share, shown) = Drawn(badges[j]);+ if (!shown) continue;+ seen++; largest = Mathf.Max(largest, share);+ if (Mathf.Abs(badges[j].lossyScale.x - UnitBadge.SizeM) > 1e-2f)+ changed.Add($"{distance / 1000f:0} km, yaw {yaw}, {ahead / 1000f:0} km beyond {badges[i].parent.name}: {badges[j].parent.name}'s plate is {badges[j].lossyScale.x:0} m ({share * 1080f:0} px of 1080)");+ }+ }+ Debug.Log($"BADGECAP from 30 km out: the largest plate on the screen is {largest:0.0000} of its height ({largest * 1080f:0} px of 1080), over {seen} plates; the cap is {UnitBadge.MaxScreenShare}");+ Assert.Greater(seen, 1000, "plates seen from 30 km out");+ Assert.IsEmpty(changed, $"from 30 km out every plate keeps its size; {changed.Count} did not, first: {string.Join("; ", changed.Take(5))}");++ // Nearer, no plate on the screen is taller than the cap; it shrinks about its lower edge, which stays where it+ // was at 30 km (the camera's yaw alone places it), clear of the flag and the figures; clicks follow the plate.+ int capped = 0;+ foreach (var distance in new[] { CameraController.MinDistance, 8000f, 12000f, 20000f })+ foreach (var yaw in Yaws)+ for (int i = 0; i < badges.Count; i++)+ foreach (var ahead in Ahead)+ {+ Look(i, distance, yaw, ahead);+ for (int j = 0; j < badges.Count; j++)+ {+ var (share, shown) = Drawn(badges[j]);+ if (!shown) continue;+ Assert.LessOrEqual(share, UnitBadge.MaxScreenShare + 1e-4f,+ $"{distance / 1000f:0} km, yaw {yaw}, {ahead / 1000f:0} km beyond {badges[i].parent.name}: {badges[j].parent.name}'s plate is {share * 1080f:0} px of 1080");+ if (badges[j].lossyScale.x < UnitBadge.SizeM - 1f) capped++;+ }+ if (ahead != 0f) continue;+ var b = badges[i];+ Assert.Less((LowerEdge(b) - lowerAt30[(i, yaw)]).magnitude, 1f, $"{distance / 1000f:0} km, yaw {yaw}, {b.parent.name}: the plate's lower edge stays put");+ var box = layer.KeepClear[i][0];+ Assert.Less((box.center - b.position).magnitude, 1f, $"{b.parent.name}: the click box is round the plate as drawn");+ Assert.AreEqual(b.lossyScale.x, box.size.x, 1e-2f, $"{b.parent.name}: the click box is the plate's own width");+ var centre = cam.WorldToScreenPoint(b.position);+ if (centre.z > 0f)+ Assert.AreEqual(b.parent.name, layer.TokenAtScreen(cam, centre)?.Token.Unit.Id, $"{distance / 1000f:0} km, yaw {yaw}: a click on {b.parent.name}'s plate picks its unit");+ }+ Assert.Greater(capped, 100, "nearer views shrink the plates");++ // The QA view of 31 October (v0.3.2, turn1-gaza-12km): the Egyptian plate low on the screen at 12 km, as low as+ // it stays whole.+ float low = new[] { 6000f, 5000f, 4000f, 3000f, 2000f, 0f }.First(a => { Look(egypt, 12000f, 0f, a); return Drawn(badges[egypt]).shown; });+ Look(egypt, 12000f, 0f, low);+ var (gaza, _) = Drawn(badges[egypt]);+ Debug.Log($"BADGECAP 12 km, {low / 1000f:0} km beyond Gaza: the Egyptian plate is {gaza * 1080f:0} px of 1080, {badges[egypt].lossyScale.x:0} m wide");+ Assert.Less(badges[egypt].lossyScale.x, 0.5f * UnitBadge.SizeM, "at 12 km the Egyptian plate is drawn smaller");+ Assert.LessOrEqual(gaza, UnitBadge.MaxScreenShare + 1e-4f, "and no taller than the cap");+ }+ finally+ {+ UnitFigures.Source = null; UnitFigures.Forget();+ cam.targetTexture = null; Object.DestroyImmediate(rt); Object.DestroyImmediate(map.gameObject); Object.DestroyImmediate(standIn);+ }+ }+}diff --git a/unity/Assets/Tests/EditMode/UnitBadgeScreenCapTests.cs.meta b/unity/Assets/Tests/EditMode/UnitBadgeScreenCapTests.cs.metanew file mode 100644index 000000000..820b43643--- /dev/null+++ b/unity/Assets/Tests/EditMode/UnitBadgeScreenCapTests.cs.meta@@ -0,0 +1,2 @@+fileFormatVersion: 2+guid: 2ecbb1c059b3c409fa1cd03c9804ff71\ No newline at end of file