GravityField.PullSlope and the generated gravitySample: closed-form gradient and Hessian of the lowered wells

PullSlope is dPull/dr over the same band polynomial; GravityShader emits gravityTermPull, gravityTermPullSlope and gravitySample beside gravityHeight; Gravity.cginc regenerated.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-11-30 00:17:33 +01:00
commit c551a27d64
4 changed files with 210 additions and 0 deletions

View file

@ -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);
}
/// <summary>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.</summary>
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));
}
/// <summary>The potential at distance r, per unit strength: 0 at and beyond outer, the global log when outer is +inf.</summary>
public static float Potential(float softening, float inner, float outer, float r)
{

View file

@ -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
";
}

View file

@ -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

View file

@ -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<float>();
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}");
}
}