diff --git a/src/Aquarium.Engine.Contracts/Render/AquariumRenderPlan.cs b/src/Aquarium.Engine.Contracts/Render/AquariumRenderPlan.cs index 2db63cb..4b0773a 100644 --- a/src/Aquarium.Engine.Contracts/Render/AquariumRenderPlan.cs +++ b/src/Aquarium.Engine.Contracts/Render/AquariumRenderPlan.cs @@ -540,6 +540,14 @@ public sealed class AquariumBokushoBrushFrame public float Friction { get; init; } = 0.62f; + public float RadiusScale { get; init; } = 1.0f; + + public float PressureScale { get; init; } = 1.0f; + + public float NormalScale { get; init; } = 1.0f; + + public float TangentScale { get; init; } = 1.0f; + public Vector4 StrokeP0 { get; init; } = new(-7.2f, 1.0f, 0.0f, 0.0f); public Vector4 StrokeP1 { get; init; } = new(-2.4f, -1.8f, 0.0f, 0.0f); @@ -562,6 +570,10 @@ public sealed class AquariumBokushoBrushFrame Splay = Math.Clamp(Splay, 0.0f, 2.0f), Bend = Math.Clamp(Bend, 0.0f, 2.4f), Friction = Math.Clamp(Friction, 0.0f, 1.0f), + RadiusScale = Math.Clamp(RadiusScale, 0.1f, 4.0f), + PressureScale = Math.Clamp(PressureScale, 0.1f, 4.0f), + NormalScale = Math.Clamp(NormalScale, 0.1f, 4.0f), + TangentScale = Math.Clamp(TangentScale, 0.1f, 4.0f), StrokeP0 = StrokeP0, StrokeP1 = StrokeP1, StrokeP2 = StrokeP2, diff --git a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs index 32f7f4b..663997a 100644 --- a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs +++ b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs @@ -36,13 +36,15 @@ public static class BokushoBrushSimulation throw new ArgumentException("Canvas buffer is smaller than the brush simulation frame.", nameof(canvas)); } - var pressure = Saturate(frame.Pressure * 0.5f) * 2.0f; + var pressure = Saturate(frame.Pressure * frame.PressureScale * 0.5f) * 2.0f; var wetness = Saturate(frame.Wetness / 1.6f); var load = Saturate(frame.InkLoad / 2.0f); var splay = Saturate(frame.Splay / 2.0f); var bend = Saturate(frame.Bend / 2.4f); var friction = Saturate(frame.Friction); - var radius = MathF.Max(frame.BrushRadius, 0.0001f); + var radius = MathF.Max(frame.BrushRadius * frame.RadiusScale, 0.0001f); + var normalRadius = MathF.Max(radius * frame.NormalScale, 0.0001f); + var tangentRadius = MathF.Max(radius * frame.TangentScale, 0.0001f); for (var tuft = 0; tuft < tuftCount; tuft++) { @@ -50,7 +52,7 @@ public static class BokushoBrushSimulation var restOffset = laneT * 2.0f - 1.0f; var edge = MathF.Abs(restOffset); var tip = StrokePoint(frame, 0.0f); - var offset = restOffset * radius * (0.42f + splay * 0.38f); + var offset = restOffset * normalRadius * (0.42f + splay * 0.38f); var stateLoad = load * (1.0f - edge * 0.36f); var stateWet = wetness * (0.86f + (1.0f - edge) * 0.14f); var shed = 0.0f; @@ -62,11 +64,11 @@ public static class BokushoBrushSimulation var tangent = StrokeTangent(frame, t, sampleCount); var normal = new Vector2(-tangent.Y, tangent.X); var turn = MathF.Sin(t * MathF.Tau + 0.0f); - var targetOffset = restOffset * radius * (0.38f + splay * 0.52f + pressure * 0.08f - wetness * 0.10f) + turn * radius * 0.14f * (1.0f - edge); + var targetOffset = restOffset * normalRadius * (0.38f + splay * 0.52f + pressure * 0.08f - wetness * 0.10f) + turn * normalRadius * 0.14f * (1.0f - edge); var recovery = Saturate(0.08f + stateWet * 0.12f + pressure * 0.08f + (1.0f - edge) * 0.07f); offset = Lerp(offset, targetOffset, recovery); - var lag = radius * (0.12f + bend * 0.72f + friction * pressure * 0.34f + edge * 0.18f); + var lag = tangentRadius * (0.12f + bend * 0.72f + friction * pressure * 0.34f + edge * 0.18f); var desiredTip = center + normal * offset - tangent * lag; var slip = desiredTip - tip; var contact = Saturate(pressure * stateWet * stateLoad * (0.70f + (1.0f - edge) * 0.26f)); @@ -74,7 +76,7 @@ public static class BokushoBrushSimulation 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(radius, 0.001f) * 0.38f + velocity * 0.018f + drag * 0.46f); + var tension = Saturate(MathF.Abs(targetOffset - offset) / MathF.Max(normalRadius, 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 + pressure * 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); @@ -184,16 +186,16 @@ public static class BokushoBrushSimulation var tangent = StrokeTangent(frame, bestT, sampleCount); var normal = new Vector2(-tangent.Y, tangent.X); var lateral = Vector2.Dot(world - centerPoint, normal); - var radius = MathF.Max(frame.BrushRadius, 0.0001f); + var radius = MathF.Max(frame.BrushRadius * frame.RadiusScale * frame.NormalScale, 0.0001f); var splay = Saturate(frame.Splay / 2.0f); - var footprint = radius * (0.42f + splay * 0.74f + Saturate(frame.Pressure * 0.5f) * 0.18f); + var footprint = radius * (0.42f + splay * 0.74f + Saturate(frame.Pressure * frame.PressureScale * 0.5f) * 0.18f); 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.80f, 0.001f)); var sample = Math.Clamp((int)MathF.Round(bestT * MathF.Max(sampleCount - 1.0f, 1.0f)), 0, sampleCount - 1); var tuft = Math.Clamp((int)MathF.Round(tuftT * MathF.Max(tuftCount - 1.0f, 0.0f)), 0, tuftCount - 1); var pigment = Saturate(canvas[tuft * sampleCount + sample]); - var pressure = Saturate(frame.Pressure * 0.5f); + var pressure = Saturate(frame.Pressure * frame.PressureScale * 0.5f); return pigment * contact * (0.10f + pressure * 0.18f + Saturate(frame.InkLoad * 0.5f) * 0.08f); } diff --git a/src/Aquarium.Engine/Render/D3D12Renderer.cs b/src/Aquarium.Engine/Render/D3D12Renderer.cs index e7836d7..684f4fa 100644 --- a/src/Aquarium.Engine/Render/D3D12Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D12Renderer.cs @@ -4041,6 +4041,7 @@ public sealed class D3D12Renderer : IAquariumRenderer new Vector4(sampleCount, tuftCount, frame.PhysicsHz, 1.0f), new Vector4(frame.BrushRadius, frame.Pressure, frame.InkLoad, frame.Wetness), new Vector4(frame.Splay, frame.Bend, frame.Friction, 0.0f), + new Vector4(frame.RadiusScale, frame.PressureScale, frame.NormalScale, frame.TangentScale), frame.StrokeP0, frame.StrokeP1, frame.StrokeP2, @@ -4726,6 +4727,7 @@ public sealed class D3D12Renderer : IAquariumRenderer Vector4.Zero, Vector4.Zero, Vector4.Zero, + Vector4.Zero, Vector4.Zero); } @@ -4736,6 +4738,7 @@ public sealed class D3D12Renderer : IAquariumRenderer new Vector4(sampleCount, tuftCount, frame.PhysicsHz, 1.0f), new Vector4(frame.BrushRadius, frame.Pressure, frame.InkLoad, frame.Wetness), new Vector4(frame.Splay, frame.Bend, frame.Friction, 0.0f), + new Vector4(frame.RadiusScale, frame.PressureScale, frame.NormalScale, frame.TangentScale), frame.StrokeP0, frame.StrokeP1, frame.StrokeP2, @@ -6951,6 +6954,7 @@ public sealed class D3D12Renderer : IAquariumRenderer Vector4 Shape, Vector4 Brush, Vector4 Dynamics, + Vector4 Profile, 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 5cb5185..b5ebe2d 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl @@ -5,6 +5,7 @@ cbuffer BokushoBrushConstants : register(b4) float4 brushShape; // sampleCount, tuftCount, physicsHz, timeSeconds float4 brushMaterial; // radius, pressure, inkLoad, wetness float4 brushDynamics; // splay, bend, friction, reserved + float4 brushProfile; // radiusScale, pressureScale, normalScale, tangentScale float4 strokeP0; float4 strokeP1; float4 strokeP2; @@ -41,15 +42,17 @@ void D3D12BokushoBrushCS(uint3 dispatchThreadId : SV_DispatchThreadID) float laneT = tuftCount <= 1u ? 0.5 : (float)tuft / (float)(tuftCount - 1u); float restOffset = laneT * 2.0 - 1.0; float edge = abs(restOffset); - float pressure = saturate(brushMaterial.y * 0.5) * 2.0; + float pressure = saturate(brushMaterial.y * brushProfile.y * 0.5) * 2.0; float wetness = saturate(brushMaterial.w / 1.6); float load = saturate(brushMaterial.z / 2.0); float splay = saturate(brushDynamics.x / 2.0); float bend = saturate(brushDynamics.y / 2.4); float friction = saturate(brushDynamics.z); - float radius = max(brushMaterial.x, 0.0001); + float radius = max(brushMaterial.x * brushProfile.x, 0.0001); + float normalRadius = max(radius * brushProfile.z, 0.0001); + float tangentRadius = max(radius * brushProfile.w, 0.0001); float2 tip = StrokePoint(0.0); - float offset = restOffset * radius * (0.42 + splay * 0.38); + float offset = restOffset * normalRadius * (0.42 + splay * 0.38); float stateLoad = load * (1.0 - edge * 0.36); float stateWet = wetness * (0.86 + (1.0 - edge) * 0.14); float shed = 0.0; @@ -62,11 +65,11 @@ void D3D12BokushoBrushCS(uint3 dispatchThreadId : SV_DispatchThreadID) float2 tangent = StrokeTangent(t); float2 normal = float2(-tangent.y, tangent.x); float turn = sin(t * 6.28318530718); - float targetOffset = restOffset * radius * (0.38 + splay * 0.52 + pressure * 0.08 - wetness * 0.10) + turn * radius * 0.14 * (1.0 - edge); + float targetOffset = restOffset * normalRadius * (0.38 + splay * 0.52 + pressure * 0.08 - wetness * 0.10) + turn * normalRadius * 0.14 * (1.0 - edge); float recovery = saturate(0.08 + stateWet * 0.12 + pressure * 0.08 + (1.0 - edge) * 0.07); offset = cultmath_lerp(offset, targetOffset, recovery); - float lag = radius * (0.12 + bend * 0.72 + friction * pressure * 0.34 + edge * 0.18); + float lag = tangentRadius * (0.12 + bend * 0.72 + friction * pressure * 0.34 + edge * 0.18); float2 desiredTip = center + normal * offset - tangent * lag; float2 slip = desiredTip - tip; float contact = saturate(pressure * stateWet * stateLoad * (0.70 + (1.0 - edge) * 0.26)); @@ -74,7 +77,7 @@ void D3D12BokushoBrushCS(uint3 dispatchThreadId : SV_DispatchThreadID) tip = tip + slip * (1.0 - drag); float velocity = length(slip) * brushShape.z / max(radius, 0.001); - float tension = saturate(abs(targetOffset - offset) / max(radius, 0.001) * 0.38 + velocity * 0.018 + drag * 0.46); + float tension = saturate(abs(targetOffset - offset) / max(normalRadius, 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 + pressure * 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); diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl index 815eb6c..ecdf522 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl @@ -38,6 +38,7 @@ cbuffer BokushoPageConstants : register(b5) float4 bokushoShape; // sampleCount, tuftCount, physicsHz, hasInput float4 bokushoMaterial; // radius, pressure, inkLoad, wetness float4 bokushoDynamics; // splay, bend, friction, reserved + float4 bokushoProfile; // radiusScale, pressureScale, normalScale, tangentScale float4 bokushoStrokeP0; float4 bokushoStrokeP1; float4 bokushoStrokeP2; @@ -150,16 +151,16 @@ float bokushoPageHeight(float2 world) float2 tangent = bokushoStrokeTangent(bestT); float2 normal = float2(-tangent.y, tangent.x); float lateral = dot(world - center, normal); - float radius = max(bokushoMaterial.x, 0.0001); + float radius = max(bokushoMaterial.x * bokushoProfile.x * bokushoProfile.z, 0.0001); float splay = saturate(bokushoDynamics.x / 2.0); - float footprint = radius * (0.42 + splay * 0.74 + saturate(bokushoMaterial.y * 0.5) * 0.18); + float footprint = radius * (0.42 + splay * 0.74 + saturate(bokushoMaterial.y * bokushoProfile.y * 0.5) * 0.18); 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.80, 0.001)); float sampleIndex = bestT * max(bokushoShape.x - 1.0, 1.0); float tuftIndex = tuftT * max(bokushoShape.y - 1.0, 0.0); float pigment = bokushoCanvasSample(sampleIndex, tuftIndex); - float pressure = saturate(bokushoMaterial.y * 0.5); + float pressure = saturate(bokushoMaterial.y * bokushoProfile.y * 0.5); return pigment * contact * (0.10 + pressure * 0.18 + saturate(bokushoMaterial.z * 0.5) * 0.08); } diff --git a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs index 982aad5..48c8ffd 100644 --- a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs +++ b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs @@ -110,4 +110,42 @@ public sealed class BokushoBrushSimulationTests Assert.True(max > offStrokeCorner * 8.0f); Assert.Contains(page, value => value > 0.0f); } + + [Fact] + public void CpuBrushProfileControlsPageFootprint() + { + var narrow = new AquariumBokushoBrushFrame + { + TuftCount = 9, + SampleCount = 48, + PhysicsHz = 500.0f, + BrushRadius = 1.8f, + Pressure = 0.86f, + InkLoad = 1.18f, + Wetness = 0.88f, + NormalScale = 0.55f, + TangentScale = 1.10f, + }; + var wide = new AquariumBokushoBrushFrame + { + TuftCount = narrow.TuftCount, + SampleCount = narrow.SampleCount, + PhysicsHz = narrow.PhysicsHz, + BrushRadius = narrow.BrushRadius, + Pressure = narrow.Pressure, + InkLoad = narrow.InkLoad, + Wetness = narrow.Wetness, + NormalScale = 1.45f, + TangentScale = narrow.TangentScale, + }; + var narrowResult = BokushoBrushSimulation.Evaluate(narrow); + var wideResult = BokushoBrushSimulation.Evaluate(wide); + + var narrowPage = BokushoBrushSimulation.ProjectCanvasToPage(narrow, narrowResult.Canvas, 64, 64, Vector2.Zero, 8.0f); + var widePage = BokushoBrushSimulation.ProjectCanvasToPage(wide, wideResult.Canvas, 64, 64, Vector2.Zero, 8.0f); + var narrowCoverage = narrowPage.Count(value => value > 0.001f); + var wideCoverage = widePage.Count(value => value > 0.001f); + + Assert.True(wideCoverage > narrowCoverage); + } }