-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.html
More file actions
368 lines (311 loc) · 11.3 KB
/
Main.html
File metadata and controls
368 lines (311 loc) · 11.3 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Homepage</title>
<meta charset="utf-8">
<meta content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" name="viewport">
<link type="text/css" rel="stylesheet" href="main.css">
<audio loop id="music" src="./Media/alexi-action-i-wanna-feel-110039.mp3"></audio>
</head>
<body>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<!-- <link rel="stylesheet" href="https://unpkg.com/handsfree@8.5.1/build/lib/assets/handsfree.css" /> -->
<!-- <script src="https://unpkg.com/handsfree@8.5.1/build/lib/handsfree.js"></script> -->
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5.1/dat.gui.min.js"></script> -->
<script src="handsfree-master/build/lib/handsfree.js"></script>
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "./Threefiles/build/three.module.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { FBXLoader } from './Threefiles/FBXLoader.js';
import { GLTFLoader } from './Threefiles/GLTFLoader.js';
import { FontLoader } from './Threefiles/FontLoader.js';
import { TextGeometry } from './Threefiles/TextGeometry.js';
// Global Initializers
let camera, scene, renderer, clock, mixer, raycaster, object;
let textMesh, textMesh2, textMesh3, sphere, sphere2, handsfree, xVal, yVal;
let INTERSECTED;
let pointersNotInitialized = false;
let clicked = false, released = true;
const pointer = new THREE.Vector2();
// Get and start playing music
let audioElement = document.getElementById("music");
audioElement.play();
// Event Listener for resizing screen when press 'F' key
// NOTE: when in chrome can also use fn+f11 to remain in full screen
window.addEventListener('resize', onWindowResize);
document.addEventListener("keyup", function (event) {
if (event.keyCode === 70) {
if (getFullscreenElement()) {
document.exitFullscreen();
} else {
document.documentElement.requestFullscreen();
}
}
});
// Event Listener for the mousemoving - developer shortcut
document.addEventListener('mousemove', onPointerMove);
// Event Listener for mouse click on 'DEMO' button - hidden developer shortcut
window.addEventListener('click', (event) => {
const intersectDemo = raycaster.intersectObject(scene.children[65], false);
if (intersectDemo.length != 0) {
//console.log("demo")
location.href = "./Demo.html";
}
});
// Create first scene
init();
// After first scene is created start loop to simulate animation
animate();
// Fullscreen methods
function getFullscreenElement() {
return document.fullscreenElement //standard property
|| document.webkitFullscreenElement //safari/opera support
|| document.mozFullscreenElement //firefox support
|| document.msFullscreenElement; //ie/edge support
}
function toggleFullscreen() {
if (getFullscreenElement()) {
document.exitFullscreen();
}
}
function init() {
// Camera
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(2, 0.5, - 2.5);
camera.lookAt(1, 1, 0);
// Initialize timer
clock = new THREE.Clock();
// Create scene
scene = new THREE.Scene();
scene.background = new THREE.Color(0xa0a0a0);
scene.fog = new THREE.Fog(0xa0a0a0, 10, 150);
// Lights
const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444);
hemiLight.position.set(0, 20, 0);
scene.add(hemiLight);
const dirLight = new THREE.DirectionalLight(0xffffff);
dirLight.position.set(- 3, 10, - 10);
dirLight.castShadow = true;
dirLight.shadow.camera.top = 4;
dirLight.shadow.camera.bottom = - 4;
dirLight.shadow.camera.left = - 4;
dirLight.shadow.camera.right = 4;
dirLight.shadow.camera.near = 0.1;
dirLight.shadow.camera.far = 40;
scene.add(dirLight);
// Ground
const mesh = new THREE.Mesh(new THREE.PlaneGeometry(200, 200), new THREE.MeshPhongMaterial({ color: 0x999999, depthWrite: false }));
mesh.rotation.x = - Math.PI / 2;
mesh.receiveShadow = true;
scene.add(mesh);
// Cube background
const geometry = new THREE.BoxGeometry(20, 20, 20);
for (let j = 0; j < 5; j++) {
for (let i = 0; i < 12; i++) {
object = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: Math.random() * 0xffffff }));
object.position.x = -120 + (15 * i);
object.position.y = 80 - (j * 17);
object.position.z = 70 + (i * 8);
object.scale.set(j * 0.2 + 0.5, j * 0.2 + 0.5, j * 0.20 + 0.5);
scene.add(object);
}
}
// Text graphics
let text = "Interaction with Graphics";
const loader = new FontLoader();
loader.load("./Media/Ubuntu_Regular.json", function (font) {
const tGeometry = new TextGeometry(text, {
font: font,
size: 15,
height: 1,
curveSegments: 12,
bevelEnabled: true,
bevelThickness: 0.5,
bevelSize: 1,
bevelOffset: 1,
bevelSegments: 1
});
textMesh = new THREE.Mesh(tGeometry, new THREE.MeshPhongMaterial({ color: Math.random() * 0xffffff }));
textMesh.position.set(-10, 15, 30);
textMesh.rotation.y = 2.5;
textMesh.rotation.x = -0.10;
textMesh.rotation.z = -0.05;
textMesh.scale.set(0.1, 0.1, 0.1);
scene.add(textMesh);
});
let text2 = "by: Tucker Johnson";
loader.load("./Media/Ubuntu_Regular.json", function (font) {
const tGeometry = new TextGeometry(text2, {
font: font,
size: 15,
height: 2,
curveSegments: 12,
bevelEnabled: true,
bevelThickness: 1,
bevelSize: 1,
bevelOffset: 1,
bevelSegments: 1
});
textMesh2 = new THREE.Mesh(tGeometry, new THREE.MeshPhongMaterial({ color: Math.random() * 0xffffff }));
textMesh2.position.set(-19, 13.5, 30);
textMesh2.rotation.y = 2.5;
textMesh2.rotation.x = -0.060;
textMesh2.rotation.z = -0.05;
textMesh2.scale.set(0.05, 0.05, 0.05);
scene.add(textMesh2);
});
let text3 = "Demo";
loader.load("./Media/Ubuntu_Regular.json", function (font) {
const tGeometry = new TextGeometry(text3, {
font: font,
size: 15,
height: 2,
curveSegments: 12,
bevelEnabled: true,
bevelThickness: 1,
bevelSize: 1,
bevelOffset: 1,
bevelSegments: 1
});
textMesh3 = new THREE.Mesh(tGeometry, new THREE.MeshPhongMaterial({ color: Math.random() * 0xffffff }));
textMesh3.position.set(-17, 8, 30);
textMesh3.rotation.y = 2.5;
textMesh3.rotation.x = -0.010;
textMesh3.rotation.z = -0.05;
textMesh3.scale.set(0.2, 0.2, 0.2);
scene.add(textMesh3);
});
// sphere cursor - never implemented - went with hand gestures instead
sphere = new THREE.Mesh(new THREE.SphereGeometry(1, 1, .5), new THREE.MeshPhongMaterial({ color: Math.random() * 0xffffff }));
sphere.position.set(0, 0, 1);
sphere.scale.set(0.1, 0.1, 0.1);
// scene.add(sphere);
sphere2 = new THREE.Mesh(new THREE.SphereGeometry(1, 1, .5), new THREE.MeshPhongMaterial({ color: Math.random() * 0xffffff }));
sphere2.position.set(0, 0, 1);
sphere2.scale.set(0.1, 0.1, 0.1);
//scene.add(sphere2);
// Dancing Bob Model
const loader2 = new FBXLoader();
loader2.load('./Media/Silly Dancing.fbx', function (object) {
mixer = new THREE.AnimationMixer(object);
const action = mixer.clipAction(object.animations[0]);
action.play();
object.traverse(function (child) {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
object.position.set(2, 0, 5);
object.scale.set(0.02, 0.02, 0.02);
object.rotation.y = 3.5;
scene.add(object);
});
// Instruction text
let directionsText = "Pinch both index fingers with thumbs to start Demo.";
loader.load("./Media/Ubuntu_Regular.json", function (font) {
const tGeometry = new TextGeometry(directionsText, {
font: font,
size: 15,
height: 2,
curveSegments: 12,
bevelEnabled: true,
bevelThickness: 1,
bevelSize: 1,
bevelOffset: 1,
bevelSegments: 1
});
directionsText = new THREE.Mesh(tGeometry, new THREE.MeshPhongMaterial({ color: Math.random() * 0xffffff }));
directionsText.position.set(4, -4, 30);
directionsText.rotation.y = 2.6;
directionsText.rotation.x = -0.055;
directionsText.rotation.z = 0.05;
directionsText.scale.set(0.05, 0.05, 0.05);
scene.add(directionsText);
});
raycaster = new THREE.Raycaster();
// Renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
// Event listener to resize browser to be either fullscreen or exit fullscreen
window.addEventListener('resize', onWindowResize);
// Initialize handsfree
handsfree = new Handsfree({
hands: {
enabled: true,
maxNumhands: 2,
minDetectionConfidence: 0.8,
minTrackingConfidence: 0.4
}
});
handsfree.plugin.pinchScroll.disable();
handsfree.start();
// console.log(handsfree)
}
// Resize window function
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
// Developer method with mouse interaction
function onPointerMove(event) {
pointer.x = (event.clientX / window.innerWidth) * 2 - 1;
pointer.y = - (event.clientY / window.innerHeight) * 2 + 1;
}
// Animation loop
function animate() {
// Request next frame
requestAnimationFrame(animate);
camera.updateMatrixWorld();
// Update clock
const delta = clock.getDelta();
if (mixer) mixer.update(delta);
// Mouse interaction with cubes and text
raycaster.setFromCamera(pointer, camera);
const intersects = raycaster.intersectObjects(scene.children, false);
if (handsfree.data.hands != undefined) {
// check for pinch with pointer finger for hand 1
if (handsfree.data.hands.pinchState[0] != undefined) {
if (handsfree.data.hands.pinchState[0][0] == "released") {
// check for pinch with pointer finger for hand 2
if (handsfree.data.hands.pinchState[1] != undefined) {
console.log("check pinch _ 1", handsfree.data.hands.pinchState[0][0], "check pinch_2", handsfree.data.hands.pinchState[1][0]);
if (handsfree.data.hands.pinchState[1][0] == "released") {
// If both hands are pinched then go to Demo page
window.open("./Demo.html", "_parent");
}
}
}
}
}
// Mouse intersection code
if (intersects.length > 0) {
if (INTERSECTED != intersects[0].object) {
if (INTERSECTED) INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex);
INTERSECTED = intersects[0].object;
INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
INTERSECTED.material.emissive.setHex(0xff0000);
}
} else {
if (INTERSECTED) INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex);
INTERSECTED = null;
}
// Render current state's scene
renderer.render(scene, camera);
}
</script>
</body>
</html>