From 55e99641d5afeeebe647718898798f29b0883f93 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 13 Jun 2026 16:13:20 +0100 Subject: [PATCH] Borrow Bokusho page samples across source segments --- .../Render/BokushoBrushSimulation.cs | 73 +++++++++++++++--- .../Render/Shaders/D3D12HeightField.hlsl | 76 ++++++++++++++++--- .../BokushoBrushSimulationTests.cs | 31 ++++++++ 3 files changed, 162 insertions(+), 18 deletions(-) diff --git a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs index 29dee13..4fa1f9b 100644 --- a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs +++ b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs @@ -253,7 +253,7 @@ public static class BokushoBrushSimulation var value = 0.0f; for (var strokeIndex = 0; strokeIndex < strokes.Length; strokeIndex++) { - value += ProjectCanvasSample(frame, strokes[strokeIndex], canvas, tips, strokeIndex, world); + value += ProjectCanvasSample(frame, strokes, canvas, tips, strokeIndex, world); } page[y * width + x] = Saturate(value); @@ -337,8 +337,9 @@ public static class BokushoBrushSimulation return Normalize(after - before); } - private static float ProjectCanvasSample(AquariumBokushoBrushFrame frame, AquariumBokushoBrushStroke stroke, ReadOnlySpan canvas, ReadOnlySpan tips, int strokeIndex, Vector2 world) + private static float ProjectCanvasSample(AquariumBokushoBrushFrame frame, IReadOnlyList strokes, ReadOnlySpan canvas, ReadOnlySpan tips, int strokeIndex, Vector2 world) { + var stroke = strokes[strokeIndex]; var bestDistance = float.PositiveInfinity; var bestT = 0.0f; for (var scan = 0; scan < 16; scan++) @@ -405,16 +406,62 @@ public static class BokushoBrushSimulation for (var sampleDelta = -3; sampleDelta <= 3; sampleDelta++) { - var sampleIndex = Math.Clamp(centerSample + sampleDelta, 0, sampleCount - 1); + var rawSampleIndex = centerSample + sampleDelta; + var sampleStrokeIndex = strokeIndex; + var sampleStroke = stroke; + var sampleIndex = rawSampleIndex; + if (rawSampleIndex < 0) + { + if (CanBorrowSourceSample(strokes, strokeIndex, strokeIndex - 1)) + { + sampleStrokeIndex = strokeIndex - 1; + sampleStroke = strokes[sampleStrokeIndex]; + sampleIndex = sampleCount + rawSampleIndex; + } + else + { + sampleIndex = 0; + } + } + else if (rawSampleIndex >= sampleCount) + { + if (CanBorrowSourceSample(strokes, strokeIndex, strokeIndex + 1)) + { + sampleStrokeIndex = strokeIndex + 1; + sampleStroke = strokes[sampleStrokeIndex]; + sampleIndex = rawSampleIndex - sampleCount; + } + else + { + sampleIndex = sampleCount - 1; + } + } + + sampleIndex = Math.Clamp(sampleIndex, 0, sampleCount - 1); var sampleT = sampleIndex / MathF.Max(sampleCount - 1.0f, 1.0f); - var sampleTangent = StrokeTangent(stroke, sampleT, sampleCount); + var sampleTangent = StrokeTangent(sampleStroke, sampleT, sampleCount); var sampleNormal = new Vector2(-sampleTangent.Y, sampleTangent.X); for (var tuftDelta = -4; tuftDelta <= 4; tuftDelta++) { var tuftIndex = Math.Clamp(centerTuft + tuftDelta, 0, tuftCount - 1); - var index = ((strokeIndex * tuftCount) + tuftIndex) * sampleCount + sampleIndex; - var previousIndex = ((strokeIndex * tuftCount) + tuftIndex) * sampleCount + Math.Max(sampleIndex - 1, 0); + var previousStrokeIndex = sampleStrokeIndex; + var previousSampleIndex = sampleIndex - 1; + if (previousSampleIndex < 0) + { + if (CanBorrowSourceSample(strokes, sampleStrokeIndex, sampleStrokeIndex - 1)) + { + previousStrokeIndex = sampleStrokeIndex - 1; + previousSampleIndex = sampleCount - 1; + } + else + { + previousSampleIndex = 0; + } + } + + var index = ((sampleStrokeIndex * tuftCount) + tuftIndex) * sampleCount + sampleIndex; + var previousIndex = ((previousStrokeIndex * tuftCount) + tuftIndex) * sampleCount + previousSampleIndex; var tip = tips[index]; var previousTip = tips[previousIndex]; var tipPoint = new Vector2(tip.X, tip.Y); @@ -435,8 +482,8 @@ public static class BokushoBrushSimulation var longitudinalGate = SmoothStep(1.0f, 0.0f, MathF.Abs(tangentDistance) * 0.62f); var contribution = canvas[index] * tipContact - * (0.03f + contactCore * 0.97f) - * (0.18f + longitudinalGate * 0.82f) + * (0.006f + contactCore * 0.994f) + * (0.06f + longitudinalGate * 0.94f) * (1.0f - MathF.Abs(sampleDelta) * 0.070f) * (0.82f + sweepContact * 0.24f); pigmentPeak = MathF.Max(pigmentPeak, contribution); @@ -444,7 +491,15 @@ public static class BokushoBrushSimulation } } - return Saturate(pigmentPeak * 1.30f + pigmentFlow * 0.010f) * hold * (0.36f + pressure * 0.44f + Saturate(frame.InkLoad * 0.5f) * 0.20f); + return Saturate(pigmentPeak * 1.75f + pigmentFlow * 0.004f) * hold * (0.36f + pressure * 0.44f + Saturate(frame.InkLoad * 0.5f) * 0.20f); + } + + private static bool CanBorrowSourceSample(IReadOnlyList strokes, int strokeIndex, int candidateIndex) + { + return candidateIndex >= 0 + && candidateIndex < strokes.Count + && strokes[strokeIndex].SourceStrokeId >= 0 + && strokes[candidateIndex].SourceStrokeId == strokes[strokeIndex].SourceStrokeId; } private static float StrokeTaper(float t, float entryTaper, float exitTaper) diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl index 0843fb8..1a2201a 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl @@ -224,6 +224,17 @@ float bokushoPaperTooth(float2 world, uint strokeIndex) return saturate(fine * 0.58 + fiber * 0.42); } +bool bokushoCanBorrowSourceSample(uint strokeIndex, int candidateIndex, uint strokeCount) +{ + if (candidateIndex < 0 || (uint)candidateIndex >= strokeCount) + { + return false; + } + + float sourceStrokeId = BokushoBrushStrokes[strokeIndex].p3.w; + return sourceStrokeId >= 0.0 && abs(BokushoBrushStrokes[(uint)candidateIndex].p3.w - sourceStrokeId) <= 0.5; +} + float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint strokeIndex) { float bestDistance = 1.0e20; @@ -266,6 +277,7 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro uint sampleCount = max((uint)round(bokushoShape.x), 2u); uint tuftCount = max((uint)round(bokushoShape.y), 1u); + uint strokeCount = min(max((uint)round(bokushoShape.w), 0u), 64u); float2 center = bokushoStrokePoint(stroke, bestT); float2 tangent = bokushoStrokeTangent(stroke, bestT); float2 normal = float2(-tangent.y, tangent.x); @@ -297,18 +309,64 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro [unroll] for (int sampleDelta = -3; sampleDelta <= 3; sampleDelta += 1) { - uint sampleIndex = min((uint)max((int)centerSample + sampleDelta, 0), sampleCount - 1u); + int rawSampleIndex = (int)centerSample + sampleDelta; + uint sampleStrokeIndex = strokeIndex; + BokushoBrushStroke sampleStroke = stroke; + int resolvedSampleIndex = rawSampleIndex; + if (rawSampleIndex < 0) + { + if (bokushoCanBorrowSourceSample(strokeIndex, (int)strokeIndex - 1, strokeCount)) + { + sampleStrokeIndex = strokeIndex - 1u; + sampleStroke = BokushoBrushStrokes[sampleStrokeIndex]; + resolvedSampleIndex = (int)sampleCount + rawSampleIndex; + } + else + { + resolvedSampleIndex = 0; + } + } + else if (rawSampleIndex >= (int)sampleCount) + { + if (bokushoCanBorrowSourceSample(strokeIndex, (int)strokeIndex + 1, strokeCount)) + { + sampleStrokeIndex = strokeIndex + 1u; + sampleStroke = BokushoBrushStrokes[sampleStrokeIndex]; + resolvedSampleIndex = rawSampleIndex - (int)sampleCount; + } + else + { + resolvedSampleIndex = (int)sampleCount - 1; + } + } + + uint sampleIndex = min((uint)max(resolvedSampleIndex, 0), sampleCount - 1u); float sampleT = (float)sampleIndex / max((float)(sampleCount - 1u), 1.0); - float2 sampleTangent = bokushoStrokeTangent(stroke, sampleT); + float2 sampleTangent = bokushoStrokeTangent(sampleStroke, sampleT); float2 sampleNormal = float2(-sampleTangent.y, sampleTangent.x); [unroll] for (int tuftDelta = -4; tuftDelta <= 4; tuftDelta += 1) { uint tuftIndex = min((uint)max((int)centerTuft + tuftDelta, 0), tuftCount - 1u); - float4 tip = bokushoTipSample(strokeIndex, sampleIndex, tuftIndex); - uint previousSampleIndex = min((uint)max((int)sampleIndex - 1, 0), sampleCount - 1u); - float4 previousTip = bokushoTipSample(strokeIndex, previousSampleIndex, tuftIndex); + float4 tip = bokushoTipSample(sampleStrokeIndex, sampleIndex, tuftIndex); + uint previousStrokeIndex = sampleStrokeIndex; + int previousSampleIndexValue = (int)sampleIndex - 1; + if (previousSampleIndexValue < 0) + { + if (bokushoCanBorrowSourceSample(sampleStrokeIndex, (int)sampleStrokeIndex - 1, strokeCount)) + { + previousStrokeIndex = sampleStrokeIndex - 1u; + previousSampleIndexValue = (int)sampleCount - 1; + } + else + { + previousSampleIndexValue = 0; + } + } + + uint previousSampleIndex = min((uint)max(previousSampleIndexValue, 0), sampleCount - 1u); + float4 previousTip = bokushoTipSample(previousStrokeIndex, previousSampleIndex, tuftIndex); float2 sweep = tip.xy - previousTip.xy; float sweepLengthSquared = dot(sweep, sweep); float sweepT = sweepLengthSquared <= 0.000001 ? 1.0 : saturate(dot(world - previousTip.xy, sweep) / sweepLengthSquared); @@ -321,11 +379,11 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro float ellipse = sqrt(normalDistance * normalDistance + tangentDistance * tangentDistance); float tipContact = max(smoothstep(1.0, 0.0, ellipse), sweepContact * 0.86); float longitudinalGate = smoothstep(1.0, 0.0, abs(tangentDistance) * 0.62); - float pigment = BokushoCanvasField[((strokeIndex * tuftCount) + tuftIndex) * sampleCount + sampleIndex]; + float pigment = BokushoCanvasField[((sampleStrokeIndex * tuftCount) + tuftIndex) * sampleCount + sampleIndex]; float contribution = pigment * tipContact - * (0.03 + contactCore * 0.97) - * (0.18 + longitudinalGate * 0.82) + * (0.006 + contactCore * 0.994) + * (0.06 + longitudinalGate * 0.94) * (1.0 - abs((float)sampleDelta) * 0.070) * (0.82 + sweepContact * 0.24); pigmentPeak = max(pigmentPeak, contribution); @@ -333,7 +391,7 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro } } - return saturate(pigmentPeak * 1.30 + pigmentFlow * 0.010) * hold * (0.36 + pressure * 0.44 + saturate(bokushoMaterial.z * 0.5) * 0.20); + return saturate(pigmentPeak * 1.75 + pigmentFlow * 0.004) * hold * (0.36 + pressure * 0.44 + saturate(bokushoMaterial.z * 0.5) * 0.20); } float bokushoPageHeight(float2 world) diff --git a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs index 309d896..f51153e 100644 --- a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs +++ b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs @@ -408,6 +408,25 @@ public sealed class BokushoBrushSimulationTests Assert.True(MathF.Abs(sharedJoin - splitJoin) > 0.0001f); } + [Fact] + public void CpuBrushProjectionBorrowsSameSourceSamplesAcrossSegmentEdges() + { + var shared = SourceIdentityFrame(sameSourceId: true); + var split = SourceIdentityFrame(sameSourceId: false); + var sharedResult = BokushoBrushSimulation.Evaluate(shared); + var splitResult = BokushoBrushSimulation.Evaluate(split); + var sharedCanvas = LastSampleOnly(sharedResult.Canvas, sharedResult.SampleCount, sharedResult.TuftCount); + var splitCanvas = LastSampleOnly(splitResult.Canvas, splitResult.SampleCount, splitResult.TuftCount); + + var sharedPage = BokushoBrushSimulation.ProjectCanvasToPage(shared, sharedCanvas, sharedResult.Tips, 160, 160, Vector2.Zero, 4.0f); + var splitPage = BokushoBrushSimulation.ProjectCanvasToPage(split, splitCanvas, splitResult.Tips, 160, 160, Vector2.Zero, 4.0f); + var sharedJoin = SamplePage(sharedPage, 160, 160, new Vector2(0.03f, 0.0f), 4.0f); + var splitJoin = SamplePage(splitPage, 160, 160, new Vector2(0.03f, 0.0f), 4.0f); + + Assert.True(sharedJoin > 0.0f); + Assert.True(sharedJoin > splitJoin * 1.05f, $"shared join={sharedJoin:0.000000}; split join={splitJoin:0.000000}"); + } + [Fact] public void CpuBrushGeometryEnrichesCurvedStrokePressureAndWidth() { @@ -640,6 +659,18 @@ public sealed class BokushoBrushSimulationTests return canvas.Skip(tuft * sampleCount).Take(sampleCount).Sum(); } + private static float[] LastSampleOnly(IReadOnlyList canvas, int sampleCount, int tuftCount) + { + var sparse = new float[canvas.Count]; + for (var tuft = 0; tuft < tuftCount; tuft++) + { + var index = (tuft * sampleCount) + sampleCount - 1; + sparse[index] = canvas[index]; + } + + return sparse; + } + private static float TipJumpAtSecondSegmentStart(BokushoBrushSimulationResult result, int tuft) { var lastFirstIndex = (tuft * result.SampleCount) + result.SampleCount - 1;