A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Statecraft.Game.Map;
using Statecraft.Game.Map.Units;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
public class UnitShowcaseTests
{
// These tests are about the code-built models. An authored model in Resources/Units/Authored (#2734's Spitfire) would
// replace one in every Spawn, so each test starts with none; the authored tests set their own (AuthoredModel is a seam).
System.Func<string, GameObject> realAuthoredModel;
[SetUp] public void CodeBuiltModels() { realAuthoredModel = UnitShowcase.AuthoredModel; UnitShowcase.AuthoredModel = _ => null; }
[TearDown] public void RestoreAuthoredModels() { if (realAuthoredModel != null) UnitShowcase.AuthoredModel = realAuthoredModel; }
static Mesh[] AllMeshes() => new[]
{
UnitModels.Rifleman(UnitModels.Headgear.SlouchHat, UnitModels.RiflePose.Ready, false, "a"),
UnitModels.Rifleman(UnitModels.Headgear.SteelHelmet, UnitModels.RiflePose.Grounded, true, "b"),
UnitModels.Rifleman(UnitModels.Headgear.WoolCap, UnitModels.RiflePose.Slung, false, "c"),
UnitModels.Spitfire("s"), UnitModels.SpitfirePropeller("p"), UnitModels.Corvette("k"),
UnitModels.AviaS199("v"), UnitModels.S199Propeller("q"),
};
[Test]
public void ShowcaseIsAskedForOnlyByUnits()
{
Assert.IsTrue(UnitShowcase.Requested("https://x/index.html?units", new string[0], true));
Assert.IsTrue(UnitShowcase.Requested("https://x/index.html?golden&shot&units", new string[0], true));
Assert.IsFalse(UnitShowcase.Requested("https://x/index.html?golden&shot", new string[0], true), "a plain ?shot stays comparable across tracks");
Assert.IsFalse(UnitShowcase.Requested("https://x/index.html?golden&smoke=5", new string[0], true));
Assert.IsFalse(UnitShowcase.Requested("https://x/index.html?vprofile", new string[0], true), "profiles price it only with &units");
Assert.IsFalse(UnitShowcase.Requested("https://x/units/index.html?unitsx", new string[0], true));
Assert.IsTrue(UnitShowcase.Requested(null, new[] { "player", "-units" }, false));
Assert.IsFalse(UnitShowcase.Requested(null, new[] { "player", "-shot" }, false));
Assert.IsTrue(UnitShowcase.StillRequested("https://x/?shot&units", new string[0], true));
Assert.IsFalse(UnitShowcase.StillRequested("https://x/?units", new string[0], true));
}
[Test]
public void ModelsAreRepeatableAndWithinBudget()
{
var a = AllMeshes(); var b = AllMeshes();
int total = 0;
for (int i = 0; i < a.Length; i++)
{
CollectionAssert.AreEqual(a[i].vertices, b[i].vertices, a[i].name);
CollectionAssert.AreEqual(a[i].triangles, b[i].triangles, a[i].name);
CollectionAssert.AreEqual(a[i].uv, b[i].uv, a[i].name);
total += a[i].triangles.Length / 3;
}
// Budget: the whole slice under 25k triangles (a WebGL integrated GPU draws millions; this keeps a full front of
// trios cheap when #104 instances them).
Assert.Less(total, 25000, $"slice triangles {total}");
foreach (var m in a.Concat(b)) Object.DestroyImmediate(m);
}
[Test]
public void EveryTriangleFacesAlongItsNormals()
{
foreach (var mesh in AllMeshes())
{
var v = mesh.vertices; var n = mesh.normals; var t = mesh.triangles;
int bad = 0, counted = 0;
for (int i = 0; i < t.Length; i += 3)
{
var face = Vector3.Cross(v[t[i + 1]] - v[t[i]], v[t[i + 2]] - v[t[i]]);
if (face.sqrMagnitude < 1e-12f) continue;
counted++;
if (Vector3.Dot(face, n[t[i]] + n[t[i + 1]] + n[t[i + 2]]) < 0f) bad++;
}
Assert.AreEqual(0, bad, $"{mesh.name}: {bad} of {counted} triangles face against their normals");
Object.DestroyImmediate(mesh);
}
}
/// <summary>The builders' normals point out of each solid (the winding test above only checks Build against the
/// normals, so it cannot catch an inside-out builder). Convex parts, placed by rotated, scaled and mirrored matrices.</summary>
[Test]
public void BuilderNormalsPointOutwards()
{
var at = Matrix4x4.TRS(new Vector3(3, 1, -2), Quaternion.Euler(20, 35, -10), new Vector3(1.5f, 0.6f, 2f));
var mirrored = Matrix4x4.TRS(new Vector3(-4, 0, 1), Quaternion.Euler(0, 0, 6), new Vector3(-1f, 1f, 1f));
var parts = new (string name, MiniatureMesh mesh, Vector3 centre)[]
{
("ellipsoid", new MiniatureMesh().Ellipsoid(at, 0), at.MultiplyPoint3x4(Vector3.zero)),
("mirrored ellipsoid", new MiniatureMesh().Ellipsoid(mirrored, 0), mirrored.MultiplyPoint3x4(Vector3.zero)),
("capped cone", new MiniatureMesh().Lathe(at, new[] { new Vector2(0f, 1f), new Vector2(2f, 0.4f) }, 0), at.MultiplyPoint3x4(new Vector3(0, 1f, 0))),
("tapered rod", new MiniatureMesh().Rod(new Vector3(0, 0, 0), new Vector3(1, 2, 0.5f), 0.3f, 0.2f, 0), new Vector3(0.5f, 1f, 0.25f)),
("box", new MiniatureMesh().Box(at, 0), at.MultiplyPoint3x4(Vector3.zero)),
};
foreach (var (name, builder, centre) in parts)
{
var mesh = builder.Build(name);
var v = mesh.vertices; var n = mesh.normals;
int inward = 0;
for (int i = 0; i < v.Length; i++) if (Vector3.Dot(n[i], v[i] - centre) < -1e-4f) inward++;
Assert.AreEqual(0, inward, $"{name}: {inward} of {v.Length} normals point inwards");
Object.DestroyImmediate(mesh);
}
}
[Test]
public void RiflemanStandsOnTheOriginAtAMansHeight()
{
foreach (UnitModels.Headgear hat in System.Enum.GetValues(typeof(UnitModels.Headgear)))
{
var m = UnitModels.Rifleman(hat, UnitModels.RiflePose.Grounded, false, "r");
Assert.That(m.bounds.min.y, Is.InRange(-0.01f, 0.02f), $"{hat}: feet on the ground");
Assert.That(m.bounds.max.y, Is.InRange(1.70f, 1.95f), $"{hat}: about a man's height");
Object.DestroyImmediate(m);
}
}
[Test]
public void PaletteSlotsSampleTheirOwnColour()
{
var tex = UnitPalette.MakeTexture();
for (int slot = 0; slot < UnitPalette.Count; slot++)
{
var c = tex.GetPixelBilinear(UnitPalette.U(slot), 0.5f);
var want = UnitPalette.Shade(slot, 0.5f);
Assert.That(Mathf.Abs(c.r - want.r) + Mathf.Abs(c.g - want.g) + Mathf.Abs(c.b - want.b), Is.LessThan(0.03f), $"slot {slot}");
}
Object.DestroyImmediate(tex);
}
/// <summary>Figures round 1 (refs #197): the uniform reads darker than the sunlit ground and warm brown, as promo 03's
/// figures do (figure/ground luminance about 0.45 there; 0.68-0.82 with the old khaki, 0.50-0.57 now, on the editor
/// renders), and the slope base is trodden earth, not a dark disc. Measured on the paint as drawn: the middle shade row
/// (Shade at 0.5, with its wash), Rec. 709 weights. The old khaki was 0.39-0.52 there and red/green 1.13-1.21.</summary>
[Test]
public void TheUniformIsDarkAndWarmAndTheBaseIsEarth()
{
static float Luma(Color c) => 0.2126f * c.r + 0.7152f * c.g + 0.0722f * c.b;
foreach (var slot in new[] { UnitPalette.Shirt, UnitPalette.ShirtFaded, UnitPalette.Trousers, UnitPalette.SlouchHat })
{
var c = UnitPalette.Shade(slot, 0.5f);
Assert.Less(Luma(c), 0.35f, $"slot {slot}: dark enough to stand out against the ground");
Assert.Greater(c.r / c.g, 1.24f, $"slot {slot}: warm brown, not olive khaki");
}
Assert.Less(Luma(UnitPalette.Shade(UnitPalette.Trousers, 0.5f)), Luma(UnitPalette.Shade(UnitPalette.Shirt, 0.5f)), "the trousers read darker than the shirt");
Assert.Greater(UnitPalette.Shade(UnitPalette.Earth, 0.5f).r / UnitPalette.Shade(UnitPalette.Earth, 0.5f).g, 1.15f, "warm earth");
var rows = Enumerable.Range(0, 100).Select(i => UnitPlinth.Mottle(i * 137f, i * 211f)).ToList();
Assert.That(rows.Min(), Is.GreaterThanOrEqualTo(UnitPlinth.MottleLow)); Assert.That(rows.Max(), Is.LessThanOrEqualTo(UnitPlinth.MottleHigh));
Assert.Greater(rows.Max() - rows.Min(), 0.3f, "mottled: the earth takes a spread of shade rows across the base");
}
[Test]
public void FlightHoldsItsStillPhaseAndKeepsItsAltitude()
{
var go = new GameObject("f"); var prop = new GameObject("p");
try
{
var f = go.AddComponent<ShowcaseFlight>();
f.Configure(new Vector3(1000, 0, 2000), 500f, 300f, 10f, 20f, prop.transform, 0.25f);
var first = go.transform.position;
f.Configure(new Vector3(1000, 0, 2000), 500f, 300f, 10f, 20f, prop.transform, 0.25f);
Assert.AreEqual(first, go.transform.position);
Assert.AreEqual(300f, first.y, 1e-3f);
Assert.AreEqual(500f, Vector2.Distance(new Vector2(first.x, first.z), new Vector2(1000, 2000)), 0.01f);
// Nose along the path (tangent to the circle).
var radial = (first - new Vector3(1000, first.y, 2000)).normalized;
Assert.AreEqual(0f, Vector3.Dot(Vector3.ProjectOnPlane(go.transform.forward, Vector3.up).normalized, radial), 1e-3f);
}
finally { Object.DestroyImmediate(go); Object.DestroyImmediate(prop); }
}
static MapScene BuildMap()
{
EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects, NewSceneMode.Single);
var map = new GameObject("MapScene").AddComponent<MapScene>();
map.Configure(Load("Terrain"), Load("SettlementMarker"), Load("HexSelection"), Load("Sea"), Load("MapFrame"));
map.Build();
return map;
}
/// <summary>Everything a spawn made that must be freed with it: meshes, the palette material and its texture, and the
/// aircraft's glossy paint and its blur and shadow material.</summary>
static List<Object> SpawnedAssets(GameObject root)
{
var list = new List<Object>();
foreach (var f in root.GetComponentsInChildren<MeshFilter>(true)) list.Add(f.sharedMesh);
var paint = root.transform.Find("Rifleman0").GetComponent<MeshRenderer>().sharedMaterial;
list.Add(paint); list.Add(paint.GetTexture("_BaseMap"));
list.Add(root.transform.Find("Spitfire1948/Airframe").GetComponent<MeshRenderer>().sharedMaterial);
list.Add(root.transform.Find("Spitfire1948/" + UnitShowcase.ShadowName).GetComponent<MeshRenderer>().sharedMaterial);
Assert.IsTrue(list.All(o => o != null));
return list;
}
/// <summary>Issue #249: the showcase trio leaves out the kit trees under its plinth, and its toggle gives them back.</summary>
[Test]
public void TheShowcaseTrioStandsClearOfTheTrees()
{
EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);
var map = Object.FindFirstObjectByType<MapScene>();
map.Build();
try
{
var veg = map.GetComponentInChildren<Statecraft.Game.Map.Kits.VegetationLayer>(true);
Assert.IsNotNull(veg, "the kits and their trees are on");
var root = UnitShowcase.Spawn(map, still: true);
var plinth = root.transform.Find(UnitPlinth.PlinthName);
var centre = new Vector2(plinth.position.x, plinth.position.z);
foreach (var i in new[] { 0, 1, 2 })
{
var f = root.transform.Find("Rifleman" + i).position;
Assert.Less((new Vector2(f.x, f.z) - centre).magnitude + 0.2f * UnitShowcase.FigureScale, plinth.localScale.x, $"Rifleman{i}'s boots are on the plinth");
}
bool Under(Statecraft.Game.Map.Kits.TreeKind k, Matrix4x4 m)
{
var p = m.GetColumn(3); float reach = plinth.localScale.x * UnitPlinth.RimOuter + Statecraft.Game.Map.Kits.VegetationLayer.CrownRadius(k, m);
return (centre - new Vector2(p.x, p.z)).sqrMagnitude < reach * reach;
}
Assert.IsFalse(veg.Drawable().Any(t => Under(t.kind, t.m)), "no drawn tree reaches the trio's plinth");
Assert.AreEqual(veg.Planted().Count(t => Under(t.kind, t.m)), veg.Hidden, "only the trio's trees are left out");
VisualToggles.Set(UnitShowcase.ToggleName, false);
Assert.AreEqual(0, veg.Hidden, "the showcase off gives them back");
}
finally { if (VisualToggles.Names.Contains(UnitShowcase.ToggleName)) VisualToggles.Set(UnitShowcase.ToggleName, true); Object.DestroyImmediate(map.gameObject); }
}
[Test]
public void SpawnBuildsTheSliceAtGameplay03AndTheToggleHidesIt()
{
var map = BuildMap();
try
{
var root = UnitShowcase.Spawn(map, still: true);
Assert.AreSame(map.Terrain.transform, root.transform.parent, "a map rebuild destroys it");
Assert.AreEqual(3, root.GetComponentsInChildren<MeshFilter>().Count(f => f.name.StartsWith("Rifleman")));
Assert.IsNotNull(root.transform.Find("SelectionRing"));
Assert.IsNotNull(root.transform.Find("Corvette1948"));
Assert.IsNotNull(root.transform.Find("Spitfire1948/Propeller"));
var plane = root.GetComponentInChildren<ShowcaseFlight>();
Assert.AreEqual(UnitShowcase.StillPhase, plane.StillPhase);
// Each figure stands on the draped base at its own point (the ground there plus the lip).
var plinth = root.transform.Find(UnitPlinth.PlinthName);
Assert.IsNotNull(plinth, "the trio stands on a plinth");
foreach (var f in root.GetComponentsInChildren<MeshFilter>().Where(f => f.name.StartsWith("Rifleman")))
Assert.AreEqual(UnitPlinth.FigureY(map, plinth.localScale.x, f.transform.position.x, f.transform.position.z), f.transform.position.y, 1f, f.name);
// The aircraft clears the highest ground under its loop.
var loop = map.Frame.ToWorld(UnitShowcase.LoopCentre.lon, UnitShowcase.LoopCentre.lat, 0);
Assert.Greater(plane.transform.position.y, UnitShowcase.LoopFloor(map, loop, UnitShowcase.LoopRadiusM) + UnitShowcase.AltitudeM * 0.99f);
// Everything stands inside the gameplay03 view.
var cam = Camera.main; var rig = cam.GetComponent<CameraRig>();
PromoPose.Gameplay03.Apply(rig, map.Frame);
foreach (var r in root.GetComponentsInChildren<Renderer>())
{
var vp = cam.WorldToViewportPoint(r.bounds.center);
Assert.That(vp.x, Is.InRange(0.02f, 0.98f), r.name); Assert.That(vp.y, Is.InRange(0.02f, 0.98f), r.name); Assert.Greater(vp.z, 0f, r.name);
}
CollectionAssert.Contains(VisualToggles.Names, UnitShowcase.ToggleName);
VisualToggles.Set(UnitShowcase.ToggleName, false);
Assert.IsFalse(root.activeSelf);
VisualToggles.Set(UnitShowcase.ToggleName, true);
Assert.IsTrue(root.activeSelf);
// A second spawn replaces the first rather than stacking, and frees the first one's assets.
var first = SpawnedAssets(root);
UnitShowcase.Spawn(map, still: true);
int count = 0; foreach (Transform c in map.Terrain.transform) if (c.name == UnitShowcase.RootName) count++;
Assert.AreEqual(1, count);
Assert.IsTrue(first.All(o => o == null), "the replaced spawn's meshes, material and palette are destroyed");
CollectionAssert.Contains(VisualToggles.Names, UnitShowcase.ToggleName, "the new spawn keeps its switch");
}
finally { Object.DestroyImmediate(map.gameObject); }
CollectionAssert.DoesNotContain(VisualToggles.Names, UnitShowcase.ToggleName, "the map's teardown removes the switch");
}
[Test]
public void TheTrioStandsOnAHexCentreAndTheMapFreesTheSlice()
{
var map = BuildMap();
List<Object> assets;
try
{
var root = UnitShowcase.Spawn(map, still: true);
var ring = root.transform.Find("SelectionRing").GetComponent<Renderer>().bounds.center;
float nearest = float.MaxValue;
foreach (var h in map.Hexes.hexes)
{
var c = map.Frame.ToWorld(h.lon, h.lat, 0);
nearest = Mathf.Min(nearest, new Vector2(c.x - ring.x, c.z - ring.z).magnitude);
}
Assert.Less(nearest, 50f, "the ring (and the trio inside it) is centred on a hex");
// The ring fits inside its hex: its outer edge is short of the hex's inscribed circle.
float hex = map.Hexes.hex_size_km * 1000f;
Assert.Less(UnitShowcase.RingRadius * hex * (1f + UnitShowcase.RingWidth), hex * Mathf.Sqrt(3f) / 2f);
assets = SpawnedAssets(root);
}
finally { Object.DestroyImmediate(map.gameObject); }
Assert.IsTrue(assets.All(o => o == null), "destroying the map destroys every mesh, the material and the palette");
}
[Test]
public void AnAuthoredModelReplacesTheCodeBuiltOneForItsType()
{
var map = BuildMap(); // first: a new scene would destroy the stand-in models
var helmet = new GameObject("authored-helmet"); new GameObject("AuthoredMarker").transform.SetParent(helmet.transform);
var spitfire = new GameObject("authored-spitfire"); new GameObject("AuthoredMarker").transform.SetParent(spitfire.transform);
// An authored model's own propeller: a child named UnitShowcase.AuthoredPropellerName, turning about its local +z.
var bladesMesh = UnitModels.SpitfirePropeller("authored-blades");
var blades = new GameObject(UnitShowcase.AuthoredPropellerName);
blades.transform.SetParent(spitfire.transform, false);
blades.AddComponent<MeshFilter>().sharedMesh = bladesMesh; blades.AddComponent<MeshRenderer>();
var saved = UnitShowcase.AuthoredModel;
UnitShowcase.AuthoredModel = type => type == UnitShowcase.RiflemanHelmetType ? helmet : type == UnitShowcase.SpitfireType ? spitfire : null;
try
{
var root = UnitShowcase.Spawn(map, still: true);
Assert.IsNotNull(root.transform.Find("Rifleman1/AuthoredMarker"), "the helmet rifleman is the authored model");
Assert.IsNull(root.transform.Find("Rifleman1").GetComponent<MeshFilter>(), "no code-built mesh for it");
Assert.IsNotNull(root.transform.Find("Rifleman0").GetComponent<MeshFilter>(), "the others stay code-built");
Assert.AreEqual(UnitShowcase.FigureScale, root.transform.Find("Rifleman1").localScale.x, 1e-3f, "placed and scaled as the code-built one");
Assert.IsNotNull(root.transform.Find("Spitfire1948/Airframe/AuthoredMarker"));
Assert.IsNull(root.transform.Find("Spitfire1948/Propeller"), "an authored aircraft brings its own propeller");
Assert.IsFalse(root.transform.Find("Spitfire1948/Airframe").GetComponentsInChildren<Renderer>(true)
.Any(r => r.sharedMaterial != null && r.sharedMaterial.name.EndsWith(" (glossy)")), "nor the code-built glossy paint: it brings its own materials");
// The flight's look applies to it all the same: its own propeller turns and carries the blur, sized to its blades,
// and a soft shadow lies on the ground while it casts none.
var ownProp = root.transform.Find("Spitfire1948/Airframe/" + UnitShowcase.AuthoredPropellerName);
Assert.IsNotNull(ownProp, "fixture: the authored propeller");
var flight = root.GetComponentInChildren<ShowcaseFlight>();
Assert.AreSame(ownProp, typeof(ShowcaseFlight).GetField("propeller", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!.GetValue(flight),
"the flight turns the authored propeller");
var blur = ownProp.Find(UnitShowcase.PropellerBlurName);
Assert.IsNotNull(blur, "the blur on the authored propeller");
Assert.That(blur.localScale.x, Is.InRange(1.4f, 1.9f), "sized to its blades (the code-built blades here, reaching about 1.6)");
Assert.IsNotNull(flight.transform.Find(UnitShowcase.ShadowName), "the soft shadow under an authored aircraft too");
Assert.IsTrue(flight.GetComponentsInChildren<Renderer>(true).All(r => r.shadowCastingMode == UnityEngine.Rendering.ShadowCastingMode.Off),
"and it casts no shadow of its own");
Assert.IsNotNull(root.transform.Find("Corvette1948").GetComponent<MeshFilter>());
}
finally
{
UnitShowcase.AuthoredModel = saved;
Object.DestroyImmediate(map.gameObject); Object.DestroyImmediate(helmet); Object.DestroyImmediate(spitfire); Object.DestroyImmediate(bladesMesh);
}
}
/// <summary>The coordinator-approved Spitfire plan, step 1 (5 November 2026): the code-built 101 Squadron Spitfire's
/// propeller carries a light, see-through blur that turns with it: pale, clear at the rim, at most about half opaque,
/// brighter at its four blades than between them, casting and taking no shadow.</summary>
[Test]
public void TheCodeBuiltSpitfiresPropellerCarriesALightBlur()
{
bool readable = UnitPlinth.KeepMeshesReadable; UnitPlinth.KeepMeshesReadable = true; // the blur's colours are read below
var map = BuildMap();
try
{
var root = UnitShowcase.Spawn(map, still: true);
var prop = root.transform.Find("Spitfire1948/Propeller");
var blur = prop.Find(UnitShowcase.PropellerBlurName);
Assert.IsNotNull(blur, "a blur, a child of the turning propeller, so it turns with it");
var r = blur.GetComponent<MeshRenderer>();
Assert.AreEqual(UnityEngine.Rendering.ShadowCastingMode.Off, r.shadowCastingMode); Assert.IsFalse(r.receiveShadows);
Assert.AreNotSame(root.transform.Find("Rifleman0").GetComponent<MeshRenderer>().sharedMaterial, r.sharedMaterial, "a see-through material, not the paint");
Assert.Greater(Mathf.Abs(Vector3.Dot(blur.up, prop.forward)), 0.999f, "the disc faces along the propeller's axis (+z)");
var colours = blur.GetComponent<MeshFilter>().sharedMesh.colors32;
Assert.IsTrue(colours.All(c => c.r > 180 && c.g > 180 && c.b > 180), "light, not the dark rods' colour");
float max = colours.Max(c => c.a) / 255f;
Assert.That(max, Is.InRange(0.3f, 0.6f), "see-through");
// e21's AFTER captures of #2728: two layers read as a near-opaque plate under the dark rods. One layer, lighter
// than the Egyptian Spitfire's haze at the same angle.
var positions = blur.GetComponent<MeshFilter>().sharedMesh.vertices;
Assert.IsTrue(positions.All(v => Mathf.Abs(v.y - positions[0].y) < 1e-5f), "one layer");
Assert.Less(max, Statecraft.Game.Map.Collection.SeaAirCollection.PropBlurAlpha(0f, 90f, 0f) - 0.1f, "lighter than the Egyptian haze at a blade");
Assert.AreEqual(0, colours.Min(c => c.a), "clear at its rim");
Assert.Greater(max - colours.Where(c => c.a > 0).Min(c => c.a) / 255f, 0.15f, "brighter at the blades: streaked, not a flat disc");
// The streaks sit at the four rods (UnitModels.SpitfirePropeller: up turned 90 b + 20 degrees about +z, so at 110 +
// 90 b from +x) and trail them: the propeller turns clockwise seen from behind (ShowcaseFlight, -z), so just behind a
// rod is a little more angle. Read in the propeller's own frame.
var verts = blur.GetComponent<MeshFilter>().sharedMesh.vertices;
float near = 0f, far = 0f; int nearN = 0, farN = 0;
for (int i = 0; i < verts.Length; i++)
{
var p = prop.InverseTransformPoint(blur.TransformPoint(verts[i]));
if (new Vector2(p.x, p.y).magnitude < 0.5f * UnitShowcase.PropellerBlurRadius || colours[i].a == 0) continue; // the outer rings
float behind = Mathf.Repeat(Mathf.Atan2(p.y, p.x) * Mathf.Rad2Deg - 110f, 90f);
if (behind < 15f) { near += colours[i].a; nearN++; } else if (behind > 45f && behind < 75f) { far += colours[i].a; farN++; }
}
Assert.Greater(nearN, 0); Assert.Greater(farN, 0);
Assert.Greater(near / nearN - far / farN, 0.1f * 255f, "brighter just behind each of the four rods than between them");
}
finally { UnitPlinth.KeepMeshesReadable = readable; Object.DestroyImmediate(map.gameObject); }
}
/// <summary>The coordinator-approved Spitfire plan, step 2: the code-built Spitfire wears a glossy copy of the units' paint
/// (the same palette, smoothness UnitShowcase.AircraftSmoothness), its own: the riflemen and the corvette keep theirs.</summary>
[Test]
public void TheCodeBuiltSpitfireWearsAGlossyCopyOfTheUnitsPaint()
{
var map = BuildMap();
try
{
var root = UnitShowcase.Spawn(map, still: true);
var paint = root.transform.Find("Rifleman0").GetComponent<MeshRenderer>().sharedMaterial;
var gloss = root.transform.Find("Spitfire1948/Airframe").GetComponent<MeshRenderer>().sharedMaterial;
Assert.AreNotSame(paint, gloss, "its own material");
Assert.AreSame(gloss, root.transform.Find("Spitfire1948/Propeller").GetComponent<MeshRenderer>().sharedMaterial, "the propeller too");
Assert.AreSame(paint, root.transform.Find("Corvette1948").GetComponent<MeshRenderer>().sharedMaterial, "the corvette keeps the units' paint");
Assert.AreEqual(UnitShowcase.AircraftSmoothness, gloss.GetFloat("_Smoothness"), 1e-6f);
Assert.Greater(gloss.GetFloat("_Smoothness"), paint.GetFloat("_Smoothness"), "glossier than the units' paint");
Assert.AreSame(paint.shader, gloss.shader); Assert.AreSame(paint.GetTexture("_BaseMap"), gloss.GetTexture("_BaseMap"), "the same palette");
Assert.AreEqual(paint.GetFloat("_Metallic"), gloss.GetFloat("_Metallic"), "painted, not bare metal");
}
finally { Object.DestroyImmediate(map.gameObject); }
}
/// <summary>The coordinator-approved Spitfire plan, step 3: the flying code-built Spitfire casts a soft shadow on the ground,
/// as the Egyptian Spitfire does: on the map sun's ray from SpitfireLoop.ShadowHeightShare of its height, a little over the
/// ground there, turned with its heading, about as long and as wide as the aircraft seen from above, going with it.</summary>
[Test]
public void TheFlyingCodeBuiltSpitfireCastsASoftShadowOnTheGround()
{
var map = BuildMap();
var sun = new GameObject("TEST ONLY sun").AddComponent<Light>();
var sunWas = RenderSettings.sun;
try
{
sun.type = LightType.Directional; MapLighting.Apply(sun);
var root = UnitShowcase.Spawn(map, still: true);
var flight = root.GetComponentInChildren<ShowcaseFlight>();
var shadow = flight.transform.Find(UnitShowcase.ShadowName);
Assert.IsNotNull(shadow, "a shadow, a child of the aircraft, so it goes with it");
Assert.IsTrue(flight.GetComponentsInChildren<Renderer>(true).All(x => x.shadowCastingMode == UnityEngine.Rendering.ShadowCastingMode.Off),
"its only shadow: the aircraft casts none (one would lie about 7 km off at its drawn height)");
var r = shadow.GetComponent<MeshRenderer>();
Assert.AreEqual(UnityEngine.Rendering.ShadowCastingMode.Off, r.shadowCastingMode); Assert.IsFalse(r.receiveShadows);
var model = UnitModels.Spitfire("span"); float span = model.bounds.size.x * UnitShowcase.AircraftScale; Object.DestroyImmediate(model);
float y0 = map.Terrain.transform.position.y, sea = UnitShowcase.SeaLevelY(map);
float Surface(Vector3 p) => Mathf.Max(map.Terrain.SampleHeight(p) + y0, sea);
var ray = sun.transform.forward;
for (int i = 0; i < 12; i++)
{
flight.Place(i / 12f);
var drawn = flight.transform.position; var s = shadow.position;
Assert.That(s.y - Surface(s), Is.InRange(0f, Statecraft.Game.Map.Collection.SpitfireLoop.ShadowLiftM + 400f), $"on the ground at phase {i}/12");
float under = Surface(drawn);
var low = new Vector3(drawn.x, under + Statecraft.Game.Map.Collection.SpitfireLoop.ShadowHeightShare * (drawn.y - under), drawn.z);
var cast = low + ray * ((low.y - Surface(s)) / -ray.y);
Assert.Less(new Vector2(s.x - cast.x, s.z - cast.z).magnitude, 50f + 0.03f * (low.y - Surface(s)), $"on the sun's ray from its lowered height at phase {i}/12");
Assert.Greater(Vector3.Dot(shadow.forward, Vector3.ProjectOnPlane(flight.transform.forward, Vector3.up).normalized), 0.999f, "turned with its heading");
Assert.That(shadow.lossyScale.x / span, Is.InRange(0.4f, 0.6f), "about half the span across (a little narrowed in the bank)");
Assert.That(shadow.lossyScale.z / span, Is.InRange(0.3f, 0.6f), "about half its length along");
}
}
finally { RenderSettings.sun = sunWas; Object.DestroyImmediate(sun.gameObject); Object.DestroyImmediate(map.gameObject); }
}
/// <summary>The authored 101 Squadron Spitfire (#2734), once it is in Resources/Units/Authored: the prefab the game loads
/// for the type has a DIRECT child named UnitShowcase.AuthoredPropellerName (UnitShowcase.FlyingSpitfire finds it under
/// the root Piece renames "Airframe"), with a mesh, its pivot on the centre line ahead of the airframe, at rest
/// (identity rotation, as ShowcaseFlight overwrites it), so the flight turns it and its blur fits its blades. Ignored
/// until the model lands.</summary>
[Test]
public void TheAuthoredSpitfireHasADirectPropellerChildTheFlightCanTurn()
{
var prefab = Resources.Load<GameObject>(UnitShowcase.AuthoredFolder + UnitShowcase.SpitfireType);
if (prefab == null) Assert.Ignore("no authored Spitfire in Resources yet (#2734)");
var prop = prefab.transform.Find(UnitShowcase.AuthoredPropellerName);
Assert.IsNotNull(prop, $"a direct child named {UnitShowcase.AuthoredPropellerName}");
Assert.IsNotNull(prop.GetComponentInChildren<MeshFilter>()?.sharedMesh, "its blades are a mesh");
Assert.AreEqual(Quaternion.identity, prop.localRotation, "at rest: ShowcaseFlight sets its turn about local +z");
Assert.AreEqual(Vector3.one, prop.localScale);
Assert.AreEqual(0f, prop.localPosition.x, 0.01f, "the hub on the centre line");
var others = prefab.GetComponentsInChildren<MeshFilter>(true).Where(f => !f.transform.IsChildOf(prop)).Select(f => f.sharedMesh.bounds).ToList();
Assert.IsNotEmpty(others, "an airframe besides the propeller");
Assert.Greater(prop.localPosition.z, others.Max(b => b.center.z), "the propeller is at the nose (+z)");
// And through the real showcase: the flight turns that propeller and it carries the blur.
UnitShowcase.AuthoredModel = realAuthoredModel;
var map = BuildMap();
try
{
var root = UnitShowcase.Spawn(map, still: true);
var spawned = root.transform.Find("Spitfire1948/Airframe/" + UnitShowcase.AuthoredPropellerName);
Assert.IsNotNull(spawned, "Airframe/Propeller after Piece renames the prefab's root");
var flight = root.GetComponentInChildren<ShowcaseFlight>();
Assert.AreSame(spawned, typeof(ShowcaseFlight).GetField("propeller", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!.GetValue(flight));
Assert.IsNotNull(spawned.Find(UnitShowcase.PropellerBlurName), "the blur on it");
Assert.IsNotNull(flight.transform.Find(UnitShowcase.ShadowName), "the soft shadow");
// And the flight really turns it about its local +z, 1500 degrees a second, once it flies (not held still).
typeof(ShowcaseFlight).GetField("still", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!.SetValue(flight, null);
var update = typeof(ShowcaseFlight).GetMethod("Update", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!;
var clock = ShowcaseFlight.Clock;
try
{
ShowcaseFlight.Clock = () => 0f; update.Invoke(flight, null); var q0 = spawned.localRotation;
ShowcaseFlight.Clock = () => 0.01f; update.Invoke(flight, null);
Assert.AreEqual(15f, Quaternion.Angle(q0, spawned.localRotation), 0.1f, "15 degrees in a hundredth of a second");
Assert.Greater(Mathf.Abs(Vector3.Dot(spawned.localRotation * Vector3.forward, Vector3.forward)), 0.999f, "about its local +z");
}
finally { ShowcaseFlight.Clock = clock; }
}
finally { Object.DestroyImmediate(map.gameObject); }
}
static Material Load(string name) => AssetDatabase.LoadAssetAtPath<Material>($"Assets/Materials/{name}.mat");
}