Add Zyphos planetary demo client

This commit is contained in:
Meta Vens 2026-05-17 23:37:20 +01:00
commit e4c90498a8
13 changed files with 497 additions and 14 deletions

View file

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<AssemblyName>Aquarium.Zyphos</AssemblyName>
<RootNamespace>Aquarium.Zyphos</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Aquarium.Engine.Contracts\Aquarium.Engine.Contracts.csproj" />
</ItemGroup>
<ItemGroup>
<Content Include="Shaders\**\*.hlsl" CopyToOutputDirectory="PreserveNewest" TargetPath="Render\Shaders\%(RecursiveDir)%(Filename)%(Extension)" />
<Content Include="Shaders\**\*.hlsli" CopyToOutputDirectory="PreserveNewest" TargetPath="Render\Shaders\%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,48 @@
static const int SDF_INDEX = 1;
#include "D3D12SdfCommon.hlsli"
#include "D3D12SdfMath.hlsli"
float zyMoonRelief(float3 dir)
{
float ridges =
sin(dir.x * 15.0 + dir.y * 4.0) * 0.018 +
sin(dir.y * 21.0 - dir.z * 7.0) * 0.012 +
sin((dir.x + dir.y + dir.z) * 34.0) * 0.006;
float craterA = 1.0 - smoothstep(0.055, 0.115, abs(length(dir.xy - float2(0.22, -0.18)) - 0.32));
float craterB = 1.0 - smoothstep(0.040, 0.090, abs(length(dir.yz - float2(0.30, 0.24)) - 0.22));
return ridges - (craterA * 0.035 + craterB * 0.026);
}
float sdfDistance(float3 p, int sdfIndex)
{
SdfObject sdfObject = sdfObjects[sdfIndex];
float radius = max(sdfObject.state.x, 0.001);
float3 local = p - sdfObject.centerRadius.xyz;
float3 dir = normalize(local);
return length(local) - (radius + zyMoonRelief(dir));
}
SdfSurface sdfSurface(float3 p, int sdfIndex)
{
SdfObject sdfObject = sdfObjects[sdfIndex];
float3 dir = normalize(p - sdfObject.centerRadius.xyz);
float relief = zyMoonRelief(dir);
float dust = sin(dir.x * 31.0 + dir.y * 17.0 - dir.z * 11.0) * 0.5 + 0.5;
SdfSurface surface;
surface.baseColor = lerp(float3(0.31, 0.30, 0.28), float3(0.66, 0.62, 0.54), dust);
surface.baseColor = lerp(surface.baseColor, float3(0.20, 0.20, 0.19), saturate(-relief * 18.0));
surface.metallic = 0.0;
surface.roughness = 0.92;
surface.emission = 0.0;
return surface;
}
float3 shadeSdf(float2 uv, float travel, float3 p, float3 normal, int sdfIndex, SdfSurface surface)
{
float3 sunDirection = normalize(float3(-0.46, -0.72, 0.52));
float daylight = saturate(dot(normal, sunDirection) * 0.82 + 0.24);
return shadeSdfPbr(p, normal, surface) * daylight;
}
#include "D3D12SdfProxy.hlsli"

View file

