fix(render): clip portal projection against frustum side planes (clip-space)

ProjectToNdc clipped only the eye half-space (w>MinW, a 2026-06-03 workaround) and left the 4 frustum side planes to the 2D ScreenPolygonClip. When the eye is within a portal's near plane, small-w verts explode under the perspective divide (probe saw NDC (10.2,-67.4)); the 2D clip then collapses to empty -> OutsideView empty -> terrain Skip -> the bluish doorway void. Clip the eye plane + 4 side planes (homogeneous Sutherland-Hodgman) before the divide so NDC is bounded to the screen by construction, matching retail GetClip -> polyClipFinish (clip in clip-space before the divide; pc:432344).

Partial: NOT the full flicker fix. The dominant cause (camera boom drift + viewer-cell flip at boundaries + missing w=0 near-plane clip) is identified and deferred to the next session per the handoff. 2 RED->GREEN tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erik 2026-06-05 15:27:24 +02:00
parent 02837ad5dc
commit 5f596f2d25
2 changed files with 85 additions and 12 deletions

View file

@ -1,9 +1,11 @@
// PortalProjection.cs
//
// Phase A8.F: project a cell-local portal polygon to NDC screen space, clipping against the
// IN-FRONT-OF-EYE half-space (keep where w > MinW) so a portal straddling the camera does not
// invert under the perspective divide, and the divide stays bounded away from the w=0 eye
// singularity.
// Phase A8.F: project a cell-local portal polygon to NDC screen space. Homogeneous frustum clip
// in CLIP SPACE (before the perspective divide): first the IN-FRONT-OF-EYE half-space (keep where
// w > MinW) so a portal straddling the camera does not invert under the divide and the divide
// stays bounded away from the w=0 eye singularity, then the 4 SIDE planes (x,y within ±w) so every
// surviving vertex lands on the screen [-1,1] by construction. The side-plane clip is the R1
// void-flap fix (2026-06-05) — see ProjectToNdc.
//
// The clip is NEAR-INDEPENDENT on purpose. We only use the projected x/y for the visibility clip
// REGION, so a vertex in front of the eye is meaningful even if it is closer than the projection's
@ -38,10 +40,24 @@ public static class PortalProjection
foreach (var lp in localPoly)
clip.Add(Vector4.Transform(new Vector4(lp, 1f), m));
// Clip against the in-front-of-eye half-space (keep where w > MinW). Near-independent:
// see the file header — clipping at the projection's near plane culls portals the camera
// is standing in (the doorway "void").
clip = ClipBehindEye(clip);
// Homogeneous frustum clip in CLIP SPACE, before the perspective divide. First the
// in-front-of-eye half-space (w > MinW) — near-INDEPENDENT, so a portal the camera is
// standing in still projects (see header); then the 4 SIDE planes (x,y within ±w). The
// side clip is the R1 void-flap fix (2026-06-05): without it, a portal WITHIN the near
// plane projected small-w verts to wildly off-screen NDC (the probe saw (10.2,-67.4)),
// which corrupted the downstream 2D ScreenPolygonClip into an EMPTY region -> OutsideView
// empty -> terrain Skip -> the bluish doorway "void". Clipping the side planes here bounds
// every surviving vertex to the screen [-1,1] by construction, so a screen-covering doorway
// clips to the screen (non-empty) instead of collapsing. The eye plane is clipped FIRST so
// all survivors have w > 0, making the side-plane functionals (w ± x, w ± y) well defined.
// Near/far are intentionally NOT clipped (near-independence). Retail PView::GetClip
// (decomp:0x005a4320) projects + frustum-clips the portal poly likewise (research doc A §3.5).
clip = ClipPlane(clip, v => v.W - MinW); // in front of eye (near-independent)
if (clip.Count < 3) return System.Array.Empty<Vector2>();
clip = ClipPlane(clip, v => v.W + v.X); // left: x/w >= -1 <=> w + x >= 0
clip = ClipPlane(clip, v => v.W - v.X); // right: x/w <= 1 <=> w - x >= 0
clip = ClipPlane(clip, v => v.W + v.Y); // bottom: y/w >= -1 <=> w + y >= 0
clip = ClipPlane(clip, v => v.W - v.Y); // top: y/w <= 1 <=> w - y >= 0
if (clip.Count < 3) return System.Array.Empty<Vector2>();
// Perspective divide → NDC xy.
@ -60,16 +76,20 @@ public static class PortalProjection
// standing in still projects and the cell behind it stays visible. See the file header.
private const float MinW = 0.05f;
// Sutherland-Hodgman against the in-front-of-eye half-space: keep where w > MinW.
private static List<Vector4> ClipBehindEye(List<Vector4> poly)
// Sutherland-Hodgman against one half-space of the homogeneous view frustum, in CLIP SPACE.
// `dist` is the signed plane functional (>= 0 keeps the vertex); crossings are interpolated in
// homogeneous coords (perspective-correct). Callers apply the eye plane first so every survivor
// has w > 0, making the side-plane functionals (w ± x, w ± y) well defined.
private static List<Vector4> ClipPlane(List<Vector4> poly, System.Func<Vector4, float> dist)
{
if (poly.Count == 0) return poly;
var result = new List<Vector4>(poly.Count + 1);
for (int i = 0; i < poly.Count; i++)
{
Vector4 cur = poly[i];
Vector4 prev = poly[(i + poly.Count - 1) % poly.Count];
float dCur = cur.W - MinW;
float dPrev = prev.W - MinW;
float dCur = dist(cur);
float dPrev = dist(prev);
bool curIn = dCur >= 0f;
bool prevIn = dPrev >= 0f;