forked from borismus/webvr-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webvr-manager.js
279 lines (245 loc) · 8.32 KB
/
webvr-manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Copyright 2015 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var ButtonManager = require('./button-manager.js');
var Emitter = require('./emitter.js');
var Modes = require('./modes.js');
var Util = require('./util.js');
/**
* Helper for getting in and out of VR mode.
*/
function WebVRManager(renderer, effect, params) {
this.params = params || {};
this.mode = Modes.UNKNOWN;
// Set option to hide the button.
this.hideButton = this.params.hideButton || false;
// Whether or not the FOV should be distorted or un-distorted. By default, it
// should be distorted, but in the case of vertex shader based distortion,
// ensure that we use undistorted parameters.
this.predistorted = !!this.params.predistorted;
// Save the THREE.js renderer and effect for later.
this.renderer = renderer;
this.effect = effect;
var polyfillWrapper = document.querySelector('.webvr-polyfill-fullscreen-wrapper');
this.button = new ButtonManager(polyfillWrapper);
this.isFullscreenDisabled = !!Util.getQueryParameter('no_fullscreen');
this.startMode = Modes.NORMAL;
var startModeParam = parseInt(Util.getQueryParameter('start_mode'));
if (!isNaN(startModeParam)) {
this.startMode = startModeParam;
}
if (this.hideButton) {
this.button.setVisibility(false);
}
// Check if the browser is compatible with WebVR.
this.getDeviceByType_(VRDisplay).then(function(hmd) {
this.hmd = hmd;
// Only enable VR mode if there's a VR device attached or we are running the
// polyfill on mobile.
if (!this.isVRCompatibleOverride) {
this.isVRCompatible = !hmd.isPolyfilled || Util.isMobile();
}
switch (this.startMode) {
case Modes.MAGIC_WINDOW:
this.setMode_(Modes.MAGIC_WINDOW);
break;
case Modes.VR:
this.enterVRMode_();
this.setMode_(Modes.VR);
break;
default:
this.setMode_(Modes.NORMAL);
}
this.emit('initialized');
}.bind(this));
// Hook up button listeners.
this.button.on('fs', this.onFSClick_.bind(this));
this.button.on('vr', this.onVRClick_.bind(this));
// Bind to fullscreen events.
document.addEventListener('webkitfullscreenchange',
this.onFullscreenChange_.bind(this));
document.addEventListener('mozfullscreenchange',
this.onFullscreenChange_.bind(this));
document.addEventListener('msfullscreenchange',
this.onFullscreenChange_.bind(this));
// Bind to VR* specific events.
window.addEventListener('vrdisplaypresentchange',
this.onVRDisplayPresentChange_.bind(this));
window.addEventListener('vrdisplaydeviceparamschange',
this.onVRDisplayDeviceParamsChange_.bind(this));
}
WebVRManager.prototype = new Emitter();
// Expose these values externally.
WebVRManager.Modes = Modes;
WebVRManager.prototype.render = function(scene, camera, timestamp) {
// Scene may be an array of two scenes, one for each eye.
if (scene instanceof Array) {
this.effect.render(scene[0], camera);
} else {
this.effect.render(scene, camera);
}
};
WebVRManager.prototype.setVRCompatibleOverride = function(isVRCompatible) {
this.isVRCompatible = isVRCompatible;
this.isVRCompatibleOverride = true;
// Don't actually change modes, just update the buttons.
this.button.setMode(this.mode, this.isVRCompatible);
};
WebVRManager.prototype.setFullscreenCallback = function(callback) {
this.fullscreenCallback = callback;
};
WebVRManager.prototype.setVRCallback = function(callback) {
this.vrCallback = callback;
};
WebVRManager.prototype.setExitFullscreenCallback = function(callback) {
this.exitFullscreenCallback = callback;
}
/**
* Promise returns true if there is at least one HMD device available.
*/
WebVRManager.prototype.getDeviceByType_ = function(type) {
return new Promise(function(resolve, reject) {
navigator.getVRDisplays().then(function(displays) {
// Promise succeeds, but check if there are any displays actually.
for (var i = 0; i < displays.length; i++) {
if (displays[i] instanceof type) {
resolve(displays[i]);
break;
}
}
resolve(null);
}, function() {
// No displays are found.
resolve(null);
});
});
};
/**
* Helper for entering VR mode.
*/
WebVRManager.prototype.enterVRMode_ = function() {
this.hmd.requestPresent([{
source: this.renderer.domElement,
predistorted: this.predistorted
}]);
};
WebVRManager.prototype.setMode_ = function(mode) {
var oldMode = this.mode;
if (mode == this.mode) {
console.warn('Not changing modes, already in %s', mode);
return;
}
// console.log('Mode change: %s => %s', this.mode, mode);
this.mode = mode;
this.button.setMode(mode, this.isVRCompatible);
// Emit an event indicating the mode changed.
this.emit('modechange', mode, oldMode);
};
/**
* Main button was clicked.
*/
WebVRManager.prototype.onFSClick_ = function() {
switch (this.mode) {
case Modes.NORMAL:
// TODO: Remove this hack if/when iOS gets real fullscreen mode.
// If this is an iframe on iOS, break out and open in no_fullscreen mode.
if (Util.isIOS() && Util.isIFrame()) {
if (this.fullscreenCallback) {
this.fullscreenCallback();
} else {
var url = window.location.href;
url = Util.appendQueryParameter(url, 'no_fullscreen', 'true');
url = Util.appendQueryParameter(url, 'start_mode', Modes.MAGIC_WINDOW);
top.location.href = url;
return;
}
}
this.setMode_(Modes.MAGIC_WINDOW);
this.requestFullscreen_();
break;
case Modes.MAGIC_WINDOW:
if (this.isFullscreenDisabled) {
window.history.back();
return;
}
if (this.exitFullscreenCallback) {
this.exitFullscreenCallback();
}
this.setMode_(Modes.NORMAL);
this.exitFullscreen_();
break;
}
};
/**
* The VR button was clicked.
*/
WebVRManager.prototype.onVRClick_ = function() {
// TODO: Remove this hack when iOS has fullscreen mode.
// If this is an iframe on iOS, break out and open in no_fullscreen mode.
if (this.mode == Modes.NORMAL && Util.isIOS() && Util.isIFrame()) {
if (this.vrCallback) {
this.vrCallback();
} else {
var url = window.location.href;
url = Util.appendQueryParameter(url, 'no_fullscreen', 'true');
url = Util.appendQueryParameter(url, 'start_mode', Modes.VR);
top.location.href = url;
return;
}
}
this.enterVRMode_();
};
WebVRManager.prototype.requestFullscreen_ = function() {
var canvas = document.body;
//var canvas = this.renderer.domElement;
if (canvas.requestFullscreen) {
canvas.requestFullscreen();
} else if (canvas.mozRequestFullScreen) {
canvas.mozRequestFullScreen();
} else if (canvas.webkitRequestFullscreen) {
canvas.webkitRequestFullscreen();
} else if (canvas.msRequestFullscreen) {
canvas.msRequestFullscreen();
}
};
WebVRManager.prototype.exitFullscreen_ = function() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
};
WebVRManager.prototype.onVRDisplayPresentChange_ = function(e) {
console.log('onVRDisplayPresentChange_', e);
if (this.hmd.isPresenting) {
this.setMode_(Modes.VR);
} else {
this.setMode_(Modes.NORMAL);
}
};
WebVRManager.prototype.onVRDisplayDeviceParamsChange_ = function(e) {
console.log('onVRDisplayDeviceParamsChange_', e);
};
WebVRManager.prototype.onFullscreenChange_ = function(e) {
// If we leave full-screen, go back to normal mode.
if (document.webkitFullscreenElement === null ||
document.mozFullScreenElement === null) {
this.setMode_(Modes.NORMAL);
}
};
module.exports = WebVRManager;