From 3d8a0d1d344f8dbaebfd6e5ce58438f1ebbafb59 Mon Sep 17 00:00:00 2001 From: soul Date: Mon, 30 Nov 2026 23:03:15 +0100 Subject: [PATCH] Isoline render smoke: s quality on the map pose, gated against the finite-difference readings; camera field render cost Ports Soul's chain measurement (BuildChains, EdgesOf, percentiles), E1 dash runs, flow-frame ratio and the two fine-resolution kinks; writes flow-after.png and e1-after.png. Cost reports Sample alone and the camera field render the extractor replaces. Co-Authored-By: Claude Sonnet 5.5 --- Assets/Scripts/Editor/IsolineRenderSmoke.cs | 322 ++++++++++++++++++++ 1 file changed, 322 insertions(+) diff --git a/Assets/Scripts/Editor/IsolineRenderSmoke.cs b/Assets/Scripts/Editor/IsolineRenderSmoke.cs index 01e029eef..ed94ce185 100644 --- a/Assets/Scripts/Editor/IsolineRenderSmoke.cs +++ b/Assets/Scripts/Editor/IsolineRenderSmoke.cs @@ -45,6 +45,14 @@ namespace Aetheria.EditorTools const float WrongWayBound = 0.045f; const float FlowLagBound = 3.5f; + // s quality on the map pose (margin rule against Soul's readings on ab17b688b, the finite-difference mutant; the + // shipped reading is this cut's). Rays |ds|/L (1 is arclength): p5 shipped 0.349 against 0.193 (higher is better), p50 1.00 + // against 1.63, p75 1.00 against 7.34, p95 11.4 against 38.7; non-isometric share 19.1% against 57.3%; s sign flips at + // joints 12.2 against 258.4 per 1000; near the deepest well |s - r| / r p50 1.9e-6 against 5.1e-3 (its p95 reads 0.851 on + // both: not separated, unclaimed). Each bound is the geometric mean, separations 1.35, 1.28, 2.7, 1.8, 1.7, 4.6, 52. + const double RayP5Floor = 0.2595, RayP50Ceiling = 1.277, RayP75Ceiling = 2.709, RayP95Ceiling = 21.0; + const double NonIsometricCeiling = 0.3308, SignFlipCeiling = 56.1, NearWellP50Ceiling = 9.86e-5; + static bool _ok = true; static string _out; static string _wrote = ""; @@ -280,6 +288,7 @@ namespace Aetheria.EditorTools var terms = GravityLowering.Lower(zone, 0f, ref wells); field = GravityLowering.CreateField(material); Guarded("flow follows depth near the real-zone saddle", () => FlowOnRealZone(zone, orbits, terms)); + Guarded("s quality on the map pose", () => SQuality(zone, orbits, terms)); Guarded("stress field and the shipped pool", StressAndPool); Guarded("captures and cost", () => CapturesAndCost(zone, orbits, terms)); Info(_ok ? "PASS" : "FAIL: see errors above"); @@ -471,6 +480,281 @@ namespace Aetheria.EditorTools finally { view.Dispose(); } } + // ---- s quality on the map pose (verdict cut-isoline-renderer.s1, findings ray-dashes-alias-from-s-jitter and + // ray-noise-grows-as-texel-shrinks) -------------------------------------------------------------------------------- + + sealed class Chain + { + public List Seg = new List(); + public List Rev = new List(); + } + + sealed class Edge + { + public Vector2 P0, P1; + public float S0, S1, L; + public int Seg; + public bool Rev; + public float Ds => S1 - S0; + } + + static (int, int) PointKey(Vector2 p) => (BitConverter.SingleToInt32Bits(p.x), BitConverter.SingleToInt32Bits(p.y)); + + // The polylines of one family: segments joined where exactly two endpoints share a bit-equal point. + static List BuildChains(Seg[] segs, uint family) + { + var ends = new Dictionary<(int, int), List<(int seg, int end)>>(); + void Add(Vector2 p, int seg, int end) + { + var k = PointKey(p); + if (!ends.TryGetValue(k, out var l)) ends[k] = l = new List<(int, int)>(); + l.Add((seg, end)); + } + for (var i = 0; i < segs.Length; i++) + { + if (((segs[i].key >> 8) & 1u) != family) continue; + Add(segs[i].a, i, 0); + Add(segs[i].b, i, 1); + } + var used = new bool[segs.Length]; + var chains = new List(); + for (var i = 0; i < segs.Length; i++) + { + if (((segs[i].key >> 8) & 1u) != family || used[i]) continue; + var c = new Chain(); + c.Seg.Add(i); c.Rev.Add(false); used[i] = true; + while (true) + { + var last = c.Seg[c.Seg.Count - 1]; + var rev = c.Rev[c.Rev.Count - 1]; + var l = ends[PointKey(rev ? segs[last].a : segs[last].b)]; + if (l.Count != 2) break; + var o = l[0].seg == last ? l[1] : l[0]; + if (used[o.seg]) break; + used[o.seg] = true; c.Seg.Add(o.seg); c.Rev.Add(o.end == 1); + } + while (true) + { + var first = c.Seg[0]; + var rev = c.Rev[0]; + var l = ends[PointKey(rev ? segs[first].b : segs[first].a)]; + if (l.Count != 2) break; + var o = l[0].seg == first ? l[1] : l[0]; + if (used[o.seg]) break; + used[o.seg] = true; c.Seg.Insert(0, o.seg); c.Rev.Insert(0, o.end == 0); + } + chains.Add(c); + } + return chains; + } + + static List EdgesOf(Seg[] segs, Chain c) + { + var list = new List(); + for (var i = 0; i < c.Seg.Count; i++) + { + var s = segs[c.Seg[i]]; + var r = c.Rev[i]; + var e = new Edge { Seg = c.Seg[i], Rev = r, P0 = r ? s.b : s.a, P1 = r ? s.a : s.b, S0 = r ? s.sB : s.sA, S1 = r ? s.sA : s.sB }; + e.L = (e.P1 - e.P0).magnitude; + list.Add(e); + } + return list; + } + + static double Pct(List v, double q) + { + if (v.Count == 0) return double.NaN; + var a = v.OrderBy(x => x).ToList(); + return a[Mathf.Clamp((int)(q * (a.Count - 1)), 0, a.Count - 1)]; + } + + static string Pcts(List v) => $"p5 {Pct(v, .05):G3} p25 {Pct(v, .25):G3} p50 {Pct(v, .5):G3} p75 {Pct(v, .75):G3} p95 {Pct(v, .95):G3}"; + + // Records the extraction through the terms on the caller grid and the draw into target, saves the PNG and returns the pixels. + static Frame RecordFrame(in GravityLowering.GravityTerms terms, Vector2 origin, Vector2 perTexel, Vector2Int size, + IsolineExtractor.IsolineLevels levels, View view, IsolineLook look, string name, float time = 0f) + { + var renderer = new IsolineRenderer(ExtractShader, RenderShader, ShippedSegments, ShippedPool); + var target = HalfTarget(view.W, view.H); + try + { + RecordAndSave(renderer, terms, origin, perTexel, size, levels, view.ViewProjection, target, look, name, time); + return new Frame { W = view.W, H = view.H, Px = Read(target) }; + } + finally + { + renderer.Release(); + target.Release(); + UnityEngine.Object.DestroyImmediate(target); + } + } + + static Seg[] ExtractThrough(in GravityLowering.GravityTerms terms, Vector2 origin, Vector2 perTexel, Vector2Int size, IsolineExtractor.IsolineLevels levels) + { + var extractor = new IsolineExtractor(ExtractShader, ShippedSegments); + var command = new CommandBuffer { name = "IsolineSQuality" }; + try + { + extractor.Extract(command, terms, origin, perTexel, size, levels); + Graphics.ExecuteCommandBuffer(command); + return ReadExtraction(extractor).Item1; + } + finally + { + command.Release(); + extractor.Release(); + } + } + + // The real zone at the map pose (1920 x 1080 at 2.04 u/px) through the extractor, its ray and contour chains measured: + // the |ds| / L of every edge (1 is true arclength), s sign flips at joints, and |s - r| / r near the deepest well, where + // the radius is the truth. Readings against Soul's at ab17b688b (finite-difference kappa, unity-r1.log), which are the + // mutant of this cut. Then the dash runs along 20 ray chains at the map pose, the flow frame's |ds| / L at 1.17 u/px, the + // two named kinks at 0.117 u/texel, and the captures flow-after.png and e1-after.png. + static void SQuality(Zone zone, Orbit[] orbits, GravityLowering.GravityTerms terms) + { + var range = ContourRange(zone, orbits); + var levels = new IsolineExtractor.IsolineLevels(0f, range, zone.Pack.Radius, SlopeEpsilon); + var mapView = Ortho(ZoneCentre, 1100f, 1920, 1080); + try + { + var ppx = 2200f / 1080f; + IsolineExtractSmoke.TopFrame(ZoneCentre, 1100f, 1920, 1080, out var mapOrigin, out var mapPerTexel); + var map = ExtractThrough(terms, mapOrigin, mapPerTexel, new Vector2Int(1920, 1080), levels); + var deepest = orbits.OrderByDescending(o => -zone.GetHeight(new float2(o.Position.x, o.Position.y))).First(); + var well1 = new Vector2(deepest.Position.x, deepest.Position.y); + var rays = BuildChains(map, 1u); + var contours = BuildChains(map, 0u); + Info($"s quality: map pose, {map.Length} segments, {rays.Count} ray chains, {contours.Count} contour chains"); + foreach (var (label, chains) in new[] { ("rays", rays), ("contours", contours) }) + { + var ratio = new List(); + var near = new List(); + int edges = 0, nonIso = 0, joints = 0, flips = 0; + foreach (var chain in chains) + { + var e = EdgesOf(map, chain); + for (var i = 0; i < e.Count; i++) + { + edges++; + var r = e[i].L > 1e-6f ? Mathf.Abs(e[i].Ds) / e[i].L : 0f; + ratio.Add(r); + if (r < .5f || r > 2f) nonIso++; + var mid = (e[i].P0 + e[i].P1) * .5f; + if (label == "rays" && (mid - well1).magnitude < 900f + && orbits.All(o => o == deepest || (mid - new Vector2(o.Position.x, o.Position.y)).magnitude > 250f)) + near.Add(Mathf.Abs((e[i].S0 + e[i].S1) * .5f - (mid - well1).magnitude) / Mathf.Max((mid - well1).magnitude, 1f)); + if (i + 1 < e.Count) + { + joints++; + if (Sgn(e[i].Ds) * Sgn(e[i + 1].Ds) < 0f) flips++; + } + } + } + if (label == "rays") + { + var nonIsoShare = nonIso / (double)Math.Max(edges, 1); + var flipRate = flips * 1000.0 / Math.Max(joints, 1); + if (Pct(ratio, .05) < RayP5Floor) Fail("s quality", $"rays |ds|/L p5 {Pct(ratio, .05):G4} < {RayP5Floor:G4}"); + if (Pct(ratio, .5) > RayP50Ceiling) Fail("s quality", $"rays |ds|/L p50 {Pct(ratio, .5):G4} > {RayP50Ceiling:G4}"); + if (Pct(ratio, .75) > RayP75Ceiling) Fail("s quality", $"rays |ds|/L p75 {Pct(ratio, .75):G4} > {RayP75Ceiling:G4}"); + if (Pct(ratio, .95) > RayP95Ceiling) Fail("s quality", $"rays |ds|/L p95 {Pct(ratio, .95):G4} > {RayP95Ceiling:G4}"); + if (nonIsoShare > NonIsometricCeiling) Fail("s quality", $"non-isometric share {nonIsoShare:P1} > {NonIsometricCeiling:P1}"); + if (flipRate > SignFlipCeiling) Fail("s quality", $"s sign flips {flipRate:F1}/1000 > {SignFlipCeiling:F1}"); + if (near.Count < 1000 || Pct(near, .5) > NearWellP50Ceiling) Fail("s quality", $"near well 1: {near.Count} edges, |s-r|/r p50 {Pct(near, .5):G4} > {NearWellP50Ceiling:G4}"); + } + Info($"s quality {label}: {edges} edges; |ds|/L {Pcts(ratio)}; non-isometric (<0.5 or >2) {nonIso / (double)Math.Max(edges, 1):P1}; s sign flips at joints {flips * 1000.0 / Math.Max(joints, 1):F1}/1000" + + (near.Count > 0 ? $"; near well 1 ({near.Count} edges) |s-r|/r p50 {Pct(near, .5):G3} p95 {Pct(near, .95):G3}" : "")); + } + + // dash runs along the ray chains at the map pose, rays only, Period 160 u (78.5 px) + var rayLook = IsolineLook.Default; + rayLook.Contour.W = 0f; rayLook.Contour.G = 0f; rayLook.Contour.Bloom = 0f; rayLook.Ray.Bloom = 0f; + var e1 = RecordFrame(terms, mapOrigin, mapPerTexel, new Vector2Int(1920, 1080), levels, mapView, rayLook, "e1-after.png"); + var rates = new List(); + var runs = new List(); + foreach (var chain in rays) + { + var e = EdgesOf(map, chain); + if (e.Sum(x => x.L) / ppx < 150f) continue; + var samples = new List(); + foreach (var edge in e) + { + var steps = Mathf.Max(1, Mathf.RoundToInt(edge.L / ppx)); + for (var q = 0; q < steps; q++) + { + var p = Vector2.Lerp(edge.P0, edge.P1, (q + .5f) / steps); + Project(mapView, p, p, out var pa, out _, out _, out _); + var ix = Mathf.Clamp((int)pa.x, 0, 1919); + var iy = Mathf.Clamp((int)pa.y, 0, 1079); + var m = 0f; + for (var dy = -1; dy <= 1; dy++) + for (var dx = -1; dx <= 1; dx++) m = Mathf.Max(m, e1.At(Mathf.Clamp(ix + dx, 0, 1919), Mathf.Clamp(iy + dy, 0, 1079)).w); + samples.Add(m); + } + } + if (samples.Count < 100) continue; + var threshold = .5f * samples.Max(); + if (threshold < .02f) continue; + var on = samples.Select(v => v > threshold).ToList(); + var run = 1; + for (var q = 1; q < on.Count; q++) + { + if (on[q] != on[q - 1]) { runs.Add(run); run = 1; } + else run++; + } + } + Info($"dash runs (E1): {runs.Count} runs along the ray chains over 150 px; run length px {Pcts(runs)} (Soul's shipped p5 1, p25 7, p50 38; true arclength p5 28, p25 45, p50 51)"); + + // the flow frame pose around the saddle (Period 60 u, 512 x 512 over 600 u = 1.17 u/px) + var flowView = Ortho(Saddle, 300f, 512, 512); + try + { + IsolineExtractSmoke.TopFrame(Saddle, 300f, 512, 512, out var flowOrigin, out var flowPerTexel); + var flow = ExtractThrough(terms, flowOrigin, flowPerTexel, new Vector2Int(512, 512), levels); + var flowRatio = new List(); + foreach (var chain in BuildChains(flow, 1u)) + foreach (var edge in EdgesOf(flow, chain)) flowRatio.Add(edge.L > 1e-6f ? Mathf.Abs(edge.Ds) / edge.L : 0f); + Info($"flow frame: {flowRatio.Count} ray edges at 1.17 u/px, |ds|/L {Pcts(flowRatio)} (Soul's shipped p95 62)"); + var flowLook = IsolineLook.Default; + flowLook.Ray.Period = 60f; + flowLook.Ray.Speed = 1f; + RecordFrame(terms, flowOrigin, flowPerTexel, new Vector2Int(512, 512), levels, flowView, flowLook, "flow-after.png"); + } + finally { flowView.Dispose(); } + + // the two named kinks at 0.117 u/texel (a 120 u crop over 1024 samples) against the map's 2.04 + foreach (var (kink, index) in new[] { (new Vector2(586.5f, -625f), 0), (new Vector2(993.5f, -219.7f), 1) }) + { + IsolineExtractSmoke.TopFrame(kink, 60f, 1024, 1024, out var fineOrigin, out var finePerTexel); + var fine = ExtractThrough(terms, fineOrigin, finePerTexel, new Vector2Int(1024, 1024), levels); + float WindowTurn(Seg[] segs) + { + var best = 0f; + foreach (var chain in BuildChains(segs, 1u)) + { + var e = EdgesOf(segs, chain); + var cumulative = new float[e.Count + 1]; + for (var i = 0; i < e.Count; i++) cumulative[i + 1] = cumulative[i] + e[i].L; + for (var i = 1; i < e.Count; i++) + { + if ((e[i].P0 - kink).magnitude > 25f) continue; + int j = i, k = i; + while (j > 0 && cumulative[i] - cumulative[j] < 6f) j--; + while (k < e.Count - 1 && cumulative[k] - cumulative[i] < 6f) k++; + if (cumulative[i] - cumulative[j] < 6f || cumulative[k] - cumulative[i] < 6f) continue; + best = Mathf.Max(best, Vector2.Angle(e[i].P0 - e[j].P0, e[k].P0 - e[i].P0)); + } + } + return best; + } + Info($"kink {index} at {kink}: max net turn over 6 u windows {WindowTurn(map):F0} deg at map resolution, {WindowTurn(fine):F0} deg at {finePerTexel.x:F3} u/texel (Soul's shipped: {(index == 0 ? "49 and 180" : "24 and 172")})"); + } + } + finally { mapView.Dispose(); } + } + static void Guarded(string name, Action check) { try { check(); } @@ -1118,6 +1402,7 @@ namespace Aetheria.EditorTools for (var phase = 0; phase < 6; phase++) RecordAndSave(flowRenderer, terms, fOrigin, fPerTexel, new Vector2Int(512, 512), levels, flowView.ViewProjection, flowTarget, flowLook, $"flow-{phase}.png", phase / 6f); + Info($"cost camera field render (the path the extractor replaces), wall clock to a fence, median of 31: 128 x 128 {FieldRenderMs(sceneCamera, 128, 128):F3} ms; 1920 x 1080 {FieldRenderMs(mapView.Camera, 1920, 1080):F3} ms"); Cost("256 x 256 (minimap)", miniRenderer, terms, new Vector2Int(128, 128), minimapField, miniOrigin, miniPerTexel, levels, miniVp, miniTarget, look, minimapMaterial, 10f, range); Cost("1920 x 1080 (map)", mapRenderer, terms, new Vector2Int(1920, 1080), mapField, mapOrigin, mapPerTexel, levels, mapView.ViewProjection, mapTarget, look, mapMaterial, mapScale / 2f, range); var stressTerms = IsolineExtractSmoke.WellsFrom(StressWells(out var stressRange)); @@ -1137,6 +1422,43 @@ namespace Aetheria.EditorTools mapView.Dispose(); tacticalCamera.Dispose(); tacticalView.Dispose(); flowView.Dispose(); stressView.Dispose(); } + // Wall clock of one camera render of the gravity field (the path the extractor replaces) into an RFloat target of the same + // size, timed to a 1 pixel readback: median of 31 after 2 warm-ups. + static double FieldRenderMs(Camera camera, int w, int h) + { + var target = new RenderTexture(w, h, 0, RenderTextureFormat.RFloat) { filterMode = FilterMode.Point }; + target.Create(); + var previousTarget = camera.targetTexture; + var previousMask = camera.cullingMask; + var previousClear = camera.clearFlags; + var previousColour = camera.backgroundColor; + camera.targetTexture = target; + camera.cullingMask = 1 << GravityLowering.FieldLayer; + camera.clearFlags = CameraClearFlags.SolidColor; + camera.backgroundColor = Color.black; + var one = new Texture2D(1, 1, TextureFormat.RFloat, false); + var times = new List(); + for (var i = -2; i < 31; i++) + { + var clock = Stopwatch.StartNew(); + camera.Render(); + var previous = RenderTexture.active; + RenderTexture.active = target; + one.ReadPixels(new Rect(0, 0, 1, 1), 0, 0); + RenderTexture.active = previous; + clock.Stop(); + if (i >= 0) times.Add(clock.Elapsed.TotalMilliseconds); + } + camera.targetTexture = previousTarget; + camera.cullingMask = previousMask; + camera.clearFlags = previousClear; + camera.backgroundColor = previousColour; + UnityEngine.Object.DestroyImmediate(one); + target.Release(); + UnityEngine.Object.DestroyImmediate(target); + return Median(times); + } + // Today's look: the field through a gravity-lines material into an ARGB32 target. static void Blit(Material asset, RenderTexture field, int w, int h, float range, float scale, string name) {