154 lines
9.1 KiB
C#
154 lines
9.1 KiB
C#
using System.Numerics;
|
|
using GamePort.Assets;
|
|
using GamePort.Game;
|
|
|
|
var count = 0;
|
|
void Check(bool value, string message) { if (!value) throw new Exception(message); }
|
|
void Near(float actual, float expected, float tolerance, string message) =>
|
|
Check(MathF.Abs(actual - expected) <= tolerance, $"{message}: {actual} != {expected}");
|
|
void Test(string name, Action action) { action(); count++; Console.WriteLine($"PASS {name}"); }
|
|
void Reject<T>(Action action, string message) where T : Exception
|
|
{
|
|
try { action(); } catch (T) { return; }
|
|
throw new Exception(message);
|
|
}
|
|
SupportTriangle Horizontal(float y, int shape = 1, int polygon = 0) =>
|
|
new(new(-10, y, -10), new(10, y, -10), new(0, y, 10), shape, polygon);
|
|
CameraRayPolygon Wall(float y, int shape = 1, int polygon = 0) => new(Horizontal(y, shape, polygon), Vector3.Zero);
|
|
CameraRayPolygon Floor(float y, int shape = 1, int polygon = 0) => new(Horizontal(y, shape, polygon), Vector3.UnitY);
|
|
var high = new Vector3(0, 100, 0);
|
|
var low = Vector3.Zero;
|
|
|
|
Test("farther FIFO wall hit wins over nearer wall and general-grid floor", () =>
|
|
{
|
|
var ray = new OriginalCameraRay([Wall(20, 1), Wall(80, 2), Floor(90, 0)]);
|
|
var hit = ray.Trace(high, low);
|
|
Check(hit?.ShapeId == 1 && hit?.PolygonId == 0, "camera used nearest rather than first source wall");
|
|
Near(hit!.Value.Position.Y, 20, .00001f, "first wall point");
|
|
Near(hit.Value.Distance, 80, .00001f, "first wall distance");
|
|
var nearest = OriginalBodySupport.Trace([Horizontal(20, 1), Horizontal(80, 2), Horizontal(90, 0)], high, low);
|
|
Check(nearest?.ShapeId == 0, "support nearest reduction changed while extracting the shared kernel");
|
|
var result = ray.Constrain(high, low);
|
|
Near(result.Eye.Y, 40, .00001f, "source .25look+.75hit point");
|
|
Check(result.Gain == 1.5f && result.Hit == hit, "obstructed gain/identity");
|
|
});
|
|
|
|
Test("registration uses shape and polygon identities rather than render order", () =>
|
|
{
|
|
var ray = new OriginalCameraRay([Wall(80, 7, 2), Wall(20, 7, 1), Wall(90, 8, 0)]);
|
|
Check(ray.Trace(high, low)?.PolygonId == 1, "source polygon registration FIFO changed");
|
|
ray = new([Wall(80, 7), Wall(20, 3)]);
|
|
Check(ray.Trace(high, low)?.ShapeId == 3, "source shape registration order changed");
|
|
});
|
|
|
|
Test("inclusive cell rectangle scans increasing X and Z even against ray direction", () =>
|
|
{
|
|
SupportTriangle PlaneX(float x, int id) => new(new(x, -10, -10), new(x, -10, 10), new(x, 10, 0), id);
|
|
var rayX = new OriginalCameraRay([new(PlaneX(900, 1), Vector3.Zero), new(PlaneX(100, 2), Vector3.Zero)]);
|
|
Check(rayX.Trace(new(1000, 0, 0), Vector3.Zero)?.ShapeId == 2,
|
|
"grid rectangle should test farther lower-X cell first");
|
|
SupportTriangle PlaneZ(float z, int id) => new(new(-10, -10, z), new(0, 10, z), new(10, -10, z), id);
|
|
var rayZ = new OriginalCameraRay([new(PlaneZ(900, 1), Vector3.Zero), new(PlaneZ(100, 2), Vector3.Zero)]);
|
|
Check(rayZ.Trace(new(0, 0, 1000), Vector3.Zero)?.ShapeId == 2,
|
|
"inner grid rectangle should test farther lower-Z cell first");
|
|
});
|
|
|
|
Test("missed polygons are tested again in each cell and in fallback grid", () =>
|
|
{
|
|
var triangle = new SupportTriangle(new(-10, 0, 0), new(10, 0, 0), new(0, 0, 600));
|
|
var ray = new OriginalCameraRay([new(triangle, Vector3.Zero)]);
|
|
Check(ray.Trace(new(0, 10, 1), new(0, 10, 501)) == null, "parallel segment fabricated hit");
|
|
Check(ray.LastTriangleTests == 4, "query silently deduplicated source repeated-cell candidates");
|
|
});
|
|
|
|
Test("general grid fallback, stored-normal threshold and source winding", () =>
|
|
{
|
|
var ray = new OriginalCameraRay([new(Horizontal(50), new(0, .7f, 0))]);
|
|
Check(ray.PolygonCount == 1 && ray.WallPolygonCount == 0, "normal.Y equality must be excluded from wall grid");
|
|
Check(ray.Trace(high, low).HasValue && ray.LastTriangleTests == 1, "general grid fallback missing");
|
|
Check(ray.Trace(low, high) == null, "source backface accepted");
|
|
Check(ray.Trace(low, high, true).HasValue, "two-sided source override rejected");
|
|
var reverse = Horizontal(50) with { B = Horizontal(50).C, C = Horizontal(50).B };
|
|
Check(new OriginalCameraRay([new(reverse, Vector3.UnitY)]).Trace(high, low) == null, "serialized winding was reversed");
|
|
});
|
|
|
|
Test("beginning accepted, endpoint rejected, zero-length and degenerate triangles miss", () =>
|
|
{
|
|
var ray = new OriginalCameraRay([Floor(0)]);
|
|
Check(ray.Trace(new(0, 20, 0), low) == null, "strict segment endpoint accepted");
|
|
Check(ray.Trace(low, new(0, -20, 0)).HasValue, "beginning was rejected by an invented skin");
|
|
Check(ray.Trace(low, low) == null, "zero segment fabricated hit");
|
|
var degenerate = new OriginalCameraRay([new(new(Vector3.Zero, Vector3.Zero, Vector3.Zero), Vector3.Zero)]);
|
|
Check(degenerate.Trace(high, low) == null, "degenerate triangle fabricated hit");
|
|
Check(ray.Trace(new(0, 20, 0), new(0, .001f, 0)) == null, "triangle beyond segment accepted");
|
|
});
|
|
|
|
Test("NC shapes and checkpoint materials filter registration, null material remains", () =>
|
|
{
|
|
var ray = new OriginalCameraRay([
|
|
new(Horizontal(90, 1), Vector3.Zero, "NCwater"),
|
|
new(Horizontal(80, 2), Vector3.Zero, null, "CSTART"),
|
|
new(Horizontal(70, 3), Vector3.Zero, null, "cCHECK9"),
|
|
new(Horizontal(60, 4), Vector3.Zero, "ordinary", null),
|
|
new(Horizontal(50, 5), Vector3.Zero, null, "ccheck0"),
|
|
new(Horizontal(40, 6), Vector3.Zero, null, "ccheck10")]);
|
|
Check(ray.PolygonCount == 3 && ray.WallPolygonCount == 3, "source registration filters differ");
|
|
Check(ray.Trace(high, low)?.ShapeId == 4, "invisible null-material proxy was excluded");
|
|
});
|
|
|
|
Test("source enable flag and unobstructed gain leave desired eye unchanged", () =>
|
|
{
|
|
var ray = new OriginalCameraRay([Wall(50)]) { Enabled = false };
|
|
var result = ray.Constrain(high, low);
|
|
Check(result.Eye == low && result.Gain == 1 && result.Hit == null && ray.LastTriangleTests == 0,
|
|
"disabled predicate queried geometry or moved eye");
|
|
Check(ray.TraceGeometry(high, low)?.ShapeId == 1 && ray.LastTriangleTests == 0,
|
|
"camera-only debug switch disabled ceiling geometry or changed camera diagnostics");
|
|
ray.Enabled = true;
|
|
result = ray.Constrain(new(100, 100, 100), new(100, 0, 100));
|
|
Check(result.Eye == new Vector3(100, 0, 100) && result.Gain == 1 && result.Hit == null, "miss changed eye or gain");
|
|
});
|
|
|
|
Test("stored float cell scale, negative truncation, signed16 domain and out-of-grid misses", () =>
|
|
{
|
|
Check(BitConverter.SingleToInt32Bits(OriginalCameraRay.CellScale) == 0x3b03126f, "source cell scale changed");
|
|
Check(OriginalCameraRay.CellCoordinate(-1) == 129 && OriginalCameraRay.CellCoordinate(0) == 130,
|
|
"negative coordinate cell conversion changed");
|
|
Check(OriginalCameraRay.CellCoordinate(-65000) == 0 && OriginalCameraRay.CellCoordinate(-65500) == -1,
|
|
"source truncation near negative grid edge differs");
|
|
Check(OriginalCameraRay.CellCoordinate(1_000_000) == 2130, "out-of-grid conversion incorrectly clamps cells");
|
|
var ray = new OriginalCameraRay([Floor(50)]);
|
|
Check(ray.Trace(new(1_000_000, 100, 1_000_000), new(1_000_000, 0, 1_000_000)) == null,
|
|
"out-of-grid source query fabricated hit");
|
|
});
|
|
|
|
Test("malformed, nonfinite and unsupported scene contracts reject explicitly", () =>
|
|
{
|
|
Reject<InvalidDataException>(() => new OriginalCameraRay([Wall(20), Wall(80)]), "ambiguous source polygon identity accepted");
|
|
Reject<ArgumentOutOfRangeException>(() => new OriginalCameraRay([new(Horizontal(20), new(float.NaN, 0, 0))]), "nonfinite normal accepted");
|
|
var ray = new OriginalCameraRay([Wall(50)]);
|
|
Reject<ArgumentOutOfRangeException>(() => ray.Trace(new(float.NaN, 0, 0), low), "nonfinite segment accepted");
|
|
Reject<ArgumentOutOfRangeException>(() => OriginalCameraRay.CellCoordinate(float.MaxValue), "unknown x87 overflow silently emulated");
|
|
var empty = new Mesh([], []);
|
|
Reject<NotSupportedException>(() => OriginalCameraRay.FromRouteScene(empty, "LV2A"), "unrecovered game filter silently assumed false");
|
|
Reject<InvalidDataException>(() => OriginalCameraRay.FromRouteScene(new Mesh(new MeshVertex[3], [0, 1, 2]), "LV1B"),
|
|
"missing polygon source metadata accepted");
|
|
});
|
|
|
|
Test("original route geometry registration counts and retained invisible polygons", () =>
|
|
{
|
|
var prepared = Path.GetFullPath(args.Length > 0 ? args[0] : "artifacts/extracted");
|
|
foreach (var (scene, code, polygons, walls) in new[] {
|
|
("level1b", "LV1B", 2851, 2483), ("VR04", "VR04", 508, 420), ("level1", "LEV1", 5231, 3860) })
|
|
{
|
|
var mesh = Mesh.LoadSceneJson(AssetPath.Resolve(prepared, scene + "/scene.json"));
|
|
var ray = OriginalCameraRay.FromRouteScene(mesh, code);
|
|
Check(ray.PolygonCount == polygons && ray.WallPolygonCount == walls,
|
|
$"{scene} original polygon/wall registration differs: {ray.PolygonCount}/{ray.WallPolygonCount}");
|
|
if (scene == "level1b")
|
|
Check(mesh.Draws.Sum(d => (long)d.IndexCount) < mesh.Indices.Length, "lab proxy visibility fixture changed");
|
|
}
|
|
});
|
|
|
|
Console.WriteLine($"Camera ray checks: {count} groups passed. Source ordering/kernel translation verified; original runtime and x87 boundary equivalence remain unverified.");
|