@ -0,0 +1,96 @@
static const int SDF_INDEX = 0;
#include "D3D12SdfCommon.hlsli"
#include "D3D12SdfMath.hlsli"
float3 zyRotateZ(float3 p, float angle)
{
float s = sin(angle);
float c = cos(angle);
return float3(c * p.x - s * p.y, s * p.x + c * p.y, p.z);
}
float3 zyPlanetDir(float3 local, SdfObject sdfObject)
{
return normalize(zyRotateZ(local, -sdfObject.state.y));
}
float zySphericalField(float3 dir)
{
float latitude = asin(saturate(abs(dir.z)) * 2.0 - 1.0);
float longitude = atan2(dir.y, dir.x);
float plates =
sin(longitude * 2.0 + dir.z * 4.5) * 0.28 +
sin(longitude * 3.0 - dir.z * 7.0 + 1.7) * 0.22 +
sin((dir.x + dir.y * 0.7 + dir.z * 0.4) * 8.0) * 0.16 +
sin((dir.x * 1.3 - dir.y + dir.z * 1.9) * 13.0) * 0.08;
float equatorialWarmth = cos(latitude) * 0.18;
return 0.50 + plates + equatorialWarmth;
}
float zyTerrainOffset(float3 dir, SdfObject sdfObject)
{
float field = zySphericalField(dir);
float seaLevel = sdfObject.state.z;
float land = smoothstep(seaLevel - 0.04, seaLevel + 0.08, field);
float mountain = pow(saturate(field - seaLevel), 1.65);
float polarCap = pow(abs(dir.z), 8.0) * 0.035;
return (field - seaLevel) * 0.10 + mountain * 0.13 + polarCap * land;
}
float sdfDistance(float3 p, int sdfIndex)
{
SdfObject sdfObject = sdfObjects[sdfIndex];
float planetRadius = max(sdfObject.state.x, 0.001);
float3 local = p - sdfObject.centerRadius.xyz;
float3 dir = zyPlanetDir(local, sdfObject);
return length(local) - (planetRadius + zyTerrainOffset(dir, sdfObject));
}
SdfSurface sdfSurface(float3 p, int sdfIndex)
{
SdfObject sdfObject = sdfObjects[sdfIndex];
float3 local = p - sdfObject.centerRadius.xyz;
float3 dir = zyPlanetDir(local, sdfObject);
float field = zySphericalField(dir);
float seaLevel = sdfObject.state.z;
float land = smoothstep(seaLevel - 0.03, seaLevel + 0.06, field);
float mountain = smoothstep(seaLevel + 0.12, seaLevel + 0.28, field);
float polar = smoothstep(0.72, 0.92, abs(dir.z));
float cloud = smoothstep(0.73, 0.91, sin(dir.x * 18.0 + dir.y * 13.0 + dir.z * 9.0 + timeSeconds * 0.19) * 0.5 + 0.5);
float3 ocean = lerp(float3(0.004, 0.070, 0.130), float3(0.010, 0.220, 0.330), saturate(field));
float3 lowland = float3(0.070, 0.300, 0.185);
float3 highland = float3(0.540, 0.405, 0.220);
float3 snow = float3(0.800, 0.865, 0.830);
float3 landColor = lerp(lowland, highland, mountain);
landColor = lerp(landColor, snow, saturate(polar + mountain * 0.38));
SdfSurface surface;
surface.baseColor = lerp(ocean, landColor, land);
surface.baseColor = lerp(surface.baseColor, float3(0.78, 0.84, 0.80), cloud * 0.16);
surface.metallic = 0.0;
surface.roughness = lerp(0.34, 0.84, land);
surface.emission = 0.0;
return surface;
}
float3 shadeSdf(float2 uv, float travel, float3 p, float3 normal, int sdfIndex, SdfSurface surface)
{
SdfObject sdfObject = sdfObjects[sdfIndex];
float3 dir = zyPlanetDir(p - sdfObject.centerRadius.xyz, sdfObject);
float3 viewDirection = normalize(cameraPosition - p);
float3 sunDirection = normalize(float3(-0.46, -0.72, 0.52));
float daylight = saturate(dot(normal, sunDirection) * 0.74 + 0.34);
float night = saturate(-dot(normal, sunDirection) * 1.7 - 0.20);
float citySeed = sin(dir.x * 43.0 + sin(dir.y * 19.0) * 4.0 + dir.z * 31.0);
float cityWeb = smoothstep(0.82, 0.96, citySeed * 0.5 + 0.5);
float coast = 1.0 - smoothstep(0.020, 0.070, abs(zySphericalField(dir) - sdfObject.state.z));
float fresnel = pow(1.0 - saturate(dot(normal, viewDirection)), 3.2);
float3 city = float3(1.0, 0.54, 0.14) * cityWeb * coast * night * 1.25;
float3 atmosphere = float3(0.07, 0.38, 0.78) * (fresnel * 1.35 + pow(fresnel, 6.0) * 2.6);
return shadeSdfPbr(p, normal, surface) * daylight + city + atmosphere;
}
#include "D3D12SdfProxy.hlsli"

View file

