diff --git a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs index f71b6fd..6daded0 100644 --- a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs +++ b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs @@ -6,6 +6,10 @@ public static class BokushoBrushSimulation { public const float PageDensityScale = 65535.0f; public const float PagePatchTransferGain = 0.0032f; + public const float PagePatchTangentRadiusScale = 0.26f; + public const float PagePatchSweepRadiusScale = 0.5f; + public const float PagePatchNormalRadiusScale = 0.20f; + public const float PagePatchMinimumRadius = 0.001f; public static BokushoBrushSimulationResult Evaluate(AquariumBokushoBrushFrame source) { @@ -615,8 +619,7 @@ public static class BokushoBrushSimulation var sweepLength = sweep.Length(); var tangent = sweepLength > 0.000001f ? sweep / sweepLength : Vector2.UnitX; var normal = new Vector2(-tangent.Y, tangent.X); - var patchTangentRadius = MathF.Max(tangentRadius * 0.26f + sweepLength * 0.5f, 0.001f); - var patchNormalRadius = MathF.Max(normalRadius * 0.20f, 0.001f); + var (patchTangentRadius, patchNormalRadius) = PagePatchRadii(tangentRadius, normalRadius, sweepLength); var center = (previousTip + tip) * 0.5f; var extents = new Vector2( MathF.Abs(tangent.X) * patchTangentRadius + MathF.Abs(normal.X) * patchNormalRadius, @@ -738,6 +741,13 @@ public static class BokushoBrushSimulation public static float PagePatchContribution(float ink, float coverage) => Saturate(ink) * Saturate(coverage) * PagePatchTransferGain; + public static (float TangentRadius, float NormalRadius) PagePatchRadii(float tangentRadius, float normalRadius, float sweepLength) + { + return ( + MathF.Max(tangentRadius * PagePatchTangentRadiusScale + sweepLength * PagePatchSweepRadiusScale, PagePatchMinimumRadius), + MathF.Max(normalRadius * PagePatchNormalRadiusScale, PagePatchMinimumRadius)); + } + public static uint EncodePageDensityContribution(float contribution) { var density = -MathF.Log(MathF.Max(1.0f - Saturate(contribution), 0.000001f)); diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl index 7b57f24..9e02bc4 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl @@ -2,6 +2,10 @@ static const float BOKUSHO_PAGE_DENSITY_SCALE = 65535.0; static const float BOKUSHO_PAGE_PATCH_TRANSFER_GAIN = 0.0032; +static const float BOKUSHO_PAGE_PATCH_TANGENT_RADIUS_SCALE = 0.26; +static const float BOKUSHO_PAGE_PATCH_SWEEP_RADIUS_SCALE = 0.5; +static const float BOKUSHO_PAGE_PATCH_NORMAL_RADIUS_SCALE = 0.20; +static const float BOKUSHO_PAGE_PATCH_MINIMUM_RADIUS = 0.001; cbuffer BokushoBrushConstants : register(b4) { @@ -81,15 +85,6 @@ float CanvasPigment(float value) return 0.96 * (1.0 - exp(-max(value, 0.0) * 0.82)); } -uint PagePixelIndex(float2 world, out bool inside) -{ - float pageSize = max(pageView.w, 1.0); - float2 uv = (world - pageView.xy) / max(pageView.z, 0.001) * 0.5 + 0.5; - int2 pixel = int2(floor(uv * (pageSize - 1.0) + 0.5)); - inside = all(pixel >= int2(0, 0)) && all(pixel < int2((int)pageSize, (int)pageSize)); - return (uint)(pixel.y * (int)pageSize + pixel.x); -} - void DepositSweptPatch(float2 previousTip, float2 tip, float normalRadius, float tangentRadius, float pigment) { float ink = saturate(pigment); @@ -102,8 +97,8 @@ void DepositSweptPatch(float2 previousTip, float2 tip, float normalRadius, float float sweepLength = length(sweep); float2 tangent = sweepLength > 0.000001 ? sweep / sweepLength : float2(1.0, 0.0); float2 normal = float2(-tangent.y, tangent.x); - float patchTangentRadius = max(tangentRadius * 0.26 + sweepLength * 0.5, 0.001); - float patchNormalRadius = max(normalRadius * 0.20, 0.001); + float patchTangentRadius = max(tangentRadius * BOKUSHO_PAGE_PATCH_TANGENT_RADIUS_SCALE + sweepLength * BOKUSHO_PAGE_PATCH_SWEEP_RADIUS_SCALE, BOKUSHO_PAGE_PATCH_MINIMUM_RADIUS); + float patchNormalRadius = max(normalRadius * BOKUSHO_PAGE_PATCH_NORMAL_RADIUS_SCALE, BOKUSHO_PAGE_PATCH_MINIMUM_RADIUS); float2 center = (previousTip + tip) * 0.5; float2 extents = float2( abs(tangent.x) * patchTangentRadius + abs(normal.x) * patchNormalRadius, diff --git a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs index 024abfd..16312f0 100644 --- a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs +++ b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs @@ -209,6 +209,22 @@ public sealed class BokushoBrushSimulationTests Assert.InRange(MathF.Abs(gpuPage - cpuPage), 0.0f, 0.00008f); } + [Fact] + public void PagePatchRadiiIncludeBristleFootprintAndSweepDistance() + { + var (tangentRadius, normalRadius) = BokushoBrushSimulation.PagePatchRadii( + tangentRadius: 0.42f, + normalRadius: 0.35f, + sweepLength: 0.18f); + + Assert.Equal(0.42f * BokushoBrushSimulation.PagePatchTangentRadiusScale + 0.18f * BokushoBrushSimulation.PagePatchSweepRadiusScale, tangentRadius, precision: 6); + Assert.Equal(0.35f * BokushoBrushSimulation.PagePatchNormalRadiusScale, normalRadius, precision: 6); + + var tiny = BokushoBrushSimulation.PagePatchRadii(0.0f, 0.0f, 0.0f); + Assert.Equal(BokushoBrushSimulation.PagePatchMinimumRadius, tiny.TangentRadius); + Assert.Equal(BokushoBrushSimulation.PagePatchMinimumRadius, tiny.NormalRadius); + } + [Fact] public void CpuBrushProfileControlsPageFootprint() {