[DesktopVRSwitch] Wait frame on MovementSystem OnPostSwitch.

testing
This commit is contained in:
NotAKidoS 2023-06-20 06:05:51 -05:00
parent 0fb7c3d401
commit 52d4ef3279
8 changed files with 123 additions and 48 deletions

View file

@ -3,11 +3,11 @@ using ABI_RC.Core.Savior;
using ABI_RC.Core.Util.Object_Behaviour; using ABI_RC.Core.Util.Object_Behaviour;
using ABI_RC.Systems.IK; using ABI_RC.Systems.IK;
using ABI_RC.Systems.IK.TrackingModules; using ABI_RC.Systems.IK.TrackingModules;
using cohtml;
using HarmonyLib; using HarmonyLib;
using NAK.DesktopVRSwitch.Patches; using NAK.DesktopVRSwitch.Patches;
using NAK.DesktopVRSwitch.VRModeTrackers; using NAK.DesktopVRSwitch.VRModeTrackers;
using UnityEngine; using UnityEngine;
using cohtml;
namespace NAK.DesktopVRSwitch.HarmonyPatches; namespace NAK.DesktopVRSwitch.HarmonyPatches;
@ -75,9 +75,9 @@ class CVRPickupObjectPatches
[HarmonyPatch(typeof(CVRPickupObject), nameof(CVRPickupObject.Start))] [HarmonyPatch(typeof(CVRPickupObject), nameof(CVRPickupObject.Start))]
static void Prefix_CVRPickupObject_Start(ref CVRPickupObject __instance) static void Prefix_CVRPickupObject_Start(ref CVRPickupObject __instance)
{ {
if (__instance.gripType == CVRPickupObject.GripType.Free) if (__instance.gripType == CVRPickupObject.GripType.Free)
return; return;
Transform vrOrigin = __instance.gripOrigin; Transform vrOrigin = __instance.gripOrigin;
Transform desktopOrigin = __instance.gripOrigin.Find("[Desktop]"); Transform desktopOrigin = __instance.gripOrigin.Find("[Desktop]");
if (vrOrigin != null && desktopOrigin != null) if (vrOrigin != null && desktopOrigin != null)

View file

@ -58,7 +58,7 @@ public class DesktopVRSwitch : MelonMod
{ {
if (Input.GetKeyDown(KeyCode.F6) && Input.GetKey(KeyCode.LeftControl)) if (Input.GetKeyDown(KeyCode.F6) && Input.GetKey(KeyCode.LeftControl))
{ {
VRModeSwitchManager.Instance?.StartSwitch(); VRModeSwitchManager.Instance?.AttemptSwitch();
} }
} }

View file

@ -1,7 +1,6 @@
using ABI_RC.Core.EventSystem; using ABI_RC.Core.EventSystem;
using ABI_RC.Core.Player; using ABI_RC.Core.Player;
using ABI_RC.Core.Savior; using ABI_RC.Core.Savior;
using ABI_RC.Systems.MovementSystem;
using UnityEngine; using UnityEngine;
namespace NAK.DesktopVRSwitch; namespace NAK.DesktopVRSwitch;
@ -17,13 +16,10 @@ internal static class Utils
return PlayerSetup.Instance.desktopCamera; return PlayerSetup.Instance.desktopCamera;
} }
//stole from kafe :> internal static void ClearLocalAvatar()
internal static Vector3 GetPlayerRootPosition()
{ {
return MovementSystem.Instance.rotationPivot.position with DesktopVRSwitch.Logger.Msg("Clearing local avatar.");
{ PlayerSetup.Instance.ClearAvatar();
y = MovementSystem.Instance.transform.position.y
};
} }
internal static void ReloadLocalAvatar() internal static void ReloadLocalAvatar()
@ -31,4 +27,10 @@ internal static class Utils
DesktopVRSwitch.Logger.Msg("Attempting to reload current local avatar from GUID."); DesktopVRSwitch.Logger.Msg("Attempting to reload current local avatar from GUID.");
AssetManagement.Instance.LoadLocalAvatar(MetaPort.Instance.currentAvatarGuid); AssetManagement.Instance.LoadLocalAvatar(MetaPort.Instance.currentAvatarGuid);
} }
internal static bool IsLocalAvatarLoaded()
{
return PlayerSetup.Instance._avatar != null;
}
} }

View file

@ -0,0 +1,39 @@
using System.Collections;
using UnityEngine;
namespace NAK.DesktopVRSwitch;
class VRModeSwitchDebugger : MonoBehaviour
{
private Coroutine _switchCoroutine;
private WaitForSeconds _sleep = new WaitForSeconds(2.5f);
private void OnEnable()
{
if (_switchCoroutine == null)
_switchCoroutine = StartCoroutine(SwitchLoop());
}
private void OnDisable()
{
if (_switchCoroutine != null)
{
StopCoroutine(_switchCoroutine);
_switchCoroutine = null;
}
}
private IEnumerator SwitchLoop()
{
while (true)
{
if (!VRModeSwitchManager.Instance.SwitchInProgress)
{
VRModeSwitchManager.Instance.AttemptSwitch();
yield return _sleep;
}
yield return null;
}
}
}