@ -0,0 +1,34 @@
using Aquarium.Engine.Render;
namespace Aquarium.Zyphos;
public static class ZyphosRenderPlan
{
public const int PlanetIndex = 0;
public const int MoonIndex = 1;
public const int SdfObjectCount = 2;
public static AquariumRenderPlan Create()
{
var app = new AquariumApp();
app.Shaders
.Core("D3D12HeightField.hlsl", "D3D12Scene.hlsl", "D3D12Post.hlsl")
.SdfShader("D3D12ZyphosPlanet.hlsl")
.SdfShader("D3D12ZyphosMoon.hlsl");
var heightField = app.RenderTargets.Create("height-field")
.FixedSize(128, 128)
.Format(RenderFormat.R16Float)
.Register();
var scene = app.RenderTargets.Hdr("scene");
app.Cameras.Orbit("main");
app.Graph.Pass("height-field").Fullscreen();
app.Graph.Pass("scene").Fullscreen();
app.Graph.Pass("sdf-proxies").Proxy();
app.Features.Bloom(scene.Color);
app.Features.Presentation(scene.Color);
app.Features.DirectWriteOverlay();
app.Debug.View("Height Field", heightField).View("Scene", scene.Color);
return app.Plan;
}
}

View file

@ -0,0 +1,133 @@
using System.Numerics;
using Aquarium.Engine;
using Aquarium.Engine.Audio;
using Aquarium.Engine.Input;
using Aquarium.Engine.Render;
using Aquarium.Engine.Ui;
namespace Aquarium.Zyphos;
public sealed class ZyphosRuntime : IAquariumRuntime
{
private float timeSeconds;
private float previousTimeSeconds;
private float orbitYaw = -0.28f;
private float orbitPitch = 0.34f;
private float orbitDistance = 15.6f;
private float timeScale = 1.0f;
private bool autoOrbit = true;
public AquariumRuntimeOptions Options { get; private set; }
public GraphicsSettings GraphicsSettings { get; set; } = new(0, 1.08f, 0.32f, 0.05f);
public AquariumRenderPlan RenderPlan { get; } = ZyphosRenderPlan.Create();
public AquariumUiDocument Ui { get; }
public AquariumSynthDocument Synth { get; } = AquariumSynthDocument.Empty;
public AquariumFrame Frame => new(
new ViewFrame(Vector2.Zero, 32.0f),
CameraPosition(),
timeSeconds,
Vector2.Zero,
ZyphosSceneBuilder.Build(timeSeconds, previousTimeSeconds));
public ZyphosRuntime()
{
Ui = new AquariumUiDocument()
.Panel("Zyphos", 18.0f, 82.0f, 340.0f, panel =>
{
panel.Section("Planetary Demo");
panel.Toggle("Auto Orbit", () => autoOrbit, value => autoOrbit = value);
panel.Slider("Time Scale", () => timeScale, value => timeScale = value, 0.0f, 4.0f, "0.00");
panel.Slider("Orbit Distance", () => orbitDistance, value => orbitDistance = value, 9.0f, 24.0f, "0.0");
panel.Readout("Runtime", () => $"{timeSeconds:0.0}s");
panel.Readout("Camera", () => $"{orbitDistance:0.0} wu / yaw {orbitYaw:0.00}");
panel.Readout("Objects", () => "spherical terrain, atmosphere, moon");
})
.Command("zyphos", _ => "Zyphos: planetary-scale client demo loaded.", "Report Zyphos demo status.");
}
public AquariumFrame ComposeFrame(AquariumFrame frame, AquariumFrameInput input)
{
return frame;
}
public void Start()
{
Console.WriteLine("Zyphos planetary demo booted.");
}
public void Update(float deltaSeconds, InputState input)
{
previousTimeSeconds = timeSeconds;
var safeDelta = Math.Max(deltaSeconds, 0.0f);
timeSeconds += safeDelta * MathF.Max(timeScale, 0.0f);
if (autoOrbit)
{
orbitYaw += safeDelta * 0.055f;
}
if (input.IsKeyDown(KeyCode.A))
{
orbitYaw -= safeDelta * 0.85f;
}
if (input.IsKeyDown(KeyCode.D))
{
orbitYaw += safeDelta * 0.85f;
}
if (input.IsKeyDown(KeyCode.W))
{
orbitPitch += safeDelta * 0.45f;
}
if (input.IsKeyDown(KeyCode.S))
{
orbitPitch -= safeDelta * 0.45f;
}
if (MathF.Abs(input.WheelDelta) > 0.0f)
{
orbitDistance = Math.Clamp(orbitDistance - input.WheelDelta * 0.8f, 8.0f, 26.0f);
}
orbitPitch = Math.Clamp(orbitPitch, 0.12f, 0.86f);
}
public void FlushState()
{
}
public void Dispose()
{
}
internal void SetOptions(AquariumRuntimeOptions options)
{
Options = options;
}
private Vector3 CameraPosition()
{
var horizontal = MathF.Cos(orbitPitch) * orbitDistance;
return new Vector3(
MathF.Sin(orbitYaw) * horizontal,
-MathF.Cos(orbitYaw) * horizontal,
MathF.Sin(orbitPitch) * orbitDistance + 3.1f);
}
}
public sealed class ZyphosRuntimeFactory : IAquariumRuntimeFactory
{
public IAquariumRuntime Create(AquariumRuntimeOptions options)
{
var runtime = new ZyphosRuntime();
runtime.SetOptions(options);
return runtime;
}
}

