Render texture spline fields as SDF tubes
This commit is contained in:
parent
ea208eebed
commit
062700cbc4
4 changed files with 153 additions and 10 deletions
|
|
@ -14,6 +14,7 @@ public static class AquariumFieldScriptCompiler
|
|||
ArgumentNullException.ThrowIfNull(textureBindings);
|
||||
|
||||
AquariumFractalReservoirField reservoir = AquariumFractalReservoirField.Empty;
|
||||
var useReservoirLowering = false;
|
||||
var textures = new List<AquariumTextureFieldBinding>();
|
||||
var programs = new List<AquariumTextureSplineFieldProgram>();
|
||||
var nodes = new List<AquariumFieldGraphNode>();
|
||||
|
|
@ -48,6 +49,11 @@ public static class AquariumFieldScriptCompiler
|
|||
break;
|
||||
case "reservoir":
|
||||
reservoir = ParseReservoir(args, reservoirRadius, lineIndex);
|
||||
useReservoirLowering = true;
|
||||
break;
|
||||
case "direct":
|
||||
useReservoirLowering = false;
|
||||
reservoir = AquariumFractalReservoirField.Empty;
|
||||
break;
|
||||
default:
|
||||
throw new FormatException($"Unknown field DSL command `{tokens[0]}` at line {lineIndex + 1}.");
|
||||
|
|
@ -64,7 +70,7 @@ public static class AquariumFieldScriptCompiler
|
|||
throw new FormatException("Field DSL must declare at least one `splinefield`.");
|
||||
}
|
||||
|
||||
if (!reservoir.HasInput)
|
||||
if (useReservoirLowering && !reservoir.HasInput)
|
||||
{
|
||||
var splatCount = programs.Sum(program => Math.Max(1, program.ProbePolicy.MaxProbeCount));
|
||||
reservoir = DefaultReservoir(splatCount, reservoirRadius);
|
||||
|
|
@ -75,6 +81,7 @@ public static class AquariumFieldScriptCompiler
|
|||
Textures = textures,
|
||||
TextureSplineFields = programs,
|
||||
Reservoir = reservoir,
|
||||
UseReservoirLowering = useReservoirLowering,
|
||||
SourceScript = source,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -527,6 +527,8 @@ public sealed class AquariumBufferFieldFrame
|
|||
|
||||
public AquariumFractalReservoirField Reservoir { get; init; } = AquariumFractalReservoirField.Empty;
|
||||
|
||||
public bool UseReservoirLowering { get; init; }
|
||||
|
||||
public string? SourceScript { get; init; }
|
||||
|
||||
public bool HasInput => SplineTubeFields.Count > 0 || TextureSplineFields.Count > 0;
|
||||
|
|
@ -675,6 +677,7 @@ public sealed class AquariumBufferFieldFrameBuilder
|
|||
private readonly List<AquariumTextureFieldBinding> textures = [];
|
||||
private readonly List<AquariumTextureSplineFieldProgram> textureSplineFields = [];
|
||||
private AquariumFractalReservoirField reservoir = AquariumFractalReservoirField.Empty;
|
||||
private bool useReservoirLowering;
|
||||
private string? sourceScript;
|
||||
|
||||
public AquariumBufferFieldFrameBuilder SplineTube(
|
||||
|
|
@ -723,6 +726,14 @@ public sealed class AquariumBufferFieldFrameBuilder
|
|||
public AquariumBufferFieldFrameBuilder Reservoir(AquariumFractalReservoirField field)
|
||||
{
|
||||
reservoir = field;
|
||||
useReservoirLowering = field.HasInput;
|
||||
return this;
|
||||
}
|
||||
|
||||
public AquariumBufferFieldFrameBuilder DirectSdfSurfaces()
|
||||
{
|
||||
reservoir = AquariumFractalReservoirField.Empty;
|
||||
useReservoirLowering = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -738,6 +749,7 @@ public sealed class AquariumBufferFieldFrameBuilder
|
|||
Textures = textures,
|
||||
TextureSplineFields = textureSplineFields,
|
||||
Reservoir = reservoir,
|
||||
UseReservoirLowering = useReservoirLowering,
|
||||
SourceScript = sourceScript,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2110,7 +2110,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
gpuFusionPointCount = 0;
|
||||
gpuFusionPointSource = default;
|
||||
temporalGaussiansGpuGenerated = false;
|
||||
activeFractalReservoirField = scene.BufferFieldFrame.Reservoir.HasInput
|
||||
activeFractalReservoirField = scene.BufferFieldFrame.UseReservoirLowering && scene.BufferFieldFrame.Reservoir.HasInput
|
||||
? scene.BufferFieldFrame.Reservoir
|
||||
: scene.FractalReservoirField.HasInput
|
||||
? scene.FractalReservoirField
|
||||
|
|
@ -2120,9 +2120,9 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
: AquariumBufferFieldFrame.Empty;
|
||||
activeTextureSplinePrograms = PackTextureSplinePrograms(activeBufferFieldFrame);
|
||||
activeTextureFieldSamples = FlattenTextureSamples(activeBufferFieldFrame);
|
||||
activeSplineFrame = scene.SplineFrame.HasInput
|
||||
? scene.SplineFrame
|
||||
: AquariumSplineFrame.Empty;
|
||||
activeSplineFrame = MergeSplineFrames(
|
||||
scene.SplineFrame.HasInput ? scene.SplineFrame : AquariumSplineFrame.Empty,
|
||||
BuildTextureSplineFrame(activeBufferFieldFrame));
|
||||
activeFractalProgramTransforms = activeFractalReservoirField.HasInput && scene.FractalReservoirField.ProgramTransforms.Count > 0
|
||||
? scene.FractalReservoirField.ProgramTransforms as AquariumPackedFractalIfsTransform[] ?? scene.FractalReservoirField.ProgramTransforms.ToArray()
|
||||
: [];
|
||||
|
|
@ -2319,6 +2319,130 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
return programs;
|
||||
}
|
||||
|
||||
private static AquariumSplineFrame BuildTextureSplineFrame(AquariumBufferFieldFrame frame)
|
||||
{
|
||||
if (!frame.HasInput || frame.TextureSplineFields.Count == 0 || frame.Textures.Count == 0)
|
||||
{
|
||||
return AquariumSplineFrame.Empty;
|
||||
}
|
||||
|
||||
var textures = frame.Textures.ToDictionary(texture => texture.Id, StringComparer.Ordinal);
|
||||
var splines = new List<AquariumSpline3D>();
|
||||
foreach (var program in frame.TextureSplineFields)
|
||||
{
|
||||
if (!textures.TryGetValue(program.TextureId, out var texture))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
AppendTextureSplineField(splines, texture, program);
|
||||
}
|
||||
|
||||
return splines.Count == 0
|
||||
? AquariumSplineFrame.Empty
|
||||
: new AquariumSplineFrame { Splines = splines };
|
||||
}
|
||||
|
||||
private static void AppendTextureSplineField(
|
||||
List<AquariumSpline3D> splines,
|
||||
AquariumTextureFieldBinding texture,
|
||||
AquariumTextureSplineFieldProgram program)
|
||||
{
|
||||
var axisSamples = program.FrequencyAxis == AquariumTextureAxis.X ? texture.Width : texture.Height;
|
||||
if (axisSamples <= 1 || texture.Channels <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var columnCount = Math.Clamp(program.ColumnCount, 1, program.FrequencyAxis == AquariumTextureAxis.X ? texture.Height : texture.Width);
|
||||
var style = new AquariumSplineStyle(
|
||||
program.Appearance.Radius,
|
||||
MathF.Max(MathF.Max(program.Appearance.Emission.X, MathF.Max(program.Appearance.Emission.Y, program.Appearance.Emission.Z)) * program.Appearance.Emission.W, 1.0f),
|
||||
program.Appearance.Alpha,
|
||||
program.Appearance.ZeroThreshold,
|
||||
program.Appearance.Feather);
|
||||
for (var column = 0; column < columnCount; column++)
|
||||
{
|
||||
var vertices = new AquariumSplineVertex[axisSamples];
|
||||
var hasContribution = false;
|
||||
for (var frequencyIndex = 0; frequencyIndex < axisSamples; frequencyIndex++)
|
||||
{
|
||||
var textureColumn = program.FirstColumn + column * Math.Max(1, program.ColumnStride);
|
||||
if (program.RollingWindowModulo > 0)
|
||||
{
|
||||
textureColumn = PositiveModulo(textureColumn + texture.RollingOffset, program.RollingWindowModulo);
|
||||
}
|
||||
|
||||
var x = program.FrequencyAxis == AquariumTextureAxis.X ? frequencyIndex : textureColumn;
|
||||
var y = program.FrequencyAxis == AquariumTextureAxis.X ? textureColumn : frequencyIndex;
|
||||
if (texture.RollingMode == AquariumRollingModuloMode.Columns)
|
||||
{
|
||||
x = PositiveModulo(x + texture.RollingOffset, texture.Width);
|
||||
}
|
||||
else if (texture.RollingMode == AquariumRollingModuloMode.Rows)
|
||||
{
|
||||
y = PositiveModulo(y + texture.RollingOffset, texture.Height);
|
||||
}
|
||||
|
||||
x = Math.Clamp(x, 0, texture.Width - 1);
|
||||
y = Math.Clamp(y, 0, texture.Height - 1);
|
||||
var sample = Sample(texture, x, y);
|
||||
hasContribution |= sample > program.ProbePolicy.MinimumVisualContribution;
|
||||
var position = program.Origin +
|
||||
program.AxisStep * frequencyIndex +
|
||||
program.ColumnStep * column +
|
||||
new Vector3(0.0f, sample * program.AmplitudeScale, 0.0f);
|
||||
var color = Vector4.Lerp(
|
||||
new Vector4(0.08f, 0.18f, 0.12f, 0.12f),
|
||||
program.Appearance.Emission,
|
||||
Math.Clamp(sample, 0.0f, 1.0f));
|
||||
color.W = Math.Clamp(0.16f + sample * program.Appearance.Alpha, 0.0f, 1.0f);
|
||||
vertices[frequencyIndex] = new AquariumSplineVertex(position, color);
|
||||
}
|
||||
|
||||
if (hasContribution)
|
||||
{
|
||||
splines.Add(new AquariumSpline3D(
|
||||
$"{program.Id}/column/{column}",
|
||||
vertices,
|
||||
style,
|
||||
Math.Clamp(program.Subdivisions, 1, 16)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static AquariumSplineFrame MergeSplineFrames(AquariumSplineFrame first, AquariumSplineFrame second)
|
||||
{
|
||||
if (!first.HasInput)
|
||||
{
|
||||
return second;
|
||||
}
|
||||
|
||||
if (!second.HasInput)
|
||||
{
|
||||
return first;
|
||||
}
|
||||
|
||||
return new AquariumSplineFrame { Splines = first.Splines.Concat(second.Splines).ToArray() };
|
||||
}
|
||||
|
||||
private static float Sample(AquariumTextureFieldBinding texture, int x, int y)
|
||||
{
|
||||
var index = (y * texture.Width + x) * texture.Channels;
|
||||
return index >= 0 && index < texture.Samples.Count ? texture.Samples[index] : 0.0f;
|
||||
}
|
||||
|
||||
private static int PositiveModulo(int value, int modulus)
|
||||
{
|
||||
if (modulus <= 0)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
var result = value % modulus;
|
||||
return result < 0 ? result + modulus : result;
|
||||
}
|
||||
|
||||
private static float[] FlattenTextureSamples(AquariumBufferFieldFrame frame)
|
||||
{
|
||||
if (!frame.HasInput || frame.Textures.Count == 0)
|
||||
|
|
@ -2982,7 +3106,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
Blend.Zero,
|
||||
BlendOperation.Add,
|
||||
LogicOp.Noop,
|
||||
ColorWriteEnable.None);
|
||||
index < 4 ? ColorWriteEnable.All : ColorWriteEnable.None);
|
||||
}
|
||||
|
||||
var description = new GraphicsPipelineStateDescription
|
||||
|
|
@ -2994,7 +3118,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
|
|||
RasterizerState = RasterizerDescription.CullNone,
|
||||
DepthStencilState = DepthStencilDescription.None,
|
||||
SampleMask = uint.MaxValue,
|
||||
PrimitiveTopologyType = PrimitiveTopologyType.Line,
|
||||
PrimitiveTopologyType = PrimitiveTopologyType.Triangle,
|
||||
InputLayout = new InputLayoutDescription(
|
||||
[
|
||||
new InputElementDescription("POSITION", 0, Format.R32G32B32_Float, 0, 0),
|
||||
|
|
|
|||
|
|
@ -434,9 +434,9 @@ SceneOut D3D12SplinePS(SplineVertexOut input)
|
|||
float alpha = saturate(input.color.a * coverage);
|
||||
SceneOut output;
|
||||
output.colorTravel = float4(input.color.rgb * input.material.y, alpha);
|
||||
output.metadata = float4(0.0, 0.0, 0.0, 0.0);
|
||||
output.control = float4(0.0, 0.0, 0.0, 0.0);
|
||||
output.reservoirGuide = float4(1.0, 0.0, 1.0, 0.0);
|
||||
output.metadata = float4(5000.0, 0.0, 0.0, 1.0);
|
||||
output.control = float4(alpha, coverage, input.material.x, 0.0);
|
||||
output.reservoirGuide = float4(saturate(alpha + coverage * 0.5), 0.0, coverage, 0.0);
|
||||
output.depth = input.position.z;
|
||||
return output;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue