Skip to content

Commit

Permalink
Merge pull request #10374 from microsoft/OculusVisMerge
Browse files Browse the repository at this point in the history
Oculus visualization now integrated with MRTK (#10067)
  • Loading branch information
Michael Hoffman authored Dec 15, 2021
2 parents 75e30f8 + d913b85 commit 726fe83
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,6 @@ private void RenderControllerList(SerializedProperty controllerList)
{
EditorGUILayout.HelpBox("A controller type must be defined!", MessageType.Error);
}
else
{
// Only check for Oculus if we already know the type is valid (otherwise, null ref)
bool isOculusType = controllerType.Type.FullName.Contains("OculusXRSDKTouchController");
if (isOculusType)
{
EditorGUILayout.HelpBox("Oculus Touch controller model visualization is not managed by MRTK, refer to the Oculus XRSDK Device Manager to configure controller visualization settings", MessageType.Error);
}
}

var handednessValue = mixedRealityControllerHandedness.intValue - 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using UnityEngine;
using UnityEngine.XR;
using System;
using System.Threading.Tasks;
using System.Threading;

#if OCULUS_ENABLED
using Unity.XR.Oculus;
Expand All @@ -33,6 +35,8 @@ public OculusXRSDKTouchController(
: base(trackingState, controllerHandedness, inputSource, interactions, new OculusTouchControllerDefinition(controllerHandedness))
{ }

internal GameObject OculusControllerVisualization { get; private set; }

private static readonly ProfilerMarker UpdateButtonDataPerfMarker = new ProfilerMarker("[MRTK] OculusXRSDKController.UpdateButtonData");

protected override void UpdateButtonData(MixedRealityInteractionMapping interactionMapping, InputDevice inputDevice)
Expand Down Expand Up @@ -91,25 +95,58 @@ protected override void UpdateButtonData(MixedRealityInteractionMapping interact
}
}

/// <summary>
/// Determines whether or not this controller is using MRTK for controller visualization
///
/// When false, the Oculus Touch controller model visualization will not be managed by MRTK
/// Ensure that the Oculus Integration Package is installed and the Ovr Camera Rig is set correctly in the Oculus XRSDK Device Manager to use the
/// Oculus Integration Package's visualization
/// </summary>
internal bool UseMRTKControllerVisualization
/// <inheritdoc />
protected override bool TryRenderControllerModel(System.Type controllerType, InputSourceType inputSourceType)
{
get
if (GetControllerVisualizationProfile() != null &&
GetControllerVisualizationProfile().GetUsePlatformModelsOverride(GetType(), ControllerHandedness))
{
TryRenderControllerModelFromOculus();
return true;
}
else
{
return Visualizer != null && Visualizer.GameObjectProxy != null && Visualizer.GameObjectProxy.activeSelf;
return base.TryRenderControllerModel(controllerType, inputSourceType);
}
set
}

private async void TryRenderControllerModelFromOculus()
{
await WaitForOculusVisuals();

if (this != null)
{
if(Visualizer != null && Visualizer.GameObjectProxy)
if (OculusControllerVisualization != null
&& MixedRealityControllerModelHelpers.TryAddVisualizationScript(OculusControllerVisualization, GetType(), ControllerHandedness)
&& TryAddControllerModelToSceneHierarchy(OculusControllerVisualization))
{
Visualizer.GameObjectProxy.SetActive(value);
OculusControllerVisualization.SetActive(true);
return;
}

Debug.LogWarning("Failed to obtain Oculus controller model; defaulting to BaseController behavior.");
base.TryRenderControllerModel(GetType(), InputSource.SourceType);
}
}

private const int controllerInitializationTimeout = 1000;
private async Task WaitForOculusVisuals()
{
int timeWaited = 0;
while (OculusControllerVisualization == null || timeWaited > controllerInitializationTimeout)
{
await Task.Delay(100);
timeWaited += 100;
}
}

internal void RegisterControllerVisualization(GameObject visualization)
{
OculusControllerVisualization = visualization;
if (GetControllerVisualizationProfile() != null &&
!GetControllerVisualizationProfile().GetUsePlatformModelsOverride(GetType(), ControllerHandedness))
{
OculusControllerVisualization.SetActive(false);
}
}
}
Expand Down
47 changes: 36 additions & 11 deletions Assets/MRTK/Providers/Oculus/XRSDK/OculusXRSDKDeviceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#if OCULUSINTEGRATION_PRESENT
using System.Collections.Generic;
using UnityEngine;
#endif // OCULUSINTEGRATION_PRESENT

namespace Microsoft.MixedReality.Toolkit.XRSDK.Oculus.Input
Expand Down Expand Up @@ -58,6 +57,8 @@ public override void Initialize()
private readonly Dictionary<Handedness, OculusHand> trackedHands = new Dictionary<Handedness, OculusHand>();

private OVRCameraRig cameraRig;
private OVRControllerHelper leftControllerHelper;
private OVRControllerHelper rightControllerHelper;

