diff --git a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs index 3b74446..836a068 100644 --- a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs +++ b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs @@ -493,6 +493,10 @@ public static class BokushoBrushSimulation var tip = tips[index]; var previousTip = tips[previousIndex]; var pigment = canvas[index]; + var nextSampleIndex = Math.Clamp(sampleIndex + 1, 0, sampleCount - 1); + var nextIndex = ((sampleStrokeIndex * tuftCount) + tuftIndex) * sampleCount + nextSampleIndex; + var neighborPigment = MathF.Max(canvas[previousIndex], canvas[nextIndex]); + var releasePigment = SmoothStep(0.12f, 0.68f, pigment) * SmoothStep(0.030f, 0.16f, pigment - neighborPigment); var tipPoint = new Vector2(tip.X, tip.Y); var previousTipPoint = new Vector2(previousTip.X, previousTip.Y); var sweep = tipPoint - previousTipPoint; @@ -522,7 +526,8 @@ public static class BokushoBrushSimulation 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 thinPigment = SmoothStep(0.006f, 0.075f, pigment) * (1.0f - SmoothStep(0.22f, 0.55f, pigment)); + var islandPigment = MathF.Max(thinPigment, releasePigment * 0.72f); var islandGate = SmoothStep(0.38f, 0.88f, laneEdge) * dryIslandLift * islandPigment @@ -541,7 +546,7 @@ public static class BokushoBrushSimulation 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); + var fleckGate = islandGate * SmoothStep(0.44f - releasePigment * 0.18f, 0.88f, fleckSeed) * (1.0f + releasePigment * 1.35f); if (fleckGate > 0.0f) { var fleckSide = fleckSeed < 0.86f ? side : -side; diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl index 396eaa1..5c10551 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl @@ -396,6 +396,9 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro uint previousSampleIndex = min((uint)max(previousSampleIndexValue, 0), sampleCount - 1u); float4 previousTip = bokushoTipSample(previousStrokeIndex, previousSampleIndex, tuftIndex); + uint nextSampleIndex = min(sampleIndex + 1u, sampleCount - 1u); + float previousPigment = BokushoCanvasField[((previousStrokeIndex * tuftCount) + tuftIndex) * sampleCount + previousSampleIndex]; + float nextPigment = BokushoCanvasField[((sampleStrokeIndex * tuftCount) + tuftIndex) * sampleCount + nextSampleIndex]; float2 sweep = tip.xy - previousTip.xy; float sweepLengthSquared = dot(sweep, sweep); float sweepT = sweepLengthSquared <= 0.000001 ? 1.0 : saturate(dot(world - previousTip.xy, sweep) / sweepLengthSquared); @@ -409,6 +412,8 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro float tipContact = max(smoothstep(1.0, 0.0, ellipse), sweepContact * 0.86); float longitudinalGate = smoothstep(1.0, 0.0, abs(tangentDistance) * 0.62); float pigment = BokushoCanvasField[((sampleStrokeIndex * tuftCount) + tuftIndex) * sampleCount + sampleIndex]; + float neighborPigment = max(previousPigment, nextPigment); + float releasePigment = smoothstep(0.12, 0.68, pigment) * smoothstep(0.030, 0.16, pigment - neighborPigment); float contribution = pigment * tipContact * (0.006 + contactCore * 0.994) @@ -422,7 +427,8 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro 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 thinPigment = smoothstep(0.006, 0.075, pigment) * (1.0 - smoothstep(0.22, 0.55, pigment)); + float islandPigment = max(thinPigment, releasePigment * 0.72); float islandGate = smoothstep(0.38, 0.88, laneEdge) * dryIslandLift * islandPigment @@ -441,7 +447,7 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro 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); + float fleckGate = islandGate * smoothstep(0.44 - releasePigment * 0.18, 0.88, fleckSeed) * (1.0 + releasePigment * 1.35); if (fleckGate > 0.0) { float fleckSide = fleckSeed < 0.86 ? side : -side; diff --git a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs index cc94135..86ae452 100644 --- a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs +++ b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs @@ -393,6 +393,18 @@ public sealed class BokushoBrushSimulationTests Assert.True(dryFringe > strippedFringe + 8, $"full={dryFringe}; stripped={strippedFringe}"); } + [Fact] + public void CpuBrushProjectionLiftsIsolatedReleasePigmentAsFringeFlecks() + { + var dry = LaneCohesionFrame(wetness: 0.42f, inkLoad: 1.46f, splay: 1.16f); + var result = BokushoBrushSimulation.Evaluate(dry); + var isolatedCanvas = IsolatedEdgeReleaseCanvas(result.Canvas, result.SampleCount, result.TuftCount); + var isolatedPage = BokushoBrushSimulation.ProjectCanvasToPage(dry, isolatedCanvas, result.Tips, 128, 128, Vector2.Zero, 4.0f); + var isolatedSmallComponents = CountSmallPageComponents(isolatedPage, 128, 128, threshold: 0.018f, maxSize: 28); + + Assert.True(isolatedSmallComponents > 12, $"isolated={isolatedSmallComponents}"); + } + [Fact] public void CpuBrushSourceStrokeIdPreventsAccidentalSegmentReplay() { @@ -871,6 +883,57 @@ public sealed class BokushoBrushSimulationTests return count; } + private static int CountSmallPageComponents(IReadOnlyList page, int width, int height, float threshold, int maxSize) + { + var visited = new bool[page.Count]; + var stack = new Stack(); + var components = 0; + for (var index = 0; index < page.Count; index++) + { + if (visited[index] || page[index] <= threshold) + { + continue; + } + + var size = 0; + visited[index] = true; + stack.Push(index); + while (stack.Count > 0) + { + var current = stack.Pop(); + size++; + var x = current % width; + var y = current / width; + TryVisit(x - 1, y); + TryVisit(x + 1, y); + TryVisit(x, y - 1); + TryVisit(x, y + 1); + } + + if (size <= maxSize) + { + components++; + } + } + + return components; + + void TryVisit(int x, int y) + { + if (x < 0 || x >= width || y < 0 || y >= height) + { + return; + } + + var next = y * width + x; + if (!visited[next] && page[next] > threshold) + { + visited[next] = true; + stack.Push(next); + } + } + } + private static float[] WithoutEdgeTuftPigment(IReadOnlyList canvas, int sampleCount, int tuftCount) { var stripped = canvas.ToArray(); @@ -891,6 +954,26 @@ public sealed class BokushoBrushSimulationTests return stripped; } + private static float[] IsolatedEdgeReleaseCanvas(IReadOnlyList canvas, int sampleCount, int tuftCount) + { + var isolated = new float[canvas.Count]; + 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.55f) + { + continue; + } + + for (var sample = 2; sample < sampleCount - 2; sample += 9) + { + isolated[tuft * sampleCount + sample] = MathF.Max(canvas[tuft * sampleCount + sample], 0.42f); + } + } + + return isolated; + } + private static float[] LastSampleOnly(IReadOnlyList canvas, int sampleCount, int tuftCount) { var sparse = new float[canvas.Count];