-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
executable file
·54 lines (50 loc) · 1.1 KB
/
controller.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
console.log("Controller loaded");
const controller = (function() {
let onClick = () => {},
raycaster = new THREE.Raycaster(),
mouse = new THREE.Vector2(),
isMoved = false,
clickedAt = 0;
window.addEventListener(
"mousemove",
e => {
isMoved = true;
mouse.x = (e.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(e.clientY / window.innerHeight) * 2 + 1;
},
false
);
window.addEventListener(
"mousedown",
() => {
clickedAt = Date.now();
isMoved = false;
},
false
);
window.addEventListener(
"click",
() => {
if (isMoved && Date.now() - clickedAt > 120) {
return;
}
raycaster.setFromCamera(mouse, view.camera);
let intersects = raycaster.intersectObject(view.mesh);
if (intersects.length == 0) {
return;
}
onClick(intersects[0].faceIndex);
},
false
);
return {
startClickMonitoring(callback) {
onClick = callback;
}, //callback(face)
stopClickMonitoring() {
onClick = () => {};
isMoved = false;
clickedAt = 0;
}
};
})();