View file

@ -44,11 +44,11 @@ public class VRModeSwitchManager : MonoBehaviour
} }
// Settings // Settings
private bool _useWorldTransition = true; public bool _useWorldTransition = true;
private bool _reloadLocalAvatar = true; public bool _reloadLocalAvatar = true;
// Internal // Info
private bool _switchInProgress = false; public bool SwitchInProgress { get; private set; }
void Awake() void Awake()
{ {
@ -60,18 +60,18 @@ public class VRModeSwitchManager : MonoBehaviour
Instance = this; Instance = this;
} }
public void StartSwitch() public void AttemptSwitch()
{ {
StartCoroutine(StartSwitchCoroutine()); StartCoroutine(StartSwitchCoroutine());
} }
private IEnumerator StartSwitchCoroutine() private IEnumerator StartSwitchCoroutine()
{ {
if (_switchInProgress) if (SwitchInProgress)
{ {
yield break; yield break;
} }
_switchInProgress = true; SwitchInProgress = true;
yield return null; yield return null;
@ -96,12 +96,14 @@ public class VRModeSwitchManager : MonoBehaviour
// Check for updated VR mode // Check for updated VR mode
if (isUsingVr != IsInVR()) if (isUsingVr != IsInVR())
{ {
InvokeOnPostSwitch(!isUsingVr);
// reload the local avatar // reload the local avatar
// only reload on success
if (_reloadLocalAvatar) if (_reloadLocalAvatar)
{
Utils.ClearLocalAvatar();
Utils.ReloadLocalAvatar(); Utils.ReloadLocalAvatar();
}
InvokeOnPostSwitch(!isUsingVr);
} }
else else
{ {
@ -111,7 +113,7 @@ public class VRModeSwitchManager : MonoBehaviour
if (_useWorldTransition) // finish the visual transition and wait if (_useWorldTransition) // finish the visual transition and wait
yield return WorldTransitionSystem.Instance.ContinueTransitionCoroutine(); yield return WorldTransitionSystem.Instance.ContinueTransitionCoroutine();
_switchInProgress = false; SwitchInProgress = false;
yield break; yield break;
} }
@ -166,14 +168,14 @@ public class VRModeSwitchManager : MonoBehaviour
SteamVR_ActionSet_Manager.DisableAllActionSets(); SteamVR_ActionSet_Manager.DisableAllActionSets();
SteamVR_Input.initialized = false; SteamVR_Input.initialized = false;
// Remove SteamVR // Remove SteamVR behaviour & render
DestroyImmediate(SteamVR_Behaviour.instance.gameObject); DestroyImmediate(SteamVR_Behaviour.instance.gameObject);
SteamVR.enabled = false; SteamVR.enabled = false; // disposes SteamVR
// Disable UnityXR // Disable UnityXR
XRSettings.LoadDeviceByName(""); XRSettings.LoadDeviceByName("");
XRSettings.enabled = false; XRSettings.enabled = false;
// We don't really need to wait on Stop() // We don't really need to wait a frame on Stop()
} }
} }

View file

