Skip to content

Commit 32f62a3

Browse files
spitemrdoob
authored andcommitted
Decouple navigator.getVRDisplays from VREffect/VRControls (mrdoob#9771)
* added listDisplays * updated vreffect and cubes example * updated controls * merged getDisplays with listDisplays * switched to setDisplay/getDisplay * updated example
1 parent 79f4f7d commit 32f62a3

File tree

4 files changed

+27
-60
lines changed

4 files changed

+27
-60
lines changed

examples/js/controls/VRControls.js

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ THREE.VRControls = function ( object, onError ) {
77

88
var scope = this;
99

10-
var vrDisplay, vrDisplays;
10+
var vrDisplay;
1111

1212
var standingMatrix = new THREE.Matrix4();
1313

@@ -16,28 +16,6 @@ THREE.VRControls = function ( object, onError ) {
1616
frameData = new VRFrameData();
1717
}
1818

19-
function gotVRDisplays( displays ) {
20-
21-
vrDisplays = displays;
22-
23-
if ( displays.length > 0 ) {
24-
25-
vrDisplay = displays[ 0 ];
26-
27-
} else {
28-
29-
if ( onError ) onError( 'VR input not available.' );
30-
31-
}
32-
33-
}
34-
35-
if ( navigator.getVRDisplays ) {
36-
37-
navigator.getVRDisplays().then( gotVRDisplays );
38-
39-
}
40-
4119
// the Rift SDK returns the position in meters
4220
// this scale factor allows the user to define how meters
4321
// are converted to scene units.
@@ -52,15 +30,15 @@ THREE.VRControls = function ( object, onError ) {
5230
// standing=true but the VRDisplay doesn't provide stageParameters.
5331
this.userHeight = 1.6;
5432

55-
this.getVRDisplay = function () {
33+
this.setDisplay = function ( display ) {
5634

57-
return vrDisplay;
35+
vrDisplay = display;
5836

5937
};
6038

61-
this.getVRDisplays = function () {
39+
this.getDisplay = function () {
6240

63-
return vrDisplays;
41+
return vrDisplay;
6442

6543
};
6644

examples/js/effects/VREffect.js

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
*
1010
*/
1111

12-
THREE.VREffect = function ( renderer, onError ) {
12+
THREE.VREffect = function ( renderer ) {
13+
14+
var vrDisplay;
1315

14-
var vrDisplay, vrDisplays;
1516
var eyeTranslationL = new THREE.Vector3();
1617
var eyeTranslationR = new THREE.Vector3();
1718
var renderRectL, renderRectR;
@@ -23,28 +24,6 @@ THREE.VREffect = function ( renderer, onError ) {
2324

2425
}
2526

26-
function gotVRDisplays( displays ) {
27-
28-
vrDisplays = displays;
29-
30-
if ( displays.length > 0 ) {
31-
32-
vrDisplay = displays[ 0 ];
33-
34-
} else {
35-
36-
if ( onError ) onError( 'HMD not available' );
37-
38-
}
39-
40-
}
41-
42-
if ( navigator.getVRDisplays ) {
43-
44-
navigator.getVRDisplays().then( gotVRDisplays );
45-
46-
}
47-
4827
//
4928

5029
this.isPresenting = false;
@@ -56,15 +35,15 @@ THREE.VREffect = function ( renderer, onError ) {
5635
var rendererUpdateStyle = false;
5736
var rendererPixelRatio = renderer.getPixelRatio();
5837

59-
this.getVRDisplay = function () {
38+
this.setDisplay = function ( display ) {
6039

61-
return vrDisplay;
40+
vrDisplay = display;
6241

6342
};
6443

65-
this.getVRDisplays = function () {
44+
this.getDisplay = function () {
6645

67-
return vrDisplays;
46+
return vrDisplay;
6847

6948
};
7049

examples/js/vr/WebVR.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ var WEBVR = {
100100

101101
return button;
102102

103+
},
104+
105+
getDisplays: function() {
106+
107+
return navigator.getVRDisplays();
108+
103109
}
104110

105111
};

examples/webvr_cubes.html

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,15 @@
130130
controls = new THREE.VRControls( camera );
131131
effect = new THREE.VREffect( renderer );
132132

133-
if ( WEBVR.isAvailable() === true ) {
134-
135-
document.body.appendChild( WEBVR.getButton( effect ) );
136-
137-
}
133+
WEBVR.getDisplays()
134+
.then( function( displays ) {
135+
effect.setDisplay( displays[ 0 ] );
136+
controls.setDisplay( displays[ 0 ] );
137+
document.body.appendChild( WEBVR.getButton( effect ) );
138+
} )
139+
.catch( function() {
140+
// no displays
141+
} );
138142

139143
renderer.domElement.addEventListener( 'mousedown', onMouseDown, false );
140144
renderer.domElement.addEventListener( 'mouseup', onMouseUp, false );

0 commit comments

Comments
 (0)