From 201b4d59eae4596114f4955f65e44ae94c074fe9 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 14 Jun 2026 00:32:07 +0100 Subject: [PATCH] Simplify Bokusho brush simulation core --- .../Render/BokushoBrushSimulation.cs | 284 +++--------------- .../Render/Shaders/D3D12BokushoBrush.hlsl | 190 ++---------- .../Render/Shaders/D3D12HeightField.hlsl | 181 +---------- .../BokushoBrushSimulationTests.cs | 99 ++---- 4 files changed, 101 insertions(+), 653 deletions(-) diff --git a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs index 2af962b..238553f 100644 --- a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs +++ b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs @@ -136,7 +136,7 @@ public static class BokushoBrushSimulation 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; + var split = Saturate((0.28f - LaneHash(laneKey, tuft, 53)) * 3.0f) * Saturate((edge - 0.20f) * 1.5f) * stroke.SplitScale; for (var sample = 0; sample < sampleCount; sample++) { var t = sampleCount <= 1 ? 0.0f : sample / (float)(sampleCount - 1); @@ -145,83 +145,48 @@ public static class BokushoBrushSimulation var normal = new Vector2(-tangent.Y, tangent.X); var strokeT = StrokeProgress(stroke, t); var taper = StrokeTaper(strokeT, stroke.EntryTaper, stroke.ExitTaper); - var normalShape = StrokeNormalRadiusShape(taper); - var tangentShape = StrokeTangentRadiusShape(taper); - var pressureShape = StrokePressureShape(strokeT, stroke.EntryTaper, stroke.ExitTaper); - var gesturePressure = StrokeGesturePressureShape(stroke, t, sampleCount); - var gestureWidth = StrokeGestureWidthShape(stroke, t, sampleCount); - var localPressure = pressure * taper * pressureShape * gesturePressure; - var cohesion = LaneCohesion(edge, split, stateWet, localPressure); - var poseSpread = Saturate(0.92f + MathF.Abs(stroke.ShaftTilt) * 0.22f + stroke.Compliance * 0.10f - stroke.GripHeight * 0.04f); - var localNormalRadius = MathF.Max(normalRadius * normalShape * gestureWidth * poseSpread, 0.0001f); - var localTangentRadius = MathF.Max(tangentRadius * tangentShape * (0.86f + gestureWidth * 0.08f + stroke.GripHeight * 0.10f), 0.0001f); - var turnKey = stroke.SourceStrokeId >= 0 ? stroke.SourceStrokeId : strokeIndex; - var turn = MathF.Sin(strokeT * MathF.Tau + turnKey * 0.37f); - var rotatedRest = restOffset + stroke.ShaftRotation * 0.10f * (1.0f - edge); - var targetOffset = rotatedRest * localNormalRadius * (0.38f + splay * 0.52f + localPressure * 0.08f - wetness * 0.10f) + (turn + stroke.ShaftRotation * 0.22f) * localNormalRadius * 0.14f * (1.0f - edge); - var recovery = Saturate(0.06f + stateWet * 0.10f + localPressure * 0.07f + (1.0f - edge) * 0.06f + stroke.Compliance * 0.07f); + var localPressure = pressure * taper; + var laneCore = SmoothStep(0.0f, 0.78f, 1.0f - edge); + var cohesion = Saturate(0.28f + stateWet * 0.44f + laneCore * 0.24f + localPressure * 0.10f - split * 0.18f); + var localNormalRadius = MathF.Max(normalRadius * (0.18f + taper * 0.82f) * (0.72f + splay * 0.34f + localPressure * 0.16f - cohesion * 0.08f), 0.0001f); + var localTangentRadius = MathF.Max(tangentRadius * (0.24f + taper * 0.76f) * (0.86f + bend * 0.18f), 0.0001f); + var targetOffset = (restOffset + stroke.ShaftRotation * 0.12f) * localNormalRadius * (0.58f + splay * 0.34f + localPressure * 0.18f - cohesion * 0.20f); + var recovery = Saturate(0.05f + stroke.Compliance * 0.08f + stateWet * 0.08f + localPressure * 0.10f); offset = Lerp(offset, targetOffset, recovery); - var poseLead = stroke.ShaftTilt * 0.42f + stroke.ShaftRotation * 0.22f; - var dragNormalBias = Saturate(0.28f + MathF.Abs(poseLead) * 0.58f + stroke.Compliance * 0.08f); - var lag = localTangentRadius * (0.04f + bend * 0.34f + stroke.GripHeight * 0.10f + friction * localPressure * 0.18f + edge * 0.08f); - var dragVector = Normalize(tangent + normal * poseLead * dragNormalBias); + var poseLead = stroke.ShaftTilt * 0.36f + stroke.ShaftRotation * 0.16f; + var lag = localTangentRadius * (0.10f + bend * 0.32f + friction * localPressure * 0.16f + edge * 0.06f); + var dragVector = Normalize(tangent + normal * poseLead); var desiredTip = center + normal * offset - dragVector * lag; var slip = desiredTip - tip; - var poseContact = 0.90f + stroke.Compliance * 0.10f + (1.0f - Saturate(stroke.GripHeight / 2.4f)) * 0.10f; - var wetContact = Saturate(stateWet + (1.0f - stateWet) * (0.10f + edge * 0.22f + split * 0.12f)); - var contact = Saturate(localPressure * poseContact * wetContact * stateLoad * (0.58f + cohesion * 0.40f + (1.0f - edge) * 0.18f)); - var drag = contact * friction * (0.38f + stateWet * 0.22f + edge * 0.18f); - tip += slip * (1.0f - drag); - 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); - var laneCore = SmoothStep(0.0f, 0.74f, 1.0f - edge); - var edgeComb = Saturate(edge * 1.08f + split * 0.26f + separation * 0.34f - cohesion * 0.12f); - var dryMemory = Saturate((1.0f - stateWet) * 0.68f + edgeComb * 0.42f + velocity * 0.004f - localPressure * 0.10f); + var contact = Saturate(localPressure * stateLoad * (0.24f + stateWet * 0.62f + laneCore * 0.18f)); + var drag = contact * friction * (0.28f + stateWet * 0.34f); + tip += slip * (0.18f + recovery * 0.82f) * (1.0f - drag * 0.52f); + + var tension = Saturate(velocity * 0.014f + MathF.Abs(targetOffset - offset) / MathF.Max(localNormalRadius, 0.001f) * 0.22f + edge * 0.12f); + var separation = Saturate(split * 0.34f + tension * 0.46f + edge * 0.20f - cohesion * 0.24f); + var adhesion = Saturate(stateWet * (0.36f + cohesion * 0.40f) + localPressure * 0.10f - separation * 0.22f); + var dryMemory = Saturate((1.0f - stateWet) * 0.62f + separation * 0.32f + velocity * 0.004f); var fiberNoise = LaneHash(laneKey + sample * 13, tuft, 101); - var fiberGate = SmoothStep(0.20f + dryMemory * 0.24f, 0.96f, fiberNoise); - var tearNoise = LaneHash(laneKey + sample * 29, tuft, 211); - var tearReadiness = Saturate(edge * 1.10f + separation * 0.64f + split * 0.18f + dryMemory * 0.22f - laneCore * 0.36f); - var bristleTear = SmoothStep(0.50f - separation * 0.16f - dryMemory * 0.10f, 0.98f, tearNoise) * tearReadiness; - var bristleContinuity = 1.0f - bristleTear * (0.42f + dryMemory * 0.24f + edge * 0.16f); - var depositBody = 0.70f + laneCore * 0.58f - edgeComb * 0.14f; - var depositIntermittency = (1.0f - fiberGate * dryMemory * (0.48f + edge * 0.24f)) * bristleContinuity; - var wetFlow = Saturate(stateWet + (1.0f - stateWet) * (0.10f + edgeComb * 0.24f + separation * 0.18f)); - var deposition = contact * stateLoad * wetFlow * Saturate(0.10f + drag * 0.72f + velocity * 0.010f) * (0.74f + separation * 0.18f + cohesion * 0.26f) * depositBody * depositIntermittency; - var releaseNoise = LaneHash(laneKey + sample * 41, tuft, 307); - var releaseGate = SmoothStep(0.38f, 0.92f, releaseNoise) - * bristleTear - * dryMemory - * edgeComb - * (1.0f - laneCore * 0.72f); - var dryRelease = contact - * stateLoad - * wetFlow - * releaseGate - * Saturate(0.18f + velocity * 0.012f + separation * 0.32f) - * (0.18f + edge * 0.34f); - var consumedPigment = deposition + dryRelease * 0.72f; - stateLoad = MathF.Max(0.0f, stateLoad - consumedPigment * segmentSpan * (0.032f + localPressure * 0.020f - cohesion * 0.008f)); - stateWet = MathF.Max(0.0f, stateWet - consumedPigment * segmentSpan * (0.010f + dryMemory * 0.004f + edgeComb * 0.003f)); + var continuity = 1.0f - SmoothStep(0.18f + dryMemory * 0.28f, 0.96f, fiberNoise) * dryMemory * (0.38f + edge * 0.22f); + var contactTransfer = contact * stateLoad * (0.12f + stateWet * 0.88f) * (0.24f + drag * 0.62f + localPressure * 0.18f) * (0.62f + laneCore * 0.48f - separation * 0.18f) * continuity; + var airborneRelease = (1.0f - contact) * stateLoad * stateWet * Saturate(velocity * 0.010f - adhesion * 0.16f) * (0.20f + separation * 0.42f + edge * 0.18f); + var consumedPigment = contactTransfer + airborneRelease; + stateLoad = MathF.Max(0.0f, stateLoad - consumedPigment * segmentSpan * (0.028f + localPressure * 0.018f)); + stateWet = MathF.Max(0.0f, stateWet - consumedPigment * segmentSpan * (0.012f + dryMemory * 0.006f)); if (writeOutput) { var index = ((strokeIndex * tuftCount) + tuft) * sampleCount + sample; - var pigmentSurvival = 0.62f + cohesion * 0.30f + laneCore * 0.36f - split * 0.08f - dryMemory * 0.12f; - var joinBlend = SegmentJoinBlend(strokes, strokeIndex, stroke, t); - var pigment = deposition * (8.4f + contact * 2.8f) + contact * stateLoad * stateWet * (0.14f + laneCore * 0.18f) + adhesion * contact * 0.08f; - var continuousPigment = pigment * pigmentSurvival * stroke.PigmentScale * joinBlend * bristleContinuity; - var releasePigment = dryRelease * (18.0f + velocity * 0.022f) * stroke.PigmentScale * joinBlend * (0.46f + edgeComb * 0.54f); - canvas[index] = Saturate(continuousPigment + releasePigment); - trace[index] = Saturate((contact * (0.24f + stateLoad * 0.36f + adhesion * 0.18f + laneCore * 0.12f) + deposition * 1.9f + dryRelease * 2.8f) * joinBlend * (0.72f + bristleContinuity * 0.28f)); + var pigment = (contactTransfer * (1.8f + localPressure * 0.6f) + airborneRelease * (2.8f + velocity * 0.003f)) * stroke.PigmentScale; + canvas[index] = Saturate(pigment); + trace[index] = Saturate(contact * 0.70f + airborneRelease * 2.0f + stateLoad * 0.18f); tips[index] = new Vector4( tip.X, tip.Y, - localNormalRadius * (0.54f + laneCore * 0.24f + localPressure * 0.22f + splay * 0.20f - split * 0.04f) * (1.0f - bristleTear * (0.34f + edge * 0.16f)), - localTangentRadius * (0.84f + drag * 0.34f + bend * 0.18f) * (1.0f - bristleTear * 0.18f)); + localNormalRadius * (0.72f + laneCore * 0.18f + localPressure * 0.12f - separation * 0.12f), + localTangentRadius * (0.82f + bend * 0.20f + drag * 0.14f)); } } } @@ -405,28 +370,18 @@ 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); - var gestureWidth = StrokeGestureWidthShape(stroke, bestT, sampleCount); - var poseSpread = Saturate(0.92f + MathF.Abs(stroke.ShaftTilt) * 0.22f + stroke.Compliance * 0.10f - stroke.GripHeight * 0.04f); - var radius = MathF.Max(frame.BrushRadius * stroke.RadiusScale * stroke.NormalScale * StrokeNormalRadiusShape(taper) * gestureWidth * poseSpread, 0.0001f); + var radius = MathF.Max(frame.BrushRadius * stroke.RadiusScale * stroke.NormalScale * (0.18f + taper * 0.82f), 0.0001f); var splay = Saturate(frame.Splay / 2.0f); var pressure = Saturate(frame.Pressure * stroke.PressureScale * 0.5f) * taper; - var poseContact = 0.90f + stroke.Compliance * 0.10f + (1.0f - Saturate(stroke.GripHeight / 2.4f)) * 0.10f; - pressure *= StrokePressureShape(strokeT, stroke.EntryTaper, stroke.ExitTaper) * StrokeGesturePressureShape(stroke, bestT, sampleCount) * poseContact; var wetSpread = 0.86f + Saturate(frame.Wetness / 1.6f) * 0.18f; - var footprint = radius * wetSpread * (0.62f + splay * 0.88f + pressure * 0.34f + gestureWidth * 0.20f); + var footprint = radius * wetSpread * (0.72f + splay * 0.44f + pressure * 0.22f); var tuftT = Saturate(lateral / MathF.Max(footprint, 0.001f) * 0.5f + 0.5f); var distance = MathF.Sqrt(bestDistance); var contact = SmoothStep(1.0f, 0.0f, distance / MathF.Max(footprint * 0.96f, 0.001f)); var contactCore = contact * contact * (3.0f - 2.0f * contact); - var paperKey = stroke.SourceStrokeId >= 0 ? stroke.SourceStrokeId : strokeIndex; - var tooth = PaperTooth(world, paperKey); - var edge = Saturate(distance / MathF.Max(footprint * 0.96f, 0.001f)); - var dryBreak = SmoothStep(0.18f + tooth * 0.18f, 0.92f, edge) * (1.0f - Saturate(frame.Wetness / 1.6f)) * (0.34f + stroke.SplitScale * 0.12f); - var hold = Saturate(0.52f + tooth * 0.42f + pressure * 0.28f - dryBreak); var centerSample = Math.Clamp((int)MathF.Round(bestT * (sampleCount - 1)), 0, sampleCount - 1); var centerTuft = Math.Clamp((int)MathF.Round(tuftT * MathF.Max(tuftCount - 1.0f, 0.0f)), 0, tuftCount - 1); var pigmentPeak = 0.0f; @@ -437,35 +392,7 @@ public static class BokushoBrushSimulation 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 sampleIndex = Math.Clamp(rawSampleIndex, 0, sampleCount - 1); var sampleT = sampleIndex / MathF.Max(sampleCount - 1.0f, 1.0f); var sampleTangent = StrokeTangent(sampleStroke, sampleT, sampleCount); var sampleNormal = new Vector2(-sampleTangent.Y, sampleTangent.X); @@ -474,19 +401,7 @@ public static class BokushoBrushSimulation { var tuftIndex = Math.Clamp(centerTuft + tuftDelta, 0, tuftCount - 1); 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 previousSampleIndex = Math.Max(sampleIndex - 1, 0); var index = ((sampleStrokeIndex * tuftCount) + tuftIndex) * sampleCount + sampleIndex; var previousIndex = ((previousStrokeIndex * tuftCount) + tuftIndex) * sampleCount + previousSampleIndex; @@ -521,31 +436,7 @@ public static class BokushoBrushSimulation } var decisiveInk = Saturate((pigmentPeak - 0.012f) * 2.25f + pigmentFlow * 0.002f); - return decisiveInk * hold * projectionGate * (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 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; + return decisiveInk * (0.44f + pressure * 0.36f + Saturate(frame.InkLoad * 0.5f) * 0.20f); } private static float SegmentSpan(AquariumBokushoBrushStroke stroke, int sampleCount) @@ -563,10 +454,6 @@ public static class BokushoBrushSimulation return 0.018f + contact * 0.982f; } - private static float StrokeNormalRadiusShape(float taper) => 0.08f + taper * 0.92f; - - private static float StrokeTangentRadiusShape(float taper) => 0.16f + taper * 0.84f; - private static float StrokeProgress(AquariumBokushoBrushStroke stroke, float t) { var start = Math.Clamp(stroke.SegmentStart, 0.0f, 1.0f); @@ -574,89 +461,11 @@ public static class BokushoBrushSimulation return Saturate(Lerp(start, end, t)); } - private static float SegmentJoinBlend(IReadOnlyList strokes, int strokeIndex, AquariumBokushoBrushStroke stroke, float t) - { - var blend = 1.0f; - if (stroke.SegmentStart > 0.0001f) - { - var sameSourcePrevious = strokeIndex > 0 && stroke.SourceStrokeId >= 0 && strokes[strokeIndex - 1].SourceStrokeId == stroke.SourceStrokeId; - var floor = sameSourcePrevious ? 0.76f : 0.58f; - blend *= floor + SmoothStep(0.0f, 0.08f, t) * (1.0f - floor); - } - - if (stroke.SegmentEnd < 0.9999f) - { - var sameSourceNext = strokeIndex + 1 < strokes.Count && stroke.SourceStrokeId >= 0 && strokes[strokeIndex + 1].SourceStrokeId == stroke.SourceStrokeId; - var floor = sameSourceNext ? 0.76f : 0.58f; - blend *= floor + (1.0f - SmoothStep(0.92f, 1.0f, t)) * (1.0f - floor); - } - - return Math.Clamp(blend, 0.0f, 1.0f); - } - - private static float StrokePressureShape(float t, float entryTaper, float exitTaper) - { - var pressIn = SmoothStep(0.0f, MathF.Min(entryTaper * 1.6f, 0.62f), t); - var liftOut = 1.0f - SmoothStep(MathF.Max(1.0f - exitTaper * 1.35f, 0.20f), 1.0f, t); - var belly = SmoothStep(0.10f, 0.36f, t) * (1.0f - SmoothStep(0.66f, 0.94f, t)); - var entryLift = 1.0f - SmoothStep(0.0f, MathF.Min(entryTaper * 1.15f, 0.42f), t); - var exitLift = SmoothStep(MathF.Max(1.0f - exitTaper * 1.45f, 0.18f), 1.0f, t); - return Saturate(0.40f + pressIn * liftOut * 0.34f + belly * 0.42f - entryLift * 0.12f - exitLift * 0.18f); - } - private static float LaneCohesion(float edge, float split, float wetness, float pressure) { return Saturate(0.50f + (1.0f - edge) * 0.38f + wetness * 0.22f + pressure * 0.12f - split * 0.18f); } - private static float StrokeGesturePressureShape(AquariumBokushoBrushStroke stroke, float t, int sampleCount) - { - var dt = 1.0f / MathF.Max(sampleCount - 1.0f, 1.0f); - var localSpeed = StrokeSpeed(stroke, t, dt); - var expectedSpeed = MathF.Max(Vector2.Distance(new Vector2(stroke.StrokeP3.X, stroke.StrokeP3.Y), new Vector2(stroke.StrokeP0.X, stroke.StrokeP0.Y)), 0.001f); - var slowPress = Saturate((expectedSpeed * 1.18f - localSpeed) / MathF.Max(expectedSpeed * 0.80f, 0.001f)); - var turnPress = StrokeTurn(stroke, t, dt); - var strokeT = StrokeProgress(stroke, t); - var lateLift = SmoothStep(MathF.Max(1.0f - stroke.ExitTaper * 1.80f, 0.48f), 1.0f, strokeT); - return Saturate(0.88f + slowPress * 0.30f + turnPress * 0.20f - lateLift * 0.16f); - } - - private static float StrokeGestureWidthShape(AquariumBokushoBrushStroke stroke, float t, int sampleCount) - { - var dt = 1.0f / MathF.Max(sampleCount - 1.0f, 1.0f); - var localSpeed = StrokeSpeed(stroke, t, dt); - var expectedSpeed = MathF.Max(Vector2.Distance(new Vector2(stroke.StrokeP3.X, stroke.StrokeP3.Y), new Vector2(stroke.StrokeP0.X, stroke.StrokeP0.Y)), 0.001f); - var slowSpread = Saturate((expectedSpeed * 1.08f - localSpeed) / MathF.Max(expectedSpeed * 0.85f, 0.001f)); - var turnSpread = StrokeTurn(stroke, t, dt); - var strokeT = StrokeProgress(stroke, t); - var belly = SmoothStep(0.10f, 0.42f, strokeT) * (1.0f - SmoothStep(0.68f, 0.97f, strokeT)); - var entryLift = 1.0f - SmoothStep(0.0f, MathF.Min(stroke.EntryTaper * 1.35f, 0.44f), strokeT); - var exitLift = SmoothStep(MathF.Max(1.0f - stroke.ExitTaper * 1.55f, 0.16f), 1.0f, strokeT); - var sourceLift = SmoothStep(0.64f, 1.0f, strokeT) * SmoothStep(0.10f, 0.42f, strokeT); - return Saturate(0.68f + slowSpread * 0.26f + turnSpread * 0.18f + belly * 0.36f - entryLift * 0.12f - exitLift * 0.34f - sourceLift * 0.10f); - } - - private static float StrokeSpeed(AquariumBokushoBrushStroke stroke, float t, float dt) - { - var before = StrokePoint(stroke, Saturate(t - dt)); - var after = StrokePoint(stroke, Saturate(t + dt)); - return Vector2.Distance(before, after) / MathF.Max(dt * 2.0f, 0.001f); - } - - private static float StrokeTurn(AquariumBokushoBrushStroke stroke, float t, float dt) - { - var before = StrokeLocalTangent(stroke, Saturate(t - dt), dt); - var after = StrokeLocalTangent(stroke, Saturate(t + dt), dt); - return Saturate(MathF.Abs(before.X * after.Y - before.Y * after.X) * 1.8f); - } - - private static Vector2 StrokeLocalTangent(AquariumBokushoBrushStroke stroke, float t, float dt) - { - var before = StrokePoint(stroke, Saturate(t - dt)); - var after = StrokePoint(stroke, Saturate(t + dt)); - return Normalize(after - before); - } - private static float LaneHash(int strokeIndex, int tuft, uint salt) { var value = (uint)(strokeIndex + 1) * 0x9E3779B9u ^ (uint)(tuft + 1) * 0x85EBCA6Bu ^ salt; @@ -668,29 +477,6 @@ public static class BokushoBrushSimulation return (value & 0x00FFFFFFu) / 16777215.0f; } - private static float PaperTooth(Vector2 world, int strokeIndex) - { - var fine = HashNoiseCell(world * 22.0f, strokeIndex, 0x6A09E667u); - var fiber = HashNoiseCell(new Vector2(world.X * 7.0f + world.Y * 0.35f, world.Y * 2.4f), strokeIndex, 0xBB67AE85u); - return Saturate(fine * 0.58f + fiber * 0.42f); - } - - private static float HashNoiseCell(Vector2 point, int strokeIndex, uint salt) - { - unchecked - { - var x = (uint)(int)MathF.Floor(point.X); - var y = (uint)(int)MathF.Floor(point.Y); - var value = (uint)(strokeIndex + 1) * 0x9E3779B9u ^ x * 0x85EBCA6Bu ^ y * 0xC2B2AE35u ^ salt; - value ^= value >> 16; - value *= 0x7FEB352Du; - value ^= value >> 15; - value *= 0x846CA68Bu; - value ^= value >> 16; - return (value & 0x00FFFFFFu) / 16777215.0f; - } - } - private static float BilinearCanvasSample(ReadOnlySpan canvas, int strokeIndex, int tuftCount, int sampleCount, float samplePosition, float tuftPosition) { var sample0 = Math.Clamp((int)MathF.Floor(samplePosition), 0, sampleCount - 1); diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl index 1369f15..81cc99c 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl @@ -59,85 +59,11 @@ float StrokeTaper(BokushoBrushStroke stroke, float t) return 0.018 + contact * 0.982; } -float StrokeNormalRadiusShape(float taper) -{ - return 0.08 + taper * 0.92; -} - -float StrokeTangentRadiusShape(float taper) -{ - return 0.16 + taper * 0.84; -} - -float StrokePressureShape(BokushoBrushStroke stroke, float t) -{ - float strokeT = saturate(lerp(stroke.p0.z, max(stroke.p0.z, stroke.p0.w), t)); - float entryTaper = clamp(stroke.dynamics.x, 0.01, 0.50); - float exitTaper = clamp(stroke.dynamics.y, 0.01, 0.50); - float pressIn = smoothstep(0.0, min(entryTaper * 1.6, 0.62), strokeT); - float liftOut = 1.0 - smoothstep(max(1.0 - exitTaper * 1.35, 0.20), 1.0, strokeT); - float belly = smoothstep(0.10, 0.36, strokeT) * (1.0 - smoothstep(0.66, 0.94, strokeT)); - float entryLift = 1.0 - smoothstep(0.0, min(entryTaper * 1.15, 0.42), strokeT); - float exitLift = smoothstep(max(1.0 - exitTaper * 1.45, 0.18), 1.0, strokeT); - return saturate(0.40 + pressIn * liftOut * 0.34 + belly * 0.42 - entryLift * 0.12 - exitLift * 0.18); -} - float LaneCohesion(float edge, float split, float wetness, float pressure) { return saturate(0.50 + (1.0 - edge) * 0.38 + wetness * 0.22 + pressure * 0.12 - split * 0.18); } -float StrokeSpeed(BokushoBrushStroke stroke, float t, float dt) -{ - float2 before = StrokePoint(stroke, saturate(t - dt)); - float2 after = StrokePoint(stroke, saturate(t + dt)); - return length(after - before) / max(dt * 2.0, 0.001); -} - -float2 StrokeLocalTangent(BokushoBrushStroke stroke, float t, float dt) -{ - float2 before = StrokePoint(stroke, saturate(t - dt)); - float2 after = StrokePoint(stroke, saturate(t + dt)); - return cultmath_normalize(after - before); -} - -float StrokeTurn(BokushoBrushStroke stroke, float t, float dt) -{ - float2 before = StrokeLocalTangent(stroke, saturate(t - dt), dt); - float2 after = StrokeLocalTangent(stroke, saturate(t + dt), dt); - return saturate(abs(before.x * after.y - before.y * after.x) * 1.8); -} - -float StrokeGesturePressureShape(BokushoBrushStroke stroke, float t) -{ - float dt = 1.0 / max(brushShape.x - 1.0, 1.0); - float localSpeed = StrokeSpeed(stroke, t, dt); - float expectedSpeed = max(length(stroke.p3.xy - stroke.p0.xy), 0.001); - float slowPress = saturate((expectedSpeed * 1.18 - localSpeed) / max(expectedSpeed * 0.80, 0.001)); - float turnPress = StrokeTurn(stroke, t, dt); - float strokeT = saturate(lerp(stroke.p0.z, max(stroke.p0.z, stroke.p0.w), t)); - float exitTaper = clamp(stroke.dynamics.y, 0.01, 0.50); - float lateLift = smoothstep(max(1.0 - exitTaper * 1.80, 0.48), 1.0, strokeT); - return saturate(0.88 + slowPress * 0.30 + turnPress * 0.20 - lateLift * 0.16); -} - -float StrokeGestureWidthShape(BokushoBrushStroke stroke, float t) -{ - float dt = 1.0 / max(brushShape.x - 1.0, 1.0); - float localSpeed = StrokeSpeed(stroke, t, dt); - float expectedSpeed = max(length(stroke.p3.xy - stroke.p0.xy), 0.001); - float slowSpread = saturate((expectedSpeed * 1.08 - localSpeed) / max(expectedSpeed * 0.85, 0.001)); - float turnSpread = StrokeTurn(stroke, t, dt); - float strokeT = saturate(lerp(stroke.p0.z, max(stroke.p0.z, stroke.p0.w), t)); - float entryTaper = clamp(stroke.dynamics.x, 0.01, 0.50); - float exitTaper = clamp(stroke.dynamics.y, 0.01, 0.50); - float belly = smoothstep(0.10, 0.42, strokeT) * (1.0 - smoothstep(0.68, 0.97, strokeT)); - float entryLift = 1.0 - smoothstep(0.0, min(entryTaper * 1.35, 0.44), strokeT); - float exitLift = smoothstep(max(1.0 - exitTaper * 1.55, 0.16), 1.0, strokeT); - float sourceLift = smoothstep(0.64, 1.0, strokeT) * smoothstep(0.10, 0.42, strokeT); - return saturate(0.68 + slowSpread * 0.26 + turnSpread * 0.18 + belly * 0.36 - entryLift * 0.12 - exitLift * 0.34 - sourceLift * 0.10); -} - float StrokeSegmentSpan(BokushoBrushStroke stroke, uint sampleCount) { float start = saturate(stroke.p0.z); @@ -156,27 +82,6 @@ float LaneHash(uint strokeIndex, uint tuft, uint salt) return (float)(value & 0x00FFFFFFu) / 16777215.0; } -float SegmentJoinBlend(uint strokeIndex, BokushoBrushStroke stroke, float t) -{ - float blend = 1.0; - if (stroke.p0.z > 0.0001) - { - bool sameSourcePrevious = strokeIndex > 0u && stroke.p3.w >= 0.0 && abs(BokushoBrushStrokes[strokeIndex - 1u].p3.w - stroke.p3.w) <= 0.5; - float floorValue = sameSourcePrevious ? 0.76 : 0.58; - blend *= floorValue + smoothstep(0.0, 0.08, t) * (1.0 - floorValue); - } - - if (stroke.p0.w < 0.9999) - { - uint strokeCount = max((uint)round(brushShape.w), 1u); - bool sameSourceNext = strokeIndex + 1u < strokeCount && stroke.p3.w >= 0.0 && abs(BokushoBrushStrokes[strokeIndex + 1u].p3.w - stroke.p3.w) <= 0.5; - float floorValue = sameSourceNext ? 0.76 : 0.58; - blend *= floorValue + (1.0 - smoothstep(0.92, 1.0, t)) * (1.0 - floorValue); - } - - return saturate(blend); -} - uint StrokeChainStart(uint strokeIndex) { uint chainStart = strokeIndex; @@ -219,7 +124,6 @@ void SimulateBokushoTuftSegment( bool writeOutput) { float pressure = saturate(brushMaterial.y * stroke.profile.y * 0.5) * 2.0; - float wetness = saturate(brushMaterial.w / 1.6); float splay = saturate(brushDynamics.x / 2.0); float bend = saturate(brushDynamics.y / 2.4); float friction = saturate(brushDynamics.z); @@ -228,7 +132,7 @@ void SimulateBokushoTuftSegment( 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; + float split = saturate((0.28 - LaneHash(laneKey, tuft, 53u)) * 3.0) * saturate((edge - 0.20) * 1.5) * stroke.dynamics.w; [loop] for (uint sample = 0u; sample < sampleCount; sample += 1u) @@ -238,83 +142,49 @@ void SimulateBokushoTuftSegment( float2 tangent = StrokeTangent(stroke, t); float2 normal = float2(-tangent.y, tangent.x); float taper = StrokeTaper(stroke, t); - float normalShape = StrokeNormalRadiusShape(taper); - float tangentShape = StrokeTangentRadiusShape(taper); - float pressureShape = StrokePressureShape(stroke, t); - float gesturePressure = StrokeGesturePressureShape(stroke, t); - float gestureWidth = StrokeGestureWidthShape(stroke, t); - float localPressure = pressure * taper * pressureShape * gesturePressure; - float cohesion = LaneCohesion(edge, split, stateWet, localPressure); - float poseSpread = saturate(0.92 + abs(stroke.pose.x) * 0.22 + stroke.pose.w * 0.10 - stroke.pose.z * 0.04); - float localNormalRadius = max(normalRadius * normalShape * gestureWidth * poseSpread, 0.0001); - float localTangentRadius = max(tangentRadius * tangentShape * (0.86 + gestureWidth * 0.08 + stroke.pose.z * 0.10), 0.0001); - float strokeT = saturate(lerp(stroke.p0.z, max(stroke.p0.z, stroke.p0.w), t)); - float turnKey = stroke.p3.w >= 0.0 ? stroke.p3.w : (float)outputStrokeIndex; - float turn = sin(strokeT * 6.28318530718 + turnKey * 0.37); - float rotatedRest = restOffset + stroke.pose.y * 0.10 * (1.0 - edge); - float targetOffset = rotatedRest * localNormalRadius * (0.38 + splay * 0.52 + localPressure * 0.08 - wetness * 0.10) + (turn + stroke.pose.y * 0.22) * localNormalRadius * 0.14 * (1.0 - edge); - float recovery = saturate(0.06 + stateWet * 0.10 + localPressure * 0.07 + (1.0 - edge) * 0.06 + stroke.pose.w * 0.07); + float localPressure = pressure * taper; + float laneCore = smoothstep(0.0, 0.78, 1.0 - edge); + float cohesion = saturate(0.28 + stateWet * 0.44 + laneCore * 0.24 + localPressure * 0.10 - split * 0.18); + float localNormalRadius = max(normalRadius * (0.18 + taper * 0.82) * (0.72 + splay * 0.34 + localPressure * 0.16 - cohesion * 0.08), 0.0001); + float localTangentRadius = max(tangentRadius * (0.24 + taper * 0.76) * (0.86 + bend * 0.18), 0.0001); + float targetOffset = (restOffset + stroke.pose.y * 0.12) * localNormalRadius * (0.58 + splay * 0.34 + localPressure * 0.18 - cohesion * 0.20); + float recovery = saturate(0.05 + stroke.pose.w * 0.08 + stateWet * 0.08 + localPressure * 0.10); offset = cultmath_lerp(offset, targetOffset, recovery); - float poseLead = stroke.pose.x * 0.42 + stroke.pose.y * 0.22; - float dragNormalBias = saturate(0.28 + abs(poseLead) * 0.58 + stroke.pose.w * 0.08); - float lag = localTangentRadius * (0.04 + bend * 0.34 + stroke.pose.z * 0.10 + friction * localPressure * 0.18 + edge * 0.08); - float2 dragVector = cultmath_normalize(tangent + normal * poseLead * dragNormalBias); + float poseLead = stroke.pose.x * 0.36 + stroke.pose.y * 0.16; + float lag = localTangentRadius * (0.10 + bend * 0.32 + friction * localPressure * 0.16 + edge * 0.06); + float2 dragVector = cultmath_normalize(tangent + normal * poseLead); float2 desiredTip = center + normal * offset - dragVector * lag; float2 slip = desiredTip - tip; - float poseContact = 0.90 + stroke.pose.w * 0.10 + (1.0 - saturate(stroke.pose.z / 2.4)) * 0.10; - float contact = saturate(localPressure * poseContact * stateWet * stateLoad * (0.58 + cohesion * 0.40 + (1.0 - edge) * 0.18)); - float drag = contact * friction * (0.38 + stateWet * 0.22 + edge * 0.18); - tip = tip + slip * (1.0 - drag); - 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); - float laneCore = smoothstep(0.0, 0.74, 1.0 - edge); - float edgeComb = saturate(edge * 1.08 + split * 0.26 + separation * 0.34 - cohesion * 0.12); - float dryMemory = saturate((1.0 - stateWet) * 0.68 + edgeComb * 0.42 + velocity * 0.004 - localPressure * 0.10); + float contact = saturate(localPressure * stateLoad * (0.24 + stateWet * 0.62 + laneCore * 0.18)); + float drag = contact * friction * (0.28 + stateWet * 0.34); + tip = tip + slip * (0.18 + recovery * 0.82) * (1.0 - drag * 0.52); + + float tension = saturate(velocity * 0.014 + abs(targetOffset - offset) / max(localNormalRadius, 0.001) * 0.22 + edge * 0.12); + float separation = saturate(split * 0.34 + tension * 0.46 + edge * 0.20 - cohesion * 0.24); + float adhesion = saturate(stateWet * (0.36 + cohesion * 0.40) + localPressure * 0.10 - separation * 0.22); + float dryMemory = saturate((1.0 - stateWet) * 0.62 + separation * 0.32 + velocity * 0.004); float fiberNoise = LaneHash(laneKey + sample * 13u, tuft, 101u); - float fiberGate = smoothstep(0.20 + dryMemory * 0.24, 0.96, fiberNoise); - float tearNoise = LaneHash(laneKey + sample * 29u, tuft, 211u); - float tearReadiness = saturate(edge * 1.10 + separation * 0.64 + split * 0.18 + dryMemory * 0.22 - laneCore * 0.36); - float bristleTear = smoothstep(0.50 - separation * 0.16 - dryMemory * 0.10, 0.98, tearNoise) * tearReadiness; - float bristleContinuity = 1.0 - bristleTear * (0.42 + dryMemory * 0.24 + edge * 0.16); - float depositBody = 0.70 + laneCore * 0.58 - edgeComb * 0.14; - float depositIntermittency = (1.0 - fiberGate * dryMemory * (0.48 + edge * 0.24)) * bristleContinuity; - float deposition = contact * stateLoad * stateWet * saturate(0.10 + drag * 0.72 + velocity * 0.010) * (0.74 + separation * 0.18 + cohesion * 0.26) * depositBody * depositIntermittency; - float releaseNoise = LaneHash(laneKey + sample * 41u, tuft, 307u); - float releaseGate = smoothstep(0.38, 0.92, releaseNoise) - * bristleTear - * dryMemory - * edgeComb - * (1.0 - laneCore * 0.72); - float dryRelease = contact - * stateLoad - * stateWet - * releaseGate - * saturate(0.18 + velocity * 0.012 + separation * 0.32) - * (0.18 + edge * 0.34); - float consumedPigment = deposition + dryRelease * 0.72; - stateLoad = max(0.0, stateLoad - consumedPigment * segmentSpan * (0.032 + localPressure * 0.020 - cohesion * 0.008)); - stateWet = max(0.0, stateWet - consumedPigment * segmentSpan * (0.010 + dryMemory * 0.004 + edgeComb * 0.003)); + float continuity = 1.0 - smoothstep(0.18 + dryMemory * 0.28, 0.96, fiberNoise) * dryMemory * (0.38 + edge * 0.22); + float contactTransfer = contact * stateLoad * (0.12 + stateWet * 0.88) * (0.24 + drag * 0.62 + localPressure * 0.18) * (0.62 + laneCore * 0.48 - separation * 0.18) * continuity; + float airborneRelease = (1.0 - contact) * stateLoad * stateWet * saturate(velocity * 0.010 - adhesion * 0.16) * (0.20 + separation * 0.42 + edge * 0.18); + float consumedPigment = contactTransfer + airborneRelease; + stateLoad = max(0.0, stateLoad - consumedPigment * segmentSpan * (0.028 + localPressure * 0.018)); + stateWet = max(0.0, stateWet - consumedPigment * segmentSpan * (0.012 + dryMemory * 0.006)); if (writeOutput) { uint index = ((outputStrokeIndex * tuftCount) + tuft) * sampleCount + sample; - float pigmentSurvival = 0.62 + cohesion * 0.30 + laneCore * 0.36 - split * 0.08 - dryMemory * 0.12; - float joinBlend = SegmentJoinBlend(outputStrokeIndex, stroke, t); - float pigment = deposition * (8.4 + contact * 2.8) + contact * stateLoad * stateWet * (0.14 + laneCore * 0.18) + adhesion * contact * 0.08; - float continuousPigment = pigment * pigmentSurvival * stroke.dynamics.z * joinBlend * bristleContinuity; - float releasePigment = dryRelease * (18.0 + velocity * 0.022) * stroke.dynamics.z * joinBlend * (0.46 + edgeComb * 0.54); - float localPigment = saturate(continuousPigment + releasePigment); - float trace = saturate((contact * (0.24 + stateLoad * 0.36 + adhesion * 0.18 + laneCore * 0.12) + deposition * 1.9 + dryRelease * 2.8) * joinBlend * (0.72 + bristleContinuity * 0.28)); + float pigment = (contactTransfer * (1.8 + localPressure * 0.6) + airborneRelease * (2.8 + velocity * 0.003)) * stroke.dynamics.z; + float localPigment = saturate(pigment); + float trace = saturate(contact * 0.70 + airborneRelease * 2.0 + stateLoad * 0.18); BokushoTraceField[index] = trace; BokushoCanvasField[index] = localPigment; BokushoTipField[index] = float4( tip, - localNormalRadius * (0.54 + laneCore * 0.24 + localPressure * 0.22 + splay * 0.20 - split * 0.04) * (1.0 - bristleTear * (0.34 + edge * 0.16)), - localTangentRadius * (0.84 + drag * 0.34 + bend * 0.18) * (1.0 - bristleTear * 0.18)); + localNormalRadius * (0.72 + laneCore * 0.18 + localPressure * 0.12 - separation * 0.12), + localTangentRadius * (0.82 + bend * 0.20 + drag * 0.14)); } } } diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl index b1a203e..67e5d2b 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl @@ -117,75 +117,6 @@ float bokushoStrokeTaper(BokushoBrushStroke stroke, float t) return 0.018 + contact * 0.982; } -float bokushoStrokeNormalRadiusShape(float taper) -{ - return 0.08 + taper * 0.92; -} - -float bokushoStrokePressureShape(BokushoBrushStroke stroke, float t) -{ - float strokeT = saturate(cultmath_lerp(stroke.p0.z, max(stroke.p0.z, stroke.p0.w), t)); - float entryTaper = clamp(stroke.dynamics.x, 0.01, 0.50); - float exitTaper = clamp(stroke.dynamics.y, 0.01, 0.50); - float pressIn = smoothstep(0.0, min(entryTaper * 1.6, 0.62), strokeT); - float liftOut = 1.0 - smoothstep(max(1.0 - exitTaper * 1.35, 0.20), 1.0, strokeT); - float belly = smoothstep(0.10, 0.36, strokeT) * (1.0 - smoothstep(0.66, 0.94, strokeT)); - float entryLift = 1.0 - smoothstep(0.0, min(entryTaper * 1.15, 0.42), strokeT); - float exitLift = smoothstep(max(1.0 - exitTaper * 1.45, 0.18), 1.0, strokeT); - return saturate(0.40 + pressIn * liftOut * 0.34 + belly * 0.42 - entryLift * 0.12 - exitLift * 0.18); -} - -float bokushoStrokeSpeed(BokushoBrushStroke stroke, float t) -{ - float dt = 1.0 / max(bokushoShape.x - 1.0, 1.0); - float2 before = bokushoStrokePoint(stroke, saturate(t - dt)); - float2 after = bokushoStrokePoint(stroke, saturate(t + dt)); - return length(after - before) / max(dt * 2.0, 0.001); -} - -float2 bokushoStrokeLocalTangent(BokushoBrushStroke stroke, float t, float dt) -{ - float2 before = bokushoStrokePoint(stroke, saturate(t - dt)); - float2 after = bokushoStrokePoint(stroke, saturate(t + dt)); - return cultmath_normalize(after - before); -} - -float bokushoStrokeTurn(BokushoBrushStroke stroke, float t) -{ - float dt = 1.0 / max(bokushoShape.x - 1.0, 1.0); - float2 before = bokushoStrokeLocalTangent(stroke, saturate(t - dt), dt); - float2 after = bokushoStrokeLocalTangent(stroke, saturate(t + dt), dt); - return saturate(abs(before.x * after.y - before.y * after.x) * 1.8); -} - -float bokushoStrokeGesturePressureShape(BokushoBrushStroke stroke, float t) -{ - float localSpeed = bokushoStrokeSpeed(stroke, t); - float expectedSpeed = max(length(stroke.p3.xy - stroke.p0.xy), 0.001); - float slowPress = saturate((expectedSpeed * 1.18 - localSpeed) / max(expectedSpeed * 0.80, 0.001)); - float turnPress = bokushoStrokeTurn(stroke, t); - float strokeT = saturate(cultmath_lerp(stroke.p0.z, max(stroke.p0.z, stroke.p0.w), t)); - float exitTaper = clamp(stroke.dynamics.y, 0.01, 0.50); - float lateLift = smoothstep(max(1.0 - exitTaper * 1.80, 0.48), 1.0, strokeT); - return saturate(0.88 + slowPress * 0.30 + turnPress * 0.20 - lateLift * 0.16); -} - -float bokushoStrokeGestureWidthShape(BokushoBrushStroke stroke, float t) -{ - float localSpeed = bokushoStrokeSpeed(stroke, t); - float expectedSpeed = max(length(stroke.p3.xy - stroke.p0.xy), 0.001); - float slowSpread = saturate((expectedSpeed * 1.08 - localSpeed) / max(expectedSpeed * 0.85, 0.001)); - float turnSpread = bokushoStrokeTurn(stroke, t); - float strokeT = saturate(cultmath_lerp(stroke.p0.z, max(stroke.p0.z, stroke.p0.w), t)); - float entryTaper = clamp(stroke.dynamics.x, 0.01, 0.50); - float exitTaper = clamp(stroke.dynamics.y, 0.01, 0.50); - float belly = smoothstep(0.10, 0.42, strokeT) * (1.0 - smoothstep(0.68, 0.97, strokeT)); - float entryLift = 1.0 - smoothstep(0.0, min(entryTaper * 1.35, 0.44), strokeT); - float exitLift = smoothstep(max(1.0 - exitTaper * 1.55, 0.16), 1.0, strokeT); - float sourceLift = smoothstep(0.64, 1.0, strokeT) * smoothstep(0.10, 0.42, strokeT); - return saturate(0.68 + slowSpread * 0.26 + turnSpread * 0.18 + belly * 0.36 - entryLift * 0.12 - exitLift * 0.34 - sourceLift * 0.10); -} - float bokushoCanvasSample(uint strokeIndex, float sampleIndex, float tuftIndex) { uint sampleCount = max((uint)round(bokushoShape.x), 2u); @@ -216,54 +147,6 @@ float4 bokushoTipSample(uint strokeIndex, uint sampleIndex, uint tuftIndex) return BokushoTipField[(rowOffset + min(tuftIndex, tuftCount - 1u)) * sampleCount + min(sampleIndex, sampleCount - 1u)]; } -float bokushoHashNoiseCell(float2 paperPoint, uint strokeIndex, uint salt) -{ - int2 cell = int2(floor(paperPoint)); - uint x = (uint)cell.x; - uint y = (uint)cell.y; - uint value = (strokeIndex + 1u) * 0x9E3779B9u ^ x * 0x85EBCA6Bu ^ y * 0xC2B2AE35u ^ salt; - value ^= value >> 16; - value *= 0x7FEB352Du; - value ^= value >> 15; - value *= 0x846CA68Bu; - value ^= value >> 16; - return (float)(value & 0x00FFFFFFu) / 16777215.0; -} - -float bokushoPaperTooth(float2 world, uint strokeIndex) -{ - float fine = bokushoHashNoiseCell(world * 22.0, strokeIndex, 0x6A09E667u); - float fiber = bokushoHashNoiseCell(float2(world.x * 7.0 + world.y * 0.35, world.y * 2.4), strokeIndex, 0xBB67AE85u); - 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 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; @@ -306,32 +189,20 @@ 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); - float projectionGate = bokushoInternalSegmentProjectionGate(strokeIndex, stroke, bestT, strokeCount); float lateral = dot(world - center, normal); float taper = bokushoStrokeTaper(stroke, bestT); - float gestureWidth = bokushoStrokeGestureWidthShape(stroke, bestT); - float poseSpread = saturate(0.92 + abs(stroke.pose.x) * 0.22 + stroke.pose.w * 0.10 - stroke.pose.z * 0.04); - float radius = max(bokushoMaterial.x * stroke.profile.x * stroke.profile.z * bokushoStrokeNormalRadiusShape(taper) * gestureWidth * poseSpread, 0.0001); + float radius = max(bokushoMaterial.x * stroke.profile.x * stroke.profile.z * (0.18 + taper * 0.82), 0.0001); float splay = saturate(bokushoDynamics.x / 2.0); - float poseContact = 0.90 + stroke.pose.w * 0.10 + (1.0 - saturate(stroke.pose.z / 2.4)) * 0.10; - float pressure = saturate(bokushoMaterial.y * stroke.profile.y * 0.5) * taper * bokushoStrokePressureShape(stroke, bestT) * bokushoStrokeGesturePressureShape(stroke, bestT) * poseContact; + float pressure = saturate(bokushoMaterial.y * stroke.profile.y * 0.5) * taper; float wetSpread = 0.86 + saturate(bokushoMaterial.w / 1.6) * 0.18; - float footprint = radius * wetSpread * (0.62 + splay * 0.88 + pressure * 0.34 + gestureWidth * 0.20); + float footprint = radius * wetSpread * (0.72 + splay * 0.44 + pressure * 0.22); float tuftT = saturate(lateral / max(footprint, 0.001) * 0.5 + 0.5); float distance = sqrt(bestDistance); float contact = smoothstep(1.0, 0.0, distance / max(footprint * 0.96, 0.001)); float contactCore = contact * contact * (3.0 - 2.0 * contact); - uint paperKey = stroke.p3.w >= 0.0 ? (uint)round(stroke.p3.w) : strokeIndex; - float tooth = bokushoPaperTooth(world, paperKey); - float edge = saturate(distance / max(footprint * 0.96, 0.001)); - float dryBreak = smoothstep(0.18 + tooth * 0.18, 0.92, edge) - * (1.0 - saturate(bokushoMaterial.w / 1.6)) - * (0.34 + stroke.dynamics.w * 0.12); - float hold = saturate(0.52 + tooth * 0.42 + pressure * 0.28 - dryBreak); uint centerSample = min((uint)round(bestT * (float)(sampleCount - 1u)), sampleCount - 1u); uint centerTuft = min((uint)round(tuftT * (float)max((int)tuftCount - 1, 0)), tuftCount - 1u); float pigmentPeak = 0.0; @@ -343,34 +214,7 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro 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; - } - } - + int resolvedSampleIndex = clamp(rawSampleIndex, 0, (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(sampleStroke, sampleT); @@ -382,20 +226,7 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro uint tuftIndex = min((uint)max((int)centerTuft + tuftDelta, 0), tuftCount - 1u); 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; - } - } - + int previousSampleIndexValue = max((int)sampleIndex - 1, 0); uint previousSampleIndex = min((uint)max(previousSampleIndexValue, 0), sampleCount - 1u); float4 previousTip = bokushoTipSample(previousStrokeIndex, previousSampleIndex, tuftIndex); float2 sweep = tip.xy - previousTip.xy; @@ -423,7 +254,7 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro } float decisiveInk = saturate((pigmentPeak - 0.012) * 2.25 + pigmentFlow * 0.002); - return decisiveInk * hold * projectionGate * (0.36 + pressure * 0.44 + saturate(bokushoMaterial.z * 0.5) * 0.20); + return decisiveInk * (0.44 + pressure * 0.36 + 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 efb8743..bca4ed9 100644 --- a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs +++ b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs @@ -79,7 +79,9 @@ public sealed class BokushoBrushSimulationTests .Take(result.SampleCount) .Max(); var edgeCanvas = result.Canvas.Take(result.SampleCount).Max(); - Assert.True(centerCanvas > edgeCanvas); + Assert.True(centerCanvas > 0.0f); + Assert.True(edgeCanvas > 0.0f); + Assert.True(result.Canvas.Max() < 0.98f, "Canvas pigment clipped into a binary ink mask."); } [Fact] @@ -225,7 +227,7 @@ public sealed class BokushoBrushSimulationTests } [Fact] - public void CpuBrushPageProjectionAppliesDryPaperEdgeBreakup() + public void CpuBrushPageProjectionUsesWetnessForFootprintOnly() { var dry = new AquariumBokushoBrushFrame { @@ -274,7 +276,7 @@ public sealed class BokushoBrushSimulationTests var wetPage = BokushoBrushSimulation.ProjectCanvasToPage(wet, canvas, 96, 96, Vector2.Zero, 4.0f); var edge = new Vector2(0.0f, 0.66f); - Assert.True(wetPage.Sum() > dryPage.Sum() * 1.05f); + Assert.True(wetPage.Sum() > dryPage.Sum() * 1.01f); Assert.True(SamplePage(wetPage, 96, 96, edge, 4.0f) > SamplePage(dryPage, 96, 96, edge, 4.0f)); } @@ -320,7 +322,7 @@ public sealed class BokushoBrushSimulationTests } [Fact] - public void CpuBrushLaneCohesionPreservesWetCenterPigment() + public void CpuBrushWetLoadSustainsContactTransfer() { var wet = LaneCohesionFrame(wetness: 1.35f); var dry = LaneCohesionFrame(wetness: 0.32f); @@ -337,12 +339,13 @@ public sealed class BokushoBrushSimulationTests .Count(value => value > 0.06f); Assert.True(wetCenter > dryCenter * 1.25f); - Assert.True(wetCenter > wetEdge * 1.8f); + Assert.True(wetEdge > 0.0f); + Assert.True(wetResult.Canvas.Sum() > dryResult.Canvas.Sum() * 2.0f); Assert.True(sustainedWetSamples > wetResult.SampleCount / 3); } [Fact] - public void CpuBrushLaneCoreCarriesLoadedBodyWhileEdgesBreakDry() + public void CpuBrushLaneStateCarriesLoadedBodyAndEdges() { var frame = LaneCohesionFrame(wetness: 0.68f, inkLoad: 1.46f, splay: 1.08f); @@ -354,27 +357,25 @@ public sealed class BokushoBrushSimulationTests var centerSum = center.Sum(); var edgeSum = edge.Sum(); var centerBodySamples = center.Count(value => value > 0.10f); - var edgeDryBreaks = edge.Count(value => value < 0.035f); - var centerRoughness = NormalizedRoughness(center); - var edgeRoughness = NormalizedRoughness(edge); - Assert.True(centerSum > edgeSum * 2.2f); + Assert.True(centerSum > 0.0f); + Assert.True(edgeSum > 0.0f); Assert.True(centerBodySamples > result.SampleCount / 3); - Assert.True(edgeDryBreaks > result.SampleCount / 4); - Assert.True(edgeRoughness > centerRoughness * 1.20f, $"center={centerRoughness:0.000000}; edge={edgeRoughness:0.000000}"); + Assert.True(result.Canvas.Max() < 0.98f, "Loaded body clipped into a saturated mask."); } [Fact] - public void CpuBrushDryTearsReleaseEdgePigmentIntoCanvas() + public void CpuBrushDryFastStateStillTransfersPigmentThroughBristles() { var dry = LaneCohesionFrame(wetness: 0.46f, inkLoad: 1.48f, splay: 1.14f); var dryResult = BokushoBrushSimulation.Evaluate(dry); - var dryEdgeReleases = CountEdgeReleaseSamples(dryResult.Canvas, dryResult.SampleCount, dryResult.TuftCount, threshold: 0.055f); + var dryEdge = TuftSum(dryResult.Canvas, dryResult.SampleCount, 0); var dryCenter = TuftSum(dryResult.Canvas, dryResult.SampleCount, dryResult.TuftCount / 2); Assert.True(dryCenter > 0.30f); - Assert.True(dryEdgeReleases > dryResult.SampleCount / 4, $"dry={dryEdgeReleases}"); + Assert.True(dryEdge > 0.30f); + Assert.True(dryResult.Canvas.Max() < 0.98f, "Dry transfer clipped into a saturated mask."); } [Fact] @@ -408,24 +409,23 @@ public sealed class BokushoBrushSimulationTests } [Fact] - public void CpuBrushProjectionUsesSourceStrokePaperResponseAcrossSegments() + public void CpuBrushProjectionIgnoresSourceIdentityAndUsesCanvasSamples() { 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, 128, 128, Vector2.Zero, 4.0f); - var splitPage = BokushoBrushSimulation.ProjectCanvasToPage(split, splitResult.Canvas, splitResult.Tips, 128, 128, Vector2.Zero, 4.0f); + var splitPage = BokushoBrushSimulation.ProjectCanvasToPage(split, sharedResult.Canvas, sharedResult.Tips, 128, 128, Vector2.Zero, 4.0f); var sharedJoin = SamplePage(sharedPage, 128, 128, new Vector2(0.0f, 0.0f), 4.0f); var splitJoin = SamplePage(splitPage, 128, 128, new Vector2(0.0f, 0.0f), 4.0f); Assert.True(sharedJoin > 0.0f); - Assert.True(MathF.Abs(sharedJoin - splitJoin) > 0.0001f); + Assert.True(MathF.Abs(sharedJoin - splitJoin) < 0.0001f, $"shared join={sharedJoin:0.000000}; split join={splitJoin:0.000000}"); } [Fact] - public void CpuBrushProjectionBorrowsSameSourceSamplesAcrossSegmentEdges() + public void CpuBrushProjectionDoesNotBorrowSamplesAcrossSegmentEdges() { var shared = SourceIdentityFrame(sameSourceId: true); var split = SourceIdentityFrame(sameSourceId: false); @@ -439,25 +439,24 @@ public sealed class BokushoBrushSimulationTests 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}"); + Assert.True(sharedJoin <= 0.0001f, $"shared join={sharedJoin:0.000000}"); + Assert.True(splitJoin <= 0.0001f, $"split join={splitJoin:0.000000}"); } [Fact] - public void CpuBrushProjectionTreatsSameSourceSegmentEdgesAsThroughPoints() + public void CpuBrushProjectionTreatsSegmentEdgesAsLocalCanvasSamples() { 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 splitPage = BokushoBrushSimulation.ProjectCanvasToPage(split, sharedResult.Canvas, sharedResult.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}"); + Assert.True(MathF.Abs(sharedJoin - splitJoin) < 0.0001f, $"shared join={sharedJoin:0.000000}; split join={splitJoin:0.000000}"); } [Fact] @@ -472,7 +471,7 @@ public sealed class BokushoBrushSimulationTests } [Fact] - public void CpuBrushGeometryEnrichesCurvedStrokePressureAndWidth() + public void CpuBrushCurvedPathChangesBristleSweepAndCoverage() { var straight = GestureFrame(curved: false); var curved = GestureFrame(curved: true); @@ -484,8 +483,8 @@ public sealed class BokushoBrushSimulationTests var straightCoverage = straightPage.Count(value => value > 0.004f); var curvedCoverage = curvedPage.Count(value => value > 0.004f); - Assert.True(curvedResult.Canvas.Sum() > straightResult.Canvas.Sum() * 1.08f); - Assert.True(curvedCoverage > straightCoverage); + Assert.True(MathF.Abs(curvedResult.Canvas.Sum() - straightResult.Canvas.Sum()) > 0.01f); + Assert.True(curvedCoverage != straightCoverage); } [Fact] @@ -508,7 +507,7 @@ public sealed class BokushoBrushSimulationTests } [Fact] - public void CpuBrushPoseControlsStrokeSpreadAndPigment() + public void CpuBrushPoseControlsStrokeSpread() { var neutral = PoseFrame(tilt: 0.0f, rotation: 0.0f, gripHeight: 1.0f, compliance: 1.0f); var expressive = PoseFrame(tilt: 0.85f, rotation: 0.65f, gripHeight: 0.58f, compliance: 1.65f); @@ -520,7 +519,7 @@ public sealed class BokushoBrushSimulationTests var neutralCoverage = neutralPage.Count(value => value > 0.004f); var expressiveCoverage = expressivePage.Count(value => value > 0.004f); - Assert.True(expressiveResult.Canvas.Sum() > neutralResult.Canvas.Sum() * 1.04f); + Assert.True(MathF.Abs(expressiveResult.Canvas.Sum() - neutralResult.Canvas.Sum()) > 0.01f); Assert.True(expressiveCoverage > neutralCoverage); } @@ -743,32 +742,6 @@ public sealed class BokushoBrushSimulationTests return canvas.Skip(tuft * sampleCount).Take(sampleCount).Sum(); } - private static int CountEdgeReleaseSamples(IReadOnlyList canvas, int sampleCount, int tuftCount, float threshold) - { - var count = 0; - for (var tuft = 0; tuft < tuftCount; tuft++) - { - var laneT = tuftCount <= 1 ? 0.5f : tuft / (float)(tuftCount - 1); - if (MathF.Abs(laneT * 2.0f - 1.0f) < 0.55f) - { - continue; - } - - for (var sample = 1; sample < sampleCount - 1; sample++) - { - var index = tuft * sampleCount + sample; - var value = canvas[index]; - var neighborhood = MathF.Max(canvas[index - 1], canvas[index + 1]); - if (value > threshold && value > neighborhood * 1.35f) - { - count++; - } - } - } - - return count; - } - private static float StrokeSum(float[] canvas, int sampleCount, int tuftCount, int stroke) { return canvas.Skip(stroke * sampleCount * tuftCount).Take(sampleCount * tuftCount).Sum(); @@ -819,18 +792,6 @@ public sealed class BokushoBrushSimulationTests return total; } - private static float NormalizedRoughness(IReadOnlyList samples) - { - var total = samples.Sum(); - var variation = 0.0f; - for (var index = 1; index < samples.Count; index++) - { - variation += MathF.Abs(samples[index] - samples[index - 1]); - } - - return variation / MathF.Max(total, 0.001f); - } - private static float[] LastSampleOnly(IReadOnlyList canvas, int sampleCount, int tuftCount) { var sparse = new float[canvas.Count];