Build portable planetary field runtime
This commit is contained in:
parent
15f27173dd
commit
42fef7cdd7
24 changed files with 2214 additions and 0 deletions
|
|
@ -19,6 +19,8 @@
|
|||
<ItemGroup>
|
||||
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
|
||||
<None Include="..\..\shaders\CultMath.hlsl" Pack="true" PackagePath="contentFiles\any\any\shaders\CultMath.hlsl" CopyToOutputDirectory="PreserveNewest" />
|
||||
<None Include="..\..\shaders\AdvancedErosionFilter.hlsl" Pack="true" PackagePath="contentFiles\any\any\shaders\AdvancedErosionFilter.hlsl" CopyToOutputDirectory="PreserveNewest" />
|
||||
<None Include="..\..\shaders\Planetary.hlsl" Pack="true" PackagePath="contentFiles\any\any\shaders\Planetary.hlsl" CopyToOutputDirectory="PreserveNewest" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
213
src/CultMath/PlanetaryField.cs
Normal file
213
src/CultMath/PlanetaryField.cs
Normal file
|
|
@ -0,0 +1,213 @@
|
|||
namespace CultMath;
|
||||
|
||||
public readonly record struct PlanetaryQueryScale(float FootprintMeters, float MaximumUnresolvedHeight)
|
||||
{
|
||||
public static PlanetaryQueryScale AtFootprint(float footprintMeters, float maximumUnresolvedHeight = float.PositiveInfinity)
|
||||
=> new PlanetaryQueryScale(footprintMeters, maximumUnresolvedHeight).Validate();
|
||||
|
||||
public PlanetaryQueryScale Validate()
|
||||
{
|
||||
if (!float.IsFinite(FootprintMeters) || FootprintMeters < 0) throw new ArgumentOutOfRangeException(nameof(FootprintMeters));
|
||||
if (float.IsNaN(MaximumUnresolvedHeight) || MaximumUnresolvedHeight < 0) throw new ArgumentOutOfRangeException(nameof(MaximumUnresolvedHeight));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly record struct PlanetaryFieldDefinition(
|
||||
ulong FieldVersion,
|
||||
float Radius,
|
||||
int Seed,
|
||||
AdvancedErosionParameters Erosion)
|
||||
{
|
||||
public static PlanetaryFieldDefinition Create(
|
||||
ulong baseFieldVersion,
|
||||
float radius,
|
||||
int seed,
|
||||
AdvancedErosionParameters erosion)
|
||||
=> new PlanetaryFieldDefinition(PlanetaryFieldIdentity.Compute(baseFieldVersion, radius, seed, erosion), radius, seed, erosion).Validate();
|
||||
|
||||
public PlanetaryFieldDefinition Validate()
|
||||
{
|
||||
if (FieldVersion == 0) throw new ArgumentOutOfRangeException(nameof(FieldVersion));
|
||||
if (!float.IsFinite(Radius) || Radius <= 0) throw new ArgumentOutOfRangeException(nameof(Radius));
|
||||
if (!float.IsFinite(Erosion.Scale) || Erosion.Scale <= 0) throw new ArgumentOutOfRangeException(nameof(Erosion));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public static class PlanetaryFieldIdentity
|
||||
{
|
||||
public static ulong Compute(ulong baseFieldVersion, float radius, int seed, in AdvancedErosionParameters erosion)
|
||||
{
|
||||
if (baseFieldVersion == 0) throw new ArgumentOutOfRangeException(nameof(baseFieldVersion));
|
||||
const ulong offset = 14695981039346656037UL;
|
||||
const ulong prime = 1099511628211UL;
|
||||
var hash = offset;
|
||||
Add(baseFieldVersion);
|
||||
Add((uint)BitConverter.SingleToInt32Bits(radius)); Add((uint)seed);
|
||||
Add((uint)BitConverter.SingleToInt32Bits(erosion.Scale)); Add((uint)BitConverter.SingleToInt32Bits(erosion.Strength));
|
||||
Add((uint)BitConverter.SingleToInt32Bits(erosion.GullyWeight)); Add((uint)BitConverter.SingleToInt32Bits(erosion.Detail));
|
||||
AddFloat4(erosion.Rounding); AddFloat4(erosion.Onset);
|
||||
Add((uint)BitConverter.SingleToInt32Bits(erosion.AssumedSlope.x)); Add((uint)BitConverter.SingleToInt32Bits(erosion.AssumedSlope.y));
|
||||
Add((uint)BitConverter.SingleToInt32Bits(erosion.CellScale)); Add((uint)BitConverter.SingleToInt32Bits(erosion.Normalization));
|
||||
Add((uint)erosion.Octaves); Add((uint)BitConverter.SingleToInt32Bits(erosion.Lacunarity)); Add((uint)BitConverter.SingleToInt32Bits(erosion.Gain));
|
||||
return hash == 0 ? 1 : hash;
|
||||
|
||||
void Add(ulong value) => hash = unchecked((hash ^ value) * prime);
|
||||
void AddFloat4(float4 value)
|
||||
{
|
||||
Add((uint)BitConverter.SingleToInt32Bits(value.x)); Add((uint)BitConverter.SingleToInt32Bits(value.y));
|
||||
Add((uint)BitConverter.SingleToInt32Bits(value.z)); Add((uint)BitConverter.SingleToInt32Bits(value.w));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Application-supplied base evidence at one unit spherical direction.
|
||||
/// FieldValue and FieldGradient drive erosion. RadialDisplacement and
|
||||
/// RadialGradient describe the uneroded surface in metres.
|
||||
/// </summary>
|
||||
public readonly record struct PlanetaryBaseFieldSample(
|
||||
float RadialDisplacement,
|
||||
float3 RadialGradient,
|
||||
float FieldValue,
|
||||
float3 FieldGradient,
|
||||
float FadeTarget);
|
||||
|
||||
public readonly record struct PlanetarySurfaceSample(
|
||||
ulong FieldVersion,
|
||||
float3 UnitDirection,
|
||||
float Radius,
|
||||
float RadialDisplacement,
|
||||
float3 TangentGradient,
|
||||
float3 SurfaceNormal,
|
||||
float Slope,
|
||||
float Ridge,
|
||||
float Gully,
|
||||
float FinestResolvedWavelength,
|
||||
float UnresolvedHeightBound)
|
||||
{
|
||||
public float SurfaceRadius => Radius + RadialDisplacement;
|
||||
public float3 PlanetLocalPosition => UnitDirection * SurfaceRadius;
|
||||
public bool Meets(PlanetaryQueryScale scale) => UnresolvedHeightBound <= scale.MaximumUnresolvedHeight;
|
||||
}
|
||||
|
||||
public static class PlanetaryField
|
||||
{
|
||||
public static PlanetarySurfaceSample Sample(
|
||||
in PlanetaryFieldDefinition definition,
|
||||
float3 unitDirection,
|
||||
in PlanetaryBaseFieldSample baseSample,
|
||||
in PlanetaryQueryScale scale)
|
||||
{
|
||||
definition.Validate();
|
||||
scale.Validate();
|
||||
PlanetaryTopology.ValidateDirection(unitDirection);
|
||||
PlanetaryTopology.ValidateFinite(baseSample.RadialGradient, nameof(baseSample));
|
||||
PlanetaryTopology.ValidateFinite(baseSample.FieldGradient, nameof(baseSample));
|
||||
if (!float.IsFinite(baseSample.RadialDisplacement) || !float.IsFinite(baseSample.FieldValue) || !float.IsFinite(baseSample.FadeTarget))
|
||||
throw new ArgumentOutOfRangeException(nameof(baseSample));
|
||||
|
||||
var direction = math.normalize(unitDirection);
|
||||
var radialGradient = Tangent(direction, baseSample.RadialGradient);
|
||||
var fieldGradient = Tangent(direction, baseSample.FieldGradient);
|
||||
var p = definition.Erosion;
|
||||
var band = ErosionFrequencyBands.Select(
|
||||
p.Scale * p.CellScale,
|
||||
scale.FootprintMeters,
|
||||
p.Octaves,
|
||||
p.Lacunarity,
|
||||
p.Strength * p.Scale,
|
||||
p.Gain);
|
||||
|
||||
var powers = new float3(
|
||||
Pow4(MathF.Abs(direction.x)),
|
||||
Pow4(MathF.Abs(direction.y)),
|
||||
Pow4(MathF.Abs(direction.z)));
|
||||
var powerSum = math.max(powers.x + powers.y + powers.z, 1.0e-6f);
|
||||
var weights = powers / powerSum;
|
||||
var world = direction * definition.Radius;
|
||||
var seed = definition.Seed;
|
||||
var xy = AdvancedErosionFilter.Sample(
|
||||
world.xy + new float2(713 + seed * 0.754877666f, -291 + seed * 0.569840296f),
|
||||
new float3(baseSample.FieldValue, fieldGradient.x, fieldGradient.y), baseSample.FadeTarget, p, band);
|
||||
var yz = AdvancedErosionFilter.Sample(
|
||||
world.yz + new float2(-431 + seed * 0.438289027f, 887 + seed * 0.328438163f),
|
||||
new float3(baseSample.FieldValue, fieldGradient.y, fieldGradient.z), baseSample.FadeTarget, p, band);
|
||||
var zx = AdvancedErosionFilter.Sample(
|
||||
new float2(world.z, world.x) + new float2(197 + seed * 0.219783071f, 557 + seed * 0.145898034f),
|
||||
new float3(baseSample.FieldValue, fieldGradient.z, fieldGradient.x), baseSample.FadeTarget, p, band);
|
||||
|
||||
var erosionHeight = xy.Delta.x * weights.z + yz.Delta.x * weights.x + zx.Delta.x * weights.y;
|
||||
var erosionGradient = new float3(
|
||||
xy.Delta.y * weights.z + zx.Delta.z * weights.y,
|
||||
xy.Delta.z * weights.z + yz.Delta.y * weights.x,
|
||||
yz.Delta.z * weights.x + zx.Delta.y * weights.y);
|
||||
var powerGradientX = Tangent(direction, new float3(4 * direction.x * direction.x * direction.x / definition.Radius, 0, 0));
|
||||
var powerGradientY = Tangent(direction, new float3(0, 4 * direction.y * direction.y * direction.y / definition.Radius, 0));
|
||||
var powerGradientZ = Tangent(direction, new float3(0, 0, 4 * direction.z * direction.z * direction.z / definition.Radius));
|
||||
var sumGradient = powerGradientX + powerGradientY + powerGradientZ;
|
||||
var weightGradientX = (powerGradientX * powerSum - sumGradient * powers.x) / (powerSum * powerSum);
|
||||
var weightGradientY = (powerGradientY * powerSum - sumGradient * powers.y) / (powerSum * powerSum);
|
||||
var weightGradientZ = (powerGradientZ * powerSum - sumGradient * powers.z) / (powerSum * powerSum);
|
||||
erosionGradient += yz.Delta.x * weightGradientX + zx.Delta.x * weightGradientY + xy.Delta.x * weightGradientZ;
|
||||
var gradient = Tangent(direction, radialGradient + erosionGradient);
|
||||
var ridgeEvidence = xy.RidgeMap * weights.z + yz.RidgeMap * weights.x + zx.RidgeMap * weights.y;
|
||||
var ridge = math.saturate(ridgeEvidence * 0.5f + 0.5f);
|
||||
var gully = math.saturate(0.5f - ridgeEvidence * 0.5f);
|
||||
var displacement = baseSample.RadialDisplacement + erosionHeight;
|
||||
|
||||
return new(
|
||||
definition.FieldVersion,
|
||||
direction,
|
||||
definition.Radius,
|
||||
displacement,
|
||||
gradient,
|
||||
PlanetaryTopology.SurfaceNormal(direction, gradient),
|
||||
math.length(gradient),
|
||||
ridge,
|
||||
gully,
|
||||
band.FinestIncludedWavelength,
|
||||
band.UnresolvedHeightBound);
|
||||
}
|
||||
|
||||
public static void SampleBatch<TSource>(
|
||||
in PlanetaryFieldDefinition definition,
|
||||
ReadOnlySpan<float3> directions,
|
||||
in PlanetaryQueryScale scale,
|
||||
TSource source,
|
||||
Span<PlanetarySurfaceSample> destination)
|
||||
where TSource : IPlanetaryBaseField
|
||||
{
|
||||
if (destination.Length < directions.Length) throw new ArgumentException("Destination is shorter than directions.", nameof(destination));
|
||||
for (var i = 0; i < directions.Length; i++)
|
||||
{
|
||||
var direction = math.normalize(directions[i]);
|
||||
var baseSample = source.Sample(direction);
|
||||
destination[i] = Sample(definition, direction, baseSample, scale);
|
||||
}
|
||||
}
|
||||
|
||||
public static PlanetarySurfaceSample SamplePosition<TSource>(
|
||||
in PlanetaryFieldDefinition definition,
|
||||
double3 planetLocalPosition,
|
||||
in PlanetaryQueryScale scale,
|
||||
TSource source)
|
||||
where TSource : IPlanetaryBaseField
|
||||
{
|
||||
if (!double.IsFinite(planetLocalPosition.x) || !double.IsFinite(planetLocalPosition.y) || !double.IsFinite(planetLocalPosition.z))
|
||||
throw new ArgumentOutOfRangeException(nameof(planetLocalPosition));
|
||||
var length = Math.Sqrt(planetLocalPosition.x * planetLocalPosition.x + planetLocalPosition.y * planetLocalPosition.y + planetLocalPosition.z * planetLocalPosition.z);
|
||||
if (length < 1.0e-12) throw new ArgumentOutOfRangeException(nameof(planetLocalPosition));
|
||||
var direction = new float3((float)(planetLocalPosition.x / length), (float)(planetLocalPosition.y / length), (float)(planetLocalPosition.z / length));
|
||||
return Sample(definition, direction, source.Sample(direction), scale);
|
||||
}
|
||||
|
||||
private static float3 Tangent(float3 direction, float3 gradient) => gradient - direction * math.dot(gradient, direction);
|
||||
private static float Pow4(float value) { var square = value * value; return square * square; }
|
||||
}
|
||||
|
||||
public interface IPlanetaryBaseField
|
||||
{
|
||||
PlanetaryBaseFieldSample Sample(float3 unitDirection);
|
||||
}
|
||||
185
src/CultMath/PlanetaryPages.cs
Normal file
185
src/CultMath/PlanetaryPages.cs
Normal file
|
|
@ -0,0 +1,185 @@
|
|||
namespace CultMath;
|
||||
|
||||
public readonly record struct PlanetaryPageLayout(PlanetaryTileAddress Tile, int InteriorSize, int BorderSize)
|
||||
{
|
||||
public int StorageSize => checked(InteriorSize + BorderSize * 2);
|
||||
|
||||
public PlanetaryPageLayout Validate()
|
||||
{
|
||||
if (InteriorSize < 2) throw new ArgumentOutOfRangeException(nameof(InteriorSize));
|
||||
if (BorderSize < 0 || BorderSize >= InteriorSize) throw new ArgumentOutOfRangeException(nameof(BorderSize));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly record struct PlanetaryPageSummary(
|
||||
float MinimumHeight,
|
||||
float MaximumHeight,
|
||||
float MaximumSlope,
|
||||
float UnresolvedHeightBound,
|
||||
float AngularTexelSize);
|
||||
|
||||
public readonly record struct PlanetaryPageSample(float Height, float3 TangentGradient, float Ridge, float Gully);
|
||||
|
||||
public static class PlanetaryPageSampling
|
||||
{
|
||||
public static float3 Direction(PlanetaryPageLayout page, int storageX, int storageY)
|
||||
{
|
||||
page.Validate();
|
||||
if ((uint)storageX >= (uint)page.StorageSize || (uint)storageY >= (uint)page.StorageSize)
|
||||
throw new ArgumentOutOfRangeException(nameof(storageX));
|
||||
var localU = (storageX - page.BorderSize) / (double)(page.InteriorSize - 1);
|
||||
var localV = (storageY - page.BorderSize) / (double)(page.InteriorSize - 1);
|
||||
return DirectionAtLocal(page, localU, localV);
|
||||
}
|
||||
|
||||
public static float3 DirectionAtLocal(PlanetaryPageLayout page, double localU, double localV)
|
||||
{
|
||||
page.Validate();
|
||||
if (!double.IsFinite(localU) || !double.IsFinite(localV)) throw new ArgumentOutOfRangeException(nameof(localU));
|
||||
var count = page.Tile.AxisTileCount;
|
||||
return PlanetaryTopology.Direction(new(
|
||||
page.Tile.Face,
|
||||
-1.0 + 2.0 * (page.Tile.X + localU) / count,
|
||||
-1.0 + 2.0 * (page.Tile.Y + localV) / count));
|
||||
}
|
||||
|
||||
public static float AngularTexelSize(PlanetaryPageLayout page)
|
||||
{
|
||||
var center = page.BorderSize + (page.InteriorSize - 1) / 2;
|
||||
var a = Direction(page, center, center);
|
||||
var b = Direction(page, Math.Min(center + 1, page.StorageSize - 1), center);
|
||||
return MathF.Acos(math.clamp(math.dot(a, b), -1.0f, 1.0f));
|
||||
}
|
||||
|
||||
public static float NominalAngularTexelSize(PlanetaryPageLayout page)
|
||||
{
|
||||
page.Validate();
|
||||
return MathF.PI * 0.5f / (page.Tile.AxisTileCount * (page.InteriorSize - 1));
|
||||
}
|
||||
|
||||
public static PlanetaryPageSample Bilinear(
|
||||
ReadOnlySpan<PlanetaryPageSample> samples,
|
||||
PlanetaryPageLayout page,
|
||||
float2 local)
|
||||
{
|
||||
page.Validate();
|
||||
if (samples.Length != page.StorageSize * page.StorageSize) throw new ArgumentException("Page sample count does not match layout.", nameof(samples));
|
||||
var texel = local * (page.InteriorSize - 1) + page.BorderSize;
|
||||
var x0 = Math.Clamp((int)MathF.Floor(texel.x), 0, page.StorageSize - 1);
|
||||
var y0 = Math.Clamp((int)MathF.Floor(texel.y), 0, page.StorageSize - 1);
|
||||
var x1 = Math.Min(x0 + 1, page.StorageSize - 1);
|
||||
var y1 = Math.Min(y0 + 1, page.StorageSize - 1);
|
||||
var tx = math.frac(texel.x);
|
||||
var ty = math.frac(texel.y);
|
||||
var a = Lerp(samples[y0 * page.StorageSize + x0], samples[y0 * page.StorageSize + x1], tx);
|
||||
var b = Lerp(samples[y1 * page.StorageSize + x0], samples[y1 * page.StorageSize + x1], tx);
|
||||
return Lerp(a, b, ty);
|
||||
}
|
||||
|
||||
public static PlanetaryPageSummary Summarize(ReadOnlySpan<PlanetaryPageSample> samples, PlanetaryPageLayout page, float unresolvedHeightBound)
|
||||
{
|
||||
page.Validate();
|
||||
if (samples.Length != page.StorageSize * page.StorageSize) throw new ArgumentException("Page sample count does not match layout.", nameof(samples));
|
||||
var min = float.PositiveInfinity;
|
||||
var max = float.NegativeInfinity;
|
||||
var maxSlope = 0.0f;
|
||||
foreach (var sample in samples)
|
||||
{
|
||||
min = MathF.Min(min, sample.Height);
|
||||
max = MathF.Max(max, sample.Height);
|
||||
maxSlope = MathF.Max(maxSlope, math.length(sample.TangentGradient));
|
||||
}
|
||||
return new(min, max, maxSlope, MathF.Max(unresolvedHeightBound, 0), AngularTexelSize(page));
|
||||
}
|
||||
|
||||
private static PlanetaryPageSample Lerp(PlanetaryPageSample a, PlanetaryPageSample b, float t) => new(
|
||||
math.lerp(a.Height, b.Height, t),
|
||||
math.lerp(a.TangentGradient, b.TangentGradient, t),
|
||||
math.lerp(a.Ridge, b.Ridge, t),
|
||||
math.lerp(a.Gully, b.Gully, t));
|
||||
}
|
||||
|
||||
public readonly record struct PlanetaryBakedPage(
|
||||
PlanetaryPageLayout Layout,
|
||||
PlanetaryPageSample[] Samples,
|
||||
PlanetaryPageSummary Summary);
|
||||
|
||||
public static class PlanetaryPageBaker
|
||||
{
|
||||
public static PlanetaryBakedPage Bake<TSource>(
|
||||
in PlanetaryFieldDefinition field,
|
||||
PlanetaryPageLayout layout,
|
||||
TSource source)
|
||||
where TSource : IPlanetaryBaseField
|
||||
{
|
||||
layout.Validate(); field.Validate();
|
||||
var spacing = PlanetaryPageSampling.NominalAngularTexelSize(layout) * field.Radius;
|
||||
var parentSpacing = layout.Tile.Level == 0
|
||||
? 0
|
||||
: PlanetaryPageSampling.NominalAngularTexelSize(new(layout.Tile.Parent(), layout.InteriorSize, layout.BorderSize)) * field.Radius;
|
||||
var samples = new PlanetaryPageSample[layout.StorageSize * layout.StorageSize];
|
||||
var unresolved = 0.0f;
|
||||
for (var y = 0; y < layout.StorageSize; y++)
|
||||
for (var x = 0; x < layout.StorageSize; x++)
|
||||
{
|
||||
var direction = PlanetaryPageSampling.Direction(layout, x, y);
|
||||
var baseSample = source.Sample(direction);
|
||||
var child = PlanetaryField.Sample(field, direction, baseSample, PlanetaryQueryScale.AtFootprint(spacing));
|
||||
var height = child.RadialDisplacement;
|
||||
var gradient = child.TangentGradient;
|
||||
if (parentSpacing > 0)
|
||||
{
|
||||
var parent = PlanetaryField.Sample(field, direction, baseSample, PlanetaryQueryScale.AtFootprint(parentSpacing));
|
||||
height -= parent.RadialDisplacement;
|
||||
gradient -= parent.TangentGradient;
|
||||
}
|
||||
samples[y * layout.StorageSize + x] = new(height, gradient, child.Ridge, child.Gully);
|
||||
unresolved = MathF.Max(unresolved, child.UnresolvedHeightBound);
|
||||
}
|
||||
return new(layout, samples, PlanetaryPageSampling.Summarize(samples, layout, unresolved));
|
||||
}
|
||||
}
|
||||
|
||||
public readonly record struct PlanetaryResidentPage(PlanetaryBakedPage Page, float Blend)
|
||||
{
|
||||
public PlanetaryResidentPage Validate()
|
||||
{
|
||||
if (!float.IsFinite(Blend) || Blend is < 0 or > 1) throw new ArgumentOutOfRangeException(nameof(Blend));
|
||||
Page.Layout.Validate();
|
||||
if (Page.Samples.Length != Page.Layout.StorageSize * Page.Layout.StorageSize) throw new ArgumentException("Page payload does not match layout.");
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public static class PlanetaryPageSetSampling
|
||||
{
|
||||
public static bool TrySample(ReadOnlySpan<PlanetaryResidentPage> pages, float3 direction, out PlanetaryPageSample sample)
|
||||
{
|
||||
PlanetaryTopology.ValidateDirection(direction);
|
||||
var height = 0.0f;
|
||||
var gradient = float3.zero;
|
||||
var ridge = 0.0f;
|
||||
var gully = 0.0f;
|
||||
var found = false;
|
||||
var deepestLevel = -1;
|
||||
foreach (var resident in pages)
|
||||
{
|
||||
resident.Validate();
|
||||
if (!PlanetaryTopology.TryLocalCoordinate(direction, resident.Page.Layout.Tile, out var local)) continue;
|
||||
var contribution = PlanetaryPageSampling.Bilinear(resident.Page.Samples, resident.Page.Layout, local);
|
||||
var blend = math.saturate(resident.Blend);
|
||||
height += contribution.Height * blend;
|
||||
gradient += contribution.TangentGradient * blend;
|
||||
if (resident.Page.Layout.Tile.Level >= deepestLevel)
|
||||
{
|
||||
ridge = found ? math.lerp(ridge, contribution.Ridge, blend) : contribution.Ridge;
|
||||
gully = found ? math.lerp(gully, contribution.Gully, blend) : contribution.Gully;
|
||||
deepestLevel = resident.Page.Layout.Tile.Level;
|
||||
}
|
||||
found = true;
|
||||
}
|
||||
sample = new(height, gradient, ridge, gully);
|
||||
return found;
|
||||
}
|
||||
}
|
||||
42
src/CultMath/PlanetaryPatch.cs
Normal file
42
src/CultMath/PlanetaryPatch.cs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
namespace CultMath;
|
||||
|
||||
public readonly record struct PlanetaryPatchVertex(float2 LocalCoordinate, float3 UnitDirection);
|
||||
|
||||
public readonly record struct PlanetaryPatchMesh(
|
||||
PlanetaryCubeFace Face,
|
||||
int CellsPerAxis,
|
||||
PlanetaryPatchVertex[] Vertices,
|
||||
int[] Indices);
|
||||
|
||||
public static class PlanetaryPatch
|
||||
{
|
||||
public static PlanetaryPatchMesh CreateFace(PlanetaryCubeFace face, int cellsPerAxis)
|
||||
{
|
||||
if (!Enum.IsDefined(typeof(PlanetaryCubeFace), face)) throw new ArgumentOutOfRangeException(nameof(face));
|
||||
if (cellsPerAxis < 1 || cellsPerAxis > 4096) throw new ArgumentOutOfRangeException(nameof(cellsPerAxis));
|
||||
var axisVertices = cellsPerAxis + 1;
|
||||
var vertices = new PlanetaryPatchVertex[checked(axisVertices * axisVertices)];
|
||||
for (var y = 0; y < axisVertices; y++)
|
||||
for (var x = 0; x < axisVertices; x++)
|
||||
{
|
||||
var local = new float2(x / (float)cellsPerAxis, y / (float)cellsPerAxis);
|
||||
vertices[y * axisVertices + x] = new(local, PlanetaryTopology.Direction(new(face, local.x * 2 - 1, local.y * 2 - 1)));
|
||||
}
|
||||
var indices = new int[checked(cellsPerAxis * cellsPerAxis * 6)];
|
||||
var write = 0;
|
||||
for (var y = 0; y < cellsPerAxis; y++)
|
||||
for (var x = 0; x < cellsPerAxis; x++)
|
||||
{
|
||||
var lowerLeft = y * axisVertices + x;
|
||||
var lowerRight = lowerLeft + 1;
|
||||
var upperLeft = lowerLeft + axisVertices;
|
||||
var upperRight = upperLeft + 1;
|
||||
indices[write++] = lowerLeft; indices[write++] = lowerRight; indices[write++] = upperRight;
|
||||
indices[write++] = lowerLeft; indices[write++] = upperRight; indices[write++] = upperLeft;
|
||||
}
|
||||
return new(face, cellsPerAxis, vertices, indices);
|
||||
}
|
||||
|
||||
public static PlanetaryPatchMesh[] CreateCubeSphere(int cellsPerAxis)
|
||||
=> ((PlanetaryCubeFace[])Enum.GetValues(typeof(PlanetaryCubeFace))).Select(face => CreateFace(face, cellsPerAxis)).ToArray();
|
||||
}
|
||||
320
src/CultMath/PlanetaryProjection.cs
Normal file
320
src/CultMath/PlanetaryProjection.cs
Normal file
|
|
@ -0,0 +1,320 @@
|
|||
namespace CultMath;
|
||||
|
||||
public enum PlanetaryProjectionKind
|
||||
{
|
||||
Equirectangular,
|
||||
WebMercator,
|
||||
EqualEarth,
|
||||
Orthographic,
|
||||
AzimuthalEquidistant,
|
||||
AzimuthalEqualArea,
|
||||
CubeAtlas,
|
||||
LocalTangent,
|
||||
}
|
||||
|
||||
public readonly record struct PlanetaryProjectionParameters(
|
||||
PlanetaryProjectionKind Kind,
|
||||
double CenterLongitude = 0,
|
||||
double CenterLatitude = 0,
|
||||
double Scale = 1)
|
||||
{
|
||||
public PlanetaryProjectionParameters Validate()
|
||||
{
|
||||
if (!double.IsFinite(CenterLongitude) || !double.IsFinite(CenterLatitude) || !double.IsFinite(Scale) || Scale <= 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(Scale));
|
||||
if (CenterLatitude is < -Math.PI / 2 or > Math.PI / 2) throw new ArgumentOutOfRangeException(nameof(CenterLatitude));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public static class PlanetaryProjection
|
||||
{
|
||||
private const double EqualEarthA1 = 1.340264;
|
||||
private const double EqualEarthA2 = -0.081106;
|
||||
private const double EqualEarthA3 = 0.000893;
|
||||
private const double EqualEarthA4 = 0.003796;
|
||||
private static readonly double EqualEarthM = Math.Sqrt(3) / 2;
|
||||
private static readonly double EqualEarthXMax = Math.PI / (EqualEarthM * EqualEarthA1);
|
||||
private static readonly double EqualEarthYMax = EqualEarthY(Math.Asin(EqualEarthM));
|
||||
public static readonly double WebMercatorMaximumLatitude = Math.Atan(Math.Sinh(Math.PI));
|
||||
|
||||
public static bool TryForward(float3 direction, PlanetaryProjectionParameters parameters, out double2 coordinate)
|
||||
{
|
||||
parameters.Validate();
|
||||
PlanetaryTopology.ValidateDirection(direction);
|
||||
var d = math.normalize(direction);
|
||||
if (parameters.Kind == PlanetaryProjectionKind.CubeAtlas)
|
||||
{
|
||||
var face = PlanetaryTopology.FaceCoordinate(d);
|
||||
var index = (int)face.Face;
|
||||
var column = index % 3;
|
||||
var row = index / 3;
|
||||
coordinate = new(
|
||||
(-1 + (column + (face.U + 1) * 0.5) * (2.0 / 3.0)) / parameters.Scale,
|
||||
(1 - (row + (face.V + 1) * 0.5)) / parameters.Scale);
|
||||
return true;
|
||||
}
|
||||
|
||||
var longitude = Math.Atan2(d.y, d.x);
|
||||
var latitude = Math.Asin(Math.Clamp(d.z, -1, 1));
|
||||
var deltaLongitude = WrapLongitude(longitude - parameters.CenterLongitude);
|
||||
var centered = CenteredForward(deltaLongitude, latitude, parameters, out coordinate);
|
||||
coordinate /= parameters.Scale;
|
||||
return centered;
|
||||
}
|
||||
|
||||
public static bool TryInverse(double2 coordinate, PlanetaryProjectionParameters parameters, out float3 direction)
|
||||
{
|
||||
parameters.Validate();
|
||||
if (!double.IsFinite(coordinate.x) || !double.IsFinite(coordinate.y))
|
||||
{
|
||||
direction = float3.zero;
|
||||
return false;
|
||||
}
|
||||
coordinate *= parameters.Scale;
|
||||
if (parameters.Kind == PlanetaryProjectionKind.CubeAtlas)
|
||||
return TryInverseCubeAtlas(coordinate, out direction);
|
||||
|
||||
if (!CenteredInverse(coordinate, parameters, out var deltaLongitude, out var latitude))
|
||||
{
|
||||
direction = float3.zero;
|
||||
return false;
|
||||
}
|
||||
var longitude = WrapLongitude(deltaLongitude + parameters.CenterLongitude);
|
||||
var cosLatitude = Math.Cos(latitude);
|
||||
direction = math.normalize(new float3(
|
||||
(float)(cosLatitude * Math.Cos(longitude)),
|
||||
(float)(cosLatitude * Math.Sin(longitude)),
|
||||
(float)Math.Sin(latitude)));
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool CenteredForward(double longitude, double latitude, PlanetaryProjectionParameters p, out double2 coordinate)
|
||||
{
|
||||
switch (p.Kind)
|
||||
{
|
||||
case PlanetaryProjectionKind.Equirectangular:
|
||||
coordinate = new(longitude / Math.PI, latitude / (Math.PI * 0.5));
|
||||
return true;
|
||||
case PlanetaryProjectionKind.WebMercator:
|
||||
if (Math.Abs(latitude) > WebMercatorMaximumLatitude) { coordinate = double2.zero; return false; }
|
||||
coordinate = new(longitude / Math.PI, Math.Asinh(Math.Tan(latitude)) / Math.PI);
|
||||
return true;
|
||||
case PlanetaryProjectionKind.EqualEarth:
|
||||
{
|
||||
var theta = Math.Asin(EqualEarthM * Math.Sin(latitude));
|
||||
var theta2 = theta * theta;
|
||||
var theta6 = theta2 * theta2 * theta2;
|
||||
var denominator = EqualEarthM * (EqualEarthA1 + 3 * EqualEarthA2 * theta2 + theta6 * (7 * EqualEarthA3 + 9 * EqualEarthA4 * theta2));
|
||||
coordinate = new(longitude * Math.Cos(theta) / denominator / EqualEarthXMax, EqualEarthY(theta) / EqualEarthYMax);
|
||||
return true;
|
||||
}
|
||||
case PlanetaryProjectionKind.Orthographic:
|
||||
case PlanetaryProjectionKind.AzimuthalEquidistant:
|
||||
case PlanetaryProjectionKind.AzimuthalEqualArea:
|
||||
case PlanetaryProjectionKind.LocalTangent:
|
||||
return ForwardAzimuthal(longitude, latitude, p, out coordinate);
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(p));
|
||||
}
|
||||
}
|
||||
|
||||
private static bool CenteredInverse(double2 coordinate, PlanetaryProjectionParameters p, out double longitude, out double latitude)
|
||||
{
|
||||
longitude = latitude = 0;
|
||||
switch (p.Kind)
|
||||
{
|
||||
case PlanetaryProjectionKind.Equirectangular:
|
||||
if (Math.Abs(coordinate.y) > 1) return false;
|
||||
longitude = coordinate.x * Math.PI; latitude = coordinate.y * Math.PI * 0.5; return Math.Abs(coordinate.x) <= 1;
|
||||
case PlanetaryProjectionKind.WebMercator:
|
||||
if (Math.Abs(coordinate.x) > 1 || Math.Abs(coordinate.y) > 1) return false;
|
||||
longitude = coordinate.x * Math.PI; latitude = Math.Atan(Math.Sinh(coordinate.y * Math.PI)); return true;
|
||||
case PlanetaryProjectionKind.EqualEarth:
|
||||
{
|
||||
if (Math.Abs(coordinate.x) > 1 || Math.Abs(coordinate.y) > 1) return false;
|
||||
var targetY = coordinate.y * EqualEarthYMax;
|
||||
var theta = targetY / EqualEarthA1;
|
||||
for (var i = 0; i < 8; i++)
|
||||
{
|
||||
var theta2 = theta * theta;
|
||||
var theta6 = theta2 * theta2 * theta2;
|
||||
var f = EqualEarthY(theta) - targetY;
|
||||
var derivative = EqualEarthA1 + 3 * EqualEarthA2 * theta2 + theta6 * (7 * EqualEarthA3 + 9 * EqualEarthA4 * theta2);
|
||||
theta -= f / derivative;
|
||||
}
|
||||
var sinLatitude = Math.Sin(theta) / EqualEarthM;
|
||||
if (Math.Abs(sinLatitude) > 1.0000000001) return false;
|
||||
latitude = Math.Asin(Math.Clamp(sinLatitude, -1, 1));
|
||||
var thetaSquared = theta * theta;
|
||||
var thetaSixth = thetaSquared * thetaSquared * thetaSquared;
|
||||
var denominator = EqualEarthM * (EqualEarthA1 + 3 * EqualEarthA2 * thetaSquared + thetaSixth * (7 * EqualEarthA3 + 9 * EqualEarthA4 * thetaSquared));
|
||||
longitude = coordinate.x * EqualEarthXMax * denominator / Math.Cos(theta);
|
||||
return Math.Abs(longitude) <= Math.PI + 1.0e-10;
|
||||
}
|
||||
case PlanetaryProjectionKind.Orthographic:
|
||||
case PlanetaryProjectionKind.AzimuthalEquidistant:
|
||||
case PlanetaryProjectionKind.AzimuthalEqualArea:
|
||||
case PlanetaryProjectionKind.LocalTangent:
|
||||
return InverseAzimuthal(coordinate, p, out longitude, out latitude);
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(p));
|
||||
}
|
||||
}
|
||||
|
||||
private static bool ForwardAzimuthal(double longitude, double latitude, PlanetaryProjectionParameters p, out double2 coordinate)
|
||||
{
|
||||
var sin0 = Math.Sin(p.CenterLatitude); var cos0 = Math.Cos(p.CenterLatitude);
|
||||
var sin = Math.Sin(latitude); var cos = Math.Cos(latitude); var cosLon = Math.Cos(longitude);
|
||||
var cosC = Math.Clamp(sin0 * sin + cos0 * cos * cosLon, -1, 1);
|
||||
var rawX = cos * Math.Sin(longitude);
|
||||
var rawY = cos0 * sin - sin0 * cos * cosLon;
|
||||
double factor;
|
||||
switch (p.Kind)
|
||||
{
|
||||
case PlanetaryProjectionKind.Orthographic:
|
||||
if (cosC < 0) { coordinate = double2.zero; return false; }
|
||||
factor = 1; break;
|
||||
case PlanetaryProjectionKind.AzimuthalEquidistant:
|
||||
{
|
||||
var c = Math.Acos(cosC); factor = c < 1.0e-12 ? 1 : c / Math.Sin(c); factor /= Math.PI; break;
|
||||
}
|
||||
case PlanetaryProjectionKind.AzimuthalEqualArea:
|
||||
factor = Math.Sqrt(2 / Math.Max(1 + cosC, 1.0e-20)) * 0.5; break;
|
||||
case PlanetaryProjectionKind.LocalTangent:
|
||||
if (cosC <= 0) { coordinate = double2.zero; return false; }
|
||||
factor = 1 / cosC; break;
|
||||
default: throw new ArgumentOutOfRangeException(nameof(p));
|
||||
}
|
||||
coordinate = new(rawX * factor, rawY * factor);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool InverseAzimuthal(double2 coordinate, PlanetaryProjectionParameters p, out double longitude, out double latitude)
|
||||
{
|
||||
longitude = latitude = 0;
|
||||
var rho = Math.Sqrt(coordinate.x * coordinate.x + coordinate.y * coordinate.y);
|
||||
double c;
|
||||
switch (p.Kind)
|
||||
{
|
||||
case PlanetaryProjectionKind.Orthographic: if (rho > 1) return false; c = Math.Asin(Math.Clamp(rho, 0, 1)); break;
|
||||
case PlanetaryProjectionKind.AzimuthalEquidistant: if (rho > 1) return false; c = rho * Math.PI; break;
|
||||
case PlanetaryProjectionKind.AzimuthalEqualArea: if (rho > 1) return false; c = 2 * Math.Asin(Math.Clamp(rho, 0, 1)); break;
|
||||
case PlanetaryProjectionKind.LocalTangent: c = Math.Atan(rho); break;
|
||||
default: throw new ArgumentOutOfRangeException(nameof(p));
|
||||
}
|
||||
if (rho < 1.0e-12) { latitude = p.CenterLatitude; return true; }
|
||||
var sinC = Math.Sin(c); var cosC = Math.Cos(c);
|
||||
var sin0 = Math.Sin(p.CenterLatitude); var cos0 = Math.Cos(p.CenterLatitude);
|
||||
latitude = Math.Asin(Math.Clamp(cosC * sin0 + coordinate.y * sinC * cos0 / rho, -1, 1));
|
||||
longitude = Math.Atan2(coordinate.x * sinC, rho * cos0 * cosC - coordinate.y * sin0 * sinC);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool TryInverseCubeAtlas(double2 coordinate, out float3 direction)
|
||||
{
|
||||
direction = float3.zero;
|
||||
if (coordinate.x is < -1 or > 1 || coordinate.y is < -1 or > 1) return false;
|
||||
var column = Math.Min((int)((coordinate.x + 1) * 1.5), 2);
|
||||
var row = Math.Min((int)((1 - coordinate.y)), 1);
|
||||
var u = ((coordinate.x + 1) * 1.5 - column) * 2 - 1;
|
||||
var v = ((1 - coordinate.y) - row) * 2 - 1;
|
||||
direction = PlanetaryTopology.Direction(new((PlanetaryCubeFace)(row * 3 + column), u, v));
|
||||
return true;
|
||||
}
|
||||
|
||||
private static double EqualEarthY(double theta)
|
||||
{
|
||||
var theta2 = theta * theta;
|
||||
var theta6 = theta2 * theta2 * theta2;
|
||||
return theta * (EqualEarthA1 + EqualEarthA2 * theta2 + theta6 * (EqualEarthA3 + EqualEarthA4 * theta2));
|
||||
}
|
||||
|
||||
private static double WrapLongitude(double longitude)
|
||||
{
|
||||
longitude %= Math.PI * 2;
|
||||
if (longitude > Math.PI) longitude -= Math.PI * 2;
|
||||
if (longitude < -Math.PI) longitude += Math.PI * 2;
|
||||
return longitude;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly record struct PlanetaryMapTileLayout(
|
||||
PlanetaryProjectionParameters Projection,
|
||||
int Level,
|
||||
int X,
|
||||
int Y,
|
||||
int InteriorSize,
|
||||
int BorderSize)
|
||||
{
|
||||
public int StorageSize => checked(InteriorSize + BorderSize * 2);
|
||||
|
||||
public PlanetaryMapTileLayout Validate()
|
||||
{
|
||||
Projection.Validate();
|
||||
if (Level is < 0 or > 30) throw new ArgumentOutOfRangeException(nameof(Level));
|
||||
var count = 1 << Level;
|
||||
if ((uint)X >= (uint)count || (uint)Y >= (uint)count) throw new ArgumentOutOfRangeException(nameof(X));
|
||||
if (InteriorSize < 2 || BorderSize < 0 || BorderSize >= InteriorSize) throw new ArgumentOutOfRangeException(nameof(InteriorSize));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public static class PlanetaryMapTileSampling
|
||||
{
|
||||
public static bool TryDirection(PlanetaryMapTileLayout tile, int storageX, int storageY, out float3 direction)
|
||||
{
|
||||
tile.Validate();
|
||||
if ((uint)storageX >= (uint)tile.StorageSize || (uint)storageY >= (uint)tile.StorageSize) throw new ArgumentOutOfRangeException(nameof(storageX));
|
||||
var count = 1 << tile.Level;
|
||||
var localU = (storageX - tile.BorderSize) / (double)(tile.InteriorSize - 1);
|
||||
var localV = (storageY - tile.BorderSize) / (double)(tile.InteriorSize - 1);
|
||||
var map = new double2(-1 + 2 * (tile.X + localU) / count, 1 - 2 * (tile.Y + localV) / count);
|
||||
return PlanetaryProjection.TryInverse(map, tile.Projection, out direction);
|
||||
}
|
||||
}
|
||||
|
||||
public readonly record struct PlanetaryMapTileKey(
|
||||
ulong FieldVersion,
|
||||
uint ProjectionVersion,
|
||||
uint LayerId,
|
||||
PlanetaryMapTileLayout Layout,
|
||||
PlanetaryQueryScale QueryScale)
|
||||
{
|
||||
public PlanetaryMapTileKey Validate()
|
||||
{
|
||||
if (FieldVersion == 0 || ProjectionVersion == 0) throw new ArgumentOutOfRangeException(nameof(FieldVersion));
|
||||
Layout.Validate(); QueryScale.Validate();
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly record struct PlanetarySurfaceMapTile(
|
||||
PlanetaryMapTileKey Key,
|
||||
PlanetarySurfaceSample[] Samples,
|
||||
bool[] Validity);
|
||||
|
||||
public static class PlanetaryMapTileBaker
|
||||
{
|
||||
public static PlanetarySurfaceMapTile Bake<TSource>(
|
||||
in PlanetaryFieldDefinition field,
|
||||
PlanetaryMapTileKey key,
|
||||
TSource source)
|
||||
where TSource : IPlanetaryBaseField
|
||||
{
|
||||
field.Validate(); key.Validate();
|
||||
if (field.FieldVersion != key.FieldVersion) throw new ArgumentException("Tile field version does not match definition.", nameof(key));
|
||||
var count = key.Layout.StorageSize * key.Layout.StorageSize;
|
||||
var samples = new PlanetarySurfaceSample[count];
|
||||
var validity = new bool[count];
|
||||
for (var y = 0; y < key.Layout.StorageSize; y++)
|
||||
for (var x = 0; x < key.Layout.StorageSize; x++)
|
||||
{
|
||||
var index = y * key.Layout.StorageSize + x;
|
||||
if (!PlanetaryMapTileSampling.TryDirection(key.Layout, x, y, out var direction)) continue;
|
||||
samples[index] = PlanetaryField.Sample(field, direction, source.Sample(direction), key.QueryScale);
|
||||
validity[index] = true;
|
||||
}
|
||||
return new(key, samples, validity);
|
||||
}
|
||||
}
|
||||
191
src/CultMath/PlanetaryQueries.cs
Normal file
191
src/CultMath/PlanetaryQueries.cs
Normal file
|
|
@ -0,0 +1,191 @@
|
|||
namespace CultMath;
|
||||
|
||||
public readonly record struct PlanetaryRadialBounds(float MinimumDisplacement, float MaximumDisplacement)
|
||||
{
|
||||
public PlanetaryRadialBounds Validate()
|
||||
{
|
||||
if (!float.IsFinite(MinimumDisplacement) || !float.IsFinite(MaximumDisplacement) || MinimumDisplacement > MaximumDisplacement)
|
||||
throw new ArgumentOutOfRangeException(nameof(MinimumDisplacement));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly record struct PlanetaryRayHit(
|
||||
double Distance,
|
||||
double3 PlanetLocalPosition,
|
||||
PlanetarySurfaceSample Surface);
|
||||
|
||||
public readonly record struct PlanetaryPathSample(double Distance, PlanetarySurfaceSample Surface);
|
||||
|
||||
public readonly record struct PlanetaryRegionSummary(
|
||||
float MinimumDisplacement,
|
||||
float MaximumDisplacement,
|
||||
float MaximumSlope,
|
||||
float MaximumUnresolvedHeight);
|
||||
|
||||
public static class PlanetaryQueries
|
||||
{
|
||||
public static bool TryIntersectRay<TSource>(
|
||||
in PlanetaryFieldDefinition field,
|
||||
double3 rayOrigin,
|
||||
double3 rayDirection,
|
||||
double maximumDistance,
|
||||
in PlanetaryQueryScale scale,
|
||||
in PlanetaryRadialBounds bounds,
|
||||
TSource source,
|
||||
out PlanetaryRayHit hit)
|
||||
where TSource : IPlanetaryBaseField
|
||||
{
|
||||
field.Validate(); scale.Validate(); bounds.Validate();
|
||||
ValidateFinite(rayOrigin, nameof(rayOrigin)); ValidateFinite(rayDirection, nameof(rayDirection));
|
||||
if (!double.IsFinite(maximumDistance) || maximumDistance <= 0) throw new ArgumentOutOfRangeException(nameof(maximumDistance));
|
||||
var directionLength = Length(rayDirection);
|
||||
if (directionLength < 1.0e-15) throw new ArgumentOutOfRangeException(nameof(rayDirection));
|
||||
var direction = rayDirection / directionLength;
|
||||
var outerRadius = field.Radius + bounds.MaximumDisplacement + scale.MaximumUnresolvedHeight;
|
||||
if (!double.IsFinite(outerRadius)) outerRadius = field.Radius + bounds.MaximumDisplacement;
|
||||
if (!TrySphereInterval(rayOrigin, direction, outerRadius, out var enter, out var leave)) { hit = default; return false; }
|
||||
var start = Math.Max(enter, 0);
|
||||
var end = Math.Min(leave, maximumDistance);
|
||||
if (start > end) { hit = default; return false; }
|
||||
|
||||
var previousDistance = start;
|
||||
var previous = SignedSurfaceDistance(field, rayOrigin + direction * previousDistance, scale, source, out var previousSample);
|
||||
if (previous <= 0)
|
||||
{
|
||||
hit = new(previousDistance, rayOrigin + direction * previousDistance, previousSample);
|
||||
return true;
|
||||
}
|
||||
var minimumStep = Math.Max(scale.FootprintMeters * 0.5, field.Radius * 1.0e-7);
|
||||
for (var iteration = 0; iteration < 512 && previousDistance < end; iteration++)
|
||||
{
|
||||
var nextDistance = Math.Min(previousDistance + Math.Max(previous * 0.5, minimumStep), end);
|
||||
var next = SignedSurfaceDistance(field, rayOrigin + direction * nextDistance, scale, source, out var nextSample);
|
||||
if (next <= 0)
|
||||
{
|
||||
var low = previousDistance;
|
||||
var high = nextDistance;
|
||||
var surface = nextSample;
|
||||
for (var refine = 0; refine < 32; refine++)
|
||||
{
|
||||
var middle = (low + high) * 0.5;
|
||||
var value = SignedSurfaceDistance(field, rayOrigin + direction * middle, scale, source, out var middleSample);
|
||||
if (value > 0) low = middle;
|
||||
else { high = middle; surface = middleSample; }
|
||||
}
|
||||
var position = rayOrigin + direction * high;
|
||||
hit = new(high, position, surface);
|
||||
return true;
|
||||
}
|
||||
if (nextDistance >= end) break;
|
||||
previousDistance = nextDistance;
|
||||
previous = next;
|
||||
previousSample = nextSample;
|
||||
}
|
||||
hit = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static double SegmentClearance<TSource>(
|
||||
in PlanetaryFieldDefinition field,
|
||||
double3 start,
|
||||
double3 end,
|
||||
double sampleSpacing,
|
||||
in PlanetaryQueryScale scale,
|
||||
TSource source)
|
||||
where TSource : IPlanetaryBaseField
|
||||
{
|
||||
ValidateFinite(start, nameof(start)); ValidateFinite(end, nameof(end));
|
||||
if (!double.IsFinite(sampleSpacing) || sampleSpacing <= 0) throw new ArgumentOutOfRangeException(nameof(sampleSpacing));
|
||||
var delta = end - start;
|
||||
var distance = Length(delta);
|
||||
var steps = Math.Max((int)Math.Ceiling(distance / sampleSpacing), 1);
|
||||
var minimum = double.PositiveInfinity;
|
||||
for (var i = 0; i <= steps; i++)
|
||||
{
|
||||
var position = start + delta * (i / (double)steps);
|
||||
minimum = Math.Min(minimum, SignedSurfaceDistance(field, position, scale, source, out _));
|
||||
}
|
||||
return minimum;
|
||||
}
|
||||
|
||||
public static PlanetaryPathSample[] SampleGreatCircle<TSource>(
|
||||
in PlanetaryFieldDefinition field,
|
||||
float3 startDirection,
|
||||
float3 endDirection,
|
||||
int segmentCount,
|
||||
in PlanetaryQueryScale scale,
|
||||
TSource source)
|
||||
where TSource : IPlanetaryBaseField
|
||||
{
|
||||
if (segmentCount < 1) throw new ArgumentOutOfRangeException(nameof(segmentCount));
|
||||
PlanetaryTopology.ValidateDirection(startDirection); PlanetaryTopology.ValidateDirection(endDirection);
|
||||
var a = math.normalize(startDirection); var b = math.normalize(endDirection);
|
||||
var angle = MathF.Acos(math.clamp(math.dot(a, b), -1, 1));
|
||||
var sinAngle = MathF.Sin(angle);
|
||||
var result = new PlanetaryPathSample[segmentCount + 1];
|
||||
for (var i = 0; i <= segmentCount; i++)
|
||||
{
|
||||
var t = i / (float)segmentCount;
|
||||
var direction = sinAngle < 1.0e-6f
|
||||
? math.normalize(math.lerp(a, b, t))
|
||||
: math.normalize(a * (MathF.Sin((1 - t) * angle) / sinAngle) + b * (MathF.Sin(t * angle) / sinAngle));
|
||||
var sample = PlanetaryField.Sample(field, direction, source.Sample(direction), scale);
|
||||
result[i] = new(angle * field.Radius * t, sample);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static PlanetaryRegionSummary Summarize<TSource>(
|
||||
in PlanetaryFieldDefinition field,
|
||||
ReadOnlySpan<float3> directions,
|
||||
in PlanetaryQueryScale scale,
|
||||
TSource source)
|
||||
where TSource : IPlanetaryBaseField
|
||||
{
|
||||
if (directions.IsEmpty) throw new ArgumentException("At least one direction is required.", nameof(directions));
|
||||
var min = float.PositiveInfinity; var max = float.NegativeInfinity; var slope = 0.0f; var unresolved = 0.0f;
|
||||
foreach (var direction in directions)
|
||||
{
|
||||
var normalized = math.normalize(direction);
|
||||
var sample = PlanetaryField.Sample(field, normalized, source.Sample(normalized), scale);
|
||||
min = MathF.Min(min, sample.RadialDisplacement);
|
||||
max = MathF.Max(max, sample.RadialDisplacement);
|
||||
slope = MathF.Max(slope, sample.Slope);
|
||||
unresolved = MathF.Max(unresolved, sample.UnresolvedHeightBound);
|
||||
}
|
||||
return new(min, max, slope, unresolved);
|
||||
}
|
||||
|
||||
private static double SignedSurfaceDistance<TSource>(
|
||||
in PlanetaryFieldDefinition field,
|
||||
double3 position,
|
||||
in PlanetaryQueryScale scale,
|
||||
TSource source,
|
||||
out PlanetarySurfaceSample sample)
|
||||
where TSource : IPlanetaryBaseField
|
||||
{
|
||||
var radius = Length(position);
|
||||
if (radius < 1.0e-15) { sample = default; return double.NegativeInfinity; }
|
||||
var direction = new float3((float)(position.x / radius), (float)(position.y / radius), (float)(position.z / radius));
|
||||
sample = PlanetaryField.Sample(field, direction, source.Sample(direction), scale);
|
||||
return radius - sample.SurfaceRadius;
|
||||
}
|
||||
|
||||
private static bool TrySphereInterval(double3 origin, double3 direction, double radius, out double enter, out double leave)
|
||||
{
|
||||
var b = Dot(origin, direction);
|
||||
var c = Dot(origin, origin) - radius * radius;
|
||||
var discriminant = b * b - c;
|
||||
if (discriminant < 0) { enter = leave = 0; return false; }
|
||||
var root = Math.Sqrt(discriminant);
|
||||
enter = -b - root; leave = -b + root; return leave >= 0;
|
||||
}
|
||||
|
||||
private static double Dot(double3 a, double3 b) => a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
private static double Length(double3 value) => Math.Sqrt(Dot(value, value));
|
||||
private static void ValidateFinite(double3 value, string name)
|
||||
{
|
||||
if (!double.IsFinite(value.x) || !double.IsFinite(value.y) || !double.IsFinite(value.z)) throw new ArgumentOutOfRangeException(name);
|
||||
}
|
||||
}
|
||||
144
src/CultMath/PlanetaryResidency.cs
Normal file
144
src/CultMath/PlanetaryResidency.cs
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
namespace CultMath;
|
||||
|
||||
public readonly record struct PlanetaryLodParameters(
|
||||
int MaximumLevel,
|
||||
int PageInteriorSize,
|
||||
int PageBorderSize,
|
||||
float ViewportHeightPixels,
|
||||
float MinimumFootprintMeters)
|
||||
{
|
||||
public PlanetaryLodParameters Validate()
|
||||
{
|
||||
if (MaximumLevel is < 0 or > PlanetaryTileAddress.MaxLevel) throw new ArgumentOutOfRangeException(nameof(MaximumLevel));
|
||||
_ = new PlanetaryPageLayout(new(PlanetaryCubeFace.PositiveX, 0, 0, 0), PageInteriorSize, PageBorderSize).Validate();
|
||||
if (!float.IsFinite(ViewportHeightPixels) || ViewportHeightPixels <= 0) throw new ArgumentOutOfRangeException(nameof(ViewportHeightPixels));
|
||||
if (!float.IsFinite(MinimumFootprintMeters) || MinimumFootprintMeters < 0) throw new ArgumentOutOfRangeException(nameof(MinimumFootprintMeters));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public static class PlanetaryLodSelector
|
||||
{
|
||||
public static int SelectLevel(
|
||||
in PlanetaryFieldDefinition field,
|
||||
float cameraDistanceFromCenter,
|
||||
in PlanetaryLodParameters parameters)
|
||||
{
|
||||
field.Validate(); parameters.Validate();
|
||||
if (!float.IsFinite(cameraDistanceFromCenter) || cameraDistanceFromCenter < 0) throw new ArgumentOutOfRangeException(nameof(cameraDistanceFromCenter));
|
||||
var altitude = MathF.Max(cameraDistanceFromCenter - field.Radius, 0.001f);
|
||||
var footprint = MathF.Max(altitude / parameters.ViewportHeightPixels * 2, parameters.MinimumFootprintMeters);
|
||||
var erosion = field.Erosion;
|
||||
for (var level = 0; level <= parameters.MaximumLevel; level++)
|
||||
{
|
||||
var page = new PlanetaryPageLayout(new(PlanetaryCubeFace.PositiveX, level, 0, 0), parameters.PageInteriorSize, parameters.PageBorderSize);
|
||||
var spacing = PlanetaryPageSampling.NominalAngularTexelSize(page) * field.Radius;
|
||||
var unresolved = ErosionFrequencyBands.Select(
|
||||
erosion.Scale * erosion.CellScale, spacing, erosion.Octaves,
|
||||
erosion.Lacunarity, erosion.Strength * erosion.Scale, erosion.Gain).UnresolvedHeightBound;
|
||||
if (unresolved <= footprint) return level;
|
||||
}
|
||||
return parameters.MaximumLevel;
|
||||
}
|
||||
|
||||
public static PlanetaryTileAddress[] SelectAncestorChain(
|
||||
in PlanetaryFieldDefinition field,
|
||||
float3 cameraDirection,
|
||||
float cameraDistanceFromCenter,
|
||||
in PlanetaryLodParameters parameters)
|
||||
{
|
||||
PlanetaryTopology.ValidateDirection(cameraDirection);
|
||||
var level = SelectLevel(field, cameraDistanceFromCenter, parameters);
|
||||
var result = new PlanetaryTileAddress[6 + level];
|
||||
for (var face = 0; face < 6; face++) result[face] = new((PlanetaryCubeFace)face, 0, 0, 0);
|
||||
var leaf = PlanetaryTopology.TileAt(cameraDirection, level);
|
||||
for (var currentLevel = level; currentLevel >= 1; currentLevel--)
|
||||
{
|
||||
result[6 + currentLevel - 1] = leaf;
|
||||
leaf = leaf.Parent();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly record struct PlanetaryResidentTile(PlanetaryTileAddress Tile, float Blend, bool Departing);
|
||||
|
||||
public readonly record struct PlanetaryResidencySnapshot(
|
||||
ulong ContentVersion,
|
||||
ulong PresentationVersion,
|
||||
PlanetaryResidentTile[] Tiles);
|
||||
|
||||
/// <summary>Owns presentation residency only. It never owns field content.</summary>
|
||||
public sealed class PlanetaryResidualResidency
|
||||
{
|
||||
private readonly Dictionary<ulong, PlanetaryTileAddress> residents = [];
|
||||
private readonly Dictionary<ulong, float> arrivals = [];
|
||||
private readonly Dictionary<ulong, (float Time, float StartingBlend)> departures = [];
|
||||
private float lastTime = float.NegativeInfinity;
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
residents.Clear(); arrivals.Clear(); departures.Clear(); lastTime = float.NegativeInfinity;
|
||||
}
|
||||
|
||||
public PlanetaryResidencySnapshot Update(ReadOnlySpan<PlanetaryTileAddress> desired, float timeSeconds, float transitionSeconds)
|
||||
{
|
||||
if (!float.IsFinite(timeSeconds)) throw new ArgumentOutOfRangeException(nameof(timeSeconds));
|
||||
if (!float.IsFinite(transitionSeconds) || transitionSeconds < 0) throw new ArgumentOutOfRangeException(nameof(transitionSeconds));
|
||||
if (timeSeconds < lastTime) Reset();
|
||||
lastTime = timeSeconds;
|
||||
|
||||
var desiredKeys = new HashSet<ulong>();
|
||||
foreach (var tile in desired)
|
||||
{
|
||||
var key = tile.StableKey;
|
||||
desiredKeys.Add(key);
|
||||
residents[key] = tile;
|
||||
if (!arrivals.ContainsKey(key)) arrivals[key] = timeSeconds;
|
||||
departures.Remove(key);
|
||||
}
|
||||
|
||||
foreach (var pair in residents.ToArray())
|
||||
{
|
||||
if (pair.Value.Level == 0 || desiredKeys.Contains(pair.Key)) continue;
|
||||
if (!departures.ContainsKey(pair.Key))
|
||||
{
|
||||
var startingBlend = transitionSeconds == 0
|
||||
? 1
|
||||
: Math.Clamp((timeSeconds - arrivals[pair.Key]) / transitionSeconds, 0, 1);
|
||||
departures[pair.Key] = (timeSeconds, startingBlend);
|
||||
}
|
||||
if (transitionSeconds > 0 && timeSeconds - departures[pair.Key].Time < transitionSeconds) continue;
|
||||
residents.Remove(pair.Key); arrivals.Remove(pair.Key); departures.Remove(pair.Key);
|
||||
}
|
||||
|
||||
var ordered = residents
|
||||
.OrderBy(pair => pair.Value.Level)
|
||||
.ThenBy(pair => pair.Value.Face)
|
||||
.ThenBy(pair => pair.Value.X)
|
||||
.ThenBy(pair => pair.Value.Y)
|
||||
.ToArray();
|
||||
var tiles = new PlanetaryResidentTile[ordered.Length];
|
||||
for (var i = 0; i < ordered.Length; i++)
|
||||
{
|
||||
var pair = ordered[i];
|
||||
var departing = departures.TryGetValue(pair.Key, out var departure);
|
||||
var blend = pair.Value.Level == 0 || transitionSeconds == 0
|
||||
? 1
|
||||
: departing
|
||||
? departure.StartingBlend * Math.Clamp(1 - (timeSeconds - departure.Time) / transitionSeconds, 0, 1)
|
||||
: Math.Clamp((timeSeconds - arrivals[pair.Key]) / transitionSeconds, 0, 1);
|
||||
tiles[i] = new(pair.Value, blend, departing);
|
||||
}
|
||||
return new(Hash(ordered.Select(pair => pair.Key)), Hash(tiles.Select(tile => tile.Tile.StableKey ^ (uint)BitConverter.SingleToInt32Bits(tile.Blend))), tiles);
|
||||
}
|
||||
|
||||
private static ulong Hash(IEnumerable<ulong> values)
|
||||
{
|
||||
const ulong offset = 14695981039346656037UL;
|
||||
const ulong prime = 1099511628211UL;
|
||||
var hash = offset;
|
||||
foreach (var value in values) hash = unchecked((hash ^ value) * prime);
|
||||
return hash == 0 ? 1 : hash;
|
||||
}
|
||||
}
|
||||
170
src/CultMath/PlanetaryTopology.cs
Normal file
170
src/CultMath/PlanetaryTopology.cs
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
namespace CultMath;
|
||||
|
||||
public enum PlanetaryCubeFace
|
||||
{
|
||||
PositiveX,
|
||||
NegativeX,
|
||||
PositiveY,
|
||||
NegativeY,
|
||||
PositiveZ,
|
||||
NegativeZ,
|
||||
}
|
||||
|
||||
public readonly record struct PlanetaryFaceCoordinate(PlanetaryCubeFace Face, double U, double V)
|
||||
{
|
||||
public PlanetaryFaceCoordinate Validate(bool allowOutsideFace = false)
|
||||
{
|
||||
if (!double.IsFinite(U) || !double.IsFinite(V)) throw new ArgumentOutOfRangeException(nameof(U));
|
||||
if (!allowOutsideFace && (U is < -1.0 or > 1.0 || V is < -1.0 or > 1.0))
|
||||
throw new ArgumentOutOfRangeException(nameof(U), "Face coordinates must be in [-1, 1].");
|
||||
if (!Enum.IsDefined(typeof(PlanetaryCubeFace), Face)) throw new ArgumentOutOfRangeException(nameof(Face));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly record struct PlanetaryTileAddress
|
||||
{
|
||||
public const int MaxLevel = 30;
|
||||
|
||||
public PlanetaryTileAddress(PlanetaryCubeFace face, int level, int x, int y)
|
||||
{
|
||||
if (!Enum.IsDefined(typeof(PlanetaryCubeFace), face)) throw new ArgumentOutOfRangeException(nameof(face));
|
||||
if (level is < 0 or > MaxLevel) throw new ArgumentOutOfRangeException(nameof(level));
|
||||
var count = 1 << level;
|
||||
if ((uint)x >= (uint)count) throw new ArgumentOutOfRangeException(nameof(x));
|
||||
if ((uint)y >= (uint)count) throw new ArgumentOutOfRangeException(nameof(y));
|
||||
Face = face;
|
||||
Level = level;
|
||||
X = x;
|
||||
Y = y;
|
||||
}
|
||||
|
||||
public PlanetaryCubeFace Face { get; }
|
||||
public int Level { get; }
|
||||
public int X { get; }
|
||||
public int Y { get; }
|
||||
public int AxisTileCount => 1 << Level;
|
||||
|
||||
public PlanetaryTileAddress Parent() => Level == 0
|
||||
? throw new InvalidOperationException("Root cube tile has no parent.")
|
||||
: new(Face, Level - 1, X >> 1, Y >> 1);
|
||||
|
||||
public PlanetaryTileAddress Child(int childX, int childY)
|
||||
{
|
||||
if ((uint)childX > 1) throw new ArgumentOutOfRangeException(nameof(childX));
|
||||
if ((uint)childY > 1) throw new ArgumentOutOfRangeException(nameof(childY));
|
||||
if (Level == MaxLevel) throw new InvalidOperationException($"Level {MaxLevel} cannot be subdivided.");
|
||||
return new(Face, Level + 1, (X << 1) | childX, (Y << 1) | childY);
|
||||
}
|
||||
|
||||
public PlanetaryFaceCoordinate PositionAt(double localU, double localV)
|
||||
{
|
||||
if (!double.IsFinite(localU) || localU is < 0.0 or > 1.0) throw new ArgumentOutOfRangeException(nameof(localU));
|
||||
if (!double.IsFinite(localV) || localV is < 0.0 or > 1.0) throw new ArgumentOutOfRangeException(nameof(localV));
|
||||
var count = AxisTileCount;
|
||||
return new(Face, -1.0 + 2.0 * (X + localU) / count, -1.0 + 2.0 * (Y + localV) / count);
|
||||
}
|
||||
|
||||
public ulong StableKey
|
||||
{
|
||||
get
|
||||
{
|
||||
const ulong offset = 14695981039346656037UL;
|
||||
const ulong prime = 1099511628211UL;
|
||||
var hash = offset;
|
||||
hash = unchecked((hash ^ (uint)Face) * prime);
|
||||
hash = unchecked((hash ^ (uint)Level) * prime);
|
||||
hash = unchecked((hash ^ (uint)X) * prime);
|
||||
hash = unchecked((hash ^ (uint)Y) * prime);
|
||||
return hash == 0 ? 1 : hash;
|
||||
}
|
||||
}
|
||||
public override string ToString() => $"{Face}/L{Level:D2}/{X}/{Y}";
|
||||
}
|
||||
|
||||
public static class PlanetaryTopology
|
||||
{
|
||||
private const float QuarterPi = MathF.PI * 0.25f;
|
||||
|
||||
public static float3 Direction(PlanetaryFaceCoordinate coordinate)
|
||||
{
|
||||
coordinate.Validate(allowOutsideFace: true);
|
||||
var u = MathF.Tan((float)coordinate.U * QuarterPi);
|
||||
var v = MathF.Tan((float)coordinate.V * QuarterPi);
|
||||
var cube = coordinate.Face switch
|
||||
{
|
||||
PlanetaryCubeFace.PositiveX => new float3(1, v, -u),
|
||||
PlanetaryCubeFace.NegativeX => new float3(-1, v, u),
|
||||
PlanetaryCubeFace.PositiveY => new float3(u, 1, -v),
|
||||
PlanetaryCubeFace.NegativeY => new float3(u, -1, v),
|
||||
PlanetaryCubeFace.PositiveZ => new float3(u, v, 1),
|
||||
PlanetaryCubeFace.NegativeZ => new float3(-u, v, -1),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(coordinate)),
|
||||
};
|
||||
return math.normalize(cube);
|
||||
}
|
||||
|
||||
public static PlanetaryFaceCoordinate FaceCoordinate(float3 direction)
|
||||
{
|
||||
ValidateDirection(direction);
|
||||
var d = math.normalize(direction);
|
||||
var a = math.abs(d);
|
||||
if (a.x >= a.y && a.x >= a.z)
|
||||
return d.x >= 0
|
||||
? FromCubeUv(PlanetaryCubeFace.PositiveX, -d.z / a.x, d.y / a.x)
|
||||
: FromCubeUv(PlanetaryCubeFace.NegativeX, d.z / a.x, d.y / a.x);
|
||||
if (a.y >= a.z)
|
||||
return d.y >= 0
|
||||
? FromCubeUv(PlanetaryCubeFace.PositiveY, d.x / a.y, -d.z / a.y)
|
||||
: FromCubeUv(PlanetaryCubeFace.NegativeY, d.x / a.y, d.z / a.y);
|
||||
return d.z >= 0
|
||||
? FromCubeUv(PlanetaryCubeFace.PositiveZ, d.x / a.z, d.y / a.z)
|
||||
: FromCubeUv(PlanetaryCubeFace.NegativeZ, -d.x / a.z, d.y / a.z);
|
||||
}
|
||||
|
||||
public static PlanetaryTileAddress TileAt(float3 direction, int level)
|
||||
{
|
||||
if (level is < 0 or > PlanetaryTileAddress.MaxLevel) throw new ArgumentOutOfRangeException(nameof(level));
|
||||
var face = FaceCoordinate(direction);
|
||||
var count = 1 << level;
|
||||
var x = Math.Clamp((int)((face.U * 0.5 + 0.5) * count), 0, count - 1);
|
||||
var y = Math.Clamp((int)((face.V * 0.5 + 0.5) * count), 0, count - 1);
|
||||
return new(face.Face, level, x, y);
|
||||
}
|
||||
|
||||
public static bool TryLocalCoordinate(float3 direction, PlanetaryTileAddress tile, out float2 local)
|
||||
{
|
||||
var face = FaceCoordinate(direction);
|
||||
if (face.Face != tile.Face) { local = float2.zero; return false; }
|
||||
var scaledU = (face.U * 0.5 + 0.5) * tile.AxisTileCount;
|
||||
var scaledV = (face.V * 0.5 + 0.5) * tile.AxisTileCount;
|
||||
local = new((float)(scaledU - tile.X), (float)(scaledV - tile.Y));
|
||||
return local.x is >= 0 and <= 1 && local.y is >= 0 and <= 1;
|
||||
}
|
||||
|
||||
public static float3 SurfaceNormal(float3 unitDirection, float3 worldDistanceTangentGradient)
|
||||
{
|
||||
ValidateDirection(unitDirection);
|
||||
ValidateFinite(worldDistanceTangentGradient, nameof(worldDistanceTangentGradient));
|
||||
var direction = math.normalize(unitDirection);
|
||||
var tangent = worldDistanceTangentGradient - direction * math.dot(worldDistanceTangentGradient, direction);
|
||||
return math.normalize(direction - tangent);
|
||||
}
|
||||
|
||||
private static PlanetaryFaceCoordinate FromCubeUv(PlanetaryCubeFace face, float cubeU, float cubeV) => new(
|
||||
face,
|
||||
MathF.Atan(cubeU) / QuarterPi,
|
||||
MathF.Atan(cubeV) / QuarterPi);
|
||||
|
||||
internal static void ValidateDirection(float3 direction)
|
||||
{
|
||||
ValidateFinite(direction, nameof(direction));
|
||||
if (math.length(direction) < 1.0e-12f) throw new ArgumentOutOfRangeException(nameof(direction));
|
||||
}
|
||||
|
||||
internal static void ValidateFinite(float3 value, string name)
|
||||
{
|
||||
if (!float.IsFinite(value.x) || !float.IsFinite(value.y) || !float.IsFinite(value.z))
|
||||
throw new ArgumentOutOfRangeException(name);
|
||||
}
|
||||
}
|
||||
Reference in a new issue