View file

@ -0,0 +1,87 @@
using System.Numerics;
using Aquarium.Engine.Render;
namespace Aquarium.Zyphos;
public static class ZyphosSceneBuilder
{
private const float PlanetSurfaceRadius = 3.8f;
private const float PlanetBoundRadius = 4.52f;
private const float MoonSurfaceRadius = 0.54f;
private const float MoonBoundRadius = 0.70f;
public static AquariumSceneState Build(float timeSeconds, float previousTimeSeconds)
{
return new AquariumSceneState
{
HeightFieldBrushes = BuildHeightFieldBrushes(),
SdfObjects = BuildSdfObjects(timeSeconds, previousTimeSeconds),
SdfLights = BuildSdfLights(timeSeconds),
};
}
private static AquariumHeightFieldBrush[] BuildHeightFieldBrushes()
{
return
[
new AquariumHeightFieldBrush(
Vector2.Zero,
30.0f,
2.6f,
-0.18f,
0.03f,
1.9f,
0.22f,
1.1f)
];
}
private static AquariumSdfObject[] BuildSdfObjects(float timeSeconds, float previousTimeSeconds)
{
var planetCenter = new Vector3(0.0f, 0.0f, PlanetSurfaceRadius + 0.48f);
var rotation = timeSeconds * 0.032f;
var previousRotation = previousTimeSeconds * 0.032f;
var moonCenter = MoonCenter(timeSeconds);
var previousMoonCenter = MoonCenter(previousTimeSeconds);
var objects = new AquariumSdfObject[ZyphosRenderPlan.SdfObjectCount];
objects[ZyphosRenderPlan.PlanetIndex] = new AquariumSdfObject(
new Vector4(planetCenter, PlanetBoundRadius),
new Vector4(planetCenter, 0.0f),
new Vector4(PlanetSurfaceRadius, rotation, 0.56f, previousRotation));
objects[ZyphosRenderPlan.MoonIndex] = new AquariumSdfObject(
new Vector4(moonCenter, MoonBoundRadius),
new Vector4(previousMoonCenter, 0.0f),
new Vector4(MoonSurfaceRadius, timeSeconds * 0.11f, PlanetSurfaceRadius, 0.0f));
return objects;
}
private static AquariumSdfLight[] BuildSdfLights(float timeSeconds)
{
var sunPhase = timeSeconds * 0.015f;
var sunCenter = new Vector3(
-12.0f + MathF.Sin(sunPhase) * 2.4f,
-18.0f,
11.0f + MathF.Cos(sunPhase * 0.7f) * 1.3f);
return
[
new AquariumSdfLight(
new Vector4(sunCenter, 5.5f),
new Vector4(8.6f, 7.9f, 6.7f, -100.0f)),
new AquariumSdfLight(
new Vector4(0.0f, 0.0f, PlanetSurfaceRadius + 0.48f, 4.0f),
new Vector4(0.10f, 0.18f, 0.28f, -101.0f))
];
}
private static Vector3 MoonCenter(float timeSeconds)
{
var orbit = timeSeconds * 0.075f + 1.3f;
var radius = 6.35f;
return new Vector3(
MathF.Cos(orbit) * radius,
MathF.Sin(orbit) * radius * 0.62f,
PlanetSurfaceRadius + 0.62f + MathF.Sin(orbit * 0.73f) * 1.2f);
}
}