diff --git a/Fika.Core/Coop/Custom/FikaHealthBar.cs b/Fika.Core/Coop/Custom/FikaHealthBar.cs index 26dccb86..cb2dc73a 100644 --- a/Fika.Core/Coop/Custom/FikaHealthBar.cs +++ b/Fika.Core/Coop/Custom/FikaHealthBar.cs @@ -6,6 +6,7 @@ using EFT.UI; using Fika.Core.Bundles; using Fika.Core.Coop.Players; +using Fika.Core.Utils; using System; using UnityEngine; using UnityEngine.UI; @@ -98,9 +99,8 @@ private void UpdateScreenSpacePosition(bool throttleUpdate) float processedDistance = Mathf.Clamp(sqrDistance / 625, 0.6f, 1f); Vector3 position = new(currentPlayer.PlayerBones.Neck.position.x, currentPlayer.PlayerBones.Neck.position.y + (1f * processedDistance), currentPlayer.PlayerBones.Neck.position.z); - Vector3 screenPoint = camera.WorldToScreenPoint(position); - - if (screenPoint.z <= 0) + + if (!WorldToScreen.GetScreenPoint(position, mainPlayer, out Vector3 screenPoint)) { UpdateColorTextMeshProUGUI(playerPlate.playerNameScreen, 0); UpdateColorImage(playerPlate.healthBarScreen, 0); diff --git a/Fika.Core/Coop/Factories/PingFactory.cs b/Fika.Core/Coop/Factories/PingFactory.cs index 51851704..3a47c5b5 100644 --- a/Fika.Core/Coop/Factories/PingFactory.cs +++ b/Fika.Core/Coop/Factories/PingFactory.cs @@ -2,6 +2,7 @@ using EFT; using Fika.Core.Bundles; using Fika.Core.Coop.Players; +using Fika.Core.Utils; using UnityEngine; using UnityEngine.UI; using Object = System.Object; @@ -75,8 +76,6 @@ protected void Update() } } - Camera camera = CameraClass.Instance.Camera; - if (CameraClass.Instance.SSAA != null && CameraClass.Instance.SSAA.isActiveAndEnabled) { int outputWidth = CameraClass.Instance.SSAA.GetOutputWidth(); @@ -84,10 +83,9 @@ protected void Update() screenScale = outputWidth / inputWidth; } - Vector3 screenPoint = camera.WorldToScreenPoint(hitPoint); - if (screenPoint.z > 0) + if (WorldToScreen.GetScreenPoint(hitPoint, mainPlayer, out Vector3 screenPoint)) { - float distanceToCenter = Vector3.Distance(screenPoint, new Vector3(x: Screen.width, Screen.height, 0) / 2); + float distanceToCenter = Vector3.Distance(screenPoint, new Vector3(Screen.width, Screen.height, 0) / 2); if (distanceToCenter < 200) { diff --git a/Fika.Core/Utils/WorldToScreen.cs b/Fika.Core/Utils/WorldToScreen.cs new file mode 100644 index 00000000..d40b6b98 --- /dev/null +++ b/Fika.Core/Utils/WorldToScreen.cs @@ -0,0 +1,91 @@ +using EFT.Animations; +using EFT.CameraControl; +using EFT.InventoryLogic; +using Fika.Core.Coop.Players; +using UnityEngine; + +namespace Fika.Core.Utils +{ + public static class WorldToScreen + { + public static bool GetScreenPoint(Vector3 worldPosition, CoopPlayer mainPlayer, out Vector3 screenPoint) + { + CameraClass worldCameraInstance = CameraClass.Instance; + Camera worldCamera = worldCameraInstance.Camera; + + screenPoint = Vector3.zero; + + if (mainPlayer == null || worldCamera == null) + { + return false; + } + + ProceduralWeaponAnimation weaponAnimation = mainPlayer.ProceduralWeaponAnimation; + + if (weaponAnimation != null) + { + if (weaponAnimation.IsAiming && weaponAnimation.CurrentScope.IsOptic) + { + Camera opticCamera = worldCameraInstance.OpticCameraManager.Camera; + + if (GetScopeZoomLevel(weaponAnimation) > 1f) + { + Vector3 opticCenterScreenPosition = GetOpticCenterScreenPosition(weaponAnimation, worldCamera); + Vector3 opticCenterScreenOffset = opticCenterScreenPosition - (new Vector3(Screen.width, Screen.height, 0f) / 2); + + float opticScale = (Screen.height / opticCamera.scaledPixelHeight); + Vector3 opticCameraOffset = new Vector3((worldCamera.pixelWidth / 2 - opticCamera.pixelWidth / 2), (worldCamera.pixelHeight / 2 - opticCamera.pixelHeight / 2), 0); + Vector3 opticScreenPoint = (opticCamera.WorldToScreenPoint(worldPosition) + opticCameraOffset) * opticScale; + + if (opticScreenPoint.z > 0f) + { + screenPoint = opticScreenPoint + opticCenterScreenOffset; + } + } + } + } + + //not able to find a zoomed optic screen point + if (screenPoint == Vector3.zero) + { + screenPoint = worldCamera.WorldToScreenPoint(worldPosition); + } + + if (screenPoint.z > 0f) + { + return true; + } + + return false; + } + + private static float GetScopeZoomLevel(ProceduralWeaponAnimation weaponAnimation) + { + SightComponent weaponSight = weaponAnimation.CurrentAimingMod; + + if (weaponSight == null) + { + return 1f; + } + + return weaponSight.GetCurrentOpticZoom(); + } + + private static Vector3 GetOpticCenterScreenPosition(ProceduralWeaponAnimation weaponAnimation, Camera worldCamera) + { + if (weaponAnimation == null) + { + return Vector3.zero; + } + + OpticSight currentOptic = weaponAnimation.HandsContainer.Weapon.GetComponentInChildren(); + if (currentOptic == null) + { + return Vector3.zero; + } + + Transform lensTransform = currentOptic.LensRenderer.transform; + return worldCamera.WorldToScreenPoint(lensTransform.position); + } + } +}