A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Units: re-size badges when the camera's projection changes at the same pose (#2102)
André Israel Andalécio de Oliveira709e7ddcb45f
diff --git a/changelog.d/2098-badge-cache-projection.md b/changelog.d/2098-badge-cache-projection.mdnew file mode 100644index 000000000..861e083c1--- /dev/null+++ b/changelog.d/2098-badge-cache-projection.md@@ -0,0 +1,3 @@+- A unit badge is re-sized when its camera's field of view or projection changes while the camera stands still, or+ when two cameras with the same pose and different projections draw in turn (#2098). The game's own camera has a fixed+ field of view, so players see no change.diff --git a/unity/Assets/Scripts/Map/Units/UnitLayer.cs b/unity/Assets/Scripts/Map/Units/UnitLayer.csindex 3741bf975..e57b1373b 100644--- a/unity/Assets/Scripts/Map/Units/UnitLayer.cs+++ b/unity/Assets/Scripts/Map/Units/UnitLayer.cs@@ -137,9 +137,9 @@ namespace Statecraft.Game.Map.UnitsList<Bounds[]> keepClear = new List<Bounds[]>();GameObject? ring;bool still;- /// <summary>The camera pose <see cref="FaceCamera"/> last turned the badges and flags to; a call from the same pose+ /// <summary>The camera pose and projection (vertical scale, orthographic) <see cref="FaceCamera"/> last turned the badges and flags to; a call from the same pose and projection/// changes nothing and returns at once (GameRoot calls it every frame). Cleared when the tokens are rebuilt.</summary>- (Vector3 eye, Quaternion rotation)? facedFor;+ (Vector3 eye, Quaternion rotation, float m11, bool orthographic)? facedFor;/// <summary>The GDD loop's anchors for units that moved off their starting hex (UnitRoster.FromState), one per row and/// sim hex, and their spots: placed once, the first time a unit stands there, so a rebuild after a move places only@@ -272,9 +272,10 @@ namespace Statecraft.Game.Map.UnitsfacedFor = null;}var 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 turn- facedFor = (eye, rotation);- Vector3 forward = cam.transform.forward, up = cam.transform.up; float m11 = cam.projectionMatrix[1, 1];+ float m11 = cam.projectionMatrix[1, 1]; bool orthographic = cam.orthographic;+ if (facedFor is { } last && last.eye == eye && last.rotation == rotation && last.m11 == m11 && last.orthographic == orthographic) return; // nothing moved since the last turn+ facedFor = (eye, rotation, m11, orthographic);+ Vector3 forward = cam.transform.forward, up = cam.transform.up;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;@@ -317,7 +318,7 @@ namespace Statecraft.Game.Map.Units// 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);+ float size = UnitBadge.SizeFor(Vector3.Dot(at - eye, forward), m11, orthographic);at -= up * (0.5f * (UnitBadge.SizeM - size));b.SetPositionAndRotation(at, rotation);b.localScale = Vector3.one * size;diff --git a/unity/Assets/Tests/EditMode/UnitBadgeScreenCapTests.cs b/unity/Assets/Tests/EditMode/UnitBadgeScreenCapTests.csindex 2947229aa..1bc64dac5 100644--- a/unity/Assets/Tests/EditMode/UnitBadgeScreenCapTests.cs+++ b/unity/Assets/Tests/EditMode/UnitBadgeScreenCapTests.cs@@ -134,4 +134,53 @@ public class UnitBadgeScreenCapTestscam.targetTexture = null; Object.DestroyImmediate(rt); Object.DestroyImmediate(map.gameObject); Object.DestroyImmediate(standIn);}}++ /// <summary>FaceCamera skips a call from the pose it last turned to; the badges' size also depends on the projection+ /// (a perspective camera caps it, an orthographic one does not), so a new projection from the same pose re-sizes them.</summary>+ [Test]+ public void ABadgeIsResizedWhenTheProjectionChangesAtTheSamePose()+ {+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);+ var map = Object.FindFirstObjectByType<MapScene>();+ map.Build();+ var cam = Camera.main; var rig = cam.GetComponent<CameraRig>();+ var standIn = GameObject.CreatePrimitive(PrimitiveType.Cube); standIn.transform.position = Vector3.one * -100000f;+ UnitFigures.Source = (_, __, done) => done(standIn); UnitFigures.Forget();+ float fov = cam.fieldOfView;+ try+ {+ 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 badge = tokens.Find(layer.Tokens.First(t => t.Token.Unit.Id == Egypt).Token.Unit.Id).Find("Badge");+ var spot = badge.position; spot.y = 0f;+ rig.TopInset = 0f; rig.LookAt = spot; rig.Distance = 8000f; rig.Apply();+ layer.FaceCamera(cam);+ float perspective = badge.lossyScale.x;+ Assert.Less(perspective, UnitBadge.SizeM - 1f, "a perspective camera 8 km out caps the plate");++ layer.FaceCamera(cam);+ Assert.AreEqual(perspective, badge.lossyScale.x, 1e-3f, "the same pose and projection changes nothing");++ cam.fieldOfView = fov * 0.5f;+ layer.FaceCamera(cam);+ float expected = perspective * Mathf.Tan(fov * 0.25f * Mathf.Deg2Rad) / Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad);+ Assert.AreEqual(expected, badge.lossyScale.x, 1e-2f, "a narrower field of view at the same pose shrinks the capped plate");++ cam.fieldOfView = fov;+ layer.FaceCamera(cam);+ Assert.AreEqual(perspective, badge.lossyScale.x, 1e-3f, "back to the first field of view");++ cam.orthographic = true;+ layer.FaceCamera(cam);+ Assert.AreEqual(UnitBadge.SizeM, badge.lossyScale.x, 1e-3f, "an orthographic camera at the same pose draws the full size");+ }+ finally+ {+ cam.orthographic = false; cam.fieldOfView = fov;+ UnitFigures.Source = null; UnitFigures.Forget();+ Object.DestroyImmediate(map.gameObject); Object.DestroyImmediate(standIn);+ }+ }}