feat(render): Phase A8.F — PortalVisibilityBuilder recursive portal-clip BFS
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
c665f3eef3
commit
0ed462cb62
2 changed files with 275 additions and 0 deletions
158
src/AcDream.App/Rendering/PortalVisibilityBuilder.cs
Normal file
158
src/AcDream.App/Rendering/PortalVisibilityBuilder.cs
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
// PortalVisibilityBuilder.cs
|
||||
//
|
||||
// Phase A8.F: recursive portal-clip visibility (the builder). Port of retail
|
||||
// PView::ConstructView (decomp:433750) -> ClipPortals (433572) -> AddViewToPortals
|
||||
// (433446). Walks the portal graph from the camera cell, accumulating a per-cell
|
||||
// screen-space CellView; exit portals union their clipped region into OutsideView.
|
||||
// GL-free; unit-tested without a GPU context.
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
|
||||
namespace AcDream.App.Rendering;
|
||||
|
||||
/// <summary>Per-frame output of the portal-frame BFS.</summary>
|
||||
public sealed class PortalVisibilityFrame
|
||||
{
|
||||
/// <summary>Screen region (NDC) where outdoor terrain/scenery may draw — exit portals
|
||||
/// recursively clipped to their portal chain. The cellar-flap fix.</summary>
|
||||
public CellView OutsideView { get; } = new();
|
||||
|
||||
/// <summary>Per-cell accumulated clip region, keyed by full cell id (wire-in #2).</summary>
|
||||
public Dictionary<uint, CellView> CellViews { get; } = new();
|
||||
|
||||
/// <summary>Entry clip regions for other buildings reached through our portals, keyed by the
|
||||
/// neighbour cell id that left the camera building's cell set (wire-in #3 / Step 5).</summary>
|
||||
public Dictionary<uint, CellView> CrossBuildingViews { get; } = new();
|
||||
}
|
||||
|
||||
public static class PortalVisibilityBuilder
|
||||
{
|
||||
// Bound on neighbour re-processing (retail uses update_count timestamps). Portal graphs are
|
||||
// small; this guards cycles while allowing multi-portal unions into the same neighbour.
|
||||
private const int MaxReprocessPerCell = 4;
|
||||
private const float PortalSideEpsilon = 0.01f; // matches CellVisibility.PointInCellEpsilon
|
||||
|
||||
/// <param name="lookup">Resolve a full cell id to its LoadedCell, or null if not loaded.</param>
|
||||
/// <param name="buildingMembership">Optional: true if a cell id is in the camera building's cell
|
||||
/// set. When provided, a neighbour OUTSIDE the set routes to CrossBuildingViews instead of
|
||||
/// continuing the in-building BFS. Pass null to treat all reachable cells as in-building.</param>
|
||||
public static PortalVisibilityFrame Build(
|
||||
LoadedCell cameraCell,
|
||||
Vector3 cameraPos,
|
||||
Func<uint, LoadedCell?> lookup,
|
||||
Matrix4x4 viewProj,
|
||||
Func<uint, bool>? buildingMembership = null)
|
||||
{
|
||||
var frame = new PortalVisibilityFrame();
|
||||
if (cameraCell == null) return frame;
|
||||
|
||||
uint lbMask = cameraCell.CellId & 0xFFFF0000u;
|
||||
|
||||
frame.CellViews[cameraCell.CellId] = CellView.FullScreen();
|
||||
var processCount = new Dictionary<uint, int>();
|
||||
var queue = new Queue<LoadedCell>();
|
||||
queue.Enqueue(cameraCell);
|
||||
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
var cell = queue.Dequeue();
|
||||
if (!frame.CellViews.TryGetValue(cell.CellId, out var currentView) || currentView.IsEmpty)
|
||||
continue;
|
||||
|
||||
processCount.TryGetValue(cell.CellId, out int pc);
|
||||
if (pc >= MaxReprocessPerCell) continue;
|
||||
processCount[cell.CellId] = pc + 1;
|
||||
|
||||
for (int i = 0; i < cell.Portals.Count; i++)
|
||||
{
|
||||
if (i >= cell.PortalPolygons.Count) continue;
|
||||
var poly = cell.PortalPolygons[i];
|
||||
if (poly == null || poly.Length < 3) continue;
|
||||
|
||||
// Portal-side test: only traverse a portal the camera is on the interior side of
|
||||
// (mirrors CellVisibility.GetVisibleCells + retail's 'seen' flag). Culls back-facing
|
||||
// portals so we never feed a degenerate/wrong-facing projection downstream.
|
||||
if (i < cell.ClipPlanes.Count && !CameraOnInteriorSide(cell, i, cameraPos))
|
||||
continue;
|
||||
|
||||
// Project to NDC, then normalize to CCW for the CCW-only ScreenPolygonClip
|
||||
// (ProjectToNdc preserves input winding; portal dat polygons may be CW).
|
||||
Vector2[] portalNdc = PortalProjection.ProjectToNdc(poly, cell.WorldTransform, viewProj);
|
||||
if (portalNdc.Length < 3) continue;
|
||||
EnsureCcw(portalNdc);
|
||||
|
||||
// Intersect the portal opening with every polygon of the current cell's view.
|
||||
var clippedRegion = new List<ViewPolygon>();
|
||||
foreach (var vp in currentView.Polygons)
|
||||
{
|
||||
var clipped = ScreenPolygonClip.Intersect(portalNdc, vp.Vertices);
|
||||
if (clipped.Length >= 3) clippedRegion.Add(new ViewPolygon(clipped));
|
||||
}
|
||||
if (clippedRegion.Count == 0) continue; // portal not visible through this chain
|
||||
|
||||
var portal = cell.Portals[i];
|
||||
|
||||
if (portal.OtherCellId == 0xFFFF)
|
||||
{
|
||||
// Exit portal -> outdoors visible through this (clipped) opening.
|
||||
foreach (var cp in clippedRegion) frame.OutsideView.Add(cp);
|
||||
continue;
|
||||
}
|
||||
|
||||
// TODO(A8.F): neighbour-side OtherPortalClip (decomp:433524) — also clip the
|
||||
// interior portal against the neighbour's matching portal polygon. Not implemented
|
||||
// here; add if multi-cell conformance shows over-inclusion.
|
||||
uint neighbourId = lbMask | portal.OtherCellId;
|
||||
|
||||
// Cross-building boundary: route to CrossBuildingViews, don't continue in-building BFS.
|
||||
if (buildingMembership != null && !buildingMembership(neighbourId))
|
||||
{
|
||||
var xview = GetOrCreate(frame.CrossBuildingViews, neighbourId);
|
||||
foreach (var cp in clippedRegion) xview.Add(cp);
|
||||
continue;
|
||||
}
|
||||
|
||||
var neighbour = lookup(neighbourId);
|
||||
if (neighbour == null) continue;
|
||||
|
||||
// Union the clipped region into the neighbour's view; (re)enqueue if it grew.
|
||||
var nview = GetOrCreate(frame.CellViews, neighbourId);
|
||||
int before = nview.Polygons.Count;
|
||||
foreach (var cp in clippedRegion) nview.Add(cp);
|
||||
if (nview.Polygons.Count > before)
|
||||
queue.Enqueue(neighbour);
|
||||
}
|
||||
}
|
||||
|
||||
return frame;
|
||||
}
|
||||
|
||||
// Mirrors CellVisibility's portal-side test (InsideSide convention).
|
||||
private static bool CameraOnInteriorSide(LoadedCell cell, int portalIndex, Vector3 cameraPos)
|
||||
{
|
||||
var plane = cell.ClipPlanes[portalIndex];
|
||||
if (plane.Normal.LengthSquared() < 1e-8f) return true; // no usable plane → allow
|
||||
var localCam = Vector3.Transform(cameraPos, cell.InverseWorldTransform);
|
||||
float dot = Vector3.Dot(plane.Normal, localCam) + plane.D;
|
||||
return plane.InsideSide == 0 ? dot >= -PortalSideEpsilon : dot <= PortalSideEpsilon;
|
||||
}
|
||||
|
||||
// Reverse vertex order in place if the polygon is wound clockwise (signed area < 0).
|
||||
private static void EnsureCcw(Vector2[] poly)
|
||||
{
|
||||
float area2 = 0f;
|
||||
for (int i = 0; i < poly.Length; i++)
|
||||
{
|
||||
var p = poly[i]; var q = poly[(i + 1) % poly.Length];
|
||||
area2 += p.X * q.Y - q.X * p.Y;
|
||||
}
|
||||
if (area2 < 0f) Array.Reverse(poly);
|
||||
}
|
||||
|
||||
private static CellView GetOrCreate(Dictionary<uint, CellView> map, uint key)
|
||||
{
|
||||
if (!map.TryGetValue(key, out var v)) { v = new CellView(); map[key] = v; }
|
||||
return v;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue