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;

View file

@ -2,7 +2,8 @@ shader_type canvas_item;
render_mode blend_add;
// Encodes positive height deltas into R ([-12m, +16m] => span 28m),
// plus positive roughness delta (G), metallic (B), and scorch/wear (A).
// roughness delta into G, scorch/track wear into B, and ALWAYS A = 1.0
// (required for gl_compatibility TransparentBg=false blend equations).
uniform int brush_kind = 0;
uniform float height_delta = 0.0;
uniform float height_span = 28.0;
@ -16,10 +17,9 @@ void fragment() {
discard;
}
float dh = 0.0;
float dg = 0.0;
float db = 0.0;
float da = 0.0;
float pos_dh = 0.0;
float pos_dg = 0.0;
float d_scorch = 0.0;
if (brush_kind == 0) {
// DuneRidge (positive)
@ -27,45 +27,41 @@ void fragment() {
env *= env;
float ridge = max(0.0, 1.0 - u * u);
float asym = 1.0 + 0.32 * u;
dh = max(0.0, height_delta) * env * (0.35 + 0.65 * ridge) * asym;
dg = 0.02 * env;
pos_dh = max(0.0, height_delta) * env * (0.35 + 0.65 * ridge) * asym;
pos_dg = 0.02 * env;
} else if (brush_kind == 1) {
// Mesa
float r = sqrt(r2);
float t = clamp((1.0 - r) / 0.45, 0.0, 1.0);
float plateau = t * t * (3.0 - 2.0 * t);
dh = max(0.0, height_delta) * plateau;
db = 0.05 * plateau;
pos_dh = max(0.0, height_delta) * plateau;
} else if (brush_kind == 2) {
// SalvagePad (positive part)
float env = 1.0 - r2;
env *= env;
dh = max(0.0, height_delta) * env;
db = 0.03 * env;
pos_dh = max(0.0, height_delta) * env;
pos_dg = 0.03 * env;
} else if (brush_kind == 4) {
// Crater ejecta rim + slag/scorch
float r = sqrt(r2);
float depth = abs(height_delta);
float rim_t = sin(3.14159265 * clamp((r - 0.55) / 0.45, 0.0, 1.0));
dh = 0.32 * depth * rim_t * rim_t;
float pit_t = clamp(1.0 - r / 0.65, 0.0, 1.0);
dg = 0.04 * (1.0 - r);
db = 0.10 * pit_t;
da = 0.78 * clamp(1.0 - r * 0.9, 0.0, 1.0);
pos_dh = 0.32 * depth * rim_t * rim_t;
pos_dg = 0.04 * (1.0 - r);
d_scorch = 0.78 * clamp(1.0 - r * 0.9, 0.0, 1.0);
} else if (brush_kind == 5) {
// Track wear
float env = 1.0 - r2;
env *= env;
da = 0.32 * env;
d_scorch = 0.32 * env;
} else if (brush_kind == 6) {
// Scorch
float r = sqrt(r2);
float mask = clamp(1.0 - r, 0.0, 1.0);
mask = mask * mask;
dg = 0.02 * mask;
db = 0.08 * mask;
da = 0.85 * mask;
pos_dg = 0.02 * mask;
d_scorch = 0.85 * mask;
}
COLOR = vec4(dh / height_span, dg, db, da);
COLOR = vec4(pos_dh / height_span, pos_dg, d_scorch, 1.0);
}

View file

