-
Notifications
You must be signed in to change notification settings - Fork 37
/
XX-monoscopic-rendering.html
284 lines (241 loc) · 10.7 KB
/
XX-monoscopic-rendering.html
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
<!doctype html>
<!--
Copyright 2016 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<title>XX - Monoscopic Rendering</title>
<!--
This sample demonstrates how to render monoscopically: drawing only one
view of the scene and presenting it to both eyes. Doing so removes stereo
vision cues (everything appears to be painted on surface infinitely far
away) but cuts the GPU work by as much as half, making it a big
performance win for some scenes. Works especially well for non-stereo
panoramic photos and videos.
The content is not mirrored on the main display while being presented.
-->
<style>
#webgl-canvas {
box-sizing: border-box;
height: 100%;
left: 0;
margin: 0;
position: absolute;
top: 0;
width: 100%;
}
</style>
<!-- This entire block in only to facilitate dynamically enabling and
disabling the WebVR polyfill, and is not necessary for most WebVR apps.
If you want to use the polyfill in your app, just include the js file and
everything will work the way you want it to by default. -->
<script>
// Prevents the polyfill from initializing automatically.
var WebVRConfig = { DEFER_INITIALIZATION: true }
</script>
<script src="js/third-party/webvr-polyfill.js"></script>
<script src="js/third-party/wglu/wglu-url.js"></script>
<script>
// Dynamically turn the polyfill on if requested by the query args.
if (WGLUUrl.getBool('polyfill', false)) { InitializeWebVRPolyfill(); }
</script>
<!-- End sample polyfill enabling logic -->
<script src="js/third-party/gl-matrix-min.js"></script>
<script src="js/third-party/wglu/wglu-debug-geometry.js"></script>
<script src="js/third-party/wglu/wglu-program.js"></script>
<script src="js/third-party/wglu/wglu-stats.js"></script>
<script src="js/third-party/wglu/wglu-texture.js"></script>
<script src="js/vr-cube-sea.js"></script>
<script src="js/vr-samples-util.js"></script>
</head>
<body>
<canvas id="webgl-canvas"></canvas>
<script>
/* global mat4, vec4, VRCubeSea, VRSamplesUtil, WGLUDebugGeometry, WGLUStats, WGLUTextureLoader */
(function () {
"use strict";
var vrDisplay = null;
var projectionMat = mat4.create();
var viewMat = mat4.create();
var vrPresentButton = null;
var monoView;
// ===================================================
// WebGL scene setup. This code is not WebVR specific.
// ===================================================
// WebGL setup.
var webglCanvas = document.getElementById("webgl-canvas");
var gl = null;
var cubeSea = null;
var stats = null;
var debugGeom = null;
function initWebGL (preserveDrawingBuffer) {
var glAttribs = {
alpha: false,
antialias: !VRSamplesUtil.isMobile(),
preserveDrawingBuffer: preserveDrawingBuffer
};
gl = webglCanvas.getContext("webgl", glAttribs);
gl.clearColor(0.1, 0.2, 0.3, 1.0);
gl.enable(gl.DEPTH_TEST);
gl.enable(gl.CULL_FACE);
var textureLoader = new WGLUTextureLoader(gl);
var texture = textureLoader.loadTexture("media/textures/cube-sea.png");
cubeSea = new VRCubeSea(gl, texture);
stats = new WGLUStats(gl);
debugGeom = new WGLUDebugGeometry(gl);
// Wait until we have a WebGL context to resize and start rendering.
window.addEventListener("resize", onResize, false);
onResize();
window.requestAnimationFrame(onAnimationFrame);
}
// ================================
// WebVR-specific code begins here.
// ================================
var calculateMonoView = (function () {
// It's expensive to frequently recreate matricies and vectors, so we
// use this pattern to cache them in a way that only makes them
// accessible to the desired function.
var leftEyeProjection = mat4.create();
var leftPlane = vec4.create();
var rightPlane = vec4.create();
return function (vrDisplay) {
var leftEye = vrDisplay.getEyeParameters("left");
mat4.perspectiveFromFieldOfView(leftEyeProjection, leftEye.fieldOfView, 0.1, 1024.0);
mat4.invert(leftEyeProjection, leftEyeProjection);
vec4.set(leftPlane, -1, 0, 0, 1 );
vec4.set(rightPlane, 1, 0, 0, 1 );
vec4.transformMat4(leftPlane, leftPlane, leftEyeProjection);
vec4.transformMat4(rightPlane, rightPlane, leftEyeProjection);
var leftX = -leftPlane[0] / leftPlane[3];
var rightX = rightPlane[0]/ rightPlane[3];
var leftPercentage = leftX / (leftX + rightX);
var leftWidth = leftPercentage * leftEye.renderWidth;
var monoWidth = 2 * leftWidth;
var eyeRenderWidth = Math.min(leftEye.renderWidth, monoWidth);
var monoFieldOfView = {
upDegrees: leftEye.fieldOfView.upDegrees,
downDegrees: leftEye.fieldOfView.downDegrees,
leftDegrees: leftEye.fieldOfView.leftDegrees,
rightDegrees: leftEye.fieldOfView.leftDegrees
};
return {
fieldOfView: monoFieldOfView,
renderWidth: monoWidth,
renderHeight: leftEye.renderHeight,
leftViewport: [0, 0, eyeRenderWidth, leftEye.renderHeight],
rightViewport: [(monoWidth - eyeRenderWidth), 0, eyeRenderWidth, leftEye.renderHeight],
leftUVBounds: [0, 0, monoWidth / eyeRenderWidth, 1],
rightUVBounds: [(monoWidth - eyeRenderWidth) / monoWidth, 0, monoWidth / eyeRenderWidth, 1]
};
};
})();
function onVRRequestPresent () {
var layer = {
source: webglCanvas,
// To indicate a monoscopic rendering we set both viewports to overlap.
leftBounds: monoView.leftUVBounds,
rightBounds: monoView.rightUVBounds,
};
vrDisplay.requestPresent(layer).then(function () {
}, function () {
VRSamplesUtil.addError("requestPresent failed.", 2000);
});
}
function onVRExitPresent () {
vrDisplay.exitPresent().then(function () {
}, function () {
VRSamplesUtil.addError("exitPresent failed.", 2000);
});
}
function onVRPresentChange () {
onResize();
if (vrDisplay.isPresenting) {
if (vrDisplay.capabilities.hasExternalDisplay) {
VRSamplesUtil.removeButton(vrPresentButton);
vrPresentButton = VRSamplesUtil.addButton("Exit VR", "E", "media/icons/cardboard64.png", onVRExitPresent);
}
} else {
if (vrDisplay.capabilities.hasExternalDisplay) {
VRSamplesUtil.removeButton(vrPresentButton);
vrPresentButton = VRSamplesUtil.addButton("Enter VR", "E", "media/icons/cardboard64.png", onVRRequestPresent);
}
}
}
if (navigator.getVRDisplays) {
navigator.getVRDisplays().then(function (displays) {
if (displays.length > 0) {
vrDisplay = displays[0];
VRSamplesUtil.addButton("Reset Pose", "R", null, function () { vrDisplay.resetPose(); });
monoView = calculateMonoView(vrDisplay);
initWebGL(vrDisplay.capabilities.hasExternalDisplay);
if (vrDisplay.capabilities.canPresent)
vrPresentButton = VRSamplesUtil.addButton("Enter VR", "E", "media/icons/cardboard64.png", onVRRequestPresent);
window.addEventListener('vrdisplaypresentchange', onVRPresentChange, false);
} else {
initWebGL(false);
VRSamplesUtil.addInfo("WebVR supported, but no VRDisplays found.", 3000);
}
});
} else if (navigator.getVRDevices) {
initWebGL(false);
VRSamplesUtil.addError("Your browser supports WebVR but not the latest version. See <a href='http://webvr.info'>webvr.info</a> for more info.");
} else {
initWebGL(false);
VRSamplesUtil.addError("Your browser does not support WebVR. See <a href='http://webvr.info'>webvr.info</a> for assistance.");
}
function onResize () {
if (vrDisplay && vrDisplay.isPresenting) {
// When rendering monoscopically you only need to have a canvas large
// enough to cover one eye, so the "* 2" on the width from the
// previous sample has been removed
webglCanvas.width = monoView.renderWidth;
webglCanvas.height = monoView.renderHeight;
} else {
webglCanvas.width = webglCanvas.offsetWidth * window.devicePixelRatio;
webglCanvas.height = webglCanvas.offsetHeight * window.devicePixelRatio;
}
}
function onAnimationFrame (t) {
stats.begin();
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// Rendering monoscopically uses the whole canvas whether you are presenting
// or not.
gl.viewport(0, 0, webglCanvas.width, webglCanvas.height);
if (vrDisplay) {
vrDisplay.requestAnimationFrame(onAnimationFrame);
if (vrDisplay.isPresenting) {
// We want to render with a symmetrical field of view.
mat4.perspectiveFromFieldOfView(projectionMat, monoView.fieldOfView, 0.1, 1024.0);
} else {
mat4.perspective(projectionMat, 45, webglCanvas.width / webglCanvas.height, 0.1, 1024.0);
}
var pose = vrDisplay.getPose();
var orientation = pose.orientation;
var position = pose.position;
if (!orientation) { orientation = [0, 0, 0, 1]; }
if (!position) { position = [0, 0, 0]; }
mat4.fromRotationTranslation(viewMat, orientation, position);
// When rendering monoscopically the eye offset should NOT be taken into account.
mat4.invert(viewMat, viewMat);
cubeSea.render(projectionMat, viewMat, stats);
vrDisplay.submitFrame(pose);
} else {
window.requestAnimationFrame(onAnimationFrame);
// No VRDisplay found.
mat4.perspective(projectionMat, 45, webglCanvas.width / webglCanvas.height, 0.1, 1024.0);
mat4.identity(viewMat);
cubeSea.render(projectionMat, viewMat, stats);
stats.renderOrtho();
}
stats.end();
}
})();
</script>
</body>
</html>