Skip to content

Commit

Permalink
Merge pull request #406 from MeteorStudioASU/master
Browse files Browse the repository at this point in the history
Updated to work with XR plugin management (non-legacy)
  • Loading branch information
fernandojsg committed Jun 9, 2020
2 parents 5b21ce0 + 79f04e6 commit 7c49ba1
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 30 deletions.
35 changes: 34 additions & 1 deletion Assets/WebXR/Scripts/WebXRCamera.cs
@@ -1,6 +1,8 @@
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using UnityEngine.XR;
using System.Collections.Generic;

namespace WebXR
{
Expand Down Expand Up @@ -79,5 +81,36 @@ private void onVRChange(WebXRState state)
cameraR.projectionMatrix = rightProjectionMatrix;
}
}
#if UNITY_EDITOR || !UNITY_WEBGL
//Update Camera position according to Unity XR, if not using WebGL
private void Update()
{
List<InputDevice> devices = new List<InputDevice>();
InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.HeadMounted, devices);
bool XRisPresent = devices.Count > 0;
if (XRisPresent) {
List<XRNodeState> mNodeStates = new List<XRNodeState>();
InputTracking.GetNodeStates(mNodeStates);

Vector3 mHeadPos = Vector3.zero;
Quaternion mHeadRot = Quaternion.identity;
foreach (XRNodeState nodeState in mNodeStates)
{
switch (nodeState.nodeType)
{
case XRNode.Head:
nodeState.TryGetPosition(out mHeadPos);
nodeState.TryGetRotation(out mHeadRot);
break;

}
}
cameraMain.transform.localPosition = mHeadPos;
cameraMain.transform.localRotation = mHeadRot.normalized;
}
}
#endif

}
}

}
72 changes: 63 additions & 9 deletions Assets/WebXR/Scripts/WebXRController.cs
Expand Up @@ -268,31 +268,85 @@ private void SetVisible(bool visible)
return controllerPosition;
}

bool xr_inited = false;


#if UNITY_EDITOR || !UNITY_WEBGL
void InitXR()
{
xr_inited = true;
List<XRInputSubsystem> subsystems = new List<XRInputSubsystem>();
SubsystemManager.GetInstances<XRInputSubsystem>(subsystems);
for (int i = 0; i < subsystems.Count; i++)
{
subsystems[i].TrySetTrackingOriginMode(TrackingOriginModeFlags.Floor);
}
}

void Update()
{
// Use Unity XR Input when enabled. When using WebXR, updates are performed onControllerUpdate.
if (!XRDevice.isPresent) return;

List<InputDevice> devices = new List<InputDevice>();
InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.HeadMounted, devices);
bool XRisPresent = devices.Count > 0;
if (!XRisPresent) return;

if (!xr_inited) InitXR();

SetVisible(true);

List<XRNodeState> mNodeStates = new List<XRNodeState>();
Vector3 mHeadPos = Vector3.zero;
Quaternion mHeadRot = Quaternion.identity;
Vector3 mHandPos = Vector3.zero;
Quaternion mHandRot = Quaternion.identity;

if (this.hand == WebXRControllerHand.LEFT)
handNode = XRNode.LeftHand;

if (this.hand == WebXRControllerHand.RIGHT)
handNode = XRNode.RightHand;

InputTracking.GetNodeStates(mNodeStates);

foreach (XRNodeState nodeState in mNodeStates)
{
switch (nodeState.nodeType)
{
case XRNode.Head:
nodeState.TryGetPosition(out mHeadPos);
nodeState.TryGetRotation(out mHeadRot);
break;
case XRNode.LeftHand:
if (this.hand == WebXRControllerHand.LEFT) {
nodeState.TryGetPosition(out mHandPos);
nodeState.TryGetRotation(out mHandRot);
}
break;
case XRNode.RightHand:
if (this.hand == WebXRControllerHand.RIGHT)
{
nodeState.TryGetPosition(out mHandPos);
nodeState.TryGetRotation(out mHandRot);
}
break;

}
}


