acdream/tests/AcDream.Core.Tests/Meshing/GfxObjMeshTests.cs
Erik 517d17b4b3 fix #426: extract solid-colour (NO_POS_UVS) faces; skip untextured subsets only on building shells and cells like retail
The Holtburg windmill axle (GfxObj 0x010010CE, 8 polygons, all
Stippling.NoPos + SurfaceType.Base1Solid) extracted to a 0-vertex mesh.
NoPos ("NO_POS_UVS", acclient.h:7380-7388) means "this side has no
texture coordinates" — true of every solid-colour polygon, since
nothing samples them — not "there is no positive face". Extraction read
it as the latter and dropped the polygon entirely, client-wide, for
every untextured polygon on every object.

Retail's D3DPolyRender::DrawMesh (@0x0059d4a0, named-retail decomp
~line 426048) draws an untextured subset on an ordinary object exactly
like a textured one; the only retail cases that skip an untextured
subset are a building shell (RenderDeviceD3D::DrawBuilding @0x0059f2a0
sets ObjBuildingOrBuildingPart=1) or an EnvCell interior
(RenderDeviceD3D::DrawEnvCell @0x0059f170, arg4=1). The #119
investigation's "retail's skipNoTexture never draws them either"
conclusion was itself wrong as a general rule.

- MeshExtractor.PrepareGfxObjMeshData / GfxObjMesh.Build: emit the
  positive side whenever PosSurface is a valid index, regardless of
  NoPos; the existing UV-index-0 fallback already produces zero
  texcoords for a NoPos polygon with no UVs on the wire.
