Client Terrain: fix HeightViewport alpha blending, 1-texel border gutters, road rut axis, and GPU readback smoke

This commit is contained in:
Meta Vens 2026-12-04 14:16:05 +01:00
commit bb68620d1c
6 changed files with 112 additions and 39 deletions

View file

@ -13,11 +13,13 @@ namespace RustToDust.Client;
public partial class TerrainRenderer : Node3D
{
private const int ViewportSize = 256;
private const float PixelsPerMeter = ViewportSize / TerrainField.TileSize;
private const float InnerTexelSpan = 253.0f; // texel 1 center (1.5) .. texel 254 center (254.5)
private const float GutterCenterOffset = 1.5f;
private const float PixelsPerMeter = InnerTexelSpan / TerrainField.TileSize;
private const float HeightSpan = TerrainField.MaxWorldHeight - TerrainField.MinWorldHeight;
private const float BaseHeightNorm = (0f - TerrainField.MinWorldHeight) / HeightSpan;
private static readonly Color HeightClearColor = new(BaseHeightNorm, 0.88f, 0.02f, 0.0f);
private static readonly Color HeightClearColor = new(BaseHeightNorm, 0.88f, 0.0f, 1.0f);
private static readonly Color AlbedoClearColor = new(0.69f, 0.50f, 0.31f, 1.0f);
private sealed class TileNode
@ -96,6 +98,51 @@ public partial class TerrainRenderer : Node3D
terrain.ClearDirtyMask();
}
public bool TrySampleBakedHeight(float wx, float wz, out float bakedHeight)
{
bakedHeight = 0f;
float cx = Math.Clamp(wx, -TerrainField.HalfExtent, TerrainField.HalfExtent);
float cz = Math.Clamp(wz, -TerrainField.HalfExtent, TerrainField.HalfExtent);
TerrainField.WorldToTile(cx, cz, out int tx, out int tz);
var tile = _tiles[tx, tz];
var tex = tile?.HeightViewport?.GetTexture();
var img = tex?.GetImage();
if (img is null || img.IsEmpty() || img.GetWidth() < ViewportSize || img.GetHeight() < ViewportSize)
{
return false;
}
float tileMinX = -TerrainField.HalfExtent + tx * TerrainField.TileSize;
float tileMinZ = -TerrainField.HalfExtent + tz * TerrainField.TileSize;
float px = GutterCenterOffset + (cx - tileMinX) * PixelsPerMeter;
float py = GutterCenterOffset + (cz - tileMinZ) * PixelsPerMeter;
float fx = Math.Clamp(px - 0.5f, 0f, ViewportSize - 1.0001f);
float fy = Math.Clamp(py - 0.5f, 0f, ViewportSize - 1.0001f);
int x0 = (int)MathF.Floor(fx);
int y0 = (int)MathF.Floor(fy);
int x1 = Math.Min(x0 + 1, ViewportSize - 1);
int y1 = Math.Min(y0 + 1, ViewportSize - 1);
float txFrac = fx - x0;
float tyFrac = fy - y0;
var c00 = img.GetPixel(x0, y0);
if (c00.A < 0.5f)
{
return false;
}
var c10 = img.GetPixel(x1, y0);
var c01 = img.GetPixel(x0, y1);
var c11 = img.GetPixel(x1, y1);
float r = (c00.R * (1f - txFrac) + c10.R * txFrac) * (1f - tyFrac) +
(c01.R * (1f - txFrac) + c11.R * txFrac) * tyFrac;
bakedHeight = TerrainField.MinWorldHeight + Math.Clamp(r, 0f, 1f) * HeightSpan;
return true;
}
private void BuildTileGrid()
{
for (int tz = 0; tz < TerrainField.TileGridCount; tz++)
@ -204,8 +251,8 @@ public partial class TerrainRenderer : Node3D
continue;
}
float px = (b.X - tileMinX) * PixelsPerMeter;
float py = (b.Z - tileMinZ) * PixelsPerMeter;
float px = GutterCenterOffset + (b.X - tileMinX) * PixelsPerMeter;
float py = GutterCenterOffset + (b.Z - tileMinZ) * PixelsPerMeter;
float rxPx = b.RadiusX * PixelsPerMeter;
float rzPx = b.RadiusZ * PixelsPerMeter;
int kindInt = (int)b.Kind;