1
1
/**
2
- * @author Eberhard Gräther / http://egraether.com/
2
+ * @author Eberhard Graether / http://egraether.com/
3
3
4
4
* parameters = {
5
5
* fov: <float>,
11
11
* radius: <float>,
12
12
* screen: { width : <float>, height : <float>, offsetLeft : <float>, offsetTop : <float> },
13
13
14
+ * rotateSpeed: <float>,
14
15
* zoomSpeed: <float>,
15
16
* panSpeed: <float>,
16
17
20
21
* staticMoving: <bool>,
21
22
* dynamicDampingFactor: <float>,
22
23
24
+ * minDistance: <float>,
25
+ * maxDistance: <float>,
26
+
23
27
* keys: <Array>, // [ rotateKey, zoomKey, panKey ],
24
28
25
29
* domElement: <HTMLElement>,
@@ -39,6 +43,7 @@ THREE.TrackballCamera = function ( parameters ) {
39
43
this . screen = parameters . screen || { width : window . innerWidth , height : window . innerHeight , offsetLeft : 0 , offsetTop : 0 } ;
40
44
this . radius = parameters . radius || ( this . screen . width + this . screen . height ) / 4 ;
41
45
46
+ this . rotateSpeed = parameters . rotateSpeed || 1.0 ;
42
47
this . zoomSpeed = parameters . zoomSpeed || 1.2 ;
43
48
this . panSpeed = parameters . panSpeed || 0.3 ;
44
49
@@ -48,7 +53,10 @@ THREE.TrackballCamera = function ( parameters ) {
48
53
this . staticMoving = parameters . staticMoving || false ;
49
54
this . dynamicDampingFactor = parameters . dynamicDampingFactor || 0.2 ;
50
55
51
- this . keys = parameters . keys || [ 65 /*A*/ , 83 /*S*/ , 68 /*D*/ ] ;
56
+ this . minDistance = parameters . minDistance || 0 ;
57
+ this . maxDistance = parameters . maxDistance || Infinity ;
58
+
59
+ this . keys = parameters . keys || [ 16 /*SHIFT*/ , 18 /*ALT*/ , 17 /*CTRL*/ ] ;
52
60
53
61
this . useTarget = true ;
54
62
@@ -126,14 +134,20 @@ THREE.TrackballCamera = function ( parameters ) {
126
134
var axis = ( new THREE . Vector3 ( ) ) . cross ( _rotateStart , _rotateEnd ) . normalize ( ) ,
127
135
quaternion = new THREE . Quaternion ( ) ;
128
136
137
+ angle *= this . rotateSpeed ;
138
+
129
139
quaternion . setFromAxisAngle ( axis , - angle ) ;
130
140
131
141
quaternion . multiplyVector3 ( this . position ) ;
132
142
quaternion . multiplyVector3 ( this . up ) ;
133
143
134
- if ( ! this . staticMoving ) {
144
+ quaternion . multiplyVector3 ( _rotateEnd ) ;
145
+
146
+ if ( this . staticMoving ) {
147
+
148
+ _rotateStart = _rotateEnd ;
135
149
136
- quaternion . multiplyVector3 ( _rotateEnd ) ;
150
+ } else {
137
151
138
152
quaternion . setFromAxisAngle ( axis , angle * ( this . dynamicDampingFactor - 1.0 ) ) ;
139
153
quaternion . multiplyVector3 ( _rotateStart ) ;
@@ -196,15 +210,33 @@ THREE.TrackballCamera = function ( parameters ) {
196
210
}
197
211
198
212
} ;
199
-
200
- this . update = function ( parentMatrixWorld , forceUpdate , camera ) {
201
-
202
- if ( ! this . staticMoving ) {
203
213
204
- this . rotateCamera ( ) ;
214
+ this . checkDistances = function ( ) {
215
+
216
+ if ( ! this . noZoom || ! this . noPan ) {
217
+
218
+ if ( this . position . lengthSq ( ) > this . maxDistance * this . maxDistance ) {
219
+
220
+ this . position . setLength ( this . maxDistance ) ;
221
+
222
+ }
223
+
224
+ var eye = this . position . clone ( ) . subSelf ( this . target . position ) ;
225
+
226
+ if ( eye . lengthSq ( ) < this . minDistance * this . minDistance ) {
227
+
228
+ this . position . add ( this . target . position , eye . setLength ( this . minDistance ) ) ;
229
+
230
+ }
205
231
206
232
}
207
233
234
+ } ;
235
+
236
+ this . update = function ( parentMatrixWorld , forceUpdate , camera ) {
237
+
238
+ this . rotateCamera ( ) ;
239
+
208
240
if ( ! this . noZoom ) {
209
241
210
242
this . zoomCamera ( ) ;
@@ -216,11 +248,14 @@ THREE.TrackballCamera = function ( parameters ) {
216
248
this . panCamera ( ) ;
217
249
218
250
}
219
-
251
+
252
+ this . checkDistances ( ) ;
253
+
220
254
this . supr . update . call ( this , parentMatrixWorld , forceUpdate , camera ) ;
221
-
255
+
222
256
} ;
223
257
258
+
224
259
// listeners
225
260
226
261
function keydown ( event ) {
@@ -232,16 +267,19 @@ THREE.TrackballCamera = function ( parameters ) {
232
267
} else if ( event . keyCode === this . keys [ this . STATE . ROTATE ] ) {
233
268
234
269
_state = this . STATE . ROTATE ;
235
- _keyPressed = true ;
236
270
237
- } else if ( event . keyCode === this . keys [ this . STATE . ZOOM ] ) {
271
+ } else if ( event . keyCode === this . keys [ this . STATE . ZOOM ] && ! this . noZoom ) {
238
272
239
273
_state = this . STATE . ZOOM ;
240
- _keyPressed = true ;
241
274
242
- } else if ( event . keyCode === this . keys [ this . STATE . PAN ] ) {
275
+ } else if ( event . keyCode === this . keys [ this . STATE . PAN ] && ! this . noPan ) {
243
276
244
277
_state = this . STATE . PAN ;
278
+
279
+ }
280
+
281
+ if ( _state !== this . STATE . NONE ) {
282
+
245
283
_keyPressed = true ;
246
284
247
285
}
@@ -271,11 +309,11 @@ THREE.TrackballCamera = function ( parameters ) {
271
309
272
310
_rotateStart = _rotateEnd = this . getMouseProjectionOnBall ( event . clientX , event . clientY ) ;
273
311
274
- } else if ( _state === this . STATE . ZOOM ) {
312
+ } else if ( _state === this . STATE . ZOOM && ! this . noZoom ) {
275
313
276
314
_zoomStart = _zoomEnd = this . getMouseOnScreen ( event . clientX , event . clientY ) ;
277
315
278
- } else {
316
+ } else if ( ! this . noPan ) {
279
317
280
318
_panStart = _panEnd = this . getMouseOnScreen ( event . clientX , event . clientY ) ;
281
319
@@ -305,12 +343,6 @@ THREE.TrackballCamera = function ( parameters ) {
305
343
306
344
_rotateEnd = this . getMouseProjectionOnBall ( event . clientX , event . clientY ) ;
307
345
308
- if ( this . staticMoving ) {
309
-
310
- this . rotateCamera ( ) ;
311
-
312
- }
313
-
314
346
} else if ( _state === this . STATE . ZOOM && ! this . noZoom ) {
315
347
316
348
_zoomEnd = this . getMouseOnScreen ( event . clientX , event . clientY ) ;
0 commit comments