if (this.simulate3dof)
{
_t.localPosition = applyArmModel(
InputTracking.GetLocalPosition(XRNode.Head), // we use head position as origin
InputTracking.GetLocalRotation(handNode),
InputTracking.GetLocalRotation(XRNode.Head));
_t.localRotation = InputTracking.GetLocalRotation(handNode);
mHeadPos, // we use head position as origin
mHandRot,
mHeadRot);
_t.localRotation = mHandRot;
}
else
{
_t.localPosition = InputTracking.GetLocalPosition(handNode);
_t.localRotation = InputTracking.GetLocalRotation(handNode);
_t.localPosition = mHandPos;
_t.localRotation = mHandRot;
}

foreach (WebXRControllerInput input in inputMap.inputs)
Expand All @@ -306,7 +360,7 @@ void Update()
}
}
}

#endif
private void Awake()
{
_t = transform;
Expand Down
26 changes: 6 additions & 20 deletions docs/xr-testing.md
Expand Up @@ -4,29 +4,15 @@

For rapid testing in your headset, you can enable Virtual Reality support in your [Unity project](./project-setup.md).

![Animation of Unity's XR Settings](./images/unity-xr-settings.gif "Animation of Unity's XR Settings")
1. Select the **`Window > Package Manager…`** menu option from the top-level Unity application menu.

1. Select the **`File > Build Settings…`** menu option from the top-level Unity application menu.
2. When the pop-up window appears, select to highlight the **`PC, Mac & Linux Standalone`** option in the **`Platform`** list. (When you are ready to [build your application for WebXR](./build.md), ensure that **`WebGL`** is still the selected platform.)
2. When the pop-up window appears, select the **`XR Plugin Management`** package, and click on the **``Install``** button at the bottom.

![Screenshot of "Platform" list in Unity's "Build Settings…" menu](./images/standalone-platform.png 'Screenshot of "Platform" list in Unity\'s "Build Settings…" menu')
3. Select the **`Edit > Project Settings…`** menu option from the top-level Unity application menu.

3. Click the **`Player Settings…`** button at the bottom of the pop-up window. Or you can access the **`Player Settings…`** by selecting the **`Edit > Project Settings > Player`** menu option from the top-level Unity application menu.
4. When the pop-up window appears, select to highlight the **`XR Plug-in Management`** setting from the list on the left.

![Screenshot of Unity's "Player Settings…" menu](./images/unity-xr-settings-5.png 'Screenshot of Unity\'s "Player Settings…" menu')
5. In the **`XR Plug-in Management`** pane, ensure that **`Initialize XR on Startup`** is checked.

4. In the **`Inspector`** pane, click the tab for **`Settings for PC, Mac & Linux Standalone`** pane (i.e., the icon with the underlined down arrow), if the tab is not already selected.
6. Select the **`Plug-in Provider`** matching your headset.

![Screenshot of Unity's "Settings for PC, Mac & Linux Standalone" pane](./images/unity-xr-settings-6.png 'Screenshot of Unity\'s "Settings for PC, Mac & Linux Standalone" pane')

5. Click on the **`XR Settings`** title to expose the section if it is hidden. Enable the **`Virtual Reality Supported`** checkbox field.

![Screenshot of Unity's "XR Settings"](./images/unity-xr-settings-7.png 'Screenshot of Unity\'s "XR Settings"')

6. Notice that are two **`Virtual Reality SDKs`** listed, sorted in the order in which Unity attempts to enable VR devices at runtime or during VR preview:
- **`OpenVR`** (i.e., for the HTC Vive and Windows Mixed Reality headsets)
- **`Oculus`** (i.e., for the Oculus Rift and, soon, Oculus Go headsets)

When building for [WebXR](./build.md), ensure that the **`WebGL`** option is still selected in the **`Platform`** list (**`File > Build Settings…`**).

![Screenshot of "Platform" list in Unity's "Build Settings…" menu](./images/webgl-platform.png 'Screenshot of "Platform" list in Unity\'s "Build Settings…" menu')

0 comments on commit 7c49ba1

Please sign in to comment.