Skip to content

Commit e3c1d33

Browse files
committed
Hack to make sure the window.rAF last frame won't stuck when entering AR session after permission popup in Chrome for Android
1 parent 08d13ae commit e3c1d33

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

Assets/WebGLTemplates/WebXR/webxr.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
this.isVRSupported = false;
5151
this.rAFCB = null;
5252
this.onInputEvent = null;
53+
this.waitingHandheldARHack = false;
5354
this.init();
5455
}
5556

@@ -94,16 +95,33 @@
9495

9596
XRManager.prototype.onRequestARSession = function () {
9697
if (!this.isARSupported) return;
98+
// The window on Chrome for Android lose focus when asking permissions.
99+
// A popup is opened and the Canvas is painted with the last frame.
100+
// We want to make sure that the Canvas is transparent when entering Handheld AR Session.
101+
this.waitingHandheldARHack = true;
102+
var thisXRMananger = this;
103+
var tempRender = function () {
104+
thisXRMananger.ctx.clearColor(0, 0, 0, 0);
105+
thisXRMananger.ctx.clear(thisXRMananger.ctx.COLOR_BUFFER_BIT | thisXRMananger.ctx.DEPTH_BUFFER_BIT);
106+
if (thisXRMananger.waitingHandheldARHack)
107+
{
108+
window.requestAnimationFrame( tempRender );
109+
}
110+
}
111+
window.requestAnimationFrame( tempRender );
97112
navigator.xr.requestSession('immersive-ar', {
98113
requiredFeatures: ['local-floor'], // TODO: Get this value from Unity
99114
optionalFeatures: ['dom-overlay'],
100-
domOverlay: {root: this.canvas}
115+
domOverlay: {root: this.canvas.parentElement}
101116
}).then(async (session) => {
117+
this.waitingHandheldARHack = false;
102118
session.isImmersive = true;
103119
session.isInSession = true;
104120
session.isAR = true;
105121
this.arSession = session;
106122
this.onSessionStarted(session);
123+
}).catch((error) => {
124+
thisXRMananger.waitingHandheldARHack = false;
107125
});
108126
}
109127

@@ -255,7 +273,9 @@
255273
func(time);
256274
});
257275
} else {
258-
window.requestAnimationFrame(func);
276+
if (!thisXRMananger.waitingHandheldARHack) {
277+
window.requestAnimationFrame(func);
278+
}
259279
}
260280
};
261281

Assets/WebGLTemplates/WebXRFullView/webxr.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
this.isVRSupported = false;
5151
this.rAFCB = null;
5252
this.onInputEvent = null;
53+
this.waitingHandheldARHack = false;
5354
this.init();
5455
}
5556

@@ -94,16 +95,33 @@
9495

9596
XRManager.prototype.onRequestARSession = function () {
9697
if (!this.isARSupported) return;
98+
// The window on Chrome for Android lose focus when asking permissions.
99+
// A popup is opened and the Canvas is painted with the last frame.
100+
// We want to make sure that the Canvas is transparent when entering Handheld AR Session.
101+
this.waitingHandheldARHack = true;
102+
var thisXRMananger = this;
103+
var tempRender = function () {
104+
thisXRMananger.ctx.clearColor(0, 0, 0, 0);
105+
thisXRMananger.ctx.clear(thisXRMananger.ctx.COLOR_BUFFER_BIT | thisXRMananger.ctx.DEPTH_BUFFER_BIT);
106+
if (thisXRMananger.waitingHandheldARHack)
107+
{
108+
window.requestAnimationFrame( tempRender );
109+
}
110+
}
111+
window.requestAnimationFrame( tempRender );
97112
navigator.xr.requestSession('immersive-ar', {
98113
requiredFeatures: ['local-floor'], // TODO: Get this value from Unity
99114
optionalFeatures: ['dom-overlay'],
100-
domOverlay: {root: this.canvas}
115+
domOverlay: {root: this.canvas.parentElement}
101116
}).then(async (session) => {
117+
this.waitingHandheldARHack = false;
102118
session.isImmersive = true;
103119
session.isInSession = true;
104120
session.isAR = true;
105121
this.arSession = session;
106122
this.onSessionStarted(session);
123+
}).catch((error) => {
124+
thisXRMananger.waitingHandheldARHack = false;
107125
});
108126
}
109127

@@ -255,7 +273,9 @@
255273
func(time);
256274
});
257275
} else {
258-
window.requestAnimationFrame(func);
276+
if (!thisXRMananger.waitingHandheldARHack) {
277+
window.requestAnimationFrame(func);
278+
}
259279
}
260280
};
261281

Build/webxr.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
this.isVRSupported = false;
5151
this.rAFCB = null;
5252
this.onInputEvent = null;
53+
this.waitingHandheldARHack = false;
5354
this.init();
5455
}
5556

@@ -94,16 +95,33 @@
9495

9596
XRManager.prototype.onRequestARSession = function () {
9697
if (!this.isARSupported) return;
98+
// The window on Chrome for Android lose focus when asking permissions.
99+
// A popup is opened and the Canvas is painted with the last frame.
100+
// We want to make sure that the Canvas is transparent when entering Handheld AR Session.
101+
this.waitingHandheldARHack = true;
102+
var thisXRMananger = this;
103+
var tempRender = function () {
104+
thisXRMananger.ctx.clearColor(0, 0, 0, 0);
105+
thisXRMananger.ctx.clear(thisXRMananger.ctx.COLOR_BUFFER_BIT | thisXRMananger.ctx.DEPTH_BUFFER_BIT);
106+
if (thisXRMananger.waitingHandheldARHack)
107+
{
108+
window.requestAnimationFrame( tempRender );
109+
}
110+
}
111+
window.requestAnimationFrame( tempRender );
97112
navigator.xr.requestSession('immersive-ar', {
98113
requiredFeatures: ['local-floor'], // TODO: Get this value from Unity
99114
optionalFeatures: ['dom-overlay'],
100-
domOverlay: {root: this.canvas}
115+
domOverlay: {root: this.canvas.parentElement}
101116
}).then(async (session) => {
117+
this.waitingHandheldARHack = false;
102118
session.isImmersive = true;
103119
session.isInSession = true;
104120
session.isAR = true;
105121
this.arSession = session;
106122
this.onSessionStarted(session);
123+
}).catch((error) => {
124+
thisXRMananger.waitingHandheldARHack = false;
107125
});
108126
}
109127

@@ -255,7 +273,9 @@
255273
func(time);
256274
});
257275
} else {
258-
window.requestAnimationFrame(func);
276+
if (!thisXRMananger.waitingHandheldARHack) {
277+
window.requestAnimationFrame(func);
278+
}
259279
}
260280
};
261281

0 commit comments

Comments
 (0)