Three root causes found via systematic debugging after the user reported that thedc60405texture fix and4763b97height table fix had no visible effect on Holtburg. ## Heightmap transpose (LandblockMesh.Build) Phase 1's LandblockMesh.Build indexed block.Height as y*9+x but AC packs per-vertex heights in x-major order (x*9+y, matching ACViewer's LandblockStruct: Height[x * VertexDim + y]). The bug was invisible on flat landblocks (Phase 1 smoke test) but left buildings buried by 10-13 world-Z units on Holtburg, because building Frame.Origin positions reference the un-transposed ground truth. Diagnostic evidence (before fix, Holtburg 0xA9B4FFFF): entity 0x020000A5 at ( 84.6,126.0) entityZ= 66.03 terrainZ= 78.15 delta=-12.13 entity 0x02000118 at ( 74.2,139.9) entityZ= 66.03 terrainZ= 78.92 delta=-12.89 After fix: deltas are 0.03 to 2.18 — buildings now sit on the ground with small positive offsets for foundations. Regression test added: Build_HeightmapPackedAsXMajor_NotYMajor asserts asymmetric heights land at the correct world positions. ## Solid-color surfaces with Translucency=1.0 (SurfaceDecoder.DecodeSolidColor) The "bright pink doors and windows" the user saw were 11 Holtburg surfaces with OrigTextureId==0 — these carry a ColorValue instead of a texture chain. Phase 2a's TextureCache dropped them into the magenta fallback. All 11 turned out to be Base1Solid|Translucent with Translucency=1.00, meaning "fully transparent placeholder surface" (debug ColorValue is gray/green/red/blue/black, never displayed). DecodeSolidColor now takes a translucency parameter and multiplies alpha by (1 - translucency), so Translucency=1.0 → alpha=0, and the mesh shader's existing alpha discard (< 0.5) makes the pixel invisible. TextureCache honors Surface.Type.HasFlag(Base1Solid) and passes surface.Translucency through. Regression tests added: DecodeSolidColor_Opaque_PreservesAlpha and DecodeSolidColor_FullyTranslucent_AlphaGoesToZero. ## Clipmap alpha-key (DecodeIndex16) AC convention (per ACViewer TextureCache.IndexToColor): on surfaces marked Base1ClipMap, palette indices 0..7 are treated as fully transparent regardless of their actual palette color. Without this, low-index pixels on clipmap surfaces (typically doorway cutouts and foliage) render as opaque using whatever sentinel color is at those palette slots. DecodeRenderSurface now takes an isClipMap parameter. TextureCache passes Surface.Type.HasFlag(Base1ClipMap). DecodeIndex16 forces rgba=(0,0,0,0) when isClipMap && idx < 8. Regression test added: DecodeIndex16_ClipMap_ZerosAlphaForLowIndices. ## Notes - dc60405's PFID_INDEX16 palette decoder remains correct — no change. - 4763b97's LandHeightTable wiring remains correct — real-table lookup still runs, it just happens to be linear at Holtburg's height range. The fix is forward-compatible with mountains elsewhere. - All three bugs were invisible to the original unit tests. The new regression tests pin them down. ## State - dotnet build: 0 warnings, 0 errors - dotnet test: 42 passing (was 38 + 4 new) - Runtime: 126 entities hydrated on Holtburg, no exceptions, no magenta fallback (counter was 11, now 0 via diagnostic confirmation) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
110 lines
3.7 KiB
C#
110 lines
3.7 KiB
C#
using System.Numerics;
|
|
using AcDream.Core.Terrain;
|
|
using DatReaderWriter.DBObjs;
|
|
using DatReaderWriter.Types;
|
|
|
|
namespace AcDream.Core.Tests.Terrain;
|
|
|
|
public class LandblockMeshTests
|
|
{
|
|
/// <summary>
|
|
/// Synthetic height table that mirrors Phase 1's simplified "* 2.0f" scale so
|
|
/// the existing tests continue to describe the same behavior. Real AC uses a
|
|
/// non-linear table from Region.LandDefs.LandHeightTable loaded at runtime.
|
|
/// </summary>
|
|
private static readonly float[] IdentityHeightTable =
|
|
Enumerable.Range(0, 256).Select(i => i * 2f).ToArray();
|
|
|
|
private static LandBlock BuildFlatLandBlock(byte heightIndex = 0)
|
|
{
|
|
var block = new LandBlock
|
|
{
|
|
HasObjects = false,
|
|
Terrain = new TerrainInfo[81],
|
|
Height = new byte[81],
|
|
};
|
|
for (int i = 0; i < 81; i++)
|
|
{
|
|
block.Terrain[i] = (ushort)0;
|
|
block.Height[i] = heightIndex;
|
|
}
|
|
return block;
|
|
}
|
|
|
|
[Fact]
|
|
public void Build_FlatBlock_Produces81VerticesAnd128Triangles()
|
|
{
|
|
var block = BuildFlatLandBlock();
|
|
|
|
var mesh = LandblockMesh.Build(block, IdentityHeightTable);
|
|
|
|
Assert.Equal(81, mesh.Vertices.Length);
|
|
Assert.Equal(128 * 3, mesh.Indices.Length);
|
|
}
|
|
|
|
[Fact]
|
|
public void Build_Vertices_Cover192x192WorldUnits()
|
|
{
|
|
var block = BuildFlatLandBlock();
|
|
|
|
var mesh = LandblockMesh.Build(block, IdentityHeightTable);
|
|
|
|
var minX = mesh.Vertices.Min(v => v.Position.X);
|
|
var maxX = mesh.Vertices.Max(v => v.Position.X);
|
|
var minY = mesh.Vertices.Min(v => v.Position.Y);
|
|
var maxY = mesh.Vertices.Max(v => v.Position.Y);
|
|
|
|
Assert.Equal(0.0f, minX);
|
|
Assert.Equal(192.0f, maxX);
|
|
Assert.Equal(0.0f, minY);
|
|
Assert.Equal(192.0f, maxY);
|
|
}
|
|
|
|
[Fact]
|
|
public void Build_FlatBlock_AllVerticesSameZ()
|
|
{
|
|
var block = BuildFlatLandBlock(heightIndex: 10);
|
|
|
|
var mesh = LandblockMesh.Build(block, IdentityHeightTable);
|
|
|
|
var zs = mesh.Vertices.Select(v => v.Position.Z).Distinct().ToArray();
|
|
Assert.Single(zs);
|
|
}
|
|
|
|
[Fact]
|
|
public void Build_HeightValues_ScaleByTwo()
|
|
{
|
|
var block = BuildFlatLandBlock(heightIndex: 5);
|
|
|
|
var mesh = LandblockMesh.Build(block, IdentityHeightTable);
|
|
|
|
// AC's Land::LandHeightTable scales height byte index by 2.0f for the simple ramp case.
|
|
Assert.Equal(10.0f, mesh.Vertices[0].Position.Z);
|
|
}
|
|
|
|
[Fact]
|
|
public void Build_HeightmapPackedAsXMajor_NotYMajor()
|
|
{
|
|
// Regression: Phase 1 used block.Height[y*9+x] which transposes the terrain
|
|
// along its diagonal relative to AC's native x-major packing. Invisible on
|
|
// flat landblocks but catastrophically wrong on Holtburg where static-object
|
|
// positions reference the un-transposed ground truth, leaving buildings
|
|
// buried by ~10 world-Z units.
|
|
//
|
|
// Set up an asymmetric heightmap: value at x-major index (x=2, y=0) = 5
|
|
// (scaled to Z=10), everything else 0. The vertex at world position
|
|
// (x=2*24=48, y=0) should have Z=10. The vertex at (x=0, y=2*24=48) should
|
|
// have Z=0. Y-major indexing would swap these.
|
|
var block = BuildFlatLandBlock();
|
|
block.Height[2 * 9 + 0] = 5; // x=2, y=0 in x-major packing
|
|
|
|
var mesh = LandblockMesh.Build(block, IdentityHeightTable);
|
|
|
|
// Find vertices by position. Vertex buffer uses y*9+x internally.
|
|
var vAt_x2_y0 = mesh.Vertices[0 * 9 + 2]; // world (48, 0)
|
|
var vAt_x0_y2 = mesh.Vertices[2 * 9 + 0]; // world (0, 48)
|
|
|
|
Assert.Equal(new Vector3(48, 0, 10), vAt_x2_y0.Position);
|
|
Assert.Equal(new Vector3(0, 48, 0), vAt_x0_y2.Position);
|
|
}
|
|
}
|