Mirror Grid brush placement for bodies

This commit is contained in:
Meta Vens 2026-05-11 06:45:50 +01:00
commit babae9165b
7 changed files with 116 additions and 39 deletions

View file

@ -32,6 +32,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
private const float GridTransparentMaxZ = 0.45f;
private const int BloomLevelCount = 3;
private const float SunRadius = 1.12f;
private const float BodyGridClearanceRadiusScale = 2.0f;
private const float SelfGravityRadius = 17.0f;
private const float CursorBodyRadius = 0.56f;
private const float CursorBodyBoundRadius = 0.72f;
private const Format SceneHdrFormat = Format.R16G16B16A16_Float;
@ -280,7 +282,7 @@ public sealed class D3D12Renderer : IAquariumRenderer
}
BuildGridHeightBrushes(frame);
BuildBodyLightTable();
BuildBodyLightTable(frame);
var frameConstants = frameResources.UploadRing.WriteConstant(new FrameConstants(
new Vector2(width, height),
frame.TimeSeconds,
@ -994,26 +996,28 @@ public sealed class D3D12Renderer : IAquariumRenderer
SetGridHeightBrush(
0,
new Vector2(0.0f, 0.0f),
8.5f,
SelfGravityRadius,
2.85f,
-1.34f,
0.055f,
1.2f,
0.74f);
0.18f,
MathF.Tau,
0.82f,
1.25f);
for (var index = 0; index < PlanetCount; index++)
{
var radius = PlanetRadius(index);
var center = PlanetCenter(index, frame.TimeSeconds, radius);
var center = PlanetAnchor(index, frame.TimeSeconds);
SetGridHeightBrush(
index + 1,
new Vector2(center.X, center.Y),
center,
3.8f + radius * 2.5f,
2.1f,
-0.42f,
0.022f,
2.4f,
1.35f);
1.35f,
0.0f);
}
}
@ -1025,20 +1029,22 @@ public sealed class D3D12Renderer : IAquariumRenderer
float amplitude,
float waveAmplitude,
float waveFrequency,
float waveSpeed)
float waveSpeed,
float waveSinePower)
{
var centerRadius = new Vector4(center, radius, 0.0f);
var shape = new Vector4(power, amplitude, 0.0f, 0.0f);
var wave = new Vector4(waveAmplitude, waveFrequency, waveSpeed, 0.0f);
var wave = new Vector4(waveAmplitude, waveFrequency, waveSpeed, waveSinePower);
gridHeightBrushConstants.Set(index, centerRadius, shape, wave);
gridHeightBrushes[index] = new GridHeightBrushCpu(center, radius, power, amplitude, waveAmplitude, waveFrequency, waveSpeed);
gridHeightBrushes[index] = new GridHeightBrushCpu(center, radius, power, amplitude, waveAmplitude, waveFrequency, waveSpeed, waveSinePower);
}
private void BuildBodyLightTable()
private void BuildBodyLightTable(AquariumFrame frame)
{
Array.Clear(bodyLights);
var selfCenter = BodyCenterAtGridHeight(frame, Vector2.Zero, SunRadius);
bodyLights[0] = new BodyLightGpu(
new Vector4(0.0f, 0.0f, 2.2f, SunRadius),
new Vector4(selfCenter, SunRadius),
new Vector4(10.0f, 8.7f, 4.2f, 2.0f));
}
@ -1058,7 +1064,11 @@ public sealed class D3D12Renderer : IAquariumRenderer
}
var well = PowerPulse(distanceValue, brush.Radius, brush.Power);
var ripple = MathF.Sin(distanceValue * brush.WaveFrequency - frame.TimeSeconds * brush.WaveSpeed);
var normalized = Math.Clamp(distanceValue / MathF.Max(brush.Radius, 0.001f), 0.0f, 1.0f);
var wavePhase = brush.WaveSinePower > 0.0f
? MathF.Pow(normalized, brush.WaveSinePower) * brush.WaveFrequency - frame.TimeSeconds * brush.WaveSpeed
: distanceValue * brush.WaveFrequency - frame.TimeSeconds * brush.WaveSpeed;
var ripple = brush.WaveSinePower > 0.0f ? MathF.Cos(wavePhase) : MathF.Sin(wavePhase);
height += brush.Amplitude * well + ripple * well * brush.WaveAmplitude;
}
@ -1072,6 +1082,11 @@ public sealed class D3D12Renderer : IAquariumRenderer
return shaped * shaped * (3.0f - 2.0f * shaped);
}
private Vector3 BodyCenterAtGridHeight(AquariumFrame frame, Vector2 world, float radius)
{
return new Vector3(world.X, world.Y, EvaluateGridHeight(frame, world) + radius * BodyGridClearanceRadiusScale);
}
private void SignalFrame(FrameResources frameResources)
{
var signalValue = ++fenceValue;
@ -1606,20 +1621,14 @@ public sealed class D3D12Renderer : IAquariumRenderer
return Lerp(0.34f, 0.62f, Hash21(index, 19.7f));
}
private static Vector3 CursorCenter(AquariumFrame frame)
{
return new Vector3(frame.CursorWorld.X, frame.CursorWorld.Y, CursorBodyRadius);
}
private static Vector3 PlanetCenter(int index, float timeSeconds, float radius)
private static Vector2 PlanetAnchor(int index, float timeSeconds)
{
var f = (float)index;
var angle = f * 0.8975979f + timeSeconds * (0.08f + 0.011f * f);
var orbitRadius = 4.1f + f * 0.77f;
return new Vector3(
return new Vector2(
MathF.Cos(angle) * orbitRadius,
MathF.Sin(angle) * orbitRadius,
1.15f + radius * 0.72f);
MathF.Sin(angle) * orbitRadius);
}
private static float Hash21(float x, float y)
@ -1736,7 +1745,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
float Amplitude,
float WaveAmplitude,
float WaveFrequency,
float WaveSpeed);
float WaveSpeed,
float WaveSinePower);
[StructLayout(LayoutKind.Sequential)]
private readonly record struct BodyLightGpu(

View file

@ -113,7 +113,10 @@ float D3D12GridHeightBrushPS(BrushVertexOut input) : SV_Target
float2 world = gridWorld(saturate(input.uv));
float distanceValue = length(world - input.centerRadius.xy);
float well = powerPulse(distanceValue, input.centerRadius.z, input.shape.x);
float ripple = sin(distanceValue * input.wave.y - timeSeconds * input.wave.z);
float normalizedDistance = saturate(distanceValue / max(input.centerRadius.z, 0.001));
float legacyPhase = distanceValue * input.wave.y - timeSeconds * input.wave.z;
float radialPhase = pow(normalizedDistance, input.wave.w) * input.wave.y - timeSeconds * input.wave.z;
float ripple = input.wave.w > 0.0 ? cos(radialPhase) : sin(legacyPhase);
float signedHeight = input.shape.y * well + ripple * well * input.wave.x;
return signedHeight;
}

