acdream/tests/AcDream.App.Tests/Rendering/PortalProjectionTests.cs
Erik 5f596f2d25 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>
2026-06-05 15:27:24 +02:00

172 lines
7.6 KiB
C#

using System;
using System.Numerics;
using AcDream.App.Rendering;
using Xunit;
namespace AcDream.App.Tests.Rendering;
public class PortalProjectionTests
{
// A simple GL-style perspective looking down -Z, camera at origin.
private static Matrix4x4 ViewProj()
{
var view = Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0, 0, -1), Vector3.UnitY);
var proj = Matrix4x4.CreatePerspectiveFieldOfView(1.0f, 1.0f, 0.1f, 1000f);
return view * proj;
}
[Fact]
public void Project_QuadInFront_ProducesNdcInsideViewport()
{
// A 2x2 quad at z=-5 (in front), cell-local == world (identity transform).
var poly = new[]
{
new Vector3(-1, -1, -5), new Vector3(1, -1, -5), new Vector3(1, 1, -5), new Vector3(-1, 1, -5),
};
var r = PortalProjection.ProjectToNdc(poly, Matrix4x4.Identity, ViewProj());
Assert.True(r.Length >= 3);
foreach (var v in r)
{
Assert.InRange(v.X, -1.001f, 1.001f);
Assert.InRange(v.Y, -1.001f, 1.001f);
}
}
[Fact]
public void Project_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),
};
var r = PortalProjection.ProjectToNdc(poly, Matrix4x4.Identity, ViewProj());
Assert.True(r.Length < 3);
}
[Fact]
public void Project_QuadStraddlingCamera_ClipsWithoutInversion()
{
// Spans from behind (z=+2) to in front (z=-5). Must clip to the in-front part,
// never produce a wildly out-of-range inverted vertex.
var poly = new[]
{
new Vector3(-1, -1, 2), new Vector3(1, -1, -5), new Vector3(1, 1, -5), new Vector3(-1, 1, 2),
};
var r = PortalProjection.ProjectToNdc(poly, Matrix4x4.Identity, ViewProj());
Assert.True(r.Length >= 3);
foreach (var v in r)
{
// A corner a few cm in front of the eye and ~1 m to the side genuinely
// projects large (~±37 NDC) but finite. ±50 still catches the ±7852
// perspective-inversion blow-up the old w-clip produced.
Assert.InRange(v.X, -50f, 50f); // bounded — no inversion blow-up
Assert.InRange(v.Y, -50f, 50f);
}
}
[Fact]
public void Project_QuadStraddlingCamera_DownstreamIntersectionIsValidOnScreen()
{
var poly = new[]
{
new Vector3(-1, -1, 2), new Vector3(1, -1, -5), new Vector3(1, 1, -5), new Vector3(-1, 1, 2),
};
var projected = PortalProjection.ProjectToNdc(poly, Matrix4x4.Identity, ViewProj());
Assert.True(projected.Length >= 3);
// The viewport region (NDC [-1,1]^2), same as CellView.FullScreen()'s single polygon.
var viewport = CellView.FullScreen().Polygons[0].Vertices;
var onScreen = ScreenPolygonClip.Intersect(projected, viewport);
Assert.True(onScreen.Length >= 3); // a non-empty visible region survives
foreach (var v in onScreen)
{
Assert.InRange(v.X, -1.001f, 1.001f);
Assert.InRange(v.Y, -1.001f, 1.001f);
}
}
[Fact]
public void Project_QuadStraddlingCamera_NdcStaysWithinScreen()
{
// R1 void-flap fix (2026-06-05): the eye-plane-only clip (w>MinW) let small-w verts
// explode under the perspective divide (~±37 NDC). Those off-screen NDC then corrupted
// the downstream 2D ScreenPolygonClip, which at glancing/close angles collapsed to EMPTY
// -> OutsideView empty -> terrain Skip -> the bluish "void" at the cottage doorway.
// Clipping the 4 frustum SIDE planes in clip space (homogeneous, before the divide)
// bounds every projected vertex to the screen [-1,1] by construction. RED before the fix.
var poly = new[]
{
new Vector3(-1, -1, 2), new Vector3(1, -1, -5), new Vector3(1, 1, -5), new Vector3(-1, 1, 2),
};
var r = PortalProjection.ProjectToNdc(poly, Matrix4x4.Identity, ViewProj());
Assert.True(r.Length >= 3);
foreach (var v in r)
{
Assert.InRange(v.X, -1.001f, 1.001f); // bounded to the screen — no off-screen explosion
Assert.InRange(v.Y, -1.001f, 1.001f);
}
}
[Fact]
public void Project_CloseDoorway_NdcStaysWithinScreen_AndCoversScreen()
{
// The probe-confirmed void frame (2026-06-05): the chase eye is ~0.28 m from the front-door
// EXIT portal — well inside RetailChaseCamera's 1.0 m near plane — and looking through it.
// The door subtends the whole screen, but the old clip produced NDC like (10.2,-67.4) and
// ScreenPolygonClip reduced it to clip=0 (the void). After the homogeneous side-plane clip
// the NDC stays on-screen AND the door still covers the viewport (non-empty), not the void.
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); // RetailChaseCamera
var viewProj = view * proj;
// A 2 m x 2 m doorway 0.28 m in front of the eye, facing it.
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 projected = PortalProjection.ProjectToNdc(doorway, Matrix4x4.Identity, viewProj);
Assert.True(projected.Length >= 3);
foreach (var v in projected)
{
Assert.InRange(v.X, -1.001f, 1.001f);
Assert.InRange(v.Y, -1.001f, 1.001f);
}
var viewport = CellView.FullScreen().Polygons[0].Vertices;
var onScreen = ScreenPolygonClip.Intersect(projected, viewport);
Assert.True(onScreen.Length >= 3, "a doorway the eye looks through must cover the screen, not collapse to the void");
}
[Fact]
public void Project_PortalEyeIsAlmostTouching_StaysVisibleOnScreen()
{
// Regression (2026-06-03 doorway "void"): the chase camera orbits to ~0.1 m from a
// doorway portal. With RetailChaseCamera's 1.0 m near plane and the old w+z>=0 GL
// near-clip — which, for a D3D CreatePerspectiveFieldOfView matrix, discards everything
// within ~0.5 m of the eye — the whole doorway was clipped to empty, so the room behind
// it was culled and rendered as a dark void (camera-orientation dependent; rotating away
// fixed it). The eye-clip must be near-INDEPENDENT: a portal you're standing in must
// still project (covering the screen) so the cell behind stays visible.
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); // RetailChaseCamera
var viewProj = view * proj;
// A 2 m x 2 m doorway 0.1 m in front of the eye, facing it.
var doorway = new[]
{
new Vector3(-1f, -1f, -0.1f), new Vector3(1f, -1f, -0.1f),
new Vector3(1f, 1f, -0.1f), new Vector3(-1f, 1f, -0.1f),
};
var projected = PortalProjection.ProjectToNdc(doorway, Matrix4x4.Identity, viewProj);
Assert.True(projected.Length >= 3,
"a doorway 0.1 m from the eye must still project (was clipped to empty -> void)");
var viewport = CellView.FullScreen().Polygons[0].Vertices;
var onScreen = ScreenPolygonClip.Intersect(projected, viewport);
Assert.True(onScreen.Length >= 3,
"the cell behind a doorway you're standing in must stay visible (the void bug)");
}
}