From 0c59b9f4975d4a3faca9ec239dd34b1dcd8ca8f2 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 13 Jun 2026 11:44:14 +0100 Subject: [PATCH] Add Bokusho wet lane cohesion --- .../Render/BokushoBrushSimulation.cs | 24 ++++--- .../Render/Shaders/D3D12BokushoBrush.hlsl | 24 ++++--- .../BokushoBrushSimulationTests.cs | 62 +++++++++++++++++++ 3 files changed, 94 insertions(+), 16 deletions(-) diff --git a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs index e5cbff8..262eb98 100644 --- a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs +++ b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs @@ -60,10 +60,11 @@ public static class BokushoBrushSimulation var laneHash = LaneHash(strokeIndex, tuft, 17); var laneLoad = 0.76f + laneHash * 0.34f; var split = Saturate((0.20f - LaneHash(strokeIndex, tuft, 53)) * 4.0f) * Saturate((edge - 0.18f) * 1.7f) * stroke.SplitScale; + var initialCohesion = LaneCohesion(edge, split, wetness, pressure); var tip = StrokePoint(stroke, 0.0f); var offset = restOffset * normalRadius * (0.42f + splay * 0.38f) + (laneHash - 0.5f) * normalRadius * 0.05f; - var stateLoad = load * (1.0f - edge * 0.36f) * laneLoad * (1.0f - split * 0.58f); - var stateWet = wetness * (0.86f + (1.0f - edge) * 0.14f); + var stateLoad = load * (0.62f + initialCohesion * 0.46f) * (1.0f - edge * 0.18f) * laneLoad * (1.0f - split * 0.36f); + var stateWet = wetness * (0.74f + initialCohesion * 0.24f - split * 0.08f); for (var sample = 0; sample < sampleCount; sample++) { @@ -76,6 +77,7 @@ public static class BokushoBrushSimulation var tangentShape = StrokeTangentRadiusShape(taper); var pressureShape = StrokePressureShape(t, stroke.EntryTaper, stroke.ExitTaper); var localPressure = pressure * taper * pressureShape; + var cohesion = LaneCohesion(edge, split, stateWet, localPressure); var localNormalRadius = MathF.Max(normalRadius * normalShape, 0.0001f); var localTangentRadius = MathF.Max(tangentRadius * tangentShape, 0.0001f); var turn = MathF.Sin(t * MathF.Tau + strokeIndex * 0.37f); @@ -86,20 +88,21 @@ public static class BokushoBrushSimulation var lag = localTangentRadius * (0.12f + bend * 0.72f + friction * localPressure * 0.34f + edge * 0.18f); var desiredTip = center + normal * offset - tangent * lag; var slip = desiredTip - tip; - var contact = Saturate(localPressure * stateWet * stateLoad * (0.70f + (1.0f - edge) * 0.26f)); + var contact = Saturate(localPressure * stateWet * 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() * frame.PhysicsHz / 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.22f + tension * 0.34f + velocity * 0.010f - stateWet * 0.16f); - var adhesion = Saturate(stateWet * (0.52f + localPressure * 0.20f) - separation * 0.18f - tension * 0.08f); - var deposition = contact * stateLoad * stateWet * Saturate(0.10f + drag * 0.72f + velocity * 0.012f) * (0.82f + separation * 0.22f); - stateLoad = MathF.Max(0.0f, stateLoad - deposition * (0.040f + localPressure * 0.025f)); + 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 deposition = contact * stateLoad * stateWet * Saturate(0.10f + drag * 0.72f + velocity * 0.010f) * (0.74f + separation * 0.18f + cohesion * 0.26f); + stateLoad = MathF.Max(0.0f, stateLoad - deposition * (0.032f + localPressure * 0.020f - cohesion * 0.008f)); stateWet = MathF.Max(0.0f, stateWet - deposition * 0.010f); var index = ((strokeIndex * tuftCount) + tuft) * sampleCount + sample; - var localPigment = Saturate((deposition * (7.5f + contact * 2.5f) + contact * stateLoad * stateWet * 0.22f + adhesion * contact * 0.08f) * stroke.PigmentScale); + var pigmentSurvival = 0.74f + cohesion * 0.36f - split * 0.08f; + var localPigment = Saturate((deposition * (7.5f + contact * 2.5f) + contact * stateLoad * stateWet * 0.24f + adhesion * contact * 0.10f) * pigmentSurvival * stroke.PigmentScale); trace[index] = Saturate(contact * (0.30f + stateLoad * 0.42f + adhesion * 0.20f) + deposition * 1.8f); canvas[index] = localPigment; } @@ -274,6 +277,11 @@ public static class BokushoBrushSimulation return Saturate(0.54f + pressIn * liftOut * 0.28f + belly * 0.30f); } + 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 LaneHash(int strokeIndex, int tuft, uint salt) { var value = (uint)(strokeIndex + 1) * 0x9E3779B9u ^ (uint)(tuft + 1) * 0x85EBCA6Bu ^ salt; diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl index ad364e6..a5ac8df 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl @@ -75,6 +75,11 @@ float StrokePressureShape(BokushoBrushStroke stroke, float t) return saturate(0.54 + pressIn * liftOut * 0.28 + belly * 0.30); } +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 LaneHash(uint strokeIndex, uint tuft, uint salt) { uint value = (strokeIndex + 1u) * 0x9E3779B9u ^ (tuft + 1u) * 0x85EBCA6Bu ^ salt; @@ -109,6 +114,7 @@ void D3D12BokushoBrushCS(uint3 dispatchThreadId : SV_DispatchThreadID) float split = saturate((0.20 - LaneHash(strokeIndex, tuft, 53u)) * 4.0) * saturate((edge - 0.18) * 1.7) * stroke.dynamics.w; float pressure = saturate(brushMaterial.y * stroke.profile.y * 0.5) * 2.0; float wetness = saturate(brushMaterial.w / 1.6); + float initialCohesion = LaneCohesion(edge, split, wetness, pressure); float load = saturate(brushMaterial.z / 2.0); float splay = saturate(brushDynamics.x / 2.0); float bend = saturate(brushDynamics.y / 2.4); @@ -118,8 +124,8 @@ void D3D12BokushoBrushCS(uint3 dispatchThreadId : SV_DispatchThreadID) float tangentRadius = max(radius * stroke.profile.w, 0.0001); float2 tip = StrokePoint(stroke, 0.0); float offset = restOffset * normalRadius * (0.42 + splay * 0.38) + (laneHash - 0.5) * normalRadius * 0.05; - float stateLoad = load * (1.0 - edge * 0.36) * laneLoad * (1.0 - split * 0.58); - float stateWet = wetness * (0.86 + (1.0 - edge) * 0.14); + float stateLoad = load * (0.62 + initialCohesion * 0.46) * (1.0 - edge * 0.18) * laneLoad * (1.0 - split * 0.36); + float stateWet = wetness * (0.74 + initialCohesion * 0.24 - split * 0.08); [loop] for (uint sample = 0u; sample < sampleCount; sample += 1u) @@ -133,6 +139,7 @@ void D3D12BokushoBrushCS(uint3 dispatchThreadId : SV_DispatchThreadID) float tangentShape = StrokeTangentRadiusShape(taper); float pressureShape = StrokePressureShape(stroke, t); float localPressure = pressure * taper * pressureShape; + float cohesion = LaneCohesion(edge, split, stateWet, localPressure); float localNormalRadius = max(normalRadius * normalShape, 0.0001); float localTangentRadius = max(tangentRadius * tangentShape, 0.0001); float turn = sin(t * 6.28318530718 + (float)strokeIndex * 0.37); @@ -143,20 +150,21 @@ void D3D12BokushoBrushCS(uint3 dispatchThreadId : SV_DispatchThreadID) float lag = localTangentRadius * (0.12 + bend * 0.72 + friction * localPressure * 0.34 + edge * 0.18); float2 desiredTip = center + normal * offset - tangent * lag; float2 slip = desiredTip - tip; - float contact = saturate(localPressure * stateWet * stateLoad * (0.70 + (1.0 - edge) * 0.26)); + float contact = saturate(localPressure * 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 / 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.22 + tension * 0.34 + velocity * 0.010 - stateWet * 0.16); - float adhesion = saturate(stateWet * (0.52 + localPressure * 0.20) - separation * 0.18 - tension * 0.08); - float deposition = contact * stateLoad * stateWet * saturate(0.10 + drag * 0.72 + velocity * 0.012) * (0.82 + separation * 0.22); - stateLoad = max(0.0, stateLoad - deposition * (0.040 + localPressure * 0.025)); + 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 deposition = contact * stateLoad * stateWet * saturate(0.10 + drag * 0.72 + velocity * 0.010) * (0.74 + separation * 0.18 + cohesion * 0.26); + stateLoad = max(0.0, stateLoad - deposition * (0.032 + localPressure * 0.020 - cohesion * 0.008)); stateWet = max(0.0, stateWet - deposition * 0.010); uint index = ((strokeIndex * tuftCount) + tuft) * sampleCount + sample; - float localPigment = saturate((deposition * (7.5 + contact * 2.5) + contact * stateLoad * stateWet * 0.22 + adhesion * contact * 0.08) * stroke.dynamics.z); + float pigmentSurvival = 0.74 + cohesion * 0.36 - split * 0.08; + float localPigment = saturate((deposition * (7.5 + contact * 2.5) + contact * stateLoad * stateWet * 0.24 + adhesion * contact * 0.10) * pigmentSurvival * stroke.dynamics.z); float trace = saturate(contact * (0.30 + stateLoad * 0.42 + adhesion * 0.20) + deposition * 1.8); BokushoTraceField[index] = trace; BokushoCanvasField[index] = localPigment; diff --git a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs index 5bf49d7..ded7f0e 100644 --- a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs +++ b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs @@ -319,6 +319,28 @@ public sealed class BokushoBrushSimulationTests Assert.True(middleShoulder > entryShoulder * 1.8f); } + [Fact] + public void CpuBrushLaneCohesionPreservesWetCenterPigment() + { + var wet = LaneCohesionFrame(wetness: 1.35f); + var dry = LaneCohesionFrame(wetness: 0.32f); + + var wetResult = BokushoBrushSimulation.Evaluate(wet); + var dryResult = BokushoBrushSimulation.Evaluate(dry); + var centerTuft = wetResult.TuftCount / 2; + var wetCenter = TuftSum(wetResult.Canvas, wetResult.SampleCount, centerTuft); + var wetEdge = TuftSum(wetResult.Canvas, wetResult.SampleCount, 0); + var dryCenter = TuftSum(dryResult.Canvas, dryResult.SampleCount, centerTuft); + var sustainedWetSamples = wetResult.Canvas + .Skip(centerTuft * wetResult.SampleCount) + .Take(wetResult.SampleCount) + .Count(value => value > 0.06f); + + Assert.True(wetCenter > dryCenter * 1.25f); + Assert.True(wetCenter > wetEdge * 1.8f); + Assert.True(sustainedWetSamples > wetResult.SampleCount / 3); + } + private static AquariumBokushoBrushFrame StrokeDynamicsFrame(float pigmentScale, float entryTaper) { return new AquariumBokushoBrushFrame @@ -354,6 +376,46 @@ public sealed class BokushoBrushSimulationTests }; } + private static AquariumBokushoBrushFrame LaneCohesionFrame(float wetness) + { + return new AquariumBokushoBrushFrame + { + TuftCount = 15, + SampleCount = 72, + PhysicsHz = 500.0f, + BrushRadius = 1.2f, + Pressure = 0.88f, + InkLoad = 1.32f, + Wetness = wetness, + Splay = 0.92f, + Bend = 0.78f, + Friction = 0.70f, + Strokes = + [ + new AquariumBokushoBrushStroke + { + StrokeP0 = new Vector4(-3.2f, 0.2f, 0.0f, 0.0f), + StrokeP1 = new Vector4(-1.8f, -0.2f, 0.0f, 0.0f), + StrokeP2 = new Vector4(1.8f, -0.1f, 0.0f, 0.0f), + StrokeP3 = new Vector4(3.2f, 0.3f, 0.0f, 0.0f), + RadiusScale = 0.92f, + PressureScale = 1.0f, + NormalScale = 0.72f, + TangentScale = 1.16f, + EntryTaper = 0.08f, + ExitTaper = 0.22f, + PigmentScale = 1.0f, + SplitScale = 1.8f, + }, + ], + }; + } + + private static float TuftSum(float[] canvas, int sampleCount, int tuft) + { + return canvas.Skip(tuft * sampleCount).Take(sampleCount).Sum(); + } + private static float SamplePage(float[] page, int width, int height, Vector2 world, float viewRadius) { var uv = world / viewRadius * 0.5f + new Vector2(0.5f, 0.5f);