diff --git a/src/Aquarium.Engine.Contracts/Render/AquariumRenderPlan.cs b/src/Aquarium.Engine.Contracts/Render/AquariumRenderPlan.cs index b34d03b..5fb380a 100644 --- a/src/Aquarium.Engine.Contracts/Render/AquariumRenderPlan.cs +++ b/src/Aquarium.Engine.Contracts/Render/AquariumRenderPlan.cs @@ -602,6 +602,14 @@ public sealed class AquariumBokushoBrushStroke public float TangentScale { get; init; } = 1.0f; + public float EntryTaper { get; init; } = 0.10f; + + public float ExitTaper { get; init; } = 0.14f; + + public float PigmentScale { get; init; } = 1.0f; + + public float SplitScale { get; init; } = 1.0f; + public AquariumBokushoBrushStroke Normalized() => new() { StrokeP0 = StrokeP0, @@ -612,6 +620,10 @@ public sealed class AquariumBokushoBrushStroke PressureScale = Math.Clamp(PressureScale, 0.1f, 4.0f), NormalScale = Math.Clamp(NormalScale, 0.1f, 4.0f), TangentScale = Math.Clamp(TangentScale, 0.1f, 4.0f), + EntryTaper = Math.Clamp(EntryTaper, 0.01f, 0.50f), + ExitTaper = Math.Clamp(ExitTaper, 0.01f, 0.50f), + PigmentScale = Math.Clamp(PigmentScale, 0.05f, 4.0f), + SplitScale = Math.Clamp(SplitScale, 0.0f, 4.0f), }; } diff --git a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs index f663470..312dfbc 100644 --- a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs +++ b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs @@ -59,7 +59,7 @@ public static class BokushoBrushSimulation var edge = MathF.Abs(restOffset); 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); + var split = Saturate((0.20f - LaneHash(strokeIndex, tuft, 53)) * 4.0f) * Saturate((edge - 0.18f) * 1.7f) * stroke.SplitScale; 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); @@ -71,7 +71,7 @@ public static class BokushoBrushSimulation var center = StrokePoint(stroke, t); var tangent = StrokeTangent(stroke, t, sampleCount); var normal = new Vector2(-tangent.Y, tangent.X); - var taper = StrokeTaper(t); + 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); @@ -96,7 +96,7 @@ public static class BokushoBrushSimulation 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); + var localPigment = Saturate((deposition * (7.5f + contact * 2.5f) + contact * stateLoad * stateWet * 0.22f + adhesion * contact * 0.08f) * stroke.PigmentScale); trace[index] = Saturate(contact * (0.30f + stateLoad * 0.42f + adhesion * 0.20f) + deposition * 1.8f); canvas[index] = localPigment; } @@ -165,6 +165,10 @@ public static class BokushoBrushSimulation PressureScale = frame.PressureScale, NormalScale = frame.NormalScale, TangentScale = frame.TangentScale, + EntryTaper = 0.10f, + ExitTaper = 0.14f, + PigmentScale = 1.0f, + SplitScale = 1.0f, }.Normalized() ]; } @@ -229,7 +233,7 @@ public static class BokushoBrushSimulation var tangent = StrokeTangent(stroke, bestT, sampleCount); var normal = new Vector2(-tangent.Y, tangent.X); var lateral = Vector2.Dot(world - centerPoint, normal); - var taper = StrokeTaper(bestT); + 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 splay = Saturate(frame.Splay / 2.0f); var pressure = Saturate(frame.Pressure * stroke.PressureScale * 0.5f) * taper; @@ -243,10 +247,10 @@ public static class BokushoBrushSimulation return pigment * contact * (0.10f + pressure * 0.18f + Saturate(frame.InkLoad * 0.5f) * 0.08f); } - private static float StrokeTaper(float t) + private static float StrokeTaper(float t, float entryTaper, float exitTaper) { - var entry = SmoothStep(0.0f, 0.10f, t); - var exit = 1.0f - SmoothStep(0.86f, 1.0f, t); + var entry = SmoothStep(0.0f, entryTaper, t); + var exit = 1.0f - SmoothStep(1.0f - exitTaper, 1.0f, t); return 0.18f + entry * exit * 0.82f; } diff --git a/src/Aquarium.Engine/Render/D3D12Renderer.cs b/src/Aquarium.Engine/Render/D3D12Renderer.cs index 3e4447b..8f67c55 100644 --- a/src/Aquarium.Engine/Render/D3D12Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D12Renderer.cs @@ -4785,6 +4785,7 @@ public sealed class D3D12Renderer : IAquariumRenderer [ new D3D12BokushoBrushStrokePacket( new Vector4(frame.RadiusScale, frame.PressureScale, frame.NormalScale, frame.TangentScale), + new Vector4(0.10f, 0.14f, 1.0f, 1.0f), frame.StrokeP0, frame.StrokeP1, frame.StrokeP2, @@ -4796,6 +4797,7 @@ public sealed class D3D12Renderer : IAquariumRenderer { return new D3D12BokushoBrushStrokePacket( new Vector4(stroke.RadiusScale, stroke.PressureScale, stroke.NormalScale, stroke.TangentScale), + new Vector4(stroke.EntryTaper, stroke.ExitTaper, stroke.PigmentScale, stroke.SplitScale), stroke.StrokeP0, stroke.StrokeP1, stroke.StrokeP2, @@ -7022,6 +7024,7 @@ public sealed class D3D12Renderer : IAquariumRenderer [StructLayout(LayoutKind.Sequential)] private readonly record struct D3D12BokushoBrushStrokePacket( Vector4 Profile, + Vector4 Dynamics, Vector4 StrokeP0, Vector4 StrokeP1, Vector4 StrokeP2, diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl index fe696a7..2261e89 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl @@ -15,6 +15,7 @@ cbuffer BokushoBrushConstants : register(b4) struct BokushoBrushStroke { float4 profile; + float4 dynamics; float4 p0; float4 p1; float4 p2; @@ -45,6 +46,15 @@ float StrokeTaper(float t) return 0.18 + entry * exit * 0.82; } +float StrokeTaper(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 entry = smoothstep(0.0, entryTaper, t); + float exit = 1.0 - smoothstep(1.0 - exitTaper, 1.0, t); + return 0.18 + entry * exit * 0.82; +} + float LaneHash(uint strokeIndex, uint tuft, uint salt) { uint value = (strokeIndex + 1u) * 0x9E3779B9u ^ (tuft + 1u) * 0x85EBCA6Bu ^ salt; @@ -76,7 +86,7 @@ void D3D12BokushoBrushCS(uint3 dispatchThreadId : SV_DispatchThreadID) float edge = abs(restOffset); float laneHash = LaneHash(strokeIndex, tuft, 17u); float laneLoad = 0.76 + laneHash * 0.34; - float split = saturate((0.20 - LaneHash(strokeIndex, tuft, 53u)) * 4.0) * saturate((edge - 0.18) * 1.7); + 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 load = saturate(brushMaterial.z / 2.0); @@ -98,7 +108,7 @@ void D3D12BokushoBrushCS(uint3 dispatchThreadId : SV_DispatchThreadID) float2 center = StrokePoint(stroke, t); float2 tangent = StrokeTangent(stroke, t); float2 normal = float2(-tangent.y, tangent.x); - float taper = StrokeTaper(t); + 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); @@ -123,7 +133,7 @@ void D3D12BokushoBrushCS(uint3 dispatchThreadId : SV_DispatchThreadID) 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); + float localPigment = saturate((deposition * (7.5 + contact * 2.5) + contact * stateLoad * stateWet * 0.22 + adhesion * contact * 0.08) * 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/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl index 04d4248..c3f5ca7 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl @@ -48,6 +48,7 @@ cbuffer BokushoPageConstants : register(b5) struct BokushoBrushStroke { float4 profile; + float4 dynamics; float4 p0; float4 p1; float4 p2; @@ -103,10 +104,12 @@ float2 bokushoStrokeTangent(BokushoBrushStroke stroke, float t) return cultmath_normalize(after - before); } -float bokushoStrokeTaper(float t) +float bokushoStrokeTaper(BokushoBrushStroke stroke, float t) { - float entry = smoothstep(0.0, 0.10, t); - float exit = 1.0 - smoothstep(0.86, 1.0, t); + float entryTaper = clamp(stroke.dynamics.x, 0.01, 0.50); + float exitTaper = clamp(stroke.dynamics.y, 0.01, 0.50); + float entry = smoothstep(0.0, entryTaper, t); + float exit = 1.0 - smoothstep(1.0 - exitTaper, 1.0, t); return 0.18 + entry * exit * 0.82; } @@ -176,7 +179,7 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro float2 tangent = bokushoStrokeTangent(stroke, bestT); float2 normal = float2(-tangent.y, tangent.x); float lateral = dot(world - center, normal); - float taper = bokushoStrokeTaper(bestT); + float taper = bokushoStrokeTaper(stroke, bestT); float radius = max(bokushoMaterial.x * stroke.profile.x * stroke.profile.z * (0.34 + taper * 0.66), 0.0001); float splay = saturate(bokushoDynamics.x / 2.0); float pressure = saturate(bokushoMaterial.y * stroke.profile.y * 0.5) * taper; diff --git a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs index 3eb053f..3d0f5ca 100644 --- a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs +++ b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs @@ -209,6 +209,56 @@ public sealed class BokushoBrushSimulationTests Assert.True(SamplePage(page, 96, 96, new Vector2(0.0f, -1.0f), 6.0f) > 0.001f); } + [Fact] + public void CpuBrushStrokeDynamicsControlPigmentAndTaper() + { + var faint = StrokeDynamicsFrame(pigmentScale: 0.35f, entryTaper: 0.24f); + var strong = StrokeDynamicsFrame(pigmentScale: 1.65f, entryTaper: 0.04f); + + var faintResult = BokushoBrushSimulation.Evaluate(faint); + var strongResult = BokushoBrushSimulation.Evaluate(strong); + var faintPage = BokushoBrushSimulation.ProjectCanvasToPage(faint, faintResult.Canvas, 96, 96, Vector2.Zero, 4.0f); + 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)); + } + + private static AquariumBokushoBrushFrame StrokeDynamicsFrame(float pigmentScale, float entryTaper) + { + return new AquariumBokushoBrushFrame + { + TuftCount = 11, + SampleCount = 56, + PhysicsHz = 500.0f, + BrushRadius = 1.4f, + Pressure = 0.92f, + InkLoad = 1.24f, + Wetness = 0.90f, + Splay = 0.82f, + Bend = 0.74f, + Friction = 0.66f, + Strokes = + [ + new AquariumBokushoBrushStroke + { + StrokeP0 = new Vector4(-2.8f, 0.1f, 0.0f, 0.0f), + StrokeP1 = new Vector4(-1.7f, 0.0f, 0.0f, 0.0f), + StrokeP2 = new Vector4(1.4f, 0.0f, 0.0f, 0.0f), + StrokeP3 = new Vector4(2.8f, -0.1f, 0.0f, 0.0f), + RadiusScale = 0.86f, + PressureScale = 1.0f, + NormalScale = 0.66f, + TangentScale = 1.2f, + EntryTaper = entryTaper, + ExitTaper = 0.18f, + PigmentScale = pigmentScale, + SplitScale = 1.0f, + }, + ], + }; + } + 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);