diff --git a/src/Aquarium.Engine.Fractal/Brushes/FractalHeightBrushCompiler.cs b/src/Aquarium.Engine.Fractal/Brushes/FractalHeightBrushCompiler.cs index 1e60b76..b23b4b6 100644 --- a/src/Aquarium.Engine.Fractal/Brushes/FractalHeightBrushCompiler.cs +++ b/src/Aquarium.Engine.Fractal/Brushes/FractalHeightBrushCompiler.cs @@ -68,4 +68,30 @@ public static class FractalHeightBrushCompiler return brushes; } + + public static AquariumHeightFieldBrush[] CompileSelectedTree(FractalOwnershipTree tree, IReadOnlyList selectedCut) + { + ArgumentNullException.ThrowIfNull(tree); + ArgumentNullException.ThrowIfNull(selectedCut); + + var selectedNodes = selectedCut.Select(cut => cut.NodeKey).ToHashSet(); + var domains = tree.Domains.ToDictionary(domain => domain.Key, domain => domain); + var brushes = new List(); + foreach (var node in tree.Nodes) + { + if (!selectedNodes.Contains(node.Key)) + { + continue; + } + + for (var claimIndex = node.FirstClaimIndex; claimIndex < node.FirstClaimIndex + node.ClaimCount; claimIndex++) + { + var claim = tree.Claims[claimIndex]; + domains.TryGetValue(claim.DomainKey, out var domain); + brushes.Add(Compile(claim, domain)); + } + } + + return brushes.ToArray(); + } } diff --git a/src/Aquarium.Zyphos/ZyphosFractalTerrain.cs b/src/Aquarium.Zyphos/ZyphosFractalTerrain.cs index db62564..77e49e7 100644 --- a/src/Aquarium.Zyphos/ZyphosFractalTerrain.cs +++ b/src/Aquarium.Zyphos/ZyphosFractalTerrain.cs @@ -14,8 +14,8 @@ public static class ZyphosFractalTerrain private static readonly Lazy PatchSource = new(() => File.ReadAllText(PatchPath)); private static readonly Lazy Tree = new(() => FractalDslCompiler.Compile(PatchSource.Value)); private static readonly Lazy Summaries = new(() => FractalSummaryBuilder.Build(Tree.Value)); - private static readonly Lazy SelectedCut = new(() => FractalSelectedCutBuilder.Build(Summaries.Value, _ => 8.0f, maxEstimatedCost: 8.0f)); - private static readonly Lazy Brushes = new(() => FractalHeightBrushCompiler.CompileTree(Tree.Value)); + private static readonly Lazy SelectedCut = new(() => FractalSelectedCutBuilder.Build(Summaries.Value, _ => 8.0f, maxEstimatedCost: 64.0f)); + private static readonly Lazy Brushes = new(() => FractalHeightBrushCompiler.CompileSelectedTree(Tree.Value, SelectedCut.Value)); public static string PatchPath => Path.Combine(AppContext.BaseDirectory, PatchRelativePath); diff --git a/tests/Aquarium.Engine.Fractal.Tests/FractalHeightBrushCompilerTests.cs b/tests/Aquarium.Engine.Fractal.Tests/FractalHeightBrushCompilerTests.cs index e7225bd..39c6574 100644 --- a/tests/Aquarium.Engine.Fractal.Tests/FractalHeightBrushCompilerTests.cs +++ b/tests/Aquarium.Engine.Fractal.Tests/FractalHeightBrushCompilerTests.cs @@ -2,6 +2,7 @@ using System.Numerics; using Aquarium.Engine.Fractal; using Aquarium.Engine.Fractal.Brushes; using Aquarium.Engine.Fractal.Grammar; +using Aquarium.Engine.Fractal.Lod; namespace Aquarium.Engine.Fractal.Tests; @@ -88,4 +89,26 @@ public sealed class FractalHeightBrushCompilerTests Assert.Equal(2.0f, brush.DomainX); Assert.Equal(1.0f, brush.DomainY); } + + [Fact] + public void SelectedTreeCompileOnlyLowersSelectedNodes() + { + const string source = """ + tile PositiveZ 0 0 0 demo/a + height basin 0 0 30 30 0 3 1 -0.18 7 basin + tile PositiveX 0 0 0 demo/b + height ridge 1 1 4 2 0 3 1 0.08 11 ridge + """; + var tree = FractalDslCompiler.Compile(source); + var selectedNode = Assert.Single(tree.Nodes, node => node.DomainKey.Value.EndsWith(":demo/b", StringComparison.Ordinal)); + var selectedCut = new[] + { + new AquariumSelectedCut(selectedNode.Key, Score: 1.0f, Fade: 1.0f, UsesSummary: true, RequestedChildren: false), + }; + + var brush = Assert.Single(FractalHeightBrushCompiler.CompileSelectedTree(tree, selectedCut)); + + Assert.Equal(1.0f, brush.Center.X); + Assert.Equal((float)CubeFace.PositiveX, brush.DomainFace); + } }