public static bool WorldToScreen(Rect clientRect, CameraInfo cameraInfo, Vector3 position, out Point screenCoordinates) { Vector3 diff = new Vector3(position) - cameraInfo.Pos; screenCoordinates = new Point(0, 0); if ((diff * cameraInfo.ViewMatrix.FirstCol) < Vector3.Zero) { return(false); } Vector3 view = diff * cameraInfo.ViewMatrix.Inverse(); Vector3 cam = new Vector3(-view.Y, -view.Z, view.X); float windowWidth = (clientRect.Right - clientRect.Left); float windowHeight = (clientRect.Bottom - clientRect.Top); float screenX = windowWidth / 2.0f; float screenY = windowHeight / 2.0f; float tmpX = screenX / MathF.Tan((cameraInfo.Fov * 180) * DEG_TO_RAD); float tmpY = screenY / MathF.Tan((cameraInfo.Fov * 180) * DEG_TO_RAD); screenCoordinates = new Point { X = (int)(screenX + cam.X * tmpX / cam.Z), Y = (int)(screenY + cam.Y * tmpY / cam.Z) }; return(screenCoordinates.X > 0 && screenCoordinates.Y > 0 && screenCoordinates.X < windowWidth && screenCoordinates.Y < windowHeight); }
public static bool WorldToScreen(Rect clientRect, RawCameraInfo cameraInfo, Vector3 position, out Point screenCoordinates) { Vector3 diff = position - cameraInfo.Pos; Vector3 view = diff * cameraInfo.ViewMatrix.Inverse(); float windowWidth = clientRect.Right - clientRect.Left; float windowHeight = clientRect.Bottom - clientRect.Top; float screenX = windowWidth / 2.0f; float screenY = windowHeight / 2.0f; float screenF = windowWidth / windowHeight; float tmpX = screenX / MathF.Tan((screenF * (screenF >= 1.6f ? 55.0f : 44.0f) / 2.0f) * DEG_TO_RAD); float tmpY = screenY / MathF.Tan((screenF * 35.0f / 2.0f) * DEG_TO_RAD); screenCoordinates = new() { X = (int)MathF.Abs(screenX + (-view.Y * tmpX / view.X)), Y = (int)MathF.Abs(screenY + (-view.Z * tmpY / view.X)) - 20 }; return(screenCoordinates.X > 0 && screenCoordinates.Y > 0 && screenCoordinates.X < windowWidth && screenCoordinates.Y < windowHeight); } }