Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 5c8520d

Browse files
committed
Updated WebXRCamera and Controller scripts to work with Unity XR Plugin system (non-legacy)
1 parent 573c1ed commit 5c8520d

File tree

2 files changed

+43
-40
lines changed

2 files changed

+43
-40
lines changed

Assets/WebXR/Scripts/WebXRCamera.cs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ void OnEnable()
3333

3434
cameraMain.transform.localPosition = new Vector3(0, WebXRManager.Instance.DefaultHeight, 0);
3535

36-
#if UNITY_EDITOR
37-
// No editor specific funtionality
38-
#elif UNITY_WEBGL
39-
postRenderCoroutine = StartCoroutine(endOfFrame());
40-
#endif
36+
#if UNITY_EDITOR
37+
// No editor specific funtionality
38+
#elif UNITY_WEBGL
39+
postRenderCoroutine = StartCoroutine(endOfFrame());
40+
#endif
4141
}
4242

4343
private void OnDisable()
@@ -81,31 +81,34 @@ private void onHeadsetUpdate(
8181
cameraR.projectionMatrix = rightProjectionMatrix;
8282
}
8383
}
84+
85+
//Update Camera position according to Unity XR, if not using WebGL
8486
private void Update()
8587
{
86-
#if !UNITY_EDITOR
87-
return;
88-
#endif
89-
InputTracking.GetNodeStates(mNodeStates);
88+
List<InputDevice> devices = new List<InputDevice>();
89+
InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.HeadMounted, devices);
90+
bool XRisPresent = devices.Count > 0;
91+
if (XRisPresent) {
92+
List<XRNodeState> mNodeStates = new List<XRNodeState>();
93+
InputTracking.GetNodeStates(mNodeStates);
9094

91-
foreach (XRNodeState nodeState in mNodeStates)
92-
{
93-
switch (nodeState.nodeType)
95+
Vector3 mHeadPos = Vector3.zero;
96+
Quaternion mHeadRot = Quaternion.identity;
97+
foreach (XRNodeState nodeState in mNodeStates)
9498
{
95-
case XRNode.Head:
96-
nodeState.TryGetPosition(out mHeadPos);
97-
nodeState.TryGetRotation(out mHeadRot);
98-
break;
99+
switch (nodeState.nodeType)
100+
{
101+
case XRNode.Head:
102+
nodeState.TryGetPosition(out mHeadPos);
103+
nodeState.TryGetRotation(out mHeadRot);
104+
break;
99105

106+
}
100107
}
108+
cameraMain.transform.localPosition = mHeadPos;
109+
cameraMain.transform.localRotation = mHeadRot.normalized;
101110
}
102-
Head.transform.localPosition = mHeadPos;
103-
Head.transform.localRotation = mHeadRot.normalized;
104111
}
105-
public GameObject Head;
106-
107-
private List<XRNodeState> mNodeStates = new List<XRNodeState>();
108-
private Vector3 mHeadPos;
109-
private Quaternion mHeadRot;
110112
}
113+
111114
}

Assets/WebXR/Scripts/WebXRController.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,8 @@ private Vector3 applyArmModel(Vector3 controllerPosition, Quaternion controllerR
270270

271271
bool xr_inited = false;
272272

273-
//public GameObject Head;
274273

275-
private List<XRNodeState> mNodeStates = new List<XRNodeState>();
276-
private Vector3 mHeadPos;
277-
private Quaternion mHeadRot;
278-
private Vector3 mHandPos;
279-
private Quaternion mHandRot;
280-
//roblkw - mod
274+
281275
void InitXR()
282276
{
283277
xr_inited = true;
@@ -290,16 +284,22 @@ void InitXR()
290284
}
291285
void Update()
292286
{
293-
#if !UNITY_EDITOR
294-
return;
295-
#endif
296287
// Use Unity XR Input when enabled. When using WebXR, updates are performed onControllerUpdate.
297-
//if (!XRDevice.isPresent) return;
288+
List<InputDevice> devices = new List<InputDevice>();
289+
InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.HeadMounted, devices);
290+
bool XRisPresent = devices.Count > 0;
291+
if (!XRisPresent) return;
298292

299293
if (!xr_inited) InitXR();
300294

301295
SetVisible(true);
302296

297+
List<XRNodeState> mNodeStates = new List<XRNodeState>();
298+
Vector3 mHeadPos = Vector3.zero;
299+
Quaternion mHeadRot = Quaternion.identity;
300+
Vector3 mHandPos = Vector3.zero;
301+
Quaternion mHandRot = Quaternion.identity;
302+
303303
if (this.hand == WebXRControllerHand.LEFT)
304304
handNode = XRNode.LeftHand;
305305

@@ -338,15 +338,15 @@ void Update()
338338
if (this.simulate3dof)
339339
{
340340
_t.localPosition = applyArmModel(
341-
mHeadPos,//InputTracking.GetLocalPosition(XRNode.Head), // we use head position as origin
342-
mHandRot,// InputTracking.GetLocalRotation(handNode);
343-
mHeadRot);//InputTracking.GetLocalRotation(XRNode.Head));
344-
_t.localRotation = mHandRot; // InputTracking.GetLocalRotation(handNode);
341+
mHeadPos, // we use head position as origin
342+
mHandRot,
343+
mHeadRot);
344+
_t.localRotation = mHandRot;
345345
}
346346
else
347347
{
348-
_t.localPosition = mHandPos;// InputTracking.GetLocalPosition(handNode);
349-
_t.localRotation = mHandRot;// InputTracking.GetLocalRotation(handNode);
348+
_t.localPosition = mHandPos;
349+
_t.localRotation = mHandRot;
350350
}
351351

352352
foreach (WebXRControllerInput input in inputMap.inputs)

0 commit comments

Comments
 (0)