-
Notifications
You must be signed in to change notification settings - Fork 43
/
index.html
258 lines (211 loc) · 6.94 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
<!--
/*
* 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>Cardboard VRDisplay 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 {
position: absolute;
top: 0;
}
#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>
<button id="reset">Reset</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>
<!-- VREffect.js handles stereo camera setup and rendering. -->
<script src="../third_party/three.js/VREffect.js"></script>
<script src="../dist/cardboard-vr-display.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;
})();
// Mock VRFrameData for VRControls
function VRFrameData () {
this.leftViewMatrix = new Float32Array(16);
this.rightViewMatrix = new Float32Array(16);
this.leftProjectionMatrix = new Float32Array(16);
this.rightProjectionMatrix = new Float32Array(16);
this.pose = null;
};
console.log('creating CardboardVRDisplay with options', config);
var vrDisplay = new CardboardVRDisplay(config);
// If loading this inside of an iframe (see iframe.html),
// force using the `devicemotion` sensor fusion, rather than
// newer Generic Sensors due to an issue with sensors
// in iframes in Chrome < m69:
// https://bugs.chromium.org/p/chromium/issues/detail?id=849501
if (window.self !== window.top) {
vrDisplay.poseSensor_.useDeviceMotion();
}
navigator.getVRDisplays = function () {
return new Promise(function (resolve) {
resolve([vrDisplay]);
});
};
// Setup three.js WebGL renderer. Note: Antialiasing is a big performance hit.
// Only enable it if you actually need to.
var renderer = new THREE.WebGLRenderer({antialias: false});
renderer.setPixelRatio(Math.floor(window.devicePixelRatio));
// Append the canvas element created by the renderer to document body element.
document.body.appendChild(renderer.domElement);
// 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 headset positional data to camera.
var controls = new THREE.VRControls(camera);
// Apply VR stereo rendering to renderer.
var effect = new THREE.VREffect(renderer);
effect.setSize(window.innerWidth, window.innerHeight);
// Add a repeating grid as a skybox.
var boxWidth = 5;
var loader = new THREE.TextureLoader();
loader.load('img/box.png', onTextureLoaded);
// Kick off the render loop.
vrDisplay.requestAnimationFrame(animate);
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);
}
// 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 mesh
cube.position.z = -1;
// Add cube mesh to your three.js scene
scene.add(cube);
// 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.0006;
// Update VR headset position and apply to camera.
controls.update();
// Render the scene.
effect.render(scene, camera);
// Keep looping.
vrDisplay.requestAnimationFrame(animate);
}
function onResize() {
console.log('Resizing to %s x %s.', window.innerWidth, window.innerHeight);
effect.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
function onVRDisplayPresentChange() {
console.log('onVRDisplayPresentChange');
onResize();
}
// Resize the WebGL canvas when we resize and also when we change modes.
window.addEventListener('resize', onResize);
window.addEventListener('vrdisplaypresentchange', onVRDisplayPresentChange);
// 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}]);
});
document.querySelector('button#reset').addEventListener('click', function() {
vrDisplay.resetPose();
});
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>