[ThirdPerson] Fix for game issue, reset player camera culling mask to default on world load.

This commit is contained in:
NotAKidoS 2023-09-25 14:08:05 -05:00
parent afb44fc3a7
commit 53c369b950
4 changed files with 66 additions and 47 deletions

View file

@ -16,9 +16,9 @@ internal static class CameraLogic
{ {
private static float _dist; private static float _dist;
private static float _scale = 1f; private static float _scale = 1f;
private static Camera _ourCam; private static Camera _thirdpersonCam;
private static Camera _desktopCam; private static Camera _desktopCam;
private static int _desktopCamMask; private static int _storedCamMask;
private static CameraFovClone _cameraFovClone; private static CameraFovClone _cameraFovClone;
internal static CameraLocation CurrentLocation = CameraLocation.Default; internal static CameraLocation CurrentLocation = CameraLocation.Default;
@ -38,8 +38,9 @@ internal static class CameraLogic
set set
{ {
_state = value; _state = value;
_desktopCam.cullingMask = _state ? 0 : _desktopCamMask; if (_state) _storedCamMask = _desktopCam.cullingMask;
_ourCam.gameObject.SetActive(_state); _desktopCam.cullingMask = _state ? 0 : _storedCamMask;
_thirdpersonCam.gameObject.SetActive(_state);
} }
} }
@ -51,46 +52,61 @@ internal static class CameraLogic
{ {
yield return new WaitUntil(() => PlayerSetup.Instance); yield return new WaitUntil(() => PlayerSetup.Instance);
_ourCam = new GameObject("ThirdPersonCameraObj", typeof(Camera)).GetComponent<Camera>(); _thirdpersonCam = new GameObject("ThirdPersonCameraObj", typeof(Camera)).GetComponent<Camera>();
_cameraFovClone = _ourCam.gameObject.AddComponent<CameraFovClone>(); _cameraFovClone = _thirdpersonCam.gameObject.AddComponent<CameraFovClone>();
_desktopCam = PlayerSetup.Instance.desktopCamera.GetComponent<Camera>(); _desktopCam = PlayerSetup.Instance.desktopCamera.GetComponent<Camera>();
_desktopCamMask = _desktopCam.cullingMask;
_cameraFovClone.targetCamera = _desktopCam; _cameraFovClone.targetCamera = _desktopCam;
_ourCam.transform.SetParent(_desktopCam.transform); _thirdpersonCam.transform.SetParent(_desktopCam.transform);
RelocateCam(CameraLocation.Default); RelocateCam(CameraLocation.Default);
_ourCam.gameObject.SetActive(false); _thirdpersonCam.gameObject.SetActive(false);
ThirdPerson.Logger.Msg("Finished setting up third person camera."); ThirdPerson.Logger.Msg("Finished setting up third person camera.");
} }
internal static void ResetPlayerCamValues()
{
Camera activePlayerCam = PlayerSetup.Instance.GetActiveCamera().GetComponent<Camera>();
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() internal static void CopyPlayerCamValues()
{ {
Camera ourCamComponent = _ourCam.GetComponent<Camera>(); Camera ourCamComponent = _thirdpersonCam.GetComponent<Camera>();
Camera playerCamComponent = PlayerSetup.Instance.GetActiveCamera().GetComponent<Camera>(); Camera activePlayerCam = PlayerSetup.Instance.GetActiveCamera().GetComponent<Camera>();
if (ourCamComponent == null || playerCamComponent == null) return; if (ourCamComponent == null || activePlayerCam == null)
return;
ThirdPerson.Logger.Msg("Copying active camera settings & components."); ThirdPerson.Logger.Msg("Copying active camera settings & components.");
// Copy basic settings // Copy basic settings
ourCamComponent.farClipPlane = playerCamComponent.farClipPlane; ourCamComponent.farClipPlane = activePlayerCam.farClipPlane;
ourCamComponent.nearClipPlane = playerCamComponent.nearClipPlane; ourCamComponent.nearClipPlane = activePlayerCam.nearClipPlane;
ourCamComponent.depthTextureMode = playerCamComponent.depthTextureMode; ourCamComponent.depthTextureMode = activePlayerCam.depthTextureMode;
// We cant copy this because we set it to 0 // Copy and store the active camera mask
ourCamComponent.cullingMask &= -32769; var cullingMask= _storedCamMask = activePlayerCam.cullingMask;
ourCamComponent.cullingMask |= 256; cullingMask &= -32769;
ourCamComponent.cullingMask |= 512; cullingMask |= 256;
ourCamComponent.cullingMask |= 32; cullingMask |= 512;
ourCamComponent.cullingMask &= -4097; cullingMask |= 32;
ourCamComponent.cullingMask |= 1024; cullingMask &= -4097;
ourCamComponent.cullingMask |= 8192; cullingMask |= 1024;
cullingMask |= 8192;
ourCamComponent.cullingMask = cullingMask;
// Copy post processing if added // Copy post processing if added
PostProcessLayer ppLayerPlayerCam = playerCamComponent.GetComponent<PostProcessLayer>(); PostProcessLayer ppLayerPlayerCam = activePlayerCam.GetComponent<PostProcessLayer>();
PostProcessLayer ppLayerThirdPerson = ourCamComponent.AddComponentIfMissing<PostProcessLayer>(); PostProcessLayer ppLayerThirdPerson = ourCamComponent.AddComponentIfMissing<PostProcessLayer>();
if (ppLayerPlayerCam != null && ppLayerThirdPerson != null) if (ppLayerPlayerCam != null && ppLayerThirdPerson != null)
{ {
@ -108,7 +124,7 @@ internal static class CameraLogic
} }
// Copy Aura camera settings // Copy Aura camera settings
AuraCamera auraPlayerCam = playerCamComponent.GetComponent<AuraCamera>(); AuraCamera auraPlayerCam = activePlayerCam.GetComponent<AuraCamera>();
AuraCamera auraThirdPerson = ourCamComponent.AddComponentIfMissing<AuraCamera>(); AuraCamera auraThirdPerson = ourCamComponent.AddComponentIfMissing<AuraCamera>();
if (auraPlayerCam != null && auraThirdPerson != null) if (auraPlayerCam != null && auraThirdPerson != null)
{ {
@ -121,7 +137,7 @@ internal static class CameraLogic
} }
// Copy Flare layer settings // Copy Flare layer settings
FlareLayer flarePlayerCam = playerCamComponent.GetComponent<FlareLayer>(); FlareLayer flarePlayerCam = activePlayerCam.GetComponent<FlareLayer>();
FlareLayer flareThirdPerson = ourCamComponent.AddComponentIfMissing<FlareLayer>(); FlareLayer flareThirdPerson = ourCamComponent.AddComponentIfMissing<FlareLayer>();
if (flarePlayerCam != null && flareThirdPerson != null) if (flarePlayerCam != null && flareThirdPerson != null)
{ {
@ -133,7 +149,7 @@ internal static class CameraLogic
} }
// Copy Azure Fog Scattering settings // Copy Azure Fog Scattering settings
AzureFogScattering azureFogPlayerCam = playerCamComponent.GetComponent<AzureFogScattering>(); AzureFogScattering azureFogPlayerCam = activePlayerCam.GetComponent<AzureFogScattering>();
AzureFogScattering azureFogThirdPerson = ourCamComponent.AddComponentIfMissing<AzureFogScattering>(); AzureFogScattering azureFogThirdPerson = ourCamComponent.AddComponentIfMissing<AzureFogScattering>();
if (azureFogPlayerCam != null && azureFogThirdPerson != null) if (azureFogPlayerCam != null && azureFogThirdPerson != null)
{ {
@ -145,7 +161,7 @@ internal static class CameraLogic
} }
// Copy Beautify settings // Copy Beautify settings
Beautify beautifyPlayerCam = playerCamComponent.GetComponent<Beautify>(); Beautify beautifyPlayerCam = activePlayerCam.GetComponent<Beautify>();
Beautify beautifyThirdPerson = ourCamComponent.AddComponentIfMissing<Beautify>(); Beautify beautifyThirdPerson = ourCamComponent.AddComponentIfMissing<Beautify>();
if (beautifyPlayerCam != null && beautifyThirdPerson != null) if (beautifyPlayerCam != null && beautifyThirdPerson != null)
{ {
@ -160,29 +176,29 @@ internal static class CameraLogic
internal static void RelocateCam(CameraLocation location, bool resetDist = false) internal static void RelocateCam(CameraLocation location, bool resetDist = false)
{ {
_ourCam.transform.rotation = _desktopCam.transform.rotation; _thirdpersonCam.transform.rotation = _desktopCam.transform.rotation;
if (resetDist) ResetDist(); if (resetDist) ResetDist();
switch (location) switch (location)
{ {
case CameraLocation.FrontView: case CameraLocation.FrontView:
_ourCam.transform.localPosition = new Vector3(0, 0.015f, 1f - _dist) * _scale; _thirdpersonCam.transform.localPosition = new Vector3(0, 0.015f, 1f - _dist) * _scale;
_ourCam.transform.localRotation = new Quaternion(0, 180, 0, 0); _thirdpersonCam.transform.localRotation = new Quaternion(0, 180, 0, 0);
CurrentLocation = CameraLocation.FrontView; CurrentLocation = CameraLocation.FrontView;
break; break;
case CameraLocation.RightSide: case CameraLocation.RightSide:
_ourCam.transform.localPosition = new Vector3(0.3f, 0.015f, -1.5f + _dist) * _scale; _thirdpersonCam.transform.localPosition = new Vector3(0.3f, 0.015f, -1.5f + _dist) * _scale;
_ourCam.transform.localRotation = new Quaternion(0, 0, 0, 0); _thirdpersonCam.transform.localRotation = new Quaternion(0, 0, 0, 0);
CurrentLocation = CameraLocation.RightSide; CurrentLocation = CameraLocation.RightSide;
break; break;
case CameraLocation.LeftSide: case CameraLocation.LeftSide:
_ourCam.transform.localPosition = new Vector3(-0.3f, 0.015f, -1.5f + _dist) * _scale; _thirdpersonCam.transform.localPosition = new Vector3(-0.3f, 0.015f, -1.5f + _dist) * _scale;
_ourCam.transform.localRotation = new Quaternion(0, 0, 0, 0); _thirdpersonCam.transform.localRotation = new Quaternion(0, 0, 0, 0);
CurrentLocation = CameraLocation.LeftSide; CurrentLocation = CameraLocation.LeftSide;
break; break;
case CameraLocation.Default: case CameraLocation.Default:
default: default:
_ourCam.transform.localPosition = new Vector3(0, 0.015f, -1.5f + _dist) * _scale; _thirdpersonCam.transform.localPosition = new Vector3(0, 0.015f, -1.5f + _dist) * _scale;
_ourCam.transform.localRotation = new Quaternion(0, 0, 0, 0); _thirdpersonCam.transform.localRotation = new Quaternion(0, 0, 0, 0);
CurrentLocation = CameraLocation.Default; CurrentLocation = CameraLocation.Default;
break; break;
} }

View file

@ -13,11 +13,12 @@ internal static class Patches
{ {
harmony.Patch( harmony.Patch(
typeof(CVRWorld).GetMethod(nameof(CVRWorld.SetDefaultCamValues), BindingFlags.NonPublic | BindingFlags.Instance), 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( harmony.Patch(
typeof(CVRWorld).GetMethod(nameof(CVRWorld.CopyRefCamValues), BindingFlags.NonPublic | BindingFlags.Instance), 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( harmony.Patch(
typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.SetupIKScaling), BindingFlags.NonPublic | BindingFlags.Instance), typeof(PlayerSetup).GetMethod(nameof(PlayerSetup.SetupIKScaling), BindingFlags.NonPublic | BindingFlags.Instance),
@ -30,7 +31,8 @@ internal static class Patches
} }
//Copy camera settings & postprocessing components //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 //Adjust camera distance with height as modifier
private static void OnScaleAdjusted(float height) => AdjustScale(height); private static void OnScaleAdjusted(float height) => AdjustScale(height);
private static void OnConfigureHudAffinity() => CheckVRMode(); private static void OnConfigureHudAffinity() => CheckVRMode();

View file

@ -27,6 +27,6 @@ using System.Reflection;
namespace NAK.ThirdPerson.Properties; namespace NAK.ThirdPerson.Properties;
internal static class AssemblyInfoParams internal static class AssemblyInfoParams
{ {
public const string Version = "1.0.4"; public const string Version = "1.0.5";
public const string Author = "Davi & NotAKidoS"; public const string Author = "Davi & NotAKidoS";
} }

View file

@ -22,13 +22,14 @@ public class ThirdPerson : MelonMod
if (!Input.GetKey(KeyCode.LeftControl)) if (!Input.GetKey(KeyCode.LeftControl))
{ {
// Prevents scrolling while in Menus or UnityExplorer // Prevents scrolling while in Menus or UnityExplorer
if (State && Cursor.lockState == CursorLockMode.Locked) if (!State || Cursor.lockState != CursorLockMode.Locked)
{ return;
float scroll = Input.GetAxis("Mouse ScrollWheel");
if (scroll != 0f) ScrollDist(Mathf.Sign(scroll)); float scroll = Input.GetAxis("Mouse ScrollWheel");
} if (scroll != 0f) ScrollDist(Mathf.Sign(scroll));
return; return;
} }
if (Input.GetKeyDown(KeyCode.T)) State = !State; if (Input.GetKeyDown(KeyCode.T)) State = !State;
if (!State || !Input.GetKeyDown(KeyCode.Y)) return; 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); RelocateCam((CameraLocation)(((int)CurrentLocation + (Input.GetKey(KeyCode.LeftShift) ? -1 : 1) + Enum.GetValues(typeof(CameraLocation)).Length) % Enum.GetValues(typeof(CameraLocation)).Length), true);