Add compact fractal SDF splat packets
This commit is contained in:
parent
b9ceb9c20e
commit
6d1927c380
14 changed files with 313 additions and 3 deletions
|
|
@ -108,6 +108,58 @@ public readonly record struct AquariumFractalSurfacePage(
|
|||
int PayloadHandle,
|
||||
float MaxError);
|
||||
|
||||
public readonly record struct AquariumFractalSdfSplat3DKey
|
||||
{
|
||||
public AquariumFractalSdfSplat3DKey(
|
||||
AquariumFractalKey domainKey,
|
||||
AquariumFractalKey nodeKey,
|
||||
int payloadHandle)
|
||||
{
|
||||
if (domainKey.Value is null)
|
||||
{
|
||||
throw new ArgumentException("3D SDF splat domain key must not be empty.", nameof(domainKey));
|
||||
}
|
||||
|
||||
if (nodeKey.Value is null)
|
||||
{
|
||||
throw new ArgumentException("3D SDF splat node key must not be empty.", nameof(nodeKey));
|
||||
}
|
||||
|
||||
if (payloadHandle < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(payloadHandle), payloadHandle, "3D SDF splat payload handle must not be negative.");
|
||||
}
|
||||
|
||||
DomainKey = domainKey;
|
||||
NodeKey = nodeKey;
|
||||
PayloadHandle = payloadHandle;
|
||||
}
|
||||
|
||||
public AquariumFractalKey DomainKey { get; }
|
||||
|
||||
public AquariumFractalKey NodeKey { get; }
|
||||
|
||||
public int PayloadHandle { get; }
|
||||
|
||||
public string Value => $"{DomainKey.Value}:{NodeKey.Value}:sdf3d:{PayloadHandle:D6}";
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Value;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly record struct AquariumFractalSdfSplat3D(
|
||||
AquariumFractalSdfSplat3DKey Key,
|
||||
Vector3 Center,
|
||||
Quaternion Orientation,
|
||||
Vector3 Radii,
|
||||
float Falloff,
|
||||
float ShapePower,
|
||||
float DistanceOffset,
|
||||
float MaterialValue,
|
||||
float Confidence);
|
||||
|
||||
public readonly record struct AquariumBrushClaim(
|
||||
AquariumFractalKey Key,
|
||||
AquariumFractalKey DomainKey,
|
||||
|
|
|
|||
44
src/Aquarium.Engine.Fractal/Lod/FractalSdfSplat3DCompiler.cs
Normal file
44
src/Aquarium.Engine.Fractal/Lod/FractalSdfSplat3DCompiler.cs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
using System.Numerics;
|
||||
|
||||
namespace Aquarium.Engine.Fractal.Lod;
|
||||
|
||||
public static class FractalSdfSplat3DCompiler
|
||||
{
|
||||
public static AquariumFractalSdfSplat3D FromProbe(
|
||||
FractalProbeSample probe,
|
||||
float thickness,
|
||||
float falloff = 4.0f,
|
||||
float shapePower = 1.0f)
|
||||
{
|
||||
if (!float.IsFinite(probe.BoundRadius) || probe.BoundRadius <= 0.0f)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(probe), probe.BoundRadius, "Probe bound radius must be positive and finite.");
|
||||
}
|
||||
|
||||
if (!float.IsFinite(thickness) || thickness <= 0.0f)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(thickness), thickness, "3D SDF splat thickness must be positive and finite.");
|
||||
}
|
||||
|
||||
if (!float.IsFinite(falloff) || falloff <= 0.0f)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(falloff), falloff, "3D SDF splat falloff must be positive and finite.");
|
||||
}
|
||||
|
||||
if (!float.IsFinite(shapePower) || shapePower <= 0.0f)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(shapePower), shapePower, "3D SDF splat shape power must be positive and finite.");
|
||||
}
|
||||
|
||||
return new AquariumFractalSdfSplat3D(
|
||||
new AquariumFractalSdfSplat3DKey(probe.DomainKey, probe.NodeKey, probe.PayloadHandle),
|
||||
probe.LocalCenter,
|
||||
Quaternion.Identity,
|
||||
new Vector3(probe.BoundRadius, probe.BoundRadius, thickness),
|
||||
falloff,
|
||||
shapePower,
|
||||
DistanceOffset: 0.0f,
|
||||
MaterialValue: Math.Clamp(probe.MaterialDelta, 0.0f, 1.0f),
|
||||
Confidence: Math.Clamp(probe.TargetContribution, 0.0f, 1.0f));
|
||||
}
|
||||
}
|
||||
33
src/Aquarium.Engine.Fractal/Lod/FractalSdfSplat3DKernel.cs
Normal file
33
src/Aquarium.Engine.Fractal/Lod/FractalSdfSplat3DKernel.cs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
using System.Numerics;
|
||||
|
||||
namespace Aquarium.Engine.Fractal.Lod;
|
||||
|
||||
public static class FractalSdfSplat3DKernel
|
||||
{
|
||||
public static float CompactWeight(AquariumFractalSdfSplat3D splat, Vector3 point)
|
||||
{
|
||||
var local = Vector3.Transform(point - splat.Center, Quaternion.Inverse(splat.Orientation));
|
||||
var radii = Vector3.Max(splat.Radii, new Vector3(0.0001f));
|
||||
var q = local / radii;
|
||||
var radiusSquared = Vector3.Dot(q, q);
|
||||
if (radiusSquared >= 1.0f)
|
||||
{
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
var falloff = MathF.Max(splat.Falloff, 0.0001f);
|
||||
var shapePower = MathF.Max(splat.ShapePower, 0.0001f);
|
||||
var edge = MathF.Exp(-falloff);
|
||||
var value = MathF.Exp(-falloff * radiusSquared);
|
||||
var compact = Math.Clamp((value - edge) / MathF.Max(1.0f - edge, 0.000001f), 0.0f, 1.0f);
|
||||
return MathF.Pow(compact, shapePower);
|
||||
}
|
||||
|
||||
public static float SignedDistance(AquariumFractalSdfSplat3D splat, Vector3 point)
|
||||
{
|
||||
var local = Vector3.Transform(point - splat.Center, Quaternion.Inverse(splat.Orientation));
|
||||
var radii = Vector3.Max(splat.Radii, new Vector3(0.0001f));
|
||||
var q = local / radii;
|
||||
return ((q.Length() - 1.0f) * MathF.Min(radii.X, MathF.Min(radii.Y, radii.Z))) + splat.DistanceOffset;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
using System.Numerics;
|
||||
using Aquarium.Engine.Fractal.Grammar;
|
||||
using Aquarium.Engine.Fractal.Temporal;
|
||||
|
||||
|
|
@ -7,11 +8,34 @@ public readonly record struct FractalProbeReservoirSnapshot(
|
|||
bool HasSample,
|
||||
AquariumFractalKey DomainKey,
|
||||
AquariumFractalKey NodeKey,
|
||||
Vector3 LocalCenter,
|
||||
float TargetContribution,
|
||||
float BoundRadius,
|
||||
float SourcePdf,
|
||||
float MaterialDelta,
|
||||
float WeightSum,
|
||||
int CandidateCount,
|
||||
float ContributionWeight);
|
||||
float ContributionWeight,
|
||||
int PayloadHandle)
|
||||
{
|
||||
public FractalProbeSample ToProbeSample()
|
||||
{
|
||||
if (!HasSample)
|
||||
{
|
||||
throw new InvalidOperationException("Cannot convert an empty structural probe reservoir snapshot to a probe sample.");
|
||||
}
|
||||
|
||||
return new FractalProbeSample(
|
||||
DomainKey,
|
||||
NodeKey,
|
||||
LocalCenter,
|
||||
BoundRadius,
|
||||
TargetContribution,
|
||||
SourcePdf,
|
||||
MaterialDelta,
|
||||
PayloadHandle);
|
||||
}
|
||||
}
|
||||
|
||||
public static class FractalStructuralProbeReservoir
|
||||
{
|
||||
|
|
@ -69,11 +93,15 @@ public static class FractalStructuralProbeReservoir
|
|||
HasSample: true,
|
||||
selected.DomainKey,
|
||||
selected.NodeKey,
|
||||
selected.LocalCenter,
|
||||
selected.TargetContribution,
|
||||
selected.BoundRadius,
|
||||
selected.SourcePdf,
|
||||
selected.MaterialDelta,
|
||||
reservoir.WeightSum,
|
||||
reservoir.CandidateCount,
|
||||
reservoir.ContributionWeight);
|
||||
reservoir.ContributionWeight,
|
||||
selected.PayloadHandle);
|
||||
}
|
||||
|
||||
private static FractalProbeReservoirSnapshot Empty()
|
||||
|
|
@ -82,10 +110,14 @@ public static class FractalStructuralProbeReservoir
|
|||
HasSample: false,
|
||||
DomainKey: default,
|
||||
NodeKey: default,
|
||||
LocalCenter: Vector3.Zero,
|
||||
TargetContribution: 0.0f,
|
||||
BoundRadius: 0.0f,
|
||||
SourcePdf: 0.0f,
|
||||
MaterialDelta: 0.0f,
|
||||
WeightSum: 0.0f,
|
||||
CandidateCount: 0,
|
||||
ContributionWeight: 0.0f);
|
||||
ContributionWeight: 0.0f,
|
||||
PayloadHandle: 0);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -71,6 +71,7 @@ public static class ZyphosFractalTerrain
|
|||
selectedCut,
|
||||
bucketPixelsPerWorld,
|
||||
new FractalXorShiftRandom(((uint)bucket ^ 0xC2B2AE35u) + (uint)selectedCut.Length));
|
||||
var structuralSdfSplats = BuildStructuralSdfSplats(structuralProbeReservoir);
|
||||
var surfacePages = PlanSurfacePages(selectedCut);
|
||||
var surfacePageResidency = FractalSurfacePageResidencyPlanner.Plan(
|
||||
surfacePages,
|
||||
|
|
@ -94,6 +95,7 @@ public static class ZyphosFractalTerrain
|
|||
selectedCut,
|
||||
resourcePlan,
|
||||
structuralProbeReservoir,
|
||||
structuralSdfSplats,
|
||||
surfacePages,
|
||||
surfacePageResidency,
|
||||
surfacePagePayloads,
|
||||
|
|
@ -114,6 +116,17 @@ public static class ZyphosFractalTerrain
|
|||
.ToArray();
|
||||
}
|
||||
|
||||
private static AquariumFractalSdfSplat3D[] BuildStructuralSdfSplats(FractalProbeReservoirSnapshot reservoir)
|
||||
{
|
||||
if (!reservoir.HasSample)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var thickness = MathF.Max(reservoir.BoundRadius * 0.25f, 0.001f);
|
||||
return [FractalSdfSplat3DCompiler.FromProbe(reservoir.ToProbeSample(), thickness)];
|
||||
}
|
||||
|
||||
private static FractalSurfacePagePayload[] BuildSurfacePagePayloads(IReadOnlyList<AquariumFractalSurfacePage> residentPages)
|
||||
{
|
||||
var payloads = new FractalSurfacePagePayload[residentPages.Count];
|
||||
|
|
@ -170,6 +183,7 @@ public static class ZyphosFractalTerrain
|
|||
$" evictions: {plan.ResourcePlan.Residency.EvictedNodes.Count}",
|
||||
$" structuralProbe: {(plan.StructuralProbeReservoir.HasSample ? plan.StructuralProbeReservoir.NodeKey.Value : "none")}",
|
||||
$" structuralProbeWeight: {plan.StructuralProbeReservoir.WeightSum:0.000} candidates={plan.StructuralProbeReservoir.CandidateCount} contributionWeight={plan.StructuralProbeReservoir.ContributionWeight:0.000}",
|
||||
$" structuralSdfSplats: {plan.StructuralSdfSplats.Length}",
|
||||
$" surfacePages: {plan.SurfacePages.Length} {SurfacePageDimension}x{SurfacePageDimension}",
|
||||
$" surfacePageResident: {plan.SurfacePageResidency.ResidentPages.Count}/{plan.SurfacePages.Length} bytes={plan.SurfacePageResidency.ResidentBytes}/{MaxResidentSurfacePageBytes}",
|
||||
$" surfacePagePayloads: {plan.SurfacePagePayloads.Length}",
|
||||
|
|
@ -233,6 +247,7 @@ public readonly record struct ZyphosFractalRenderPlan(
|
|||
AquariumSelectedCut[] SelectedCuts,
|
||||
FractalResourcePlan ResourcePlan,
|
||||
FractalProbeReservoirSnapshot StructuralProbeReservoir,
|
||||
AquariumFractalSdfSplat3D[] StructuralSdfSplats,
|
||||
AquariumFractalSurfacePage[] SurfacePages,
|
||||
FractalSurfacePageResidencyPlan SurfacePageResidency,
|
||||
FractalSurfacePagePayload[] SurfacePagePayloads,
|
||||
|
|
|
|||
Loading…
Reference in a new issue