-
Notifications
You must be signed in to change notification settings - Fork 1
/
webvr.js
275 lines (217 loc) · 6.2 KB
/
webvr.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
(function(){
var runtime = null;
var rtLeft, rtRight;
var _renderScale = 0.5; // 1
var vrHMD = null;
var frameData = null;
if ('VRFrameData' in window) {
frameData = new VRFrameData();
}
var WebVRSupport = {};
window.WebVRSupport = WebVRSupport;
// defaults element IDs/defs, normally always passed
var _viewpoint = "viewpoint";
var _background = "background";
var _scene = "scene";
var _x3dEl = "x3d-elem";
var viewpoint;
var _initialPosition;
/*
options
*/
function _initialize( options ) {
_log('Initialize WebVR support');
if (options.viewpoint)
_viewpoint = options.viewpoint;
if (options.background)
_background = options.background;
if (options.scene)
_scene = options.scene;
if (options.renderScale)
_renderScale = options.renderScale;
if (options.x3dEl)
_x3dEl = options.x3dEl;
if (document.readyState === 'complete') {
load();
} else {
window.addEventListener('load', function ld(){
window.removeEventListener('load', ld);
load();
});
}
}
WebVRSupport.initialize = _initialize;
function load() {
_log('Load external webvr.x3d dependency');
viewpoint = document.getElementById(_viewpoint);
if (viewpoint === null) {
console.log('viewpoint ID: ' + _viewpoint + ' not found');
return;
}
_initialPosition = viewpoint.getFieldValue('position');
var xhr = new XMLHttpRequest();
xhr.open('GET', 'webvr.x3d');
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
var text = xhr.responseText;
text = text.replace(/\$VIEWPOINT/g, _viewpoint);
text = text.replace(/\$BACKGROUND/g, _background);
text = text.replace(/\$SCENE/g, _scene);
var node = document.createElement('group');
node.innerHTML = text;
var scene = document.getElementById('scene');
scene.appendChild(node);
init();
} else {
_log('error: could not load webvr.x3d');
}
}
}
xhr.send();
}
function init() {
runtime = document.getElementById(_x3dEl).runtime;
var ns = "";
ns = "Webvr__";
rtLeft = document.getElementById(ns+'rtLeft');
rtRight = document.getElementById(ns+'rtRight');
disableControls();
//runtime.enterFrame = enterFrame;
requestAnimationFrame(enterFrame);
// update viewpoint based on HMD pose
function enterFrame() {
if (!vrHMD) {
window.requestAnimationFrame(enterFrame);
return;
} else {
vrHMD.requestAnimationFrame(enterFrame); // native framerate if presenting
}
var state = getPose(vrHMD);
if (state.orientation !== null) {
var o = state.orientation;
var ori = viewpoint.requestFieldRef('orientation');
ori.x = o[0];
ori.y = o[1];
ori.z = o[2];
ori.w = o[3];
viewpoint.releaseFieldRef('orientation');
}
if (state.position !== null) {
var p = state.position;
var posi = viewpoint.requestFieldRef('position');
posi.x = _initialPosition.x + p[0];
posi.y = _initialPosition.y + p[1];
posi.z = _initialPosition.z + p[2];
viewpoint.releaseFieldRef('position');
}
if (vrHMD.isPresenting) {
vrHMD.submitFrame();
}
};
runtime.exitFrame = function() {
return; // temp
runtime.triggerRedraw();
};
if (isWebVRSupported()) {
navigator.getVRDisplays().then(vrDisplayCallback);
} else {
console.error('No WebVR 1.0 support');
}
// inject enter/exit VR button
var enterVRBtn = document.createElement('button');
enterVRBtn.setAttribute('id', 'enter-vr-btn');
document.body.appendChild(enterVRBtn);
enterVRBtn.addEventListener('click', function(event){
enterVR();
});
};
function isWebVRSupported() {
return ('getVRDisplays' in navigator);
}
function getPose(vrDisplay) {
var pose = null;
if (vrDisplay.getFrameData) { // '1.1'
vrDisplay.getFrameData(frameData);
pose = frameData.pose;
} else if (vrDisplay.getPose) { // deprecated
pose = vrDisplay.getPose();
}
return pose;
}
// toggles
function enterVR() {
if (!vrHMD) {
_log('No VR headset attached');
return;
}
if (!vrHMD.isPresenting) {
var canvas = document.getElementsByTagName("canvas")[0];
vrHMD.requestPresent( [ { source: canvas } ] )
.then(function(){
_log('Started VR presenting');
});
} else {
vrHMD.exitPresent().then(function(){
_log('Exited VR presenting');
});
}
}
WebVRSupport.enterVR = enterVR;
function vrDisplayCallback(vrdisplays) {
if (vrdisplays.length) {
vrHMD = vrdisplays[0];
_log(vrHMD);
} else {
_log('NO VRDisplay found');
alert("Didn't find a VR display!");
return;
}
var leftEyeParams, rightEyeParams,
leftFOV, rightFOV,
leftTranslation, rightTranslation;
leftEyeParams = vrHMD.getEyeParameters("left");
rightEyeParams = vrHMD.getEyeParameters("right");
_log(leftEyeParams);
_log(rightEyeParams);
leftFOV = leftEyeParams.fieldOfView;
rightFOV = rightEyeParams.fieldOfView;
_log(leftFOV);
_log(rightFOV);
// TODO: use to updated views
// -currently using default in x3dom: 0.064 IPD
leftTranslation = leftEyeParams.offset;
rightTranslation = rightEyeParams.offset;
_log(leftTranslation);
_log(rightTranslation);
//modifyRTs(leftEyeParams, rightEyeParams);
}
function modifyRTs(lEyeParams, rEyeParams) {
var lW = Math.round(lEyeParams.renderWidth * _renderScale);
var lH = Math.round(lEyeParams.renderHeight * _renderScale);
var rW = Math.round(rEyeParams.renderWidth * _renderScale);
var rH = Math.round(rEyeParams.renderHeight * _renderScale);
var color_depth = '4';
rtLeft.setAttribute('dimensions', lW + ' ' + lH + ' ' + color_depth);
rtRight.setAttribute('dimensions', rH + ' ' + rH + ' ' + color_depth);
}
var _navType = "";
function disableControls() {
var navs = document.getElementsByTagName('navigationInfo');
if (navs.length) {
_navType = navs[0].getAttribute('type');
navs[0].setAttribute('type', '');
}
}
function enableControls() {
var navs = document.getElementsByTagName('navigationInfo');
if (navs.length) {
navs[0].setAttribute('type', _navType);
}
}
var enableLogging = true;
function _log() {
if (!enableLogging) return;
console.log.apply(console, arguments);
}
})();