diff --git a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs index 612d15e..9aa4120 100644 --- a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs +++ b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs @@ -407,12 +407,15 @@ public static class BokushoBrushSimulation var paperKey = stroke.SourceStrokeId >= 0 ? stroke.SourceStrokeId : strokeIndex; var tooth = PaperTooth(world, paperKey); var edge = Saturate(distance / MathF.Max(footprint * 0.80f, 0.001f)); + var dryIslandLift = SmoothStep(0.66f, 0.20f, Saturate(frame.Wetness / 1.6f)); 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); var centerSample = Math.Clamp((int)MathF.Round(bestT * (sampleCount - 1)), 0, sampleCount - 1); var centerTuft = Math.Clamp((int)MathF.Round(tuftT * MathF.Max(tuftCount - 1.0f, 0.0f)), 0, tuftCount - 1); var pigmentPeak = 0.0f; var pigmentFlow = 0.0f; + var dryIslandPeak = 0.0f; + var dryFleckPeak = 0.0f; for (var sampleDelta = -3; sampleDelta <= 3; sampleDelta++) { @@ -474,6 +477,7 @@ public static class BokushoBrushSimulation var previousIndex = ((previousStrokeIndex * tuftCount) + tuftIndex) * sampleCount + previousSampleIndex; var tip = tips[index]; var previousTip = tips[previousIndex]; + var pigment = canvas[index]; var tipPoint = new Vector2(tip.X, tip.Y); var previousTipPoint = new Vector2(previousTip.X, previousTip.Y); var sweep = tipPoint - previousTipPoint; @@ -490,7 +494,7 @@ public static class BokushoBrushSimulation var ellipse = MathF.Sqrt(normalDistance * normalDistance + tangentDistance * tangentDistance); var tipContact = MathF.Max(SmoothStep(1.0f, 0.0f, ellipse), sweepContact * 0.86f); var longitudinalGate = SmoothStep(1.0f, 0.0f, MathF.Abs(tangentDistance) * 0.62f); - var contribution = canvas[index] + var contribution = pigment * tipContact * (0.006f + contactCore * 0.994f) * (0.06f + longitudinalGate * 0.94f) @@ -498,11 +502,50 @@ public static class BokushoBrushSimulation * (0.82f + sweepContact * 0.24f); pigmentPeak = MathF.Max(pigmentPeak, contribution); pigmentFlow += contribution; + + var laneT = tuftCount <= 1 ? 0.5f : tuftIndex / (float)(tuftCount - 1); + var laneEdge = MathF.Abs(laneT * 2.0f - 1.0f); + var islandSeedPoint = contactPoint * 34.0f + new Vector2(tuftIndex * 0.19f, sampleIndex * 0.13f); + var islandSeed = HashNoiseCell(islandSeedPoint, paperKey, 0x510E527Fu); + var islandPigment = SmoothStep(0.006f, 0.075f, pigment) * (1.0f - SmoothStep(0.22f, 0.55f, pigment)); + var islandGate = SmoothStep(0.38f, 0.88f, laneEdge) + * dryIslandLift + * islandPigment + * SmoothStep(0.25f, 0.88f, tooth) + * SmoothStep(0.42f, 0.92f, islandSeed); + if (islandGate > 0.0f) + { + var side = islandSeed < 0.78f ? -1.0f : 1.0f; + var islandCenter = contactPoint + + sampleNormal * side * tip.Z * (0.88f + laneEdge * 0.55f) + + sampleTangent * (islandSeed - 0.5f) * tip.W * 0.22f; + var islandRadius = MathF.Max(tip.Z * (0.30f + laneEdge * 0.22f), 0.060f); + var islandDistance = MathF.Sqrt(Vector2.DistanceSquared(world, islandCenter)); + var islandContact = SmoothStep(1.0f, 0.0f, islandDistance / islandRadius); + dryIslandPeak = MathF.Max(dryIslandPeak, pigment * islandGate * islandContact * 5.2f); + + var fleckSeedPoint = contactPoint * 61.0f + new Vector2(sampleIndex * 0.17f, tuftIndex * 0.23f); + var fleckSeed = HashNoiseCell(fleckSeedPoint, paperKey, 0xF1E57A2Du); + var fleckGate = islandGate * SmoothStep(0.44f, 0.88f, fleckSeed); + if (fleckGate > 0.0f) + { + var fleckSide = fleckSeed < 0.86f ? side : -side; + var fleckCenter = contactPoint + + sampleNormal * fleckSide * tip.Z * (2.30f + laneEdge * 1.05f + fleckSeed * 0.55f) + + sampleTangent * (fleckSeed - 0.5f) * tip.W * 0.50f; + var fleckRadius = MathF.Max(tip.Z * (0.12f + laneEdge * 0.05f), 0.030f); + var fleckDistance = MathF.Sqrt(Vector2.DistanceSquared(world, fleckCenter)); + var fleckContact = SmoothStep(1.0f, 0.0f, fleckDistance / fleckRadius); + dryFleckPeak = MathF.Max(dryFleckPeak, pigment * fleckGate * fleckContact * 22.0f); + } + } } } var decisiveInk = Saturate((pigmentPeak - 0.012f) * 2.25f + pigmentFlow * 0.002f); - return decisiveInk * hold * projectionGate * (0.36f + pressure * 0.44f + Saturate(frame.InkLoad * 0.5f) * 0.20f); + var islandInk = Saturate((dryIslandPeak - 0.001f) * 14.0f); + var fleckInk = Saturate((dryFleckPeak - 0.001f) * 36.0f); + return Saturate(decisiveInk + islandInk + fleckInk) * hold * projectionGate * (0.36f + pressure * 0.44f + Saturate(frame.InkLoad * 0.5f) * 0.20f); } private static bool CanBorrowSourceSample(IReadOnlyList strokes, int strokeIndex, int candidateIndex) diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl index dc132e9..396eaa1 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl @@ -323,6 +323,7 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro uint paperKey = stroke.p3.w >= 0.0 ? (uint)round(stroke.p3.w) : strokeIndex; float tooth = bokushoPaperTooth(world, paperKey); float edge = saturate(distance / max(footprint * 0.80, 0.001)); + float dryIslandLift = smoothstep(0.66, 0.20, saturate(bokushoMaterial.w / 1.6)); 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); @@ -331,6 +332,8 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro uint centerTuft = min((uint)round(tuftT * (float)max((int)tuftCount - 1, 0)), tuftCount - 1u); float pigmentPeak = 0.0; float pigmentFlow = 0.0; + float dryIslandPeak = 0.0; + float dryFleckPeak = 0.0; [unroll] for (int sampleDelta = -3; sampleDelta <= 3; sampleDelta += 1) @@ -414,11 +417,50 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro * (0.82 + sweepContact * 0.24); pigmentPeak = max(pigmentPeak, contribution); pigmentFlow += contribution; + + float laneT = tuftCount <= 1u ? 0.5 : (float)tuftIndex / (float)(tuftCount - 1u); + float laneEdge = abs(laneT * 2.0 - 1.0); + float2 islandSeedPoint = contactPoint * 34.0 + float2((float)tuftIndex * 0.19, (float)sampleIndex * 0.13); + float islandSeed = bokushoHashNoiseCell(islandSeedPoint, paperKey, 0x510E527Fu); + float islandPigment = smoothstep(0.006, 0.075, pigment) * (1.0 - smoothstep(0.22, 0.55, pigment)); + float islandGate = smoothstep(0.38, 0.88, laneEdge) + * dryIslandLift + * islandPigment + * smoothstep(0.25, 0.88, tooth) + * smoothstep(0.42, 0.92, islandSeed); + if (islandGate > 0.0) + { + float side = islandSeed < 0.78 ? -1.0 : 1.0; + float2 islandCenter = contactPoint + + sampleNormal * side * tip.z * (0.88 + laneEdge * 0.55) + + sampleTangent * (islandSeed - 0.5) * tip.w * 0.22; + float islandRadius = max(tip.z * (0.30 + laneEdge * 0.22), 0.060); + float islandDistance = length(world - islandCenter); + float islandContact = smoothstep(1.0, 0.0, islandDistance / islandRadius); + dryIslandPeak = max(dryIslandPeak, pigment * islandGate * islandContact * 5.2); + + float2 fleckSeedPoint = contactPoint * 61.0 + float2((float)sampleIndex * 0.17, (float)tuftIndex * 0.23); + float fleckSeed = bokushoHashNoiseCell(fleckSeedPoint, paperKey, 0xF1E57A2Du); + float fleckGate = islandGate * smoothstep(0.44, 0.88, fleckSeed); + if (fleckGate > 0.0) + { + float fleckSide = fleckSeed < 0.86 ? side : -side; + float2 fleckCenter = contactPoint + + sampleNormal * fleckSide * tip.z * (2.30 + laneEdge * 1.05 + fleckSeed * 0.55) + + sampleTangent * (fleckSeed - 0.5) * tip.w * 0.50; + float fleckRadius = max(tip.z * (0.12 + laneEdge * 0.05), 0.030); + float fleckDistance = length(world - fleckCenter); + float fleckContact = smoothstep(1.0, 0.0, fleckDistance / fleckRadius); + dryFleckPeak = max(dryFleckPeak, pigment * fleckGate * fleckContact * 22.0); + } + } } } float decisiveInk = saturate((pigmentPeak - 0.012) * 2.25 + pigmentFlow * 0.002); - return decisiveInk * hold * projectionGate * (0.36 + pressure * 0.44 + saturate(bokushoMaterial.z * 0.5) * 0.20); + float islandInk = saturate((dryIslandPeak - 0.001) * 14.0); + float fleckInk = saturate((dryFleckPeak - 0.001) * 36.0); + return saturate(decisiveInk + islandInk + fleckInk) * hold * projectionGate * (0.36 + pressure * 0.44 + saturate(bokushoMaterial.z * 0.5) * 0.20); } float bokushoPageHeight(float2 world) diff --git a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs index a3cd6f0..a729338 100644 --- a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs +++ b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs @@ -364,6 +364,22 @@ public sealed class BokushoBrushSimulationTests Assert.True(edgeRoughness > centerRoughness * 1.20f, $"center={centerRoughness:0.000000}; edge={edgeRoughness:0.000000}"); } + [Fact] + public void CpuBrushProjectionCreatesDryEdgeIslandsFromTuftState() + { + var dry = LaneCohesionFrame(wetness: 0.52f, inkLoad: 1.46f, splay: 1.12f); + var dryResult = BokushoBrushSimulation.Evaluate(dry); + var strippedCanvas = WithoutEdgeTuftPigment(dryResult.Canvas, dryResult.SampleCount, dryResult.TuftCount); + var dryPage = BokushoBrushSimulation.ProjectCanvasToPage(dry, dryResult.Canvas, dryResult.Tips, 128, 128, Vector2.Zero, 4.0f); + var strippedPage = BokushoBrushSimulation.ProjectCanvasToPage(dry, strippedCanvas, dryResult.Tips, 128, 128, Vector2.Zero, 4.0f); + var dryFringe = CountFringeInk(dryPage, 128, 128, viewRadius: 4.0f, threshold: 0.018f); + var strippedFringe = CountFringeInk(strippedPage, 128, 128, viewRadius: 4.0f, threshold: 0.018f); + var dryCore = SamplePage(dryPage, 128, 128, Vector2.Zero, 4.0f); + + Assert.True(dryCore > 0.01f); + Assert.True(dryFringe > strippedFringe + 8, $"full={dryFringe}; stripped={strippedFringe}"); + } + [Fact] public void CpuBrushSourceStrokeIdPreventsAccidentalSegmentReplay() { @@ -792,6 +808,50 @@ public sealed class BokushoBrushSimulationTests return variation / MathF.Max(total, 0.001f); } + private static int CountFringeInk(IReadOnlyList page, int width, int height, float viewRadius, float threshold) + { + var count = 0; + for (var y = 0; y < height; y++) + { + var uvY = height <= 1 ? 0.0f : y / (float)(height - 1); + var worldY = (uvY * 2.0f - 1.0f) * viewRadius; + if (MathF.Abs(worldY) < 0.42f || MathF.Abs(worldY) > 1.35f) + { + continue; + } + + for (var x = 0; x < width; x++) + { + if (page[y * width + x] > threshold) + { + count++; + } + } + } + + return count; + } + + private static float[] WithoutEdgeTuftPigment(IReadOnlyList canvas, int sampleCount, int tuftCount) + { + var stripped = canvas.ToArray(); + for (var tuft = 0; tuft < tuftCount; tuft++) + { + var laneT = tuftCount <= 1 ? 0.5f : tuft / (float)(tuftCount - 1); + if (MathF.Abs(laneT * 2.0f - 1.0f) < 0.28f) + { + continue; + } + + for (var sample = 0; sample < sampleCount; sample++) + { + stripped[tuft * sampleCount + sample] = 0.0f; + } + } + + return stripped; + } + private static float[] LastSampleOnly(IReadOnlyList canvas, int sampleCount, int tuftCount) { var sparse = new float[canvas.Count];