mirror of
https://github.com/NotAKidoS/NAK_CVR_Mods.git
synced 2025-09-03 14:59:23 +00:00
[DesktopVRSwitch] Reworked SteamVR Initialization
New SteamVR initialization to avoid warnings/errors on switch.
This commit is contained in:
parent
61a45f97bc
commit
5f95755ad2
8 changed files with 39 additions and 306 deletions
|
@ -1,201 +0,0 @@
|
|||
/**
|
||||
|
||||
using NAK.DesktopVRSwitch.Patches;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.XR;
|
||||
using Valve.VR;
|
||||
using ABI_RC.Core.Savior;
|
||||
using ABI_RC.Core;
|
||||
|
||||
/**
|
||||
|
||||
SteamVR overrides:
|
||||
|
||||
Application.targetFrameRate = -1;
|
||||
Application.runInBackground = true;
|
||||
QualitySettings.maxQueuedFrames = -1;
|
||||
QualitySettings.vSyncCount = 0;
|
||||
Time.fixedDeltaTime = Time.timeScale / hmd_DisplayFrequency;
|
||||
|
||||
**
|
||||
|
||||
namespace NAK.DesktopVRSwitch;
|
||||
|
||||
public class DesktopVRSwitcher : MonoBehaviour
|
||||
{
|
||||
//Debug Settings
|
||||
public bool _reloadLocalAvatar = true;
|
||||
public bool _softVRSwitch = false;
|
||||
|
||||
//Internal Stuff
|
||||
private bool _switchInProgress = false;
|
||||
|
||||
void Start()
|
||||
{
|
||||
//do not pause game, this breaks dynbones & trackers
|
||||
SteamVR_Settings.instance.pauseGameWhenDashboardVisible = false;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.F6) && Input.GetKey(KeyCode.LeftControl))
|
||||
{
|
||||
SwitchVRMode();
|
||||
}
|
||||
}
|
||||
|
||||
public void SwitchVRMode()
|
||||
{
|
||||
if (_switchInProgress) return;
|
||||
if (!IsInVR())
|
||||
{
|
||||
StartCoroutine(StartVRSystem());
|
||||
}
|
||||
else
|
||||
{
|
||||
StartCoroutine(StopVR());
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsInVR() => XRSettings.enabled;
|
||||
|
||||
private IEnumerator StartVRSystem()
|
||||
{
|
||||
PreVRModeSwitch(true);
|
||||
XRSettings.LoadDeviceByName("OpenVR");
|
||||
yield return null; //wait a frame before checking
|
||||
|
||||
if (!string.IsNullOrEmpty(XRSettings.loadedDeviceName))
|
||||
{
|
||||
DesktopVRSwitch.Logger.Msg("Starting SteamVR...");
|
||||
XRSettings.enabled = true;
|
||||
//force steamvr to reinitialize input
|
||||
//this does SteamVR_Input.actionSets[0].Activate() for us (we deactivate in StopVR())
|
||||
//but only if SteamVR_Settings.instance.activateFirstActionSetOnStart is enabled
|
||||
//which in ChilloutVR, it is, because all those settings are default
|
||||
SteamVR_Input.Initialize(true);
|
||||
|
||||
yield return null;
|
||||
|
||||
PostVRModeSwitch(true);
|
||||
yield break;
|
||||
}
|
||||
|
||||
DesktopVRSwitch.Logger.Error("Initializing VR Failed. Is there no VR device connected?");
|
||||
FailedVRModeSwitch(true);
|
||||
yield break;
|
||||
}
|
||||
|
||||
private IEnumerator StopVR()
|
||||
{
|
||||
PreVRModeSwitch(false);
|
||||
yield return null;
|
||||
|
||||
if (!string.IsNullOrEmpty(XRSettings.loadedDeviceName))
|
||||
{
|
||||
//SteamVR.SafeDispose(); //might fuck with SteamVRTrackingModule
|
||||
//deactivate the action set so SteamVR_Input.Initialize can reactivate
|
||||
SteamVR_Input.actionSets[0].Deactivate(SteamVR_Input_Sources.Any);
|
||||
XRSettings.LoadDeviceByName("");
|
||||
XRSettings.enabled = false;
|
||||
|
||||
yield return null;
|
||||
|
||||
ResetSteamVROverrides();
|
||||
PostVRModeSwitch(false);
|
||||
yield break;
|
||||
}
|
||||
|
||||
DesktopVRSwitch.Logger.Error("Attempted to exit VR without a VR device loaded.");
|
||||
FailedVRModeSwitch(false);
|
||||
yield break;
|
||||
}
|
||||
|
||||
//one frame before switch attempt
|
||||
public void PreVRModeSwitch(bool enableVR)
|
||||
{
|
||||
if (_softVRSwitch) return;
|
||||
//let tracked objects know we are attempting to switch
|
||||
VRModeSwitchTracker.PreVRModeSwitch(enableVR);
|
||||
}
|
||||
|
||||
//one frame after switch attempt
|
||||
public void FailedVRModeSwitch(bool enableVR)
|
||||
{
|
||||
if (_softVRSwitch) return;
|
||||
//let tracked objects know a switch failed
|
||||
VRModeSwitchTracker.FailVRModeSwitch(enableVR);
|
||||
}
|
||||
|
||||
//one frame after switch attempt
|
||||
public void PostVRModeSwitch(bool enableVR)
|
||||
{
|
||||
if (_softVRSwitch) return;
|
||||
|
||||
SetupVR(enableVR);
|
||||
|
||||
_switchInProgress = false;
|
||||
}
|
||||
|
||||
public void SetupVR(bool intoVR)
|
||||
{
|
||||
List<TryCatchHell.TryAction> actions = new List<TryCatchHell.TryAction>
|
||||
{
|
||||
TryCatchHell.SetCheckVR,
|
||||
TryCatchHell.SetMetaPort,
|
||||
TryCatchHell.RepositionCohtmlHud,
|
||||
TryCatchHell.UpdateHudOperations,
|
||||
TryCatchHell.DisableMirrorCanvas,
|
||||
TryCatchHell.SwitchActiveCameraRigs,
|
||||
TryCatchHell.ResetCVRInputManager,
|
||||
TryCatchHell.UpdateRichPresence,
|
||||
TryCatchHell.UpdateGestureReconizerCam,
|
||||
TryCatchHell.UpdateMenuCoreData,
|
||||
};
|
||||
|
||||
foreach (var action in actions)
|
||||
{
|
||||
TryCatchHell.TryExecute(action, intoVR);
|
||||
}
|
||||
|
||||
TryCatchHell.TryExecute(VRModeSwitchTracker.PostVRModeSwitch, intoVR);
|
||||
}
|
||||
|
||||
public void ResetSteamVROverrides()
|
||||
{
|
||||
// Reset physics time to Desktop default
|
||||
Time.fixedDeltaTime = 0.02f;
|
||||
|
||||
// Reset queued frames
|
||||
QualitySettings.maxQueuedFrames = 2;
|
||||
|
||||
// Reset framerate target
|
||||
int graphicsFramerateTarget = MetaPort.Instance.settings.GetSettingInt("GraphicsFramerateTarget", 0);
|
||||
CVRTools.SetFramerateTarget(graphicsFramerateTarget);
|
||||
|
||||
// Reset VSync setting
|
||||
bool graphicsVSync = MetaPort.Instance.settings.GetSettingsBool("GraphicsVSync", false);
|
||||
QualitySettings.vSyncCount = graphicsVSync ? 1 : 0;
|
||||
|
||||
// Reset anti-aliasing
|
||||
int graphicsMsaaLevel = MetaPort.Instance.settings.GetSettingInt("GraphicsMsaaLevel", 0);
|
||||
QualitySettings.antiAliasing = graphicsMsaaLevel;
|
||||
|
||||
// Reset eye tracking initialization
|
||||
bool interactionTobiiEyeTracking = MetaPort.Instance.settings.GetSettingsBool("InteractionTobiiEyeTracking", false);
|
||||
if (interactionTobiiEyeTracking)
|
||||
{
|
||||
MetaPort.Instance.TobiiXrInitializer.Initialize();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Won't do anything if not already running
|
||||
MetaPort.Instance.TobiiXrInitializer.DeInitialize();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
**/
|
|
@ -10,16 +10,16 @@ using Object = UnityEngine.Object;
|
|||
|
||||
namespace NAK.DesktopVRSwitch.Patches;
|
||||
|
||||
internal class ReferenceCameraPatch
|
||||
class ReferenceCameraPatch
|
||||
{
|
||||
internal static void OnWorldLoad()
|
||||
public static void OnWorldLoad()
|
||||
{
|
||||
Camera activeCamera = (MetaPort.Instance.isUsingVr ? PlayerSetup.Instance.vrCamera : PlayerSetup.Instance.desktopCamera).GetComponent<Camera>();
|
||||
Camera inactiveCamera = (MetaPort.Instance.isUsingVr ? PlayerSetup.Instance.desktopCamera : PlayerSetup.Instance.vrCamera).GetComponent<Camera>();
|
||||
CopyToInactiveCam(activeCamera, inactiveCamera);
|
||||
}
|
||||
|
||||
internal static void CopyToInactiveCam(Camera activeCam, Camera inactiveCam)
|
||||
static void CopyToInactiveCam(Camera activeCam, Camera inactiveCam)
|
||||
{
|
||||
DesktopVRSwitch.Logger.Msg("Copying active camera settings & components to inactive camera.");
|
||||
|
||||
|
|
|
@ -1,67 +0,0 @@
|
|||
using UnityEngine;
|
||||
|
||||
namespace NAK.DesktopVRSwitch;
|
||||
|
||||
internal class TryCatchHell
|
||||
{
|
||||
public delegate void TryAction(bool intoVR);
|
||||
|
||||
public static void TryExecute(TryAction action, bool intoVR)
|
||||
{
|
||||
try
|
||||
{
|
||||
action(intoVR);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"Error executing action: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
internal static void CloseCohtmlMenus(bool intoVR)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
internal static void RepositionCohtmlHud(bool intoVR)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
internal static void UpdateHudOperations(bool intoVR)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
internal static void DisableMirrorCanvas(bool intoVR)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
internal static void SwitchActiveCameraRigs(bool intoVR)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
internal static void PauseInputInteractions(bool intoVR)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
internal static void ReloadLocalAvatar(bool intoVR)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
internal static void UpdateGestureReconizerCam(bool intoVR)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
internal static void UpdateMenuCoreData(bool intoVR)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
using ABI_RC.Core.EventSystem;
|
||||
using ABI_RC.Core.Player;
|
||||
using ABI_RC.Core.Savior;
|
||||
using ABI_RC.Systems.MovementSystem;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NAK.DesktopVRSwitch;
|
||||
|
@ -16,6 +17,15 @@ internal static class Utils
|
|||
return PlayerSetup.Instance.desktopCamera;
|
||||
}
|
||||
|
||||
//stole from kafe :>
|
||||
internal static Vector3 GetPlayerRootPosition()
|
||||
{
|
||||
return MovementSystem.Instance.rotationPivot.position with
|
||||
{
|
||||
y = MovementSystem.Instance.transform.position.y
|
||||
};
|
||||
}
|
||||
|
||||
internal static void ReloadLocalAvatar()
|
||||
{
|
||||
DesktopVRSwitch.Logger.Msg("Attempting to reload current local avatar from GUID.");
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
using ABI_RC.Core.Player;
|
||||
using ABI_RC.Core;
|
||||
using ABI_RC.Core.UI;
|
||||
using UnityEngine;
|
||||
|
||||
|
@ -26,9 +26,9 @@ public class CohtmlHudTracker : VRModeTracker
|
|||
}
|
||||
DesktopVRSwitch.Logger.Msg("Configuring new hud affinity for CohtmlHud.");
|
||||
|
||||
_cohtmlHud.gameObject.transform.parent = intoVR ? PlayerSetup.Instance.vrCamera.transform : PlayerSetup.Instance.desktopCamera.transform;
|
||||
_cohtmlHud.gameObject.transform.parent = Utils.GetPlayerCameraObject(intoVR).transform;
|
||||
// This handles rotation and position
|
||||
ABI_RC.Core.CVRTools.ConfigureHudAffinity();
|
||||
CVRTools.ConfigureHudAffinity();
|
||||
_cohtmlHud.gameObject.transform.localScale = new Vector3(1.2f, 1f, 1.2f);
|
||||
}
|
||||
}
|
|
@ -25,9 +25,7 @@ public class MovementSystemTracker : VRModeTracker
|
|||
{
|
||||
_movementSystem = MovementSystem.Instance;
|
||||
|
||||
Vector3 position = _movementSystem.rotationPivot.transform.position;
|
||||
position.y = _movementSystem.transform.position.y;
|
||||
preSwitchWorldPosition = position;
|
||||
preSwitchWorldPosition = Utils.GetPlayerRootPosition();
|
||||
preSwitchWorldRotation = _movementSystem.rotationPivot.transform.rotation;
|
||||
|
||||
_movementSystem.ChangeCrouch(false);
|
||||
|
|
|
@ -22,12 +22,13 @@ public class VRModeSwitchManager : MonoBehaviour
|
|||
{
|
||||
public static VRModeSwitchManager Instance { get; private set; }
|
||||
|
||||
// I don't think I *need* this. Only using cause I don't want stuff just floating off.
|
||||
// I don't think I *need* this. Only using cause I don't want stuff just existing.
|
||||
private static readonly List<VRModeTracker> _vrModeTrackers = new List<VRModeTracker>();
|
||||
|
||||
public static event UnityAction<bool> OnPreVRModeSwitch;
|
||||
public static event UnityAction<bool> OnPostVRModeSwitch;
|
||||
public static event UnityAction<bool> OnFailVRModeSwitch;
|
||||
const string XRSETTINGS_DEVICE = "OpenVR";
|
||||
|
||||
public static void RegisterVRModeTracker(VRModeTracker observer)
|
||||
{
|
||||
|
@ -55,7 +56,6 @@ public class VRModeSwitchManager : MonoBehaviour
|
|||
DestroyImmediate(this);
|
||||
return;
|
||||
}
|
||||
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
|
@ -85,11 +85,11 @@ public class VRModeSwitchManager : MonoBehaviour
|
|||
// Start switch
|
||||
if (!isUsingVr)
|
||||
{
|
||||
yield return StartCoroutine(StartOpenVR());
|
||||
yield return StartCoroutine(StartSteamVR());
|
||||
}
|
||||
else
|
||||
{
|
||||
yield return StartCoroutine(StopOpenVR());
|
||||
StopSteamVR();
|
||||
}
|
||||
|
||||
// Check for updated VR mode
|
||||
|
@ -143,43 +143,36 @@ public class VRModeSwitchManager : MonoBehaviour
|
|||
|
||||
public bool IsInVR() => XRSettings.enabled;
|
||||
|
||||
private IEnumerator StartOpenVR()
|
||||
private IEnumerator StartSteamVR()
|
||||
{
|
||||
XRSettings.LoadDeviceByName("OpenVR");
|
||||
yield return null; //wait a frame before checking
|
||||
XRSettings.LoadDeviceByName(XRSETTINGS_DEVICE);
|
||||
yield return null; // wait a frame before checking
|
||||
|
||||
if (!string.IsNullOrEmpty(XRSettings.loadedDeviceName))
|
||||
{
|
||||
DesktopVRSwitch.Logger.Msg("Starting SteamVR...");
|
||||
XRSettings.enabled = true;
|
||||
SteamVR_Input.Initialize(true);
|
||||
//SteamVR.Initialize is fucking useless
|
||||
SteamVR_Behaviour.Initialize(true);
|
||||
SteamVR_Behaviour.instance.InitializeSteamVR(true);
|
||||
}
|
||||
|
||||
yield return null;
|
||||
yield break;
|
||||
}
|
||||
|
||||
DesktopVRSwitch.Logger.Error("Initializing VR Failed. Is there no VR device connected?");
|
||||
yield return null;
|
||||
yield break;
|
||||
}
|
||||
|
||||
private IEnumerator StopOpenVR()
|
||||
private void StopSteamVR()
|
||||
{
|
||||
SteamVR_Behaviour.instance.enabled = false;
|
||||
// Forces SteamVR to reinitialize SteamVR_Input next switch
|
||||
SteamVR_ActionSet_Manager.DisableAllActionSets();
|
||||
SteamVR_Input.initialized = false;
|
||||
|
||||
yield return null;
|
||||
// Remove SteamVR
|
||||
DestroyImmediate(SteamVR_Behaviour.instance.gameObject);
|
||||
SteamVR.enabled = false;
|
||||
|
||||
if (!string.IsNullOrEmpty(XRSettings.loadedDeviceName))
|
||||
{
|
||||
SteamVR_Input.actionSets[0].Deactivate(SteamVR_Input_Sources.Any);
|
||||
// Disable UnityXR
|
||||
XRSettings.LoadDeviceByName("");
|
||||
XRSettings.enabled = false;
|
||||
|
||||
yield return null;
|
||||
yield break;
|
||||
}
|
||||
|
||||
DesktopVRSwitch.Logger.Error("Attempted to exit VR without a VR device loaded.");
|
||||
yield return null;
|
||||
yield break;
|
||||
// We don't really need to wait on Stop()
|
||||
}
|
||||
}
|
|
@ -22,7 +22,7 @@ public class VRTrackerManagerTracker : VRModeTracker
|
|||
DesktopVRSwitch.Logger.Error("Error while getting VRTrackerManager!");
|
||||
return;
|
||||
}
|
||||
DesktopVRSwitch.Logger.Msg($"Resetting VRTrackerManager.");
|
||||
DesktopVRSwitch.Logger.Msg("Resetting VRTrackerManager.");
|
||||
|
||||
_vrTrackerManager.poses = null;
|
||||
_vrTrackerManager.leftHand = null;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue