TrackballControls.js 13.8 KB
Newer Older
1
/**
2
 * @author Eberhard Graether / http://egraether.com/
M
Mark Lundin 已提交
3
 * @author Mark Lundin 	/ http://mark-lundin.com
4 5
 * @author Simone Manini / http://daron1337.github.io
 * @author Luca Antiga 	/ http://lantiga.github.io
6 7
 */

8
THREE.TrackballControls = function ( object, domElement ) {
9

M
Mr.doob 已提交
10
	var _this = this;
G
gero3 已提交
11
	var STATE = { NONE: - 1, ROTATE: 0, ZOOM: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_ZOOM_PAN: 4 };
12

13
	this.object = object;
14
	this.domElement = ( domElement !== undefined ) ? domElement : document;
15

16
	// API
17

18 19
	this.enabled = true;

20
	this.screen = { left: 0, top: 0, width: 0, height: 0 };
21

22 23 24
	this.rotateSpeed = 1.0;
	this.zoomSpeed = 1.2;
	this.panSpeed = 0.3;
25

26
	this.noRotate = false;
27 28
	this.noZoom = false;
	this.noPan = false;
29

30 31
	this.staticMoving = false;
	this.dynamicDampingFactor = 0.2;
32

33 34
	this.minDistance = 0;
	this.maxDistance = Infinity;
35

36
	this.keys = [ 65 /*A*/, 83 /*S*/, 68 /*D*/ ];
37

38
	// internals
39

M
Mr.doob 已提交
40 41
	this.target = new THREE.Vector3();

42 43
	var EPS = 0.000001;

M
Mr.doob 已提交
44
	var lastPosition = new THREE.Vector3();
45

M
Mr.doob 已提交
46 47
	var _state = STATE.NONE,
	_prevState = STATE.NONE,
48

49 50
	_eye = new THREE.Vector3(),

51 52 53 54 55 56
	_movePrev = new THREE.Vector2(),
	_moveCurr = new THREE.Vector2(),

	_lastAxis = new THREE.Vector3(),
	_lastAngle = 0,

57 58 59
	_zoomStart = new THREE.Vector2(),
	_zoomEnd = new THREE.Vector2(),

60 61 62
	_touchZoomDistanceStart = 0,
	_touchZoomDistanceEnd = 0,

63 64
	_panStart = new THREE.Vector2(),
	_panEnd = new THREE.Vector2();
65

W
WestLangley 已提交
66 67 68 69 70 71
	// for reset

	this.target0 = this.target.clone();
	this.position0 = this.object.position.clone();
	this.up0 = this.object.up.clone();

M
Mr.doob 已提交
72 73 74
	// events

	var changeEvent = { type: 'change' };
G
gero3 已提交
75 76
	var startEvent = { type: 'start' };
	var endEvent = { type: 'end' };
M
Mr.doob 已提交
77

78

79
	// methods
80

81 82
	this.handleResize = function () {

83
		if ( this.domElement === document ) {
84

85 86 87 88 89 90 91
			this.screen.left = 0;
			this.screen.top = 0;
			this.screen.width = window.innerWidth;
			this.screen.height = window.innerHeight;

		} else {

92
			var box = this.domElement.getBoundingClientRect();
93
			// adjustments come from similar code in the jquery offset() function
94 95 96 97 98
			var d = this.domElement.ownerDocument.documentElement;
			this.screen.left = box.left + window.pageXOffset - d.clientLeft;
			this.screen.top = box.top + window.pageYOffset - d.clientTop;
			this.screen.width = box.width;
			this.screen.height = box.height;
99 100

		}
101

102 103
	};

104
	this.handleEvent = function ( event ) {
105

106
		if ( typeof this[ event.type ] == 'function' ) {
107

108
			this[ event.type ]( event );
109

110
		}
111

112
	};
113

M
Mr.doob 已提交
114
	var getMouseOnScreen = ( function () {
115

116
		var vector = new THREE.Vector2();
117

118
		return function getMouseOnScreen( pageX, pageY ) {
M
Mr.doob 已提交
119 120 121 122 123 124 125

			vector.set(
				( pageX - _this.screen.left ) / _this.screen.width,
				( pageY - _this.screen.top ) / _this.screen.height
			);

			return vector;
126

M
Mr.doob 已提交
127
		};
128

M
Mr.doob 已提交
129
	}() );
130

131 132 133 134
	var getMouseOnCircle = ( function () {

		var vector = new THREE.Vector2();

135
		return function getMouseOnCircle( pageX, pageY ) {
136 137 138

			vector.set(
				( ( pageX - _this.screen.width * 0.5 - _this.screen.left ) / ( _this.screen.width * 0.5 ) ),
139
				( ( _this.screen.height + 2 * ( _this.screen.top - pageY ) ) / _this.screen.width ) // screen.width intentional
140 141 142
			);

			return vector;
G
gero3 已提交
143

144 145 146 147
		};

	}() );

G
gero3 已提交
148
	this.rotateCamera = ( function() {
149

150
		var axis = new THREE.Vector3(),
151 152 153 154 155 156
			quaternion = new THREE.Quaternion(),
			eyeDirection = new THREE.Vector3(),
			objectUpDirection = new THREE.Vector3(),
			objectSidewaysDirection = new THREE.Vector3(),
			moveDirection = new THREE.Vector3(),
			angle;
157

158
		return function rotateCamera() {
159

160 161
			moveDirection.set( _moveCurr.x - _movePrev.x, _moveCurr.y - _movePrev.y, 0 );
			angle = moveDirection.length();
162

163
			if ( angle ) {
164

165
				_eye.copy( _this.object.position ).sub( _this.target );
166

167 168 169
				eyeDirection.copy( _eye ).normalize();
				objectUpDirection.copy( _this.object.up ).normalize();
				objectSidewaysDirection.crossVectors( objectUpDirection, eyeDirection ).normalize();
170

171 172
				objectUpDirection.setLength( _moveCurr.y - _movePrev.y );
				objectSidewaysDirection.setLength( _moveCurr.x - _movePrev.x );
173

174
				moveDirection.copy( objectUpDirection.add( objectSidewaysDirection ) );
175

176
				axis.crossVectors( moveDirection, _eye ).normalize();
177

178 179
				angle *= _this.rotateSpeed;
				quaternion.setFromAxisAngle( axis, angle );
180

181 182
				_eye.applyQuaternion( quaternion );
				_this.object.up.applyQuaternion( quaternion );
183

184 185
				_lastAxis.copy( axis );
				_lastAngle = angle;
186

G
gero3 已提交
187
			} else if ( ! _this.staticMoving && _lastAngle ) {
188

189 190 191 192 193
				_lastAngle *= Math.sqrt( 1.0 - _this.dynamicDampingFactor );
				_eye.copy( _this.object.position ).sub( _this.target );
				quaternion.setFromAxisAngle( _lastAxis, _lastAngle );
				_eye.applyQuaternion( quaternion );
				_this.object.up.applyQuaternion( quaternion );
194

195
			}
196

197 198
			_movePrev.copy( _moveCurr );

199
		};
200

G
gero3 已提交
201
	}() );
202

203

M
Mr.doob 已提交
204
	this.zoomCamera = function () {
205

206 207
		var factor;

M
Mr.doob 已提交
208
		if ( _state === STATE.TOUCH_ZOOM_PAN ) {
209

210
			factor = _touchZoomDistanceStart / _touchZoomDistanceEnd;
211
			_touchZoomDistanceStart = _touchZoomDistanceEnd;
212
			_eye.multiplyScalar( factor );
213

214
		} else {
215

216
			factor = 1.0 + ( _zoomEnd.y - _zoomStart.y ) * _this.zoomSpeed;
217

218
			if ( factor !== 1.0 && factor > 0.0 ) {
219

220
				_eye.multiplyScalar( factor );
221

222
			}
223

224
			if ( _this.staticMoving ) {
225

226
				_zoomStart.copy( _zoomEnd );
227

228
			} else {
229

230
				_zoomStart.y += ( _zoomEnd.y - _zoomStart.y ) * this.dynamicDampingFactor;
231 232

			}
233 234 235

		}

236
	};
237

G
gero3 已提交
238
	this.panCamera = ( function() {
239

240 241 242
		var mouseChange = new THREE.Vector2(),
			objectUp = new THREE.Vector3(),
			pan = new THREE.Vector3();
243

244
		return function panCamera() {
245

246
			mouseChange.copy( _panEnd ).sub( _panStart );
247

248
			if ( mouseChange.lengthSq() ) {
249

250
				mouseChange.multiplyScalar( _eye.length() * _this.panSpeed );
251

252 253
				pan.copy( _eye ).cross( _this.object.up ).setLength( mouseChange.x );
				pan.add( objectUp.copy( _this.object.up ).setLength( mouseChange.y ) );
254

255 256
				_this.object.position.add( pan );
				_this.target.add( pan );
257

258
				if ( _this.staticMoving ) {
259

260
					_panStart.copy( _panEnd );
261

262 263 264
				} else {

					_panStart.add( mouseChange.subVectors( _panEnd, _panStart ).multiplyScalar( _this.dynamicDampingFactor ) );
265

266 267 268
				}

			}
G
gero3 已提交
269

270
		};
271

G
gero3 已提交
272
	}() );
273

M
Mr.doob 已提交
274
	this.checkDistances = function () {
275

G
gero3 已提交
276
		if ( ! _this.noZoom || ! _this.noPan ) {
277

278
			if ( _eye.lengthSq() > _this.maxDistance * _this.maxDistance ) {
279

280
				_this.object.position.addVectors( _this.target, _eye.setLength( _this.maxDistance ) );
M
michaelg 已提交
281
				_zoomStart.copy( _zoomEnd );
282 283 284

			}

285
			if ( _eye.lengthSq() < _this.minDistance * _this.minDistance ) {
286

287
				_this.object.position.addVectors( _this.target, _eye.setLength( _this.minDistance ) );
M
michaelg 已提交
288
				_zoomStart.copy( _zoomEnd );
289 290

			}
291 292 293

		}

294 295
	};

M
Mr.doob 已提交
296
	this.update = function () {
M
Mr.doob 已提交
297

298
		_eye.subVectors( _this.object.position, _this.target );
M
Mr.doob 已提交
299

G
gero3 已提交
300
		if ( ! _this.noRotate ) {
M
Mr.doob 已提交
301 302 303 304

			_this.rotateCamera();

		}
305

G
gero3 已提交
306
		if ( ! _this.noZoom ) {
M
Mr.doob 已提交
307 308 309 310 311

			_this.zoomCamera();

		}

G
gero3 已提交
312
		if ( ! _this.noPan ) {
M
Mr.doob 已提交
313 314 315 316 317

			_this.panCamera();

		}

318
		_this.object.position.addVectors( _this.target, _eye );
M
Mr.doob 已提交
319 320 321 322 323

		_this.checkDistances();

		_this.object.lookAt( _this.target );

324
		if ( lastPosition.distanceToSquared( _this.object.position ) > EPS ) {
325

M
Mr.doob 已提交
326
			_this.dispatchEvent( changeEvent );
M
Mr.doob 已提交
327 328 329 330 331 332 333

			lastPosition.copy( _this.object.position );

		}

	};

W
WestLangley 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346
	this.reset = function () {

		_state = STATE.NONE;
		_prevState = STATE.NONE;

		_this.target.copy( _this.target0 );
		_this.object.position.copy( _this.position0 );
		_this.object.up.copy( _this.up0 );

		_eye.subVectors( _this.object.position, _this.target );

		_this.object.lookAt( _this.target );

347
		_this.dispatchEvent( changeEvent );
W
WestLangley 已提交
348 349 350 351 352

		lastPosition.copy( _this.object.position );

	};

353
	// listeners
354

355
	function keydown( event ) {
356

357
		if ( _this.enabled === false ) return;
358

359
		window.removeEventListener( 'keydown', keydown );
360

M
Mr.doob 已提交
361
		_prevState = _state;
362

363
		if ( _state !== STATE.NONE ) {
364

365
			return;
366

G
gero3 已提交
367
		} else if ( event.keyCode === _this.keys[ STATE.ROTATE ] && ! _this.noRotate ) {
368

369
			_state = STATE.ROTATE;
370

G
gero3 已提交
371
		} else if ( event.keyCode === _this.keys[ STATE.ZOOM ] && ! _this.noZoom ) {
372

373
			_state = STATE.ZOOM;
374

G
gero3 已提交
375
		} else if ( event.keyCode === _this.keys[ STATE.PAN ] && ! _this.noPan ) {
376

377
			_state = STATE.PAN;
378 379

		}
380

M
Mr.doob 已提交
381
	}
382

383
	function keyup( event ) {
384

385
		if ( _this.enabled === false ) return;
386

M
Mr.doob 已提交
387
		_state = _prevState;
388

389
		window.addEventListener( 'keydown', keydown, false );
390

M
Mr.doob 已提交
391
	}
392

393
	function mousedown( event ) {
394

395
		if ( _this.enabled === false ) return;
396

397 398 399
		event.preventDefault();
		event.stopPropagation();

400
		if ( _state === STATE.NONE ) {
401

402
			_state = event.button;
403

M
Mr.doob 已提交
404
		}
405

G
gero3 已提交
406
		if ( _state === STATE.ROTATE && ! _this.noRotate ) {
407

408
			_moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
G
gero3 已提交
409
			_movePrev.copy( _moveCurr );
410

G
gero3 已提交
411
		} else if ( _state === STATE.ZOOM && ! _this.noZoom ) {
412

M
Mr.doob 已提交
413
			_zoomStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
G
gero3 已提交
414
			_zoomEnd.copy( _zoomStart );
415

G
gero3 已提交
416
		} else if ( _state === STATE.PAN && ! _this.noPan ) {
417

M
Mr.doob 已提交
418
			_panStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
G
gero3 已提交
419
			_panEnd.copy( _panStart );
420

421
		}
M
Mr.doob 已提交
422

M
Mr.doob 已提交
423 424
		document.addEventListener( 'mousemove', mousemove, false );
		document.addEventListener( 'mouseup', mouseup, false );
425

M
Mr.doob 已提交
426
		_this.dispatchEvent( startEvent );
427

M
Mr.doob 已提交
428
	}
429

430
	function mousemove( event ) {
431

432 433
		if ( _this.enabled === false ) return;

434 435 436
		event.preventDefault();
		event.stopPropagation();

G
gero3 已提交
437
		if ( _state === STATE.ROTATE && ! _this.noRotate ) {
438

G
gero3 已提交
439
			_movePrev.copy( _moveCurr );
440
			_moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
441

G
gero3 已提交
442
		} else if ( _state === STATE.ZOOM && ! _this.noZoom ) {
443

M
Mr.doob 已提交
444
			_zoomEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
445

G
gero3 已提交
446
		} else if ( _state === STATE.PAN && ! _this.noPan ) {
447

M
Mr.doob 已提交
448
			_panEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
449

450
		}
451

M
Mr.doob 已提交
452
	}
453

454
	function mouseup( event ) {
455

456
		if ( _this.enabled === false ) return;
457

458 459 460
		event.preventDefault();
		event.stopPropagation();

461
		_state = STATE.NONE;
462

M
Mr.doob 已提交
463 464
		document.removeEventListener( 'mousemove', mousemove );
		document.removeEventListener( 'mouseup', mouseup );
465
		_this.dispatchEvent( endEvent );
466

M
Mr.doob 已提交
467
	}
468

469 470
	function mousewheel( event ) {

471
		if ( _this.enabled === false ) return;
472

473 474 475
		event.preventDefault();
		event.stopPropagation();

476 477
		switch ( event.deltaMode ) {

F
Fred Ludlow 已提交
478 479 480 481 482
                        case 2:
                                // Zoom in pages
                                _zoomStart.y -= event.deltaY * 0.025;
                                break;

483
			case 1:
F
Fred Ludlow 已提交
484
                                // Zoom in lines
485 486 487 488
				_zoomStart.y -= event.deltaY * 0.01;
				break;

			default:
F
Fred Ludlow 已提交
489
				// undefined, 0, assume pixels
490 491
				_zoomStart.y -= event.deltaY * 0.00025;
				break;
F
Fred Ludlow 已提交
492

493
		}
494

495 496
		_this.dispatchEvent( startEvent );
		_this.dispatchEvent( endEvent );
497 498 499

	}

M
Mr.doob 已提交
500 501
	function touchstart( event ) {

502
		if ( _this.enabled === false ) return;
M
Mr.doob 已提交
503 504 505 506

		switch ( event.touches.length ) {

			case 1:
507
				_state = STATE.TOUCH_ROTATE;
508
				_moveCurr.copy( getMouseOnCircle( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ) );
G
gero3 已提交
509
				_movePrev.copy( _moveCurr );
M
Mr.doob 已提交
510
				break;
511

512
			default: // 2 or more
M
Mr.doob 已提交
513
				_state = STATE.TOUCH_ZOOM_PAN;
514 515 516
				var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
				var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
				_touchZoomDistanceEnd = _touchZoomDistanceStart = Math.sqrt( dx * dx + dy * dy );
M
Mr.doob 已提交
517 518 519

				var x = ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX ) / 2;
				var y = ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY ) / 2;
520
				_panStart.copy( getMouseOnScreen( x, y ) );
M
Mr.doob 已提交
521
				_panEnd.copy( _panStart );
M
Mr.doob 已提交
522 523 524
				break;

		}
