feat(render): indoor render WORKS — terminating portal flood + every-cell seal + look-in FPS

Checkpoint of the unified retail-faithful indoor render. The two-week HANG/grey is fixed and the
interior seals (live-verified by the user). Commits the session render-rewrite foundation together
with the fixes that made it functional.

- HANG fix: PortalVisibilityBuilder.Build portal flood did not terminate (the faithful ProjectToClip
  near-side clip drifts per round, defeating the CellView dedup; the BFS had no bound after U.2a removed
  MaxReprocessPerCell). Fix = drift-tolerant snapped/canonical CellView.Add dedup (PortalView.cs) plus
  restored MaxReprocessPerCell=16 bounded re-enqueue (PortalVisibilityBuilder.cs). Re-enqueue is kept
  (load-bearing for late-slice propagation, Build_ViewGrowthAfterDoneCell_PropagatesNewSlicesToExit);
  only its count is capped. CellViewDedupTests added.
- Seal (DrawCells Task 2): RetailPViewRenderer.DrawEnvCellShells draws EVERY visible cell via
  IndoorDrawPlan.ShellPass (was gated on the ClipFrameAssembler slot filter, leaving slot-less cells grey).
- Look-in FPS: GameWindow exterior look-in candidates limited to the player landblock +-1 (was all ~81
  loaded LBs iterated every outdoor frame). No behaviour change (far cells were >48m, already culled).

