-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
126 lines (101 loc) · 3.6 KB
/
main.js
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
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
document.addEventListener('contextmenu', function(event) {
event.preventDefault();
});
const message = document.getElementById('message');
const githubProfile = document.getElementById('github');
githubProfile.addEventListener('click', () => {
githubProfile.textContent = 'double click!'
setTimeout(() => {
githubProfile.textContent = '@lightly-toasted'
}, 1000)
return false;
});
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
});
let tiers = [
{ name: 'Eww!', color: 'darkgray', threshold: 200 },
{ name: 'Yucky', color: 'gray', threshold: 150 },
{ name: 'Meh.', color: 'darkseagreen', threshold: 100 },
{ name: 'Hmm...', color: 'darkolivegreen', threshold: 50 },
{ name: 'Good', color: 'limegreen', threshold: 25 },
{ name: 'Tasty!', color: 'greenyellow', threshold: 10 },
{ name: 'Awesome', color: 'crimson', threshold: 2 },
{ name: 'LEGENDARY', color: 'goldenrod', threshold: 1 },
{ name: 'PERFECT', color: 'gold', threshold: 0 }
]
document.body.appendChild( renderer.domElement );
renderer.setClearColor(0x000000, 0)
const directionalLight = new THREE.AmbientLight(0xffffff, 3.6);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
camera.position.z = 5;
var loader = new GLTFLoader();
loader.load('model.glb', gltf => {
const toaster = gltf.scene;
toaster.scale.set(3, 3, 3);
toaster.position.y = -.5;
toaster.rotation.set(0.4, 37, 0)
scene.add(toaster);
const burntTexture = toaster.children[0].children[0].children[3]
burntTexture.visible = false
document.body.addEventListener('mousedown', onStart);
document.body.addEventListener('mouseup', onEnd);
document.body.addEventListener('touchstart', onStart);
document.body.addEventListener('touchend', onEnd);
const mixer = new THREE.AnimationMixer( toaster );
const actionStart = mixer.clipAction(gltf.animations[0]);
const actionEnd = mixer.clipAction(gltf.animations[1]);
let time = 0
let burntAt = 0
let holdInterval;
function onStart() {
burntTexture.visible = false
message.style.color = 'white'
time = 0
burntAt = Math.round(Math.random() * 5) + 5
clearInterval(holdInterval)
holdInterval = setInterval(() => {
time++
message.textContent = `${time / 100} / ${burntAt}`
}, 10)
actionEnd.stop();
actionStart.play();
actionStart.clampWhenFinished = true;
actionStart.loop = THREE.LoopOnce;
}
function onEnd() {
const diff = burntAt * 100 - time
if (time < 100) message.textContent = 'hold...';
else if (diff >= 0) {
const resultTier = tiers.find(tier => diff >= tier.threshold);
message.style.color = resultTier.color;
message.textContent = `${resultTier.name} (-${diff / 100}s)`;
} else {
message.textContent = `Burnt! (+${-diff / 100}s)`;
message.style.color = 'black';
burntTexture.visible = true
}
clearInterval(holdInterval)
actionStart.stop();
actionEnd.play();
actionEnd.clampWhenFinished = true;
actionEnd.loop = THREE.LoopOnce;
}
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = clock.getDelta();
mixer.update(delta)
renderer.render(scene, camera);
}
animate();
} );