diff --git a/ThirdPerson/CameraLogic.cs b/ThirdPerson/CameraLogic.cs index 0948a9d..314755b 100644 --- a/ThirdPerson/CameraLogic.cs +++ b/ThirdPerson/CameraLogic.cs @@ -16,9 +16,9 @@ internal static class CameraLogic { private static float _dist; private static float _scale = 1f; - private static Camera _ourCam; + private static Camera _thirdpersonCam; private static Camera _desktopCam; - private static int _desktopCamMask; + private static int _storedCamMask; private static CameraFovClone _cameraFovClone; internal static CameraLocation CurrentLocation = CameraLocation.Default; @@ -38,8 +38,9 @@ internal static class CameraLogic set { _state = value; - _desktopCam.cullingMask = _state ? 0 : _desktopCamMask; - _ourCam.gameObject.SetActive(_state); + if (_state) _storedCamMask = _desktopCam.cullingMask; + _desktopCam.cullingMask = _state ? 0 : _storedCamMask; + _thirdpersonCam.gameObject.SetActive(_state); } } @@ -51,46 +52,61 @@ internal static class CameraLogic { yield return new WaitUntil(() => PlayerSetup.Instance); - _ourCam = new GameObject("ThirdPersonCameraObj", typeof(Camera)).GetComponent(); + _thirdpersonCam = new GameObject("ThirdPersonCameraObj", typeof(Camera)).GetComponent(); - _cameraFovClone = _ourCam.gameObject.AddComponent(); + _cameraFovClone = _thirdpersonCam.gameObject.AddComponent(); _desktopCam = PlayerSetup.Instance.desktopCamera.GetComponent(); - _desktopCamMask = _desktopCam.cullingMask; _cameraFovClone.targetCamera = _desktopCam; - _ourCam.transform.SetParent(_desktopCam.transform); + _thirdpersonCam.transform.SetParent(_desktopCam.transform); RelocateCam(CameraLocation.Default); - _ourCam.gameObject.SetActive(false); + _thirdpersonCam.gameObject.SetActive(false); ThirdPerson.Logger.Msg("Finished setting up third person camera."); } + internal static void ResetPlayerCamValues() + { + Camera activePlayerCam = PlayerSetup.Instance.GetActiveCamera().GetComponent(); + if (activePlayerCam == null) + return; + + ThirdPerson.Logger.Msg("Resetting active camera culling mask."); + + // CopyRefCamValues does not reset to default before copying! Game issue, SetDefaultCamValues is fine. + activePlayerCam.cullingMask = MetaPort.Instance.defaultCameraMask; + } + internal static void CopyPlayerCamValues() { - Camera ourCamComponent = _ourCam.GetComponent(); - Camera playerCamComponent = PlayerSetup.Instance.GetActiveCamera().GetComponent(); - if (ourCamComponent == null || playerCamComponent == null) return; + Camera ourCamComponent = _thirdpersonCam.GetComponent(); + Camera activePlayerCam = PlayerSetup.Instance.GetActiveCamera().GetComponent(); + if (ourCamComponent == null || activePlayerCam == null) + return; + ThirdPerson.Logger.Msg("Copying active camera settings & components."); - + // Copy basic settings - ourCamComponent.farClipPlane = playerCamComponent.farClipPlane; - ourCamComponent.nearClipPlane = playerCamComponent.nearClipPlane; - ourCamComponent.depthTextureMode = playerCamComponent.depthTextureMode; + ourCamComponent.farClipPlane = activePlayerCam.farClipPlane; + ourCamComponent.nearClipPlane = activePlayerCam.nearClipPlane; + ourCamComponent.depthTextureMode = activePlayerCam.depthTextureMode; - // We cant copy this because we set it to 0 - ourCamComponent.cullingMask &= -32769; - ourCamComponent.cullingMask |= 256; - ourCamComponent.cullingMask |= 512; - ourCamComponent.cullingMask |= 32; - ourCamComponent.cullingMask &= -4097; - ourCamComponent.cullingMask |= 1024; - ourCamComponent.cullingMask |= 8192; + // Copy and store the active camera mask + var cullingMask= _storedCamMask = activePlayerCam.cullingMask; + cullingMask &= -32769; + cullingMask |= 256; + cullingMask |= 512; + cullingMask |= 32; + cullingMask &= -4097; + cullingMask |= 1024; + cullingMask |= 8192; + ourCamComponent.cullingMask = cullingMask; // Copy post processing if added - PostProcessLayer ppLayerPlayerCam = playerCamComponent.GetComponent(); + PostProcessLayer ppLayerPlayerCam = activePlayerCam.GetComponent(); PostProcessLayer ppLayerThirdPerson = ourCamComponent.AddComponentIfMissing(); if (ppLayerPlayerCam != null && ppLayerThirdPerson != null) { @@ -108,7 +124,7 @@ internal static class CameraLogic } // Copy Aura camera settings - AuraCamera auraPlayerCam = playerCamComponent.GetComponent(); + AuraCamera auraPlayerCam = activePlayerCam.GetComponent(); AuraCamera auraThirdPerson = ourCamComponent.AddComponentIfMissing(); if (auraPlayerCam != null && auraThirdPerson != null) { @@ -121,7 +137,7 @@ internal static class CameraLogic } // Copy Flare layer settings - FlareLayer flarePlayerCam = playerCamComponent.GetComponent(); + FlareLayer flarePlayerCam = activePlayerCam.GetComponent(); FlareLayer flareThirdPerson = ourCamComponent.AddComponentIfMissing(); if (flarePlayerCam != null && flareThirdPerson != null) { @@ -133,7 +149,7 @@ internal static class CameraLogic } // Copy Azure Fog Scattering settings - AzureFogScattering azureFogPlayerCam = playerCamComponent.GetComponent(); + AzureFogScattering azureFogPlayerCam = activePlayerCam.GetComponent(); AzureFogScattering azureFogThirdPerson = ourCamComponent.AddComponentIfMissing(); if (azureFogPlayerCam != null && azureFogThirdPerson != null) { @@ -145,7 +161,7 @@ internal static class CameraLogic } // Copy Beautify settings - Beautify beautifyPlayerCam = playerCamComponent.GetComponent(); + Beautify beautifyPlayerCam = activePlayerCam.GetComponent(); Beautify beautifyThirdPerson = ourCamComponent.AddComponentIfMissing(); if (beautifyPlayerCam != null && beautifyThirdPerson != null) { @@ -160,29 +176,29 @@ internal static class CameraLogic internal static void RelocateCam(CameraLocation location, bool resetDist = false) { - _ourCam.transform.rotation = _desktopCam.transform.rotation; + _thirdpersonCam.transform.rotation = _desktopCam.transform.rotation; if (resetDist) ResetDist(); switch (location) { case CameraLocation.FrontView: - _ourCam.transform.localPosition = new Vector3(0, 0.015f, 1f - _dist) * _scale; - _ourCam.transform.localRotation = new Quaternion(0, 180, 0, 0); + _thirdpersonCam.transform.localPosition = new Vector3(0, 0.015f, 1f - _dist) * _scale; + _thirdpersonCam.transform.localRotation = new Quaternion(0, 180, 0, 0); CurrentLocation = CameraLocation.FrontView; break; case CameraLocation.RightSide: - _ourCam.transform.localPosition = new Vector3(0.3f, 0.015f, -1.5f + _dist) * _scale; - _ourCam.transform.localRotation = new Quaternion(0, 0, 0, 0); + _thirdpersonCam.transform.localPosition = new Vector3(0.3f, 0.015f, -1.5f + _dist) * _scale; + _thirdpersonCam.transform.localRotation = new Quaternion(0, 0, 0, 0); CurrentLocation = CameraLocation.RightSide; break; case CameraLocation.LeftSide: - _ourCam.transform.localPosition = new Vector3(-0.3f, 0.015f, -1.5f + _dist) * _scale; - _ourCam.transform.localRotation = new Quaternion(0, 0, 0, 0); + _thirdpersonCam.transform.localPosition = new Vector3(-0.3f, 0.015f, -1.5f + _dist) * _scale; + _thirdpersonCam.transform.localRotation = new Quaternion(0, 0, 0, 0); CurrentLocation = CameraLocation.LeftSide; break; case CameraLocation.Default: default: - _ourCam.transform.localPosition = new Vector3(0, 0.015f, -1.5f + _dist) * _scale; - _ourCam.transform.localRotation = new Quaternion(0, 0, 0, 0); + _thirdpersonCam.transform.localPosition = new Vector3(0, 0.015f, -1.5f + _dist) * _scale; + _thirdpersonCam.transform.localRotation = new Quaternion(0, 0, 0, 0); CurrentLocation = CameraLocation.Default; break; } diff --git a/ThirdPerson/Patches.cs b/ThirdPerson/Patches.cs index 6c176a3..1df9dd6 100644 --- a/ThirdPerson/Patches.cs +++ b/ThirdPerson/Patches.cs @@ -13,11 +13,12 @@ internal static class Patches { harmony.Patch( typeof(CVRWorld).GetMethod(nameof(CVRWorld.SetDefaultCamValues), BindingFlags.NonPublic | BindingFlags.Instance), - postfix: typeof(Patches).GetMethod(nameof(OnWorldStart), BindingFlags.NonPublic | BindingFlags.Static).ToNewHarmonyMethod() + postfix: typeof(Patches).GetMethod(nameof(OnPostWorldStart), BindingFlags.NonPublic | BindingFlags.Static).ToNewHarmonyMethod() ); harmony.Patch( typeof(CVRWorld).GetMethod(nameof(CVRWorld.CopyRefCamValues), BindingFlags.NonPublic | BindingFlags.Instance), - postfix: typeof(Patches).GetMethod(nameof(OnWorldStart), BindingFlags.NonPublic | BindingFlags.Static).ToNewHarmonyMethod() + prefix: typeof(Patches).GetMethod(nameof(OnPreWorldStart), BindingFlags.NonPublic | BindingFlags.Static).ToNewHarmonyMethod(), + postfix: typeof(Patches).GetMethod(nameof(OnPostWorldStart), BindingFlags.NonPublic | BindingFlags.Static).ToNewHarmonyMethod() ); harmony.Patch( typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.SetupIKScaling), BindingFlags.NonPublic | BindingFlags.Instance), @@ -30,7 +31,8 @@ internal static class Patches } //Copy camera settings & postprocessing components - private static void OnWorldStart() => CopyPlayerCamValues(); + private static void OnPreWorldStart() => ResetPlayerCamValues(); + private static void OnPostWorldStart() => CopyPlayerCamValues(); //Adjust camera distance with height as modifier private static void OnScaleAdjusted(float height) => AdjustScale(height); private static void OnConfigureHudAffinity() => CheckVRMode(); diff --git a/ThirdPerson/Properties/AssemblyInfo.cs b/ThirdPerson/Properties/AssemblyInfo.cs index c436336..dcd28b7 100644 --- a/ThirdPerson/Properties/AssemblyInfo.cs +++ b/ThirdPerson/Properties/AssemblyInfo.cs @@ -27,6 +27,6 @@ using System.Reflection; namespace NAK.ThirdPerson.Properties; internal static class AssemblyInfoParams { - public const string Version = "1.0.4"; + public const string Version = "1.0.5"; public const string Author = "Davi & NotAKidoS"; } \ No newline at end of file diff --git a/ThirdPerson/ThirdPerson.cs b/ThirdPerson/ThirdPerson.cs index 8c8917d..428c5ab 100644 --- a/ThirdPerson/ThirdPerson.cs +++ b/ThirdPerson/ThirdPerson.cs @@ -22,13 +22,14 @@ public class ThirdPerson : MelonMod if (!Input.GetKey(KeyCode.LeftControl)) { // Prevents scrolling while in Menus or UnityExplorer - if (State && Cursor.lockState == CursorLockMode.Locked) - { - float scroll = Input.GetAxis("Mouse ScrollWheel"); - if (scroll != 0f) ScrollDist(Mathf.Sign(scroll)); - } + if (!State || Cursor.lockState != CursorLockMode.Locked) + return; + + float scroll = Input.GetAxis("Mouse ScrollWheel"); + if (scroll != 0f) ScrollDist(Mathf.Sign(scroll)); return; } + if (Input.GetKeyDown(KeyCode.T)) State = !State; if (!State || !Input.GetKeyDown(KeyCode.Y)) return; RelocateCam((CameraLocation)(((int)CurrentLocation + (Input.GetKey(KeyCode.LeftShift) ? -1 : 1) + Enum.GetValues(typeof(CameraLocation)).Length) % Enum.GetValues(typeof(CameraLocation)).Length), true);