-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
426 lines (305 loc) · 13.1 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Luke three.js</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
html,
body {
width: 100%;
height: 100%;
}
body {
background-color: #ffffff;
margin: 0;
overflow: hidden;
font-family: arial;
}
#blocker {
position: absolute;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
}
#instructions {
width: 100%;
height: 100%;
display: -webkit-box;
display: -moz-box;
display: box;
-webkit-box-orient: horizontal;
-moz-box-orient: horizontal;
box-orient: horizontal;
-webkit-box-pack: center;
-moz-box-pack: center;
box-pack: center;
-webkit-box-align: center;
-moz-box-align: center;
box-align: center;
color: #ffffff;
text-align: center;
cursor: pointer;
}
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/84/three.js"></script>
<script src="js/lib/PointerLockControls.js"></script>
<script src="js/lib/VRControls.js"></script>
<script src="js/lib/VREffect.js"></script>
<script src="js/lib/WebVR.js"></script>
<script src="js/diamond_square.js"></script>
<div id="blocker">
<div id="instructions">
<span style="font-size:40px">Click to play</span>
<br /> (W, A, S, D = Move, SPACE = Jump, MOUSE = Look around)
</div>
</div>
<script>
/* Code modified from misc_controls_pointer_lock & webvr_cubes examples */
var blocker = document.getElementById('blocker');
var instructions = document.getElementById('instructions');
if (WEBVR.isAvailable() === false) {
document.body.appendChild(WEBVR.getMessage());
} else {
blocker.style.display = 'none';
}
var camera, scene, renderer;
var geometry, material, mesh;
var controls;
var objects = [];
var firework;
var particles = [];
var raycaster;
// http://www.html5rocks.com/en/tutorials/pointerlock/intro/
var havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;
if (havePointerLock) {
var element = document.body;
var pointerlockchange = function(event) {
if (document.pointerLockElement === element || document.mozPointerLockElement === element || document.webkitPointerLockElement === element) {
controlsEnabled = true;
controls.enabled = true;
blocker.style.display = 'none';
} else {
controls.enabled = false;
blocker.style.display = '-webkit-box';
blocker.style.display = '-moz-box';
blocker.style.display = 'box';
instructions.style.display = '';
}
};
var pointerlockerror = function(event) {
instructions.style.display = '';
};
// Hook pointer lock state change events
document.addEventListener('pointerlockchange', pointerlockchange, false);
document.addEventListener('mozpointerlockchange', pointerlockchange, false);
document.addEventListener('webkitpointerlockchange', pointerlockchange, false);
document.addEventListener('pointerlockerror', pointerlockerror, false);
document.addEventListener('mozpointerlockerror', pointerlockerror, false);
document.addEventListener('webkitpointerlockerror', pointerlockerror, false);
instructions.addEventListener('click', function(event) {
instructions.style.display = 'none';
// Ask the browser to lock the pointer
element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;
element.requestPointerLock();
}, false);
} else {
if (navigator.getVRDisplays) {
navigator.getVRDisplays()
.then(function(displays) {
effect.setVRDisplay(displays[0]);
controls.setVRDisplay(displays[0]);
})
.catch(function() {
// no displays
});
document.body.appendChild(WEBVR.getButton(effect));
}
}
init();
animate();
var controlsEnabled = false;
var moveForward = false;
var moveBackward = false;
var moveLeft = false;
var moveRight = false;
var canJump = false;
var prevTime = performance.now();
var velocity = new THREE.Vector3();
function init() {
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1000);
scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xffffff, 0, 750);
scene.background = new THREE.Color(0x0074D9);
var light = new THREE.HemisphereLight(0xeeeeff, 0x777788, 0.75);
light.position.set(0.5, 1, 0.75);
scene.add(light);
controls = new THREE.PointerLockControls(camera);
scene.add(controls.getObject());
var onKeyDown = function(event) {
switch (event.keyCode) {
case 38: // up
case 87: // w
moveForward = true;
break;
case 37: // left
case 65: // a
moveLeft = true;
break;
case 40: // down
case 83: // s
moveBackward = true;
break;
case 39: // right
case 68: // d
moveRight = true;
break;
case 32: // space
if (canJump === true) velocity.y += 350;
canJump = false;
break;
}
};
var onKeyUp = function(event) {
switch (event.keyCode) {
case 38: // up
case 87: // w
moveForward = false;
break;
case 37: // left
case 65: // a
moveLeft = false;
break;
case 40: // down
case 83: // s
moveBackward = false;
break;
case 39: // right
case 68: // d
moveRight = false;
break;
}
};
document.addEventListener('keydown', onKeyDown, false);
document.addEventListener('keyup', onKeyUp, false);
raycaster = new THREE.Raycaster(new THREE.Vector3(), new THREE.Vector3(0, -1, 0), 0, 10);
// My terrain logic
geometry = new THREE.PlaneGeometry(400, 400, 16, 16);
PLANE = geometry;
geometry.rotateX(-Math.PI / 2);
/* Apply diamond square */
var ds = diamondSquare(17, 30, .9); // from diamond_square.js
for (var i = 0; i < 17; i++) {
for (var j = 0; j < 17; j++) {
geometry.vertices[i * 17 + j].y = ds[i][j];
}
}
/* Color the faces so it looks cooler and you can actually see the variation in terrain*/
for (var i = 0, l = geometry.faces.length; i < l; i++) {
var face = geometry.faces[i];
face.vertexColors[0] = new THREE.Color().setHSL(Math.random() * 0.15 + 0.4, 0.50, Math.random() * 0.15 + 0.45);
face.vertexColors[1] = new THREE.Color().setHSL(Math.random() * 0.15 + 0.4, 0.50, Math.random() * 0.15 + 0.45);
face.vertexColors[2] = new THREE.Color().setHSL(Math.random() * 0.15 + 0.4, 0.50, Math.random() * 0.15 + 0.45);
}
material = new THREE.MeshBasicMaterial({
vertexColors: THREE.VertexColors
});
mesh = new THREE.Mesh(geometry, material);
mesh.position.y -= 30;
scene.add(mesh);
/* Build the firework */
var geometry = new THREE.SphereGeometry(5, 32, 32);
var material = new THREE.MeshBasicMaterial({
color: 0xFF851B
});
firework = new THREE.Mesh(geometry, material);
scene.add(firework);
firework.position.x = 0;
firework.position.y = 0;
firework.position.z = -100;
renderer = new THREE.WebGLRenderer();
renderer.setClearColor(0xffffff);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
window.addEventListener('resize', onWindowResize, false);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
// Firework logic
if (particles) {
// move particles randomly if they are in explosion phase
particles.forEach(function(p) {
p.position.x += Math.random() * 10 - 5;
p.position.y += Math.random() * 10 - 5;
p.position.z += Math.random() * 10 - 5;
});
}
// BOOM
if (firework.position.y >= 100) {
// remove the old particles
particles.forEach(function(p) {
scene.remove(p);
});
// make new particles in the general area of the firework
for (var i = 0; i < 15; i++) {
var geometry = new THREE.SphereGeometry(1, 5, 5);
var material = new THREE.MeshBasicMaterial({
color: 0xee0030
});
var mesh = new THREE.Mesh(geometry, material);
mesh.position.x = firework.position.x + Math.random() * 15 - 5;
mesh.position.y = firework.position.y + Math.random() * 20 - 5;
mesh.position.z = firework.position.z;
particles.push(mesh);
scene.add(mesh);
}
// reset and place the firework in a random location
firework.position.y = -100 * Math.random() * 2;
firework.position.z = Math.random() * 400 - 200;
firework.position.x = Math.random() * 400 - 200;
} else {
// fly to the sky baby
firework.position.y += 1;
firework.position.x += Math.random() * .25 + .5;
}
if (controlsEnabled) {
raycaster.ray.origin.copy(controls.getObject().position);
raycaster.ray.origin.y -= 10;
var intersections = raycaster.intersectObjects(objects);
var isOnObject = intersections.length > 0;
var time = performance.now();
var delta = (time - prevTime) / 1000;
velocity.x -= velocity.x * 10.0 * delta;
velocity.z -= velocity.z * 10.0 * delta;
velocity.y -= 9.8 * 100.0 * delta; // 100.0 = mass
if (moveForward) velocity.z -= 400.0 * delta;
if (moveBackward) velocity.z += 400.0 * delta;
if (moveLeft) velocity.x -= 400.0 * delta;
if (moveRight) velocity.x += 400.0 * delta;
if (isOnObject === true) {
velocity.y = Math.max(0, velocity.y);
canJump = true;
}
controls.getObject().translateX(velocity.x * delta);
controls.getObject().translateY(velocity.y * delta);
controls.getObject().translateZ(velocity.z * delta);
if (controls.getObject().position.y < 10) {
velocity.y = 0;
controls.getObject().position.y = 10;
canJump = true;
}
prevTime = time;
}
renderer.render(scene, camera);
}
</script>
</body>
</html>