View file

@ -57,6 +57,8 @@ static const float FIELD_ID_SELF = 2.0;
static const float FIELD_ID_GRID = 4.0;
static const float FIELD_ID_CURSOR = 5.0;
static const float FIELD_ID_PLANET_BASE = 10.0;
static const float BODY_GRID_CLEARANCE_RADIUS_SCALE = 2.0;
static const float SELF_GRAVITY_RADIUS = 17.0;
static const float MAX_HISTORY_AGE = 32.0;
VertexOut FullscreenTriangleVS(uint vertexId : SV_VertexID)
{
@ -128,13 +130,61 @@ float planetRadius(int index)
return lerp(0.34, 0.62, hash21(float2(index, 19.7)));
}
float3 planetCenterAt(int index, float sampleTime)
float2 planetAnchorAt(int index, float sampleTime)
{
float f = (float)index;
float angle = f * 0.8975979 + sampleTime * (0.08 + 0.011 * f);
float radius = 4.1 + f * 0.77;
float2 xy = float2(cos(angle), sin(angle)) * radius;
return float3(xy, 1.15 + planetRadius(index) * 0.72);
return float2(cos(angle), sin(angle)) * radius;
}
float powerPulse(float distanceValue, float radius, float power)
{
float normalized = saturate(distanceValue / max(radius, 0.001));
float shaped = pow(1.0 - normalized, power);
return shaped * shaped * (3.0 - 2.0 * shaped);
}
float gridBrushHeight(float2 world, float2 center, float radius, float power, float amplitude, float waveAmplitude, float waveFrequency, float waveSpeed, float waveSinePower, float sampleTime)
{
float distanceValue = length(world - center);
if (distanceValue > radius)
{
return 0.0;
}
float well = powerPulse(distanceValue, radius, power);
float normalizedDistance = saturate(distanceValue / max(radius, 0.001));
float legacyPhase = distanceValue * waveFrequency - sampleTime * waveSpeed;
float radialPhase = pow(normalizedDistance, waveSinePower) * waveFrequency - sampleTime * waveSpeed;
float ripple = waveSinePower > 0.0 ? cos(radialPhase) : sin(legacyPhase);
return amplitude * well + ripple * well * waveAmplitude;
}
float gridHeightAt(float2 world, float sampleTime)
{
float height = sin((world.x * 0.08 + world.y * 0.06) + sampleTime * 0.27)
* sin((world.x * -0.04 + world.y * 0.07) - sampleTime * 0.19) * 0.035;
height += gridBrushHeight(world, 0.0, SELF_GRAVITY_RADIUS, 2.85, -1.34, 0.18, 6.28318530718, 0.82, 1.25, sampleTime);
[unroll]
for (int index = 0; index < PLANET_COUNT; index++)
{
float radius = planetRadius(index);
height += gridBrushHeight(world, planetAnchorAt(index, sampleTime), 3.8 + radius * 2.5, 2.1, -0.42, 0.022, 2.4, 1.35, 0.0, sampleTime);
}
return height;
}
float3 bodyCenterAtGridHeight(float2 xy, float radius, float sampleTime)
{
return float3(xy, gridHeightAt(xy, sampleTime) + radius * BODY_GRID_CLEARANCE_RADIUS_SCALE);
}
float3 planetCenterAt(int index, float sampleTime)
{
return bodyCenterAtGridHeight(planetAnchorAt(index, sampleTime), planetRadius(index), sampleTime);
}
void cameraBasis(float3 camera, float2 center, out float3 forward, out float3 right, out float3 up)
@ -167,8 +217,8 @@ float3 temporalPreviousWorldPosition(float3 worldPosition, float fieldId)
if (abs(fieldId - FIELD_ID_CURSOR) < 0.25)
{
float3 currentCenter = float3(cursorWorlds.xy, 0.56);
float3 previousCenter = float3(cursorWorlds.zw, 0.56);
float3 currentCenter = bodyCenterAtGridHeight(cursorWorlds.xy, 0.56, timeSeconds);
float3 previousCenter = bodyCenterAtGridHeight(cursorWorlds.zw, 0.56, previousTimeSeconds);
return previousCenter + (worldPosition - currentCenter);
}

