-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.js
More file actions
191 lines (138 loc) · 5.29 KB
/
camera.js
File metadata and controls
191 lines (138 loc) · 5.29 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
class Camera {
constructor(canvas) {
this.fov = 60;
this.eye = new Vector3([16, 3, 16]);
this.at = new Vector3([16, 3, 15]);
this.up = new Vector3([0, 1, 0]);
this.viewMatrix = new Matrix4();
this.projectionMatrix = new Matrix4();
this.speed = 0.5;
this.angleSpeed = 2;
this.canvas = canvas;
this.updateView();
this.updateProjection();
}
updateView() {
this.viewMatrix.setLookAt(
this.eye.elements[0], this.eye.elements[1], this.eye.elements[2],
this.at.elements[0], this.at.elements[1], this.at.elements[2],
this.up.elements[0], this.up.elements[1], this.up.elements[2]
);
}
updateProjection() {
this.projectionMatrix.setPerspective(
this.fov,
this.canvas.width / this.canvas.height,
0.1,
1000
);
}
moveForward() {
let fx = this.at.elements[0] - this.eye.elements[0];
let fy = this.at.elements[1] - this.eye.elements[1];
let fz = this.at.elements[2] - this.eye.elements[2];
let len = Math.sqrt(fx*fx + fy*fy + fz*fz);
fx/=len; fy/=len; fz/=len;
this.eye.elements[0] += fx * this.speed;
this.eye.elements[1] += fy * this.speed;
this.eye.elements[2] += fz * this.speed;
this.at.elements[0] += fx * this.speed;
this.at.elements[1] += fy * this.speed;
this.at.elements[2] += fz * this.speed;
this.updateView();
}
moveBackwards() {
let fx = this.eye.elements[0] - this.at.elements[0];
let fy = this.eye.elements[1] - this.at.elements[1];
let fz = this.eye.elements[2] - this.at.elements[2];
let len = Math.sqrt(fx*fx + fy*fy + fz*fz);
fx/=len; fy/=len; fz/=len;
this.eye.elements[0] += fx * this.speed;
this.eye.elements[1] += fy * this.speed;
this.eye.elements[2] += fz * this.speed;
this.at.elements[0] += fx * this.speed;
this.at.elements[1] += fy * this.speed;
this.at.elements[2] += fz * this.speed;
this.updateView();
}
moveLeft() {
let fx = this.at.elements[0] - this.eye.elements[0];
let fy = this.at.elements[1] - this.eye.elements[1];
let fz = this.at.elements[2] - this.eye.elements[2];
// cross(up, forward)
let sx = this.up.elements[1]*fz - this.up.elements[2]*fy;
let sy = this.up.elements[2]*fx - this.up.elements[0]*fz;
let sz = this.up.elements[0]*fy - this.up.elements[1]*fx;
let len = Math.sqrt(sx*sx + sy*sy + sz*sz);
sx/=len; sy/=len; sz/=len;
this.eye.elements[0] += sx * this.speed;
this.eye.elements[1] += sy * this.speed;
this.eye.elements[2] += sz * this.speed;
this.at.elements[0] += sx * this.speed;
this.at.elements[1] += sy * this.speed;
this.at.elements[2] += sz * this.speed;
this.updateView();
}
moveRight() {
let fx = this.at.elements[0] - this.eye.elements[0];
let fy = this.at.elements[1] - this.eye.elements[1];
let fz = this.at.elements[2] - this.eye.elements[2];
// cross(forward, up)
let sx = fy*this.up.elements[2] - fz*this.up.elements[1];
let sy = fz*this.up.elements[0] - fx*this.up.elements[2];
let sz = fx*this.up.elements[1] - fy*this.up.elements[0];
let len = Math.sqrt(sx*sx + sy*sy + sz*sz);
sx/=len; sy/=len; sz/=len;
this.eye.elements[0] += sx * this.speed;
this.eye.elements[1] += sy * this.speed;
this.eye.elements[2] += sz * this.speed;
this.at.elements[0] += sx * this.speed;
this.at.elements[1] += sy * this.speed;
this.at.elements[2] += sz * this.speed;
this.updateView();
}
panLeft() {
let f = new Vector3([
this.at.elements[0]-this.eye.elements[0],
this.at.elements[1]-this.eye.elements[1],
this.at.elements[2]-this.eye.elements[2]
]);
let rot = new Matrix4();
rot.setRotate(this.angleSpeed, 0,1,0);
let f2 = rot.multiplyVector3(f);
this.at.elements[0] = this.eye.elements[0] + f2.elements[0];
this.at.elements[1] = this.eye.elements[1] + f2.elements[1];
this.at.elements[2] = this.eye.elements[2] + f2.elements[2];
this.updateView();
}
panRight() {
let f = new Vector3([
this.at.elements[0]-this.eye.elements[0],
this.at.elements[1]-this.eye.elements[1],
this.at.elements[2]-this.eye.elements[2]
]);
let rot = new Matrix4();
rot.setRotate(-this.angleSpeed, 0,1,0);
let f2 = rot.multiplyVector3(f);
this.at.elements[0] = this.eye.elements[0] + f2.elements[0];
this.at.elements[1] = this.eye.elements[1] + f2.elements[1];
this.at.elements[2] = this.eye.elements[2] + f2.elements[2];
this.updateView();
}
rotate(deltaYaw, deltaPitch) {
this.yaw += deltaYaw;
this.pitch += deltaPitch;
// clamp pitch to avoid flipping
if(this.pitch > 89) this.pitch = 89;
if(this.pitch < -89) this.pitch = -89;
let radYaw = this.yaw * Math.PI / 180;
let radPitch = this.pitch * Math.PI / 180;
let fx = Math.cos(radPitch) * Math.sin(radYaw);
let fy = Math.sin(radPitch);
let fz = -Math.cos(radPitch) * Math.cos(radYaw);
this.at.elements[0] = this.eye.elements[0] + fx;
this.at.elements[1] = this.eye.elements[1] + fy;
this.at.elements[2] = this.eye.elements[2] + fz;
this.updateView();
}
}