The cottage doorway 'void' (dark cell + floating entities while the chase camera looks through the opening): PortalProjection.ProjectToNdc clipped portals on w+z>=0 — the GL [-1,1] near-plane test — but acdream's camera builds its projection with D3D-convention Matrix4x4.CreatePerspectiveFieldOfView and a 1.0 m near plane (RetailChaseCamera). Against that matrix w+z>=0 discards everything within ~0.5 m of the eye, so when the camera orbits to ~0.1 m from a doorway portal the near edge is clipped, the far edge projects off-screen ([flap] showed p->0171 D=0.10 proj=4 clip=0 ndc Y=-3.5..-6.6), the room behind is culled (vis=1) and only the tiny vestibule shell draws -> dark void. Rotating away moved the eye off the portal -> vis=5 -> room rendered. Fix: clip against the EYE (w > MinW, MinW=0.05 m), near-INDEPENDENT — a portal you're standing in still projects (covers the screen) so the cell behind stays visible. We only use the projected x/y for the visibility clip region, so keeping vertices in front of the near plane is correct. Matches retail PView::GetClip near-clipping the portal before project. RED->GREEN regression test (doorway 0.1 m from a near=1.0 eye); 177 App tests green; the existing straddling ±50 bound still holds. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
94 lines
4.2 KiB
C#
94 lines
4.2 KiB
C#
// 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.
|
|
//
|
|
// 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
|
|
// near plane. acdream's cameras build projection with Matrix4x4.CreatePerspectiveFieldOfView (D3D
|
|
// convention, NDC z in [0,1]) and a 1.0 m near plane (RetailChaseCamera). The previous w+z>=0
|
|
// predicate was the GL ([-1,1]) near-plane test; against the D3D matrix it discarded everything
|
|
// within ~0.5 m of the eye, so a doorway the chase camera was ~0.1 m from got clipped to empty ->
|
|
// the cell behind it was culled -> the cottage doorway "void" (2026-06-03). Clipping at the eye
|
|
// (w > MinW) keeps a portal you're standing in (it covers the screen) so the cell behind stays
|
|
// visible. Retail PView::GetClip / ConstructView(CBldPortal) (decomp:432344 / 433832) near-clip the
|
|
// portal poly likewise before projecting.
|
|
using System.Collections.Generic;
|
|
using System.Numerics;
|
|
|
|
namespace AcDream.App.Rendering;
|
|
|
|
public static class PortalProjection
|
|
{
|
|
/// <summary>Project a cell-local polygon to NDC, preserving the projected winding of
|
|
/// the input (NOT normalized to CCW). The caller (PortalVisibilityBuilder) is responsible
|
|
/// for feeding camera-facing portal polygons (via the portal-side test) so the result is
|
|
/// CCW for the CCW-only <see cref="ScreenPolygonClip"/>. Returns fewer than 3 verts when
|
|
/// the polygon is entirely behind the camera / degenerate.</summary>
|
|
public static Vector2[] ProjectToNdc(IReadOnlyList<Vector3> localPoly, Matrix4x4 cellToWorld, Matrix4x4 viewProj)
|
|
{
|
|
if (localPoly == null || localPoly.Count < 3) return System.Array.Empty<Vector2>();
|
|
|
|
Matrix4x4 m = cellToWorld * viewProj;
|
|
|
|
// To clip space (keep w).
|
|
var clip = new List<Vector4>(localPoly.Count);
|
|
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);
|
|
if (clip.Count < 3) return System.Array.Empty<Vector2>();
|
|
|
|
// Perspective divide → NDC xy.
|
|
var ndc = new Vector2[clip.Count];
|
|
for (int i = 0; i < clip.Count; i++)
|
|
{
|
|
float w = clip[i].W;
|
|
ndc[i] = new Vector2(clip[i].X / w, clip[i].Y / w);
|
|
}
|
|
return ndc;
|
|
}
|
|
|
|
// Minimum clip-space w (≈ metres in front of the eye) to keep a vertex. Excludes the eye
|
|
// (w=0) singularity and the ~5 cm right at it (bounding the perspective divide), but is
|
|
// INTENTIONALLY far closer than the projection's 1.0 m near plane so a doorway the camera is
|
|
// 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)
|
|
{
|
|
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;
|
|
bool curIn = dCur >= 0f;
|
|
bool prevIn = dPrev >= 0f;
|
|
|
|
if (curIn)
|
|
{
|
|
if (!prevIn) result.Add(Lerp(prev, cur, dPrev, dCur));
|
|
result.Add(cur);
|
|
}
|
|
else if (prevIn)
|
|
{
|
|
result.Add(Lerp(prev, cur, dPrev, dCur));
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static Vector4 Lerp(Vector4 p, Vector4 q, float dp, float dq)
|
|
{
|
|
float t = dp / (dp - dq);
|
|
return p + t * (q - p);
|
|
}
|
|
}
|