-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththree.html
326 lines (232 loc) · 10.2 KB
/
three.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
<!DOCTYPE html>
<html>
<head>
<title>Ben Ni</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png">
<link rel="manifest" href="site.webmanifest">
<link rel="stylesheet" href="assets/css/three.css" />
<link rel="stylesheet" href="assets/css/main.css" />
<noscript>
<link rel="stylesheet" href="assets/css/noscript.css" /></noscript>
</head>
<body class="is-preload">
<!-- Header -->
<header id="header">
<h1><a class="icon solid fa-user-circle" href="index.html"> Ben Ni</a></h1>
</header>
<div id="blocker" style="display: block;">
<div id="instructions" style="">
<span style="font-size:36px">Click to view Ben's gallery</span>
<br><br>
Move: WASD<br>
Jump: SPACE<br>
Look: MOUSE<br>
Base code from: https://threejs.org/examples/ <br>
Currently only supported on desktop browsers<br>
</div>
</div>
<!-- <script src="js/three.js"></script> -->
<script type="module">
import * as THREE from './assets/js/three/three.module.js';
import { PointerLockControls } from './assets/js/three/PointerLockControls.js';
var camera, scene, renderer, controls;
var objects = [];
var raycaster;
var moveForward = false;
var moveBackward = false;
var moveLeft = false;
var moveRight = false;
var canJump = false;
var prevTime = performance.now();
var velocity = new THREE.Vector3();
var direction = new THREE.Vector3();
var vertex = new THREE.Vector3();
var color = new THREE.Color();
var g_size = 100;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.y = 10;
scene = new THREE.Scene();
scene.background = new THREE.Color(0x00000);
scene.fog = new THREE.Fog(0x00000, 0, 750);
var light = new THREE.HemisphereLight(0xeeeeff, 0x777788, 0.75);
light.position.set(0.5, 1, 0.75);
scene.add(light);
controls = new PointerLockControls(camera, document.body);
var blocker = document.getElementById('blocker');
var instructions = document.getElementById('instructions');
instructions.addEventListener('click', function () {
controls.lock();
}, false);
controls.addEventListener('lock', function () {
instructions.style.display = 'none';
blocker.style.display = 'none';
});
controls.addEventListener('unlock', function () {
blocker.style.display = 'block';
instructions.style.display = '';
});
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);
// floor
var geometry = new THREE.PlaneGeometry(1000, 1000, 10, 10);
geometry.rotateX(- Math.PI / 2);
var floorTexture = new THREE.TextureLoader().load('images/three/wood.jpg');
floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping;
floorTexture.repeat.set(50, 50);
var material = new THREE.MeshBasicMaterial({ map: floorTexture }),
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
// vertex displacement
// walls
// var targetList = [];
// var cube = new THREE.Mesh(new THREE.CubeGeometry(10, 10, 10), new THREE.MeshBasicMaterial({ color: 0xCCCCCC }));
// targetList.push(cube);
var boxGeometry = new THREE.BoxBufferGeometry(50, 50, 50);
boxGeometry = boxGeometry.toNonIndexed(); // ensure each face has unique vertices
var position = boxGeometry.attributes.position;
// loop to get the max number to loop
g_size = 0;
while (true) {
if (!imageExists('images/three/gallery/' + g_size++ + '.png')) {
break;
}
}
// Garentee minimum 10 art works
var sizeToLoop = g_size > 10 ? g_size : 10;
var pictureIndex;
for (var i = 0; i < sizeToLoop; i++) {
if (g_size < 10) {
pictureIndex = i % (g_size - 1);
}
else {
pictureIndex = i;
}
var boxTexture = new THREE.TextureLoader().load('images/three/gallery/' + pictureIndex + '.png');
boxTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping;
boxTexture.repeat.set(1, 1);
var boxMaterial = new THREE.MeshBasicMaterial({ map: boxTexture });
var box = new THREE.Mesh(boxGeometry, boxMaterial);
box.position.x = Math.floor(Math.random() * 20 - 10) * 20;
box.position.y = Math.floor(Math.random() * 20) * 20 + 10;
box.position.z = Math.floor(Math.random() * 20 - 10) * 20;
scene.add(box);
objects.push(box);
}
// var targetList = [];
// targetList.push(box);
// var intersects = raycaster.intersectObjects(targetList);
// if (intersects.length > 0) {
// textureLoader.crossOrigin = "Anonymous"
// newTexture = textureLoader.load('img/grin.png');
// intersects[0].object.material = new THREE.MeshBasicMaterial({
// map: newTexture
// });
// }
//
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
//
window.addEventListener('resize', onWindowResize, false);
}
function imageExists(image_url) {
var http = new XMLHttpRequest();
http.open('HEAD', image_url, false);
http.send();
return http.status != 404;
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
if (controls.isLocked === true) {
raycaster.ray.origin.copy(controls.getObject().position);
raycaster.ray.origin.y -= 10;
var intersections = raycaster.intersectObjects(objects);
var onObject = 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
direction.z = Number(moveForward) - Number(moveBackward);
direction.x = Number(moveRight) - Number(moveLeft);
direction.normalize(); // this ensures consistent movements in all directions
if (moveForward || moveBackward) velocity.z -= direction.z * 400.0 * delta;
if (moveLeft || moveRight) velocity.x -= direction.x * 400.0 * delta;
if (onObject === true) {
velocity.y = Math.max(0, velocity.y);
canJump = true;
}
controls.moveRight(- velocity.x * delta);
controls.moveForward(- velocity.z * delta);
controls.getObject().position.y += (velocity.y * delta); // new behavior
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>