-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.js
61 lines (52 loc) · 1.67 KB
/
camera.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
(function() {
var mouseDown = false,
lastMouseEvent = null,
INITIAL_TETA = 1.40,
INITIAL_PHI = 0.20,
INITIAL_RADIUS = game.objects.floor.length * 0.8,
cameraRadius = INITIAL_RADIUS,
cameraTeta = INITIAL_TETA,
cameraPhi = INITIAL_PHI,
cameraLookAt = new THREE.Vector3(),
camera = game.objects.camera;
function handleMouseDown(e) {
mouseDown = true;
lastMouseEvent = e;
}
function handleMouseMove(e) {
if (mouseDown) { // Dragging
cameraPhi += (e.x - lastMouseEvent.x) / 1000;
cameraTeta -= (e.y - lastMouseEvent.y) / 1000;
updateCameraPosition();
lastMouseEvent = e;
}
}
function handleMouseUp(e) {
mouseDown = false;
}
function handleMouseWheel(e) {
cameraRadius -= e.wheelDeltaY;
cameraLookAt.x -= e.wheelDeltaX;
updateCameraPosition();
}
function resetCamera(e) {
cameraTeta = INITIAL_TETA;
cameraPhi = INITIAL_PHI;
cameraRadius = INITIAL_RADIUS;
updateCameraPosition();
}
function updateCameraPosition() {
camera.position.x = cameraRadius * Math.sin(cameraTeta) * Math.cos(cameraPhi);
camera.position.z = cameraRadius * Math.sin(cameraTeta) * Math.sin(cameraPhi);
camera.position.y = cameraRadius * Math.cos(cameraTeta);
camera.lookAt(cameraLookAt);
}
window.addEventListener('mousedown', handleMouseDown);
window.addEventListener('mousemove', handleMouseMove);
window.addEventListener('mouseup', handleMouseUp);
window.addEventListener('mousewheel', handleMouseWheel);
window.addEventListener('load', function () {
document.getElementById('reset-camera').addEventListener('click', resetCamera);
});
updateCameraPosition();
})();