Skip to content

Commit f36cfe6

Browse files
committed
Merge pull request mrdoob#5020 from dmarcos/chromeVRAPI
Adds Chrome VR API support to VREffect / VRControls
2 parents 16f8af7 + 3bdc305 commit f36cfe6

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

examples/js/controls/VRControls.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ THREE.VRControls = function ( camera, done ) {
88

99
this._init = function () {
1010
var self = this;
11-
if ( !navigator.mozGetVRDevices ) {
11+
if ( !navigator.mozGetVRDevices && !navigator.getVRDevices ) {
1212
if ( done ) {
1313
done("Your browser is not VR Ready");
1414
}
1515
return;
1616
}
17-
navigator.mozGetVRDevices( gotVRDevices );
17+
if ( navigator.getVRDevices ) {
18+
navigator.getVRDevices().then( gotVRDevices );
19+
} else {
20+
navigator.mozGetVRDevices( gotVRDevices );
21+
}
1822
function gotVRDevices( devices ) {
1923
var vrInput;
2024
var error;

examples/js/effects/VREffect.js

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,41 @@
22
* @author dmarcos / https://github.com/dmarcos
33
*
44
* It handles stereo rendering
5-
* If mozGetVRDevices API is not available it gracefuly falls back to a
5+
* If mozGetVRDevices and getVRDevices APIs are not available it gracefuly falls back to a
66
* regular renderer
77
*
88
* The HMD supported is the Oculus DK1 and The Web API doesn't currently allow
9-
* to query for the display resolution. The dimensions of the screen are temporarly
10-
* hardcoded (1280 x 800).
9+
* to query for the display resolution (only the chrome API allows it).
10+
* The dimensions of the screen are temporarly hardcoded (1280 x 800).
1111
*
12-
* For VR mode to work it has to be used with the Oculus enabled builds of Firefox:
12+
* For VR mode to work it has to be used with the Oculus enabled builds of Firefox or Chrome:
13+
*
14+
* Firefox:
1315
*
1416
* OSX: http://people.mozilla.com/~vladimir/vr/firefox-33.0a1.en-US.mac.dmg
1517
* WIN: http://people.mozilla.com/~vladimir/vr/firefox-33.0a1.en-US.win64-x86_64.zip
1618
*
19+
* Chrome builds:
20+
*
21+
* https://drive.google.com/folderview?id=0BzudLt22BqGRbW9WTHMtOWMzNjQ&usp=sharing#list
22+
*
1723
*/
1824
THREE.VREffect = function ( renderer, done ) {
19-
2025
this._renderer = renderer;
2126

2227
this._init = function() {
2328
var self = this;
24-
if ( !navigator.mozGetVRDevices ) {
29+
if ( !navigator.mozGetVRDevices && !navigator.getVRDevices ) {
2530
if ( done ) {
2631
done("Your browser is not VR Ready");
2732
}
2833
return;
2934
}
30-
navigator.mozGetVRDevices( gotVRDevices );
35+
if ( navigator.getVRDevices ) {
36+
navigator.getVRDevices().then( gotVRDevices );
37+
} else {
38+
navigator.mozGetVRDevices( gotVRDevices );
39+
}
3140
function gotVRDevices( devices ) {
3241
var vrHMD;
3342
var error;
@@ -130,22 +139,30 @@ THREE.VREffect = function ( renderer, done ) {
130139
width: renderer.domElement.width,
131140
height: renderer.domElement.height
132141
};
133-
fullScreen = true;
134142
// Hardcoded Rift display size
135-
renderer.setSize( 1280, 800 );
136-
vrHMD.xxxToggleElementVR( renderer.domElement );
137-
this.startFullscreen( vrHMD );
143+
renderer.setSize( 1280, 800, false );
144+
this.startFullscreen();
138145
};
139146

140-
this.startFullscreen = function( vrHMD ) {
147+
this.startFullscreen = function() {
141148
var self = this;
142149
var renderer = this._renderer;
143-
document.addEventListener( "mozfullscreenchange", function() {
144-
if ( !document.mozFullScreenElement ) {
150+
var vrHMD = this._vrHMD;
151+
var canvas = renderer.domElement;
152+
var fullScreenChange =
153+
canvas.mozRequestFullScreen? 'mozfullscreenchange' : 'webkitfullscreenchange';
154+
155+
document.addEventListener( fullScreenChange, onFullScreenChanged, false );
156+
function onFullScreenChanged() {
157+
if ( !document.mozFullScreenElement && !document.webkitFullScreenElement ) {
145158
self.setFullScreen( false );
146159
}
147-
},false );
148-
renderer.domElement.mozRequestFullScreen( { vrDisplay: vrHMD } );
160+
}
161+
if ( canvas.mozRequestFullScreen ) {
162+
canvas.mozRequestFullScreen( { vrDisplay: vrHMD } );
163+
} else {
164+
canvas.webkitRequestFullscreen( { vrDisplay: vrHMD } );
165+
}
149166
};
150167

151168
this.FovToNDCScaleOffset = function( fov ) {

examples/webgl_effects_vr.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
var camera, scene, projector, raycaster, renderer;
4848
var vrEffect;
4949
var vrControls;
50+
var fullScreenButton = document.querySelector( '.button' );
5051

5152
var mouse = new THREE.Vector2(), INTERSECTED;
5253
var radius = 100, theta = 0;
@@ -105,14 +106,14 @@
105106
raycaster = new THREE.Raycaster();
106107

107108
renderer = new THREE.WebGLRenderer();
109+
108110
var fullScreenButton = document.querySelector( '.button' );
109111
fullScreenButton.onclick = function() {
110112
vrEffect.setFullScreen( true );
111113
};
112114

113115
vrEffect = new THREE.VREffect(renderer, VREffectLoaded);
114116
vrControls = new THREE.VRControls(camera);
115-
116117
function VREffectLoaded(error) {
117118
if (error) {
118119
fullScreenButton.innerHTML = error;

0 commit comments

Comments
 (0)