@ -7,8 +7,6 @@ namespace NAK.DesktopVRSwitch.VRModeTrackers;
public class MetaPortTracker : VRModeTracker public class MetaPortTracker : VRModeTracker
{ {
private MetaPort _metaPort;
public override void TrackerInit() public override void TrackerInit()
{ {
VRModeSwitchManager.OnPostVRModeSwitch += OnPostSwitch; VRModeSwitchManager.OnPostVRModeSwitch += OnPostSwitch;
@ -21,7 +19,7 @@ public class MetaPortTracker : VRModeTracker
private void OnPostSwitch(bool intoVR) private void OnPostSwitch(bool intoVR)
{ {
_metaPort = MetaPort.Instance; MetaPort _metaPort = MetaPort.Instance;
if (_metaPort == null) if (_metaPort == null)
{ {
DesktopVRSwitch.Logger.Error("Error while getting MetaPort!"); DesktopVRSwitch.Logger.Error("Error while getting MetaPort!");
@ -33,11 +31,11 @@ public class MetaPortTracker : VRModeTracker
_metaPort.isUsingVr = intoVR; _metaPort.isUsingVr = intoVR;
// replace // replace
UpdateRichPresence(); UpdateRichPresence(_metaPort);
ResetSteamVROverrides(intoVR); ResetSteamVROverrides(intoVR);
} }
private void UpdateRichPresence() private void UpdateRichPresence(MetaPort _metaPort)
{ {
// Hacky way of updating rich presence // Hacky way of updating rich presence
if (_metaPort.settings.GetSettingsBool("ImplementationRichPresenceDiscordEnabled", true)) if (_metaPort.settings.GetSettingsBool("ImplementationRichPresenceDiscordEnabled", true))

View file

@ -1,12 +1,11 @@
using ABI_RC.Systems.MovementSystem; using ABI_RC.Systems.MovementSystem;
using System.Collections;
using UnityEngine; using UnityEngine;
namespace NAK.DesktopVRSwitch.VRModeTrackers; namespace NAK.DesktopVRSwitch.VRModeTrackers;
public class MovementSystemTracker : VRModeTracker public class MovementSystemTracker : VRModeTracker
{ {
private MovementSystem _movementSystem;
private Vector3 preSwitchWorldPosition; private Vector3 preSwitchWorldPosition;
private Quaternion preSwitchWorldRotation; private Quaternion preSwitchWorldRotation;
@ -24,33 +23,68 @@ public class MovementSystemTracker : VRModeTracker
VRModeSwitchManager.OnPostVRModeSwitch -= OnPostSwitch; VRModeSwitchManager.OnPostVRModeSwitch -= OnPostSwitch;
} }
private MovementSystem GetMovementSystemInstance()
{
MovementSystem _movementSystem = MovementSystem.Instance;
if (_movementSystem == null)
{
DesktopVRSwitch.Logger.Error("Error while getting MovementSystem!");
}
return _movementSystem;
}
private void OnPreSwitch(bool intoVR) private void OnPreSwitch(bool intoVR)
{ {
_movementSystem = MovementSystem.Instance; MovementSystem _movementSystem = GetMovementSystemInstance();
if (_movementSystem != null)
{
DesktopVRSwitch.Logger.Msg("Storing player world position and rotation.");
preSwitchWorldPosition = _movementSystem.rotationPivot.transform.position;
preSwitchWorldPosition.y = _movementSystem.transform.position.y;
preSwitchWorldRotation = _movementSystem.rotationPivot.transform.rotation;
preSwitchWorldPosition = Utils.GetPlayerRootPosition(); _movementSystem.ChangeCrouch(false);
preSwitchWorldRotation = _movementSystem.rotationPivot.transform.rotation; _movementSystem.ChangeProne(false);
_movementSystem.SetImmobilized(true);
_movementSystem.SetImmobilized(true); }
_movementSystem.ChangeCrouch(false);
_movementSystem.ChangeProne(false);
} }
private void OnFailedSwitch(bool intoVR) private void OnFailedSwitch(bool intoVR)
{ {
_movementSystem.SetImmobilized(false); MovementSystem _movementSystem = GetMovementSystemInstance();
if (_movementSystem != null)
{
DesktopVRSwitch.Logger.Msg("Resetting MovementSystem mobility.");
_movementSystem.SetImmobilized(false);
}
} }
private void OnPostSwitch(bool intoVR) private void OnPostSwitch(bool intoVR)
{ {
_movementSystem.rotationPivot = Utils.GetPlayerCameraObject(intoVR).transform; // Lazy
_movementSystem.TeleportToPosRot(preSwitchWorldPosition, preSwitchWorldRotation, false); MelonLoader.MelonCoroutines.Start(TeleportFrameAfter(intoVR));
}
if (!intoVR) private IEnumerator TeleportFrameAfter(bool intoVR)
_movementSystem.UpdateColliderCenter(_movementSystem.transform.position); {
yield return null; // need to wait a frame
_movementSystem.SetImmobilized(false); MovementSystem _movementSystem = GetMovementSystemInstance();
_movementSystem.ChangeCrouch(false); if (_movementSystem != null)
_movementSystem.ChangeProne(false); {
DesktopVRSwitch.Logger.Msg("Resetting MovementSystem mobility and applying stored position and rotation.");
_movementSystem.rotationPivot = Utils.GetPlayerCameraObject(intoVR).transform;
_movementSystem.TeleportToPosRot(preSwitchWorldPosition, preSwitchWorldRotation, false);
if (!intoVR)
_movementSystem.UpdateColliderCenter(_movementSystem.transform.position);
_movementSystem.ChangeCrouch(false);
_movementSystem.ChangeProne(false);
_movementSystem.SetImmobilized(false);
}
yield break;
} }
} }

View file

@ -34,7 +34,7 @@ public class PlayerSetupTracker : VRModeTracker
CVR_DesktopCameraController._cam = _playerSetup.desktopCamera.GetComponent<UnityEngine.Camera>(); CVR_DesktopCameraController._cam = _playerSetup.desktopCamera.GetComponent<UnityEngine.Camera>();
CVR_DesktopCameraController.UpdateFov(); CVR_DesktopCameraController.UpdateFov();
// UICamera has a script that copies the FOV from the desktop cam. // UICamera has a script that copies the FOV from the desktop cam.
// Toggling the cameras on/off resets the aspect ratio, // Toggling the cameras on/off resets the aspect ratio,
// so when rigs switch, that is already handled. // so when rigs switch, that is already handled.