private OVRHand rightHand;
private OVRSkeleton rightSkeleton;
Expand Down Expand Up @@ -95,9 +96,23 @@ public override bool CheckCapability(MixedRealityCapability capability)
protected override GenericXRSDKController GetOrAddController(InputDevice inputDevice)
{
GenericXRSDKController controller = base.GetOrAddController(inputDevice);
if (controller is OculusXRSDKTouchController oculusTouchController)

if (!cameraRig.IsNull() && controller is OculusXRSDKTouchController oculusTouchController && oculusTouchController.OculusControllerVisualization == null)
{
oculusTouchController.UseMRTKControllerVisualization = cameraRig.IsNull();
GameObject platformVisualization = null;
if (oculusTouchController.ControllerHandedness == Handedness.Left)
{
platformVisualization = leftControllerHelper.gameObject;
}
if (oculusTouchController.ControllerHandedness == Handedness.Right)
{
platformVisualization = rightControllerHelper.gameObject;
}

if(platformVisualization != null)
{
oculusTouchController.RegisterControllerVisualization(platformVisualization);
}
}

return controller;
Expand Down Expand Up @@ -248,21 +263,31 @@ private void SetupInput()
cameraRig.EnsureGameObjectIntegrity();
}

bool useAvatarHands = SettingsProfile.RenderAvatarHandsInsteadOfController;
// If using Avatar hands, deactivate ovr controller rendering
foreach (var controllerHelper in cameraRig.gameObject.GetComponentsInChildren<OVRControllerHelper>())
bool useAvatarHands = SettingsProfile.RenderAvatarHandsWithControllers;
// If using Avatar hands, initialize the local avatar controller
if (useAvatarHands)
{
controllerHelper.gameObject.SetActive(!useAvatarHands);
GameObject.Instantiate(SettingsProfile.LocalAvatarPrefab, cameraRig.trackingSpace);
}

if (useAvatarHands)

var ovrControllerHelpers = cameraRig.GetComponentsInChildren<OVRControllerHelper>();
foreach (var ovrControllerHelper in ovrControllerHelpers)
{
// Initialize the local avatar controller
GameObject.Instantiate(SettingsProfile.LocalAvatarPrefab, cameraRig.trackingSpace);
switch (ovrControllerHelper.m_controller)
{
case OVRInput.Controller.LTouch:
leftControllerHelper = ovrControllerHelper;
break;
case OVRInput.Controller.RTouch:
rightControllerHelper = ovrControllerHelper;
break;
default:
break;
}
}

var ovrHands = cameraRig.GetComponentsInChildren<OVRHand>();

foreach (var ovrHand in ovrHands)
{
// Manage Hand skeleton data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using UnityEngine;
using UnityEngine.Serialization;

namespace Microsoft.MixedReality.Toolkit.XRSDK.Oculus.Input
{
Expand Down Expand Up @@ -60,15 +62,23 @@ public GameObject OVRCameraRigPrefab


[SerializeField]
[FormerlySerializedAs("renderAvatarHandsInsteadOfControllers")]
[Tooltip("Using avatar hands requires a local avatar prefab. Failure to provide one will result in nothing being displayed. \n\n" +
"Note: In order to render avatar hands, you will need to set an app id in Assets/Resources/OvrAvatarSettings. Any number will do, but it needs to be set.")]
private bool renderAvatarHandsInsteadOfControllers = true;
private bool renderAvatarHandsWithControllers = true;

/// <summary>
/// Using avatar hands requires a local avatar prefab. Failure to provide one will result in nothing being displayed.
/// "Note: In order to render avatar hands, you will need to set an app id in Assets/Resources/OvrAvatarSettings. Any number will do, but it needs to be set.")]
/// </summary>
public bool RenderAvatarHandsInsteadOfController => renderAvatarHandsInsteadOfControllers;
[Obsolete("Use RenderAvatarHandsWithControllers instead")]
public bool RenderAvatarHandsInsteadOfController => renderAvatarHandsWithControllers;

/// <summary>
/// Using avatar hands requires a local avatar prefab. Failure to provide one will result in nothing being displayed.
/// "Note: In order to render avatar hands, you will need to set an app id in Assets/Resources/OvrAvatarSettings. Any number will do, but it needs to be set.")]
/// </summary>
public bool RenderAvatarHandsWithControllers => renderAvatarHandsWithControllers;

[SerializeField]
[Tooltip("Prefab reference for LocalAvatar to load, if none are found in scene.")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ MonoBehaviour:
isCustomProfile: 0
ovrCameraRigPrefab: {fileID: 2343937678421323989, guid: 69a746aa83d0d0e45b4e2d33eab0fff4,
type: 3}
renderAvatarHandsInsteadOfControllers: 1
renderAvatarHandsWithControllers: 0
localAvatarPrefab: {fileID: 6297684789857299957, guid: 5357c8e6c4495c04f90e97272375c294,
type: 3}
minimumHandConfidence: 0
Expand Down

0 comments on commit 726fe83

Please sign in to comment.