-
Notifications
You must be signed in to change notification settings - Fork 96
/
rift_cube_demo.js
468 lines (402 loc) · 11.9 KB
/
rift_cube_demo.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
"use strict";
(function(global) {
var tmpVec3 = vec3.create();
var tmpMat4 = mat4.create();
/**
* Simple camera.
* @constructor
*/
var Camera = function() {
/**
* Movement speed, in m/s.
* @type {number}
*/
this.moveSpeed = 3;
/**
* Current eye position.
* @type {!vec3}
*/
this.eyePosition = vec3.fromValues(0, 0, 0);
/**
* Yaw.
* @type {number}
*/
this.eyeYaw = 0;
/**
* Pitch.
* @type {number}
*/
this.eyePitch = 0;
/**
* Roll.
* @type {number}
*/
this.eyeRoll = 0;
/**
* Previous yaw reading to support delta.
* @type {number}
* @private
*/
this.lastSensorYaw_ = 0;
/**
* View matrix.
* @type {!mat4}
*/
this.viewMatrix = mat4.create();
};
/**
* Updates the camera based on the current state.
* @param {number} time Current time.
* @param {number} timeDelta time since last frame.
* @param {!vr.State} vrstate Current vr state.
*/
Camera.prototype.update = function(time, timeDelta, vrstate) {
// Read sensor data, if present.
var rollPitchYaw = mat4.create();
if (vrstate.hmd.present) {
// TODO(benvanik): real work
mat4.fromQuat(rollPitchYaw, vrstate.hmd.rotation);
} else {
mat4.identity(rollPitchYaw);
}
// Simple head modeling from tiny world demo.
var HEAD_BASE_TO_EYE_HEIGHT = 0.15;
var HEAD_BASE_TO_EYE_PROTRUSION = 0.09;
var EYE_CENTER_IN_HEAD_FRAME =
vec3.fromValues(0, HEAD_BASE_TO_EYE_HEIGHT, -HEAD_BASE_TO_EYE_PROTRUSION);
vec3.transformMat4(tmpVec3, EYE_CENTER_IN_HEAD_FRAME, rollPitchYaw);
var shiftedEyePosition = vec3.create();
vec3.add(shiftedEyePosition, this.eyePosition, tmpVec3);
shiftedEyePosition[1] -= EYE_CENTER_IN_HEAD_FRAME[1];
var UP = vec3.fromValues(0, 1, 0);
var FORWARD = vec3.fromValues(0, 0, -1);
var up = vec3.create();
var forward = vec3.create();
vec3.transformMat4(up, UP, rollPitchYaw);
vec3.transformMat4(forward, FORWARD, rollPitchYaw);
var targetPosition = vec3.create();
vec3.add(targetPosition, shiftedEyePosition, forward);
mat4.lookAt(this.viewMatrix, shiftedEyePosition, targetPosition, up);
};
/**
* Demo app.
* @param {!Element} statusEl Element that will get status text.
* @param {!HTMLCanvasElement} canvas Target render canvas.
* @constructor
*/
var Demo = function(statusEl, canvas) {
/**
* Element that will get status text.
* @type {!Element}
* @private
*/
this.statusEl_ = statusEl;
/**
* Target canvas.
* @type {!HTMLCanvasElement}
* @private
*/
this.canvas_ = canvas;
/**
* WebGL context.
* @type {!WebGLRenderingContext}
* @private
*/
this.gl_ = this.createWebGL_(canvas);
var gl = this.gl_;
/**
* Camera.
* @type {!Camera}
* @private
*/
this.camera_ = new Camera();
/**
* Stereo renderer.
* @type {!vr.StereoRenderer}
* @private
*/
this.stereoRenderer_ = new vr.StereoRenderer(this.gl_, {
alpha: false,
depth: true,
stencil: false
});
/**
* VR state.
* @type {!vr.State}
* @private
*/
this.vrstate_ = new vr.State();
/**
* Time of the previous tick.
* Used to calculate frame deltas for animation.
* @type {number}
* @private
*/
this.lastTick_ = 0;
/**
* Simple shader program used to draw a cube.
* @type {!vr.Program}
* @private
*/
this.cubeProgram_ = new vr.Program(gl, 'CubeProgram',
[
'attribute vec3 a_xyz;',
'attribute vec2 a_uv;',
'varying vec2 v_uv;',
'uniform mat4 u_projectionMatrix;',
'uniform mat4 u_modelViewMatrix;',
'void main() {',
' gl_Position = u_projectionMatrix * u_modelViewMatrix * ',
' vec4(a_xyz, 1.0);',
' v_uv = a_uv;',
'}'
].join('\n'),
[
'precision highp float;',
'varying vec2 v_uv;',
'void main() {',
' gl_FragColor = vec4(v_uv, 0.0, 1.0);',
'}'
].join('\n'),
['a_xyz', 'a_uv'],
['u_projectionMatrix', 'u_modelViewMatrix']);
this.cubeProgram_.beginLinking();
this.cubeProgram_.endLinking();
this.cubeBuffer_ = gl.createBuffer();
this.cubeBuffer_.displayName = 'CubeVertexBuffer';
gl.bindBuffer(gl.ARRAY_BUFFER, this.cubeBuffer_);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
-1.0, -1.0, 1.0, 0.0, 0.0, // Front face
1.0, -1.0, 1.0, 1.0, 0.0,
1.0, 1.0, 1.0, 1.0, 1.0,
-1.0, 1.0, 1.0, 0.0, 1.0,
-1.0, -1.0, -1.0, 1.0, 0.0, // Back face
-1.0, 1.0, -1.0, 1.0, 1.0,
1.0, 1.0, -1.0, 0.0, 1.0,
1.0, -1.0, -1.0, 0.0, 0.0,
-1.0, 1.0, -1.0, 0.0, 1.0, // Top face
-1.0, 1.0, 1.0, 0.0, 0.0,
1.0, 1.0, 1.0, 1.0, 0.0,
1.0, 1.0, -1.0, 1.0, 1.0,
-1.0, -1.0, -1.0, 1.0, 1.0, // Bottom face
1.0, -1.0, -1.0, 0.0, 1.0,
1.0, -1.0, 1.0, 0.0, 0.0,
-1.0, -1.0, 1.0, 1.0, 0.0,
1.0, -1.0, -1.0, 1.0, 0.0, // Right face
1.0, 1.0, -1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 0.0, 1.0,
1.0, -1.0, 1.0, 0.0, 0.0,
-1.0, -1.0, -1.0, 0.0, 0.0, // Left face
-1.0, -1.0, 1.0, 1.0, 0.0,
-1.0, 1.0, 1.0, 1.0, 1.0,
-1.0, 1.0, -1.0, 0.0, 1.0,
]), gl.STATIC_DRAW);
this.cubeIndexBuffer_ = gl.createBuffer();
this.cubeIndexBuffer_.displayName = 'CubeIndexBuffer';
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.cubeIndexBuffer_);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array([
0, 1, 2, 0, 2, 3, // Front face
4, 5, 6, 4, 6, 7, // Back face
8, 9, 10, 8, 10, 11, // Top face
12, 13, 14, 12, 14, 15, // Bottom face
16, 17, 18, 16, 18, 19, // Right face
20, 21, 22, 20, 22, 23 // Left face
]), gl.STATIC_DRAW);
// Common key handlers.
var self = this;
document.addEventListener('keydown', function(e) {
if (self.keyPressed_(e)) {
e.preventDefault();
}
}, false);
// Kickoff the demo.
this.tick_();
};
/**
* Attempts to create a new WebGL context.
* An error will be thrown if the context cannot be created.
* @param {!HTMLCanvasElement} canvas Target canvas.
* @return {!WebGLRenderingContext} New context.
* @private
*/
Demo.prototype.createWebGL_ = function(canvas) {
if (!global.WebGLRenderingContext) {
throw 'WebGL not supported by this browser.';
}
var attributes = {
alpha: false,
depth: false,
stencil: false,
antialias: false, // need to use custom antialiasing
premultipliedAlpha: true,
preserveDrawingBuffer: false
};
var names = ['webgl', 'experimental-webgl'];
var gl = null;
for (var n = 0; n < names.length; n++) {
gl = canvas.getContext(names[n], attributes);
if (gl) {
break;
}
}
if (!gl) {
throw 'Unable to get a WebGL context.';
}
// TODO(benvanik): extra setup? context loss? etc?
return gl;
};
/**
* Updates the status message.
* @param {string?} value New value, if any.
*/
Demo.prototype.setStatus = function(value) {
this.statusEl_.innerHTML = value || '';
};
/**
* Handles key press events.
* @param {!Event} e Browser event.
* @return {boolean} Whether the key press was handled.
* @private
*/
Demo.prototype.keyPressed_ = function(e) {
switch (e.keyCode) {
case 32: // space
// Reset sensors to their default state.
vr.resetHmdOrientation();
return true;
case 70: // f
// Toggle fullscreen mode.
if (!vr.isFullScreen()) {
vr.enterFullScreen();
} else {
vr.exitFullScreen();
}
return true;
case 78: // n
var ipd = this.stereoRenderer_.getInterpupillaryDistance();
ipd -= 0.001;
this.stereoRenderer_.setInterpupillaryDistance(ipd);
this.setStatus('ipd: ' + ipd);
break;
case 77: // m
var ipd = this.stereoRenderer_.getInterpupillaryDistance();
ipd += 0.001;
this.stereoRenderer_.setInterpupillaryDistance(ipd);
this.setStatus('ipd: ' + ipd);
break;
case 80: // p
var mode = this.stereoRenderer_.getPostProcessingMode();
switch (mode) {
case vr.PostProcessingMode.STRAIGHT:
mode = vr.PostProcessingMode.WARP;
break;
case vr.PostProcessingMode.WARP:
mode = vr.PostProcessingMode.WARP_CHROMEAB;
break;
case vr.PostProcessingMode.WARP_CHROMEAB:
mode = vr.PostProcessingMode.STRAIGHT;
break;
}
this.stereoRenderer_.setPostProcessingMode(mode);
break;
}
return false;
};
/**
* Processes a single frame.
* @private
*/
Demo.prototype.tick_ = function() {
// Schedule the next frame.
vr.requestAnimationFrame(this.tick_, this);
// Poll VR, if it's ready.
vr.pollState(this.vrstate_);
// TODO(benvanik): now(), if possible.
var time = Date.now();
var timeDelta = this.lastTick_ ? time - this.lastTick_ : 0;
this.lastTick_ = time;
// Update scene animation/etc.
// This should all happen once, where {@see #renderScene_} may be called
// multiple times.
this.updateScene_(time, timeDelta);
// Update the stereo renderer.
this.stereoRenderer_.render(this.vrstate_, this.renderScene_, this);
};
/**
* Updates the scene.
* This will only be called once per frame.
* @param {number} time Current time.
* @param {number} timeDelta time since last frame.
* @private
*/
Demo.prototype.updateScene_ = function(time, timeDelta) {
// Update camera.
// TODO(benvanik): plumb keyboard input down.
this.camera_.update(time, timeDelta, this.vrstate_);
// TODO(benvanik): animate scene.
};
/**
* Renders the entire scene.
* This may be called multiple times per frame.
* @param {!StereoEye} eye Eye being rendered.
* @param {number} width Render target width.
* @param {number} height Render target height.
* @private
*/
Demo.prototype.renderScene_ = function(eye, width, height) {
var gl = this.gl_;
gl.clearColor(0, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
this.cubeProgram_.use();
var modelViewMatrix = mat4.create();
mat4.identity(modelViewMatrix);
vec3.set(tmpVec3, 5, 5, 5);
mat4.scale(modelViewMatrix, modelViewMatrix, tmpVec3);
mat4.multiply(modelViewMatrix, modelViewMatrix, this.camera_.viewMatrix);
mat4.multiply(modelViewMatrix, eye.viewAdjustMatrix, modelViewMatrix);
gl.uniformMatrix4fv(this.cubeProgram_.uniforms['u_projectionMatrix'], false,
eye.projectionMatrix);
gl.uniformMatrix4fv(this.cubeProgram_.uniforms['u_modelViewMatrix'], false,
modelViewMatrix);
var a_xyz = this.cubeProgram_.attributes['a_xyz'];
var a_uv = this.cubeProgram_.attributes['a_uv'];
gl.enableVertexAttribArray(a_xyz);
gl.enableVertexAttribArray(a_uv);
gl.bindBuffer(gl.ARRAY_BUFFER, this.cubeBuffer_);
gl.vertexAttribPointer(a_xyz, 3, gl.FLOAT, false, (3 + 2) * 4, 0);
gl.vertexAttribPointer(a_uv, 2, gl.FLOAT, false, (3 + 2) * 4, 3 * 4);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.cubeIndexBuffer_);
gl.drawElements(gl.TRIANGLES, 36, gl.UNSIGNED_SHORT, 0);
};
/**
* Launches the demo.
* @param {!Element} statusEl Element that will get status text.
* @param {!HTMLCanvasElement} canvas Target render canvas.
*/
global.launchDemo = function(statusEl, canvas) {
function startDemo() {
new Demo(statusEl, canvas);
};
if (!vr.isInstalled()) {
statusEl.innerText = 'NPVR plugin not installed!';
startDemo();
return;
}
vr.load(function(error) {
if (error) {
statusEl.innerText = 'Plugin load failed: ' + error.toString();
}
try {
startDemo();
} catch (e) {
statusEl.innerText = e.toString();
console.log(e);
}
});
};
vr.waitForDomReady(document, function() {
launchDemo(
document.getElementById('status'),
document.getElementById('canvas'));
});
})(window);