View file

@ -41,6 +41,7 @@ static const float FIELD_ID_PLANET_BASE = 10.0;
static const int CURSOR_PRIMITIVE_ID = PLANET_COUNT + 1;
static const float CURSOR_RADIUS = 0.56;
static const float CURSOR_BOUND_RADIUS = 0.72;
static const float BODY_GRID_CLEARANCE_RADIUS_SCALE = 2.0;
static const float PI = 3.14159265359;
static const float GRID_HEIGHT_TEXEL_COUNT = 128.0;
static const int BODY_LIGHT_COUNT = 8;
@ -122,13 +123,24 @@ float planetRadius(int index)
return lerp(0.34, 0.62, hash21(float2(index, 19.7)));
}
float3 planetCenterAt(int index, float sampleTime)
float terrainHeight(float2 p);
float2 planetAnchorAt(int index, float sampleTime)
{
float f = (float)index;
float angle = f * 0.8975979 + sampleTime * (0.08 + 0.011 * f);
float radius = 4.1 + f * 0.77;
float2 xy = float2(cos(angle), sin(angle)) * radius;
return float3(xy, 1.15 + planetRadius(index) * 0.72);
return float2(cos(angle), sin(angle)) * radius;
}
float3 bodyCenterAtGridHeight(float2 xy, float radius)
{
return float3(xy, terrainHeight(xy) + radius * BODY_GRID_CLEARANCE_RADIUS_SCALE);
}
float3 planetCenterAt(int index, float sampleTime)
{
return bodyCenterAtGridHeight(planetAnchorAt(index, sampleTime), planetRadius(index));
}
float3 shadeBody(float2 uv, float travel, float3 p, float3 normal, int primitiveId);
@ -223,7 +235,7 @@ float cursorLocatorSdf(float3 p)
static const float ContactZ = -1.0;
static const float TipZ = 1.15;
float3 center = float3(cursorWorlds.xy, CURSOR_RADIUS);
float3 center = bodyCenterAtGridHeight(cursorWorlds.xy, CURSOR_RADIUS);
float3 local = (p - center) / CURSOR_RADIUS;
float2 samplePoint = float2(length(local.xy), local.z);
float profileRadius = cursorLocatorProfileRadius(samplePoint.y);
@ -246,7 +258,7 @@ float3 cursorLocatorNormal(float3 p)
bool traceCursorLocator(float3 origin, float3 direction, float intervalStart, float intervalEnd, out float travel, out float3 normal)
{
float sphereTravel;
float3 center = float3(cursorWorlds.xy, CURSOR_RADIUS);
float3 center = bodyCenterAtGridHeight(cursorWorlds.xy, CURSOR_RADIUS);
if (!traceSphere(origin, direction, center, CURSOR_BOUND_RADIUS, sphereTravel))
{
travel = farDistance + 1.0;
@ -288,12 +300,12 @@ float3 primitiveCenterAt(int primitiveId, float sampleTime)
{
if (primitiveId == 0)
{
return float3(0.0, 0.0, 2.2);
return bodyCenterAtGridHeight(0.0, SUN_RADIUS);
}
if (primitiveId == CURSOR_PRIMITIVE_ID)
{
return float3(cursorWorlds.xy, CURSOR_RADIUS);
return bodyCenterAtGridHeight(cursorWorlds.xy, CURSOR_RADIUS);
}
return planetCenterAt(primitiveId - 1, sampleTime);