Transfer planetary geometry into GameCult.Geometry
This commit is contained in:
parent
9293471630
commit
756f5a3337
70 changed files with 4105 additions and 31 deletions
|
|
@ -0,0 +1,47 @@
|
|||
using Xunit;
|
||||
|
||||
using CultMath;
|
||||
using GameCult.Geometry;
|
||||
|
||||
namespace GameCult.Geometry.Planetary.Tests;
|
||||
|
||||
public sealed class AdvancedErosionFilterTests
|
||||
{
|
||||
[Fact]
|
||||
public void DefaultReferenceFixtureIsStable()
|
||||
{
|
||||
var result = AdvancedErosionFilter.Sample(
|
||||
new float2(0.42f, 0.31f),
|
||||
new float3(0.5f, 0.1f, -0.2f),
|
||||
0.0f,
|
||||
AdvancedErosionParameters.Default);
|
||||
|
||||
Assert.Equal(0.0639375f, result.Magnitude, precision: 7);
|
||||
Assert.True(float.IsFinite(result.Delta.x));
|
||||
Assert.True(float.IsFinite(result.Delta.y));
|
||||
Assert.True(float.IsFinite(result.Delta.z));
|
||||
Assert.InRange(result.RidgeMap, -1.0f, 1.0f);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FlatInputAndZeroRoundingRemainFinite()
|
||||
{
|
||||
var parameters = AdvancedErosionParameters.Default with { Rounding = float4.zero };
|
||||
var result = AdvancedErosionFilter.Sample(new float2(0.25f, 0.75f), float3.zero, 0.0f, parameters);
|
||||
|
||||
Assert.True(float.IsFinite(result.Delta.x));
|
||||
Assert.True(float.IsFinite(result.RidgeMap));
|
||||
Assert.True(float.IsFinite(result.FadeTarget));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PhacellePartialNormalizationIsFiniteAtEndpoints()
|
||||
{
|
||||
foreach (var normalization in new[] { 0.0f, 0.5f, 1.0f })
|
||||
{
|
||||
var sample = AdvancedErosionFilter.Phacelle(new float2(1.7f, -3.2f), new float2(0.6f, -0.8f), 0.7f, 0.25f, normalization);
|
||||
Assert.True(float.IsFinite(sample.x));
|
||||
Assert.True(float.IsFinite(sample.y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
using Xunit;
|
||||
|
||||
using CultMath;
|
||||
using GameCult.Geometry;
|
||||
|
||||
namespace GameCult.Geometry.Planetary.Tests;
|
||||
|
||||
public sealed class ErosionFrequencyBandsTests
|
||||
{
|
||||
[Fact]
|
||||
public void RadiusIndependentWorldWavelengthSelectsSameBands()
|
||||
{
|
||||
var small = ErosionFrequencyBands.Select(1_000.0f, 100.0f, 8, 2.0f, 20.0f, 0.5f);
|
||||
var large = ErosionFrequencyBands.Select(1_000.0f, 100.0f, 8, 2.0f, 20.0f, 0.5f);
|
||||
Assert.Equal(small, large);
|
||||
Assert.Equal(3, small.ActiveOctaves);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TransitionPartiallyWeightsLastResolvableOctave()
|
||||
{
|
||||
var selection = ErosionFrequencyBands.Select(1_000.0f, 50.0f, 8, 2.0f, 1.0f, 0.5f);
|
||||
Assert.Equal(4, selection.ActiveOctaves);
|
||||
Assert.InRange(selection.FinalOctaveWeight, 0.0f, 1.0f);
|
||||
Assert.True(selection.UnresolvedHeightBound > 0.0f);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BandedFilterPreservesLowOctaveResultExactly()
|
||||
{
|
||||
var p = AdvancedErosionParameters.Default;
|
||||
var band = new ErosionBandSelection(2, 1.0f, 0.0f, 0.0f);
|
||||
var banded = AdvancedErosionFilter.Sample(new float2(0.42f, 0.31f), new float3(0.5f, 0.1f, -0.2f), 0.0f, p, band);
|
||||
var truncated = AdvancedErosionFilter.Sample(new float2(0.42f, 0.31f), new float3(0.5f, 0.1f, -0.2f), 0.0f, p with { Octaves = 2 });
|
||||
Assert.Equal(truncated, banded);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\GameCult.Geometry\GameCult.Geometry.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageReference Include="xunit.v3" Version="3.2.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,83 @@
|
|||
using Xunit;
|
||||
|
||||
using CultMath;
|
||||
using GameCult.Geometry;
|
||||
|
||||
namespace GameCult.Geometry.Planetary.Tests;
|
||||
|
||||
public sealed class PlanetaryFieldTests
|
||||
{
|
||||
private static readonly PlanetaryFieldDefinition Definition = new(
|
||||
0x7f59_6d11_02ab_9c31UL,
|
||||
8.4f,
|
||||
42,
|
||||
AdvancedErosionParameters.Default with { Scale = 0.63f, Strength = 0.12f, Octaves = 7 });
|
||||
|
||||
[Fact]
|
||||
public void SampleIsDeterministicScaleAwareAndTangent()
|
||||
{
|
||||
var direction = math.normalize(new float3(0.31f, -0.72f, 0.61f));
|
||||
var source = new FixtureField();
|
||||
var coarse = PlanetaryField.Sample(Definition, direction, source.Sample(direction), PlanetaryQueryScale.AtFootprint(0.1f));
|
||||
var fine = PlanetaryField.Sample(Definition, direction, source.Sample(direction), PlanetaryQueryScale.AtFootprint(0.01f));
|
||||
var repeated = PlanetaryField.Sample(Definition, direction, source.Sample(direction), PlanetaryQueryScale.AtFootprint(0.01f));
|
||||
|
||||
Assert.Equal(fine, repeated);
|
||||
Assert.Equal(Definition.FieldVersion, fine.FieldVersion);
|
||||
Assert.True(fine.UnresolvedHeightBound <= coarse.UnresolvedHeightBound);
|
||||
Assert.True(fine.FinestResolvedWavelength <= coarse.FinestResolvedWavelength);
|
||||
Assert.InRange(MathF.Abs(math.dot(fine.UnitDirection, fine.TangentGradient)), 0, 2.0e-5f);
|
||||
Assert.Equal(1, math.length(fine.SurfaceNormal), 5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BatchAndDoublePositionUseThePointContract()
|
||||
{
|
||||
var directions = new[]
|
||||
{
|
||||
math.normalize(new float3(1, 2, 3)),
|
||||
math.normalize(new float3(-2, 0.5f, 1)),
|
||||
math.normalize(new float3(0.1f, -0.2f, -1)),
|
||||
};
|
||||
var output = new PlanetarySurfaceSample[directions.Length];
|
||||
var source = new FixtureField();
|
||||
var scale = PlanetaryQueryScale.AtFootprint(0.05f);
|
||||
PlanetaryField.SampleBatch(Definition, directions, scale, source, output);
|
||||
|
||||
for (var i = 0; i < directions.Length; i++)
|
||||
{
|
||||
var normalized = math.normalize(directions[i]);
|
||||
var expected = PlanetaryField.Sample(Definition, normalized, source.Sample(normalized), scale);
|
||||
Assert.InRange(MathF.Abs(expected.RadialDisplacement - output[i].RadialDisplacement), 0, 1.0e-6f);
|
||||
Assert.InRange(math.distance(expected.TangentGradient, output[i].TangentGradient), 0, 1.0e-6f);
|
||||
}
|
||||
|
||||
var d = directions[0];
|
||||
var position = new double3(d.x * 8.0e11, d.y * 8.0e11, d.z * 8.0e11);
|
||||
var positioned = PlanetaryField.SamplePosition(Definition, position, scale, source);
|
||||
Assert.InRange(math.distance(positioned.UnitDirection, d), 0, 2.0e-6f);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FieldIdentityChangesWithEveryLoadBearingInput()
|
||||
{
|
||||
var erosion = AdvancedErosionParameters.Default;
|
||||
var baseline = PlanetaryFieldDefinition.Create(3, 8.4f, 7, erosion);
|
||||
Assert.Equal(baseline.FieldVersion, PlanetaryFieldDefinition.Create(3, 8.4f, 7, erosion).FieldVersion);
|
||||
Assert.NotEqual(baseline.FieldVersion, PlanetaryFieldDefinition.Create(4, 8.4f, 7, erosion).FieldVersion);
|
||||
Assert.NotEqual(baseline.FieldVersion, PlanetaryFieldDefinition.Create(3, 8.5f, 7, erosion).FieldVersion);
|
||||
Assert.NotEqual(baseline.FieldVersion, PlanetaryFieldDefinition.Create(3, 8.4f, 8, erosion).FieldVersion);
|
||||
Assert.NotEqual(baseline.FieldVersion, PlanetaryFieldDefinition.Create(3, 8.4f, 7, erosion with { Gain = 0.49f }).FieldVersion);
|
||||
}
|
||||
|
||||
private readonly struct FixtureField : IPlanetaryBaseField
|
||||
{
|
||||
public PlanetaryBaseFieldSample Sample(float3 direction)
|
||||
{
|
||||
var value = direction.x * 0.6f + direction.y * direction.z * 0.3f;
|
||||
var fieldGradient = new float3(0.6f, direction.z * 0.3f, direction.y * 0.3f);
|
||||
fieldGradient -= direction * math.dot(fieldGradient, direction);
|
||||
return new(value * 0.1f, fieldGradient * 0.1f, value, fieldGradient, math.clamp(value, -1, 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
using Xunit;
|
||||
|
||||
using CultMath;
|
||||
using GameCult.Geometry;
|
||||
|
||||
namespace GameCult.Geometry.Planetary.Tests;
|
||||
|
||||
public sealed class PlanetaryGpuPageTests
|
||||
{
|
||||
[Fact]
|
||||
public void UploadContentUsesCanonicalDirectionsSpacingAndMetadata()
|
||||
{
|
||||
var layout = new PlanetaryPageLayout(new(PlanetaryCubeFace.NegativeY, 3, 4, 2), 9, 2);
|
||||
var content = PlanetaryGpuPageBuilder.BuildContent(layout, 8.4f);
|
||||
Assert.Equal(layout.StorageSize * layout.StorageSize, content.Inputs.Length);
|
||||
Assert.True(content.ParentSampleSpacing > content.SampleSpacing);
|
||||
var first = content.Inputs[0];
|
||||
Assert.Equal(PlanetaryPageSampling.Direction(layout, 0, 0), first.DirectionRadius.xyz);
|
||||
Assert.Equal(8.4f, first.DirectionRadius.w);
|
||||
Assert.Equal(content.SampleSpacing, first.Sampling.x);
|
||||
Assert.Equal(content.ParentSampleSpacing, first.Sampling.y);
|
||||
|
||||
var metadata = PlanetaryGpuPageBuilder.Metadata(content, 37, 0.25f);
|
||||
Assert.Equal(new float4((int)layout.Tile.Face, layout.Tile.Level, layout.Tile.X, layout.Tile.Y), metadata.Address);
|
||||
Assert.Equal(new float4(37, layout.StorageSize, layout.InteriorSize, layout.BorderSize), metadata.Layout);
|
||||
Assert.Equal(0.25f, metadata.State.y);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
using System.IO;
|
||||
using Xunit;
|
||||
|
||||
using CultMath;
|
||||
using GameCult.Geometry;
|
||||
|
||||
namespace GameCult.Geometry.Planetary.Tests;
|
||||
|
||||
public sealed class PlanetaryMapTileEncodingTests
|
||||
{
|
||||
[Fact]
|
||||
public void BinaryTileRoundTripsIdentityValidityAndSurfaceEvidence()
|
||||
{
|
||||
var field = PlanetaryFieldDefinition.Create(5, 10, 3, AdvancedErosionParameters.Default);
|
||||
var layout = new PlanetaryMapTileLayout(new(PlanetaryProjectionKind.EqualEarth), 1, 1, 0, 5, 1);
|
||||
var key = new PlanetaryMapTileKey(field.FieldVersion, 2, 17, layout, PlanetaryQueryScale.AtFootprint(0.25f, 0.1f));
|
||||
var tile = PlanetaryMapTileBaker.Bake(field, key, new FlatField());
|
||||
using var bytes = new MemoryStream();
|
||||
PlanetaryMapTileEncoding.Write(bytes, tile, leaveOpen: true);
|
||||
bytes.Position = 0;
|
||||
var decoded = PlanetaryMapTileEncoding.Read(bytes);
|
||||
|
||||
Assert.Equal(tile.Key, decoded.Key);
|
||||
Assert.Equal(tile.Validity, decoded.Validity);
|
||||
for (var i = 0; i < tile.Samples.Length; i++)
|
||||
if (tile.Validity[i]) Assert.Equal(tile.Samples[i], decoded.Samples[i]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DecoderRejectsForeignMagicAndVersions()
|
||||
{
|
||||
Assert.Throws<InvalidDataException>(() => PlanetaryMapTileEncoding.Read(new MemoryStream(new byte[] { 1, 2, 3, 4, 1, 0, 0, 0 })));
|
||||
var bytes = new byte[] { (byte)'C', (byte)'M', (byte)'P', (byte)'T', 99, 0, 0, 0 };
|
||||
Assert.Throws<InvalidDataException>(() => PlanetaryMapTileEncoding.Read(new MemoryStream(bytes)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DecoderRejectsTrailingBytes()
|
||||
{
|
||||
var field = PlanetaryFieldDefinition.Create(2, 1, 0, AdvancedErosionParameters.Default);
|
||||
var layout = new PlanetaryMapTileLayout(new(PlanetaryProjectionKind.Equirectangular), 0, 0, 0, 2, 0);
|
||||
var key = new PlanetaryMapTileKey(field.FieldVersion, 1, 0, layout, PlanetaryQueryScale.AtFootprint(1));
|
||||
var tile = PlanetaryMapTileBaker.Bake(field, key, new FlatField());
|
||||
using var bytes = new MemoryStream();
|
||||
PlanetaryMapTileEncoding.Write(bytes, tile, leaveOpen: true);
|
||||
bytes.WriteByte(0xff);
|
||||
bytes.Position = 0;
|
||||
Assert.Throws<InvalidDataException>(() => PlanetaryMapTileEncoding.Read(bytes));
|
||||
}
|
||||
|
||||
private readonly struct FlatField : IPlanetaryBaseField
|
||||
{
|
||||
public PlanetaryBaseFieldSample Sample(float3 direction) => new(0, float3.zero, 0, float3.zero, 0);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
using Xunit;
|
||||
|
||||
using CultMath;
|
||||
using GameCult.Geometry;
|
||||
|
||||
namespace GameCult.Geometry.Planetary.Tests;
|
||||
|
||||
public sealed class PlanetaryPageTests
|
||||
{
|
||||
[Fact]
|
||||
public void BilinearSamplingAndSummaryUseOnePageContract()
|
||||
{
|
||||
var layout = new PlanetaryPageLayout(new(PlanetaryCubeFace.PositiveZ, 0, 0, 0), 3, 1);
|
||||
var samples = new PlanetaryPageSample[layout.StorageSize * layout.StorageSize];
|
||||
for (var y = 0; y < layout.StorageSize; y++)
|
||||
for (var x = 0; x < layout.StorageSize; x++)
|
||||
samples[y * layout.StorageSize + x] = new(x + y * 10, new float3(x, y, 0), x / 10.0f, y / 10.0f);
|
||||
|
||||
var sample = PlanetaryPageSampling.Bilinear(samples, layout, new float2(0.25f, 0.75f));
|
||||
Assert.Equal(26.5f, sample.Height, 4);
|
||||
Assert.Equal(new float3(1.5f, 2.5f, 0), sample.TangentGradient);
|
||||
var summary = PlanetaryPageSampling.Summarize(samples, layout, 0.125f);
|
||||
Assert.Equal(0, summary.MinimumHeight);
|
||||
Assert.Equal(44, summary.MaximumHeight);
|
||||
Assert.Equal(0.125f, summary.UnresolvedHeightBound);
|
||||
Assert.True(summary.MaximumSlope > 0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ChildPageStoresOnlyResidualAndComposesBackToDirectField()
|
||||
{
|
||||
var field = new PlanetaryFieldDefinition(7, 8.4f, 3, AdvancedErosionParameters.Default with
|
||||
{
|
||||
Scale = 0.63f,
|
||||
Strength = 0.12f,
|
||||
Octaves = 7,
|
||||
});
|
||||
var source = new FixtureField();
|
||||
var root = PlanetaryPageBaker.Bake(field, new(new(PlanetaryCubeFace.PositiveZ, 0, 0, 0), 17, 2), source);
|
||||
var child = PlanetaryPageBaker.Bake(field, new(new(PlanetaryCubeFace.PositiveZ, 1, 1, 0), 17, 2), source);
|
||||
var direction = PlanetaryPageSampling.DirectionAtLocal(child.Layout, 0.5, 0.5);
|
||||
var pages = new[] { new PlanetaryResidentPage(root, 1), new PlanetaryResidentPage(child, 1) };
|
||||
Assert.True(PlanetaryPageSetSampling.TrySample(pages, direction, out var composed));
|
||||
var spacing = PlanetaryPageSampling.NominalAngularTexelSize(child.Layout) * field.Radius;
|
||||
var direct = PlanetaryField.Sample(field, direction, source.Sample(direction), PlanetaryQueryScale.AtFootprint(spacing));
|
||||
Assert.InRange(MathF.Abs(composed.Height - direct.RadialDisplacement), 0, 2.0e-5f);
|
||||
Assert.InRange(math.distance(composed.TangentGradient, direct.TangentGradient), 0, 3.0e-5f);
|
||||
}
|
||||
|
||||
private readonly struct FixtureField : IPlanetaryBaseField
|
||||
{
|
||||
public PlanetaryBaseFieldSample Sample(float3 direction)
|
||||
{
|
||||
var value = direction.x * 0.4f - direction.y * 0.2f + direction.z * 0.5f;
|
||||
var gradient = new float3(0.4f, -0.2f, 0.5f);
|
||||
gradient -= direction * math.dot(gradient, direction);
|
||||
return new(value * 0.08f, gradient * 0.08f, value, gradient, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
using Xunit;
|
||||
|
||||
using CultMath;
|
||||
using GameCult.Geometry;
|
||||
|
||||
namespace GameCult.Geometry.Planetary.Tests;
|
||||
|
||||
public sealed class PlanetaryPatchTests
|
||||
{
|
||||
[Fact]
|
||||
public void CubeSpherePatchMeshUsesCanonicalTopology()
|
||||
{
|
||||
var meshes = PlanetaryPatch.CreateCubeSphere(8);
|
||||
Assert.Equal(6, meshes.Length);
|
||||
foreach (var mesh in meshes)
|
||||
{
|
||||
Assert.Equal(81, mesh.Vertices.Length);
|
||||
Assert.Equal(8 * 8 * 6, mesh.Indices.Length);
|
||||
Assert.All(mesh.Vertices, vertex => Assert.Equal(1, math.length(vertex.UnitDirection), 5));
|
||||
foreach (var index in mesh.Indices) Assert.InRange(index, 0, mesh.Vertices.Length - 1);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NeighborFaceCornersAreSingleValued()
|
||||
{
|
||||
var directions = PlanetaryPatch.CreateCubeSphere(1)
|
||||
.SelectMany(mesh => mesh.Vertices.Select(vertex => vertex.UnitDirection))
|
||||
.ToArray();
|
||||
var groups = directions.GroupBy(direction => new
|
||||
{
|
||||
X = Math.Sign(direction.x),
|
||||
Y = Math.Sign(direction.y),
|
||||
Z = Math.Sign(direction.z),
|
||||
});
|
||||
Assert.Equal(8, groups.Count());
|
||||
Assert.All(groups, group =>
|
||||
{
|
||||
var first = group.First();
|
||||
Assert.All(group, direction => Assert.InRange(math.distance(first, direction), 0, 1.0e-6f));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
using Xunit;
|
||||
|
||||
using CultMath;
|
||||
using GameCult.Geometry;
|
||||
|
||||
namespace GameCult.Geometry.Planetary.Tests;
|
||||
|
||||
public sealed class PlanetaryProjectionTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(PlanetaryProjectionKind.Equirectangular)]
|
||||
[InlineData(PlanetaryProjectionKind.WebMercator)]
|
||||
[InlineData(PlanetaryProjectionKind.EqualEarth)]
|
||||
[InlineData(PlanetaryProjectionKind.Orthographic)]
|
||||
[InlineData(PlanetaryProjectionKind.AzimuthalEquidistant)]
|
||||
[InlineData(PlanetaryProjectionKind.AzimuthalEqualArea)]
|
||||
[InlineData(PlanetaryProjectionKind.LocalTangent)]
|
||||
public void ProjectionRoundTripsVisibleDirections(PlanetaryProjectionKind kind)
|
||||
{
|
||||
var parameters = new PlanetaryProjectionParameters(kind, 0.31, -0.22);
|
||||
foreach (var longitude in new[] { -0.4, -0.1, 0.2, 0.65 })
|
||||
foreach (var latitude in new[] { -0.6, -0.15, 0.3, 0.7 })
|
||||
{
|
||||
var direction = Direction(longitude + parameters.CenterLongitude, latitude);
|
||||
if (!PlanetaryProjection.TryForward(direction, parameters, out var map)) continue;
|
||||
Assert.True(PlanetaryProjection.TryInverse(map, parameters, out var roundTrip));
|
||||
Assert.InRange(math.distance(direction, roundTrip), 0, 3.0e-5f);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CubeAtlasRoundTripsEveryFaceInterior()
|
||||
{
|
||||
var projection = new PlanetaryProjectionParameters(PlanetaryProjectionKind.CubeAtlas);
|
||||
foreach (var face in Enum.GetValues<PlanetaryCubeFace>())
|
||||
{
|
||||
var direction = PlanetaryTopology.Direction(new(face, 0.23, -0.41));
|
||||
Assert.True(PlanetaryProjection.TryForward(direction, projection, out var map));
|
||||
Assert.True(PlanetaryProjection.TryInverse(map, projection, out var roundTrip));
|
||||
Assert.InRange(math.distance(direction, roundTrip), 0, 2.0e-6f);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MercatorRejectsPolarSingularity()
|
||||
{
|
||||
var northPole = new float3(0, 0, 1);
|
||||
Assert.False(PlanetaryProjection.TryForward(northPole, new(PlanetaryProjectionKind.WebMercator), out _));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MapTileBakerCarriesFieldIdentityAndProjectionValidity()
|
||||
{
|
||||
var field = new PlanetaryFieldDefinition(99, 10, 0, AdvancedErosionParameters.Default);
|
||||
var layout = new PlanetaryMapTileLayout(
|
||||
new(PlanetaryProjectionKind.Orthographic), 0, 0, 0, 9, 1);
|
||||
var key = new PlanetaryMapTileKey(field.FieldVersion, 1, 7, layout, PlanetaryQueryScale.AtFootprint(0.1f));
|
||||
var tile = PlanetaryMapTileBaker.Bake(field, key, new FlatField());
|
||||
Assert.Equal(key, tile.Key);
|
||||
Assert.Equal(layout.StorageSize * layout.StorageSize, tile.Samples.Length);
|
||||
Assert.Contains(tile.Validity, valid => valid);
|
||||
Assert.Contains(tile.Validity, valid => !valid);
|
||||
Assert.All(tile.Samples.Where((_, index) => tile.Validity[index]), sample => Assert.Equal(field.FieldVersion, sample.FieldVersion));
|
||||
}
|
||||
|
||||
private static float3 Direction(double longitude, double latitude)
|
||||
{
|
||||
var cos = Math.Cos(latitude);
|
||||
return new((float)(cos * Math.Cos(longitude)), (float)(cos * Math.Sin(longitude)), (float)Math.Sin(latitude));
|
||||
}
|
||||
|
||||
private readonly struct FlatField : IPlanetaryBaseField
|
||||
{
|
||||
public PlanetaryBaseFieldSample Sample(float3 unitDirection) => new(0, float3.zero, 0, float3.zero, 0);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
using Xunit;
|
||||
|
||||
using CultMath;
|
||||
using GameCult.Geometry;
|
||||
|
||||
namespace GameCult.Geometry.Planetary.Tests;
|
||||
|
||||
public sealed class PlanetaryQueryTests
|
||||
{
|
||||
private static readonly PlanetaryFieldDefinition Field = new(12, 10, 0, AdvancedErosionParameters.Default with { Strength = 0, Octaves = 0 });
|
||||
private static readonly PlanetaryQueryScale Scale = PlanetaryQueryScale.AtFootprint(0.1f, 0);
|
||||
|
||||
[Fact]
|
||||
public void RayIntersectionAndClearanceUseTheCpuSurfaceContract()
|
||||
{
|
||||
var source = new BulgedSphere();
|
||||
Assert.True(PlanetaryQueries.TryIntersectRay(
|
||||
Field, new double3(0, 0, 20), new double3(0, 0, -1), 30,
|
||||
Scale, new PlanetaryRadialBounds(-1, 1), source, out var hit));
|
||||
Assert.InRange(hit.Distance, 8.99999, 9.00001);
|
||||
Assert.InRange(hit.PlanetLocalPosition.z, 10.99999, 11.00001);
|
||||
var clearance = PlanetaryQueries.SegmentClearance(
|
||||
Field, new double3(-2, 0, 12), new double3(2, 0, 12), 0.1, Scale, source);
|
||||
Assert.True(clearance > 0.8);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GreatCircleAndRegionQueriesReturnVersionedSamples()
|
||||
{
|
||||
var source = new BulgedSphere();
|
||||
var path = PlanetaryQueries.SampleGreatCircle(Field, new float3(1, 0, 0), new float3(0, 1, 0), 8, Scale, source);
|
||||
Assert.Equal(9, path.Length);
|
||||
Assert.Equal(MathF.PI * 5, path[^1].Distance, 4);
|
||||
Assert.All(path, point => Assert.Equal(Field.FieldVersion, point.Surface.FieldVersion));
|
||||
var summary = PlanetaryQueries.Summarize(Field, path.Select(point => point.Surface.UnitDirection).ToArray(), Scale, source);
|
||||
Assert.True(summary.MaximumDisplacement >= summary.MinimumDisplacement);
|
||||
Assert.True(summary.MaximumSlope >= 0);
|
||||
}
|
||||
|
||||
private readonly struct BulgedSphere : IPlanetaryBaseField
|
||||
{
|
||||
public PlanetaryBaseFieldSample Sample(float3 direction)
|
||||
{
|
||||
var displacement = direction.z;
|
||||
var gradient = new float3(0, 0, 1) - direction * direction.z;
|
||||
return new(displacement, gradient, 0, float3.zero, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
using Xunit;
|
||||
|
||||
using CultMath;
|
||||
using GameCult.Geometry;
|
||||
|
||||
namespace GameCult.Geometry.Planetary.Tests;
|
||||
|
||||
public sealed class PlanetaryRadialRefinementTests
|
||||
{
|
||||
[Fact]
|
||||
public void FourBoundedStepsConvergeFromTheRasterCaptureShell()
|
||||
{
|
||||
var center=new float3(0.3f,-0.2f,0.7f);
|
||||
var radial=math.normalize(new float3(0.2f,-0.4f,0.9f));
|
||||
var targetRadius=3.85f;
|
||||
var target=center+radial*targetRadius;
|
||||
var origin=target+math.normalize(radial+new float3(0.12f,-0.05f,0.02f))*6.0f;
|
||||
var position=center+radial*(targetRadius+0.30f);
|
||||
var ray=math.normalize(position-origin);
|
||||
for(var step=0;step<4;step++)
|
||||
position=PlanetaryRadialRefinement.Step(position,ray,center,targetRadius);
|
||||
Assert.InRange(math.abs(math.length(position-center)-targetRadius),0.0f,1.0e-4f);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StepClampsWeakDerivativeCorrections()
|
||||
{
|
||||
var position=new float3(0,0,4.1f);
|
||||
var result=PlanetaryRadialRefinement.Step(position,new float3(1,0,0),float3.zero,3.8f);
|
||||
Assert.InRange(math.length(result-position),0.07999f,0.08001f);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
using Xunit;
|
||||
|
||||
using CultMath;
|
||||
using GameCult.Geometry;
|
||||
|
||||
namespace GameCult.Geometry.Planetary.Tests;
|
||||
|
||||
public sealed class PlanetaryResidencyTests
|
||||
{
|
||||
private static readonly PlanetaryFieldDefinition Field = new(
|
||||
1, 8.4f, 0, AdvancedErosionParameters.Default with { Scale = 0.63f, Strength = 0.12f, Octaves = 7 });
|
||||
private static readonly PlanetaryLodParameters Lod = new(5, 17, 2, 720, 8.4f / 65536);
|
||||
|
||||
[Fact]
|
||||
public void SelectorReturnsSixRootsAndOneAncestorChain()
|
||||
{
|
||||
var direction = math.normalize(new float3(-0.2f, 0.91f, 0.37f));
|
||||
var selected = PlanetaryLodSelector.SelectAncestorChain(Field, direction, 8.45f, Lod);
|
||||
Assert.Equal(6, selected.Count(tile => tile.Level == 0));
|
||||
var descendants = selected.Where(tile => tile.Level > 0).OrderBy(tile => tile.Level).ToArray();
|
||||
for (var i = 1; i < descendants.Length; i++) Assert.Equal(descendants[i - 1], descendants[i].Parent());
|
||||
if (descendants.Length > 0) Assert.Equal(PlanetaryTopology.TileAt(direction, descendants[^1].Level), descendants[^1]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResidencyFadesResidualsWithoutChangingContentDuringPresentation()
|
||||
{
|
||||
var root = new PlanetaryTileAddress(PlanetaryCubeFace.PositiveX, 0, 0, 0);
|
||||
var child = root.Child(1, 0);
|
||||
var residency = new PlanetaryResidualResidency();
|
||||
var arrival = residency.Update(new[] { root, child }, 0, 1);
|
||||
var midpoint = residency.Update(new[] { root, child }, 0.5f, 1);
|
||||
var settled = residency.Update(new[] { root, child }, 1, 1);
|
||||
Assert.Equal(arrival.ContentVersion, midpoint.ContentVersion);
|
||||
Assert.Equal(midpoint.ContentVersion, settled.ContentVersion);
|
||||
Assert.Equal(0, arrival.Tiles.Single(tile => tile.Tile == child).Blend);
|
||||
Assert.Equal(0.5f, midpoint.Tiles.Single(tile => tile.Tile == child).Blend);
|
||||
Assert.Equal(1, settled.Tiles.Single(tile => tile.Tile == child).Blend);
|
||||
|
||||
var leaving = residency.Update(new[] { root }, 1.25f, 1);
|
||||
Assert.True(leaving.Tiles.Single(tile => tile.Tile == child).Departing);
|
||||
Assert.Equal(1, leaving.Tiles.Single(tile => tile.Tile == child).Blend);
|
||||
var gone = residency.Update(new[] { root }, 2.25f, 1);
|
||||
Assert.DoesNotContain(gone.Tiles, tile => tile.Tile == child);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReversingAnArrivalDepartsFromItsCurrentWeightWithoutJumping()
|
||||
{
|
||||
var root = new PlanetaryTileAddress(PlanetaryCubeFace.PositiveX, 0, 0, 0);
|
||||
var child = root.Child(0, 1);
|
||||
var residency = new PlanetaryResidualResidency();
|
||||
_ = residency.Update(new[] { root, child }, 0, 1);
|
||||
var arriving = residency.Update(new[] { root, child }, 0.25f, 1);
|
||||
var departing = residency.Update(new[] { root }, 0.25f, 1);
|
||||
Assert.Equal(0.25f, arriving.Tiles.Single(tile => tile.Tile == child).Blend);
|
||||
Assert.Equal(0.25f, departing.Tiles.Single(tile => tile.Tile == child).Blend);
|
||||
Assert.True(departing.Tiles.Single(tile => tile.Tile == child).Departing);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
using Xunit;
|
||||
|
||||
using CultMath;
|
||||
using GameCult.Geometry;
|
||||
|
||||
namespace GameCult.Geometry.Planetary.Tests;
|
||||
|
||||
public sealed class PlanetaryTopologyTests
|
||||
{
|
||||
[Fact]
|
||||
public void FaceDirectionRoundTripsAcrossEveryFace()
|
||||
{
|
||||
foreach (var face in Enum.GetValues<PlanetaryCubeFace>())
|
||||
foreach (var u in new[] { -0.9, -0.25, 0.0, 0.4, 0.9 })
|
||||
foreach (var v in new[] { -0.85, -0.1, 0.3, 0.88 })
|
||||
{
|
||||
var direction = PlanetaryTopology.Direction(new(face, u, v));
|
||||
var roundTrip = PlanetaryTopology.FaceCoordinate(direction);
|
||||
Assert.Equal(face, roundTrip.Face);
|
||||
Assert.Equal(u, roundTrip.U, 5);
|
||||
Assert.Equal(v, roundTrip.V, 5);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SiblingPagesShareBitStableBoundaryDirections()
|
||||
{
|
||||
var left = new PlanetaryPageLayout(new(PlanetaryCubeFace.PositiveZ, 1, 0, 0), 17, 2);
|
||||
var right = new PlanetaryPageLayout(new(PlanetaryCubeFace.PositiveZ, 1, 1, 0), 17, 2);
|
||||
for (var y = 0; y < left.InteriorSize; y++)
|
||||
{
|
||||
var a = PlanetaryPageSampling.Direction(left, left.BorderSize + left.InteriorSize - 1, left.BorderSize + y);
|
||||
var b = PlanetaryPageSampling.Direction(right, right.BorderSize, right.BorderSize + y);
|
||||
Assert.Equal(a, b);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DirectionSelectsContainingTileAndLocalCoordinate()
|
||||
{
|
||||
var expected = new PlanetaryTileAddress(PlanetaryCubeFace.NegativeY, 5, 19, 7);
|
||||
var direction = PlanetaryPageSampling.DirectionAtLocal(new(expected, 17, 2), 0.37, 0.61);
|
||||
Assert.Equal(expected, PlanetaryTopology.TileAt(direction, expected.Level));
|
||||
Assert.True(PlanetaryTopology.TryLocalCoordinate(direction, expected, out var local));
|
||||
Assert.Equal(0.37f, local.x, 4);
|
||||
Assert.Equal(0.61f, local.y, 4);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BorderSamplesDoNotRequireNeighborResidency()
|
||||
{
|
||||
var page = new PlanetaryPageLayout(new(PlanetaryCubeFace.PositiveX, 0, 0, 0), 17, 3);
|
||||
var edge = PlanetaryPageSampling.Direction(page, page.BorderSize + page.InteriorSize - 1, page.BorderSize + 8);
|
||||
var border = PlanetaryPageSampling.Direction(page, page.StorageSize - 1, page.BorderSize + 8);
|
||||
Assert.NotEqual(edge, border);
|
||||
Assert.Equal(1, math.length(border), 5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormalizedCubeBaselineSharesFaceEdges()
|
||||
{
|
||||
var a = PlanetaryTopology.NormalizedCubeDirection(new(PlanetaryCubeFace.PositiveZ, 1, -0.4));
|
||||
var b = PlanetaryTopology.NormalizedCubeDirection(new(PlanetaryCubeFace.PositiveX, -1, -0.4));
|
||||
Assert.InRange(math.distance(a, b), 0, 1.0e-6f);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
using Xunit;
|
||||
|
||||
using CultMath;
|
||||
using GameCult.Geometry;
|
||||
|
||||
namespace GameCult.Geometry.Planetary.Tests;
|
||||
|
||||
public sealed class SphericalErosionTests
|
||||
{
|
||||
[Fact]
|
||||
public void SampleIsDeterministicAndScaleInvariant()
|
||||
{
|
||||
var parameters = SphericalErosionParameters.Default with { Seed = 42.0f };
|
||||
var a = SphericalErosion.Sample(new float3(0.31f, -0.74f, 0.59f), 0.2f, new float3(0.4f, -0.1f, 0.2f), parameters);
|
||||
var repeated = SphericalErosion.Sample(new float3(0.31f, -0.74f, 0.59f), 0.2f, new float3(0.4f, -0.1f, 0.2f), parameters);
|
||||
var b = SphericalErosion.Sample(new float3(3.1f, -7.4f, 5.9f), 0.2f, new float3(0.4f, -0.1f, 0.2f), parameters);
|
||||
|
||||
Assert.Equal(a, repeated);
|
||||
Assert.InRange(math.abs(a.HeightOffset-b.HeightOffset),0.0f,1.0e-6f);
|
||||
Assert.InRange(math.abs(a.Ridge-b.Ridge),0.0f,1.0e-6f);
|
||||
Assert.InRange(math.abs(a.Gully-b.Gully),0.0f,5.0e-5f);
|
||||
Assert.InRange(math.length(a.Gradient-b.Gradient),0.0f,5.0e-6f);
|
||||
Assert.InRange(a.HeightOffset, -0.08f, 0.08f);
|
||||
Assert.InRange(a.Ridge, 0.0f, 1.0f);
|
||||
Assert.InRange(a.Gully, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NearbySamplesRemainContinuousAcrossAChartBoundary()
|
||||
{
|
||||
var parameters = SphericalErosionParameters.Default;
|
||||
var left = SphericalErosion.Sample(math.normalize(new float3(1.0f, 0.99999f, 0.2f)), 0.1f, new float3(0.2f, -0.1f, 0.05f), parameters);
|
||||
var right = SphericalErosion.Sample(math.normalize(new float3(0.99999f, 1.0f, 0.2f)), 0.1f, new float3(0.2f, -0.1f, 0.05f), parameters);
|
||||
|
||||
Assert.InRange(math.abs(left.HeightOffset - right.HeightOffset), 0.0f, 0.0002f);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace GameCult.Geometry.Tests;
|
||||
|
||||
public sealed class GeometryShaderOwnershipTests
|
||||
{
|
||||
[Test]
|
||||
public void GeometryUmbrellaOwnsTheCompleteShaderSurface()
|
||||
{
|
||||
var shaders = Path.Combine(GetRepositoryRoot(), "src", "GameCult.Geometry", "Shaders");
|
||||
var umbrella = File.ReadAllText(Path.Combine(shaders, "GameCult.Geometry.hlsl"));
|
||||
var erosion = File.ReadAllText(Path.Combine(shaders, "AdvancedErosionFilter.hlsl"));
|
||||
var planetary = File.ReadAllText(Path.Combine(shaders, "Planetary.hlsl"));
|
||||
var spherical = File.ReadAllText(Path.Combine(shaders, "SphericalErosion.hlsl"));
|
||||
var refinement = File.ReadAllText(Path.Combine(shaders, "PlanetaryRadialRefinement.hlsl"));
|
||||
|
||||
Assert.That(umbrella, Does.Contain("#define GAMECULT_GEOMETRY_CULTMATH_INCLUDE \"CultMath.hlsl\"")
|
||||
.And.Contain("#include GAMECULT_GEOMETRY_CULTMATH_INCLUDE")
|
||||
.And.Contain("#include \"AdvancedErosionFilter.hlsl\"")
|
||||
.And.Contain("#include \"Planetary.hlsl\"")
|
||||
.And.Contain("#include \"SphericalErosion.hlsl\"")
|
||||
.And.Contain("#include \"PlanetaryRadialRefinement.hlsl\""));
|
||||
Assert.That(erosion, Does.Contain("gamecult_geometry_advanced_erosion_filter")
|
||||
.And.Contain("GameCultGeometryAdvancedErosionParameters"));
|
||||
Assert.That(planetary, Does.Contain("gamecult_geometry_planetary_face_direction")
|
||||
.And.Contain("gamecult_geometry_planetary_field_sample")
|
||||
.And.Contain("GameCultGeometryPlanetarySurfaceSample"));
|
||||
Assert.That(spherical, Does.Contain("gamecult_geometry_spherical_erosion")
|
||||
.And.Contain("GameCultGeometrySphericalErosionParameters"));
|
||||
Assert.That(refinement, Does.Contain("gamecult_geometry_planetary_radial_refinement_step"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GeometryShadersDoNotPublishCultMathGeometrySymbols()
|
||||
{
|
||||
var shaders = Path.Combine(GetRepositoryRoot(), "src", "GameCult.Geometry", "Shaders");
|
||||
foreach (var path in Directory.GetFiles(shaders, "*.hlsl"))
|
||||
{
|
||||
var source = File.ReadAllText(path);
|
||||
Assert.That(source, Does.Not.Contain("cultmath_planetary_"), Path.GetFileName(path));
|
||||
Assert.That(source, Does.Not.Contain("cultmath_spherical_"), Path.GetFileName(path));
|
||||
Assert.That(source, Does.Not.Contain("cultmath_advanced_erosion_"), Path.GetFileName(path));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UnityPlanetaryViewerUsesTheGeometryUmbrellaAndOwnedSymbols()
|
||||
{
|
||||
var shader = File.ReadAllText(Path.Combine(
|
||||
GetRepositoryRoot(),
|
||||
"unity", "org.gamecult.geometry", "Samples~", "PlanetaryViewer", "Runtime",
|
||||
"GameCultGeometryPlanetaryViewer.shader"));
|
||||
|
||||
Assert.That(shader, Does.Contain(
|
||||
"#define GAMECULT_GEOMETRY_CULTMATH_INCLUDE \"Packages/org.gamecult.cultmath/shaders/CultMath.hlsl\"")
|
||||
.And.Contain("#include \"Packages/org.gamecult.geometry/Shaders/GameCult.Geometry.hlsl\"")
|
||||
.And.Contain("gamecult_geometry_planetary_field_sample")
|
||||
.And.Not.Contain("cultmath_planetary_")
|
||||
.And.Not.Contain("CultMathPlanetary"));
|
||||
}
|
||||
|
||||
private static string GetRepositoryRoot()
|
||||
{
|
||||
var directory = new DirectoryInfo(AppContext.BaseDirectory);
|
||||
while (directory is not null)
|
||||
{
|
||||
if (File.Exists(Path.Combine(directory.FullName, "CultLib.sln")))
|
||||
{
|
||||
return directory.FullName;
|
||||
}
|
||||
|
||||
directory = directory.Parent;
|
||||
}
|
||||
|
||||
throw new InvalidOperationException("Could not locate CultLib repository root.");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue