Skip to content

Commit

Permalink
Merge pull request #52 from Outshynd/dev-fix-scope-fov
Browse files Browse the repository at this point in the history
Fix for WorldToScreen with zoomed scopes
  • Loading branch information
Lacyway authored May 20, 2024
2 parents 68e54b7 + b780b96 commit 720207d
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 8 deletions.
6 changes: 3 additions & 3 deletions Fika.Core/Coop/Custom/FikaHealthBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 3 additions & 5 deletions Fika.Core/Coop/Factories/PingFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,19 +76,16 @@ protected void Update()
}
}

Camera camera = CameraClass.Instance.Camera;

if (CameraClass.Instance.SSAA != null && CameraClass.Instance.SSAA.isActiveAndEnabled)
{
int outputWidth = CameraClass.Instance.SSAA.GetOutputWidth();
float inputWidth = CameraClass.Instance.SSAA.GetInputWidth();
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)
{
Expand Down
91 changes: 91 additions & 0 deletions Fika.Core/Utils/WorldToScreen.cs
Original file line number Diff line number Diff line change
@@ -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<OpticSight>();
if (currentOptic == null)
{
return Vector3.zero;
}

Transform lensTransform = currentOptic.LensRenderer.transform;
return worldCamera.WorldToScreenPoint(lensTransform.position);
}
}
}

0 comments on commit 720207d

Please sign in to comment.