From baa77171b58147bd6e88237cab7dc9fcc616b7f5 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 13 Jun 2026 11:14:05 +0100 Subject: [PATCH] Improve Bokusho page ink projection --- .../Render/BokushoBrushSimulation.cs | 31 ++++++++++- src/Aquarium.Engine/Render/D3D12Renderer.cs | 2 +- .../Render/Shaders/D3D12BokushoBrush.hlsl | 4 +- .../Render/Shaders/D3D12HeightField.hlsl | 31 ++++++++++- .../Render/Shaders/D3D12Scene.hlsl | 2 +- .../BokushoBrushSimulationTests.cs | 54 +++++++++++++++++++ 6 files changed, 116 insertions(+), 8 deletions(-) diff --git a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs index 312dfbc..36161a4 100644 --- a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs +++ b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs @@ -244,14 +244,18 @@ public static class BokushoBrushSimulation var samplePosition = bestT * MathF.Max(sampleCount - 1.0f, 1.0f); var tuftPosition = tuftT * MathF.Max(tuftCount - 1.0f, 0.0f); var pigment = BilinearCanvasSample(canvas, strokeIndex, tuftCount, sampleCount, samplePosition, tuftPosition); - return pigment * contact * (0.10f + pressure * 0.18f + Saturate(frame.InkLoad * 0.5f) * 0.08f); + var tooth = PaperTooth(world, strokeIndex); + var edge = Saturate(distance / MathF.Max(footprint * 0.80f, 0.001f)); + var dryBreak = SmoothStep(0.18f + tooth * 0.18f, 0.92f, edge) * (1.0f - Saturate(frame.Wetness / 1.6f)) * (0.34f + stroke.SplitScale * 0.12f); + var hold = Saturate(0.52f + tooth * 0.42f + pressure * 0.28f - dryBreak); + return pigment * contact * hold * (0.10f + pressure * 0.18f + Saturate(frame.InkLoad * 0.5f) * 0.08f); } private static float StrokeTaper(float t, float entryTaper, float exitTaper) { 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; + return 0.04f + entry * exit * 0.96f; } private static float LaneHash(int strokeIndex, int tuft, uint salt) @@ -265,6 +269,29 @@ public static class BokushoBrushSimulation return (value & 0x00FFFFFFu) / 16777215.0f; } + private static float PaperTooth(Vector2 world, int strokeIndex) + { + var fine = HashNoiseCell(world * 22.0f, strokeIndex, 0x6A09E667u); + var fiber = HashNoiseCell(new Vector2(world.X * 7.0f + world.Y * 0.35f, world.Y * 2.4f), strokeIndex, 0xBB67AE85u); + return Saturate(fine * 0.58f + fiber * 0.42f); + } + + private static float HashNoiseCell(Vector2 point, int strokeIndex, uint salt) + { + unchecked + { + var x = (uint)(int)MathF.Floor(point.X); + var y = (uint)(int)MathF.Floor(point.Y); + var value = (uint)(strokeIndex + 1) * 0x9E3779B9u ^ x * 0x85EBCA6Bu ^ y * 0xC2B2AE35u ^ salt; + value ^= value >> 16; + value *= 0x7FEB352Du; + value ^= value >> 15; + value *= 0x846CA68Bu; + value ^= value >> 16; + return (value & 0x00FFFFFFu) / 16777215.0f; + } + } + private static float BilinearCanvasSample(ReadOnlySpan canvas, int strokeIndex, int tuftCount, int sampleCount, float samplePosition, float tuftPosition) { var sample0 = Math.Clamp((int)MathF.Floor(samplePosition), 0, sampleCount - 1); diff --git a/src/Aquarium.Engine/Render/D3D12Renderer.cs b/src/Aquarium.Engine/Render/D3D12Renderer.cs index 8f67c55..f3aee4c 100644 --- a/src/Aquarium.Engine/Render/D3D12Renderer.cs +++ b/src/Aquarium.Engine/Render/D3D12Renderer.cs @@ -29,7 +29,7 @@ namespace Aquarium.Engine.Render; public sealed class D3D12Renderer : IAquariumRenderer { private const int BackBufferCount = 2; - private const int HeightFieldTextureSize = 128; + private const int HeightFieldTextureSize = 512; private const Format HeightFieldFormat = Format.R16_Float; private const Format SceneDepthFormat = Format.D32_Float; private const int MaxSdfLightCount = 64; diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl index 2261e89..f4efb05 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl @@ -43,7 +43,7 @@ float StrokeTaper(float t) { float entry = smoothstep(0.0, 0.10, t); float exit = 1.0 - smoothstep(0.86, 1.0, t); - return 0.18 + entry * exit * 0.82; + return 0.04 + entry * exit * 0.96; } float StrokeTaper(BokushoBrushStroke stroke, float t) @@ -52,7 +52,7 @@ float StrokeTaper(BokushoBrushStroke stroke, float t) 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; + return 0.04 + entry * exit * 0.96; } float LaneHash(uint strokeIndex, uint tuft, uint salt) diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl index c3f5ca7..7c65d4b 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl @@ -110,7 +110,7 @@ float bokushoStrokeTaper(BokushoBrushStroke stroke, float t) 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; + return 0.04 + entry * exit * 0.96; } float bokushoCanvasSample(uint strokeIndex, float sampleIndex, float tuftIndex) @@ -135,6 +135,27 @@ float bokushoCanvasSample(uint strokeIndex, float sampleIndex, float tuftIndex) return saturate(cultmath_lerp(lower, upper, tuftBlend)); } +float bokushoHashNoiseCell(float2 paperPoint, uint strokeIndex, uint salt) +{ + int2 cell = int2(floor(paperPoint)); + uint x = (uint)cell.x; + uint y = (uint)cell.y; + uint value = (strokeIndex + 1u) * 0x9E3779B9u ^ x * 0x85EBCA6Bu ^ y * 0xC2B2AE35u ^ salt; + value ^= value >> 16; + value *= 0x7FEB352Du; + value ^= value >> 15; + value *= 0x846CA68Bu; + value ^= value >> 16; + return (float)(value & 0x00FFFFFFu) / 16777215.0; +} + +float bokushoPaperTooth(float2 world, uint strokeIndex) +{ + float fine = bokushoHashNoiseCell(world * 22.0, strokeIndex, 0x6A09E667u); + float fiber = bokushoHashNoiseCell(float2(world.x * 7.0 + world.y * 0.35, world.y * 2.4), strokeIndex, 0xBB67AE85u); + return saturate(fine * 0.58 + fiber * 0.42); +} + float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint strokeIndex) { float bestDistance = 1.0e20; @@ -190,7 +211,13 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro float sampleIndex = bestT * max(bokushoShape.x - 1.0, 1.0); float tuftIndex = tuftT * max(bokushoShape.y - 1.0, 0.0); float pigment = bokushoCanvasSample(strokeIndex, sampleIndex, tuftIndex); - return pigment * contact * (0.10 + pressure * 0.18 + saturate(bokushoMaterial.z * 0.5) * 0.08); + float tooth = bokushoPaperTooth(world, strokeIndex); + float edge = saturate(distance / max(footprint * 0.80, 0.001)); + float dryBreak = smoothstep(0.18 + tooth * 0.18, 0.92, edge) + * (1.0 - saturate(bokushoMaterial.w / 1.6)) + * (0.34 + stroke.dynamics.w * 0.12); + float hold = saturate(0.52 + tooth * 0.42 + pressure * 0.28 - dryBreak); + return pigment * contact * hold * (0.10 + pressure * 0.18 + saturate(bokushoMaterial.z * 0.5) * 0.08); } float bokushoPageHeight(float2 world) diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl index 8f7c56f..53363dc 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl @@ -34,7 +34,7 @@ SamplerState linearSampler : register(s0); static const float FIELD_ID_HEIGHT_FIELD = 4.0; static const float PI = 3.14159265359; -static const float HEIGHT_FIELD_TEXEL_COUNT = 128.0; +static const float HEIGHT_FIELD_TEXEL_COUNT = 512.0; static const float SURFACE_FLAT_REFLECTION_MAX_LOD = 3.0; static const float BACKGROUND_PMREM_LOD = 3.0; static const float BACKGROUND_PMREM_CONE = 0.16; diff --git a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs index 3d0f5ca..e0d26e1 100644 --- a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs +++ b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs @@ -224,6 +224,60 @@ public sealed class BokushoBrushSimulationTests 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)); } + [Fact] + public void CpuBrushPageProjectionAppliesDryPaperEdgeBreakup() + { + var dry = new AquariumBokushoBrushFrame + { + TuftCount = 13, + SampleCount = 64, + PhysicsHz = 500.0f, + BrushRadius = 1.8f, + Pressure = 0.64f, + InkLoad = 1.0f, + Wetness = 0.0f, + Splay = 0.88f, + Strokes = + [ + new AquariumBokushoBrushStroke + { + StrokeP0 = new Vector4(-3.2f, 0.0f, 0.0f, 0.0f), + StrokeP1 = new Vector4(-2.0f, 0.0f, 0.0f, 0.0f), + StrokeP2 = new Vector4(2.0f, 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.84f, + TangentScale = 1.0f, + EntryTaper = 0.08f, + ExitTaper = 0.08f, + PigmentScale = 1.0f, + SplitScale = 1.5f, + }, + ], + }; + var wet = new AquariumBokushoBrushFrame + { + TuftCount = dry.TuftCount, + SampleCount = dry.SampleCount, + PhysicsHz = dry.PhysicsHz, + BrushRadius = dry.BrushRadius, + Pressure = dry.Pressure, + InkLoad = dry.InkLoad, + Wetness = 1.6f, + Splay = dry.Splay, + Strokes = dry.Strokes, + }; + var canvas = Enumerable.Repeat(1.0f, dry.TuftCount * dry.SampleCount).ToArray(); + + var dryPage = BokushoBrushSimulation.ProjectCanvasToPage(dry, canvas, 96, 96, Vector2.Zero, 4.0f); + var wetPage = BokushoBrushSimulation.ProjectCanvasToPage(wet, canvas, 96, 96, Vector2.Zero, 4.0f); + var edge = new Vector2(0.0f, 0.66f); + + Assert.True(wetPage.Sum() > dryPage.Sum() * 1.05f); + Assert.True(SamplePage(wetPage, 96, 96, edge, 4.0f) > SamplePage(dryPage, 96, 96, edge, 4.0f)); + } + private static AquariumBokushoBrushFrame StrokeDynamicsFrame(float pigmentScale, float entryTaper) { return new AquariumBokushoBrushFrame