Skip to content

Commit f4a08e5

Browse files
committed
added minMaxDistances to TrackballCamera and some refactorings
1 parent 6d08b49 commit f4a08e5

File tree

1 file changed

+55
-23
lines changed

1 file changed

+55
-23
lines changed

src/extras/cameras/TrackballCamera.js

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @author Eberhard Gräther / http://egraether.com/
2+
* @author Eberhard Graether / http://egraether.com/
33
44
* parameters = {
55
* fov: <float>,
@@ -11,6 +11,7 @@
1111
* radius: <float>,
1212
* screen: { width : <float>, height : <float>, offsetLeft : <float>, offsetTop : <float> },
1313
14+
* rotateSpeed: <float>,
1415
* zoomSpeed: <float>,
1516
* panSpeed: <float>,
1617
@@ -20,6 +21,9 @@
2021
* staticMoving: <bool>,
2122
* dynamicDampingFactor: <float>,
2223
24+
* minDistance: <float>,
25+
* maxDistance: <float>,
26+
2327
* keys: <Array>, // [ rotateKey, zoomKey, panKey ],
2428
2529
* domElement: <HTMLElement>,
@@ -39,6 +43,7 @@ THREE.TrackballCamera = function ( parameters ) {
3943
this.screen = parameters.screen || { width : window.innerWidth, height : window.innerHeight, offsetLeft : 0, offsetTop : 0 };
4044
this.radius = parameters.radius || ( this.screen.width + this.screen.height ) / 4;
4145

46+
this.rotateSpeed = parameters.rotateSpeed || 1.0;
4247
this.zoomSpeed = parameters.zoomSpeed || 1.2;
4348
this.panSpeed = parameters.panSpeed || 0.3;
4449

@@ -48,7 +53,10 @@ THREE.TrackballCamera = function ( parameters ) {
4853
this.staticMoving = parameters.staticMoving || false;
4954
this.dynamicDampingFactor = parameters.dynamicDampingFactor || 0.2;
5055

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*/ ];
5260

5361
this.useTarget = true;
5462

@@ -126,14 +134,20 @@ THREE.TrackballCamera = function ( parameters ) {
126134
var axis = (new THREE.Vector3()).cross( _rotateStart, _rotateEnd ).normalize(),
127135
quaternion = new THREE.Quaternion();
128136

137+
angle *= this.rotateSpeed;
138+
129139
quaternion.setFromAxisAngle( axis, -angle );
130140

131141
quaternion.multiplyVector3( this.position );
132142
quaternion.multiplyVector3( this.up );
133143

134-
if ( !this.staticMoving ) {
144+
quaternion.multiplyVector3( _rotateEnd );
145+
146+
if ( this.staticMoving ) {
147+
148+
_rotateStart = _rotateEnd;
135149

136-
quaternion.multiplyVector3( _rotateEnd );
150+
} else {
137151

138152
quaternion.setFromAxisAngle( axis, angle * ( this.dynamicDampingFactor - 1.0 ) );
139153
quaternion.multiplyVector3( _rotateStart );
@@ -196,15 +210,33 @@ THREE.TrackballCamera = function ( parameters ) {
196210
}
197211

198212
};
199-
200-
this.update = function( parentMatrixWorld, forceUpdate, camera ) {
201-
202-
if ( !this.staticMoving ) {
203213

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+
}
205231

206232
}
207233

234+
};
235+
236+
this.update = function( parentMatrixWorld, forceUpdate, camera ) {
237+
238+
this.rotateCamera();
239+
208240
if ( !this.noZoom ) {
209241

210242
this.zoomCamera();
@@ -216,11 +248,14 @@ THREE.TrackballCamera = function ( parameters ) {
216248
this.panCamera();
217249

218250
}
219-
251+
252+
this.checkDistances();
253+
220254
this.supr.update.call( this, parentMatrixWorld, forceUpdate, camera );
221-
255+
222256
};
223257

258+
224259
// listeners
225260

226261
function keydown( event ) {
@@ -232,16 +267,19 @@ THREE.TrackballCamera = function ( parameters ) {
232267
} else if ( event.keyCode === this.keys[ this.STATE.ROTATE ] ) {
233268

234269
_state = this.STATE.ROTATE;
235-
_keyPressed = true;
236270

237-
} else if ( event.keyCode === this.keys[ this.STATE.ZOOM ] ) {
271+
} else if ( event.keyCode === this.keys[ this.STATE.ZOOM ] && !this.noZoom ) {
238272

239273
_state = this.STATE.ZOOM;
240-
_keyPressed = true;
241274

242-
} else if ( event.keyCode === this.keys[ this.STATE.PAN ] ) {
275+
} else if ( event.keyCode === this.keys[ this.STATE.PAN ] && !this.noPan ) {
243276

244277
_state = this.STATE.PAN;
278+
279+
}
280+
281+
if ( _state !== this.STATE.NONE ) {
282+
245283
_keyPressed = true;
246284

247285
}
@@ -271,11 +309,11 @@ THREE.TrackballCamera = function ( parameters ) {
271309

272310
_rotateStart = _rotateEnd = this.getMouseProjectionOnBall( event.clientX, event.clientY );
273311

274-
} else if ( _state === this.STATE.ZOOM ) {
312+
} else if ( _state === this.STATE.ZOOM && !this.noZoom ) {
275313

276314
_zoomStart = _zoomEnd = this.getMouseOnScreen( event.clientX, event.clientY );
277315

278-
} else {
316+
} else if ( !this.noPan ) {
279317

280318
_panStart = _panEnd = this.getMouseOnScreen( event.clientX, event.clientY );
281319

@@ -305,12 +343,6 @@ THREE.TrackballCamera = function ( parameters ) {
305343

306344
_rotateEnd = this.getMouseProjectionOnBall( event.clientX, event.clientY );
307345

308-
if ( this.staticMoving ) {
309-
310-
this.rotateCamera();
311-
312-
}
313-
314346
} else if ( _state === this.STATE.ZOOM && !this.noZoom ) {
315347

316348
_zoomEnd = this.getMouseOnScreen( event.clientX, event.clientY );

0 commit comments

Comments
 (0)