-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.js
79 lines (64 loc) · 2.23 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function Camera(){
// [PITCH, YAW, ROLL]
this.yaw = 0;
this.pitch = 0;
this.roll = 0;
this.panSpeed=50;
}
Camera.prototype.position = function(renderer){
this.processPanning(renderer);
mat4.translate(renderer.mvMatrix, [-this.x, -this.y, -this.z]);
mat4.lookAt([this.x, this.y, this.z], [this.focusX, this.focusY, this.focusZ], [0, 1, 0], renderer.mvMatrix);
}
Camera.prototype.rotate = function(renderer){
mat4.translate(renderer.mvMatrix, [this.focusX, this.focusY, this.focusZ]);
mat4.rotate(renderer.mvMatrix, -renderer.degToRad(this.pitch), [1, 0, 0]);
mat4.rotate(renderer.mvMatrix, -renderer.degToRad(this.yaw), [0, 1, 0]);
mat4.rotate(renderer.mvMatrix, -renderer.degToRad(this.roll), [0, 0, 1]);
mat4.translate(renderer.mvMatrix, [-this.focusX, -this.focusY, -this.focusZ]);
}
Camera.prototype.lookAt = function(x, y, z){
this.focusX = x;
this.focusY = y;
this.focusZ = z;
this.panTo(x, y, z);
}
Camera.prototype.panTo = function(x, y, z){
this.targetFocusX = x;
this.targetFocusY = y;
this.targetFocusZ = z;
}
Camera.prototype.processPanning = function(renderer){
if( this.targetFocusX == this.focusX
&& this.targetFocusY == this.focusY
&& this.targetFocusZ == this.focusZ
) {
return;
}
var tx = this.targetFocusX - this.focusX;
var ty = this.targetFocusY - this.focusY;
var tz = this.targetFocusZ - this.focusZ;
var myPoint = {
x: this.focusX,
y: this.focusY,
z: this.focusZ
};
var thatPoint = {
x: this.targetFocusX,
y: this.targetFocusY,
z: this.targetFocusZ
};
var dist = engine.distance(myPoint, thatPoint);
if(dist > (this.panSpeed * (renderer.timeElapsed/1000))){
var velX = (tx/dist) * (this.panSpeed * (renderer.timeElapsed/1000));
var velY = (ty/dist) * (this.panSpeed * (renderer.timeElapsed/1000));
var velZ = (tz/dist) * (this.panSpeed * (renderer.timeElapsed/1000));
this.focusX += velX;
this.focusY += velY;
this.focusZ += velZ;
} else {
this.focusX = this.targetFocusX;
this.focusY = this.targetFocusY;
this.focusZ = this.targetFocusZ;
}
}