diff --git a/client/TerrainRenderer.cs b/client/TerrainRenderer.cs index c8bb7e8..b5786f5 100644 --- a/client/TerrainRenderer.cs +++ b/client/TerrainRenderer.cs @@ -40,6 +40,8 @@ public partial class TerrainRenderer : Node3D } private readonly TileNode[,] _tiles = new TileNode[TerrainField.TileGridCount, TerrainField.TileGridCount]; + private ImageTexture _white1x1 = null!; + private Shader _clearShader = null!; private Shader _brushAddShader = null!; private Shader _brushSubShader = null!; private Shader _brushAlbedoShader = null!; @@ -51,6 +53,21 @@ public partial class TerrainRenderer : Node3D public override void _Ready() { + var whiteImg = Image.CreateEmpty(1, 1, false, Image.Format.Rgba8); + whiteImg.SetPixel(0, 0, Colors.White); + _white1x1 = ImageTexture.CreateFromImage(whiteImg); + + _clearShader = new Shader + { + Code = """ + shader_type canvas_item; + render_mode blend_disabled; + uniform vec4 clear_color = vec4(0.0, 0.0, 0.0, 1.0); + void fragment() { + COLOR = clear_color; + } + """ + }; _brushAddShader = GD.Load("res://terrain_brush.gdshader"); _brushSubShader = GD.Load("res://terrain_brush_sub.gdshader"); _brushAlbedoShader = GD.Load("res://terrain_brush_albedo.gdshader"); @@ -105,6 +122,18 @@ public partial class TerrainRenderer : Node3D terrain.ClearDirtyMask(); } + public bool TryGetHeightImageFormat(out Image.Format format) + { + format = Image.Format.Max; + var img = _tiles[0, 0]?.HeightViewport?.GetTexture()?.GetImage(); + if (img is null || img.IsEmpty()) + { + return false; + } + format = img.GetFormat(); + return true; + } + public bool TrySampleBakedHeight(float wx, float wz, out float bakedHeight) { bakedHeight = 0f; @@ -222,7 +251,9 @@ public partial class TerrainRenderer : Node3D { AnchorMode = Camera2D.AnchorModeEnum.FixedTopLeft, Position = Vector2.Zero, - Enabled = true + Enabled = true, + PhysicsInterpolationMode = PhysicsInterpolationModeEnum.Off, + ProcessCallback = Camera2D.Camera2DProcessCallback.Physics }; vp.AddChild(cam); @@ -237,8 +268,8 @@ public partial class TerrainRenderer : Node3D ClearCanvasChildren(tile.AlbedoCanvas); // Full-tile base clear quads (Polygon2D avoids GUI pixel snapping) - tile.HeightCanvas.AddChild(CreateClearQuad(HeightClearColor)); - tile.AlbedoCanvas.AddChild(CreateClearQuad(AlbedoClearColor)); + 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; @@ -263,25 +294,25 @@ 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)); - tile.HeightCanvas.AddChild(CreateBrushQuad(px, py, rxPx, rzPx, b.RotationRad, _brushAddShader, kindInt, b.HeightDelta)); + 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)); } else if (b.Kind == TerrainBrushKind.Track) { - tile.HeightCanvas.AddChild(CreateBrushQuad(px, py, rxPx, rzPx, b.RotationRad, _brushSubShader, kindInt, b.HeightDelta)); - tile.HeightCanvas.AddChild(CreateBrushQuad(px, py, rxPx, rzPx, b.RotationRad, _brushAddShader, kindInt, b.HeightDelta)); + 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)); } else if (b.Kind == TerrainBrushKind.Road || b.HeightDelta < 0f) { - tile.HeightCanvas.AddChild(CreateBrushQuad(px, py, rxPx, rzPx, b.RotationRad, _brushSubShader, kindInt, b.HeightDelta)); + tile.HeightCanvas.AddChild(CreateBrushQuad(px, py, rxPx, rzPx, b.RotationRad, _brushSubShader, kindInt, b.HeightDelta, _white1x1)); } else { - tile.HeightCanvas.AddChild(CreateBrushQuad(px, py, rxPx, rzPx, b.RotationRad, _brushAddShader, kindInt, b.HeightDelta)); + tile.HeightCanvas.AddChild(CreateBrushQuad(px, py, rxPx, rzPx, b.RotationRad, _brushAddShader, kindInt, b.HeightDelta, _white1x1)); } // 2. Albedo brush pass - var albedoQuad = CreateOrientedQuad(px, py, rxPx, rzPx, b.RotationRad); + 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)); @@ -294,8 +325,10 @@ public partial class TerrainRenderer : Node3D tile.AlbedoViewport.RenderTargetUpdateMode = SubViewport.UpdateMode.Once; } - private static Polygon2D CreateClearQuad(Color color) + private static Polygon2D CreateClearQuad(Color color, Shader clearShader, ImageTexture whiteTex) { + var mat = new ShaderMaterial { Shader = clearShader }; + mat.SetShaderParameter("clear_color", color); return new Polygon2D { Polygon = new[] @@ -305,14 +338,17 @@ public partial class TerrainRenderer : Node3D new Vector2(ViewportSize, ViewportSize), new Vector2(0f, ViewportSize) }, - Color = color + UV = BrushUv, + Texture = whiteTex, + Material = mat }; } private static Polygon2D CreateOrientedQuad( float px, float py, float rxPx, float rzPx, - float rotationRad) + float rotationRad, + ImageTexture whiteTex) { return new Polygon2D { @@ -325,7 +361,8 @@ public partial class TerrainRenderer : Node3D new Vector2(rxPx, rzPx), new Vector2(-rxPx, rzPx) }, - UV = BrushUv + UV = BrushUv, + Texture = whiteTex }; } @@ -335,9 +372,10 @@ public partial class TerrainRenderer : Node3D float rotationRad, Shader shader, int kindInt, - float heightDelta) + float heightDelta, + ImageTexture whiteTex) { - var quad = CreateOrientedQuad(px, py, rxPx, rzPx, rotationRad); + 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); diff --git a/client/tests/Smoke.cs b/client/tests/Smoke.cs index e2de61a..2d1a032 100644 --- a/client/tests/Smoke.cs +++ b/client/tests/Smoke.cs @@ -101,13 +101,75 @@ public partial class Smoke : Node float gpuBefore = 0f; if (!isHeadless) { + if (_main.Terrain.TryGetHeightImageFormat(out var imgFormat)) + { + GD.Print($"SMOKE INFO: HeightViewport format={imgFormat}"); + } + bool hasGpuBefore = _main.Terrain.TrySampleBakedHeight(-96f, -96f, out gpuBefore); Check(hasGpuBefore, "Expected GPU HeightViewport readback to succeed before crater"); 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"); + float beforeErr = MathF.Abs(gpuBefore - cpuBefore); + GD.Print($"SMOKE INFO: Crater-site pre-stamp cpu={cpuBefore:F4}m gpu={gpuBefore:F4}m err={beforeErr:F4}m"); + Check(beforeErr <= 0.20f, + $"Expected baked GPU height ({gpuBefore:F4}m) to match CPU height ({cpuBefore:F4}m) before crater within 0.20m"); } + + // Verify brushed terrain relief (tallest Mesa/DuneRidge center) so a flat-map bug can never pass + TerrainBrush? reliefBrush = null; + float bestHillCpu = 0f; + foreach (var b in _main.Session.State.Terrain.Brushes) + { + if (b.Kind == TerrainBrushKind.Mesa || (b.Kind == TerrainBrushKind.DuneRidge && b.HeightDelta > 0f)) + { + float h = _main.Session.State.Terrain.SampleHeight(b.X, b.Z); + if (h > bestHillCpu) + { + bestHillCpu = h; + reliefBrush = b; + } + } + } + + Check(reliefBrush.HasValue && bestHillCpu > 10f, "Expected brushed terrain relief > 10m in generated arena"); + if (reliefBrush.HasValue) + { + var rb = reliefBrush.Value; + bool hasGpuHill = _main.Terrain.TrySampleBakedHeight(rb.X, rb.Z, out float gpuHill); + Check(hasGpuHill, "Expected GPU HeightViewport readback to succeed on brushed relief hill/mesa"); + if (hasGpuHill) + { + float hillErr = MathF.Abs(gpuHill - bestHillCpu); + GD.Print($"SMOKE INFO: Relief {rb.Kind} at ({rb.X:F1},{rb.Z:F1}) cpu={bestHillCpu:F4}m gpu={gpuHill:F4}m err={hillErr:F4}m"); + Check(gpuHill > 10f, $"Expected GPU baked hill height > 10m, got {gpuHill:F4}m"); + Check(hillErr <= 0.20f, + $"Expected GPU hill height ({gpuHill:F4}m) to match CPU ({bestHillCpu:F4}m) within 0.20m"); + } + } + + // Verify tile seam continuity across interior tile boundaries (< 0.01m) + float maxSeamErr = 0f; + float[] boundaries = { -192f, 0f, 192f }; + float[] offsets = { -280f, -96f, 96f, 280f }; + foreach (float bCoord in boundaries) + { + foreach (float ortho in offsets) + { + if (_main.Terrain.TrySampleBakedHeight(bCoord - 0.0001f, ortho, out float hL) && + _main.Terrain.TrySampleBakedHeight(bCoord + 0.0001f, ortho, out float hR)) + { + maxSeamErr = MathF.Max(maxSeamErr, MathF.Abs(hL - hR)); + } + if (_main.Terrain.TrySampleBakedHeight(ortho, bCoord - 0.0001f, out float hD) && + _main.Terrain.TrySampleBakedHeight(ortho, bCoord + 0.0001f, out float hU)) + { + maxSeamErr = MathF.Max(maxSeamErr, MathF.Abs(hD - hU)); + } + } + } + GD.Print($"SMOKE INFO: Max tile seam error={maxSeamErr:F5}m"); + Check(maxSeamErr < 0.01f, $"Expected tile seam error < 0.01m, got {maxSeamErr:F5}m"); } _main.Session.State.Terrain.StampCrater(-96f, -96f, radius: 5f, depth: 1.4f); @@ -127,10 +189,12 @@ public partial class Smoke : Node Check(hasGpuAfter, "Expected GPU HeightViewport readback to succeed after crater"); if (hasGpuAfter) { - Check(MathF.Abs(gpuAfter - cpuAfter) <= 0.25f, - $"Expected baked GPU height ({gpuAfter:F2}m) to match CPU height ({cpuAfter:F2}m) after crater"); + float afterErr = MathF.Abs(gpuAfter - cpuAfter); + GD.Print($"SMOKE INFO: Crater-site post-stamp cpu={cpuAfter:F4}m gpu={gpuAfter:F4}m err={afterErr:F4}m"); + Check(afterErr <= 0.20f, + $"Expected baked GPU height ({gpuAfter:F4}m) to match CPU height ({cpuAfter:F4}m) after crater within 0.20m"); Check(gpuAfter < gpuBefore - 0.8f, - $"Expected baked GPU height to drop after crater (before {gpuBefore:F2}m, after {gpuAfter:F2}m)"); + $"Expected baked GPU height to drop after crater (before {gpuBefore:F4}m, after {gpuAfter:F4}m)"); } }