Project Bokusho compute canvas onto page field
This commit is contained in:
parent
a46fc26bf8
commit
7a09df598d
4 changed files with 272 additions and 2 deletions
|
|
@ -89,6 +89,41 @@ public static class BokushoBrushSimulation
|
|||
}
|
||||
}
|
||||
|
||||
public static float[] ProjectCanvasToPage(
|
||||
AquariumBokushoBrushFrame source,
|
||||
ReadOnlySpan<float> canvas,
|
||||
int width,
|
||||
int height,
|
||||
Vector2 viewCenter,
|
||||
float viewRadius)
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(width, 1);
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(height, 1);
|
||||
var frame = source.Normalized();
|
||||
var sampleCount = Math.Clamp(frame.SampleCount, 2, 4096);
|
||||
var tuftCount = Math.Clamp(frame.TuftCount, 1, 4096);
|
||||
var valueCount = checked(sampleCount * tuftCount);
|
||||
if (canvas.Length < valueCount)
|
||||
{
|
||||
throw new ArgumentException("Canvas buffer is smaller than the brush simulation frame.", nameof(canvas));
|
||||
}
|
||||
|
||||
var page = new float[checked(width * height)];
|
||||
var safeRadius = MathF.Max(viewRadius, 0.001f);
|
||||
for (var y = 0; y < height; y++)
|
||||
{
|
||||
var uvY = height <= 1 ? 0.0f : y / (float)(height - 1);
|
||||
for (var x = 0; x < width; x++)
|
||||
{
|
||||
var uvX = width <= 1 ? 0.0f : x / (float)(width - 1);
|
||||
var world = viewCenter + (new Vector2(uvX, uvY) * 2.0f - Vector2.One) * safeRadius;
|
||||
page[y * width + x] = ProjectCanvasSample(frame, canvas, world);
|
||||
}
|
||||
}
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
private static Vector2 StrokePoint(AquariumBokushoBrushFrame frame, float t)
|
||||
{
|
||||
return CatmullRom(
|
||||
|
|
@ -107,6 +142,61 @@ public static class BokushoBrushSimulation
|
|||
return Normalize(after - before);
|
||||
}
|
||||
|
||||
private static float ProjectCanvasSample(AquariumBokushoBrushFrame frame, ReadOnlySpan<float> canvas, Vector2 world)
|
||||
{
|
||||
var bestDistance = float.PositiveInfinity;
|
||||
var bestT = 0.0f;
|
||||
for (var scan = 0; scan < 16; scan++)
|
||||
{
|
||||
var t = scan / 15.0f;
|
||||
var center = StrokePoint(frame, t);
|
||||
var distanceSquared = Vector2.DistanceSquared(world, center);
|
||||
if (distanceSquared < bestDistance)
|
||||
{
|
||||
bestDistance = distanceSquared;
|
||||
bestT = t;
|
||||
}
|
||||
}
|
||||
|
||||
for (var refine = 0; refine < 3; refine++)
|
||||
{
|
||||
var span = 1.0f / (15.0f * MathF.Pow(2.0f, refine));
|
||||
var leftT = Saturate(bestT - span);
|
||||
var rightT = Saturate(bestT + span);
|
||||
var leftDistance = Vector2.DistanceSquared(world, StrokePoint(frame, leftT));
|
||||
var rightDistance = Vector2.DistanceSquared(world, StrokePoint(frame, rightT));
|
||||
if (leftDistance < bestDistance)
|
||||
{
|
||||
bestDistance = leftDistance;
|
||||
bestT = leftT;
|
||||
}
|
||||
|
||||
if (rightDistance < bestDistance)
|
||||
{
|
||||
bestDistance = rightDistance;
|
||||
bestT = rightT;
|
||||
}
|
||||
}
|
||||
|
||||
var sampleCount = Math.Clamp(frame.SampleCount, 2, 4096);
|
||||
var tuftCount = Math.Clamp(frame.TuftCount, 1, 4096);
|
||||
var centerPoint = StrokePoint(frame, bestT);
|
||||
var tangent = StrokeTangent(frame, bestT, sampleCount);
|
||||
var normal = new Vector2(-tangent.Y, tangent.X);
|
||||
var lateral = Vector2.Dot(world - centerPoint, normal);
|
||||
var radius = MathF.Max(frame.BrushRadius, 0.0001f);
|
||||
var splay = Saturate(frame.Splay / 2.0f);
|
||||
var footprint = radius * (0.42f + splay * 0.74f + Saturate(frame.Pressure * 0.5f) * 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[tuft * sampleCount + sample]);
|
||||
var pressure = Saturate(frame.Pressure * 0.5f);
|
||||
return pigment * contact * (0.10f + pressure * 0.18f + Saturate(frame.InkLoad * 0.5f) * 0.08f);
|
||||
}
|
||||
|
||||
private static Vector2 CatmullRom(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float t)
|
||||
{
|
||||
var t2 = t * t;
|
||||
|
|
@ -125,6 +215,12 @@ public static class BokushoBrushSimulation
|
|||
|
||||
private static float Lerp(float left, float right, float t) => left + (right - left) * t;
|
||||
|
||||
private static float SmoothStep(float edge0, float edge1, float x)
|
||||
{
|
||||
var t = Saturate((x - edge0) / (edge1 - edge0));
|
||||
return t * t * (3.0f - 2.0f * t);
|
||||
}
|
||||
|
||||
private static float Saturate(float value) => Math.Clamp(value, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -136,6 +136,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
private const int RootBokushoBrushConstants = 0;
|
||||
private const int RootBokushoBrushTrace = 1;
|
||||
private const int RootBokushoBrushCanvas = 2;
|
||||
private const int RootBokushoPageConstants = 23;
|
||||
private const int RootBokushoPageCanvas = 24;
|
||||
private static readonly DebugUi.DebugUiOption[] RenderDebugOptions =
|
||||
[
|
||||
new(0, "Final"),
|
||||
|
|
@ -4036,7 +4038,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
var tuftCount = Math.Clamp(frame.TuftCount, 1, MaxBokushoBrushTufts);
|
||||
var valueCount = checked(sampleCount * tuftCount);
|
||||
var constants = new D3D12BokushoBrushConstants(
|
||||
new Vector4(sampleCount, tuftCount, frame.PhysicsHz, 0.0f),
|
||||
new Vector4(sampleCount, tuftCount, frame.PhysicsHz, 1.0f),
|
||||
new Vector4(frame.BrushRadius, frame.Pressure, frame.InkLoad, frame.Wetness),
|
||||
new Vector4(frame.Splay, frame.Bend, frame.Friction, 0.0f),
|
||||
frame.StrokeP0,
|
||||
|
|
@ -4086,6 +4088,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
var tubeConstantsUpload = frameResources.UploadRing.WriteConstant(tubeConstants);
|
||||
|
||||
bokushoBrushTraceBuffer.Transition(activeCommandList, ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource);
|
||||
bokushoBrushCanvasBuffer.Transition(activeCommandList, ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource);
|
||||
tubeFieldVertexBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess);
|
||||
tubeFieldIndexBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess);
|
||||
tubeFieldStatsBuffer.Transition(activeCommandList, ResourceStates.UnorderedAccess);
|
||||
|
|
@ -4555,6 +4558,11 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
activeCommandList.SetPipelineState(heightFieldBasePipelineState!);
|
||||
activeCommandList.SetGraphicsRootSignature(fullscreenRootSignature);
|
||||
activeCommandList.SetGraphicsRootDescriptorTable(RootFrameConstants, frameResources.FrameConstantsDescriptor.Gpu);
|
||||
var pageConstants = BuildBokushoPageConstants();
|
||||
var pageConstantsUpload = frameResources.UploadRing.WriteConstant(pageConstants);
|
||||
bokushoBrushCanvasBuffer.Transition(activeCommandList, ResourceStates.PixelShaderResource | ResourceStates.NonPixelShaderResource);
|
||||
activeCommandList.SetGraphicsRootConstantBufferView(RootBokushoPageConstants, pageConstantsUpload.GpuVirtualAddress);
|
||||
activeCommandList.SetGraphicsRootShaderResourceView(RootBokushoPageCanvas, bokushoBrushCanvasBuffer.Resource.GPUVirtualAddress);
|
||||
activeCommandList.RSSetViewports(viewport);
|
||||
activeCommandList.RSSetScissorRects(scissorRect);
|
||||
activeCommandList.OMSetRenderTargets(heightFieldRenderTarget.RenderTargetView.Cpu, null);
|
||||
|
|
@ -4707,6 +4715,33 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
}
|
||||
}
|
||||
|
||||
private D3D12BokushoBrushConstants BuildBokushoPageConstants()
|
||||
{
|
||||
if (!activeBokushoBrushFrame.HasInput)
|
||||
{
|
||||
return new D3D12BokushoBrushConstants(
|
||||
Vector4.Zero,
|
||||
Vector4.Zero,
|
||||
Vector4.Zero,
|
||||
Vector4.Zero,
|
||||
Vector4.Zero,
|
||||
Vector4.Zero,
|
||||
Vector4.Zero);
|
||||
}
|
||||
|
||||
var frame = activeBokushoBrushFrame.Normalized();
|
||||
var sampleCount = Math.Clamp(frame.SampleCount, 2, MaxBokushoBrushSamples);
|
||||
var tuftCount = Math.Clamp(frame.TuftCount, 1, MaxBokushoBrushTufts);
|
||||
return new D3D12BokushoBrushConstants(
|
||||
new Vector4(sampleCount, tuftCount, frame.PhysicsHz, 1.0f),
|
||||
new Vector4(frame.BrushRadius, frame.Pressure, frame.InkLoad, frame.Wetness),
|
||||
new Vector4(frame.Splay, frame.Bend, frame.Friction, 0.0f),
|
||||
frame.StrokeP0,
|
||||
frame.StrokeP1,
|
||||
frame.StrokeP2,
|
||||
frame.StrokeP3);
|
||||
}
|
||||
|
||||
private void AppendFieldEvidenceGpuSensorInputs()
|
||||
{
|
||||
gpuSensorTextureCount = gpuSensorExternalTextureCount;
|
||||
|
|
@ -5953,6 +5988,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
new RootParameter(RootParameterType.ShaderResourceView, new RootDescriptor(59, 0), ShaderVisibility.All),
|
||||
new RootParameter(new RootDescriptorTable([tubeFieldReplaySourceRange]), ShaderVisibility.All),
|
||||
new RootParameter(new RootDescriptorTable([sceneOverdrawRange]), ShaderVisibility.Pixel),
|
||||
new RootParameter(RootParameterType.ConstantBufferView, new RootDescriptor(5, 0), ShaderVisibility.All),
|
||||
new RootParameter(RootParameterType.ShaderResourceView, new RootDescriptor(77, 0), ShaderVisibility.All),
|
||||
};
|
||||
var staticSamplers = new[]
|
||||
{
|
||||
|
|
|
|||
|
|
@ -33,6 +33,21 @@ cbuffer HeightFieldBrushes : register(b1)
|
|||
float4 brushDomain[64];
|
||||
};
|
||||
|
||||
cbuffer BokushoPageConstants : register(b5)
|
||||
{
|
||||
float4 bokushoShape; // sampleCount, tuftCount, physicsHz, hasInput
|
||||
float4 bokushoMaterial; // radius, pressure, inkLoad, wetness
|
||||
float4 bokushoDynamics; // splay, bend, friction, reserved
|
||||
float4 bokushoStrokeP0;
|
||||
float4 bokushoStrokeP1;
|
||||
float4 bokushoStrokeP2;
|
||||
float4 bokushoStrokeP3;
|
||||
};
|
||||
|
||||
StructuredBuffer<float> BokushoCanvasField : register(t77);
|
||||
|
||||
#include "CultMath/CultMath.hlsl"
|
||||
|
||||
struct VertexOut
|
||||
{
|
||||
float4 position : SV_Position;
|
||||
|
|
@ -64,6 +79,90 @@ float2 viewWorld(float2 uv)
|
|||
return viewCenter + (uv * 2.0 - 1.0) * viewRadius;
|
||||
}
|
||||
|
||||
float2 bokushoStrokePoint(float t)
|
||||
{
|
||||
return cultmath_catmullrom(bokushoStrokeP0.xy, bokushoStrokeP1.xy, bokushoStrokeP2.xy, bokushoStrokeP3.xy, t);
|
||||
}
|
||||
|
||||
float2 bokushoStrokeTangent(float t)
|
||||
{
|
||||
float dt = 1.0 / max(bokushoShape.x - 1.0, 1.0);
|
||||
float2 before = bokushoStrokePoint(saturate(t - dt));
|
||||
float2 after = bokushoStrokePoint(saturate(t + dt));
|
||||
return cultmath_normalize(after - before);
|
||||
}
|
||||
|
||||
float bokushoCanvasSample(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[tuft * sampleCount + sample]);
|
||||
}
|
||||
|
||||
float bokushoPageHeight(float2 world)
|
||||
{
|
||||
if (bokushoShape.w <= 0.0)
|
||||
{
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
float bestDistance = 1.0e20;
|
||||
float bestT = 0.0;
|
||||
[unroll]
|
||||
for (uint scan = 0u; scan < 16u; scan += 1u)
|
||||
{
|
||||
float t = (float)scan / 15.0;
|
||||
float2 center = bokushoStrokePoint(t);
|
||||
float distanceSquared = dot(world - center, world - center);
|
||||
if (distanceSquared < bestDistance)
|
||||
{
|
||||
bestDistance = distanceSquared;
|
||||
bestT = t;
|
||||
}
|
||||
}
|
||||
|
||||
[unroll]
|
||||
for (uint refine = 0u; refine < 3u; refine += 1u)
|
||||
{
|
||||
float span = 1.0 / (15.0 * pow(2.0, (float)refine));
|
||||
float leftT = saturate(bestT - span);
|
||||
float rightT = saturate(bestT + span);
|
||||
float2 left = bokushoStrokePoint(leftT);
|
||||
float2 right = bokushoStrokePoint(rightT);
|
||||
float leftDistance = dot(world - left, world - left);
|
||||
float rightDistance = dot(world - right, world - right);
|
||||
if (leftDistance < bestDistance)
|
||||
{
|
||||
bestDistance = leftDistance;
|
||||
bestT = leftT;
|
||||
}
|
||||
|
||||
if (rightDistance < bestDistance)
|
||||
{
|
||||
bestDistance = rightDistance;
|
||||
bestT = rightT;
|
||||
}
|
||||
}
|
||||
|
||||
float2 center = bokushoStrokePoint(bestT);
|
||||
float2 tangent = bokushoStrokeTangent(bestT);
|
||||
float2 normal = float2(-tangent.y, tangent.x);
|
||||
float lateral = dot(world - center, normal);
|
||||
float radius = max(bokushoMaterial.x, 0.0001);
|
||||
float splay = saturate(bokushoDynamics.x / 2.0);
|
||||
float footprint = radius * (0.42 + splay * 0.74 + saturate(bokushoMaterial.y * 0.5) * 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(sampleIndex, tuftIndex);
|
||||
float pressure = saturate(bokushoMaterial.y * 0.5);
|
||||
return pigment * contact * (0.10 + pressure * 0.18 + saturate(bokushoMaterial.z * 0.5) * 0.08);
|
||||
}
|
||||
|
||||
float powerPulse(float distanceValue, float radius, float power)
|
||||
{
|
||||
float normalized = saturate(distanceValue / max(radius, 0.001));
|
||||
|
|
@ -98,7 +197,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;
|
||||
return slow + bokushoPageHeight(world);
|
||||
}
|
||||
|
||||
BrushVertexOut D3D12HeightFieldBrushVS(uint vertexId : SV_VertexID, uint instanceId : SV_InstanceID)
|
||||
|
|
|
|||
|
|
@ -72,4 +72,42 @@ public sealed class BokushoBrushSimulationTests
|
|||
var edgeCanvas = result.Canvas[result.SampleCount - 1];
|
||||
Assert.True(centerCanvas > edgeCanvas);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CpuBrushSimulationProjectsDepositedCanvasToPageField()
|
||||
{
|
||||
var frame = new AquariumBokushoBrushFrame
|
||||
{
|
||||
TuftCount = 11,
|
||||
SampleCount = 64,
|
||||
PhysicsHz = 500.0f,
|
||||
BrushRadius = 2.0f,
|
||||
Pressure = 0.92f,
|
||||
InkLoad = 1.36f,
|
||||
Wetness = 0.94f,
|
||||
Splay = 0.82f,
|
||||
Bend = 0.78f,
|
||||
Friction = 0.68f,
|
||||
StrokeP0 = new Vector4(-7.0f, 0.6f, 0.0f, 0.0f),
|
||||
StrokeP1 = new Vector4(-2.6f, -1.8f, 0.0f, 0.0f),
|
||||
StrokeP2 = new Vector4(2.4f, -1.6f, 0.0f, 0.0f),
|
||||
StrokeP3 = new Vector4(7.0f, 0.7f, 0.0f, 0.0f),
|
||||
};
|
||||
var result = BokushoBrushSimulation.Evaluate(frame);
|
||||
|
||||
var page = BokushoBrushSimulation.ProjectCanvasToPage(
|
||||
frame,
|
||||
result.Canvas,
|
||||
width: 96,
|
||||
height: 96,
|
||||
viewCenter: Vector2.Zero,
|
||||
viewRadius: 8.0f);
|
||||
|
||||
var max = page.Max();
|
||||
var offStrokeCorner = page[0];
|
||||
|
||||
Assert.True(max > 0.01f);
|
||||
Assert.True(max > offStrokeCorner * 8.0f);
|
||||
Assert.Contains(page, value => value > 0.0f);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue