-
Notifications
You must be signed in to change notification settings - Fork 323
/
index.html
275 lines (228 loc) · 7.61 KB
/
index.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
<!--
/*
* Copyright 2017 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.
*/
-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>WebVR Polyfill Demo</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0, shrink-to-fit=no">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<style>
html, body {
width: 100%;
height: 100%;
background-color: #000;
color: #fff;
margin: 0px;
padding: 0;
overflow: hidden;
}
canvas {
width: 100%;
height: 100%;
}
#buttons {
position: fixed;
top: 0;
right: 0;
z-index: 1;
background: white;
}
</style>
</head>
<body>
<div id="buttons">
<button id="fullscreen">Fullscreen</button>
<button id="vr">VR (WebVR/Mobile only)</button>
</div>
</body>
<script>
document.addEventListener('touchmove', function(e) {
e.preventDefault();
});
</script>
<!-- three.js library -->
<script src="third_party/three.js/three.js"></script>
<!-- VRControls.js applies the WebVR transformations to a three.js camera object. -->
<script src="third_party/three.js/VRControls.js"></script>
<!-- OrbitControls.js for controlling camera on desktop. -->
<script src="third_party/three.js/OrbitControls.js"></script>
<!-- VREffect.js handles stereo camera setup and rendering. -->
<script src="third_party/three.js/VREffect.js"></script>
<script src="../build/webvr-polyfill.js"></script>
<script>
// Get config from URL
var config = (function() {
var config = {};
var q = window.location.search.substring(1);
if (q === '') {
return config;
}
var params = q.split('&');
var param, name, value;
for (var i = 0; i < params.length; i++) {
param = params[i].split('=');
name = param[0];
value = param[1];
// All config values are either boolean or float
config[name] = value === 'true' ? true :
value === 'false' ? false :
parseFloat(value);
}
return config;
})();
var polyfill = new WebVRPolyfill(config);
console.log("Using webvr-polyfill version " + WebVRPolyfill.version +
" with configuration: " + JSON.stringify(config));
var renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio(Math.floor(window.devicePixelRatio));
// Append the canvas element created by the renderer to document body element.
var canvas = renderer.domElement;
document.body.appendChild(canvas);
// Create a three.js scene.
var scene = new THREE.Scene();
// Create a three.js camera.
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 10000);
// Create a reticle
var reticle = new THREE.Mesh(
new THREE.RingBufferGeometry(0.005, 0.01, 15),
new THREE.MeshBasicMaterial({ color: 0xffffff })
);
reticle.position.z = -0.5;
camera.add(reticle);
scene.add(camera);
// Apply VR stereo rendering to renderer.
var effect = new THREE.VREffect(renderer);
effect.setSize(canvas.clientWidth, canvas.clientHeight, false);
var vrDisplay, controls;
// Add a repeating grid as a skybox.
var boxWidth = 5;
// Create 3D objects.
var geometry = new THREE.BoxGeometry(0.5, 0.5, 0.5);
var material = new THREE.MeshNormalMaterial();
var cube = new THREE.Mesh(geometry, material);
// Position cube
cube.position.z = -1;
// Add cube mesh to your three.js scene
scene.add(cube);
// Load the skybox texture and cube
var loader = new THREE.TextureLoader();
loader.load('img/box.png', onTextureLoaded);
function onTextureLoaded(texture) {
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(boxWidth, boxWidth);
var geometry = new THREE.BoxGeometry(boxWidth, boxWidth, boxWidth);
var material = new THREE.MeshBasicMaterial({
map: texture,
color: 0x01BE00,
side: THREE.BackSide
});
var skybox = new THREE.Mesh(geometry, material);
scene.add(skybox);
}
// The polyfill provides this in the event this browser
// does not support WebVR 1.1
navigator.getVRDisplays().then(function (vrDisplays) {
// If we have a native display, or we have a CardboardVRDisplay
// from the polyfill, use it
if (vrDisplays.length) {
vrDisplay = vrDisplays[0];
// Apply VR headset positional data to camera.
controls = new THREE.VRControls(camera);
// Kick off the render loop.
vrDisplay.requestAnimationFrame(animate);
}
// Otherwise, we're on a desktop environment with no native
// displays, so provide controls for a monoscopic desktop view
else {
controls = new THREE.OrbitControls(camera);
controls.target.set(0, 0, -1);
// Disable the "Enter VR" button
var enterVRButton = document.querySelector('#vr');
enterVRButton.disabled = true;
// Kick off the render loop.
requestAnimationFrame(animate);
}
});
// Request animation frame loop function
var lastRender = 0;
function animate(timestamp) {
var delta = Math.min(timestamp - lastRender, 500);
lastRender = timestamp;
// Apply rotation to cube mesh
cube.rotation.y += delta * 0.0002;
// Update VR headset position and apply to camera.
controls.update();
// Render the scene.
effect.render(scene, camera);
// Keep looping; if using a VRDisplay, call its requestAnimationFrame,
// otherwise call window.requestAnimationFrame.
if (vrDisplay) {
vrDisplay.requestAnimationFrame(animate);
} else {
requestAnimationFrame(animate);
}
}
function onResize() {
// The delay ensures the browser has a chance to layout
// the page and update the clientWidth/clientHeight.
// This problem particularly crops up under iOS.
if (!onResize.resizeDelay) {
onResize.resizeDelay = setTimeout(function () {
onResize.resizeDelay = null;
console.log('Resizing to %s x %s.', canvas.clientWidth, canvas.clientHeight);
effect.setSize(canvas.clientWidth, canvas.clientHeight, false);
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
}, 250);
}
}
function onVRDisplayPresentChange() {
console.log('onVRDisplayPresentChange');
onResize();
buttons.hidden = vrDisplay.isPresenting;
}
function onVRDisplayConnect(e) {
console.log('onVRDisplayConnect', (e.display || (e.detail && e.detail.display)));
}
// Resize the WebGL canvas when we resize and also when we change modes.
window.addEventListener('resize', onResize);
window.addEventListener('vrdisplaypresentchange', onVRDisplayPresentChange);
window.addEventListener('vrdisplayconnect', onVRDisplayConnect);
// Button click handlers.
document.querySelector('button#fullscreen').addEventListener('click', function() {
enterFullscreen(renderer.domElement);
});
document.querySelector('button#vr').addEventListener('click', function() {
vrDisplay.requestPresent([{source: renderer.domElement}]);
});
function enterFullscreen (el) {
if (el.requestFullscreen) {
el.requestFullscreen();
} else if (el.mozRequestFullScreen) {
el.mozRequestFullScreen();
} else if (el.webkitRequestFullscreen) {
el.webkitRequestFullscreen();
} else if (el.msRequestFullscreen) {
el.msRequestFullscreen();
}
}
</script>
</html>