Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion Uuvr/ModConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,17 @@ public enum VrApi
[Description("OpenXR")]
OpenXr,
}

#endif


public enum DegreesOfFreedom
{
[Description("3-DOF")]
Three,
[Description("6-DOF")]
Six,
}

public enum ScreenSpaceCanvasType
{
[Description("None")]
Expand Down Expand Up @@ -61,8 +70,10 @@ public enum UiPatchMode
CanvasRedirect,
}


public readonly ConfigFile Config;
public readonly ConfigEntry<CameraTrackingMode> CameraTracking;
public readonly ConfigEntry<DegreesOfFreedom> HeadsetDOF;
public readonly ConfigEntry<bool> RelativeCameraSetStereoView;
public readonly ConfigEntry<int> VrCameraDepth;
public readonly ConfigEntry<int> VrUiLayerOverride;
Expand Down Expand Up @@ -103,7 +114,9 @@ public ModConfiguration(ConfigFile config)
CameraTrackingMode.RelativeTransform,
#endif
"Defines how camera tracking is done. Relative is usually preferred, but not all games support it. Changing this might require restarting the level.");



RelativeCameraSetStereoView = config.Bind(
"Relative Camera",
"Use SetStereoView for Relative Camera",
Expand All @@ -116,6 +129,13 @@ public ModConfiguration(ConfigFile config)
false,
"Prevents pitch and roll changes on the camera, allowing only yaw changes.");


HeadsetDOF = config.Bind(
"Camera",
"Headset Degrees of Freedom",
DegreesOfFreedom.Three,
"Some games are better with 3-DOF, some are better with 6-DOF. Changing this might require restarting the level.");

CameraPositionOffsetX = config.Bind(
"Camera",
"Camera Position Offset X",
Expand Down
12 changes: 10 additions & 2 deletions Uuvr/UuvrCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public UuvrCore(IntPtr pointer) : base(pointer)
}
#endif

private readonly KeyboardKey _recenterKey = new (KeyboardKey.KeyCode.F2);
private readonly KeyboardKey _toggleVrKey = new (KeyboardKey.KeyCode.F3);
private float _originalFixedDeltaTime;

Expand Down Expand Up @@ -56,12 +57,19 @@ private void Start()

_vrTogglerManager = new VrTogglerManager();

SetPositionTrackingEnabled(false);
SetPositionTrackingEnabled(true);
}

private void Update()
{
if (_toggleVrKey.UpdateIsDown()) _vrTogglerManager?.ToggleVr();
if (_toggleVrKey.UpdateIsDown())
_vrTogglerManager?.ToggleVr();

if (_recenterKey.UpdateIsDown())
{
UuvrPoseDriver.RecenterView();
}

UpdatePhysicsRate();
}

Expand Down
42 changes: 42 additions & 0 deletions Uuvr/UuvrPoseDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using UnityEngine;
using Uuvr.UnityTypesHelper;

using static Uuvr.ModConfiguration;

namespace Uuvr;

public class UuvrPoseDriver: UuvrBehaviour
Expand All @@ -14,6 +16,9 @@ public UuvrPoseDriver(IntPtr pointer) : base(pointer)
#endif

private MethodInfo? _trackingRotationMethod;

private MethodInfo? _trackingPositionMethod;

private readonly object[] _trackingRotationMethodArgs = {
2 // Enum value for XRNode.CenterEye
};
Expand All @@ -27,6 +32,7 @@ protected override void Awake()
Type.GetType("UnityEngine.VR.InputTracking, UnityEngine");

_trackingRotationMethod = inputTrackingType?.GetMethod("GetLocalRotation");


if (_trackingRotationMethod == null)
{
Expand All @@ -35,7 +41,14 @@ protected override void Awake()
return;
}

_trackingPositionMethod = inputTrackingType?.GetMethod("GetLocalPosition");
if (_trackingPositionMethod == null)
{
Debug.LogWarning("Failed to find InputTracking.GetLocalPosition. Position tracking will be disabled.");
}

DisableCameraAutoTracking();
RecenterView();
}

protected override void OnBeforeRender()
Expand All @@ -54,12 +67,41 @@ private void LateUpdate()
UpdateTransform();
}

/// <summary>
/// This vector is used to recenter the view position. (its static so that all VRCamera's share the same offset.
/// </summary>
private static Vector3? _positionOffset;


public static void RecenterView()
{
_positionOffset = null;
}


private void UpdateTransform()
{
if (_trackingRotationMethod != null)
{
transform.localRotation = (Quaternion)_trackingRotationMethod.Invoke(null, _trackingRotationMethodArgs);
}

if (_trackingPositionMethod != null && ModConfiguration.Instance.HeadsetDOF.Value == DegreesOfFreedom.Six)
{
var pos = (Vector3)_trackingPositionMethod.Invoke(null, _trackingRotationMethodArgs);
if (_positionOffset == null)
{
_positionOffset = -pos; // First time we get the position, we store the reverse as the offset.
}
// add the offset to the position.
pos += _positionOffset.Value;

transform.localPosition = pos;
}
else
{
transform.localPosition = Vector3.zero;
}
}

private void DisableCameraAutoTracking()
Expand Down