acdream/src/AcDream.Core/Meshing/RetailUntexturedSurfacePolicy.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

76 lines
3.9 KiB
C#

using DatReaderWriter.Enums;
namespace AcDream.Core.Meshing;
/// <summary>
/// Retail's textured-vs-untextured surface classification, ported from
/// <c>D3DPolyRender::DrawMesh</c> @0x0059d4a0 (named-retail decomp,
/// docs/research/named-retail/acclient_2013_pseudo_c.txt ~line 426048): a
/// surface subset is TEXTURED — and therefore never subject to either of
/// retail's "skip untextured" gates (see
/// <see cref="RetailUntexturedSubsetPolicy"/>) — when
/// <c>(surface-&gt;type &amp; 6) != 0</c>, i.e. when
/// <see cref="SurfaceType.Base1Image"/> (<c>BASE1_IMAGE</c>, 0x2) or
/// <see cref="SurfaceType.Base1ClipMap"/> (<c>BASE1_CLIPMAP</c>, 0x4) is set
/// (docs/research/named-retail/acclient.h:5822-5824). Every other surface —
/// including <see cref="SurfaceType.Base1Solid"/> and any surface whose type
/// carries neither bit — is UNTEXTURED: a flat-colour ("solid") subset filled
/// from <c>Surface.ColorValue</c> rather than a decoded texture.
/// </summary>
/// <remarks>
/// #426 (2026-08-23, the Holtburg windmill axle 0x010010CE): the polygon-side
/// <c>StipplingType.NoPos</c> flag ("this side has no texture coordinates",
/// acclient.h:7386) is NOT the same fact as "this surface is untextured" —
/// every solid-colour polygon carries NoPos (it has no UVs to carry), but
/// NoPos says nothing about whether the surface itself is textured. The old
/// extraction conflated the two (<c>isSolid = NoPos || Base1Solid</c>) and,
/// worse, used NoPos to decide whether to emit the polygon's positive side AT
/// ALL — dropping every solid-colour polygon on every object. This type is
/// the ONE place that answers "is this surface textured", built from the
/// Surface's own Type flags so extraction (isSolid / TextureKey.IsSolid) and
/// draw-time skip policy agree by construction.
/// </remarks>
public static class RetailUntexturedSurfacePolicy
{
public static bool IsUntextured(SurfaceType type) =>
(type & (SurfaceType.Base1Image | SurfaceType.Base1ClipMap)) == 0;
}
/// <summary>
/// Retail's draw-time policy for an UNTEXTURED (solid-colour) mesh subset on
/// an ordinary <see cref="AcDream.Core.World.WorldEntity"/>, ported from the
/// same <c>D3DPolyRender::DrawMesh</c> untextured branch: the subset draws
/// unless <c>skipNoTexture != 0 &amp;&amp;
/// RenderDeviceD3D::ObjBuildingOrBuildingPart != 0</c>. <c>skipNoTexture</c>
/// @0x00820e30 is a global initialised to 1 and never cleared, so in
/// practice the gate reduces to
/// <c>RenderDeviceD3D::ObjBuildingOrBuildingPart == 0</c>.
/// <c>RenderDeviceD3D::DrawBuilding</c> (@0x0059f2a0) sets that flag around
/// the building-shell draw, so a building shell's own untextured subsets are
/// the ONE case where an ordinary WorldEntity skips them — statics, scenery,
/// creatures, and items (<c>DrawMeshInternal</c> @0x0059f360 →
/// <c>DrawMesh(gfxobj, mesh, arg4: 0)</c>) always draw their untextured
/// subsets.
/// </summary>
/// <remarks>
/// EnvCell interiors are retail's OTHER "skip untextured" case
/// (<c>RenderDeviceD3D::DrawEnvCell</c> @0x0059f170 calls
/// <c>DrawMesh(..., arg4: 1)</c>), but EnvCell/CellStruct geometry never
/// reaches this predicate — it draws through <c>EnvCellRenderer</c> /
/// <c>MeshExtractor.PrepareCellStructMeshData</c>, which keeps its own
/// NoPos-based approximation of the same rule (see
/// <c>docs/architecture/retail-divergence-register.md</c> AP-234 and
/// <c>CellMesh.cs</c>'s matching gate).
/// <para>
/// ONE shared predicate for <c>WbDrawDispatcher</c>'s classic classifier
/// (<c>ClassifyBatches</c>), packed classifier (<c>ClassifyPackedBatches</c>),
/// and the directional-shadow caster walk (<c>AddDirectionalShadowBatches</c>)
/// — Campaign VM VM6 showed that two hand-maintained classifiers computing
/// the "same" fact independently drift apart.
/// </para>
/// </remarks>
public static class RetailUntexturedSubsetPolicy
{
public static bool Draws(bool isBuildingShell, bool isUntextured) =>
!isUntextured || !isBuildingShell;
}