From e6d1546b44001bc786ab2b682df83100b78ea079 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 15 Oct 2026 07:59:15 +0200 Subject: [PATCH] Gravity grid-lowering fixes: formula mirror test, verify across moved off-axis bodies and a zero-strength body, map camera wiring, RTS Planet's Gravity Well deleted GravityShaderMirrorTests compares the expression of GravityField.Potential with gravityPotential in Gravity.cginc. GravityFieldParityVerify now builds four off-axis bodies (one of mass 0), lowers through three frames with bodies moved between them, and checks the map screen's gravity camera mask. RTS/Planet.prefab loses the Gravity Well object bound to the Gravity Field material. Builds: yes (verify passes on Starfire; the new test is unrun until the Yggdrasil job). Co-Authored-By: Claude Opus 5.5 --- Assets/Prefabs/RTS/Planet.prefab | 93 +------------------ .../Editor/GravityFieldParityVerify.cs | 63 +++++++++---- .../GravityShaderMirrorTests.cs | 50 ++++++++++ 3 files changed, 97 insertions(+), 109 deletions(-) create mode 100644 tests/Aetheria.Shared.Tests/GravityShaderMirrorTests.cs diff --git a/Assets/Prefabs/RTS/Planet.prefab b/Assets/Prefabs/RTS/Planet.prefab index dc8300809..b288f30c5 100644 --- a/Assets/Prefabs/RTS/Planet.prefab +++ b/Assets/Prefabs/RTS/Planet.prefab @@ -223,7 +223,6 @@ Transform: m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: - - {fileID: 5321404120083801960} - {fileID: 5321404118931821940} - {fileID: 5321404119494867799} - {fileID: 1093865191529778971} @@ -245,7 +244,7 @@ MonoBehaviour: Label: {fileID: 5321404119494867796} Message: {fileID: 6196070888394201391} Children: [] - GravityMesh: {fileID: 5321404120083801962} + GravityMesh: {fileID: 0} --- !u!1 &5321404118931821943 GameObject: m_ObjectHideFlags: 0 @@ -564,93 +563,3 @@ MonoBehaviour: m_hasFontAssetChanged: 0 m_renderer: {fileID: 5321404119494867795} m_maskType: 0 ---- !u!1 &5321404120083801964 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 5321404120083801960} - - component: {fileID: 5321404120083801963} - - component: {fileID: 5321404120083801962} - m_Layer: 8 - m_Name: Gravity Well - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!4 &5321404120083801960 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 5321404120083801964} - serializedVersion: 2 - m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0} - m_LocalScale: {x: 10, y: 10, z: 1} - m_ConstrainProportionsScale: 0 - m_Children: [] - m_Father: {fileID: 5321404118521994135} - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!33 &5321404120083801963 -MeshFilter: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 5321404120083801964} - m_Mesh: {fileID: 10210, guid: 0000000000000000e000000000000000, type: 0} ---- !u!23 &5321404120083801962 -MeshRenderer: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 5321404120083801964} - m_Enabled: 1 - m_CastShadows: 1 - m_ReceiveShadows: 1 - m_DynamicOccludee: 1 - m_StaticShadowCaster: 0 - m_MotionVectors: 1 - m_LightProbeUsage: 1 - m_ReflectionProbeUsage: 1 - m_RayTracingMode: 2 - m_RayTraceProcedural: 0 - m_RayTracingAccelStructBuildFlagsOverride: 0 - m_RayTracingAccelStructBuildFlags: 1 - m_SmallMeshCulling: 1 - m_ForceMeshLod: -1 - m_MeshLodSelectionBias: 0 - m_RenderingLayerMask: 1 - m_RendererPriority: 0 - m_Materials: - - {fileID: 2100000, guid: 44a8bbb89acb4e34c9f41a9da5842bb4, type: 2} - m_StaticBatchInfo: - firstSubMesh: 0 - subMeshCount: 0 - m_StaticBatchRoot: {fileID: 0} - m_ProbeAnchor: {fileID: 0} - m_LightProbeVolumeOverride: {fileID: 0} - m_ScaleInLightmap: 1 - m_ReceiveGI: 1 - m_PreserveUVs: 0 - m_IgnoreNormalsForChartDetection: 0 - m_ImportantGI: 0 - m_StitchLightmapSeams: 0 - m_SelectedEditorRenderState: 3 - m_MinimumChartSize: 4 - m_AutoUVMaxDistance: 0.5 - m_AutoUVMaxAngle: 89 - m_LightmapParameters: {fileID: 0} - m_GlobalIlluminationMeshLod: 0 - m_SortingLayerID: 0 - m_SortingLayer: 0 - m_SortingOrder: 0 - m_MaskInteraction: 0 - m_AdditionalVertexStreams: {fileID: 0} diff --git a/Assets/Scripts/Editor/GravityFieldParityVerify.cs b/Assets/Scripts/Editor/GravityFieldParityVerify.cs index 343789089..0f459c016 100644 --- a/Assets/Scripts/Editor/GravityFieldParityVerify.cs +++ b/Assets/Scripts/Editor/GravityFieldParityVerify.cs @@ -26,9 +26,9 @@ namespace Aetheria.EditorTools { const string Tag = "[GravityFieldParityVerify]"; const int Size = 256; - // Measured on this tree (the "max error" line this run prints): the largest |texel + GetHeight| over the three - // windows is 0.0059 u, in the planetoid's window (float32 on both sides, the GPU's log against the CPU's, the - // quad's interpolated world position). The tolerance is 5x that. A 0.1% error in the tail constant (.5 -> .501) + // Measured on this tree (the "max error" line this run prints): the largest |texel + GetHeight| over the windows of the three + // frames and windows is 0.0065 u, in the last frame's planetoid window (float32 on both sides, the GPU's log against the CPU's, the + // quad's interpolated world position). The tolerance is 4.6x that. A 0.1% error in the tail constant (.5 -> .501) // moves the drawn height by 0.053 u where all three tails reach (the planetoid and zone windows), 1.8x the // tolerance; a reference of zero moves it by about 150 u. const float Tolerance = 0.03f; @@ -70,16 +70,22 @@ namespace Aetheria.EditorTools var material = AssetDatabase.LoadAssetAtPath("Assets/Materials/Brushes/Gravity Field.mat") ?? throw new InvalidOperationException("No Gravity Field.mat."); - var zone = BuildZone(settings, work, out var bodies); + var zone = BuildZone(settings, work, out var orbits); GravityLowering.Lower(zone, ref wells); field = GravityLowering.CreateField(material); - var sun = Core(zone, 14152f); - var planetoid = Core(zone, 29.2f); - CheckTexels("planetoid", zone, bodies[2], 4f * planetoid); - CheckTexels("sun", zone, bodies[0], 4f * sun); - CheckTexels("zone", zone, new Vector2(0f, 0f), 1.05f * zone.Pack.Radius); - CheckGradient(zone, bodies); + // Three frames through the one buffer, as ZoneRenderer.Update runs it: the bodies as built, then two moves + // (an orbit's Position is what Zone.GetHeight reads). A lowering that mirrors an axis or stops refreshing + // the upload agrees with the sim in the first frame only where it is symmetric, and not after a move. + CheckFrame("built", zone, orbits); + orbits[1].Position = new float2(-420f, 560f); + orbits[2].Position = new float2(900f, -700f); + GravityLowering.Lower(zone, ref wells); + CheckFrame("moved", zone, orbits); + orbits[0].Position = new float2(210f, -95f); + orbits[2].Position = new float2(-1800f, 1300f); + GravityLowering.Lower(zone, ref wells); + CheckFrame("moved-again", zone, orbits); Debug.Log($"{Tag} max error {_maxError:G4} u (tolerance {Tolerance:G4}); max gradient angle {_maxAngle:F2} degrees (limit {GradientDegrees:F0})"); CheckWiring(settings); @@ -102,26 +108,43 @@ namespace Aetheria.EditorTools static float Core(Zone zone, float mass) => zone.Settings.CoreRadius.Evaluate(mass); - // A sun (14152 at the origin), a planet (273.9 at 610) and a planetoid (29.2 at 670): parentless, fixed, so the - // zone's wells sit where the verify says before any Update. - static Zone BuildZone(GameSettings settings, string work, out Vector2[] positions) + static Vector2 At(Orbit orbit) => new Vector2(orbit.Position.x, orbit.Position.y); + + // Orbits 0-2 are the sun, the planet and the planetoid, each window centred where the body is now; orbit 3 is the + // body of no strength. The zone window holds all four. + static void CheckFrame(string frame, Zone zone, Orbit[] orbits) + { + CheckTexels(frame + "-planetoid", zone, At(orbits[2]), 4f * Core(zone, 29.2f)); + CheckTexels(frame + "-sun", zone, At(orbits[0]), 4f * Core(zone, 14152f)); + CheckTexels(frame + "-zone", zone, Vector2.zero, 1.05f * zone.Pack.Radius); + CheckGradient(zone, orbits.Select(At).ToArray()); + } + + // A sun (14152 at (-130, 85)), a planet (273.9 at (610, -240)), a planetoid (29.2 at (670, -270)) and a body of + // mass 0, whose well has no strength (a legal body; its curves give c = 0 and R = 0), at (1700, 300): parentless, + // fixed, off both axes and asymmetric, so the zone's wells sit where the verify says before any Update and a + // mirrored or swapped axis cannot agree with the sim. + static Zone BuildZone(GameSettings settings, string work, out Orbit[] orbits) { var cache = AetheriaStores.Open(Path.Combine(work, "catalog.cc"), Path.Combine(work, "run.cc"), catalogWritable: true); cache.Upsert(new VerseGrammar { Revision = 1 }); cache.FlushAsync().Wait(); var items = new ItemManager(cache, new ProvenanceLedger(), settings.GameplaySettings, _ => { }); - positions = new[] { new Vector2(0f, 0f), new Vector2(610f, 0f), new Vector2(670f, 0f) }; - var masses = new[] { 14152f, 273.9f, 29.2f }; + var positions = new[] { new Vector2(-130f, 85f), new Vector2(610f, -240f), new Vector2(670f, -270f), new Vector2(1700f, 300f) }; + var masses = new[] { 14152f, 273.9f, 29.2f, 0f }; + var keys = new CultRecordKey[positions.Length]; var pack = new ZonePack { Radius = 4182f }; for (var i = 0; i < positions.Length; i++) { - var orbit = cache.Upsert(new OrbitData { FixedPosition = new float2(positions[i].x, positions[i].y) }).Key; + var orbit = keys[i] = cache.Upsert(new OrbitData { FixedPosition = new float2(positions[i].x, positions[i].y) }).Key; var body = cache.Upsert(new PlanetData { Name = "Body " + i, Orbit = new CultRecordRef(orbit), Mass = masses[i] }).Key; pack.Orbits.Add(new CultRecordRef(orbit)); pack.Planets.Add(new CultRecordRef(body)); } - return new Zone(items, settings.PlanetSettings, pack, new GalaxyZone { Name = "Gravity Parity", Owner = null }, null); + var zone = new Zone(items, settings.PlanetSettings, pack, new GalaxyZone { Name = "Gravity Parity", Owner = null }, null); + orbits = keys.Select(k => zone.Orbits[k]).ToArray(); + return zone; } // Renders the field through a downward orthographic camera that culls layer 9 only, centred on `centre` with @@ -244,6 +267,12 @@ namespace Aetheria.EditorTools } if (seen < gravityTargets.Length) Fail("wiring", $"only {seen} gravity cameras found in ARPG.unity"); + // The map screen's gravity camera has no target until MapRenderer gives it one at run time. + var map = UnityEngine.Object.FindFirstObjectByType(FindObjectsInactive.Include); + if (map == null || map.GravityCamera == null) Fail("wiring", "no MapRenderer.GravityCamera in ARPG.unity"); + else if (map.GravityCamera.cullingMask != 1 << GravityLowering.FieldLayer) + Fail("wiring", $"MapRenderer.GravityCamera culls mask {map.GravityCamera.cullingMask}, not layer {GravityLowering.FieldLayer} alone"); + var renderer = UnityEngine.Object.FindFirstObjectByType(FindObjectsInactive.Include); if (renderer == null) Fail("wiring", "no ZoneRenderer in ARPG.unity"); else if (renderer.GravityField == null || renderer.GravityField.shader.name != "Brushes/Gravity Field") diff --git a/tests/Aetheria.Shared.Tests/GravityShaderMirrorTests.cs b/tests/Aetheria.Shared.Tests/GravityShaderMirrorTests.cs new file mode 100644 index 000000000..e4f182e25 --- /dev/null +++ b/tests/Aetheria.Shared.Tests/GravityShaderMirrorTests.cs @@ -0,0 +1,50 @@ +using System.IO; +using System.Text.RegularExpressions; +using Xunit; + +// potential-owns-gravity (aetheria-gravity, cut grid-lowering): Assets/Shaders/Gravity.cginc lowers the potential for +// the GPU and decides nothing, so its gravityPotential must be GravityField.Potential, term for term, zero-strength +// guard and core branch included. The shader cannot be compiled here (GravityFieldParityVerify renders it, on Starfire +// only), so this test is the automated guard: each copy's expression is read from its file, reduced to the same +// spelling (float suffixes, the core radius's name, whitespace and comments), and compared. A drift in either file fails it. +public sealed class GravityShaderMirrorTests +{ + private static string Read(string relative) => + File.ReadAllText(Path.Combine(RunStartTests.FindRepoRoot(), relative)); + + // The text between `start` and the first `;` after it, comments removed and spellings reduced. + private static string Expression(string source, string start, string then = "") + { + source = Regex.Replace(source, @"//[^\n]*", ""); + var from = source.IndexOf(start, System.StringComparison.Ordinal); + Assert.True(from >= 0, $"'{start}' not found"); + var body = source.Substring(from + start.Length); + if (then != "") + { + var at = body.IndexOf(then, System.StringComparison.Ordinal); + Assert.True(at >= 0, $"'{then}' not found after '{start}'"); + body = body.Substring(at + then.Length); + } + body = body.Substring(0, body.IndexOf(';')); + body = Regex.Replace(body, @"(?<=[\d.])f\b", ""); + body = body.Replace("coreRadius", "R"); + return Regex.Replace(body, @"\s+", ""); + } + + private static string CSharpLaw() => + Expression(Read("Assets/Scripts/ServerShared/GravityField.cs"), "Potential(float c, float coreRadius, float r) =>"); + + private static string ShaderLaw() => + Expression(Read("Assets/Shaders/Gravity.cginc"), "float gravityPotential(float c, float R, float r)", "return"); + + [Fact] + public void TheShaderRunsTheSameLawAsGravityFieldPotential() + { + var law = CSharpLaw(); + // The reduction found the whole law, so equality is not two empty strings agreeing. + Assert.Contains("log(r/R)", law); + Assert.Contains("c==0?0:", law); + Assert.Contains("c*r*r/(2*R*R)", law); + Assert.Equal(law, ShaderLaw()); + } +}