Remaining dominant issue = the FLAP at transitions: viewer-cell metastability (render roots at the
camera-eye cell, which oscillates outdoor-indoor as the 3rd-person boom drifts across the doorway,
confirmed in render-sig). SEPARATE fix, NOT the DrawCells port. Full handoff + flap fix plan + tracked
follow-ups (#78 terrain, look-in-from-inside, look-in FPS, L-spotlight):
docs/research/2026-06-07-indoor-render-session-handoff.md.

Baselines: build 0 err; App.Tests 210/210; Core.Tests 1331 pass / 4 fail (pre-existing) / 1 skip.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erik 2026-06-07 10:14:43 +02:00
parent bff1955066
commit 1405dd8e90
27 changed files with 3635 additions and 814 deletions

View file

@ -169,4 +169,175 @@ public class PortalProjectionTests
Assert.True(onScreen.Length >= 3,
"the cell behind a doorway you're standing in must stay visible (the void bug)");
}
// ---------------------------------------------------------------------------
// Faithful homogeneous (w-space) portal clip — port of retail PView::GetClip +
// PrimD3DRender::xformStart + ACRender::polyClipFinish (decomp 432344 / 424310 /
// 702749). The early divide + fixed side-plane clamp (ProjectToNdc) collapsed
// grazing/near portals to zero-area edge slivers (-> the flap) and near doorways
// to empty (-> the void/fallback). The faithful pipeline keeps homogeneous clip
// coords (ProjectToClip — eye-plane clip only, no divide) and runs Sutherland-
// Hodgman against the view region with w-aware edge tests, dividing the survivors
// only after they are bounded to the region (ClipToRegion). 2026-06-06.
// ---------------------------------------------------------------------------
private static Vector2[] FullScreenCcw() => new[]
{
new Vector2(-1f, -1f), new Vector2(1f, -1f), new Vector2(1f, 1f), new Vector2(-1f, 1f),
};
[Fact]
public void ProjectToClip_QuadInFront_KeepsVertsWithPositiveW()
{
var poly = new[]
{
new Vector3(-1, -1, -5), new Vector3(1, -1, -5), new Vector3(1, 1, -5), new Vector3(-1, 1, -5),
};
var clip = PortalProjection.ProjectToClip(poly, Matrix4x4.Identity, ViewProj());
Assert.True(clip.Length >= 3);
foreach (var v in clip)
Assert.True(v.W > 0f, $"an in-front portal vertex must keep w>0 (homogeneous), got w={v.W}");
}
[Fact]
public void ProjectToClip_QuadFullyBehind_ReturnsEmpty()
{
var poly = new[]
{
new Vector3(-1, -1, 5), new Vector3(1, -1, 5), new Vector3(1, 1, 5), new Vector3(-1, 1, 5),
};
Assert.True(PortalProjection.ProjectToClip(poly, Matrix4x4.Identity, ViewProj()).Length < 3);
}
[Fact]
public void ClipToRegion_OnScreenQuad_ReturnsBoundedNdc()
{
// A small 2x2 quad at z=-5 is fully on-screen; clipping against the full screen
// returns it bounded to [-1,1].
var poly = new[]
{
new Vector3(-1, -1, -5), new Vector3(1, -1, -5), new Vector3(1, 1, -5), new Vector3(-1, 1, -5),
};
var clip = PortalProjection.ProjectToClip(poly, Matrix4x4.Identity, ViewProj());
var ndc = PortalProjection.ClipToRegion(clip, FullScreenCcw());
Assert.True(ndc.Length >= 3);
foreach (var v in ndc) { Assert.InRange(v.X, -1.001f, 1.001f); Assert.InRange(v.Y, -1.001f, 1.001f); }
}
[Fact]
public void ClipToRegion_FullyOffScreen_ReturnsEmpty_NotSliver()
{
// The flap's root: a portal entirely off one screen edge. The old early-divide +
// side-plane clamp collapsed it to a zero-area sliver pinned to x=1.0 (proj=3) that
// propagated a degenerate region one hop and then died; the faithful clip returns
// EMPTY so the flood stops cleanly. Quad at z=-5, x in [3,5] -> ndc x ~[1.1,1.8], off right.
var poly = new[]
{
new Vector3(3, -1, -5), new Vector3(5, -1, -5), new Vector3(5, 1, -5), new Vector3(3, 1, -5),
};
var clip = PortalProjection.ProjectToClip(poly, Matrix4x4.Identity, ViewProj());
var ndc = PortalProjection.ClipToRegion(clip, FullScreenCcw());
Assert.True(ndc.Length < 3, $"fully off-screen portal must clip to empty, got {ndc.Length} verts (sliver)");
}
[Fact]
public void ClipToRegion_PartlyOffScreen_ClipsToBoundedNonEmpty()
{
// A quad straddling the right edge -> clipped to the on-screen part, bounded, non-empty.
var poly = new[]
{
new Vector3(0, -1, -5), new Vector3(4, -1, -5), new Vector3(4, 1, -5), new Vector3(0, 1, -5),
};
var clip = PortalProjection.ProjectToClip(poly, Matrix4x4.Identity, ViewProj());
var ndc = PortalProjection.ClipToRegion(clip, FullScreenCcw());
Assert.True(ndc.Length >= 3, "a partly-on-screen portal must produce a non-empty clipped region");
foreach (var v in ndc) { Assert.InRange(v.X, -1.001f, 1.001f); Assert.InRange(v.Y, -1.001f, 1.001f); }
}
[Fact]
public void ClipToRegion_DoorwayEyeLooksThrough_CoversScreen_WithoutFallback()
{
// The void frame: chase eye 0.28 m from a 2x2 m doorway, looking through it. The doorway
// subtends the whole screen. The faithful clip keeps the homogeneous verts (no early-divide
// blow-up) and clips to the screen quad -> covers the screen (non-empty). This is what makes
// the EyeInsidePortalOpening *fallback* unnecessary for an in-front doorway.
var view = Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0, 0, -1), Vector3.UnitY);
var proj = Matrix4x4.CreatePerspectiveFieldOfView(MathF.PI / 3f, 16f / 9f, 1.0f, 5000f);
var viewProj = view * proj;
var doorway = new[]
{
new Vector3(-1f, -1f, -0.28f), new Vector3(1f, -1f, -0.28f),
new Vector3(1f, 1f, -0.28f), new Vector3(-1f, 1f, -0.28f),
};
var clip = PortalProjection.ProjectToClip(doorway, Matrix4x4.Identity, viewProj);
var ndc = PortalProjection.ClipToRegion(clip, FullScreenCcw());
Assert.True(ndc.Length >= 3, "a doorway the eye looks through must cover the screen, not collapse to the void");
foreach (var v in ndc) { Assert.InRange(v.X, -1.001f, 1.001f); Assert.InRange(v.Y, -1.001f, 1.001f); }
}
[Fact]
public void ClipToRegion_StraddlingEye_OnScreenBounded_NoBlowup()
{
// A portal spanning from behind (z=+2) to in front (z=-5). The faithful clip keeps the
// in-front part and bounds it to the screen — no perspective-inversion blow-up, non-empty.
var poly = new[]
{
new Vector3(-1, -1, 2), new Vector3(1, -1, -5), new Vector3(1, 1, -5), new Vector3(-1, 1, 2),
};
var clip = PortalProjection.ProjectToClip(poly, Matrix4x4.Identity, ViewProj());
var ndc = PortalProjection.ClipToRegion(clip, FullScreenCcw());
Assert.True(ndc.Length >= 3);
foreach (var v in ndc) { Assert.InRange(v.X, -1.001f, 1.001f); Assert.InRange(v.Y, -1.001f, 1.001f); }
}
private static float AbsArea(Vector2[] p)
{
if (p == null || p.Length < 3) return 0f;
float a2 = 0f;
for (int i = 0; i < p.Length; i++) { var u = p[i]; var w = p[(i + 1) % p.Length]; a2 += u.X * w.Y - w.X * u.Y; }
return MathF.Abs(a2) * 0.5f;
}
[Fact]
public void ClipToRegion_SubjectFullyInsideRegion_ReturnsSubjectNotRegion()
{
// Regression for Build_AppliesReciprocalOtherPortalClip: a NARROW subject fully inside a WIDE
// region must return the narrow (subject ∩ region = subject), NOT the wide region. The builder's
// reciprocal clip is exactly this shape (reciprocal opening ∩ near-side region).
var view = Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0, 0, -1), Vector3.UnitY);
var proj = Matrix4x4.CreatePerspectiveFieldOfView(1.2f, 1.0f, 0.1f, 1000f);
var vp = view * proj;
var narrow = new[] { new Vector3(-0.3f, -0.9f, -3f), new Vector3(0.3f, -0.9f, -3f), new Vector3(0.3f, 0.9f, -3f), new Vector3(-0.3f, 0.9f, -3f) };
var wide = new[] { new Vector3(-0.9f, -0.9f, -3f), new Vector3(0.9f, -0.9f, -3f), new Vector3(0.9f, 0.9f, -3f), new Vector3(-0.9f, 0.9f, -3f) };
var narrowClip = PortalProjection.ProjectToClip(narrow, Matrix4x4.Identity, vp);
// Build the region exactly as the builder does (clip wide against the full screen → CCW region).
var wideRegion = PortalProjection.ClipToRegion(PortalProjection.ProjectToClip(wide, Matrix4x4.Identity, vp), FullScreenCcw());
var clipped = PortalProjection.ClipToRegion(narrowClip, wideRegion);
float narrowArea = AbsArea(PortalProjection.ClipToRegion(narrowClip, FullScreenCcw()));
float wideArea = AbsArea(wideRegion);
float clippedArea = AbsArea(clipped);
Assert.True(clippedArea <= narrowArea + 1e-3f,
$"subject∩region must be the narrow subject (area {narrowArea}), not the wide region (area {wideArea}); got {clippedArea}");
}
[Fact]
public void ClipToRegion_AgainstSubRegion_TightensToIntersection()
{
// The region clip is the propagation step: clipping a wide on-screen portal against a
// narrower view region must yield the intersection (the narrow region), not the wide portal.
var wide = new[]
{
new Vector3(-2, -2, -5), new Vector3(2, -2, -5), new Vector3(2, 2, -5), new Vector3(-2, 2, -5),
};
var clip = PortalProjection.ProjectToClip(wide, Matrix4x4.Identity, ViewProj());
var narrow = new[]
{
new Vector2(-0.3f, -0.3f), new Vector2(0.3f, -0.3f), new Vector2(0.3f, 0.3f), new Vector2(-0.3f, 0.3f),
};
var ndc = PortalProjection.ClipToRegion(clip, narrow);
Assert.True(ndc.Length >= 3);
foreach (var v in ndc) { Assert.InRange(v.X, -0.301f, 0.301f); Assert.InRange(v.Y, -0.301f, 0.301f); }
}
}