diff --git a/Assets/Scripts/Zone Display/IsolineExtractor.cs b/Assets/Scripts/Zone Display/IsolineExtractor.cs
new file mode 100644
index 000000000..4e216168c
--- /dev/null
+++ b/Assets/Scripts/Zone Display/IsolineExtractor.cs
@@ -0,0 +1,149 @@
+using System.Runtime.InteropServices;
+using UnityEngine;
+using UnityEngine.Experimental.Rendering;
+using UnityEngine.Rendering;
+
+///
+/// The C# side of isoline extraction (campaign aetheria-release, cut isoline-extraction): the one owner of the segment
+/// buffer. Extract records the dispatches of Assets/Shaders/IsolineExtract.compute onto a command buffer; the compute
+/// shader decides which segments exist and every attribute they carry. Nothing here reads a result back or edits a
+/// segment, and nothing in the frame calls it yet: the renderer that consumes Segments and Count is a later cut.
+///
+public sealed class IsolineExtractor
+{
+ /// Mirrors IsoSegment of Assets/Shaders/IsolineSegment.hlsl: 12 words.
+ [StructLayout(LayoutKind.Sequential)]
+ public struct IsoSegment
+ {
+ public Vector2 a, b;
+ public float sA, sB;
+ public float slopeA, slopeB;
+ public float depthA, depthB;
+ public uint key;
+ public uint cell;
+ }
+
+ /// The levels to extract and the clamps of the along-line parameter. Contour levels are
+ /// Start + i * Range / (ContourCount - 1), i = 1..ContourCount.
+ public readonly struct IsolineLevels
+ {
+ public readonly float Start, Range, RhoMax, SlopeEpsilon;
+ public readonly int ContourCount, RayCount;
+
+ public IsolineLevels(float start, float range, float rhoMax, float slopeEpsilon, int contourCount = 21, int rayCount = 12)
+ {
+ Start = start;
+ Range = range;
+ RhoMax = rhoMax;
+ SlopeEpsilon = slopeEpsilon;
+ ContourCount = contourCount;
+ RayCount = rayCount;
+ }
+ }
+
+ static readonly int Field = Shader.PropertyToID("_Field");
+ static readonly int GradOut = Shader.PropertyToID("_GradOut");
+ static readonly int GradIn = Shader.PropertyToID("_GradIn");
+ static readonly int CurveOut = Shader.PropertyToID("_CurveOut");
+ static readonly int CurveIn = Shader.PropertyToID("_CurveIn");
+ static readonly int SegmentsId = Shader.PropertyToID("_Segments");
+ static readonly int CountId = Shader.PropertyToID("_Count");
+ static readonly uint[] Zero = new uint[2];
+
+ readonly ComputeShader _shader;
+ readonly int _derive, _curve, _march, _finish;
+ RenderTexture _grad, _curvature;
+
+ /// The segment buffer, IsoSegment[backing]; overwritten by each Extract.
+ public GraphicsBuffer Segments { get; }
+
+ /// uint[2]: [0] segments written (at most Capacity), [1] segments requested (overflow when above Capacity).
+ public GraphicsBuffer Count { get; }
+
+ /// The most segments Extract writes.
+ public int Capacity { get; }
+
+ /// Segments the buffer holds, at least capacity; Extract never writes past Capacity.
+ /// Larger only where a check needs room past the guard.
+ public IsolineExtractor(ComputeShader shader, int capacity, int backing = 0)
+ {
+ _shader = shader;
+ Capacity = capacity;
+ _derive = shader.FindKernel("Derive");
+ _curve = shader.FindKernel("Curve");
+ _march = shader.FindKernel("March");
+ _finish = shader.FindKernel("Finish");
+ Segments = new GraphicsBuffer(GraphicsBuffer.Target.Structured, Mathf.Max(capacity, backing), Marshal.SizeOf());
+ Count = new GraphicsBuffer(GraphicsBuffer.Target.Structured, 2, sizeof(uint));
+ }
+
+ ///
+ /// Records the extraction of one field texture. The field is d per texel (deeper is larger); texel (x, y)'s centre sits
+ /// at worldOrigin + (x, y) * worldPerTexel in world xz, the step signed per axis as the camera that drew it has it.
+ ///
+ public void Extract(CommandBuffer cmd, RenderTexture field, Vector2 worldOrigin, Vector2 worldPerTexel, IsolineLevels levels)
+ {
+ if (_grad == null || _grad.width != field.width || _grad.height != field.height)
+ {
+ Release(ref _grad);
+ Release(ref _curvature);
+ _grad = Scratch(field, GraphicsFormat.R32G32B32A32_SFloat);
+ _curvature = Scratch(field, GraphicsFormat.R32G32_SFloat);
+ }
+
+ cmd.SetBufferData(Count, Zero);
+ cmd.SetComputeIntParams(_shader, "_Size", field.width, field.height);
+ cmd.SetComputeVectorParam(_shader, "_Origin", worldOrigin);
+ cmd.SetComputeVectorParam(_shader, "_PerTexel", worldPerTexel);
+ cmd.SetComputeFloatParam(_shader, "_Start", levels.Start);
+ cmd.SetComputeFloatParam(_shader, "_Range", levels.Range);
+ cmd.SetComputeIntParam(_shader, "_ContourCount", levels.ContourCount);
+ cmd.SetComputeIntParam(_shader, "_RayCount", levels.RayCount);
+ cmd.SetComputeFloatParam(_shader, "_RhoMax", levels.RhoMax);
+ cmd.SetComputeFloatParam(_shader, "_SlopeEpsilon", levels.SlopeEpsilon);
+ cmd.SetComputeIntParam(_shader, "_Capacity", Capacity);
+
+ var groupsX = (field.width + 7) / 8;
+ var groupsY = (field.height + 7) / 8;
+ cmd.SetComputeTextureParam(_shader, _derive, Field, field);
+ cmd.SetComputeTextureParam(_shader, _derive, GradOut, _grad);
+ cmd.DispatchCompute(_shader, _derive, groupsX, groupsY, 1);
+
+ cmd.SetComputeTextureParam(_shader, _curve, GradIn, _grad);
+ cmd.SetComputeTextureParam(_shader, _curve, CurveOut, _curvature);
+ cmd.DispatchCompute(_shader, _curve, groupsX, groupsY, 1);
+
+ cmd.SetComputeTextureParam(_shader, _march, GradIn, _grad);
+ cmd.SetComputeTextureParam(_shader, _march, CurveIn, _curvature);
+ cmd.SetComputeBufferParam(_shader, _march, SegmentsId, Segments);
+ cmd.SetComputeBufferParam(_shader, _march, CountId, Count);
+ cmd.DispatchCompute(_shader, _march, groupsX, groupsY, 1);
+
+ cmd.SetComputeBufferParam(_shader, _finish, CountId, Count);
+ cmd.DispatchCompute(_shader, _finish, 1, 1, 1);
+ }
+
+ public void Release()
+ {
+ Release(ref _grad);
+ Release(ref _curvature);
+ Segments.Release();
+ Count.Release();
+ }
+
+ static RenderTexture Scratch(RenderTexture like, GraphicsFormat format)
+ {
+ var texture = new RenderTexture(like.width, like.height, 0, format)
+ { enableRandomWrite = true, filterMode = FilterMode.Point, hideFlags = HideFlags.HideAndDontSave };
+ texture.Create();
+ return texture;
+ }
+
+ static void Release(ref RenderTexture texture)
+ {
+ if (texture == null) return;
+ texture.Release();
+ Object.DestroyImmediate(texture);
+ texture = null;
+ }
+}
diff --git a/Assets/Scripts/Zone Display/IsolineExtractor.cs.meta b/Assets/Scripts/Zone Display/IsolineExtractor.cs.meta
new file mode 100644
index 000000000..2857574fe
--- /dev/null
+++ b/Assets/Scripts/Zone Display/IsolineExtractor.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: 77d8c657abd46634d99b3eeb4b5381e1
\ No newline at end of file
diff --git a/Assets/Shaders/IsolineExtract.compute b/Assets/Shaders/IsolineExtract.compute
new file mode 100644
index 000000000..f19373ebb
--- /dev/null
+++ b/Assets/Shaders/IsolineExtract.compute
@@ -0,0 +1,209 @@
+// Isoline extraction on the GPU (campaign aetheria-release, cut isoline-extraction). Reads one gravity field texture
+// (d = -Zone.GetHeight per texel, written only by Brushes/Gravity Field) and writes world-space line segments of two
+// isoline families into a segment buffer, with no readback:
+// contours: d = _Start + i * _Range / (_ContourCount - 1), i = 1.._ContourCount (the 21 levels of IsolinesUI.shader)
+// rays: theta = (k + .5) * 2pi / _RayCount - pi, k = 0.._RayCount-1, where theta is the angle of the HEIGHT gradient
+// (-grad d), the angle the iso-angle loop of IsolinesUI.shader reads, so the level index k matches it
+// Kernels, in dispatch order: Derive (gradient), Curve (level-set curvature radius), March (marching squares), Finish.
+// The count buffer is cleared by the recorded command list before March (IsolineExtractor).
+#pragma kernel Derive
+#pragma kernel Curve
+#pragma kernel March
+#pragma kernel Finish
+
+#include "IsolineSegment.hlsl"
+
+#define PI 3.14159265358979
+#define TWO_PI 6.28318530717959
+
+Texture2D _Field;
+RWTexture2D _GradOut; // Derive: (gx, gy, |g|, d), gradient of d in world units per world unit
+Texture2D _GradIn; // Curve, March
+RWTexture2D _CurveOut; // Curve: (theta, rho)
+Texture2D _CurveIn; // March
+RWStructuredBuffer _Segments;
+RWStructuredBuffer _Count;
+
+int2 _Size; // field size in texels
+float2 _Origin; // world xz of texel (0, 0)'s centre
+float2 _PerTexel; // world xz step per texel; signed per axis
+float _Start;
+float _Range;
+int _ContourCount;
+int _RayCount;
+float _RhoMax;
+float _SlopeEpsilon;
+uint _Capacity;
+
+float Texel() { return max(abs(_PerTexel.x), abs(_PerTexel.y)); }
+float WrapPi(float x) { return x - TWO_PI * round(x / TWO_PI); }
+float FieldAt(int2 p) { return _Field.Load(int3(p, 0)); }
+
+[numthreads(8, 8, 1)]
+void Derive(uint3 id : SV_DispatchThreadID)
+{
+ if (any(int2(id.xy) >= _Size)) return;
+ int2 p = int2(id.xy);
+ int2 lo = max(p - 1, 0);
+ int2 hi = min(p + 1, _Size - 1);
+ float gx = (FieldAt(int2(hi.x, p.y)) - FieldAt(int2(lo.x, p.y))) / ((hi.x - lo.x) * _PerTexel.x);
+ float gy = (FieldAt(int2(p.x, hi.y)) - FieldAt(int2(p.x, lo.y))) / ((hi.y - lo.y) * _PerTexel.y);
+ _GradOut[p] = float4(gx, gy, sqrt(gx * gx + gy * gy), FieldAt(p));
+}
+
+float2 UnitGrad(int2 p)
+{
+ float3 g = _GradIn.Load(int3(p, 0)).xyz;
+ return g.z > 1e-20 ? g.xy / g.z : float2(0, 0);
+}
+
+// kappa = div(grad d / |grad d|), the level set's curvature; rho = 1 / |kappa|, the radius of the contour through the
+// texel (the distance to the centre for any radial well, whatever its profile). theta is the angle of -grad d.
+[numthreads(8, 8, 1)]
+void Curve(uint3 id : SV_DispatchThreadID)
+{
+ if (any(int2(id.xy) >= _Size)) return;
+ int2 p = int2(id.xy);
+ int2 lo = max(p - 1, 0);
+ int2 hi = min(p + 1, _Size - 1);
+ float kappa = (UnitGrad(int2(hi.x, p.y)).x - UnitGrad(int2(lo.x, p.y)).x) / ((hi.x - lo.x) * _PerTexel.x)
+ + (UnitGrad(int2(p.x, hi.y)).y - UnitGrad(int2(p.x, lo.y)).y) / ((hi.y - lo.y) * _PerTexel.y);
+ float rho = abs(kappa) > 1e-12 ? 1.0 / abs(kappa) : _RhoMax;
+ rho = clamp(rho, Texel(), _RhoMax);
+ float3 g = _GradIn.Load(int3(p, 0)).xyz;
+ float theta = g.z > 0 ? atan2(-g.y, -g.x) : 0;
+ _CurveOut[p] = float2(theta, rho);
+}
+
+// One cell's corners, in the order (x, y), (x+1, y), (x+1, y+1), (x, y+1); edges 0..3 join corners 0-1, 1-2, 3-2, 0-3.
+static uint gIdx[4];
+static float2 gPos[4];
+static float gVal[4]; // the scalar being contoured: d for contours, the cell-unwrapped theta for rays
+static float gS[4];
+static float gSlope[4];
+static float gDepth[4];
+static uint gCell;
+static const uint2 kEdge[4] = { uint2(0, 1), uint2(1, 2), uint2(3, 2), uint2(0, 3) };
+// Edge pair (e0 | e1 << 4) of the one segment of each non-saddle case; 0xFF none or saddle.
+static const uint kSegment[16] = { 0xFF, 0x03, 0x10, 0x13, 0x21, 0xFF, 0x20, 0x32, 0x32, 0x20, 0xFF, 0x21, 0x13, 0x10, 0x03, 0xFF };
+
+// One crossing of `level` on an edge, a function of (edge, level) alone: the corners go in canonical order (lower linear
+// texel index first), so both cells sharing the edge compute the same floats bit for bit.
+void Crossing(uint edge, float level, out float2 p, out float s, out float slope, out float depth)
+{
+ uint a = kEdge[edge].x;
+ uint b = kEdge[edge].y;
+ if (gIdx[a] > gIdx[b]) { uint w = a; a = b; b = w; }
+ precise float t = saturate((level - gVal[a]) / (gVal[b] - gVal[a]));
+ precise float2 q = gPos[a] + (gPos[b] - gPos[a]) * t;
+ p = q;
+ s = lerp(gS[a], gS[b], t);
+ slope = lerp(gSlope[a], gSlope[b], t);
+ depth = lerp(gDepth[a], gDepth[b], t);
+}
+
+void Emit(uint e0, uint e1, float level, uint key)
+{
+ IsoSegment seg;
+ Crossing(e0, level, seg.a, seg.sA, seg.slopeA, seg.depthA);
+ Crossing(e1, level, seg.b, seg.sB, seg.slopeB, seg.depthB);
+ if (all(seg.a == seg.b)) return;
+ seg.key = key;
+ seg.cell = gCell;
+ uint slot;
+ InterlockedAdd(_Count[1], 1, slot);
+ if (slot < _Capacity) _Segments[slot] = seg;
+}
+
+// Marching squares for one level of gVal. Corners at or above the level are high. The saddle cases 5 and 10 resolve by
+// the cell centre (the mean of the four corners): if it is high, the high corners connect.
+void March1(float level, uint key)
+{
+ uint c = (gVal[0] >= level ? 1u : 0u) | (gVal[1] >= level ? 2u : 0u) | (gVal[2] >= level ? 4u : 0u) | (gVal[3] >= level ? 8u : 0u);
+ if (c == 0u || c == 15u) return;
+ if (c == 5u || c == 10u)
+ {
+ bool centreHigh = 0.25 * (gVal[0] + gVal[1] + gVal[2] + gVal[3]) >= level;
+ // Case 5 (corners 0, 2 high): joined high corners cut off corners 1 and 3; apart, they cut off corners 0 and 2.
+ // Case 10 (corners 1, 3 high): joined high corners cut off corners 0 and 2; apart, corners 1 and 3.
+ bool aroundOdd = (c == 5u) == centreHigh;
+ if (aroundOdd) { Emit(0, 1, level, key); Emit(2, 3, level, key); }
+ else { Emit(3, 0, level, key); Emit(1, 2, level, key); }
+ return;
+ }
+ uint pair = kSegment[c];
+ Emit(pair & 0xFu, pair >> 4, level, key);
+}
+
+[numthreads(8, 8, 1)]
+void March(uint3 id : SV_DispatchThreadID)
+{
+ if (any(int2(id.xy) + 1 >= _Size)) return;
+ int2 p0 = int2(id.xy);
+ float rhoMin = Texel() * 1.0001;
+ float rhoMaxTest = _RhoMax * 0.9999;
+ float dMin = 1e30, dMax = -1e30, slopeMin = 1e30;
+ float theta[4], rho[4];
+ bool clamped = false;
+ [unroll] for (uint i = 0; i < 4; i++)
+ {
+ int2 q = p0 + int2((i == 1 || i == 2) ? 1 : 0, i >= 2 ? 1 : 0);
+ gIdx[i] = (uint)(q.y * _Size.x + q.x);
+ gPos[i] = _Origin + float2(q) * _PerTexel;
+ float4 g = _GradIn.Load(int3(q, 0));
+ float2 c = _CurveIn.Load(int3(q, 0));
+ gSlope[i] = g.z;
+ gDepth[i] = g.w;
+ theta[i] = c.x;
+ rho[i] = c.y;
+ clamped = clamped || c.y <= rhoMin || c.y >= rhoMaxTest;
+ dMin = min(dMin, g.w);
+ dMax = max(dMax, g.w);
+ slopeMin = min(slopeMin, g.z);
+ }
+ gCell = (uint)(p0.y * _Size.x + p0.x);
+
+ // Angles unwrapped to corner 0: a cell never crosses the branch cut inside an interpolation.
+ float tu[4];
+ bool seam = false;
+ float tMin = 1e30, tMax = -1e30;
+ [unroll] for (uint j = 0; j < 4; j++)
+ {
+ tu[j] = theta[0] + WrapPi(theta[j] - theta[0]);
+ seam = seam || abs(tu[j] - theta[j]) > 1.0;
+ tMin = min(tMin, tu[j]);
+ tMax = max(tMax, tu[j]);
+ }
+
+ // Contours: s is the arclength around the well, theta_unwrapped * rho.
+ if (_Range > 0 && _ContourCount > 1)
+ {
+ float spacing = _Range / (_ContourCount - 1);
+ int lo = (int)clamp(floor((dMin - _Start) / spacing), 1, _ContourCount);
+ int hi = (int)clamp(ceil((dMax - _Start) / spacing), 0, _ContourCount);
+ [unroll] for (uint k = 0; k < 4; k++) { gVal[k] = gDepth[k]; gS[k] = tu[k] * rho[k]; }
+ for (int n = lo; n <= hi; n++)
+ March1(_Start + n * spacing, IsoKey((uint)(n - 1), ISO_FAMILY_CONTOUR, seam, clamped));
+ }
+
+ // Rays: s is the distance along the ray, rho. Singular cells (a well centre, a saddle) have no angle: skipped.
+ if (_RayCount > 0 && slopeMin >= _SlopeEpsilon && tMax - tMin <= PI * 0.5
+ && dMax >= _Start && dMin <= _Start + _Range)
+ {
+ float step = TWO_PI / _RayCount;
+ [unroll] for (uint m = 0; m < 4; m++) { gVal[m] = tu[m]; gS[m] = rho[m]; }
+ int lo = (int)floor((tMin + PI) / step - 0.5);
+ int hi = (int)ceil((tMax + PI) / step - 0.5);
+ for (int r = lo; r <= hi; r++)
+ {
+ int level = ((r % _RayCount) + _RayCount) % _RayCount;
+ March1((r + 0.5) * step - PI, IsoKey((uint)level, ISO_FAMILY_RAY, false, clamped));
+ }
+ }
+}
+
+[numthreads(1, 1, 1)]
+void Finish(uint3 id : SV_DispatchThreadID)
+{
+ _Count[0] = min(_Count[1], _Capacity);
+}
diff --git a/Assets/Shaders/IsolineExtract.compute.meta b/Assets/Shaders/IsolineExtract.compute.meta
new file mode 100644
index 000000000..bbb17392a
--- /dev/null
+++ b/Assets/Shaders/IsolineExtract.compute.meta
@@ -0,0 +1,7 @@
+fileFormatVersion: 2
+guid: 31ef7108234b45d4f8639d22197cea27
+ComputeShaderImporter:
+ externalObjects: {}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Assets/Shaders/IsolineSegment.hlsl b/Assets/Shaders/IsolineSegment.hlsl
new file mode 100644
index 000000000..45da08073
--- /dev/null
+++ b/Assets/Shaders/IsolineSegment.hlsl
@@ -0,0 +1,34 @@
+// The isoline segment buffer's published layout (campaign aetheria-release, cut isoline-extraction): one segment of one
+// isoline of a gravity field texture, endpoints in world xz. Written by IsolineExtract.compute, read by whichever renderer
+// draws the lines. IsolineExtractor.IsoSegment mirrors this struct; the Editor smoke asserts both are 48 bytes.
+//
+// Count buffer: uint[2]. [0] = segments written (<= capacity); [1] = segments requested (overflow when > capacity).
+// There is no indirect-args layout here: a renderer derives its own args from Count[0].
+#ifndef ISOLINE_SEGMENT_INCLUDED
+#define ISOLINE_SEGMENT_INCLUDED
+
+struct IsoSegment
+{
+ float2 a, b; // endpoints, world xz
+ float sA, sB; // along-line parameter at each endpoint, world units
+ float slopeA, slopeB; // |grad d| at each endpoint, world units of depth per world unit
+ float depthA, depthB; // d at each endpoint (the field value; deeper is larger)
+ uint key; // bits 0-7 level index, 8 family, 9 seam, 10 rho clamped
+ uint cell; // linear index of the cell (y * field width + x): tile-binning locality and a deterministic sort key
+};
+
+#define ISO_FAMILY_CONTOUR 0u
+#define ISO_FAMILY_RAY 1u
+
+uint IsoLevel(uint key) { return key & 0xFFu; }
+uint IsoFamily(uint key) { return (key >> 8) & 1u; }
+// The segment sits where the unwrapped angle is cut: s jumps by a full turn between this segment and its neighbour.
+bool IsoSeam(uint key) { return (key & (1u << 9)) != 0u; }
+// An endpoint's curvature radius hit a clamp (one texel or RhoMax): s is not trustworthy there.
+bool IsoRhoClamped(uint key) { return (key & (1u << 10)) != 0u; }
+uint IsoKey(uint level, uint family, bool seam, bool clamped)
+{
+ return (level & 0xFFu) | (family << 8) | (seam ? (1u << 9) : 0u) | (clamped ? (1u << 10) : 0u);
+}
+
+#endif
diff --git a/Assets/Shaders/IsolineSegment.hlsl.meta b/Assets/Shaders/IsolineSegment.hlsl.meta
new file mode 100644
index 000000000..bbe3b5f48
--- /dev/null
+++ b/Assets/Shaders/IsolineSegment.hlsl.meta
@@ -0,0 +1,7 @@
+fileFormatVersion: 2
+guid: d6a28a833690bbb499b920e42d660688
+ShaderIncludeImporter:
+ externalObjects: {}
+ userData:
+ assetBundleName:
+ assetBundleVariant: