diff --git a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs index 262eb98..ea5cc2e 100644 --- a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs +++ b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs @@ -76,10 +76,12 @@ public static class BokushoBrushSimulation var normalShape = StrokeNormalRadiusShape(taper); var tangentShape = StrokeTangentRadiusShape(taper); var pressureShape = StrokePressureShape(t, stroke.EntryTaper, stroke.ExitTaper); - var localPressure = pressure * taper * pressureShape; + 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 localNormalRadius = MathF.Max(normalRadius * normalShape, 0.0001f); - var localTangentRadius = MathF.Max(tangentRadius * tangentShape, 0.0001f); + var localNormalRadius = MathF.Max(normalRadius * normalShape * gestureWidth, 0.0001f); + var localTangentRadius = MathF.Max(tangentRadius * tangentShape * (0.92f + gestureWidth * 0.08f), 0.0001f); var turn = MathF.Sin(t * MathF.Tau + strokeIndex * 0.37f); var targetOffset = restOffset * localNormalRadius * (0.38f + splay * 0.52f + localPressure * 0.08f - wetness * 0.10f) + turn * localNormalRadius * 0.14f * (1.0f - edge); var recovery = Saturate(0.08f + stateWet * 0.12f + localPressure * 0.08f + (1.0f - edge) * 0.07f); @@ -240,10 +242,11 @@ public static class BokushoBrushSimulation var normal = new Vector2(-tangent.Y, tangent.X); var lateral = Vector2.Dot(world - centerPoint, normal); var taper = StrokeTaper(bestT, stroke.EntryTaper, stroke.ExitTaper); - var radius = MathF.Max(frame.BrushRadius * stroke.RadiusScale * stroke.NormalScale * StrokeNormalRadiusShape(taper), 0.0001f); + var gestureWidth = StrokeGestureWidthShape(stroke, bestT, sampleCount); + var radius = MathF.Max(frame.BrushRadius * stroke.RadiusScale * stroke.NormalScale * StrokeNormalRadiusShape(taper) * gestureWidth, 0.0001f); var splay = Saturate(frame.Splay / 2.0f); var pressure = Saturate(frame.Pressure * stroke.PressureScale * 0.5f) * taper; - pressure *= StrokePressureShape(bestT, stroke.EntryTaper, stroke.ExitTaper); + pressure *= StrokePressureShape(bestT, stroke.EntryTaper, stroke.ExitTaper) * StrokeGesturePressureShape(stroke, bestT, sampleCount); var footprint = radius * (0.42f + splay * 0.74f + pressure * 0.18f); var tuftT = Saturate(lateral / MathF.Max(footprint, 0.001f) * 0.5f + 0.5f); var distance = MathF.Sqrt(bestDistance); @@ -282,6 +285,47 @@ public static class BokushoBrushSimulation 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); + return Saturate(0.88f + slowPress * 0.30f + turnPress * 0.20f); + } + + 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); + return Saturate(0.84f + slowSpread * 0.36f + turnSpread * 0.20f); + } + + 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; diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl index a5ac8df..4fb9146 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl @@ -80,6 +80,47 @@ 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); + return saturate(0.88 + slowPress * 0.30 + turnPress * 0.20); +} + +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); + return saturate(0.84 + slowSpread * 0.36 + turnSpread * 0.20); +} + float LaneHash(uint strokeIndex, uint tuft, uint salt) { uint value = (strokeIndex + 1u) * 0x9E3779B9u ^ (tuft + 1u) * 0x85EBCA6Bu ^ salt; @@ -138,10 +179,12 @@ void D3D12BokushoBrushCS(uint3 dispatchThreadId : SV_DispatchThreadID) float normalShape = StrokeNormalRadiusShape(taper); float tangentShape = StrokeTangentRadiusShape(taper); float pressureShape = StrokePressureShape(stroke, t); - float localPressure = pressure * taper * pressureShape; + float gesturePressure = StrokeGesturePressureShape(stroke, t); + float gestureWidth = StrokeGestureWidthShape(stroke, t); + float localPressure = pressure * taper * pressureShape * gesturePressure; float cohesion = LaneCohesion(edge, split, stateWet, localPressure); - float localNormalRadius = max(normalRadius * normalShape, 0.0001); - float localTangentRadius = max(tangentRadius * tangentShape, 0.0001); + float localNormalRadius = max(normalRadius * normalShape * gestureWidth, 0.0001); + float localTangentRadius = max(tangentRadius * tangentShape * (0.92 + gestureWidth * 0.08), 0.0001); float turn = sin(t * 6.28318530718 + (float)strokeIndex * 0.37); float targetOffset = restOffset * localNormalRadius * (0.38 + splay * 0.52 + localPressure * 0.08 - wetness * 0.10) + turn * localNormalRadius * 0.14 * (1.0 - edge); float recovery = saturate(0.08 + stateWet * 0.12 + localPressure * 0.08 + (1.0 - edge) * 0.07); diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl index 98787e7..e966c41 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl @@ -128,6 +128,47 @@ float bokushoStrokePressureShape(BokushoBrushStroke stroke, float t) return saturate(0.54 + pressIn * liftOut * 0.28 + belly * 0.30); } +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); + return saturate(0.88 + slowPress * 0.30 + turnPress * 0.20); +} + +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); + return saturate(0.84 + slowSpread * 0.36 + turnSpread * 0.20); +} + float bokushoCanvasSample(uint strokeIndex, float sampleIndex, float tuftIndex) { uint sampleCount = max((uint)round(bokushoShape.x), 2u); @@ -216,9 +257,10 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro float2 normal = float2(-tangent.y, tangent.x); float lateral = dot(world - center, normal); float taper = bokushoStrokeTaper(stroke, bestT); - float radius = max(bokushoMaterial.x * stroke.profile.x * stroke.profile.z * bokushoStrokeNormalRadiusShape(taper), 0.0001); + float gestureWidth = bokushoStrokeGestureWidthShape(stroke, bestT); + float radius = max(bokushoMaterial.x * stroke.profile.x * stroke.profile.z * bokushoStrokeNormalRadiusShape(taper) * gestureWidth, 0.0001); float splay = saturate(bokushoDynamics.x / 2.0); - float pressure = saturate(bokushoMaterial.y * stroke.profile.y * 0.5) * taper * bokushoStrokePressureShape(stroke, bestT); + float pressure = saturate(bokushoMaterial.y * stroke.profile.y * 0.5) * taper * bokushoStrokePressureShape(stroke, bestT) * bokushoStrokeGesturePressureShape(stroke, bestT); float footprint = radius * (0.42 + splay * 0.74 + pressure * 0.18); float tuftT = saturate(lateral / max(footprint, 0.001) * 0.5 + 0.5); float distance = sqrt(bestDistance); diff --git a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs index ded7f0e..a48564a 100644 --- a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs +++ b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs @@ -341,6 +341,23 @@ public sealed class BokushoBrushSimulationTests Assert.True(sustainedWetSamples > wetResult.SampleCount / 3); } + [Fact] + public void CpuBrushGeometryEnrichesCurvedStrokePressureAndWidth() + { + var straight = GestureFrame(curved: false); + var curved = GestureFrame(curved: true); + + var straightResult = BokushoBrushSimulation.Evaluate(straight); + var curvedResult = BokushoBrushSimulation.Evaluate(curved); + var straightPage = BokushoBrushSimulation.ProjectCanvasToPage(straight, straightResult.Canvas, 128, 128, Vector2.Zero, 4.0f); + var curvedPage = BokushoBrushSimulation.ProjectCanvasToPage(curved, curvedResult.Canvas, 128, 128, Vector2.Zero, 4.0f); + 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); + } + private static AquariumBokushoBrushFrame StrokeDynamicsFrame(float pigmentScale, float entryTaper) { return new AquariumBokushoBrushFrame @@ -376,6 +393,41 @@ public sealed class BokushoBrushSimulationTests }; } + private static AquariumBokushoBrushFrame GestureFrame(bool curved) + { + return new AquariumBokushoBrushFrame + { + TuftCount = 13, + SampleCount = 84, + PhysicsHz = 500.0f, + BrushRadius = 1.25f, + Pressure = 0.84f, + InkLoad = 1.20f, + Wetness = 1.0f, + Splay = 0.78f, + Bend = 0.72f, + Friction = 0.66f, + Strokes = + [ + new AquariumBokushoBrushStroke + { + StrokeP0 = new Vector4(-2.8f, curved ? 0.8f : 0.0f, 0.0f, 0.0f), + StrokeP1 = new Vector4(-1.4f, curved ? -1.2f : 0.0f, 0.0f, 0.0f), + StrokeP2 = new Vector4(1.4f, curved ? 1.1f : 0.0f, 0.0f, 0.0f), + StrokeP3 = new Vector4(2.8f, curved ? -0.7f : 0.0f, 0.0f, 0.0f), + RadiusScale = 0.88f, + PressureScale = 1.0f, + NormalScale = 0.62f, + TangentScale = 1.12f, + EntryTaper = 0.08f, + ExitTaper = 0.24f, + PigmentScale = 1.0f, + SplitScale = 1.1f, + }, + ], + }; + } + private static AquariumBokushoBrushFrame LaneCohesionFrame(float wetness) { return new AquariumBokushoBrushFrame