diff --git a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs index 4fa1f9b..58a2830 100644 --- a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs +++ b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs @@ -134,6 +134,8 @@ public static class BokushoBrushSimulation var radius = MathF.Max(frameBrushRadius * stroke.RadiusScale, 0.0001f); var normalRadius = MathF.Max(radius * stroke.NormalScale, 0.0001f); var tangentRadius = MathF.Max(radius * stroke.TangentScale, 0.0001f); + var segmentSpan = SegmentSpan(stroke, sampleCount); + var segmentVelocityScale = 1.0f / segmentSpan; var split = Saturate((0.20f - LaneHash(laneKey, tuft, 53)) * 4.0f) * Saturate((edge - 0.18f) * 1.7f) * stroke.SplitScale; for (var sample = 0; sample < sampleCount; sample++) { @@ -168,7 +170,7 @@ public static class BokushoBrushSimulation var drag = contact * friction * (0.38f + stateWet * 0.22f + edge * 0.18f); tip += slip * (1.0f - drag); - var velocity = slip.Length() * physicsHz / MathF.Max(radius, 0.001f); + var velocity = slip.Length() * physicsHz * segmentVelocityScale / MathF.Max(radius, 0.001f); var tension = Saturate(MathF.Abs(targetOffset - offset) / MathF.Max(localNormalRadius, 0.001f) * 0.38f + velocity * 0.018f + drag * 0.46f); var separation = Saturate(edge * 0.18f + tension * (0.24f + split * 0.18f) + velocity * 0.008f - stateWet * (0.12f + cohesion * 0.10f)); var adhesion = Saturate(stateWet * (0.44f + cohesion * 0.28f + localPressure * 0.20f) - separation * 0.16f - tension * 0.07f); @@ -180,8 +182,8 @@ public static class BokushoBrushSimulation var depositBody = 0.70f + laneCore * 0.58f - edgeComb * 0.14f; var depositIntermittency = 1.0f - fiberGate * dryMemory * (0.48f + edge * 0.24f); var deposition = contact * stateLoad * stateWet * Saturate(0.10f + drag * 0.72f + velocity * 0.010f) * (0.74f + separation * 0.18f + cohesion * 0.26f) * depositBody * depositIntermittency; - stateLoad = MathF.Max(0.0f, stateLoad - deposition * (0.032f + localPressure * 0.020f - cohesion * 0.008f)); - stateWet = MathF.Max(0.0f, stateWet - deposition * (0.010f + dryMemory * 0.004f + edgeComb * 0.003f)); + stateLoad = MathF.Max(0.0f, stateLoad - deposition * segmentSpan * (0.032f + localPressure * 0.020f - cohesion * 0.008f)); + stateWet = MathF.Max(0.0f, stateWet - deposition * segmentSpan * (0.010f + dryMemory * 0.004f + edgeComb * 0.003f)); if (writeOutput) { @@ -379,6 +381,7 @@ public static class BokushoBrushSimulation var centerPoint = StrokePoint(stroke, bestT); var tangent = StrokeTangent(stroke, bestT, sampleCount); var normal = new Vector2(-tangent.Y, tangent.X); + var projectionGate = InternalSegmentProjectionGate(strokes, strokeIndex, stroke, bestT); var lateral = Vector2.Dot(world - centerPoint, normal); var strokeT = StrokeProgress(stroke, bestT); var taper = StrokeTaper(strokeT, stroke.EntryTaper, stroke.ExitTaper); @@ -491,7 +494,7 @@ public static class BokushoBrushSimulation } } - return Saturate(pigmentPeak * 1.75f + pigmentFlow * 0.004f) * hold * (0.36f + pressure * 0.44f + Saturate(frame.InkLoad * 0.5f) * 0.20f); + return Saturate(pigmentPeak * 1.75f + pigmentFlow * 0.004f) * hold * projectionGate * (0.36f + pressure * 0.44f + Saturate(frame.InkLoad * 0.5f) * 0.20f); } private static bool CanBorrowSourceSample(IReadOnlyList strokes, int strokeIndex, int candidateIndex) @@ -502,6 +505,29 @@ public static class BokushoBrushSimulation && strokes[candidateIndex].SourceStrokeId == strokes[strokeIndex].SourceStrokeId; } + private static float InternalSegmentProjectionGate(IReadOnlyList strokes, int strokeIndex, AquariumBokushoBrushStroke stroke, float t) + { + var gate = 1.0f; + if (stroke.SegmentStart > 0.0001f && CanBorrowSourceSample(strokes, strokeIndex, strokeIndex - 1)) + { + gate *= 0.34f + SmoothStep(0.0f, 0.16f, t) * 0.66f; + } + + if (stroke.SegmentEnd < 0.9999f && CanBorrowSourceSample(strokes, strokeIndex, strokeIndex + 1)) + { + gate *= 0.34f + (1.0f - SmoothStep(0.84f, 1.0f, t)) * 0.66f; + } + + return gate; + } + + private static float SegmentSpan(AquariumBokushoBrushStroke stroke, int sampleCount) + { + var start = Math.Clamp(stroke.SegmentStart, 0.0f, 1.0f); + var end = Math.Clamp(stroke.SegmentEnd, start, 1.0f); + return MathF.Max(end - start, 1.0f / MathF.Max(sampleCount - 1.0f, 1.0f)); + } + private static float StrokeTaper(float t, float entryTaper, float exitTaper) { var entry = SmoothStep(0.0f, entryTaper, t); diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl index c688953..0e73e31 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl @@ -125,6 +125,13 @@ float StrokeGestureWidthShape(BokushoBrushStroke stroke, float t) return saturate(0.84 + slowSpread * 0.36 + turnSpread * 0.20); } +float StrokeSegmentSpan(BokushoBrushStroke stroke, uint sampleCount) +{ + float start = saturate(stroke.p0.z); + float endValue = saturate(max(stroke.p0.z, stroke.p0.w)); + return max(endValue - start, 1.0 / max((float)(sampleCount - 1u), 1.0)); +} + float LaneHash(uint strokeIndex, uint tuft, uint salt) { uint value = (strokeIndex + 1u) * 0x9E3779B9u ^ (tuft + 1u) * 0x85EBCA6Bu ^ salt; @@ -206,6 +213,8 @@ void SimulateBokushoTuftSegment( float radius = max(brushMaterial.x * stroke.profile.x, 0.0001); float normalRadius = max(radius * stroke.profile.z, 0.0001); float tangentRadius = max(radius * stroke.profile.w, 0.0001); + float segmentSpan = StrokeSegmentSpan(stroke, sampleCount); + float segmentVelocityScale = 1.0 / segmentSpan; float split = saturate((0.20 - LaneHash(laneKey, tuft, 53u)) * 4.0) * saturate((edge - 0.18) * 1.7) * stroke.dynamics.w; [loop] @@ -242,7 +251,7 @@ void SimulateBokushoTuftSegment( float drag = contact * friction * (0.38 + stateWet * 0.22 + edge * 0.18); tip = tip + slip * (1.0 - drag); - float velocity = length(slip) * brushShape.z / max(radius, 0.001); + float velocity = length(slip) * brushShape.z * segmentVelocityScale / max(radius, 0.001); float tension = saturate(abs(targetOffset - offset) / max(localNormalRadius, 0.001) * 0.38 + velocity * 0.018 + drag * 0.46); float separation = saturate(edge * 0.18 + tension * (0.24 + split * 0.18) + velocity * 0.008 - stateWet * (0.12 + cohesion * 0.10)); float adhesion = saturate(stateWet * (0.44 + cohesion * 0.28 + localPressure * 0.20) - separation * 0.16 - tension * 0.07); @@ -254,8 +263,8 @@ void SimulateBokushoTuftSegment( float depositBody = 0.70 + laneCore * 0.58 - edgeComb * 0.14; float depositIntermittency = 1.0 - fiberGate * dryMemory * (0.48 + edge * 0.24); float deposition = contact * stateLoad * stateWet * saturate(0.10 + drag * 0.72 + velocity * 0.010) * (0.74 + separation * 0.18 + cohesion * 0.26) * depositBody * depositIntermittency; - stateLoad = max(0.0, stateLoad - deposition * (0.032 + localPressure * 0.020 - cohesion * 0.008)); - stateWet = max(0.0, stateWet - deposition * (0.010 + dryMemory * 0.004 + edgeComb * 0.003)); + stateLoad = max(0.0, stateLoad - deposition * segmentSpan * (0.032 + localPressure * 0.020 - cohesion * 0.008)); + stateWet = max(0.0, stateWet - deposition * segmentSpan * (0.010 + dryMemory * 0.004 + edgeComb * 0.003)); if (writeOutput) { diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl index 1a2201a..678be4b 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl @@ -235,6 +235,22 @@ bool bokushoCanBorrowSourceSample(uint strokeIndex, int candidateIndex, uint str return sourceStrokeId >= 0.0 && abs(BokushoBrushStrokes[(uint)candidateIndex].p3.w - sourceStrokeId) <= 0.5; } +float bokushoInternalSegmentProjectionGate(uint strokeIndex, BokushoBrushStroke stroke, float t, uint strokeCount) +{ + float gate = 1.0; + if (stroke.p0.z > 0.0001 && bokushoCanBorrowSourceSample(strokeIndex, (int)strokeIndex - 1, strokeCount)) + { + gate *= 0.34 + smoothstep(0.0, 0.16, t) * 0.66; + } + + if (stroke.p0.w < 0.9999 && bokushoCanBorrowSourceSample(strokeIndex, (int)strokeIndex + 1, strokeCount)) + { + gate *= 0.34 + (1.0 - smoothstep(0.84, 1.0, t)) * 0.66; + } + + return gate; +} + float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint strokeIndex) { float bestDistance = 1.0e20; @@ -281,6 +297,7 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro float2 center = bokushoStrokePoint(stroke, bestT); float2 tangent = bokushoStrokeTangent(stroke, bestT); float2 normal = float2(-tangent.y, tangent.x); + float projectionGate = bokushoInternalSegmentProjectionGate(strokeIndex, stroke, bestT, strokeCount); float lateral = dot(world - center, normal); float taper = bokushoStrokeTaper(stroke, bestT); float gestureWidth = bokushoStrokeGestureWidthShape(stroke, bestT); @@ -391,7 +408,7 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro } } - return saturate(pigmentPeak * 1.75 + pigmentFlow * 0.004) * hold * (0.36 + pressure * 0.44 + saturate(bokushoMaterial.z * 0.5) * 0.20); + return saturate(pigmentPeak * 1.75 + pigmentFlow * 0.004) * hold * projectionGate * (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 f51153e..6fe1f16 100644 --- a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs +++ b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs @@ -427,6 +427,34 @@ public sealed class BokushoBrushSimulationTests Assert.True(sharedJoin > splitJoin * 1.05f, $"shared join={sharedJoin:0.000000}; split join={splitJoin:0.000000}"); } + [Fact] + public void CpuBrushProjectionTreatsSameSourceSegmentEdgesAsThroughPoints() + { + var shared = SourceIdentityFrame(sameSourceId: true); + var split = SourceIdentityFrame(sameSourceId: false); + var sharedResult = BokushoBrushSimulation.Evaluate(shared); + var splitResult = BokushoBrushSimulation.Evaluate(split); + + var sharedPage = BokushoBrushSimulation.ProjectCanvasToPage(shared, sharedResult.Canvas, sharedResult.Tips, 160, 160, Vector2.Zero, 4.0f); + var splitPage = BokushoBrushSimulation.ProjectCanvasToPage(split, splitResult.Canvas, splitResult.Tips, 160, 160, Vector2.Zero, 4.0f); + var sharedJoin = SamplePage(sharedPage, 160, 160, new Vector2(0.0f, 0.0f), 4.0f); + var splitJoin = SamplePage(splitPage, 160, 160, new Vector2(0.0f, 0.0f), 4.0f); + + Assert.True(sharedJoin > 0.0f); + Assert.True(sharedJoin < splitJoin, $"shared join={sharedJoin:0.000000}; split join={splitJoin:0.000000}"); + } + + [Fact] + public void CpuBrushSegmentSpanKeepsLaterFittedPiecesLoaded() + { + var frame = SourceIdentityFrame(sameSourceId: true); + var result = BokushoBrushSimulation.Evaluate(frame); + var firstStrokeInk = StrokeSum(result.Canvas, result.SampleCount, result.TuftCount, stroke: 0); + var secondStrokeInk = StrokeSum(result.Canvas, result.SampleCount, result.TuftCount, stroke: 1); + + Assert.True(secondStrokeInk > firstStrokeInk * 0.25f, $"first={firstStrokeInk:0.000000}; second={secondStrokeInk:0.000000}"); + } + [Fact] public void CpuBrushGeometryEnrichesCurvedStrokePressureAndWidth() { @@ -659,6 +687,11 @@ public sealed class BokushoBrushSimulationTests return canvas.Skip(tuft * sampleCount).Take(sampleCount).Sum(); } + private static float StrokeSum(float[] canvas, int sampleCount, int tuftCount, int stroke) + { + return canvas.Skip(stroke * sampleCount * tuftCount).Take(sampleCount * tuftCount).Sum(); + } + private static float[] LastSampleOnly(IReadOnlyList canvas, int sampleCount, int tuftCount) { var sparse = new float[canvas.Count];