fix(terrain): align per-cell triangle geometry with ACE's ConstructPolygons convention

Our LandblockMesh, terrain.vert corner tables, and TerrainSurface.SampleZ
used the OPPOSITE diagonal for each CellSplitDirection enum value from
what ACE (and the decompiled retail client at FUN_00532a50) picks for the
same sign bit. Same formula, same sign-bit mapping, inverted geometry.

Symptom: remote players rendered at server-broadcast Z hovered or clipped
by up to ~1m on sloped cells. Flat cells masked the bug because all four
corner heights were equal so any triangle pair returned the same Z. Live
diagnostic confirmed +0.79m hover on cell (7,5) at lb(AA,B4) — a ~20°
slope — while flat neighbors agreed to floating-point noise.

Three coordinated edits so CPU mesh + GPU corner lookup + CPU sampler all
agree on the retail geometry:
 - LandblockMesh: SWtoNE branch now emits {BL,BR,TR}+{BL,TR,TL} (y=x cut),
   SEtoNW emits {BL,BR,TL}+{BR,TR,TL} (x+y=1 cut).
 - terrain.vert: corner-index tables updated to match.
 - TerrainSurface.SampleZ: swapped the two branches' interpolation.

After the fix, 19 live DIAG samples across flat + two slope transitions
all land within 0.01m of server Z. Staircase pattern during remote motion
on slopes is a separate bug (no per-frame collision resolution) and will
be addressed via the transition/FindValidPosition port.

Cross-verified against: ACE LandblockStruct.ConstructPolygons lines 221-
244, decompiled retail FUN_00532a50 (chunk_00530000.c:2235), ClientReference
IsSWtoNECut (tests/AcDream.Core.Tests/Terrain/ClientReference.cs).

Updated test SplitDirection_TerrainSurface_AgreesWith_TerrainBlending
with corrected expectations (Z values swap between the two branches).
All 717 tests green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erik 2026-04-21 13:20:59 +02:00
parent beffdf477e
commit 56975f8919
5 changed files with 96 additions and 71 deletions

View file

@ -79,7 +79,7 @@ public class ClientConformanceTests
public void SplitDirection_TerrainSurface_AgreesWith_TerrainBlending()
{
// Build an asymmetric heightmap where the two split directions
// produce different Z at the cell center (0.5, 0.5).
// produce different Z off-center.
// Heights: BL=0, BR=100, TL=0, TR=0 (steep slope along X only at Y=0)
var heights = new byte[81];
var heightTable = new float[256];
@ -91,32 +91,33 @@ public class ClientConformanceTests
heights[0 * 9 + 1] = 0; // TL
heights[1 * 9 + 1] = 0; // TR
// Sample at cell center (12, 12) = (0.5, 0.5) in cell coords.
// For SWtoNE split (tx+ty=1 boundary): both triangles give Z=50 at center.
// For SEtoNW split (ty=tx boundary): both triangles give Z=50 at center.
// So at exact center both agree. Sample at (18, 6) = (0.75, 0.25) instead.
// SWtoNE: tx+ty=1.0 → tx=0.75, ty=0.25, 0.75+0.25=1.0 → boundary
// Use (20, 4) = (0.833, 0.167): tx+ty=1.0 → still boundary
// Use (20, 2) = (0.833, 0.083): tx+ty=0.917 < 1 → BL+BR+TL triangle
// Z = 0 + (100-0)*0.833 + (0-0)*0.083 = 83.3
// For SEtoNW: ty=0.083 < tx=0.833 → BL+BR+TR triangle
// Z = 0 + (100-0)*0.833 + (0-100)*0.083 = 83.3 - 8.3 = 75.0
// These differ! So we can distinguish.
// Sample at (20, 2) = (0.833, 0.083) in cell-local coords.
//
// 2026-04-21 fix: matched to ACE's ConstructPolygons geometry.
// Our enum values now mean:
// SWtoNE → diagonal BL→TR (y=x), triangles {BL,BR,TR} + {BL,TR,TL}
// SEtoNW → diagonal BR→TL (x+y=1), triangles {BL,BR,TL} + {BR,TR,TL}
//
// At (tx=0.833, ty=0.083):
// SWtoNE: tx > ty → {BL,BR,TR} triangle
// Z = hBL + tx*(hBR-hBL) + ty*(hTR-hBR) = 0.833*100 + 0.083*(-100) ≈ 75.0
// SEtoNW: tx+ty=0.917 ≤ 1 → {BL,BR,TL} triangle
// Z = hBL + tx*(hBR-hBL) + ty*(hTL-hBL) = 0.833*100 + 0 ≈ 83.3
// Expectations swapped vs pre-fix.
// Use landblock (0,0) and check what the client says the split is.
bool clientSplit = ClientReference.IsSWtoNECut(0, 0);
var surface = new TerrainSurface(heights, heightTable, 0, 0);
float z = surface.SampleZ(20f, 2f);
if (clientSplit)
{
// SWtoNE → BL+BR+TL triangle at (0.833, 0.083) → Z ≈ 83.3
Assert.InRange(z, 82f, 85f);
// SWtoNE → {BL,BR,TR} triangle at (0.833, 0.083) → Z ≈ 75.0
Assert.InRange(z, 74f, 77f);
}
else
{
// SEtoNW → BL+BR+TR triangle at (0.833, 0.083) → Z ≈ 75.0
Assert.InRange(z, 74f, 77f);
// SEtoNW → {BL,BR,TL} triangle at (0.833, 0.083) → Z ≈ 83.3
Assert.InRange(z, 82f, 85f);
}
}