Skip to content

Commit 34ca99c

Browse files
committed
Added support for WebXR XRVisibilityState
1 parent 7acd880 commit 34ca99c

File tree

5 files changed

+172
-90
lines changed

5 files changed

+172
-90
lines changed

Packages/webxr/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- Support for WebXR XRVisibilityState.
10+
811
### Changed
912
- Use view transform matrix instead of orientation/position for pose.
1013

Packages/webxr/Runtime/Plugins/WebGL/webxr.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@ typedef void (*webxr_void_string)(const char *ptr);
88

99
webxr_void_int_float4_float4 on_start_ar_ref;
1010
webxr_void_int_float4_float4 on_start_vr_ref;
11+
webxr_void_int on_visibility_change_ref;
1112
webxr_void on_end_xr_ref;
1213
webxr_void_string on_xr_capabilities_ref;
1314
webxr_void_string on_input_profiles_ref;
1415

1516
void set_webxr_events(
1617
webxr_void_int_float4_float4 _on_start_ar,
1718
webxr_void_int_float4_float4 _on_start_vr,
19+
webxr_void_int _on_visibility_change,
1820
webxr_void _on_end_xr,
1921
webxr_void_string _on_xr_capabilities,
2022
webxr_void_string _on_input_profiles
2123
) {
2224
on_start_ar_ref = _on_start_ar;
2325
on_start_vr_ref = _on_start_vr;
26+
on_visibility_change_ref = _on_visibility_change;
2427
on_end_xr_ref = _on_end_xr;
2528
on_xr_capabilities_ref = _on_xr_capabilities;
2629
on_input_profiles_ref = _on_input_profiles;
@@ -44,6 +47,11 @@ void EMSCRIPTEN_KEEPALIVE on_start_vr(int views_count,
4447
right_x, right_y, right_w, right_h);
4548
}
4649

