diff --git a/Assets/Scripts/ServerShared/GravityField.cs b/Assets/Scripts/ServerShared/GravityField.cs index 084aa1f45..739862306 100644 --- a/Assets/Scripts/ServerShared/GravityField.cs +++ b/Assets/Scripts/ServerShared/GravityField.cs @@ -90,6 +90,28 @@ public static class GravityField return r * Profile(inner, outer, s) / (s + softening * softening); } + // dB/ds, B's slope in s = r^2: the derivative of the band polynomial over (b2 - a2) inside the band, 0 outside and with no fade. + private static float ProfileSlope(float inner, float outer, float s) + { + if (float.IsPositiveInfinity(outer)) return 0f; + var a2 = inner * inner; + var b2 = outer * outer; + if (s <= a2 || s >= b2) return 0f; + var t = (s - a2) / (b2 - a2); + var acc = 0f; + for (var i = OneMinusS.Length - 1; i >= 1; i--) acc = acc * t + i * OneMinusS[i]; + return acc / (b2 - a2); + } + + /// The slope of the pull, dPull/dr at distance r, per unit strength: B/(s+e2) + 2s (B'(s)/(s+e2) - B/(s+e2)^2), s = r^2. + public static float PullSlope(float softening, float inner, float outer, float r) + { + var s = r * r; + var den = s + softening * softening; + var b = Profile(inner, outer, s); + return b / den + 2f * s * (ProfileSlope(inner, outer, s) / den - b / (den * den)); + } + /// The potential at distance r, per unit strength: 0 at and beyond outer, the global log when outer is +inf. public static float Potential(float softening, float inner, float outer, float r) { diff --git a/Assets/Scripts/ServerShared/GravityShader.cs b/Assets/Scripts/ServerShared/GravityShader.cs index e4736ec95..43700d03f 100644 --- a/Assets/Scripts/ServerShared/GravityShader.cs +++ b/Assets/Scripts/ServerShared/GravityShader.cs @@ -123,6 +123,89 @@ float gravityHeight(float2 p) return result / _HeightExaggeration; } +// GravityField.Pull: the pull toward the term's centre at distance r, per unit strength, r B(s) / (s + e2) with s = r^2. +float gravityTermPull(float softening, float a, float b, float r) +{ + float s = r * r; + float profile = 1; + if (!isinf(b)) + { + float a2 = a * a; + float b2 = b * b; + if (s > a2) + { + profile = 0; + if (s < b2) + { + float t = (s - a2) / (b2 - a2); + [unroll] for (int i = @N@ - 1; i >= 0; i--) profile = profile * t + GravityOneMinusS[i]; + } + } + } + return r * profile / (s + softening * softening); +} + +// GravityField.PullSlope: dPull/dr = B/(s+e2) + 2s (B'(s)/(s+e2) - B/(s+e2)^2), B' the derivative of the band polynomial +// over (b2 - a2) inside the band (Horner over GravityOneMinusS's derivative) and 0 outside. +float gravityTermPullSlope(float softening, float a, float b, float r) +{ + float s = r * r; + float den = s + softening * softening; + float profile = 1; + float slope = 0; + if (!isinf(b)) + { + float a2 = a * a; + float b2 = b * b; + if (s > a2) + { + profile = 0; + if (s < b2) + { + float t = (s - a2) / (b2 - a2); + [unroll] for (int i = @N@ - 1; i >= 0; i--) profile = profile * t + GravityOneMinusS[i]; + [unroll] for (int j = @N@ - 1; j >= 1; j--) slope = slope * t + j * GravityOneMinusS[j]; + slope /= b2 - a2; + } + } + } + return profile / den + 2 * s * (slope / den - profile / (den * den)); +} + +// The height's negative d = -Zone.GetHeight, its gradient and Hessian (xx, xy, yy) at world xz, in closed form from the same +// terms: per term, with k = -strength / exaggeration and u the unit vector from the term, grad += k Pull u and +// H += k (PullSlope u u^T + (Pull / r) (I - u u^T)); a point on the centre has the isotropic limit PullSlope I. Terms at or beyond +// outer add nothing, as in gravityHeight. +void gravitySample(float2 p, out float d, out float2 grad, out float3 hessian) +{ + float height = 0; + grad = float2(0, 0); + hessian = float3(0, 0, 0); + for (int i = 0; i < _GravityWellCount; i++) + { + GravityTerm term = _GravityWells[i]; + float2 offset = p - term.position; + float r = length(offset); + if (r < term.outer) + { + height += term.strength * gravityTermPotential(term.softening, term.inner, term.outer, r) - term.reference; + float k = -term.strength / _HeightExaggeration; + float slope = gravityTermPullSlope(term.softening, term.inner, term.outer, r); + if (r < 1e-6 * term.softening || r < 1e-20) + hessian += k * slope * float3(1, 0, 1); + else + { + float pull = gravityTermPull(term.softening, term.inner, term.outer, r); + float2 u = offset / r; + float turn = pull / r; + grad += k * pull * u; + hessian += k * float3(slope * u.x * u.x + turn * (1 - u.x * u.x), (slope - turn) * u.x * u.y, slope * u.y * u.y + turn * (1 - u.y * u.y)); + } + } + } + d = -height / _HeightExaggeration; +} + #endif "; } diff --git a/Assets/Shaders/Gravity.cginc b/Assets/Shaders/Gravity.cginc index 5eacdec0f..ad3e6fce3 100644 --- a/Assets/Shaders/Gravity.cginc +++ b/Assets/Shaders/Gravity.cginc @@ -93,4 +93,87 @@ float gravityHeight(float2 p) return result / _HeightExaggeration; } +// GravityField.Pull: the pull toward the term's centre at distance r, per unit strength, r B(s) / (s + e2) with s = r^2. +float gravityTermPull(float softening, float a, float b, float r) +{ + float s = r * r; + float profile = 1; + if (!isinf(b)) + { + float a2 = a * a; + float b2 = b * b; + if (s > a2) + { + profile = 0; + if (s < b2) + { + float t = (s - a2) / (b2 - a2); + [unroll] for (int i = 8 - 1; i >= 0; i--) profile = profile * t + GravityOneMinusS[i]; + } + } + } + return r * profile / (s + softening * softening); +} + +// GravityField.PullSlope: dPull/dr = B/(s+e2) + 2s (B'(s)/(s+e2) - B/(s+e2)^2), B' the derivative of the band polynomial +// over (b2 - a2) inside the band (Horner over GravityOneMinusS's derivative) and 0 outside. +float gravityTermPullSlope(float softening, float a, float b, float r) +{ + float s = r * r; + float den = s + softening * softening; + float profile = 1; + float slope = 0; + if (!isinf(b)) + { + float a2 = a * a; + float b2 = b * b; + if (s > a2) + { + profile = 0; + if (s < b2) + { + float t = (s - a2) / (b2 - a2); + [unroll] for (int i = 8 - 1; i >= 0; i--) profile = profile * t + GravityOneMinusS[i]; + [unroll] for (int j = 8 - 1; j >= 1; j--) slope = slope * t + j * GravityOneMinusS[j]; + slope /= b2 - a2; + } + } + } + return profile / den + 2 * s * (slope / den - profile / (den * den)); +} + +// The height's negative d = -Zone.GetHeight, its gradient and Hessian (xx, xy, yy) at world xz, in closed form from the same +// terms: per term, with k = -strength / exaggeration and u the unit vector from the term, grad += k Pull u and +// H += k (PullSlope u u^T + (Pull / r) (I - u u^T)); a point on the centre has the isotropic limit PullSlope I. Terms at or beyond +// outer add nothing, as in gravityHeight. +void gravitySample(float2 p, out float d, out float2 grad, out float3 hessian) +{ + float height = 0; + grad = float2(0, 0); + hessian = float3(0, 0, 0); + for (int i = 0; i < _GravityWellCount; i++) + { + GravityTerm term = _GravityWells[i]; + float2 offset = p - term.position; + float r = length(offset); + if (r < term.outer) + { + height += term.strength * gravityTermPotential(term.softening, term.inner, term.outer, r) - term.reference; + float k = -term.strength / _HeightExaggeration; + float slope = gravityTermPullSlope(term.softening, term.inner, term.outer, r); + if (r < 1e-6 * term.softening || r < 1e-20) + hessian += k * slope * float3(1, 0, 1); + else + { + float pull = gravityTermPull(term.softening, term.inner, term.outer, r); + float2 u = offset / r; + float turn = pull / r; + grad += k * pull * u; + hessian += k * float3(slope * u.x * u.x + turn * (1 - u.x * u.x), (slope - turn) * u.x * u.y, slope * u.y * u.y + turn * (1 - u.y * u.y)); + } + } + } + d = -height / _HeightExaggeration; +} + #endif diff --git a/tests/Aetheria.Shared.Tests/GravityShaderTests.cs b/tests/Aetheria.Shared.Tests/GravityShaderTests.cs index 963e6b1a7..8116e8f26 100644 --- a/tests/Aetheria.Shared.Tests/GravityShaderTests.cs +++ b/tests/Aetheria.Shared.Tests/GravityShaderTests.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Linq; using Xunit; // potential-owns-gravity (aetheria-gravity, cut grid-lowering): Assets/Shaders/Gravity.cginc is the output of @@ -20,4 +21,25 @@ public sealed class GravityShaderTests Assert.Contains("static const float GravityS[8] = { 0, 0, 0, 0, 35, -84, 70, -20 };", generated); Assert.Equal(generated, File.ReadAllText(path).Replace("\r\n", "\n")); } + + // PullSlope is dPull/dr: against a central difference of Pull (float, step 0.05 u) over radii that cross the softened core, + // the band's inner edge, its interior and its outer edge, for a banded term and the global one. Errors are normalised by the + // largest |dPull/dr| of the sweep. Margin rule (lodur:ruling:deterministic-bound-margin): shipped worst BOUND_SHIPPED, + // the least claimed mutant (PullSlope = 0) reads 1 by construction; bound 1.0. + [Theory] + [InlineData(5f, 20f, 100f)] + [InlineData(2f, 0f, 60f)] + [InlineData(8f, 30f, float.PositiveInfinity)] + public void PullSlopeIsThePullsDerivative(float softening, float inner, float outer) + { + var radii = new System.Collections.Generic.List(); + for (var r = 0.5f; r < (float.IsPositiveInfinity(outer) ? 150f : outer + 10f); r += 0.37f) radii.Add(r); + const float h = 0.05f; + var truth = radii.Select(r => (GravityField.Pull(softening, inner, outer, r + h) - GravityField.Pull(softening, inner, outer, r - h)) / (2f * h)).ToArray(); + var scale = truth.Max(Math.Abs); + var worst = 0.0; + for (var i = 0; i < radii.Count; i++) + worst = Math.Max(worst, Math.Abs(GravityField.PullSlope(softening, inner, outer, radii[i]) - truth[i]) / scale); + Assert.True(worst < 0.0193, $"worst normalised error {worst:G4}"); + } }