525

526
		_this.dispatchEvent( startEvent );
M
Mr.doob 已提交
527 528 529 530 531

	}

	function touchmove( event ) {

532
		if ( _this.enabled === false ) return;
M
Mr.doob 已提交
533

534 535 536
		event.preventDefault();
		event.stopPropagation();

M
Mr.doob 已提交
537 538 539
		switch ( event.touches.length ) {

			case 1:
G
gero3 已提交
540
				_movePrev.copy( _moveCurr );
S
Shaw 已提交
541
				_moveCurr.copy( getMouseOnCircle( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ) );
M
Mr.doob 已提交
542
				break;
543

544
			default: // 2 or more
545 546
				var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
				var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
547
				_touchZoomDistanceEnd = Math.sqrt( dx * dx + dy * dy );
M
Mr.doob 已提交
548 549 550 551

				var x = ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX ) / 2;
				var y = ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY ) / 2;
				_panEnd.copy( getMouseOnScreen( x, y ) );
M
Mr.doob 已提交
552 553
				break;

554 555 556 557 558 559 560 561 562 563
		}

	}

	function touchend( event ) {

		if ( _this.enabled === false ) return;

		switch ( event.touches.length ) {

S
Shaw 已提交
564 565 566 567
			case 0:
				_state = STATE.NONE;
				break;

568
			case 1:
569
				_state = STATE.TOUCH_ROTATE;
S
Shaw 已提交
570 571
				_moveCurr.copy( getMouseOnCircle( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ) );
				_movePrev.copy( _moveCurr );
572 573
				break;

M
Mr.doob 已提交
574 575
		}

576
		_this.dispatchEvent( endEvent );
577

M
Mr.doob 已提交
578 579
	}

