-
Notifications
You must be signed in to change notification settings - Fork 0
/
cameraMovement.js
169 lines (145 loc) · 5.42 KB
/
cameraMovement.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
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
// Camera control parameters
// https://sites.google.com/site/csc8820/educational/move-a-camera
var pitchAngle = 0;
var minPitchAngle = -90;
var maxPitchAngle = 90;
var yawAngle = 0;
var minYawAngle = -90;
var maxYawAngle = 90;
var rollCamera = false;
var rollAngle = 0;
var minRollAngle = -180;
var maxRollAngle = 180;
var trackLeftRight = 0;
var pushInPullOut = 0;
var craneUpDown = 0;
var step = 1;
var fovStep = .1;
var fov = degToRad(45);
var fovMin = 10;
var fovMax = 160;
var cameraPosition = [10, 0, -60];
var target = [0, 0, 0];
var up = [0, 1, 0];
var zoomIntensity = 100;
var lastX = 0, lastY = 0;
var dMouseX = 0, dMouseY = 0;
var trackingMouseMotion = false;
function setupControls(canvas){
initKeyboardCallback();
initMouseMotionCallback(canvas);
initWheelCallback(canvas);
}
// Register a keyboard callback function.
function initKeyboardCallback() {
document.onkeydown = function(event) {
switch(event.keyCode) {
case 82: // Use r or R to turn on/off camera rolling.
rollCamera = !rollCamera;
break;
case 65: // Use a or A to turn on/off animation.
animated = !animated;
break;
case 37: // Use left arrow to move the camera to the left.
trackLeftRight += step;
break;
case 38: // Use up arrow to move the camera forward.
pushInPullOut -= step;
break;
case 39: // Use right arrow to move the camera to the right.
trackLeftRight -= step;
break;
case 40: // Use down arrow to move the camera backward.
pushInPullOut += step;
break;
case 85: // Use u or U key to move the camera upward.
craneUpDown += step;
break;
case 68: // Use d or D key to move the camera downward.
craneUpDown -= step;
break;
case 107: // Use + key to zoom in.
fov -= step;
fov = Math.max(fov, fovMin); // lower limit of fov
break;
case 109: // Use - key to zoom out.
fov += step;
fov = Math.min(fov, fovMax); // upper limit of fov
break;
default: return;
}
}
}
// Register mouse callback functions
function initMouseMotionCallback(canvas) {
// If a mouse button is pressed, save the current mouse location
// and start tracking mouse motion.
canvas.onmousedown = function(event) {
var x = event.clientX;
var y = event.clientY;
var rect = event.target.getBoundingClientRect();
// Check if the mouse cursor is in canvas.
if (rect.left <= x && rect.right > x &&
rect.top <= y && rect.bottom > y) {
lastX = x;
lastY = y;
trackingMouseMotion = true;
}
}
// If the mouse button is release, stop tracking mouse motion.
canvas.onmouseup = function(event) {
trackingMouseMotion = false;
}
// Calculate how far the mouse cusor has moved and convert the mouse motion
// to rotation angles.
canvas.onmousemove = function(event) {
var x = event.clientX;
var y = event.clientY;
if (trackingMouseMotion) {
var scale = 1;
// Calculate how much the mouse has moved along X and Y axis, and then
// normalize them based on the canvas' width and height.
dMouseX = (x - lastX)/canvas.width;
dMouseY = (y - lastY)/canvas.height;
if (!rollCamera) {
// For camera pitch and yaw motions
scale = 30;
// Add the mouse motion to the current rotation angle so that the rotation
// is added to the previous rotations.
// Use scale to control the speed of the rotation.
yawAngle += scale * dMouseX;
// Impose the upper and lower limits to the rotation angle.
yawAngle = Math.max(Math.min(yawAngle, maxYawAngle), minYawAngle);
pitchAngle += scale * dMouseY;
pitchAngle = Math.max(Math.min(pitchAngle, maxPitchAngle), minPitchAngle);
} else {
// For camera roll motion
scale = 100;
// Add the mouse motion delta to the rotation angle, don't just replace it.
// Use scale to control the speed of the rotation.
rollAngle += scale * dMouseX;
rollAngle %= 360;
}
}
// Save the current mouse location in order to calculate the next mouse motion.
lastX = x;
lastY = y;
}
}
function initWheelCallback(canvas) {
canvas.onwheel = function (event){
event.preventDefault();
// var mousex = event.clientX - canvas.offsetLeft;
// var mousey = event.clientY - canvas.offsetTop;
var wheel = event.deltaY < 0 ? 1 : -1;
//var zoom = Math.exp(Math.abs(event.deltaY));
cameraPosition[2] += wheel*zoomIntensity;
// fov -= wheel * fovStep;
// if (wheel){
// fov = Math.min(fov, fovMin);
// }
// else{
// fov = Math.min(fov, fovMax);
// }
}
}