Phase 1 simplified per-vertex height as byte * 2.0f, but AC stores heights as byte indices into a 256-entry non-linear float lookup (Region.LandDefs.LandHeightTable). Static object placements in LandBlockInfo use the real table, so terrain rendered with the simplified scale left buildings floating or buried. LandblockMesh.Build now takes an explicit float[] heightTable so the core code stays testable without a DatCollection. GameWindow loads Region id 0x13000000 once at startup and passes its LandDefs.LandHeightTable into every landblock mesh build. The Phase 1 tests use an identity table (i * 2f for i in 0..255) so their expectations remain unchanged. Addresses the 'buildings buried and floating' issue the user observed after the Phase 2a visual checkpoint. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
84 lines
2.5 KiB
C#
84 lines
2.5 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);
|
|
}
|
|
}
|