-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
474 lines (364 loc) · 18.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<!DOCTYPE html>
<html lang="en">
<link type="text/css" rel="stylesheet" href="style.css">
<head>
<meta charset="UTF-8">
<title>Three.js Gallery Room</title>
<link rel="icon" type="image/x-icon" href="/assets/images/favicon3.ico">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Ojuju:wght@200..800&display=swap" rel="stylesheet">
<style>
body {
margin: 0;
}
canvas {
display: block;
}
</style>
</head>
<body>
<div id="blocker">
<div class="flex-container">
<div class="flex-child left">
<div id="image">
<img src="assets/images/favicon2.ico" style="width:300px;">
</div>
</div>
<div class="flex-child right">
<div id="instructions">
<p style="font-size:50px; font-weight:700">
Click anywhere to explore
</p>
<p style="font-weight:300">
Move: WASD<br />
Look: MOUSE<br />
Esc: Pause
</p>
</div>
</div>
</div>
</div>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.161.0/build/three.module.js",
"three/addons/": "https://unpkg.com/three@0.161.0/examples/jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { PointerLockControls } from 'three/addons/controls/PointerLockControls.js';
import { VRButton } from 'three/addons/webxr/VRButton.js';
let camera, scene, renderer, controls;
let bowieModel, shieldModel, pusheenModel, froggyModel, riverModel, wingmanModel, pupModel;
let moveForward = false;
let moveBackward = false;
let moveLeft = false;
let moveRight = false;
let canJump = false;
let raycaster;
let prevTime = performance.now();
const velocity = new THREE.Vector3();
const direction = new THREE.Vector3();
const vertex = new THREE.Vector3();
init();
animate();
function init() {
// Scene setup
scene = new THREE.Scene();
scene.background = new THREE.Color(0xffffff);
scene.fog = new THREE.Fog(0xffffff, 0, 750);
// Camera setup
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(2, 1.6, 10); // Adjust height to match your scene scale
// Renderer setup
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.xr.enabled = true;
document.body.appendChild(renderer.domElement);
let vrButton = VRButton.createButton( renderer );
document.body.appendChild( vrButton );
vrButton.addEventListener('click', function () {
controls.lock();
}, false);
// Lighting
const ambientLight = new THREE.AmbientLight(0xcccccc, 0.4);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
scene.add(directionalLight);
// spotlight
const spotLight = new THREE.SpotLight( 0xffffff );
spotLight.position.set( 3, 1.5, 4 );
// spotLight.map = new THREE.TextureLoader().load( url );
spotLight.castShadow = true;
// spotLight.shadow.mapSize.width = 1024;
// spotLight.shadow.mapSize.height = 1024;
// spotLight.shadow.camera.near = 500;
// spotLight.shadow.camera.far = 4000;
// spotLight.shadow.camera.fov = 30;
scene.add( spotLight );
// Room creation
createRoom();
// Controls
controls = new PointerLockControls(camera, renderer.domElement);
const blocker = document.getElementById('blocker');
const instructions = document.getElementById('instructions');
document.addEventListener('click', function () {
controls.lock();
}, false);
controls.addEventListener('lock', function () {
instructions.style.display = 'none';
blocker.style.display = 'none';
document.getElementById('octicon').style.filter="invert(100%)";
});
controls.addEventListener('unlock', function () {
blocker.style.display = 'block';
instructions.style.display = '';
document.getElementById('octicon').style.filter="invert(0%)";
});
// scene.add(controls.getObject());
const onKeyDown = function (event) {
switch (event.code) {
case 'ArrowUp':
case 'KeyW':
moveForward = true;
break;
case 'ArrowLeft':
case 'KeyA':
moveLeft = true;
break;
case 'ArrowDown':
case 'KeyS':
moveBackward = true;
break;
case 'ArrowRight':
case 'KeyD':
moveRight = true;
break;
case 'Space':
if (canJump === true) velocity.y += 350;
canJump = false;
break;
}
};
const onKeyUp = function (event) {
switch (event.code) {
case 'ArrowUp':
case 'KeyW':
moveForward = false;
break;
case 'ArrowLeft':
case 'KeyA':
moveLeft = false;
break;
case 'ArrowDown':
case 'KeyS':
moveBackward = false;
break;
case 'ArrowRight':
case 'KeyD':
moveRight = false;
break;
}
};
document.addEventListener('keydown', onKeyDown);
document.addEventListener('keyup', onKeyUp);
raycaster = new THREE.Raycaster(new THREE.Vector3(), new THREE.Vector3(0, - 1, 0), 0, 10);
// Handle window resize
window.addEventListener('resize', onWindowResize, false);
}
function createRoom() {
// Room dimensions and materials
const roomGeometry = new THREE.BoxGeometry(100, 20, 100); // Example dimensions
var groundTexture = new THREE.TextureLoader().load('assets/textures/oak.png');
groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
groundTexture.repeat.set(2500, 2500);
groundTexture.encoding = THREE.sRGBEncoding;
const floorMaterial = new THREE.MeshStandardMaterial({ map: groundTexture });
const wallMaterial = new THREE.MeshLambertMaterial({ color: 0xFFFFFF }); // White
// Floor
const floor = new THREE.Mesh(new THREE.PlaneGeometry(10000, 10000), floorMaterial);
floor.rotation.x = -Math.PI / 2;
scene.add(floor);
// Walls
// Adjust positions and rotations to create walls around the floor
// Example for one wall:
const wall = new THREE.Mesh(roomGeometry, wallMaterial);
wall.position.set(0, 2, -5); // Adjust position according to your room dimensions
// scene.add(wall);
// bowie ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
// Column (as a simple cylinder for this example)
const columnGeometry = new THREE.CylinderGeometry(0.5, 0.5, 1, 32);
const columnMaterial = new THREE.MeshBasicMaterial({ color: 0x808080 });
const column = new THREE.Mesh(columnGeometry, columnMaterial);
column.position.set(3, 1, 5); // Position it on the floor
scene.add(column);
// load model
const bowieloader = new GLTFLoader();
bowieloader.load('assets/models/thisisbowie2.gltf', function (gltf) {
gltf.scene.position.set(3, 2.2, 5); // Adjust position to be on top of the column
gltf.scene.rotation.x += 1.5;
gltf.scene.scale.set(.5, .5, .5);
scene.add(gltf.scene);
bowieModel = gltf.scene;
}, undefined, function (error) {
console.error(error);
});
// ganon's shield ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
// Column (as a simple cylinder for this example)
const columnGeometry1 = new THREE.CylinderGeometry(0.5, 0.5, 1, 32);
const columnMaterial1 = new THREE.MeshBasicMaterial({ color: 0x808080 });
const column1 = new THREE.Mesh(columnGeometry1, columnMaterial1);
column1.position.set(5, 1, 5); // Position it on the floor
scene.add(column1);
// load model
const shieldloader = new GLTFLoader();
shieldloader.load('assets/models/ganon.gltf', function (gltf) {
gltf.scene.position.set(5, 2.2, 5); // Adjust position to be on top of the column
gltf.scene.rotation.x += 4.75;
gltf.scene.scale.set(.5, .5, .5);
scene.add(gltf.scene);
shieldModel = gltf.scene;
}, undefined, function (error) {
console.error(error);
});
// pusheen ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
// Column (as a simple cylinder for this example)
const columnGeometry2 = new THREE.CylinderGeometry(0.5, 0.5, 1, 32);
const columnMaterial2 = new THREE.MeshBasicMaterial({ color: 0x808080 });
const column2 = new THREE.Mesh(columnGeometry2, columnMaterial2);
column2.position.set(1, 1, 5); // Position it on the floor
scene.add(column2);
// load model
const pusheenloader = new GLTFLoader();
pusheenloader.load('assets/models/pusheen.gltf', function (gltf) {
gltf.scene.position.set(1, 2.15, 5); // Adjust position to be on top of the column
// gltf.scene.rotation.x += 1.5;
gltf.scene.scale.set(.5, .5, .5);
scene.add(gltf.scene);
pusheenModel = gltf.scene;
}, undefined, function (error) {
console.error(error);
});
// froggy ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
// Column (as a simple cylinder for this example)
const columnGeometry3 = new THREE.CylinderGeometry(0.5, 0.5, 1, 32);
const columnMaterial3 = new THREE.MeshBasicMaterial({ color: 0x808080 });
const column3 = new THREE.Mesh(columnGeometry3, columnMaterial3);
column3.position.set(-1, 1, 5); // Position it on the floor
scene.add(column3);
// load model
const froggyloader = new GLTFLoader();
froggyloader.load('assets/models/disfroggy.gltf', function (gltf) {
gltf.scene.position.set(-1, 2.8, 5); // Adjust position to be on top of the column
// gltf.scene.rotation.x += 1.5;
gltf.scene.scale.set(.5, .5, .5);
scene.add(gltf.scene);
froggyModel = gltf.scene;
}, undefined, function (error) {
console.error(error);
});
// river ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
// load model
const riverloader = new GLTFLoader();
riverloader.load('assets/models/riverscene.gltf', function (gltf) {
gltf.scene.position.set(-8, -.5, 9); // Adjust position to be on top of the column
gltf.scene.rotation.y += 1.5;
gltf.scene.scale.set(.8, .8, .8);
scene.add(gltf.scene);
riverModel = gltf.scene;
}, undefined, function (error) {
console.error(error);
});
// wingman ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
// Column (as a simple cylinder for this example)
const columnGeometry4 = new THREE.CylinderGeometry(0.5, 0.5, 1, 32);
const columnMaterial4 = new THREE.MeshBasicMaterial({ color: 0x808080 });
const column4 = new THREE.Mesh(columnGeometry4, columnMaterial4);
column4.position.set(7, 1, 5); // Position it on the floor
scene.add(column4);
// load model
const wingloader = new GLTFLoader();
wingloader.load('assets/models/wingman.gltf', function (gltf) {
gltf.scene.position.set(7, 2.2, 5); // Adjust position to be on top of the column
// gltf.scene.rotation.x += 1.5;
gltf.scene.scale.set(.5, .5, .5);
scene.add(gltf.scene);
wingmanModel = gltf.scene;
}, undefined, function (error) {
console.error(error);
});
// pup ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
// Column (as a simple cylinder for this example)
const columnGeometry5 = new THREE.CylinderGeometry(0.5, 0.5, 1, 32);
const columnMaterial5 = new THREE.MeshBasicMaterial({ color: 0x808080 });
const column5 = new THREE.Mesh(columnGeometry5, columnMaterial5);
column5.position.set(9, 1, 5); // Position it on the floor
scene.add(column5);
// load model
const puploader = new GLTFLoader();
puploader.load('assets/models/pup.gltf', function (gltf) {
gltf.scene.position.set(9, 2.2, 5); // Adjust position to be on top of the column
// gltf.scene.rotation.x += 1.5;
gltf.scene.scale.set(.35, .35, .35);
scene.add(gltf.scene);
pupModel = gltf.scene;
}, undefined, function (error) {
console.error(error);
});
}
function animate() {
requestAnimationFrame(animate);
const time = performance.now();
if (controls.isLocked === true) {
// raycaster.ray.origin.copy(controls.getObject().position);
// raycaster.ray.origin.y -= 10;
// const intersections = raycaster.intersectObjects(objects, false);
// const onObject = intersections.length > 0;
const 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;
if (bowieModel) bowieModel.rotation.z += 0.01;
if (shieldModel) shieldModel.rotation.z += 0.01;
if (pusheenModel) pusheenModel.rotation.y += 0.01;
if (froggyModel) froggyModel.rotation.y += 0.01;
if (riverModel) riverModel.rotation.y += 0.005;
if (wingmanModel) wingmanModel.rotation.y += 0.01;
if (pupModel) pupModel.rotation.y += 0.01;
render();
}
function render() {
renderer.render(scene, camera);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
</script>
</body>
<footer><a href="https://github.com/bailey/gallery"><img src='./assets/octicon_white.png' id="octicon" width="40">
</html>