D
dubejf 已提交
580
	function contextmenu( event ) {
G
gero3 已提交
581

582 583
		if ( _this.enabled === false ) return;

G
gero3 已提交
584 585
		event.preventDefault();

D
dubejf 已提交
586
	}
587

D
dubejf 已提交
588 589 590 591
	this.dispose = function() {

		this.domElement.removeEventListener( 'contextmenu', contextmenu, false );
		this.domElement.removeEventListener( 'mousedown', mousedown, false );
M
Mr.doob 已提交
592
		this.domElement.removeEventListener( 'wheel', mousewheel, false );
D
dubejf 已提交
593 594 595 596 597 598 599

		this.domElement.removeEventListener( 'touchstart', touchstart, false );
		this.domElement.removeEventListener( 'touchend', touchend, false );
		this.domElement.removeEventListener( 'touchmove', touchmove, false );

		document.removeEventListener( 'mousemove', mousemove, false );
		document.removeEventListener( 'mouseup', mouseup, false );
M
Mr.doob 已提交
600

601 602
		window.removeEventListener( 'keydown', keydown, false );
		window.removeEventListener( 'keyup', keyup, false );
D
dubejf 已提交
603

M
Mr.doob 已提交
604
	};
D
dubejf 已提交
605 606 607

	this.domElement.addEventListener( 'contextmenu', contextmenu, false );
	this.domElement.addEventListener( 'mousedown', mousedown, false );
M
Mr.doob 已提交
608
	this.domElement.addEventListener( 'wheel', mousewheel, false );
M
Mr.doob 已提交
609 610

	this.domElement.addEventListener( 'touchstart', touchstart, false );
611
	this.domElement.addEventListener( 'touchend', touchend, false );
M
Mr.doob 已提交
612
	this.domElement.addEventListener( 'touchmove', touchmove, false );
M
Mr.doob 已提交
613

614 615
	window.addEventListener( 'keydown', keydown, false );
	window.addEventListener( 'keyup', keyup, false );
616

617 618
	this.handleResize();

619 620 621
	// force an update at start
	this.update();

622
};
623

M
Mr.doob 已提交
624
THREE.TrackballControls.prototype = Object.create( THREE.EventDispatcher.prototype );
625
THREE.TrackballControls.prototype.constructor = THREE.TrackballControls;