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.

Shot rigs: set the camera's aspect through ShotCamera.Configure (Refs #2882) (#2889)

Beri01328d4c8af9
diff --git a/unity/Assets/Tests/EditMode/CabinetPreviewShots.cs b/unity/Assets/Tests/EditMode/CabinetPreviewShots.cs
index cc148308f..d0d222660 100644
--- a/unity/Assets/Tests/EditMode/CabinetPreviewShots.cs
+++ b/unity/Assets/Tests/EditMode/CabinetPreviewShots.cs
@@ -74,6 +74,7 @@ public class CabinetPreviewShots
IEnumerator Boot(bool hebrew)
{
EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);
+ ShotCamera.Configure(Camera.main, Width, Height);
var doc = Object.FindFirstObjectByType<UIDocument>();
// The UI goes into a transparent texture and Shot puts it over the map camera's frame, as CivMovementShots does.
rt = new RenderTexture(Width, Height, 24, RenderTextureFormat.ARGB32);
diff --git a/unity/Assets/Tests/EditMode/CivMovementShots.cs b/unity/Assets/Tests/EditMode/CivMovementShots.cs
index f6e0cbe13..dbedcf51f 100644
--- a/unity/Assets/Tests/EditMode/CivMovementShots.cs
+++ b/unity/Assets/Tests/EditMode/CivMovementShots.cs
@@ -53,6 +53,7 @@ public class CivMovementShots
internal static IEnumerator Boot(bool hebrew, string saveDir, Action<RenderTexture, PanelSettings, VisualElement, GameRoot> booted)
{
EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);
+ ShotCamera.Configure(Camera.main, Width, Height);
var doc = Object.FindFirstObjectByType<UIDocument>();
var rt = new RenderTexture(Width, Height, 24, RenderTextureFormat.ARGB32);
var ps = Object.Instantiate(doc.panelSettings);
@@ -101,6 +102,9 @@ public class CivMovementShots
internal static void Shot(RenderTexture uiTexture, string name)
{
var cam = Camera.main;
+ // A camera at another aspect writes a stretched still (#2882): its rig skipped ShotCamera.Configure.
+ if (Mathf.Abs(cam.aspect - (float)Width / Height) > 0.001f)
+ throw new InvalidOperationException($"the camera's aspect is {cam.aspect:0.000}, its frame's {(float)Width / Height:0.000} ({Width} x {Height}): call ShotCamera.Configure after the scene opens");
var frame = new RenderTexture(Width, Height, 24, RenderTextureFormat.ARGB32);
var before = cam.targetTexture;
cam.targetTexture = frame; cam.Render(); cam.targetTexture = before;
diff --git a/unity/Assets/Tests/EditMode/FireTakeRetargetShots.cs b/unity/Assets/Tests/EditMode/FireTakeRetargetShots.cs
index 4be547cc9..579591342 100644
--- a/unity/Assets/Tests/EditMode/FireTakeRetargetShots.cs
+++ b/unity/Assets/Tests/EditMode/FireTakeRetargetShots.cs
@@ -90,7 +90,7 @@ public class FireTakeRetargetShots
Answer();
var camera = Object.FindFirstObjectByType<CameraRig>().GetComponent<Camera>();
var rig = camera.GetComponent<CameraRig>();
- camera.aspect = 16f / 9f;
+ ShotCamera.Configure(camera, 1920, 1080);
// Every requested model answered and the layer settled on them over editor frames: a model's arrival makes
// FaceCamera draw the tokens again (UnitFigures.AvailabilityVersion), which would replace a token held mid-shot.
for (int i = 0; i < 6; i++) { camera.Render(); Answer(); layer.FaceCamera(camera); Answer(); yield return null; }
diff --git a/unity/Assets/Tests/EditMode/PanelPreviewShots.cs b/unity/Assets/Tests/EditMode/PanelPreviewShots.cs
index 3df6b0f8d..32db38d67 100644
--- a/unity/Assets/Tests/EditMode/PanelPreviewShots.cs
+++ b/unity/Assets/Tests/EditMode/PanelPreviewShots.cs
@@ -82,6 +82,7 @@ public class PanelPreviewShots
IEnumerator Boot(bool hebrew, Action<GameSession> shape)
{
EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);
+ ShotCamera.Configure(Camera.main, Width, Height);
var doc = Object.FindFirstObjectByType<UIDocument>();
// The UI goes into a transparent texture and Shot puts it over the map camera's frame, as CivMovementShots does.
rt = new RenderTexture(Width, Height, 24, RenderTextureFormat.ARGB32);
diff --git a/unity/Assets/Tests/EditMode/ShotCamera.cs b/unity/Assets/Tests/EditMode/ShotCamera.cs
new file mode 100644
index 000000000..1e8dd3519
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/ShotCamera.cs
@@ -0,0 +1,18 @@
+#nullable enable
+using Statecraft.Game.Map;
+using UnityEngine;
+
+/// <summary>#2882: the one place a shot rig sets its camera to the frame it renders. CameraRig.ApplyProjection freezes the
+/// projection as a custom matrix once TopInset is above 0 (the top bar sets it), and a custom matrix no longer follows the
+/// target texture, so a camera left at the batch run's 4:3 aspect draws a 16:9 still 1.33 times too wide.</summary>
+static class ShotCamera
+{
+ /// <summary>Sets the camera's aspect to <paramref name="width"/> by <paramref name="height"/> and rebuilds the rig's
+ /// projection at it.</summary>
+ internal static void Configure(Camera cam, int width, int height)
+ {
+ cam.aspect = (float)width / height;
+ var rig = cam.GetComponent<CameraRig>();
+ if (rig != null) rig.ApplyProjection();
+ }
+}
diff --git a/unity/Assets/Tests/EditMode/ShotCamera.cs.meta b/unity/Assets/Tests/EditMode/ShotCamera.cs.meta
new file mode 100644
index 000000000..e931c5c68
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/ShotCamera.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: d74649b227764b869b3470c7949f3d6f
diff --git a/unity/Assets/Tests/EditMode/ShotCameraTests.cs b/unity/Assets/Tests/EditMode/ShotCameraTests.cs
new file mode 100644
index 000000000..60e46207d
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/ShotCameraTests.cs
@@ -0,0 +1,39 @@
+#nullable enable
+using System;
+using System.Collections;
+using System.IO;
+using NUnit.Framework;
+using UnityEditor.SceneManagement;
+using UnityEngine;
+using UnityEngine.TestTools;
+using UnityEngine.UIElements;
+using Object = UnityEngine.Object;
+
+/// <summary>#2882: the shot rigs boot the game and render Camera.main into a 1280 x 720 texture. CameraRig.ApplyProjection
+/// freezes the projection as a custom matrix once the top bar sets TopInset, so a camera left at the batch run's 4:3 aspect
+/// drew every still 1.33 times too wide. Must run in batch mode: an editor whose Game view is 16:9 passes by accident.</summary>
+public class ShotCameraTests
+{
+ RenderTexture? rt; PanelSettings? ps; string dir = "";
+
+ [TearDown]
+ public void DiscardScene()
+ {
+ EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
+ if (ps != null) Object.DestroyImmediate(ps);
+ if (rt != null) { rt.Release(); Object.DestroyImmediate(rt); }
+ if (dir != "" && Directory.Exists(dir)) Directory.Delete(dir, true);
+ }
+
+ [UnityTest]
+ public IEnumerator TheShotBootsCameraMatchesItsFrame()
+ {
+ dir = Path.Combine(Path.GetTempPath(), "statecraft-shot-camera-" + Guid.NewGuid().ToString("N"));
+ yield return CivMovementShots.Boot(false, dir, (t, p, u, g) => { rt = t; ps = p; });
+ var cam = Camera.main;
+ float frame = (float)CivMovementShots.Width / CivMovementShots.Height;
+ Assert.AreEqual(frame, cam.aspect, 0.001f, "the camera's aspect is its frame's");
+ var p = cam.projectionMatrix;
+ Assert.AreEqual(frame, p[1, 1] / p[0, 0], 0.001f, "the projection is built at the frame's aspect");
+ }
+}
diff --git a/unity/Assets/Tests/EditMode/ShotCameraTests.cs.meta b/unity/Assets/Tests/EditMode/ShotCameraTests.cs.meta
new file mode 100644
index 000000000..246ed9dca
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/ShotCameraTests.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 60647b115a274b53b88f46e3f9bdb172
diff --git a/unity/Assets/Tests/EditMode/TalModelsShots.cs b/unity/Assets/Tests/EditMode/TalModelsShots.cs
index fc2cd4f2e..e6e575118 100644
--- a/unity/Assets/Tests/EditMode/TalModelsShots.cs
+++ b/unity/Assets/Tests/EditMode/TalModelsShots.cs
@@ -279,6 +279,7 @@ public class TalModelsShots
var inputs = Statecraft.Game.Boot.GameInputs.FromDisk(Statecraft.Game.Paths.Content, Statecraft.Game.Paths.StreamingMap);
foreach (var era in inputs.Content.Eras) { era.Systems.Ruleset = SaveGame.Gdd03Ruleset; era.Systems.Loop = EraSystems.Gdd; }
EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);
+ ShotCamera.Configure(Camera.main, CivMovementShots.Width, CivMovementShots.Height);
var doc = Object.FindFirstObjectByType<UIDocument>();
rt = new RenderTexture(CivMovementShots.Width, CivMovementShots.Height, 24, RenderTextureFormat.ARGB32);
ps = Object.Instantiate(doc.panelSettings);
@@ -558,8 +559,6 @@ public class TalModelsShots
{
UnitFigures.Source = (id, detail, done) => done(Prefab(id, detail));
yield return Boot(hebrew);
- // Keep the real rig's projection and the saved RenderTexture on the same aspect throughout these frames.
- Camera.main.aspect = (float)CivMovementShots.Width / CivMovementShots.Height;
root.Bridge!.EndTurn();
for (int i = 0; i < 3; i++) yield return null;
var ui = Object.FindFirstObjectByType<UIDocument>().rootVisualElement;
diff --git a/unity/Assets/Tests/EditMode/TalSoldierRigShots.cs b/unity/Assets/Tests/EditMode/TalSoldierRigShots.cs
index 73213706f..74ee3bbbe 100644
--- a/unity/Assets/Tests/EditMode/TalSoldierRigShots.cs
+++ b/unity/Assets/Tests/EditMode/TalSoldierRigShots.cs
@@ -59,7 +59,6 @@ public class TalSoldierRigShots
UnitFigures.Source = (id, detail, done) => done(Prefab(!after && id == UnitFigures.Soldier1948 ? UnitFigures.VeteranFighter : id, detail));
UnitWeapons.Source = (id, done) => done(Prefab(id, CollectionDetail.Far));
yield return CivMovementShots.Boot(false, saveDir, (t, p, u, g) => { rt = t; ps = p; root = g; });
- Camera.main.aspect = (float)CivMovementShots.Width / CivMovementShots.Height;
var map = Object.FindFirstObjectByType<MapScene>();
var subject = root.Units!.Tokens.First(t => t.Token.Unit.Id == "negev");
var token = map.Terrain.transform.Find(UnitLayer.RootName + "/Tokens/" + subject.Token.Unit.Id);
diff --git a/unity/Assets/Tests/EditMode/TalWeaponsShots.cs b/unity/Assets/Tests/EditMode/TalWeaponsShots.cs
index 549aa6b1e..5a44dc115 100644
--- a/unity/Assets/Tests/EditMode/TalWeaponsShots.cs
+++ b/unity/Assets/Tests/EditMode/TalWeaponsShots.cs
@@ -57,7 +57,6 @@ public class TalWeaponsShots
var weapons = TalModelsShots.Answer(after ? Array.Empty<string>() : New);
UnitWeapons.Source = (id, done) => weapons(id, CollectionDetail.Far, done);
yield return CivMovementShots.Boot(hebrew, saveDir, (t, p, u, g) => { rt = t; ps = p; root = g; });
- Camera.main.aspect = (float)CivMovementShots.Width / CivMovementShots.Height;
var map = Object.FindFirstObjectByType<MapScene>();
// The subject: Harel's token for the Sten (its third figure is the Palmach scout), else the first token with a sim
// unit whose figures include C06 (the steel helmet), by id.