diff --git a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs index 36161a4..e5cbff8 100644 --- a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs +++ b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs @@ -72,9 +72,12 @@ public static class BokushoBrushSimulation var tangent = StrokeTangent(stroke, t, sampleCount); var normal = new Vector2(-tangent.Y, tangent.X); var taper = StrokeTaper(t, stroke.EntryTaper, stroke.ExitTaper); - var localPressure = pressure * taper; - var localNormalRadius = MathF.Max(normalRadius * (0.34f + taper * 0.66f), 0.0001f); - var localTangentRadius = MathF.Max(tangentRadius * (0.48f + taper * 0.52f), 0.0001f); + var normalShape = StrokeNormalRadiusShape(taper); + var tangentShape = StrokeTangentRadiusShape(taper); + var pressureShape = StrokePressureShape(t, stroke.EntryTaper, stroke.ExitTaper); + var localPressure = pressure * taper * pressureShape; + 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); 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); @@ -234,9 +237,10 @@ 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 * (0.34f + taper * 0.66f), 0.0001f); + var radius = MathF.Max(frame.BrushRadius * stroke.RadiusScale * stroke.NormalScale * StrokeNormalRadiusShape(taper), 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); 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); @@ -258,6 +262,18 @@ public static class BokushoBrushSimulation return 0.04f + entry * exit * 0.96f; } + 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 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.12f, 0.42f, t) * (1.0f - SmoothStep(0.68f, 0.96f, t)); + return Saturate(0.54f + pressIn * liftOut * 0.28f + belly * 0.30f); + } + 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 f4efb05..ad364e6 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl @@ -55,6 +55,26 @@ float StrokeTaper(BokushoBrushStroke stroke, float t) return 0.04 + entry * exit * 0.96; } +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 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), t); + float liftOut = 1.0 - smoothstep(max(1.0 - exitTaper * 1.35, 0.20), 1.0, t); + float belly = smoothstep(0.12, 0.42, t) * (1.0 - smoothstep(0.68, 0.96, t)); + return saturate(0.54 + pressIn * liftOut * 0.28 + belly * 0.30); +} + float LaneHash(uint strokeIndex, uint tuft, uint salt) { uint value = (strokeIndex + 1u) * 0x9E3779B9u ^ (tuft + 1u) * 0x85EBCA6Bu ^ salt; @@ -109,9 +129,12 @@ void D3D12BokushoBrushCS(uint3 dispatchThreadId : SV_DispatchThreadID) float2 tangent = StrokeTangent(stroke, t); float2 normal = float2(-tangent.y, tangent.x); float taper = StrokeTaper(stroke, t); - float localPressure = pressure * taper; - float localNormalRadius = max(normalRadius * (0.34 + taper * 0.66), 0.0001); - float localTangentRadius = max(tangentRadius * (0.48 + taper * 0.52), 0.0001); + float normalShape = StrokeNormalRadiusShape(taper); + float tangentShape = StrokeTangentRadiusShape(taper); + float pressureShape = StrokePressureShape(stroke, t); + float localPressure = pressure * taper * pressureShape; + float localNormalRadius = max(normalRadius * normalShape, 0.0001); + float localTangentRadius = max(tangentRadius * tangentShape, 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 7c65d4b..98787e7 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl @@ -113,6 +113,21 @@ float bokushoStrokeTaper(BokushoBrushStroke stroke, float t) return 0.04 + entry * exit * 0.96; } +float bokushoStrokeNormalRadiusShape(float taper) +{ + return 0.08 + taper * 0.92; +} + +float bokushoStrokePressureShape(BokushoBrushStroke stroke, float 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), t); + float liftOut = 1.0 - smoothstep(max(1.0 - exitTaper * 1.35, 0.20), 1.0, t); + float belly = smoothstep(0.12, 0.42, t) * (1.0 - smoothstep(0.68, 0.96, t)); + return saturate(0.54 + pressIn * liftOut * 0.28 + belly * 0.30); +} + float bokushoCanvasSample(uint strokeIndex, float sampleIndex, float tuftIndex) { uint sampleCount = max((uint)round(bokushoShape.x), 2u); @@ -201,9 +216,9 @@ 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 * (0.34 + taper * 0.66), 0.0001); + float radius = max(bokushoMaterial.x * stroke.profile.x * stroke.profile.z * bokushoStrokeNormalRadiusShape(taper), 0.0001); float splay = saturate(bokushoDynamics.x / 2.0); - float pressure = saturate(bokushoMaterial.y * stroke.profile.y * 0.5) * taper; + float pressure = saturate(bokushoMaterial.y * stroke.profile.y * 0.5) * taper * bokushoStrokePressureShape(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 e0d26e1..5bf49d7 100644 --- a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs +++ b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs @@ -221,7 +221,7 @@ public sealed class BokushoBrushSimulationTests var strongPage = BokushoBrushSimulation.ProjectCanvasToPage(strong, strongResult.Canvas, 96, 96, Vector2.Zero, 4.0f); Assert.True(strongPage.Max() > faintPage.Max() * 1.8f); - Assert.True(SamplePage(strongPage, 96, 96, new Vector2(-1.8f, 0.0f), 4.0f) > SamplePage(faintPage, 96, 96, new Vector2(-1.8f, 0.0f), 4.0f)); + Assert.True(strongPage.Sum() > faintPage.Sum() * 1.45f); } [Fact] @@ -278,6 +278,47 @@ public sealed class BokushoBrushSimulationTests Assert.True(SamplePage(wetPage, 96, 96, edge, 4.0f) > SamplePage(dryPage, 96, 96, edge, 4.0f)); } + [Fact] + public void CpuBrushPageProjectionNarrowsTaperedStrokeEnds() + { + var frame = new AquariumBokushoBrushFrame + { + TuftCount = 11, + SampleCount = 80, + PhysicsHz = 500.0f, + BrushRadius = 1.8f, + Pressure = 0.84f, + InkLoad = 1.0f, + Wetness = 1.2f, + Splay = 0.72f, + Strokes = + [ + new AquariumBokushoBrushStroke + { + StrokeP0 = new Vector4(-3.2f, 0.0f, 0.0f, 0.0f), + StrokeP1 = new Vector4(-2.1f, 0.0f, 0.0f, 0.0f), + StrokeP2 = new Vector4(2.1f, 0.0f, 0.0f, 0.0f), + StrokeP3 = new Vector4(3.2f, 0.0f, 0.0f, 0.0f), + RadiusScale = 1.0f, + PressureScale = 1.0f, + NormalScale = 0.80f, + TangentScale = 1.0f, + EntryTaper = 0.20f, + ExitTaper = 0.20f, + PigmentScale = 1.0f, + SplitScale = 1.0f, + }, + ], + }; + var canvas = Enumerable.Repeat(1.0f, frame.TuftCount * frame.SampleCount).ToArray(); + + var page = BokushoBrushSimulation.ProjectCanvasToPage(frame, canvas, 128, 128, Vector2.Zero, 4.0f); + var middleShoulder = SamplePage(page, 128, 128, new Vector2(0.0f, 0.46f), 4.0f); + var entryShoulder = SamplePage(page, 128, 128, new Vector2(-2.1f, 0.46f), 4.0f); + + Assert.True(middleShoulder > entryShoulder * 1.8f); + } + private static AquariumBokushoBrushFrame StrokeDynamicsFrame(float pigmentScale, float entryTaper) { return new AquariumBokushoBrushFrame