forked from tparisi/WebVR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apibasics.js
100 lines (83 loc) · 3.44 KB
/
apibasics.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
myVRApp.prototype.queryVRDevices = function() {
// Polyfill - hide FF/Webkit differences
var getVRDevices = navigator.mozGetVRDevices /* FF */ ||
navigator.getVRDevices; /* webkit */
if (!getVRDevices) {
// handle error here, either via throwing an exception or
// calling an error callback
}
var self = this;
getVRDevices().then( gotVRDevices );
function gotVRDevices( devices ) {
// Look for HMDVRDevice (display) first
var vrHMD;
var error;
for ( var i = 0; i < devices.length; ++i ) {
if ( devices[i] instanceof HMDVRDevice ) {
vrHMD = devices[i];
self._vrHMD = vrHMD;
if ( vrHMD.getEyeParameters ) {
self.left = vrHMD.getEyeParameters( "left" );
self.right = vrHMD.getEyeParameters( "right" );
}
else {
self.left = {
renderRect: vrHMD.getRecommendedEyeRenderRect( "left" ),
eyeTranslation: vrHMD.getEyeTranslation( "left" ),
recommendedFieldOfView: vrHMD.getRecommendedEyeFieldOfView(
"left" )
};
self.right = {
renderRect: vrHMD.getRecommendedEyeRenderRect( "right" ),
eyeTranslation: vrHMD.getEyeTranslation( "right" ),
recommendedFieldOfView: vrHMD.getRecommendedEyeFieldOfView(
"right" )
};
}
self.leftEyeTranslation = vrHMD.getEyeTranslation( "left" );
self.rightEyeTranslation = vrHMD.getEyeTranslation( "right" );
self.leftEyeFOV = vrHMD.getRecommendedEyeFieldOfView( "left" );
self.rightEyeFOV = vrHMD.getRecommendedEyeFieldOfView( "right" );
break; // We keep the first we encounter
}
}
// Now look for PositionSensorVRDevice (head tracking)
var vrInput;
var error;
for ( var i = 0; i < devices.length; ++i ) {
if ( devices[i] instanceof PositionSensorVRDevice ) {
vrInput = devices[i]
self._vrInput = vrInput;
break; // We keep the first we encounter
}
}
}
}
myVRApp.prototype.goFullScreen = function() {
var vrHMD = this._vrHMD;
// this._canvas is an HTML5 canvas element
var canvas = this._canvas;
// Polyfill - hide FF/Webkit differences
function requestFullScreen() {
if ( canvas.mozRequestFullScreen ) {
canvas.mozRequestFullScreen( { vrDisplay: vrHMD } );
} else {
canvas.webkitRequestFullscreen( { vrDisplay: vrHMD } );
}
}
requestFullScreen();
}
myVRApp.prototype.update = function() {
var vrInput = this._vrInput;
var vrState = vrInput.getState();
if ( !vrState ) {
return;
}
// Update the camera's position and orientation
if ( vrState.position !== null ) {
this.setCameraPosition( vrState.position );
}
if ( vrState.orientation !== null ) {
this.setCameraOrientation( vrState.orientation );
}
}