-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·324 lines (252 loc) · 9.66 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Web VR boilerplate</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>
body {
width: 100%;
height: 100%;
background-color: #000;
color: #fff;
margin: 0px;
padding: 0;
overflow: hidden;
}
</style>
</head>
<body>
</body>
<script>
/*
* Debug parameters.
*/
WebVRConfig = {
/**
* webvr-polyfill configuration
*/
// Forces availability of VR mode.
//FORCE_ENABLE_VR: true, // Default: false.
// Complementary filter coefficient. 0 for accelerometer, 1 for gyro.
//K_FILTER: 0.98, // Default: 0.98.
// How far into the future to predict during fast motion.
//PREDICTION_TIME_S: 0.040, // Default: 0.040 (in seconds).
// Flag to disable touch panner. In case you have your own touch controls
//TOUCH_PANNER_DISABLED: true, // Default: false.
// Enable yaw panning only, disabling roll and pitch. This can be useful for
// panoramas with nothing interesting above or below.
//YAW_ONLY: true, // Default: false.
// Enable the deprecated version of the API (navigator.getVRDevices).
//ENABLE_DEPRECATED_API: true, // Default: false.
// Scales the recommended buffer size reported by WebVR, which can improve
// performance. Making this very small can lower the effective resolution of
// your scene.
BUFFER_SCALE: 0.5, // default: 1.0
// Allow VRDisplay.submitFrame to change gl bindings, which is more
// efficient if the application code will re-bind it's resources on the
// next frame anyway.
// Dirty bindings include: gl.FRAMEBUFFER_BINDING, gl.CURRENT_PROGRAM,
// gl.ARRAY_BUFFER_BINDING, gl.ELEMENT_ARRAY_BUFFER_BINDING,
// and gl.TEXTURE_BINDING_2D for texture unit 0
// Warning: enabling this might lead to rendering issues.
//DIRTY_SUBMIT_FRAME_BINDINGS: true // default: false
};
</script>
<!--
A polyfill for Promises. Needed for IE and Edge.
-->
<script src="node_modules/es6-promise/dist/es6-promise.js"></script>
<script src="node_modules/recordrtc-socketio-/dist/es6-promise.js"></script>
<!--
three.js 3d library
-->
<script src="node_modules/three/three.js"></script>
<!--
VRControls.js acquires positional information from connected VR devices and applies the transformations to a three.js camera object.
-->
<script src="node_modules/three/examples/js/controls/VRControls.js"></script>
<script src="node_modules/three/examples/js/controls/FirstPersonControls.js"></script>
<script src="node_modules/three/node_modules/three-firstperson-vr-controls/FirstPersonVRControls.js"></script>
<!--
VREffect.js handles stereo camera setup and rendering.
-->
<script src="node_modules/three/examples/js/effects/VREffect.js"></script>
<!--
A polyfill for WebVR using the Device{Motion,Orientation}Event API.
-->
<script src="node_modules/webvr-polyfill/build/webvr-polyfill.js"></script>
<!--
Helps enter and exit VR mode, provides best practices while in VR.
-->
<script src="build/webvr-manager.js"></script>
<script>
// 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: true});
renderer.setPixelRatio(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();
//attempt parenting, didn't work: revisit? or switch to freaking unity
//var character = new THREE.Object3D();
//var camParent = new THREE.Object3D();
// Create a three.js camera.
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 10000);
//var vrControls = new THREE.VRControls(camera);
//thanks to https://github.com/brianpeiris/three-firstperson-vr-controls
var fpVRControls = new THREE.FirstPersonVRControls(camera, scene);
fpVRControls.verticalMovement = true;
//garbage
// var camControls = new THREE.FirstPersonControls(camera);
// camControls.lookSpeed = 0.4;
// camControls.movementSpeed = 20;
// camControls.noFly = true;
// camControls.lookVertical = true;
// camControls.constrainVertical = true;
// camControls.verticalMin = 1.0;
// camControls.verticalMax = 2.0;
// camControls.lon = -150;
// camControls.lat = 120;
//character.add(camera);
//character.add(camera);
// Create first eprson controls
//var FirstPersonControls = new THREE.FirstPersonControls(character, renderer.domElement);
//Apply VR headset positional data to camera.
var controls = new THREE.VRControls(camera);
controls.standing = true;
// Apply VR stereo rendering to renderer.
var effect = new THREE.VREffect(renderer);
effect.setSize(window.innerWidth, window.innerHeight);
console.log("I'm running")
// Add a repeating grid as a skybox.
var boxSize = 15;
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(boxSize, boxSize);
var geometry = new THREE.BoxGeometry(boxSize, boxSize, boxSize);
var material = new THREE.MeshBasicMaterial({
map: texture,
color: 0x01BE00,
side: THREE.BackSide
});
// Align the skybox to the floor (which is at y=0).
skybox = new THREE.Mesh(geometry, material);
skybox.position.y = boxSize/2;
scene.add(skybox);
//create an actual floor
//starting with this one. We can also use planegeometry,. Not sure which will be better :/
var terrainShape = new THREE.CubeGeometry(15, 0.5, 15);
//audioCube.position.x = camera.position.x;
//audioCube.position.y= camera.position.y;
//audioCube.position.z = camera.position.z;
var material_1 = new THREE.MeshBasicMaterial({
color: 0xffffff,
});
var terrain= new THREE.Mesh(terrainShape, material_1);
terrain.position.y = 0;
scene.add(terrain);
//collidableMeshList.push(terrain);
/* //working on getting collision working.
for (var vertexIndex = 0; vertexIndex < MovingCube.geometry.vertices.length; vertexIndex++)
{
var localVertex = MovingCube.geometry.vertices[vertexIndex].clone();
var globalVertex = localVertex.applyMatrix4( MovingCube.matrix );
var directionVector = globalVertex.sub( MovingCube.position );
var ray = new THREE.Raycaster( originPoint, directionVector.clone().normalize() );
var collisionResults = ray.intersectObjects( collidableMeshList );
if ( collisionResults.length > 0 && collisionResults[0].distance < directionVector.length() )
appendText(" Hit ");*/
//https://stemkoski.github.io/Three.js/Collision-Detection.html
// For high end VR devices like Vive and Oculus, take into account the stage
// parameters provided.
setupStage();
}
// Create a VR manager helper to enter and exit VR mode.
var params = {
hideButton: false, // Default: false.
isUndistorted: false // Default: false.
};
var manager = new WebVRManager(renderer, effect, params);
// 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);
console.log("3d objects are created");
//var camcube = new THREE.Mesh(geometry, material);
//position camcube to be in same location as camera
//camcube.position.set(0, controls.userHeight, 0);
// Position cube mesh to be right in front of you.
cube.position.set(0, controls.userHeight, -1);
// Add cube mesh to your three.js scene
scene.add(cube);
//scene.add(camcube);
// Kick off animation loop
requestAnimationFrame(animate);
function onKey(event) {
if (event.keyCode == 32) {
createAudioObject();
console.log("Detected onkey");
}
}
window.addEventListener('resize', onResize, true);
window.addEventListener('vrdisplaypresentchange', onResize, true);
window.addEventListener('keydown', onKey, true);
// Request animation frame loop function
var lastRender = 0;
function animate(timestamp) {
var delta = Math.min(timestamp - lastRender, 500);
lastRender = timestamp;
console.log("animation is running")
// Apply rotation to cube mesh
cube.rotation.y += delta * 0.0006;
// Update VR headset position and apply to camera.
controls.update();
fpVRControls.update(timestamp);
//FirstPersonControls.update();
// TK update VR camcube and apply to camera
//FirstPersonControls.update()
// Render the scene through the manager.
manager.render(scene, camera, timestamp);
requestAnimationFrame(animate);
}
function onResize(e) {
effect.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
var display;
// Get the HMD, and if we're dealing with something that specifies
// stageParameters, rearrange the scene.
function setupStage() {
navigator.getVRDisplays().then(function(displays) {
if (displays.length > 0) {
display = displays[0];
if (display.stageParameters) {
setStageDimensions(display.stageParameters);
}
}
});
}
function setStageDimensions(stage) {
// Make the skybox fit the stage.
var material = skybox.material;
scene.remove(skybox);
// Size the skybox according to the size of the actual stage.
var geometry = new THREE.BoxGeometry(stage.sizeX, boxSize, stage.sizeZ);
skybox = new THREE.Mesh(geometry, material);
// Place it on the floor.
skybox.position.y = boxSize/2;
scene.add(skybox);
// Place the cube in the middle of the scene, at user height.
cube.position.set(0, controls.userHeight, 0);
}
</script>
</html>