@ -40,10 +40,10 @@ void fragment() {
col = mix(brush_albedo, vec3(0.32, 0.28, 0.24), border * 0.45);
alpha = clamp(env * 1.15, 0.0, 0.88);
} else if (brush_kind == 3) {
// Road: twin packed dirt ruts
// Road: twin packed dirt ruts running longitudinally along u (cross-track offset abs(v))
float env = 1.0 - r2;
env *= env;
float rut = smoothstep(0.15, 0.45, abs(u)) * (1.0 - smoothstep(0.60, 0.92, abs(u)));
float rut = smoothstep(0.15, 0.45, abs(v)) * (1.0 - smoothstep(0.60, 0.92, abs(v)));
col = mix(brush_albedo, brush_albedo * 0.76, rut);
alpha = env * (0.45 + 0.35 * rut);
} else if (brush_kind == 4) {

View file

@ -16,6 +16,7 @@ void fragment() {
}
float neg_dh = 0.0;
float neg_dg = 0.0;
if (brush_kind == 0) {
// Dry wash (negative DuneRidge)
float env = 1.0 - r2;
@ -36,5 +37,5 @@ void fragment() {
neg_dh = depth * pit_t * pit_t * (3.0 - 2.0 * pit_t);
}
COLOR = vec4(neg_dh / height_span, 0.0, 0.0, 0.0);
COLOR = vec4(neg_dh / height_span, neg_dg, 0.0, 1.0);
}

View file

@ -17,32 +17,37 @@ float hash21(vec2 p) {
return fract(p.x * p.y);
}
vec2 to_baked_uv(vec2 mesh_uv) {
return (mesh_uv * 253.0 + 1.5) / 256.0;
}
void vertex() {
float h_norm = textureLod(height_tex, UV, 0.0).r;
float h_norm = textureLod(height_tex, to_baked_uv(UV), 0.0).r;
VERTEX.y += decode_height(h_norm);
}
void fragment() {
vec2 uv_baked = to_baked_uv(UV);
const float texel_step = 1.0 / 256.0;
float world_step = tile_size / 256.0;
float world_step = tile_size / 253.0;
float hL = decode_height(texture(height_tex, UV - vec2(texel_step, 0.0)).r);
float hR = decode_height(texture(height_tex, UV + vec2(texel_step, 0.0)).r);
float hD = decode_height(texture(height_tex, UV - vec2(0.0, texel_step)).r);
float hU = decode_height(texture(height_tex, UV + vec2(0.0, texel_step)).r);
float hL = decode_height(texture(height_tex, uv_baked - vec2(texel_step, 0.0)).r);
float hR = decode_height(texture(height_tex, uv_baked + vec2(texel_step, 0.0)).r);
float hD = decode_height(texture(height_tex, uv_baked - vec2(0.0, texel_step)).r);
float hU = decode_height(texture(height_tex, uv_baked + vec2(0.0, texel_step)).r);
vec3 world_normal = normalize(vec3(hL - hR, 2.0 * world_step, hD - hU));
NORMAL = normalize((VIEW_MATRIX * vec4(world_normal, 0.0)).xyz);
vec4 mat = texture(height_tex, UV);
vec3 alb = texture(albedo_tex, UV).rgb;
vec4 mat = texture(height_tex, uv_baked);
vec3 alb = texture(albedo_tex, uv_baked).rgb;
// Subtle desert sand grain so dunes read crisply under sunlight
float grain = (hash21(floor(UV * 768.0)) - 0.5) * 0.045;
float scorch = clamp(mat.a, 0.0, 1.0);
float scorch = clamp(mat.b, 0.0, 1.0);
alb = clamp(alb + vec3(grain), 0.0, 1.0) * (1.0 - 0.30 * scorch);
ALBEDO = alb;
ROUGHNESS = clamp(mat.g, 0.2, 0.98);
METALLIC = clamp(mat.b, 0.0, 0.85);
ROUGHNESS = clamp(mat.g, 0.25, 1.0);
METALLIC = 0.05;
}

View file

@ -91,11 +91,35 @@ public partial class Smoke : Node
Check(!float.IsNaN(player.X) && !float.IsNaN(player.Y) && !float.IsNaN(player.Z), "Player position became NaN");
int rebakesBefore = _main.Terrain.DirtyRebakes;
float cpuBefore = _main.Session.State.Terrain.SampleHeight(-48f, -48f);
bool hasGpuBefore = _main.Terrain.TrySampleBakedHeight(-48f, -48f, out float gpuBefore);
if (hasGpuBefore)
{
Check(MathF.Abs(gpuBefore - cpuBefore) <= 0.25f,
$"Expected baked GPU height ({gpuBefore:F2}m) to match CPU height ({cpuBefore:F2}m) before crater");
}
_main.Session.State.Terrain.StampCrater(-48f, -48f, radius: 5f, depth: 1.4f);
await ToSignal(GetTree(), SceneTree.SignalName.PhysicsFrame);
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
float cpuAfter = _main.Session.State.Terrain.SampleHeight(-48f, -48f);
Check(cpuAfter < cpuBefore - 0.8f,
$"Expected CPU terrain height to drop after crater stamp (before {cpuBefore:F2}m, after {cpuAfter:F2}m)");
Check(_main.Terrain.DirtyRebakes == rebakesBefore + 1,
$"Expected 1 dirty tile rebake from isolated crater stamp, got {_main.Terrain.DirtyRebakes - rebakesBefore}");
if (_main.Terrain.TrySampleBakedHeight(-48f, -48f, out float gpuAfter))
{
Check(MathF.Abs(gpuAfter - cpuAfter) <= 0.25f,
$"Expected baked GPU height ({gpuAfter:F2}m) to match CPU height ({cpuAfter:F2}m) after crater");
if (hasGpuBefore)
{
Check(gpuAfter < gpuBefore - 0.8f,
$"Expected baked GPU height to drop after crater (before {gpuBefore:F2}m, after {gpuAfter:F2}m)");
}
}
_main.SetScriptedInput(null);
}