diff --git a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs index c5b29ae..f663470 100644 --- a/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs +++ b/src/Aquarium.Engine.Contracts/Render/BokushoBrushSimulation.cs @@ -57,11 +57,13 @@ public static class BokushoBrushSimulation var laneT = tuftCount <= 1 ? 0.5f : tuft / (float)(tuftCount - 1); var restOffset = laneT * 2.0f - 1.0f; var edge = MathF.Abs(restOffset); + var laneHash = LaneHash(strokeIndex, tuft, 17); + var laneLoad = 0.76f + laneHash * 0.34f; + var split = Saturate((0.20f - LaneHash(strokeIndex, tuft, 53)) * 4.0f) * Saturate((edge - 0.18f) * 1.7f); var tip = StrokePoint(stroke, 0.0f); - var offset = restOffset * normalRadius * (0.42f + splay * 0.38f); - var stateLoad = load * (1.0f - edge * 0.36f); + var offset = restOffset * normalRadius * (0.42f + splay * 0.38f) + (laneHash - 0.5f) * normalRadius * 0.05f; + var stateLoad = load * (1.0f - edge * 0.36f) * laneLoad * (1.0f - split * 0.58f); var stateWet = wetness * (0.86f + (1.0f - edge) * 0.14f); - var shed = 0.0f; for (var sample = 0; sample < sampleCount; sample++) { @@ -69,30 +71,34 @@ public static class BokushoBrushSimulation var center = StrokePoint(stroke, t); var tangent = StrokeTangent(stroke, t, sampleCount); var normal = new Vector2(-tangent.Y, tangent.X); + var taper = StrokeTaper(t); + var localPressure = pressure * taper; + var localNormalRadius = MathF.Max(normalRadius * (0.34f + taper * 0.66f), 0.0001f); + var localTangentRadius = MathF.Max(tangentRadius * (0.48f + taper * 0.52f), 0.0001f); var turn = MathF.Sin(t * MathF.Tau + strokeIndex * 0.37f); - 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); + var targetOffset = restOffset * localNormalRadius * (0.38f + splay * 0.52f + localPressure * 0.08f - wetness * 0.10f) + turn * localNormalRadius * 0.14f * (1.0f - edge); + var recovery = Saturate(0.08f + stateWet * 0.12f + localPressure * 0.08f + (1.0f - edge) * 0.07f); offset = Lerp(offset, targetOffset, recovery); - var lag = tangentRadius * (0.12f + bend * 0.72f + friction * pressure * 0.34f + edge * 0.18f); + var lag = localTangentRadius * (0.12f + bend * 0.72f + friction * localPressure * 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)); + var contact = Saturate(localPressure * stateWet * stateLoad * (0.70f + (1.0f - edge) * 0.26f)); var drag = contact * friction * (0.38f + stateWet * 0.22f + edge * 0.18f); 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(normalRadius, 0.001f) * 0.38f + velocity * 0.018f + drag * 0.46f); + var tension = Saturate(MathF.Abs(targetOffset - offset) / MathF.Max(localNormalRadius, 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 adhesion = Saturate(stateWet * (0.52f + localPressure * 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); - stateLoad = MathF.Max(0.0f, stateLoad - deposition * (0.040f + pressure * 0.025f)); + stateLoad = MathF.Max(0.0f, stateLoad - deposition * (0.040f + localPressure * 0.025f)); stateWet = MathF.Max(0.0f, stateWet - deposition * 0.010f); - shed += deposition; var index = ((strokeIndex * tuftCount) + tuft) * sampleCount + sample; + var localPigment = Saturate(deposition * (7.5f + contact * 2.5f) + contact * stateLoad * stateWet * 0.22f + adhesion * contact * 0.08f); trace[index] = Saturate(contact * (0.30f + stateLoad * 0.42f + adhesion * 0.20f) + deposition * 1.8f); - canvas[index] = Saturate(shed); + canvas[index] = localPigment; } } } @@ -223,19 +229,56 @@ public static class BokushoBrushSimulation var tangent = StrokeTangent(stroke, bestT, sampleCount); var normal = new Vector2(-tangent.Y, tangent.X); var lateral = Vector2.Dot(world - centerPoint, normal); - var radius = MathF.Max(frame.BrushRadius * stroke.RadiusScale * stroke.NormalScale, 0.0001f); + var taper = StrokeTaper(bestT); + var radius = MathF.Max(frame.BrushRadius * stroke.RadiusScale * stroke.NormalScale * (0.34f + taper * 0.66f), 0.0001f); var splay = Saturate(frame.Splay / 2.0f); - var footprint = radius * (0.42f + splay * 0.74f + Saturate(frame.Pressure * stroke.PressureScale * 0.5f) * 0.18f); + var pressure = Saturate(frame.Pressure * stroke.PressureScale * 0.5f) * taper; + var footprint = radius * (0.42f + splay * 0.74f + pressure * 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[((strokeIndex * tuftCount) + tuft) * sampleCount + sample]); - var pressure = Saturate(frame.Pressure * stroke.PressureScale * 0.5f); + 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); } + private static float StrokeTaper(float t) + { + var entry = SmoothStep(0.0f, 0.10f, t); + var exit = 1.0f - SmoothStep(0.86f, 1.0f, t); + return 0.18f + entry * exit * 0.82f; + } + + private static float LaneHash(int strokeIndex, int tuft, uint salt) + { + var value = (uint)(strokeIndex + 1) * 0x9E3779B9u ^ (uint)(tuft + 1) * 0x85EBCA6Bu ^ 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); + var sample1 = Math.Clamp(sample0 + 1, 0, sampleCount - 1); + var tuft0 = Math.Clamp((int)MathF.Floor(tuftPosition), 0, tuftCount - 1); + var tuft1 = Math.Clamp(tuft0 + 1, 0, tuftCount - 1); + var sampleBlend = Saturate(samplePosition - sample0); + var tuftBlend = Saturate(tuftPosition - tuft0); + var rowOffset = strokeIndex * tuftCount; + var p00 = canvas[(rowOffset + tuft0) * sampleCount + sample0]; + var p10 = canvas[(rowOffset + tuft0) * sampleCount + sample1]; + var p01 = canvas[(rowOffset + tuft1) * sampleCount + sample0]; + var p11 = canvas[(rowOffset + tuft1) * sampleCount + sample1]; + var lower = Lerp(p00, p10, sampleBlend); + var upper = Lerp(p01, p11, sampleBlend); + return Saturate(Lerp(lower, upper, tuftBlend)); + } + private static Vector2 CatmullRom(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float t) { var t2 = t * t; diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl index 8c45f1b..fe696a7 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12BokushoBrush.hlsl @@ -38,6 +38,24 @@ float2 StrokeTangent(BokushoBrushStroke stroke, float t) return cultmath_normalize(after - before); } +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; +} + +float LaneHash(uint strokeIndex, uint tuft, uint salt) +{ + uint value = (strokeIndex + 1u) * 0x9E3779B9u ^ (tuft + 1u) * 0x85EBCA6Bu ^ salt; + value ^= value >> 16; + value *= 0x7FEB352Du; + value ^= value >> 15; + value *= 0x846CA68Bu; + value ^= value >> 16; + return (float)(value & 0x00FFFFFFu) / 16777215.0; +} + [numthreads(128, 1, 1)] void D3D12BokushoBrushCS(uint3 dispatchThreadId : SV_DispatchThreadID) { @@ -56,6 +74,9 @@ 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 laneHash = LaneHash(strokeIndex, tuft, 17u); + float laneLoad = 0.76 + laneHash * 0.34; + float split = saturate((0.20 - LaneHash(strokeIndex, tuft, 53u)) * 4.0) * saturate((edge - 0.18) * 1.7); float pressure = saturate(brushMaterial.y * stroke.profile.y * 0.5) * 2.0; float wetness = saturate(brushMaterial.w / 1.6); float load = saturate(brushMaterial.z / 2.0); @@ -66,10 +87,9 @@ void D3D12BokushoBrushCS(uint3 dispatchThreadId : SV_DispatchThreadID) float normalRadius = max(radius * stroke.profile.z, 0.0001); float tangentRadius = max(radius * stroke.profile.w, 0.0001); float2 tip = StrokePoint(stroke, 0.0); - float offset = restOffset * normalRadius * (0.42 + splay * 0.38); - float stateLoad = load * (1.0 - edge * 0.36); + float offset = restOffset * normalRadius * (0.42 + splay * 0.38) + (laneHash - 0.5) * normalRadius * 0.05; + float stateLoad = load * (1.0 - edge * 0.36) * laneLoad * (1.0 - split * 0.58); float stateWet = wetness * (0.86 + (1.0 - edge) * 0.14); - float shed = 0.0; [loop] for (uint sample = 0u; sample < sampleCount; sample += 1u) @@ -78,30 +98,34 @@ void D3D12BokushoBrushCS(uint3 dispatchThreadId : SV_DispatchThreadID) float2 center = StrokePoint(stroke, t); float2 tangent = StrokeTangent(stroke, t); float2 normal = float2(-tangent.y, tangent.x); + float taper = StrokeTaper(t); + float localPressure = pressure * taper; + float localNormalRadius = max(normalRadius * (0.34 + taper * 0.66), 0.0001); + float localTangentRadius = max(tangentRadius * (0.48 + taper * 0.52), 0.0001); float turn = sin(t * 6.28318530718 + (float)strokeIndex * 0.37); - 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); + float targetOffset = restOffset * localNormalRadius * (0.38 + splay * 0.52 + localPressure * 0.08 - wetness * 0.10) + turn * localNormalRadius * 0.14 * (1.0 - edge); + float recovery = saturate(0.08 + stateWet * 0.12 + localPressure * 0.08 + (1.0 - edge) * 0.07); offset = cultmath_lerp(offset, targetOffset, recovery); - float lag = tangentRadius * (0.12 + bend * 0.72 + friction * pressure * 0.34 + edge * 0.18); + float lag = localTangentRadius * (0.12 + bend * 0.72 + friction * localPressure * 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)); + float contact = saturate(localPressure * stateWet * stateLoad * (0.70 + (1.0 - edge) * 0.26)); float drag = contact * friction * (0.38 + stateWet * 0.22 + edge * 0.18); tip = tip + slip * (1.0 - drag); float velocity = length(slip) * brushShape.z / max(radius, 0.001); - float tension = saturate(abs(targetOffset - offset) / max(normalRadius, 0.001) * 0.38 + velocity * 0.018 + drag * 0.46); + float tension = saturate(abs(targetOffset - offset) / max(localNormalRadius, 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 adhesion = saturate(stateWet * (0.52 + localPressure * 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); - stateLoad = max(0.0, stateLoad - deposition * (0.040 + pressure * 0.025)); + stateLoad = max(0.0, stateLoad - deposition * (0.040 + localPressure * 0.025)); stateWet = max(0.0, stateWet - deposition * 0.010); - shed += deposition; uint index = ((strokeIndex * tuftCount) + tuft) * sampleCount + sample; + float localPigment = saturate(deposition * (7.5 + contact * 2.5) + contact * stateLoad * stateWet * 0.22 + adhesion * contact * 0.08); float trace = saturate(contact * (0.30 + stateLoad * 0.42 + adhesion * 0.20) + deposition * 1.8); BokushoTraceField[index] = trace; - BokushoCanvasField[index] = saturate(shed); + BokushoCanvasField[index] = localPigment; } } diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl index 1da7a91..04d4248 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12HeightField.hlsl @@ -103,13 +103,33 @@ float2 bokushoStrokeTangent(BokushoBrushStroke stroke, float t) return cultmath_normalize(after - before); } +float bokushoStrokeTaper(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; +} + float bokushoCanvasSample(uint strokeIndex, float sampleIndex, float tuftIndex) { uint sampleCount = max((uint)round(bokushoShape.x), 2u); uint tuftCount = max((uint)round(bokushoShape.y), 1u); - uint sample = min((uint)round(sampleIndex), sampleCount - 1u); - uint tuft = min((uint)round(tuftIndex), tuftCount - 1u); - return saturate(BokushoCanvasField[((strokeIndex * tuftCount) + tuft) * sampleCount + sample]); + float clampedSample = clamp(sampleIndex, 0.0, (float)(sampleCount - 1u)); + float clampedTuft = clamp(tuftIndex, 0.0, (float)(tuftCount - 1u)); + uint sample0 = min((uint)floor(clampedSample), sampleCount - 1u); + uint sample1 = min(sample0 + 1u, sampleCount - 1u); + uint tuft0 = min((uint)floor(clampedTuft), tuftCount - 1u); + uint tuft1 = min(tuft0 + 1u, tuftCount - 1u); + float sampleBlend = saturate(clampedSample - (float)sample0); + float tuftBlend = saturate(clampedTuft - (float)tuft0); + uint rowOffset = strokeIndex * tuftCount; + float p00 = BokushoCanvasField[(rowOffset + tuft0) * sampleCount + sample0]; + float p10 = BokushoCanvasField[(rowOffset + tuft0) * sampleCount + sample1]; + float p01 = BokushoCanvasField[(rowOffset + tuft1) * sampleCount + sample0]; + float p11 = BokushoCanvasField[(rowOffset + tuft1) * sampleCount + sample1]; + float lower = cultmath_lerp(p00, p10, sampleBlend); + float upper = cultmath_lerp(p01, p11, sampleBlend); + return saturate(cultmath_lerp(lower, upper, tuftBlend)); } float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint strokeIndex) @@ -156,16 +176,17 @@ float bokushoStrokePageHeight(float2 world, BokushoBrushStroke stroke, uint stro float2 tangent = bokushoStrokeTangent(stroke, bestT); float2 normal = float2(-tangent.y, tangent.x); float lateral = dot(world - center, normal); - float radius = max(bokushoMaterial.x * stroke.profile.x * stroke.profile.z, 0.0001); + float taper = bokushoStrokeTaper(bestT); + float radius = max(bokushoMaterial.x * stroke.profile.x * stroke.profile.z * (0.34 + taper * 0.66), 0.0001); float splay = saturate(bokushoDynamics.x / 2.0); - float footprint = radius * (0.42 + splay * 0.74 + saturate(bokushoMaterial.y * stroke.profile.y * 0.5) * 0.18); + float pressure = saturate(bokushoMaterial.y * stroke.profile.y * 0.5) * taper; + float footprint = radius * (0.42 + splay * 0.74 + pressure * 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(strokeIndex, sampleIndex, tuftIndex); - float pressure = saturate(bokushoMaterial.y * stroke.profile.y * 0.5); return pigment * contact * (0.10 + pressure * 0.18 + saturate(bokushoMaterial.z * 0.5) * 0.08); } @@ -216,7 +237,7 @@ float D3D12HeightFieldBasePS(VertexOut input) : SV_Target float2 world = viewWorld(saturate(input.uv)); float slow = sin((world.x * 0.08 + world.y * 0.06) + timeSeconds * 0.27) * sin((world.x * -0.04 + world.y * 0.07) - timeSeconds * 0.19) * 0.035; - return slow + bokushoPageHeight(world) * 3.0; + return slow + bokushoPageHeight(world) * 5.0; } BrushVertexOut D3D12HeightFieldBrushVS(uint vertexId : SV_VertexID, uint instanceId : SV_InstanceID) diff --git a/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl b/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl index a3d02cb..8f7c56f 100644 --- a/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl +++ b/src/Aquarium.Engine/Render/Shaders/D3D12Scene.hlsl @@ -344,10 +344,10 @@ float3 surfaceMirrorRadiance(float3 p, float3 direction, out float3 normal) float flatness = 1.0 - smoothstep(SURFACE_FLAT_SLOPE_START, SURFACE_FLAT_SLOPE_END, slope); float lod = flatness * SURFACE_FLAT_REFLECTION_MAX_LOD; float3 reflected = studioPmremConeSample(reflectionDirection, lod, flatness * 0.055); - float inkMass = saturate((height - 0.028) * 14.0 + smoothstep(0.04, 0.20, slope) * 0.42); - float3 paper = float3(0.78, 0.68, 0.48) * (0.46 + flatness * 0.22); + float inkMass = saturate((height - 0.006) * 32.0 + smoothstep(0.03, 0.16, slope) * 0.42); + float3 paper = float3(0.84, 0.75, 0.55) * (0.62 + flatness * 0.28); float3 ink = float3(0.010, 0.008, 0.006); - return lerp(reflected + paper * 0.28, ink, inkMass * 0.86); + return lerp(reflected * 0.18 + paper, ink, inkMass * 0.90); } float3 backgroundRadiance(float3 direction) diff --git a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs index 55c1c4c..3eb053f 100644 --- a/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs +++ b/tests/Aquarium.Engine.Tests/BokushoBrushSimulationTests.cs @@ -39,7 +39,7 @@ public sealed class BokushoBrushSimulationTests } [Fact] - public void CpuBrushSimulationAccumulatesPigmentPerTuftWithoutRetroactiveRepair() + public void CpuBrushSimulationDepositsLocalPigmentPerTuftWithoutHistorySmear() { var frame = new AquariumBokushoBrushFrame { @@ -58,18 +58,27 @@ public sealed class BokushoBrushSimulationTests for (var tuft = 0; tuft < result.TuftCount; tuft++) { - var previous = 0.0f; + var variation = 0.0f; for (var sample = 0; sample < result.SampleCount; sample++) { var current = result.Canvas[tuft * result.SampleCount + sample]; - Assert.True(current >= previous, $"Canvas pigment for tuft {tuft} sample {sample} moved backward."); - previous = current; + Assert.InRange(current, 0.0f, 1.0f); + if (sample > 0) + { + var previous = result.Canvas[tuft * result.SampleCount + sample - 1]; + variation += MathF.Abs(current - previous); + } } + + Assert.True(variation > 0.0001f, $"Canvas pigment for tuft {tuft} collapsed to an unchanging history field."); } var centerTuft = result.TuftCount / 2; - var centerCanvas = result.Canvas[centerTuft * result.SampleCount + result.SampleCount - 1]; - var edgeCanvas = result.Canvas[result.SampleCount - 1]; + var centerCanvas = result.Canvas + .Skip(centerTuft * result.SampleCount) + .Take(result.SampleCount) + .Max(); + var edgeCanvas = result.Canvas.Take(result.SampleCount).Max(); Assert.True(centerCanvas > edgeCanvas); }