- RetailUntexturedSurfacePolicy.IsUntextured(SurfaceType): the one
  place that answers "is this surface textured"
  ((type & (Base1Image|Base1ClipMap)) == 0), replacing the old
  `isSolid = NoPos || Base1Solid` (which also mis-classified a NEG-side
  batch by the POS-side's NoPos flag).
- RetailUntexturedSubsetPolicy.Draws(isBuildingShell, isUntextured):
  the shared draw-time gate wired into WbDrawDispatcher.ClassifyBatches,
  .PackedOracle.ClassifyPackedBatches, and
  .DirectionalShadows.AddDirectionalShadowBatches — one predicate so the
  three walks cannot drift (Campaign VM VM6 lesson).
- CellMesh.cs / MeshExtractor.PrepareCellStructMeshData deliberately
  KEEP their NoPos-gated skip for cell-wall geometry — retail's
  DrawEnvCell really does skip untextured subsets there; register row
  AP-234 documents the NoPos-vs-Surface.Type approximation.
- PakFormat.CurrentBakeToolVersion 4->5 (LauncherInstallRecordStore in
  lockstep): a pak baked by an older tool is missing every untextured
  face. No bake was run as part of this commit.

Also fixed: WorldBuilder's own upstream ObjectMeshManager.cs has the
identical NoPos bug (ObjectMeshManager.cs:959,984) — our port had
faithfully carried it over, and our own conformance test
(Build_NoPosFlag_OnlyEmitsNegSide) asserted the bug as correct WB
conformance. Renamed/reworded to Build_NoPosFlag_EmitsBothPosAndNegSide
with a citation for why retail decomp overrides WB here.

Issue119UpNullGfxObjDumpTests re-run against the installed DAT:
#119's own two objects (0x010002B4 9/9 polys, 0x010008A8 1/1 poly) now
gate DRAWS on every polygon instead of extracting to nothing.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-23 11:20:24 +02:00

302 lines
10 KiB
C#

using System.Numerics;
using AcDream.Core.Meshing;
using DatReaderWriter.DBObjs;
using DatReaderWriter.Enums;
using DatReaderWriter.Lib;
using DatReaderWriter.Types;
namespace AcDream.Core.Tests.Meshing;
public class GfxObjMeshTests
{
/// <summary>
/// Build a minimal GfxObj fixture with a single triangle using surface index 0.
/// Three unique positions, one UV slot each.
/// </summary>
private static GfxObj BuildSingleTriangle()
{
var gfx = new GfxObj
{
Surfaces = { 0x08000000u }, // synthetic surface id
VertexArray = new VertexArray
{
VertexType = VertexType.CSWVertexType,
Vertices =
{
[0] = new SWVertex
{
Origin = new Vector3(0, 0, 0),
Normal = new Vector3(0, 0, 1),
UVs = { new Vec2Duv { U = 0, V = 0 } },
},
[1] = new SWVertex
{
Origin = new Vector3(1, 0, 0),
Normal = new Vector3(0, 0, 1),
UVs = { new Vec2Duv { U = 1, V = 0 } },
},
[2] = new SWVertex
{
Origin = new Vector3(0, 1, 0),
Normal = new Vector3(0, 0, 1),
UVs = { new Vec2Duv { U = 0, V = 1 } },
},
},
},
Polygons =
{
[0] = new Polygon
{
PosSurface = 0,
NegSurface = -1,
VertexIds = { 0, 1, 2 },
PosUVIndices = { 0, 0, 0 },
},
},
};
return gfx;
}
[Fact]
public void Build_SingleTriangle_ProducesOneSubMeshOneTriangle()
{
var gfx = BuildSingleTriangle();
var subs = GfxObjMesh.Build(gfx);
var sub = Assert.Single(subs);
Assert.Equal(0x08000000u, sub.SurfaceId);
Assert.Equal(3, sub.Vertices.Length);
Assert.Equal(3, sub.Indices.Length); // one triangle, 3 indices
}
[Fact]
public void Build_SingleTriangle_CopiesPositionsNormalsAndUVs()
{
var gfx = BuildSingleTriangle();
var sub = GfxObjMesh.Build(gfx).Single();
// Indices point at unique vertices; collect them in order.
var vAtIdx0 = sub.Vertices[sub.Indices[0]];
var vAtIdx1 = sub.Vertices[sub.Indices[1]];
var vAtIdx2 = sub.Vertices[sub.Indices[2]];
Assert.Equal(new Vector3(0, 0, 0), vAtIdx0.Position);
Assert.Equal(new Vector3(1, 0, 0), vAtIdx1.Position);
Assert.Equal(new Vector3(0, 1, 0), vAtIdx2.Position);
Assert.Equal(new Vector3(0, 0, 1), vAtIdx0.Normal);
Assert.Equal(new Vector2(0, 0), vAtIdx0.TexCoord);
Assert.Equal(new Vector2(1, 0), vAtIdx1.TexCoord);
Assert.Equal(new Vector2(0, 1), vAtIdx2.TexCoord);
}
[Fact]
public void Build_Quad_IsTriangulatedAsFan()
{
// Single quad polygon with 4 vertices -> 2 triangles, 6 indices.
var gfx = new GfxObj
{
Surfaces = { 0x08000000u },
VertexArray = new VertexArray
{
Vertices =
{
[0] = new SWVertex { Origin = new(0, 0, 0), UVs = { new Vec2Duv() } },
[1] = new SWVertex { Origin = new(1, 0, 0), UVs = { new Vec2Duv() } },
[2] = new SWVertex { Origin = new(1, 1, 0), UVs = { new Vec2Duv() } },
[3] = new SWVertex { Origin = new(0, 1, 0), UVs = { new Vec2Duv() } },
},
},
Polygons =
{
[0] = new Polygon
{
PosSurface = 0,
VertexIds = { 0, 1, 2, 3 },
PosUVIndices = { 0, 0, 0, 0 },
},
},
};
var sub = GfxObjMesh.Build(gfx).Single();
Assert.Equal(4, sub.Vertices.Length);
Assert.Equal(6, sub.Indices.Length); // 2 triangles
}
[Fact]
public void Build_SamePositionDifferentUVs_DuplicatesOutputVertices()
{
// One vertex has two different UV slots. Each (posIdx, uvIdx) combo
// becomes a distinct output vertex.
var gfx = new GfxObj
{
Surfaces = { 0x08000000u },
VertexArray = new VertexArray
{
Vertices =
{
[0] = new SWVertex
{
Origin = new(0, 0, 0),
UVs =
{
new Vec2Duv { U = 0, V = 0 },
new Vec2Duv { U = 1, V = 1 },
},
},
[1] = new SWVertex { Origin = new(1, 0, 0), UVs = { new Vec2Duv() } },
[2] = new SWVertex { Origin = new(0, 1, 0), UVs = { new Vec2Duv() } },
},
},
Polygons =
{
[0] = new Polygon
{
PosSurface = 0,
VertexIds = { 0, 1, 2 },
PosUVIndices = { 0, 0, 0 },
},
[1] = new Polygon
{
PosSurface = 0,
VertexIds = { 0, 1, 2 },
PosUVIndices = { 1, 0, 0 }, // same positions, different UV on vert 0
},
},
};
var sub = GfxObjMesh.Build(gfx).Single();
// vert 0 has two different UV slots → 2 output vertices for pos 0
// vert 1 + 2 unique → 2 more output vertices
// total: 4 output vertices
Assert.Equal(4, sub.Vertices.Length);
Assert.Equal(6, sub.Indices.Length); // 2 triangles
}
[Fact]
public void Build_MultipleSurfaces_ProducesMultipleSubMeshes()
{
// 2 polygons, 2 surfaces → 2 sub-meshes.
var gfx = new GfxObj
{
Surfaces = { 0x08000001u, 0x08000002u },
VertexArray = new VertexArray
{
Vertices =
{
[0] = new SWVertex { Origin = new(0, 0, 0), UVs = { new Vec2Duv() } },
[1] = new SWVertex { Origin = new(1, 0, 0), UVs = { new Vec2Duv() } },
[2] = new SWVertex { Origin = new(0, 1, 0), UVs = { new Vec2Duv() } },
[3] = new SWVertex { Origin = new(1, 1, 0), UVs = { new Vec2Duv() } },
},
},
Polygons =
{
[0] = new Polygon
{
PosSurface = 0,
VertexIds = { 0, 1, 2 },
PosUVIndices = { 0, 0, 0 },
},
[1] = new Polygon
{
PosSurface = 1,
VertexIds = { 1, 3, 2 },
PosUVIndices = { 0, 0, 0 },
},
},
};
var subs = GfxObjMesh.Build(gfx);
Assert.Equal(2, subs.Count);
Assert.Contains(subs, s => s.SurfaceId == 0x08000001u);
Assert.Contains(subs, s => s.SurfaceId == 0x08000002u);
}
[Fact]
public void Build_DegeneratePolygonWithTwoVertices_Skipped()
{
var gfx = new GfxObj
{
Surfaces = { 0x08000000u },
VertexArray = new VertexArray
{
Vertices =
{
[0] = new SWVertex { Origin = new(0, 0, 0), UVs = { new Vec2Duv() } },
[1] = new SWVertex { Origin = new(1, 0, 0), UVs = { new Vec2Duv() } },
},
},
Polygons =
{
[0] = new Polygon
{
PosSurface = 0,
VertexIds = { 0, 1 },
PosUVIndices = { 0, 0 },
},
},
};
var subs = GfxObjMesh.Build(gfx);
Assert.Empty(subs); // no valid polygons → no sub-meshes
}
/// <summary>
/// #426 (2026-08-23, Holtburg windmill axle 0x010010CE): a NoPos-flagged
/// polygon ("this side has no texture coordinates", acclient.h:7386) is
/// NOT "no positive face" — every solid-colour polygon carries NoPos.
/// Before the fix, <c>hasPos = !Stippling.NoPos</c> dropped this quad
/// entirely, producing zero vertices. GfxObjMesh.Build doesn't branch on
/// Surface.Type at all (that classification — "is this untextured" —
/// lives in RetailUntexturedSurfacePolicy and is consumed by
/// MeshExtractor, which is what actually decides solid-vs-textured
/// rendering), so a NoPos polygon over a "solid" surface and a NoPos
/// polygon over a "textured" surface are IDENTICAL from this method's
/// point of view — both are proven by this one case.
/// </summary>
[Fact]
public void Build_NoPosQuad_StillEmitsPositiveSideVerticesAndIndices()
{
var gfx = new GfxObj
{
Surfaces = { 0x08000000u },
VertexArray = new VertexArray
{
Vertices =
{
// No UVs at all — matches a real solid-colour polygon's
// vertices, which carry no UV entries because nothing
// ever samples them.
[0] = new SWVertex { Origin = new(0, 0, 0) },
[1] = new SWVertex { Origin = new(1, 0, 0) },
[2] = new SWVertex { Origin = new(1, 1, 0) },
[3] = new SWVertex { Origin = new(0, 1, 0) },
},
},
Polygons =
{
[0] = new Polygon
{
Stippling = StipplingType.NoPos,
PosSurface = 0,
NegSurface = -1,
VertexIds = { 0, 1, 2, 3 },
// No PosUVIndices — NoPos means there ARE none on the wire.
},
},
};
var sub = GfxObjMesh.Build(gfx).Single();
Assert.Equal(4, sub.Vertices.Length);
Assert.Equal(6, sub.Indices.Length); // fan-triangulated quad, 2 triangles
Assert.All(sub.Vertices, v => Assert.Equal(Vector2.Zero, v.TexCoord));
}
}