This reverts ceec3bc4. Two independent reasons, either sufficient.
The rendering regression. The slice deleted TextRenderGlStateScope, which
saved GL_MULTISAMPLE and GL_SAMPLE_ALPHA_TO_COVERAGE on entry, disabled them
for the text pass, and restored them on exit (TextRenderGlStateScope.cs:111-112
and 153-154 at the parent commit). Its replacement bakes that state into the
text pipeline but nothing restores it, and GlGpuPassEncoder.Dispose does not
either. Every world renderer is still raw GL at this point in the campaign, so
from the first UI frame onward the world drew with multisampling disabled.
The offline pixel gate caught it: 1,791 of 563,200 compared pixels differed,
0.318% against a 0.001 threshold. The commit message attributed this to
wall-clock-driven ambient animation shifting phase, and committed through the
failure. That explanation does not survive its own control: capturing twice at
the reverted-to commit differs by 19 pixels and twice at the slice's own commit
by 8, while base-versus-head differs by 1,791 - a 224x gap that no shared-noise
source explains. An amplified difference image settles it visually: the changed
pixels are the silhouette edges of every tree, building and rock, with terrain
interiors, water and the entire UI untouched. That is the signature of losing
edge antialiasing, not of animated sprites.
This is the exact failure mode two existing memory notes already warn about -
a mid-frame renderer must set every GL state it uses rather than inherit it,
and issue #52's lesson that a rendering migration must audit per-pass GL state
before declaring itself done.
The scope. The brief was three small leaf renderers plus additive frame-
lifecycle wiring, roughly ten files. The commit changed 334 files with 3,665
insertions and 3,845 deletions, including 323 public-to-internal visibility
conversions across the App assembly, 55 test files, two retired conformance
tests, and a self-described temporary escape hatch for bridging raw-GL viewport
textures. Even without the regression, that is not separable into the part
worth keeping and the part worth dropping.
Reverting rather than patching because the good work here - the RHI frame
lifecycle wiring and a genuine render-state-cache staleness fix - is small
enough to redo cleanly against a tightened spec, while untangling it from 300+
files of unrelated churn is not.
Post-revert: Release build clean, App suite back to 3,843 passed / 3 skipped,
offline pixel gate passing at 19 differing pixels.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
147 lines
5 KiB
C#
147 lines
5 KiB
C#
using System.Numerics;
|
|
|
|
// Phase O-T7: verbatim copy of WorldBuilder.Shared.Lib.GeometryUtils into
|
|
// the AcDream.App.Rendering.Wb namespace so the WorldBuilder.Shared project
|
|
// reference can be dropped. Only the float-precision overloads are used by
|
|
// ObjectMeshManager (RayIntersectsSphere + RayIntersectsTriangle). The
|
|
// double-precision overloads are kept verbatim for completeness.
|
|
|
|
namespace AcDream.App.Rendering.Wb;
|
|
|
|
public static class GeometryUtils {
|
|
|
|
public static bool RayIntersectsBox(Vector3 rayOrigin, Vector3 rayDirection, Vector3 min, Vector3 max, out float distance) {
|
|
distance = 0;
|
|
float tmin = 0.0f;
|
|
float tmax = float.MaxValue;
|
|
|
|
if (Math.Abs(rayDirection.X) < 1e-7f) {
|
|
if (rayOrigin.X < min.X || rayOrigin.X > max.X) return false;
|
|
}
|
|
else {
|
|
float invD = 1.0f / rayDirection.X;
|
|
float t0 = (min.X - rayOrigin.X) * invD;
|
|
float t1 = (max.X - rayOrigin.X) * invD;
|
|
if (t0 > t1) (t0, t1) = (t1, t0);
|
|
tmin = Math.Max(tmin, t0);
|
|
tmax = Math.Min(tmax, t1);
|
|
if (tmin > tmax) return false;
|
|
}
|
|
|
|
if (Math.Abs(rayDirection.Y) < 1e-7f) {
|
|
if (rayOrigin.Y < min.Y || rayOrigin.Y > max.Y) return false;
|
|
}
|
|
else {
|
|
float invD = 1.0f / rayDirection.Y;
|
|
float t0 = (min.Y - rayOrigin.Y) * invD;
|
|
float t1 = (max.Y - rayOrigin.Y) * invD;
|
|
if (t0 > t1) (t0, t1) = (t1, t0);
|
|
tmin = Math.Max(tmin, t0);
|
|
tmax = Math.Min(tmax, t1);
|
|
if (tmin > tmax) return false;
|
|
}
|
|
|
|
if (Math.Abs(rayDirection.Z) < 1e-7f) {
|
|
if (rayOrigin.Z < min.Z || rayOrigin.Z > max.Z) return false;
|
|
}
|
|
else {
|
|
float invD = 1.0f / rayDirection.Z;
|
|
float t0 = (min.Z - rayOrigin.Z) * invD;
|
|
float t1 = (max.Z - rayOrigin.Z) * invD;
|
|
if (t0 > t1) (t0, t1) = (t1, t0);
|
|
tmin = Math.Max(tmin, t0);
|
|
tmax = Math.Min(tmax, t1);
|
|
if (tmin > tmax) return false;
|
|
}
|
|
|
|
distance = tmin;
|
|
return true;
|
|
}
|
|
|
|
public static bool RayIntersectsTriangle(Vector3 origin, Vector3 direction, Vector3 v0, Vector3 v1, Vector3 v2, out float t) {
|
|
t = 0;
|
|
Vector3 edge1 = v1 - v0;
|
|
Vector3 edge2 = v2 - v0;
|
|
Vector3 h = Vector3.Cross(direction, edge2);
|
|
float a = Vector3.Dot(edge1, h);
|
|
|
|
if (a > -0.00001f && a < 0.00001f) return false;
|
|
|
|
float f = 1.0f / a;
|
|
Vector3 s = origin - v0;
|
|
float u = f * Vector3.Dot(s, h);
|
|
|
|
if (u < 0.0f || u > 1.0f) return false;
|
|
|
|
Vector3 q = Vector3.Cross(s, edge1);
|
|
float v = f * Vector3.Dot(direction, q);
|
|
|
|
if (v < 0.0f || u + v > 1.0f) return false;
|
|
|
|
t = f * Vector3.Dot(edge2, q);
|
|
return t > 0.00001f;
|
|
}
|
|
|
|
public static bool RayIntersectsSphere(Vector3 rayOrigin, Vector3 rayDirection, Vector3 sphereOrigin, float sphereRadius, out float distance) {
|
|
distance = 0;
|
|
Vector3 l = sphereOrigin - rayOrigin;
|
|
float tca = Vector3.Dot(l, rayDirection);
|
|
if (tca < 0) return false;
|
|
float d2 = Vector3.Dot(l, l) - tca * tca;
|
|
float r2 = sphereRadius * sphereRadius;
|
|
if (d2 > r2) return false;
|
|
float thc = MathF.Sqrt(r2 - d2);
|
|
distance = tca - thc;
|
|
return true;
|
|
}
|
|
|
|
public static ushort PackKey(int x, int y) => (ushort)((x << 8) | y);
|
|
|
|
/// <summary>
|
|
/// Converts a quaternion to Euler angles (in degrees) using the ZYX convention.
|
|
/// </summary>
|
|
public static Vector3 QuaternionToEuler(Quaternion q) {
|
|
float x = q.X, y = q.Y, z = q.Z, w = q.W;
|
|
float roll, pitch, yaw;
|
|
|
|
float sinr_cosp = 2 * (w * x + y * z);
|
|
float cosr_cosp = 1 - 2 * (x * x + y * y);
|
|
roll = (float)Math.Atan2(sinr_cosp, cosr_cosp);
|
|
|
|
float sinp = 2 * (w * y - z * x);
|
|
if (Math.Abs(sinp) >= 1)
|
|
pitch = (float)Math.CopySign(Math.PI / 2, sinp);
|
|
else
|
|
pitch = (float)Math.Asin(sinp);
|
|
|
|
float siny_cosp = 2 * (w * z + x * y);
|
|
float cosy_cosp = 1 - 2 * (y * y + z * z);
|
|
yaw = (float)Math.Atan2(siny_cosp, cosy_cosp);
|
|
|
|
return new Vector3(roll, pitch, yaw) * (180.0f / (float)Math.PI);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts Euler angles (in degrees) to a quaternion using the ZYX convention.
|
|
/// </summary>
|
|
public static Quaternion EulerToQuaternion(Vector3 euler) {
|
|
Vector3 rads = euler * (MathF.PI / 180.0f);
|
|
float roll = rads.X;
|
|
float pitch = rads.Y;
|
|
float yaw = rads.Z;
|
|
|
|
float cr = MathF.Cos(roll * 0.5f);
|
|
float sr = MathF.Sin(roll * 0.5f);
|
|
float cp = MathF.Cos(pitch * 0.5f);
|
|
float sp = MathF.Sin(pitch * 0.5f);
|
|
float cy = MathF.Cos(yaw * 0.5f);
|
|
float sy = MathF.Sin(yaw * 0.5f);
|
|
|
|
return new Quaternion(
|
|
sr * cp * cy - cr * sp * sy,
|
|
cr * sp * cy + sr * cp * sy,
|
|
cr * cp * sy - sr * sp * cy,
|
|
cr * cp * cy + sr * sp * sy
|
|
);
|
|
}
|
|
}
|