Skip to content

Commit 92071e9

Browse files
committed
Make the packages work both on Editor and WebGL. Use WebXRSettings for refSpace and features.
1 parent b2e085f commit 92071e9

File tree

27 files changed

+502
-294
lines changed

27 files changed

+502
-294
lines changed

Build/Build/Build.data.unityweb

49 KB
Binary file not shown.

Build/Build/Build.wasm

167 KB
Binary file not shown.

Build/Build/Build.wasm.framework.unityweb

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

Build/webxr.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@
186186
XRManager.prototype.onRequestARSession = function () {
187187
if (!this.isARSupported) return;
188188
navigator.xr.requestSession('immersive-ar', {
189-
requiredFeatures: ['local-floor'], // TODO: Get this value from Unity
190-
optionalFeatures: ['hand-tracking', 'hit-test']
189+
requiredFeatures: this.gameInstance.Module.WebXR.Settings.ARRequiredReferenceSpace,
190+
optionalFeatures: this.gameInstance.Module.WebXR.Settings.AROptionalFeatures
191191
}).then(async (session) => {
192192
session.isImmersive = true;
193193
session.isInSession = true;
@@ -200,8 +200,8 @@
200200
XRManager.prototype.onRequestVRSession = function () {
201201
if (!this.isVRSupported) return;
202202
navigator.xr.requestSession('immersive-vr', {
203-
requiredFeatures: ['local-floor'], // TODO: Get this value from Unity
204-
optionalFeatures: ['hand-tracking']
203+
requiredFeatures: this.gameInstance.Module.WebXR.Settings.VRRequiredReferenceSpace,
204+
optionalFeatures: this.gameInstance.Module.WebXR.Settings.VROptionalFeatures
205205
}).then(async (session) => {
206206
session.isImmersive = true;
207207
session.isInSession = true;
@@ -648,7 +648,10 @@
648648

649649
let refSpaceType = 'viewer';
650650
if (session.isImmersive) {
651-
refSpaceType = 'local-floor';
651+
refSpaceType = this.gameInstance.Module.WebXR.Settings.VRRequiredReferenceSpace[0];
652+
if (session.isAR) {
653+
refSpaceType = this.gameInstance.Module.WebXR.Settings.ARRequiredReferenceSpace[0];
654+
}
652655

653656
var onSessionEnded = this.onEndSession.bind(this);
654657
session.addEventListener('end', onSessionEnded);

MainProject/Assets/XR/Settings/Web XR Settings.asset

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ MonoBehaviour:
1212
m_Script: {fileID: 11500000, guid: fc85f61fbfe6db9419f177419a072d26, type: 3}
1313
m_Name: Web XR Settings
1414
m_EditorClassIdentifier:
15+
VRRequiredReferenceSpace: 2
16+
VROptionalFeatures: 2
17+
ARRequiredReferenceSpace: 2
18+
AROptionalFeatures: -1

MainProject/Packages/packages-lock.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
"version": "file:webxr",
55
"depth": 0,
66
"source": "embedded",
7-
"dependencies": {}
7+
"dependencies": {
8+
"com.unity.xr.management": "3.2.13"
9+
}
810
},
911
"com.de-panther.webxr-interactions": {
1012
"version": "file:webxr-interactions",
1113
"depth": 0,
1214
"source": "embedded",
13-
"dependencies": {}
15+
"dependencies": {
16+
"com.de-panther.webxr": "0.2.0"
17+
}
1418
},
1519
"com.unity.ext.nunit": {
1620
"version": "1.0.0",

MainProject/ProjectSettings/ProjectSettings.asset

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ PlayerSettings:
128128
preloadedAssets:
129129
- {fileID: 2478256764130162806, guid: 552445d583cb06c4eaf9d56596dd45c2, type: 2}
130130
- {fileID: 11400000, guid: 65eada5edf5f16b4a9d0bd3a76fa026f, type: 2}
131+
- {fileID: 11400000, guid: a5e5b7605fb48984988490688c2a74e2, type: 2}
131132
metroInputSource: 0
132133
wsaTransparentSwapchain: 0
133134
m_HolographicPauseOnTrackingLoss: 1
Lines changed: 69 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,97 @@
11
using UnityEngine;
22
using System.Collections.Generic;
3-
using WebXR;
43

5-
public class ControllerInteraction : MonoBehaviour
4+
namespace WebXR.Interactions
65
{
7-
private FixedJoint attachJoint = null;
8-
private Rigidbody currentRigidBody = null;
9-
private List<Rigidbody> contactRigidBodies = new List<Rigidbody>();
10-
11-
private Animator anim;
12-
private WebXRController controller;
13-
14-
void Awake()
6+
public class ControllerInteraction : MonoBehaviour
157
{
16-
attachJoint = GetComponent<FixedJoint>();
17-
}
8+
private FixedJoint attachJoint = null;
9+
private Rigidbody currentRigidBody = null;
10+
private List<Rigidbody> contactRigidBodies = new List<Rigidbody>();
1811

19-
void Start()
20-
{
21-
anim = gameObject.GetComponent<Animator>();
22-
controller = gameObject.GetComponent<WebXRController>();
23-
}
12+
private Animator anim;
13+
private WebXRController controller;
2414

25-
void Update()
26-
{
27-
float normalizedTime = controller.GetButton("Trigger") ? 1 : controller.GetAxis("Grip");
15+
void Awake()
16+
{
17+
attachJoint = GetComponent<FixedJoint>();
18+
}
2819

29-
if (controller.GetButtonDown("Trigger") || controller.GetButtonDown("Grip"))
30-
Pickup();
20+
void Start()
21+
{
22+
anim = gameObject.GetComponent<Animator>();
23+
controller = gameObject.GetComponent<WebXRController>();
24+
}
3125

32-
if (controller.GetButtonUp("Trigger") || controller.GetButtonUp("Grip"))
33-
Drop();
26+
void Update()
27+
{
28+
float normalizedTime = controller.GetButton("Trigger") ? 1 : controller.GetAxis("Grip");
3429

35-
// Use the controller button or axis position to manipulate the playback time for hand model.
36-
anim.Play("Take", -1, normalizedTime);
37-
}
30+
if (controller.GetButtonDown("Trigger") || controller.GetButtonDown("Grip"))
31+
Pickup();
3832

39-
void OnTriggerEnter(Collider other)
40-
{
41-
if (other.gameObject.tag != "Interactable")
42-
return;
33+
if (controller.GetButtonUp("Trigger") || controller.GetButtonUp("Grip"))
34+
Drop();
4335

44-
contactRigidBodies.Add(other.gameObject.GetComponent<Rigidbody>());
45-
controller.Pulse(0.5f, 250);
46-
}
36+
// Use the controller button or axis position to manipulate the playback time for hand model.
37+
anim.Play("Take", -1, normalizedTime);
38+
}
4739

48-
void OnTriggerExit(Collider other)
49-
{
50-
if (other.gameObject.tag != "Interactable")
51-
return;
40+
void OnTriggerEnter(Collider other)
41+
{
42+
if (other.gameObject.tag != "Interactable")
43+
return;
5244

53-
contactRigidBodies.Remove(other.gameObject.GetComponent<Rigidbody>());
54-
}
45+
contactRigidBodies.Add(other.gameObject.GetComponent<Rigidbody>());
46+
controller.Pulse(0.5f, 250);
47+
}
5548

56-
public void Pickup()
57-
{
58-
currentRigidBody = GetNearestRigidBody();
49+
void OnTriggerExit(Collider other)
50+
{
51+
if (other.gameObject.tag != "Interactable")
52+
return;
5953

60-
if (!currentRigidBody)
61-
return;
54+
contactRigidBodies.Remove(other.gameObject.GetComponent<Rigidbody>());
55+
}
6256

63-
currentRigidBody.MovePosition(transform.position);
64-
attachJoint.connectedBody = currentRigidBody;
65-
}
57+
public void Pickup()
58+
{
59+
currentRigidBody = GetNearestRigidBody();
6660

67-
public void Drop()
68-
{
69-
if (!currentRigidBody)
70-
return;
61+
if (!currentRigidBody)
62+
return;
7163

72-
attachJoint.connectedBody = null;
73-
currentRigidBody = null;
74-
}
64+
currentRigidBody.MovePosition(transform.position);
65+
attachJoint.connectedBody = currentRigidBody;
66+
}
7567

76-
private Rigidbody GetNearestRigidBody()
77-
{
78-
Rigidbody nearestRigidBody = null;
79-
float minDistance = float.MaxValue;
80-
float distance = 0.0f;
68+
public void Drop()
69+
{
70+
if (!currentRigidBody)
71+
return;
72+
73+
attachJoint.connectedBody = null;
74+
currentRigidBody = null;
75+
}
8176

82-
foreach (Rigidbody contactBody in contactRigidBodies)
77+
private Rigidbody GetNearestRigidBody()
8378
{
84-
distance = (contactBody.gameObject.transform.position - transform.position).sqrMagnitude;
79+
Rigidbody nearestRigidBody = null;
80+
float minDistance = float.MaxValue;
81+
float distance = 0.0f;
8582

86-
if (distance < minDistance)
83+
foreach (Rigidbody contactBody in contactRigidBodies)
8784
{
88-
minDistance = distance;
89-
nearestRigidBody = contactBody;
85+
distance = (contactBody.gameObject.transform.position - transform.position).sqrMagnitude;
86+
87+
if (distance < minDistance)
88+
{
89+
minDistance = distance;
90+
nearestRigidBody = contactBody;
91+
}
9092
}
91-
}
9293

93-
return nearestRigidBody;
94+
return nearestRigidBody;
95+
}
9496
}
9597
}
Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
using UnityEngine;
22

3-
public class FPSCounter : MonoBehaviour
3+
namespace WebXR.Interactions
44
{
5-
public TextMesh text;
6-
private float fps = 0;
7-
private float framesCount = 0;
8-
private float lastCheck = 0;
9-
private float rate = 0.5f;
10-
11-
void Update()
5+
public class FPSCounter : MonoBehaviour
126
{
13-
framesCount++;
14-
if (Time.time >= lastCheck+rate)
7+
public TextMesh text;
8+
private float fps = 0;
9+
private float framesCount = 0;
10+
private float lastCheck = 0;
11+
private float rate = 0.5f;
12+
13+
void Update()
1514
{
16-
fps = framesCount / (Time.time-lastCheck);
17-
lastCheck = Time.time;
18-
framesCount = 0;
19-
text.text = fps.ToString("F0");
15+
framesCount++;
16+
if (Time.time >= lastCheck + rate)
17+
{
18+
fps = framesCount / (Time.time - lastCheck);
19+
lastCheck = Time.time;
20+
framesCount = 0;
21+
text.text = fps.ToString("F0");
22+
}
2023
}
2124
}
2225
}
Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,68 @@
11
using UnityEngine;
22

3-
[RequireComponent(typeof(Rigidbody))]
4-
public class MouseDragObject : MonoBehaviour
3+
namespace WebXR.Interactions
54
{
6-
private Camera currentCamera;
7-
private new Rigidbody rigidbody;
8-
private Vector3 screenPoint;
9-
private Vector3 offset;
10-
11-
void Awake()
5+
[RequireComponent(typeof(Rigidbody))]
6+
public class MouseDragObject : MonoBehaviour
127
{
13-
rigidbody = GetComponent<Rigidbody>();
14-
}
8+
private Camera currentCamera;
9+
private new Rigidbody rigidbody;
10+
private Vector3 screenPoint;
11+
private Vector3 offset;
1512

16-
void OnMouseDown()
17-
{
18-
currentCamera = FindCamera();
19-
if (currentCamera != null)
13+
void Awake()
2014
{
21-
screenPoint = currentCamera.WorldToScreenPoint(gameObject.transform.position);
22-
offset = gameObject.transform.position - currentCamera.ScreenToWorldPoint(GetMousePosWithScreenZ(screenPoint.z));
15+
rigidbody = GetComponent<Rigidbody>();
2316
}
24-
}
25-
26-
void OnMouseUp()
27-
{
28-
currentCamera = null;
29-
}
3017

31-
void FixedUpdate()
32-
{
33-
if (currentCamera != null)
18+
void OnMouseDown()
3419
{
35-
Vector3 currentScreenPoint = GetMousePosWithScreenZ(screenPoint.z);
36-
rigidbody.velocity = Vector3.zero;
37-
rigidbody.MovePosition(currentCamera.ScreenToWorldPoint(currentScreenPoint) + offset);
20+
currentCamera = FindCamera();
21+
if (currentCamera != null)
22+
{
23+
screenPoint = currentCamera.WorldToScreenPoint(gameObject.transform.position);
24+
offset = gameObject.transform.position - currentCamera.ScreenToWorldPoint(GetMousePosWithScreenZ(screenPoint.z));
25+
}
3826
}
39-
}
4027

41-
Vector3 GetMousePosWithScreenZ(float screenZ)
42-
{
43-
return new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenZ);
44-
}
28+
void OnMouseUp()
29+
{
30+
currentCamera = null;
31+
}
4532

46-
Camera FindCamera()
47-
{
48-
Camera[] cameras = FindObjectsOfType<Camera>();
49-
Camera result = null;
50-
int camerasSum = 0;
51-
foreach (var camera in cameras)
33+
void FixedUpdate()
5234
{
53-
if (camera.enabled)
35+
if (currentCamera != null)
5436
{
55-
result = camera;
56-
camerasSum++;
37+
Vector3 currentScreenPoint = GetMousePosWithScreenZ(screenPoint.z);
38+
rigidbody.velocity = Vector3.zero;
39+
rigidbody.MovePosition(currentCamera.ScreenToWorldPoint(currentScreenPoint) + offset);
5740
}
5841
}
59-
if (camerasSum > 1)
42+
43+
Vector3 GetMousePosWithScreenZ(float screenZ)
44+
{
45+
return new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenZ);
46+
}
47+
48+
Camera FindCamera()
6049
{
61-
result = null;
50+
Camera[] cameras = FindObjectsOfType<Camera>();
51+
Camera result = null;
52+
int camerasSum = 0;
53+
foreach (var camera in cameras)
54+
{
55+
if (camera.enabled)
56+
{
57+
result = camera;
58+
camerasSum++;
59+
}
60+
}
61+
if (camerasSum > 1)
62+
{
63+
result = null;
64+
}
65+
return result;
6266
}
63-
return result;
6467
}
6568
}

0 commit comments

Comments
 (0)