50+
void EMSCRIPTEN_KEEPALIVE on_visibility_change(int visibility_state)
51+
{
52+
on_visibility_change_ref(visibility_state);
53+
}
54+
4755
void EMSCRIPTEN_KEEPALIVE on_end_xr()
4856
{
4957
on_end_xr_ref();

Packages/webxr/Runtime/Plugins/WebGL/webxr.jspre

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ setTimeout(function () {
217217
this.isARSupported = false;
218218
this.isVRSupported = false;
219219
this.onInputEvent = null;
220+
this.onSessionVisibilityEvent = null;
220221
this.BrowserObject = null;
221222
this.JSEventsObject = null;
222223
this.init();
@@ -331,6 +332,7 @@ setTimeout(function () {
331332
xrSessionEvent.session.removeEventListener('squeeze', this.onInputEvent);
332333
xrSessionEvent.session.removeEventListener('squeezestart', this.onInputEvent);
333334
xrSessionEvent.session.removeEventListener('squeezeend', this.onInputEvent);
335+
xrSessionEvent.session.removeEventListener('visibilitychange', this.onSessionVisibilityEvent);
334336
}
335337

336338
if (this.viewerHitTestSource) {
@@ -458,7 +460,11 @@ setTimeout(function () {
458460
}
459461
}
460462
}
461-
463+
464+
XRManager.prototype.onVisibilityChange = function (event) {
465+
this.gameModule.WebXR.OnVisibilityChange(this.xrSession.visibilityState);
466+
}
467+
462468
XRManager.prototype.toggleAr = function () {
463469
if (!this.gameModule)
464470
{
@@ -571,6 +577,7 @@ setTimeout(function () {
571577
this.UpdateXRCapabilities();
572578

573579
this.onInputEvent = this.onInputSourceEvent.bind(this);
580+
this.onSessionVisibilityEvent = this.onVisibilityChange.bind(this);
574581
}
575582

576583
XRManager.prototype.UpdateXRCapabilities = function() {
@@ -812,6 +819,7 @@ setTimeout(function () {
812819
session.addEventListener('squeeze', this.onInputEvent);
813820
session.addEventListener('squeezestart', this.onInputEvent);
814821
session.addEventListener('squeezeend', this.onInputEvent);
822+
session.addEventListener('visibilitychange', this.onSessionVisibilityEvent);
815823

816824
this.xrData.controllerA.updatedProfiles = 0;
817825
this.xrData.controllerB.updatedProfiles = 0;
@@ -999,6 +1007,7 @@ setTimeout(function () {
9991007
} else {
10001008
this.gameModule.WebXR.OnStartVR(eyeCount, leftRect, rightRect);
10011009
}
1010+
this.gameModule.WebXR.OnVisibilityChange(session.visibilityState);
10021011
this.didNotifyUnity = true;
10031012
}
10041013
return this.didNotifyUnity;
@@ -1167,6 +1176,17 @@ Module['WebXR'].OnStartVR = function (views_count, left_rect, right_rect) {
11671176
right_rect.x, right_rect.y, right_rect.w, right_rect.h);
11681177
}
11691178

1179+
Module['WebXR'].OnVisibilityChange = function (visibility_state) {
1180+
this.OnVisibilityChangeInternal = this.OnVisibilityChangeInternal || Module.cwrap("on_visibility_change", null, ["number"]);
1181+
var visibility_state_int = 0;
1182+
if (visibility_state == "visible-blurred") {
1183+
visibility_state_int = 1;
1184+
} else if (visibility_state == "hidden") {
1185+
visibility_state_int = 2;
1186+
}
1187+
this.OnVisibilityChangeInternal(visibility_state_int);
1188+
}
1189+
11701190
Module['WebXR'].OnEndXR = function () {
11711191
Module.WebXR.isInXR = false;
11721192
this.OnEndXRInternal = this.OnEndXRInternal || Module.cwrap("on_end_xr", null, []);

Packages/webxr/Runtime/Scripts/WebXRManager.cs

Lines changed: 106 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,101 +3,125 @@
33
namespace WebXR
44
{
55
public enum WebXRState { VR, AR, NORMAL }
6+
public enum WebXRVisibilityState
7+
{
8+
VISIBLE,
9+
VISIBLE_BLURRED,
10+
HIDDEN
11+
}
612

713
[DefaultExecutionOrder(-2020)]
814
public class WebXRManager : SubsystemLifecycleManager<WebXRSubsystem, WebXRSubsystemDescriptor>
915
{
10-
public static WebXRManager Instance { get; private set; }
11-
12-
public WebXRState XRState => subsystem.xrState;
13-
14-
public static event WebXRSubsystem.XRCapabilitiesUpdate OnXRCapabilitiesUpdate
15-
{
16-
add => WebXRSubsystem.OnXRCapabilitiesUpdate += value;
17-
remove => WebXRSubsystem.OnXRCapabilitiesUpdate -= value;
18-
}
19-
20-
public static event WebXRSubsystem.XRChange OnXRChange
21-
{
22-
add => WebXRSubsystem.OnXRChange += value;
23-
remove => WebXRSubsystem.OnXRChange -= value;
24-
}
25-
26-
public static event WebXRSubsystem.ControllerUpdate OnControllerUpdate
27-
{
28-
add => WebXRSubsystem.OnControllerUpdate += value;
29-
remove => WebXRSubsystem.OnControllerUpdate -= value;
30-
}
31-
32-
public static event WebXRSubsystem.HandUpdate OnHandUpdate
33-
{
34-
add => WebXRSubsystem.OnHandUpdate += value;
35-
remove => WebXRSubsystem.OnHandUpdate -= value;
36-
}
37-
38-
public static event WebXRSubsystem.HeadsetUpdate OnHeadsetUpdate
16+
public static WebXRManager Instance { get; private set; }
17+
18+
public WebXRState XRState => subsystem.xrState;
19+
20+
public static event WebXRSubsystem.XRCapabilitiesUpdate OnXRCapabilitiesUpdate
21+
{
22+
add => WebXRSubsystem.OnXRCapabilitiesUpdate += value;
23+
remove => WebXRSubsystem.OnXRCapabilitiesUpdate -= value;
24+
}
25+
26+
public static event WebXRSubsystem.XRChange OnXRChange
27+
{
28+
add => WebXRSubsystem.OnXRChange += value;
29+
remove => WebXRSubsystem.OnXRChange -= value;
30+
}
31+
32+
public static event WebXRSubsystem.VisibilityChange OnVisibilityChange
33+
{
34+
add => WebXRSubsystem.OnVisibilityChange += value;
35+
remove => WebXRSubsystem.OnVisibilityChange -= value;
36+
}
37+
38+
public static event WebXRSubsystem.ControllerUpdate OnControllerUpdate
39+
{
40+
add => WebXRSubsystem.OnControllerUpdate += value;
41+
remove => WebXRSubsystem.OnControllerUpdate -= value;
42+
}
43+
44+
public static event WebXRSubsystem.HandUpdate OnHandUpdate
45+
{
46+
add => WebXRSubsystem.OnHandUpdate += value;
47+
remove => WebXRSubsystem.OnHandUpdate -= value;
48+
}
49+
50+
public static event WebXRSubsystem.HeadsetUpdate OnHeadsetUpdate
51+
{
52+
add => WebXRSubsystem.OnHeadsetUpdate += value;
53+
remove => WebXRSubsystem.OnHeadsetUpdate -= value;
54+
}
55+
56+
public static event WebXRSubsystem.HitTestUpdate OnViewerHitTestUpdate
57+
{
58+
add => WebXRSubsystem.OnViewerHitTestUpdate += value;
59+
remove => WebXRSubsystem.OnViewerHitTestUpdate -= value;
60+
}
61+
62+
public bool isSupportedAR
63+
{
64+
get
3965
{
40-
add => WebXRSubsystem.OnHeadsetUpdate += value;
41-
remove => WebXRSubsystem.OnHeadsetUpdate -= value;
66+
return subsystem.capabilities.canPresentAR;
4267
}
68+
}
4369

44-
public static event WebXRSubsystem.HitTestUpdate OnViewerHitTestUpdate
70+
public bool isSupportedVR
71+
{
72+
get
4573
{
46-
add => WebXRSubsystem.OnViewerHitTestUpdate += value;
47-
remove => WebXRSubsystem.OnViewerHitTestUpdate -= value;
48-
}
49-
50-
public bool isSupportedAR
51-
{
52-
get
53-
{
54-
return subsystem.capabilities.canPresentAR;
55-
}
74+
return subsystem.capabilities.canPresentVR;
5675
}
76+
}
5777

58-
public bool isSupportedVR
78+
public WebXRVisibilityState visibilityState
79+
{
80+
get
5981
{
60-
get
82+
if (subsystem == null)
6183
{
62-
return subsystem.capabilities.canPresentVR;
84+
return WebXRVisibilityState.VISIBLE;
6385
}
86+
return subsystem.visibilityState;
6487
}
65-
66-
public void ToggleAR()
67-
{
68-
subsystem.ToggleAR();
69-
}
70-
71-
public void ToggleVR()
72-
{
73-
subsystem.ToggleVR();
74-
}
75-
76-
public void HapticPulse(WebXRControllerHand hand, float intensity, float duration)
77-
{
78-
subsystem.HapticPulse(hand, intensity, duration);
79-
}
80-
81-
public void StartViewerHitTest()
82-
{
83-
subsystem.StartViewerHitTest();
84-
}
85-
86-
public void StopViewerHitTest()
87-
{
88-
subsystem.StopViewerHitTest();
89-
}
90-
91-
protected override void Awake()
92-
{
93-
base.Awake();
94-
Instance = this;
95-
enabled = subsystem != null;
96-
}
97-
98-
private void Update()
99-
{
100-
subsystem.OnUpdate();
101-
}
88+
}
89+
90+
public void ToggleAR()
91+
{
92+
subsystem?.ToggleAR();
93+
}
94+
95+
public void ToggleVR()
96+
{
97+
subsystem?.ToggleVR();
98+
}
99+
100+
public void HapticPulse(WebXRControllerHand hand, float intensity, float duration)
101+
{
102+
subsystem?.HapticPulse(hand, intensity, duration);
103+
}
104+
105+
public void StartViewerHitTest()
106+
{
107+
subsystem?.StartViewerHitTest();
108+
}
109+
110+
public void StopViewerHitTest()
111+
{
112+
subsystem?.StopViewerHitTest();
113+
}
114+
115+
protected override void Awake()
116+
{
117+
base.Awake();
118+
Instance = this;
119+
enabled = subsystem != null;
120+
}
121+
122+
private void Update()
123+
{
124+
subsystem.OnUpdate();
125+
}
102126
}
103127
}

0 commit comments

Comments
 (0)