qstbak/Auspex/Auspex.Rendering.ImGuiFallback/FallbackClipMath.cs
2026-08-17 20:25:32 +10:00

64 lines
2 KiB
C#

using System.Numerics;
using System.Runtime.CompilerServices;
using Dalamud.Interface.Utility;
namespace Auspex.Rendering.ImGuiFallback;
internal static class FallbackClipMath
{
public enum LineClipStatus
{
NotVisible,
NotClipped,
A_Clipped,
B_Clipped
}
public static Vector3 XYZ(this Vector4 v)
{
return new Vector3(v.X, v.Y, v.Z);
}
public static LineClipStatus ClipLineToPlane(Vector4 plane, ref Vector3 a, ref Vector3 b, out float t)
{
t = 0f;
float num = Vector4.Dot(new Vector4(a, 1f), plane);
float num2 = Vector4.Dot(new Vector4(b, 1f), plane);
bool flag = num < 0f;
bool flag2 = num2 < 0f;
if (flag && flag2)
{
return LineClipStatus.NotClipped;
}
if (!flag && !flag2)
{
return LineClipStatus.NotVisible;
}
Vector3 vector = b - a;
t = (0f - num) / Vector3.Dot(vector, plane.XYZ());
Vector3 vector2 = a + vector * t;
if (flag)
{
b = vector2;
return LineClipStatus.B_Clipped;
}
a = vector2;
return LineClipStatus.A_Clipped;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void TransformCoordinate(in Vector3 coordinate, in Matrix4x4 transform, out Vector3 result)
{
result.X = coordinate.X * transform.M11 + coordinate.Y * transform.M21 + coordinate.Z * transform.M31 + transform.M41;
result.Y = coordinate.X * transform.M12 + coordinate.Y * transform.M22 + coordinate.Z * transform.M32 + transform.M42;
result.Z = coordinate.X * transform.M13 + coordinate.Y * transform.M23 + coordinate.Z * transform.M33 + transform.M43;
float num = 1f / (coordinate.X * transform.M14 + coordinate.Y * transform.M24 + coordinate.Z * transform.M34 + transform.M44);
result *= num;
}
public static Vector2 WorldToScreenOld(in Matrix4x4 viewProj, in Vector3 worldPos)
{
TransformCoordinate(in worldPos, in viewProj, out var result);
return new Vector2(0.5f * ImGuiHelpers.MainViewport.Size.X * (1f + result.X), 0.5f * ImGuiHelpers.MainViewport.Size.Y * (1f - result.Y)) + ImGuiHelpers.MainViewport.Pos;
}
}