Cut 8: Forward+ Vulkan cutover, atmospheric scattering sky, volumetric fog, SSR, 4-split shadows, & linear HDR
This commit is contained in:
parent
c789fe8af8
commit
35626ccd02
21 changed files with 1232 additions and 227 deletions
|
|
@ -28,13 +28,29 @@ public partial class TerrainRenderer : Node3D
|
|||
new(1f, 1f),
|
||||
new(0f, 1f)
|
||||
};
|
||||
private static readonly Vector2[] UnitQuadPoly =
|
||||
{
|
||||
new(-1f, -1f),
|
||||
new(1f, -1f),
|
||||
new(1f, 1f),
|
||||
new(-1f, 1f)
|
||||
};
|
||||
private static readonly Vector2[] ClearQuadPoly =
|
||||
{
|
||||
new(0f, 0f),
|
||||
new(ViewportSize, 0f),
|
||||
new(ViewportSize, ViewportSize),
|
||||
new(0f, ViewportSize)
|
||||
};
|
||||
|
||||
private sealed class TileNode
|
||||
{
|
||||
public SubViewport HeightViewport = null!;
|
||||
public Node2D HeightCanvas = null!;
|
||||
public readonly List<Polygon2D> HeightQuads = new(64);
|
||||
public SubViewport AlbedoViewport = null!;
|
||||
public Node2D AlbedoCanvas = null!;
|
||||
public readonly List<Polygon2D> AlbedoQuads = new(48);
|
||||
public MeshInstance3D SurfaceMesh = null!;
|
||||
public int BakedVersion = -1;
|
||||
}
|
||||
|
|
@ -48,6 +64,10 @@ public partial class TerrainRenderer : Node3D
|
|||
private Shader _brushSubShader = null!;
|
||||
private Shader _brushAlbedoShader = null!;
|
||||
private Shader _surfaceShader = null!;
|
||||
private ShaderMaterial _heightClearMat = null!;
|
||||
private ShaderMaterial _albedoClearMat = null!;
|
||||
private readonly Dictionary<(Shader Shader, int Kind, float HeightDelta), ShaderMaterial> _heightBrushMatCache = new();
|
||||
private readonly Dictionary<(int Kind, float R, float G, float B, float HeightDelta), ShaderMaterial> _albedoBrushMatCache = new();
|
||||
private const int MaxDirtyRebakesPerSync = 2;
|
||||
private TerrainField? _lastTerrain;
|
||||
private int _rebakeCursor;
|
||||
|
|
@ -81,9 +101,9 @@ public partial class TerrainRenderer : Node3D
|
|||
Code = """
|
||||
shader_type canvas_item;
|
||||
render_mode blend_disabled;
|
||||
uniform vec4 clear_color = vec4(0.0, 0.0, 0.0, 1.0);
|
||||
uniform vec4 clear_vec = vec4(0.0, 0.0, 0.0, 1.0);
|
||||
void fragment() {
|
||||
COLOR = clear_color;
|
||||
COLOR = clear_vec;
|
||||
}
|
||||
"""
|
||||
};
|
||||
|
|
@ -92,6 +112,11 @@ public partial class TerrainRenderer : Node3D
|
|||
_brushAlbedoShader = GD.Load<Shader>("res://terrain_brush_albedo.gdshader");
|
||||
_surfaceShader = GD.Load<Shader>("res://terrain_surface.gdshader");
|
||||
|
||||
_heightClearMat = new ShaderMaterial { Shader = _clearShader };
|
||||
_heightClearMat.SetShaderParameter("clear_vec", new Vector4(HeightClearColor.R, HeightClearColor.G, HeightClearColor.B, HeightClearColor.A));
|
||||
_albedoClearMat = new ShaderMaterial { Shader = _clearShader };
|
||||
_albedoClearMat.SetShaderParameter("clear_vec", new Vector4(AlbedoClearColor.R, AlbedoClearColor.G, AlbedoClearColor.B, AlbedoClearColor.A));
|
||||
|
||||
BuildTileGrid();
|
||||
}
|
||||
|
||||
|
|
@ -184,7 +209,7 @@ public partial class TerrainRenderer : Node3D
|
|||
public void UpdateTileLod(Vector3 camPos)
|
||||
{
|
||||
const float nearRadiusSq = 240f * 240f;
|
||||
const float shadowRadiusSq = 155f * 155f;
|
||||
const float shadowRadiusSq = 220f * 220f;
|
||||
for (int tz = 0; tz < TerrainField.TileGridCount; tz++)
|
||||
{
|
||||
for (int tx = 0; tx < TerrainField.TileGridCount; tx++)
|
||||
|
|
@ -281,10 +306,12 @@ public partial class TerrainRenderer : Node3D
|
|||
|
||||
tile.HeightViewport = CreateOrthoViewport($"Tile_{tx}_{tz}_Height", useHdr: true, out var heightCanvas);
|
||||
tile.HeightCanvas = heightCanvas;
|
||||
heightCanvas.AddChild(CreateClearQuad(_heightClearMat, _white1x1));
|
||||
AddChild(tile.HeightViewport);
|
||||
|
||||
tile.AlbedoViewport = CreateOrthoViewport($"Tile_{tx}_{tz}_Albedo", useHdr: false, out var albedoCanvas);
|
||||
tile.AlbedoCanvas = albedoCanvas;
|
||||
albedoCanvas.AddChild(CreateClearQuad(_albedoClearMat, _white1x1));
|
||||
AddChild(tile.AlbedoViewport);
|
||||
|
||||
var mat = new ShaderMaterial { Shader = _surfaceShader };
|
||||
|
|
@ -350,18 +377,15 @@ public partial class TerrainRenderer : Node3D
|
|||
private void BakeTile(int tx, int tz, TileNode tile, TerrainField terrain)
|
||||
{
|
||||
long t0 = RustToDust.Ymir.ArenaSession.PerfEnabled ? System.Diagnostics.Stopwatch.GetTimestamp() : 0;
|
||||
ClearCanvasChildren(tile.HeightCanvas);
|
||||
ClearCanvasChildren(tile.AlbedoCanvas);
|
||||
|
||||
// Full-tile base clear quads (Polygon2D avoids GUI pixel snapping)
|
||||
tile.HeightCanvas.AddChild(CreateClearQuad(HeightClearColor, _clearShader, _white1x1));
|
||||
tile.AlbedoCanvas.AddChild(CreateClearQuad(AlbedoClearColor, _clearShader, _white1x1));
|
||||
|
||||
float tileMinX = -TerrainField.HalfExtent + tx * TerrainField.TileSize;
|
||||
float tileMinZ = -TerrainField.HalfExtent + tz * TerrainField.TileSize;
|
||||
IReadOnlyList<int> indices = terrain.GetTileBrushIndices(tx, tz);
|
||||
var brushes = terrain.Brushes;
|
||||
|
||||
int hCount = 0;
|
||||
int aCount = 0;
|
||||
|
||||
for (int i = 0; i < indices.Count; i++)
|
||||
{
|
||||
var b = brushes[indices[i]];
|
||||
|
|
@ -380,33 +404,30 @@ public partial class TerrainRenderer : Node3D
|
|||
if (b.Kind == TerrainBrushKind.Crater)
|
||||
{
|
||||
// Crater uses subtractive pit + additive ejecta rim/scorch
|
||||
tile.HeightCanvas.AddChild(CreateBrushQuad(px, py, rxPx, rzPx, b.RotationRad, _brushSubShader, kindInt, b.HeightDelta, _white1x1));
|
||||
tile.HeightCanvas.AddChild(CreateBrushQuad(px, py, rxPx, rzPx, b.RotationRad, _brushAddShader, kindInt, b.HeightDelta, _white1x1));
|
||||
SetQuad(tile.HeightCanvas, tile.HeightQuads, ref hCount, px, py, rxPx, rzPx, b.RotationRad, GetOrCreateHeightBrushMat(_brushSubShader, kindInt, b.HeightDelta), _white1x1);
|
||||
SetQuad(tile.HeightCanvas, tile.HeightQuads, ref hCount, px, py, rxPx, rzPx, b.RotationRad, GetOrCreateHeightBrushMat(_brushAddShader, kindInt, b.HeightDelta), _white1x1);
|
||||
}
|
||||
else if (b.Kind == TerrainBrushKind.Track)
|
||||
{
|
||||
tile.HeightCanvas.AddChild(CreateBrushQuad(px, py, rxPx, rzPx, b.RotationRad, _brushSubShader, kindInt, b.HeightDelta, _white1x1));
|
||||
tile.HeightCanvas.AddChild(CreateBrushQuad(px, py, rxPx, rzPx, b.RotationRad, _brushAddShader, kindInt, b.HeightDelta, _white1x1));
|
||||
SetQuad(tile.HeightCanvas, tile.HeightQuads, ref hCount, px, py, rxPx, rzPx, b.RotationRad, GetOrCreateHeightBrushMat(_brushSubShader, kindInt, b.HeightDelta), _white1x1);
|
||||
SetQuad(tile.HeightCanvas, tile.HeightQuads, ref hCount, px, py, rxPx, rzPx, b.RotationRad, GetOrCreateHeightBrushMat(_brushAddShader, kindInt, b.HeightDelta), _white1x1);
|
||||
}
|
||||
else if (b.Kind == TerrainBrushKind.Road || b.HeightDelta < 0f)
|
||||
{
|
||||
tile.HeightCanvas.AddChild(CreateBrushQuad(px, py, rxPx, rzPx, b.RotationRad, _brushSubShader, kindInt, b.HeightDelta, _white1x1));
|
||||
SetQuad(tile.HeightCanvas, tile.HeightQuads, ref hCount, px, py, rxPx, rzPx, b.RotationRad, GetOrCreateHeightBrushMat(_brushSubShader, kindInt, b.HeightDelta), _white1x1);
|
||||
}
|
||||
else
|
||||
{
|
||||
tile.HeightCanvas.AddChild(CreateBrushQuad(px, py, rxPx, rzPx, b.RotationRad, _brushAddShader, kindInt, b.HeightDelta, _white1x1));
|
||||
SetQuad(tile.HeightCanvas, tile.HeightQuads, ref hCount, px, py, rxPx, rzPx, b.RotationRad, GetOrCreateHeightBrushMat(_brushAddShader, kindInt, b.HeightDelta), _white1x1);
|
||||
}
|
||||
|
||||
// 2. Albedo brush pass
|
||||
var albedoQuad = CreateOrientedQuad(px, py, rxPx, rzPx, b.RotationRad, _white1x1);
|
||||
var albedoMat = new ShaderMaterial { Shader = _brushAlbedoShader };
|
||||
albedoMat.SetShaderParameter("brush_kind", kindInt);
|
||||
albedoMat.SetShaderParameter("brush_albedo", new Vector3(b.AlbedoR, b.AlbedoG, b.AlbedoB));
|
||||
albedoMat.SetShaderParameter("height_delta", b.HeightDelta);
|
||||
albedoQuad.Material = albedoMat;
|
||||
tile.AlbedoCanvas.AddChild(albedoQuad);
|
||||
SetQuad(tile.AlbedoCanvas, tile.AlbedoQuads, ref aCount, px, py, rxPx, rzPx, b.RotationRad, GetOrCreateAlbedoBrushMat(kindInt, b.AlbedoR, b.AlbedoG, b.AlbedoB, b.HeightDelta), _white1x1);
|
||||
}
|
||||
|
||||
HideUnusedQuads(tile.HeightQuads, hCount);
|
||||
HideUnusedQuads(tile.AlbedoQuads, aCount);
|
||||
|
||||
tile.HeightViewport.RenderTargetUpdateMode = SubViewport.UpdateMode.Once;
|
||||
tile.AlbedoViewport.RenderTargetUpdateMode = SubViewport.UpdateMode.Once;
|
||||
if (t0 != 0)
|
||||
|
|
@ -416,72 +437,94 @@ public partial class TerrainRenderer : Node3D
|
|||
}
|
||||
}
|
||||
|
||||
private static Polygon2D CreateClearQuad(Color color, Shader clearShader, ImageTexture whiteTex)
|
||||
private ShaderMaterial GetOrCreateHeightBrushMat(Shader shader, int kindInt, float heightDelta)
|
||||
{
|
||||
var key = (shader, kindInt, heightDelta);
|
||||
if (!_heightBrushMatCache.TryGetValue(key, out var mat))
|
||||
{
|
||||
mat = new ShaderMaterial { Shader = shader };
|
||||
mat.SetShaderParameter("brush_kind", kindInt);
|
||||
mat.SetShaderParameter("height_delta", heightDelta);
|
||||
mat.SetShaderParameter("height_span", HeightSpan);
|
||||
_heightBrushMatCache[key] = mat;
|
||||
}
|
||||
return mat;
|
||||
}
|
||||
|
||||
private ShaderMaterial GetOrCreateAlbedoBrushMat(int kindInt, float r, float g, float b, float heightDelta)
|
||||
{
|
||||
var key = (kindInt, r, g, b, heightDelta);
|
||||
if (!_albedoBrushMatCache.TryGetValue(key, out var mat))
|
||||
{
|
||||
mat = new ShaderMaterial { Shader = _brushAlbedoShader };
|
||||
mat.SetShaderParameter("brush_kind", kindInt);
|
||||
mat.SetShaderParameter("brush_albedo", new Vector3(r, g, b));
|
||||
mat.SetShaderParameter("height_delta", heightDelta);
|
||||
_albedoBrushMatCache[key] = mat;
|
||||
}
|
||||
return mat;
|
||||
}
|
||||
|
||||
private static Polygon2D CreateClearQuad(ShaderMaterial mat, ImageTexture whiteTex)
|
||||
{
|
||||
var mat = new ShaderMaterial { Shader = clearShader };
|
||||
mat.SetShaderParameter("clear_color", color);
|
||||
return new Polygon2D
|
||||
{
|
||||
Polygon = new[]
|
||||
{
|
||||
new Vector2(0f, 0f),
|
||||
new Vector2(ViewportSize, 0f),
|
||||
new Vector2(ViewportSize, ViewportSize),
|
||||
new Vector2(0f, ViewportSize)
|
||||
},
|
||||
Polygon = ClearQuadPoly,
|
||||
UV = BrushUv,
|
||||
Texture = whiteTex,
|
||||
Material = mat
|
||||
};
|
||||
}
|
||||
|
||||
private static Polygon2D CreateOrientedQuad(
|
||||
private static void SetQuad(
|
||||
Node2D canvas,
|
||||
List<Polygon2D> pool,
|
||||
ref int index,
|
||||
float px, float py,
|
||||
float rxPx, float rzPx,
|
||||
float rotationRad,
|
||||
ShaderMaterial mat,
|
||||
ImageTexture whiteTex)
|
||||
{
|
||||
return new Polygon2D
|
||||
Polygon2D quad;
|
||||
if (index < pool.Count)
|
||||
{
|
||||
Position = new Vector2(px, py),
|
||||
Rotation = rotationRad,
|
||||
Polygon = new[]
|
||||
quad = pool[index];
|
||||
quad.Position = new Vector2(px, py);
|
||||
quad.Rotation = rotationRad;
|
||||
quad.Scale = new Vector2(rxPx, rzPx);
|
||||
quad.Material = mat;
|
||||
if (!quad.Visible)
|
||||
{
|
||||
new Vector2(-rxPx, -rzPx),
|
||||
new Vector2(rxPx, -rzPx),
|
||||
new Vector2(rxPx, rzPx),
|
||||
new Vector2(-rxPx, rzPx)
|
||||
},
|
||||
UV = BrushUv,
|
||||
Texture = whiteTex
|
||||
};
|
||||
}
|
||||
|
||||
private static Polygon2D CreateBrushQuad(
|
||||
float px, float py,
|
||||
float rxPx, float rzPx,
|
||||
float rotationRad,
|
||||
Shader shader,
|
||||
int kindInt,
|
||||
float heightDelta,
|
||||
ImageTexture whiteTex)
|
||||
{
|
||||
var quad = CreateOrientedQuad(px, py, rxPx, rzPx, rotationRad, whiteTex);
|
||||
var mat = new ShaderMaterial { Shader = shader };
|
||||
mat.SetShaderParameter("brush_kind", kindInt);
|
||||
mat.SetShaderParameter("height_delta", heightDelta);
|
||||
mat.SetShaderParameter("height_span", HeightSpan);
|
||||
quad.Material = mat;
|
||||
return quad;
|
||||
}
|
||||
|
||||
private static void ClearCanvasChildren(Node2D canvas)
|
||||
{
|
||||
for (int i = canvas.GetChildCount() - 1; i >= 0; i--)
|
||||
quad.Visible = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var child = canvas.GetChild(i);
|
||||
canvas.RemoveChild(child);
|
||||
child.QueueFree();
|
||||
quad = new Polygon2D
|
||||
{
|
||||
Position = new Vector2(px, py),
|
||||
Rotation = rotationRad,
|
||||
Scale = new Vector2(rxPx, rzPx),
|
||||
Polygon = UnitQuadPoly,
|
||||
UV = BrushUv,
|
||||
Texture = whiteTex,
|
||||
Material = mat
|
||||
};
|
||||
pool.Add(quad);
|
||||
canvas.AddChild(quad);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
private static void HideUnusedQuads(List<Polygon2D> pool, int usedCount)
|
||||
{
|
||||
for (int i = usedCount; i < pool.Count; i++)
|
||||
{
|
||||
if (pool[i].Visible)
|
||||
{
|
||||
pool[i].Visible = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue