Skip to content

Commit e3b9cc7

Browse files
authored
Merge pull request #6575 from DougReeder/pdf-update-vr
Fixes bug: PDF page advance doesn't work in mobile VR
2 parents 55ae9ec + b4eabe9 commit e3b9cc7

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

src/systems/hubs-systems.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { AudioZonesSystem } from "./audio-zones-system";
3030
import { GainSystem } from "./audio-gain-system";
3131
import { EnvironmentSystem } from "./environment-system";
3232
import { NameTagVisibilitySystem } from "./name-tag-visibility-system";
33+
import { MediaPDFOculusFix } from "./media-pdf-oculus-fix";
3334

3435
// new world
3536
import { networkReceiveSystem } from "../bit-systems/network-receive-system";
@@ -136,6 +137,7 @@ AFRAME.registerSystem("hubs-systems", {
136137
this.drawingMenuSystem = new DrawingMenuSystem(this.el);
137138
this.characterController = new CharacterControllerSystem(this.el);
138139
this.waypointSystem = new WaypointSystem(this.el, this.characterController);
140+
this.mediaPDFOculusFix = new MediaPDFOculusFix(this.el);
139141
this.cursorPoseTrackingSystem = new CursorPoseTrackingSystem();
140142
this.menuAnimationSystem = new MenuAnimationSystem();
141143
this.audioSettingsSystem = new AudioSettingsSystem(this.el);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
PDFjs which is used to render pdfs depends on the window.requestAnimationFrame function.
3+
This is not called when in mobileVR.
4+
This system replaces the function with the requestAnimationFrame of the xrSession.
5+
It restores window.requestAnimationFrame when the user leaves xr.
6+
*/
7+
export class MediaPDFOculusFix {
8+
constructor(sceneEl) {
9+
// PC VR calls window.requestAnimationFrame even in VR mode
10+
if (AFRAME.utils.device.isMobileVR()) {
11+
this.requestAnimationFrameOriginal = window.requestAnimationFrame;
12+
this.cancelAnimationFrameOriginal = window.cancelAnimationFrame;
13+
sceneEl.addEventListener("enter-vr", () => {
14+
console.debug(`PDF: monkey-patching window.requestAnimationFrame:`, window.requestAnimationFrame);
15+
const { xrSession } = sceneEl;
16+
window.requestAnimationFrame = callback => {
17+
return xrSession?.requestAnimationFrame(callback);
18+
};
19+
window.cancelAnimationFrame = handle => {
20+
return xrSession?.cancelAnimationFrame(handle);
21+
};
22+
});
23+
sceneEl.addEventListener("exit-vr", () => {
24+
console.debug(`PDF: removing window.requestAnimationFrame monkey-patch`, this.requestAnimationFrameOriginal);
25+
window.requestAnimationFrame = this.requestAnimationFrameOriginal;
26+
window.cancelAnimationFrame = this.cancelAnimationFrameOriginal;
27+
});
28+
}
29+
}
30+
}

types/aframe.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ declare module "aframe" {
5252
drawingMenuSystem: DrawingMenuSystem;
5353
characterController: CharacterControllerSystem;
5454
waypointSystem: WaypointSystem;
55+
mediaPDFOculusFix: MediaPDFOculusFix;
5556
cursorPoseTrackingSystem: CursorPoseTrackingSystem;
5657
menuAnimationSystem: MenuAnimationSystem;
5758
audioSettingsSystem: AudioSettingsSystem;

0 commit comments

Comments
 (0)