From c169f811b1ab7e1c930fcd29c56c7913737873bb Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 16 Jun 2026 08:32:36 +0100 Subject: [PATCH] Carry Bokusho source stroke tangents across packets --- .../Render/BokushoBrushSimulation.cs | 55 +++++++++++- .../Render/Shaders/D3D12BokushoBrush.hlsl | 88 ++++++++++++++----- 2 files changed, 115 insertions(+), 28 deletions(-) diff --git a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs index 96c71b6..c96eadb 100644 --- a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs +++ b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs @@ -11,6 +11,7 @@ public static class BokushoBrushSimulation public const float PagePatchNormalRadiusScale = 0.32f; public const float PagePatchMinimumRadius = 0.001f; public const float PagePatchWorldQuantum = 1.0f / 1024.0f; + private const float BristleTangentWorldQuantum = 1.0f / 4096.0f; private const float StrokeRhythmQuantum = 1.0f / 4096.0f; public const uint PagePatchInkUnits = 2048u; public const uint PagePatchCoverageUnits = 1024u; @@ -209,10 +210,10 @@ public static class BokushoBrushSimulation for (var replayStrokeIndex = chainStart; replayStrokeIndex < strokeIndex; replayStrokeIndex++) { - SimulateTuftSegmentToPage(strokes[replayStrokeIndex], replayStrokeIndex, chainStart, tuft, sampleCount, frame.PhysicsHz, frame.Pressure, frame.BrushRadius, splay, bend, friction, restOffset, edge, ref offset, ref tip, ref stateLoad, ref stateWet, ref previousPatchNormalRadius, ref previousPatchTangentRadius, densityPage, width, height, viewCenter, safeRadius, writeOutput: false, writeInitialStep: true); + SimulateTuftSegmentToPage(strokes, strokes[replayStrokeIndex], replayStrokeIndex, chainStart, tuft, sampleCount, frame.PhysicsHz, frame.Pressure, frame.BrushRadius, splay, bend, friction, restOffset, edge, ref offset, ref tip, ref stateLoad, ref stateWet, ref previousPatchNormalRadius, ref previousPatchTangentRadius, densityPage, width, height, viewCenter, safeRadius, writeOutput: false, writeInitialStep: true); } - SimulateTuftSegmentToPage(stroke, strokeIndex, chainStart, tuft, sampleCount, frame.PhysicsHz, frame.Pressure, frame.BrushRadius, splay, bend, friction, restOffset, edge, ref offset, ref tip, ref stateLoad, ref stateWet, ref previousPatchNormalRadius, ref previousPatchTangentRadius, densityPage, width, height, viewCenter, safeRadius, writeOutput: true, writeInitialStep: strokeIndex == chainStart); + SimulateTuftSegmentToPage(strokes, stroke, strokeIndex, chainStart, tuft, sampleCount, frame.PhysicsHz, frame.Pressure, frame.BrushRadius, splay, bend, friction, restOffset, edge, ref offset, ref tip, ref stateLoad, ref stateWet, ref previousPatchNormalRadius, ref previousPatchTangentRadius, densityPage, width, height, viewCenter, safeRadius, writeOutput: true, writeInitialStep: strokeIndex == chainStart); } } } @@ -255,7 +256,7 @@ public static class BokushoBrushSimulation { var t = sampleCount <= 1 ? 0.0f : sample / (float)(sampleCount - 1); var center = StrokePoint(stroke, t); - var tangent = StrokeTangent(stroke, t, sampleCount); + var tangent = StrokeTangent(strokes, strokeIndex, stroke, t, sampleCount); var normal = new Vector2(-tangent.Y, tangent.X); var strokeT = StrokeProgress(stroke, t); var taper = StrokeTaper(strokeT, stroke.EntryTaper, stroke.ExitTaper); @@ -312,6 +313,7 @@ public static class BokushoBrushSimulation } private static void SimulateTuftSegmentToPage( + IReadOnlyList strokes, AquariumBokushoBrushStroke stroke, int strokeIndex, int laneKey, @@ -352,7 +354,7 @@ public static class BokushoBrushSimulation { var t = stepCount <= 1 ? 0.0f : step / (float)(stepCount - 1); var center = StrokePoint(stroke, t); - var tangent = StrokeTangent(stroke, t, stepCount); + var tangent = StrokeTangent(strokes, strokeIndex, stroke, t, stepCount); var normal = new Vector2(-tangent.Y, tangent.X); var strokeT = StrokeProgress(stroke, t); var taper = StrokeTaper(strokeT, stroke.EntryTaper, stroke.ExitTaper); @@ -546,6 +548,51 @@ public static class BokushoBrushSimulation return Normalize(after - before); } + private static Vector2 StrokeTangent(IReadOnlyList strokes, int strokeIndex, AquariumBokushoBrushStroke stroke, float t, int sampleCount) + { + var dt = 1.0f / MathF.Max(sampleCount - 1.0f, 1.0f); + var beforeT = t - dt; + var afterT = t + dt; + var before = beforeT < 0.0f && PreviousSourcePacket(strokes, strokeIndex) is { } previous + ? StrokePoint(previous, Saturate(1.0f + beforeT)) + : StrokePoint(stroke, Saturate(beforeT)); + var after = afterT > 1.0f && NextSourcePacket(strokes, strokeIndex) is { } next + ? StrokePoint(next, Saturate(afterT - 1.0f)) + : StrokePoint(stroke, Saturate(afterT)); + before = Quantize(before, BristleTangentWorldQuantum); + after = Quantize(after, BristleTangentWorldQuantum); + return Normalize(after - before); + } + + private static AquariumBokushoBrushStroke? PreviousSourcePacket(IReadOnlyList strokes, int strokeIndex) + { + if (strokeIndex <= 0) + { + return null; + } + + var stroke = strokes[strokeIndex]; + var previous = strokes[strokeIndex - 1]; + return SameSourceBoundary(previous, stroke) ? previous : null; + } + + private static AquariumBokushoBrushStroke? NextSourcePacket(IReadOnlyList strokes, int strokeIndex) + { + if (strokeIndex + 1 >= strokes.Count) + { + return null; + } + + var stroke = strokes[strokeIndex]; + var next = strokes[strokeIndex + 1]; + return SameSourceBoundary(stroke, next) ? next : null; + } + + private static bool SameSourceBoundary(AquariumBokushoBrushStroke previous, AquariumBokushoBrushStroke next) + => previous.SourceStrokeId >= 0 && + previous.SourceStrokeId == next.SourceStrokeId && + MathF.Abs(Math.Clamp(previous.SegmentEnd, 0.0f, 1.0f) - Math.Clamp(next.SegmentStart, 0.0f, 1.0f)) <= 0.001f; + private static float ProjectCanvasSample(AquariumBokushoBrushFrame frame, IReadOnlyList strokes, ReadOnlySpan canvas, ReadOnlySpan tips, int strokeIndex, Vector2 world) { var stroke = strokes[strokeIndex]; diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl index 65a4be6..f58dbbe 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl @@ -6,6 +6,7 @@ static const float BOKUSHO_PAGE_PATCH_TANGENT_RADIUS_SCALE = 0.32; static const float BOKUSHO_PAGE_PATCH_NORMAL_RADIUS_SCALE = 0.32; static const float BOKUSHO_PAGE_PATCH_MINIMUM_RADIUS = 0.001; static const float BOKUSHO_PAGE_PATCH_WORLD_QUANTUM = 1.0 / 1024.0; +static const float BOKUSHO_BRISTLE_TANGENT_WORLD_QUANTUM = 1.0 / 4096.0; static const float BOKUSHO_STROKE_RHYTHM_QUANTUM = 1.0 / 4096.0; static const uint BOKUSHO_PAGE_PATCH_INK_UNITS = 2048u; static const uint BOKUSHO_PAGE_PATCH_COVERAGE_UNITS = 1024u; @@ -42,6 +43,26 @@ RWStructuredBuffer BokushoTipField : register(u22); RWStructuredBuffer BokushoPageDensityField : register(u23); StructuredBuffer BokushoBrushStrokes : register(t78); +float QuantizeScalar(float value, float quantum) +{ + return quantum > 0.0 ? floor((value / quantum) + 0.5) * quantum : value; +} + +float2 QuantizeFloat2(float2 value, float quantum) +{ + return float2(QuantizeScalar(value.x, quantum), QuantizeScalar(value.y, quantum)); +} + +float QuantizePositive(float value, float quantum) +{ + return QuantizeScalar(max(value, 0.0), quantum); +} + +uint QuantizePositiveUnits(float value, uint units) +{ + return (uint)floor(saturate(value) * (float)units + 0.5); +} + float2 StrokePoint(BokushoBrushStroke stroke, float t) { return cultmath_catmullrom(stroke.p0.xy, stroke.p1.xy, stroke.p2.xy, stroke.p3.xy, t); @@ -63,6 +84,44 @@ float2 StrokeTangent(BokushoBrushStroke stroke, float t, uint stepCount) return cultmath_normalize(after - before); } +bool SameSourceBoundary(BokushoBrushStroke previous, BokushoBrushStroke next) +{ + return previous.p3.w >= 0.0 && + abs(previous.p3.w - next.p3.w) <= 0.5 && + abs(saturate(previous.p0.w) - saturate(next.p0.z)) <= 0.001; +} + +float2 StrokeTangent(BokushoBrushStroke stroke, uint strokeIndex, float t, uint stepCount) +{ + float dt = 1.0 / max((float)stepCount - 1.0, 1.0); + float beforeT = t - dt; + float afterT = t + dt; + float2 before = StrokePoint(stroke, saturate(beforeT)); + float2 after = StrokePoint(stroke, saturate(afterT)); + uint strokeCount = max((uint)round(brushShape.w), 1u); + if (beforeT < 0.0 && strokeIndex > 0u) + { + BokushoBrushStroke previous = BokushoBrushStrokes[strokeIndex - 1u]; + if (SameSourceBoundary(previous, stroke)) + { + before = StrokePoint(previous, saturate(1.0 + beforeT)); + } + } + + if (afterT > 1.0 && strokeIndex + 1u < strokeCount) + { + BokushoBrushStroke next = BokushoBrushStrokes[strokeIndex + 1u]; + if (SameSourceBoundary(stroke, next)) + { + after = StrokePoint(next, saturate(afterT - 1.0)); + } + } + + before = QuantizeFloat2(before, BOKUSHO_BRISTLE_TANGENT_WORLD_QUANTUM); + after = QuantizeFloat2(after, BOKUSHO_BRISTLE_TANGENT_WORLD_QUANTUM); + return cultmath_normalize(after - before); +} + float StrokeTaper(float t) { float entry = cultmath_smoothstep(0.0, 0.10, t); @@ -109,26 +168,6 @@ float CanvasPigment(float value) return 0.96 * (1.0 - exp(-max(value, 0.0) * 0.82)); } -float QuantizeScalar(float value, float quantum) -{ - return quantum > 0.0 ? floor((value / quantum) + 0.5) * quantum : value; -} - -float2 QuantizeFloat2(float2 value, float quantum) -{ - return float2(QuantizeScalar(value.x, quantum), QuantizeScalar(value.y, quantum)); -} - -float QuantizePositive(float value, float quantum) -{ - return QuantizeScalar(max(value, 0.0), quantum); -} - -uint QuantizePositiveUnits(float value, uint units) -{ - return (uint)floor(saturate(value) * (float)units + 0.5); -} - float4 StrokeRhythm(BokushoBrushStroke stroke, float strokeT) { float entry = cultmath_smoothstep(0.0, 0.18, strokeT); @@ -314,7 +353,7 @@ void SimulateBokushoTuftSegment( { float t = sampleCount <= 1u ? 0.0 : (float)sample / (float)(sampleCount - 1u); float2 center = StrokePoint(stroke, t); - float2 tangent = StrokeTangent(stroke, t); + float2 tangent = StrokeTangent(stroke, outputStrokeIndex, t, sampleCount); float2 normal = float2(-tangent.y, tangent.x); float strokeT = StrokeProgress(stroke, t); float taper = StrokeTaper(stroke, t); @@ -373,6 +412,7 @@ void SimulateBokushoTuftSegment( void SimulateBokushoTuftSegmentToPage( BokushoBrushStroke stroke, + uint strokeIndex, uint laneKey, uint tuft, uint sampleCount, @@ -404,7 +444,7 @@ void SimulateBokushoTuftSegmentToPage( { float t = stepCount <= 1u ? 0.0 : (float)step / (float)(stepCount - 1u); float2 center = StrokePoint(stroke, t); - float2 tangent = StrokeTangent(stroke, t, stepCount); + float2 tangent = StrokeTangent(stroke, strokeIndex, t, stepCount); float2 normal = float2(-tangent.y, tangent.x); float strokeT = StrokeProgress(stroke, t); float taper = StrokeTaper(stroke, t); @@ -558,8 +598,8 @@ void D3D12BokushoPageDepositCS(uint3 dispatchThreadId : SV_DispatchThreadID) [loop] for (uint replayStrokeIndex = chainStart; replayStrokeIndex < strokeIndex; replayStrokeIndex += 1u) { - SimulateBokushoTuftSegmentToPage(BokushoBrushStrokes[replayStrokeIndex], chainStart, tuft, sampleCount, restOffset, edge, offset, tip, stateLoad, stateWet, previousPatchNormalRadius, previousPatchTangentRadius, false, true); + SimulateBokushoTuftSegmentToPage(BokushoBrushStrokes[replayStrokeIndex], replayStrokeIndex, chainStart, tuft, sampleCount, restOffset, edge, offset, tip, stateLoad, stateWet, previousPatchNormalRadius, previousPatchTangentRadius, false, true); } - SimulateBokushoTuftSegmentToPage(stroke, chainStart, tuft, sampleCount, restOffset, edge, offset, tip, stateLoad, stateWet, previousPatchNormalRadius, previousPatchTangentRadius, true, strokeIndex == chainStart); + SimulateBokushoTuftSegmentToPage(stroke, strokeIndex, chainStart, tuft, sampleCount, restOffset, edge, offset, tip, stateLoad, stateWet, previousPatchNormalRadius, previousPatchTangentRadius, true, strokeIndex == chainStart); }