Pin Bokusho page density composition

This commit is contained in:
Meta Vens 2026-06-15 23:23:31 +01:00
commit a94e8e05d0
4 changed files with 50 additions and 4 deletions

View file

@ -4,6 +4,9 @@ namespace Aquarium.Engine.Render;
public static class BokushoBrushSimulation
{
public const float PageDensityScale = 65535.0f;
public const float PagePatchTransferGain = 0.0032f;
public static BokushoBrushSimulationResult Evaluate(AquariumBokushoBrushFrame source)
{
var frame = source.HasInput ? source.Normalized() : AquariumBokushoBrushFrame.Empty;
@ -645,7 +648,7 @@ public static class BokushoBrushSimulation
}
var sampleIndex = y * width + x;
var contribution = ink * coverage * 0.0032f;
var contribution = PagePatchContribution(ink, coverage);
page[sampleIndex] = 1.0f - (1.0f - page[sampleIndex]) * (1.0f - contribution);
}
}
@ -733,6 +736,19 @@ public static class BokushoBrushSimulation
private static float CanvasPigment(float value) => 0.96f * (1.0f - MathF.Exp(-MathF.Max(value, 0.0f) * 0.82f));
public static float PagePatchContribution(float ink, float coverage) => Saturate(ink) * Saturate(coverage) * PagePatchTransferGain;
public static uint EncodePageDensityContribution(float contribution)
{
var density = -MathF.Log(MathF.Max(1.0f - Saturate(contribution), 0.000001f));
return (uint)MathF.Round(MathF.Min(density * PageDensityScale, PageDensityScale));
}
public static float DecodePageDensity(uint encodedDensity)
{
return 1.0f - MathF.Exp(-encodedDensity / PageDensityScale);
}
private static float SmoothStep(float edge0, float edge1, float x)
{
var t = Saturate((x - edge0) / (edge1 - edge0));

View file

@ -1,5 +1,8 @@
#include "CultMath/CultMath.hlsl"
static const float BOKUSHO_PAGE_DENSITY_SCALE = 65535.0;
static const float BOKUSHO_PAGE_PATCH_TRANSFER_GAIN = 0.0032;
cbuffer BokushoBrushConstants : register(b4)
{
float4 brushShape; // sampleCount, tuftCount, physicsHz, strokeCount
@ -132,9 +135,9 @@ void DepositSweptPatch(float2 previousTip, float2 tip, float normalRadius, float
continue;
}
float contribution = ink * coverage * 0.0032;
float contribution = ink * coverage * BOKUSHO_PAGE_PATCH_TRANSFER_GAIN;
float density = -log(max(1.0 - saturate(contribution), 0.000001));
uint fixedDensity = (uint)round(min(density * 65535.0, 65535.0));
uint fixedDensity = (uint)round(min(density * BOKUSHO_PAGE_DENSITY_SCALE, BOKUSHO_PAGE_DENSITY_SCALE));
InterlockedAdd(BokushoPageDensityField[(uint)(y * (int)pageSize + x)], fixedDensity);
}
}

View file

@ -63,6 +63,8 @@ StructuredBuffer<uint> BokushoPageDensityField : register(t80);
#include "CultMath/CultMath.hlsl"
static const float BOKUSHO_PAGE_DENSITY_SCALE = 65535.0;
struct VertexOut
{
float4 position : SV_Position;
@ -268,7 +270,7 @@ float bokushoPageHeight(float2 world)
if (all(pixel >= int2(0, 0)) && all(pixel < int2((int)pageSize, (int)pageSize)))
{
uint densityValue = BokushoPageDensityField[(uint)(pixel.y * (int)pageSize + pixel.x)];
float density = (float)densityValue / 65535.0;
float density = (float)densityValue / BOKUSHO_PAGE_DENSITY_SCALE;
return saturate(1.0 - exp(-density));
}
}

View file

@ -184,6 +184,31 @@ public sealed class BokushoBrushSimulationTests
Assert.True(longestRun > width / 4, $"longest continuous contact run={longestRun}");
}
[Fact]
public void PageDensityEncodingMatchesMultiplicativeInkComposition()
{
var cpuPage = 0.0f;
uint gpuDensity = 0;
var contacts = new (float Ink, float Coverage)[]
{
(0.84f, 0.95f),
(0.62f, 0.42f),
(0.30f, 0.80f),
(1.00f, 0.22f),
(0.18f, 0.66f),
};
foreach (var (ink, coverage) in contacts)
{
var contribution = BokushoBrushSimulation.PagePatchContribution(ink, coverage);
cpuPage = 1.0f - (1.0f - cpuPage) * (1.0f - contribution);
gpuDensity += BokushoBrushSimulation.EncodePageDensityContribution(contribution);
}
var gpuPage = BokushoBrushSimulation.DecodePageDensity(gpuDensity);
Assert.InRange(MathF.Abs(gpuPage - cpuPage), 0.0f, 0.00008f);
}
[Fact]
public void CpuBrushProfileControlsPageFootprint()
{