1 | var Point3d = require('./Point3d');
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function Camera() {
|
14 | this.armLocation = new Point3d();
|
15 | this.armRotation = {};
|
16 | this.armRotation.horizontal = 0;
|
17 | this.armRotation.vertical = 0;
|
18 | this.armLength = 1.7;
|
19 | this.cameraOffset = new Point3d();
|
20 | this.offsetMultiplier = 0.6;
|
21 |
|
22 | this.cameraLocation = new Point3d();
|
23 | this.cameraRotation = new Point3d(0.5*Math.PI, 0, 0);
|
24 |
|
25 | this.calculateCameraOrientation();
|
26 | }
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | Camera.prototype.setOffset = function(x, y) {
|
34 | var abs = Math.abs,
|
35 | sign = Math.sign,
|
36 | mul = this.offsetMultiplier,
|
37 | border = this.armLength * mul;
|
38 |
|
39 | if (abs(x) > border) {
|
40 | x = sign(x) * border;
|
41 | }
|
42 | if (abs(y) > border) {
|
43 | y = sign(y) * border;
|
44 | }
|
45 | this.cameraOffset.x = x;
|
46 | this.cameraOffset.y = y;
|
47 | this.calculateCameraOrientation();
|
48 | };
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | Camera.prototype.getOffset = function() {
|
56 | return this.cameraOffset;
|
57 | };
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | Camera.prototype.setArmLocation = function(x, y, z) {
|
66 | this.armLocation.x = x;
|
67 | this.armLocation.y = y;
|
68 | this.armLocation.z = z;
|
69 |
|
70 | this.calculateCameraOrientation();
|
71 | };
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | Camera.prototype.setArmRotation = function(horizontal, vertical) {
|
82 | if (horizontal !== undefined) {
|
83 | this.armRotation.horizontal = horizontal;
|
84 | }
|
85 |
|
86 | if (vertical !== undefined) {
|
87 | this.armRotation.vertical = vertical;
|
88 | if (this.armRotation.vertical < 0) this.armRotation.vertical = 0;
|
89 | if (this.armRotation.vertical > 0.5*Math.PI) this.armRotation.vertical = 0.5*Math.PI;
|
90 | }
|
91 |
|
92 | if (horizontal !== undefined || vertical !== undefined) {
|
93 | this.calculateCameraOrientation();
|
94 | }
|
95 | };
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | Camera.prototype.getArmRotation = function() {
|
102 | var rot = {};
|
103 | rot.horizontal = this.armRotation.horizontal;
|
104 | rot.vertical = this.armRotation.vertical;
|
105 |
|
106 | return rot;
|
107 | };
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | Camera.prototype.setArmLength = function(length) {
|
114 | if (length === undefined)
|
115 | return;
|
116 |
|
117 | this.armLength = length;
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | if (this.armLength < 0.71) this.armLength = 0.71;
|
123 | if (this.armLength > 5.0) this.armLength = 5.0;
|
124 |
|
125 | this.setOffset(this.cameraOffset.x, this.cameraOffset.y);
|
126 | this.calculateCameraOrientation();
|
127 | };
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | Camera.prototype.getArmLength = function() {
|
134 | return this.armLength;
|
135 | };
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | Camera.prototype.getCameraLocation = function() {
|
142 | return this.cameraLocation;
|
143 | };
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | Camera.prototype.getCameraRotation = function() {
|
150 | return this.cameraRotation;
|
151 | };
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | Camera.prototype.calculateCameraOrientation = function() {
|
158 |
|
159 | this.cameraLocation.x = this.armLocation.x - this.armLength * Math.sin(this.armRotation.horizontal) * Math.cos(this.armRotation.vertical);
|
160 | this.cameraLocation.y = this.armLocation.y - this.armLength * Math.cos(this.armRotation.horizontal) * Math.cos(this.armRotation.vertical);
|
161 | this.cameraLocation.z = this.armLocation.z + this.armLength * Math.sin(this.armRotation.vertical);
|
162 |
|
163 |
|
164 | this.cameraRotation.x = Math.PI/2 - this.armRotation.vertical;
|
165 | this.cameraRotation.y = 0;
|
166 | this.cameraRotation.z = -this.armRotation.horizontal;
|
167 |
|
168 | var xa = this.cameraRotation.x;
|
169 | var za = this.cameraRotation.z;
|
170 | var dx = this.cameraOffset.x;
|
171 | var dy = this.cameraOffset.y;
|
172 | var sin = Math.sin, cos = Math.cos;
|
173 |
|
174 | this.cameraLocation.x = this.cameraLocation.x + dx * cos(za) + dy * - sin(za) * cos(xa);
|
175 | this.cameraLocation.y = this.cameraLocation.y + dx * sin(za) + dy * cos(za) * cos(xa);
|
176 | this.cameraLocation.z = this.cameraLocation.z + dy * sin(xa);
|
177 | };
|
178 |
|
179 | module.exports = Camera;
|