Update WorldToScreen

- switch from SharpDX to .NET provided Matrix4x4 and Vector3.Transform
- add fix for positions behind the camera
This commit is contained in:
pohky 2022-11-25 14:19:02 +01:00
parent 8d402b0797
commit cd58456c03

View file

@ -191,25 +191,14 @@ public sealed unsafe class GameGui : IDisposable, IServiceType
var matrixSingleton = this.getMatrixSingleton();
// Read current ViewProjectionMatrix plus game window size
var viewProjectionMatrix = default(SharpDX.Matrix);
float width, height;
var windowPos = ImGuiHelpers.MainViewport.Pos;
var viewProjectionMatrix = *(Matrix4x4*)(matrixSingleton + 0x1b4);
var device = Device.Instance();
float width = device->Width;
float height = device->Height;
unsafe
{
var rawMatrix = (float*)(matrixSingleton + 0x1b4).ToPointer();
for (var i = 0; i < 16; i++, rawMatrix++)
viewProjectionMatrix[i] = *rawMatrix;
var device = Device.Instance();
width = device->Width;
height = device->Height;
}
var worldPosDx = worldPos.ToSharpDX();
SharpDX.Vector3.Transform(ref worldPosDx, ref viewProjectionMatrix, out SharpDX.Vector3 pCoords);
screenPos = new Vector2(pCoords.X / pCoords.Z, pCoords.Y / pCoords.Z);
var pCoords = Vector3.Transform(worldPos, viewProjectionMatrix);
screenPos = new Vector2(pCoords.X / MathF.Abs(pCoords.Z), pCoords.Y / MathF.Abs(pCoords.Z));
screenPos.X = (0.5f * width * (screenPos.X + 1f)) + windowPos.X;
screenPos.Y = (0.5f * height * (1f - screenPos.Y)) + windowPos.Y;