Preserve Bokusho source stroke continuity

This commit is contained in:
Meta Vens 2026-06-16 05:36:46 +01:00
commit ee1b92a94c
3 changed files with 55 additions and 13 deletions

View file

@ -206,10 +206,10 @@ public static class BokushoBrushSimulation
for (var replayStrokeIndex = chainStart; replayStrokeIndex < strokeIndex; replayStrokeIndex++)
{
SimulateTuftSegmentToPage(strokes[replayStrokeIndex], replayStrokeIndex, chainStart, tuft, sampleCount, frame.PhysicsHz, frame.Pressure, frame.BrushRadius, splay, bend, friction, restOffset, edge, ref offset, ref tip, ref stateLoad, ref stateWet, densityPage, width, height, viewCenter, safeRadius, writeOutput: false);
SimulateTuftSegmentToPage(strokes[replayStrokeIndex], replayStrokeIndex, chainStart, tuft, sampleCount, frame.PhysicsHz, frame.Pressure, frame.BrushRadius, splay, bend, friction, restOffset, edge, ref offset, ref tip, ref stateLoad, ref stateWet, densityPage, width, height, viewCenter, safeRadius, writeOutput: false, writeInitialStep: true);
}
SimulateTuftSegmentToPage(stroke, strokeIndex, chainStart, tuft, sampleCount, frame.PhysicsHz, frame.Pressure, frame.BrushRadius, splay, bend, friction, restOffset, edge, ref offset, ref tip, ref stateLoad, ref stateWet, densityPage, width, height, viewCenter, safeRadius, writeOutput: true);
SimulateTuftSegmentToPage(stroke, strokeIndex, chainStart, tuft, sampleCount, frame.PhysicsHz, frame.Pressure, frame.BrushRadius, splay, bend, friction, restOffset, edge, ref offset, ref tip, ref stateLoad, ref stateWet, densityPage, width, height, viewCenter, safeRadius, writeOutput: true, writeInitialStep: strokeIndex == chainStart);
}
}
}
@ -282,7 +282,7 @@ public static class BokushoBrushSimulation
var separation = Saturate(split * 0.34f + tension * 0.46f + edge * 0.20f - cohesion * 0.24f);
var adhesion = Saturate(stateWet * (0.36f + cohesion * 0.40f) + localPressure * 0.10f - separation * 0.22f);
var dryMemory = Saturate((1.0f - stateWet) * 0.62f + separation * 0.32f + velocity * 0.004f);
var fiberNoise = LaneHash(laneKey + sample * 13, tuft, 101);
var fiberNoise = LaneHash(laneKey + StrokeNoiseStep(strokeT) * 13, tuft, 101);
var continuity = 1.0f - SmoothStep(0.18f + dryMemory * 0.28f, 0.96f, fiberNoise) * dryMemory * (0.38f + edge * 0.22f);
var contactTransfer = contact * stateLoad * (0.16f + stateWet * 0.92f) * (0.30f + drag * 0.64f + localPressure * 0.22f + sweptPatch * 0.18f) * (0.68f + laneCore * 0.50f - separation * 0.14f) * continuity;
var airborneRelease = (1.0f - contact) * stateLoad * stateWet * Saturate(velocity * 0.010f - adhesion * 0.16f) * (0.20f + separation * 0.42f + edge * 0.18f);
@ -328,7 +328,8 @@ public static class BokushoBrushSimulation
int height,
Vector2 viewCenter,
float viewRadius,
bool writeOutput)
bool writeOutput,
bool writeInitialStep)
{
var pressure = Saturate(framePressure * stroke.PressureScale * 0.5f) * 2.0f;
var radius = MathF.Max(frameBrushRadius * stroke.RadiusScale, 0.0001f);
@ -373,7 +374,7 @@ public static class BokushoBrushSimulation
var separation = Saturate(split * 0.34f + tension * 0.46f + edge * 0.20f - cohesion * 0.24f);
var adhesion = Saturate(stateWet * (0.36f + cohesion * 0.40f) + localPressure * 0.10f - separation * 0.22f);
var dryMemory = Saturate((1.0f - stateWet) * 0.62f + separation * 0.32f + velocity * 0.004f);
var fiberNoise = LaneHash(laneKey + step * 13, tuft, 101);
var fiberNoise = LaneHash(laneKey + StrokeNoiseStep(strokeT) * 13, tuft, 101);
var continuity = 1.0f - SmoothStep(0.18f + dryMemory * 0.28f, 0.96f, fiberNoise) * dryMemory * (0.38f + edge * 0.22f);
var contactTransfer = contact * stateLoad * (0.16f + stateWet * 0.92f) * (0.30f + drag * 0.64f + localPressure * 0.22f + sweptPatch * 0.18f) * (0.68f + laneCore * 0.50f - separation * 0.14f) * continuity;
var airborneRelease = (1.0f - contact) * stateLoad * stateWet * Saturate(velocity * 0.010f - adhesion * 0.16f) * (0.20f + separation * 0.42f + edge * 0.18f);
@ -381,7 +382,7 @@ public static class BokushoBrushSimulation
stateLoad = MathF.Max(0.0f, stateLoad - consumedPigment * segmentSpan * (0.010f + localPressure * 0.006f));
stateWet = MathF.Max(0.0f, stateWet - consumedPigment * segmentSpan * (0.006f + dryMemory * 0.003f));
if (writeOutput)
if (writeOutput && (writeInitialStep || step > 0))
{
var pigment = (contactTransfer * (0.95f + localPressure * 0.34f + sweptPatch * 0.24f) + airborneRelease * (1.6f + velocity * 0.002f)) * stroke.PigmentScale;
var patchNormalRadius = localNormalRadius * (0.84f + laneCore * 0.20f + localPressure * 0.14f + sweptPatch * 0.08f - separation * 0.10f);
@ -738,6 +739,11 @@ public static class BokushoBrushSimulation
return Saturate(Lerp(start, end, t));
}
private static int StrokeNoiseStep(float strokeT)
{
return (int)MathF.Floor(Saturate(strokeT) * 4096.0f + 0.5f);
}
private static float LaneCohesion(float edge, float split, float wetness, float pressure)
{
return Saturate(0.50f + (1.0f - edge) * 0.38f + wetness * 0.22f + pressure * 0.12f - split * 0.18f);

View file

@ -69,9 +69,14 @@ float StrokeTaper(float t)
return 0.04 + entry * exit * 0.96;
}
float StrokeProgress(BokushoBrushStroke stroke, float t)
{
return saturate(lerp(stroke.p0.z, max(stroke.p0.z, stroke.p0.w), t));
}
float StrokeTaper(BokushoBrushStroke stroke, float t)
{
float strokeT = saturate(lerp(stroke.p0.z, max(stroke.p0.z, stroke.p0.w), t));
float strokeT = StrokeProgress(stroke, t);
float entryTaper = clamp(stroke.dynamics.x, 0.01, 0.50);
float exitTaper = clamp(stroke.dynamics.y, 0.01, 0.50);
float entry = cultmath_smoothstep(0.0, entryTaper, strokeT);
@ -81,6 +86,11 @@ float StrokeTaper(BokushoBrushStroke stroke, float t)
return 0.018 + contact * 0.982;
}
uint StrokeNoiseStep(float strokeT)
{
return (uint)floor(saturate(strokeT) * 4096.0 + 0.5);
}
float LaneCohesion(float edge, float split, float wetness, float pressure)
{
return saturate(0.50 + (1.0 - edge) * 0.38 + wetness * 0.22 + pressure * 0.12 - split * 0.18);
@ -267,6 +277,7 @@ void SimulateBokushoTuftSegment(
float2 center = StrokePoint(stroke, t);
float2 tangent = StrokeTangent(stroke, t);
float2 normal = float2(-tangent.y, tangent.x);
float strokeT = StrokeProgress(stroke, t);
float taper = StrokeTaper(stroke, t);
float localPressure = pressure * taper;
float laneCore = cultmath_smoothstep(0.0, 0.78, 1.0 - edge);
@ -294,7 +305,7 @@ void SimulateBokushoTuftSegment(
float separation = saturate(split * 0.34 + tension * 0.46 + edge * 0.20 - cohesion * 0.24);
float adhesion = saturate(stateWet * (0.36 + cohesion * 0.40) + localPressure * 0.10 - separation * 0.22);
float dryMemory = saturate((1.0 - stateWet) * 0.62 + separation * 0.32 + velocity * 0.004);
float fiberNoise = LaneHash(laneKey + sample * 13u, tuft, 101u);
float fiberNoise = LaneHash(laneKey + StrokeNoiseStep(strokeT) * 13u, tuft, 101u);
float continuity = 1.0 - cultmath_smoothstep(0.18 + dryMemory * 0.28, 0.96, fiberNoise) * dryMemory * (0.38 + edge * 0.22);
float contactTransfer = contact * stateLoad * (0.16 + stateWet * 0.92) * (0.30 + drag * 0.64 + localPressure * 0.22 + sweptPatch * 0.18) * (0.68 + laneCore * 0.50 - separation * 0.14) * continuity;
float airborneRelease = (1.0 - contact) * stateLoad * stateWet * saturate(velocity * 0.010 - adhesion * 0.16) * (0.20 + separation * 0.42 + edge * 0.18);
@ -329,7 +340,8 @@ void SimulateBokushoTuftSegmentToPage(
inout float2 tip,
inout float stateLoad,
inout float stateWet,
bool writeOutput)
bool writeOutput,
bool writeInitialStep)
{
float pressure = saturate(brushMaterial.y * stroke.profile.y * 0.5) * 2.0;
float splay = saturate(brushDynamics.x / 2.0);
@ -350,6 +362,7 @@ void SimulateBokushoTuftSegmentToPage(
float2 center = StrokePoint(stroke, t);
float2 tangent = StrokeTangent(stroke, t, stepCount);
float2 normal = float2(-tangent.y, tangent.x);
float strokeT = StrokeProgress(stroke, t);
float taper = StrokeTaper(stroke, t);
float localPressure = pressure * taper;
float laneCore = cultmath_smoothstep(0.0, 0.78, 1.0 - edge);
@ -377,7 +390,7 @@ void SimulateBokushoTuftSegmentToPage(
float separation = saturate(split * 0.34 + tension * 0.46 + edge * 0.20 - cohesion * 0.24);
float adhesion = saturate(stateWet * (0.36 + cohesion * 0.40) + localPressure * 0.10 - separation * 0.22);
float dryMemory = saturate((1.0 - stateWet) * 0.62 + separation * 0.32 + velocity * 0.004);
float fiberNoise = LaneHash(laneKey + step * 13u, tuft, 101u);
float fiberNoise = LaneHash(laneKey + StrokeNoiseStep(strokeT) * 13u, tuft, 101u);
float continuity = 1.0 - cultmath_smoothstep(0.18 + dryMemory * 0.28, 0.96, fiberNoise) * dryMemory * (0.38 + edge * 0.22);
float contactTransfer = contact * stateLoad * (0.16 + stateWet * 0.92) * (0.30 + drag * 0.64 + localPressure * 0.22 + sweptPatch * 0.18) * (0.68 + laneCore * 0.50 - separation * 0.14) * continuity;
float airborneRelease = (1.0 - contact) * stateLoad * stateWet * saturate(velocity * 0.010 - adhesion * 0.16) * (0.20 + separation * 0.42 + edge * 0.18);
@ -385,7 +398,7 @@ void SimulateBokushoTuftSegmentToPage(
stateLoad = max(0.0, stateLoad - consumedPigment * segmentSpan * (0.010 + localPressure * 0.006));
stateWet = max(0.0, stateWet - consumedPigment * segmentSpan * (0.006 + dryMemory * 0.003));
if (writeOutput)
if (writeOutput && (writeInitialStep || step > 0u))
{
float pigment = (contactTransfer * (0.95 + localPressure * 0.34 + sweptPatch * 0.24) + airborneRelease * (1.6 + velocity * 0.002)) * stroke.dynamics.z;
float patchNormalRadius = localNormalRadius * (0.84 + laneCore * 0.20 + localPressure * 0.14 + sweptPatch * 0.08 - separation * 0.10);
@ -490,8 +503,8 @@ void D3D12BokushoPageDepositCS(uint3 dispatchThreadId : SV_DispatchThreadID)
[loop]
for (uint replayStrokeIndex = chainStart; replayStrokeIndex < strokeIndex; replayStrokeIndex += 1u)
{
SimulateBokushoTuftSegmentToPage(BokushoBrushStrokes[replayStrokeIndex], chainStart, tuft, sampleCount, restOffset, edge, offset, tip, stateLoad, stateWet, false);
SimulateBokushoTuftSegmentToPage(BokushoBrushStrokes[replayStrokeIndex], chainStart, tuft, sampleCount, restOffset, edge, offset, tip, stateLoad, stateWet, false, true);
}
SimulateBokushoTuftSegmentToPage(stroke, chainStart, tuft, sampleCount, restOffset, edge, offset, tip, stateLoad, stateWet, true);
SimulateBokushoTuftSegmentToPage(stroke, chainStart, tuft, sampleCount, restOffset, edge, offset, tip, stateLoad, stateWet, true, strokeIndex == chainStart);
}

View file

@ -658,6 +658,21 @@ public sealed class BokushoBrushSimulationTests
Assert.True(MathF.Abs(sharedJoin - splitJoin) < 0.0001f, $"shared join={sharedJoin:0.000000}; split join={splitJoin:0.000000}");
}
[Fact]
public void CpuPageDepositDoesNotDoubleStampContinuousSegmentBoundary()
{
var shared = SourceIdentityFrame(sameSourceId: true);
var split = SourceIdentityFrame(sameSourceId: false);
var sharedDensity = BokushoBrushSimulation.EvaluatePageDensity(shared, 160, 160, Vector2.Zero, 4.0f);
var splitDensity = BokushoBrushSimulation.EvaluatePageDensity(split, 160, 160, Vector2.Zero, 4.0f);
var sharedJoin = SampleDensity(sharedDensity, 160, 160, new Vector2(0.0f, 0.0f), 4.0f);
var splitJoin = SampleDensity(splitDensity, 160, 160, new Vector2(0.0f, 0.0f), 4.0f);
Assert.True(sharedJoin > 0.0f);
Assert.True(splitJoin > sharedJoin, $"shared join={sharedJoin:0.000000}; split join={splitJoin:0.000000}");
}
[Fact]
public void CpuBrushSegmentSpanKeepsLaterFittedPiecesLoaded()
{
@ -1019,4 +1034,12 @@ public sealed class BokushoBrushSimulationTests
var y = Math.Clamp((int)MathF.Round(uv.Y * (height - 1)), 0, height - 1);
return page[y * width + x];
}
private static float SampleDensity(uint[] page, int width, int height, Vector2 world, float viewRadius)
{
var uv = world / viewRadius * 0.5f + new Vector2(0.5f, 0.5f);
var x = Math.Clamp((int)MathF.Round(uv.X * (width - 1)), 0, width - 1);
var y = Math.Clamp((int)MathF.Round(uv.Y * (height - 1)), 0, height - 1);
return BokushoBrushSimulation.DecodePageDensity(page[y * width + x]);
}
}