TrackballControls.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /**
  2. * @author Eberhard Graether / http://egraether.com/
  3. * @author Mark Lundin / http://mark-lundin.com
  4. * @author Simone Manini / http://daron1337.github.io
  5. * @author Luca Antiga / http://lantiga.github.io
  6. */
  7. THREE.TrackballControls = function ( object, domElement ) {
  8. var _this = this;
  9. var STATE = { NONE: - 1, ROTATE: 0, ZOOM: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_ZOOM_PAN: 4 };
  10. this.object = object;
  11. this.domElement = ( domElement !== undefined ) ? domElement : document;
  12. // API
  13. this.enabled = true;
  14. this.screen = { left: 0, top: 0, width: 0, height: 0 };
  15. this.rotateSpeed = 1.0;
  16. this.zoomSpeed = 1.2;
  17. this.panSpeed = 0.3;
  18. this.noRotate = false;
  19. this.noZoom = false;
  20. this.noPan = false;
  21. this.staticMoving = false;
  22. this.dynamicDampingFactor = 0.2;
  23. this.minDistance = 0;
  24. this.maxDistance = Infinity;
  25. this.keys = [ 65 /*A*/, 83 /*S*/, 68 /*D*/ ];
  26. this.mouseButtons = { LEFT: THREE.MOUSE.ROTATE, MIDDLE: THREE.MOUSE.ZOOM, RIGHT: THREE.MOUSE.PAN };
  27. // internals
  28. this.target = new THREE.Vector3();
  29. var EPS = 0.000001;
  30. var lastPosition = new THREE.Vector3();
  31. var lastZoom = 1;
  32. var _state = STATE.NONE,
  33. _keyState = STATE.NONE,
  34. _eye = new THREE.Vector3(),
  35. _movePrev = new THREE.Vector2(),
  36. _moveCurr = new THREE.Vector2(),
  37. _lastAxis = new THREE.Vector3(),
  38. _lastAngle = 0,
  39. _zoomStart = new THREE.Vector2(),
  40. _zoomEnd = new THREE.Vector2(),
  41. _touchZoomDistanceStart = 0,
  42. _touchZoomDistanceEnd = 0,
  43. _panStart = new THREE.Vector2(),
  44. _panEnd = new THREE.Vector2();
  45. // for reset
  46. this.target0 = this.target.clone();
  47. this.position0 = this.object.position.clone();
  48. this.up0 = this.object.up.clone();
  49. this.zoom0 = this.object.zoom;
  50. // events
  51. var changeEvent = { type: 'change' };
  52. var startEvent = { type: 'start' };
  53. var endEvent = { type: 'end' };
  54. // methods
  55. this.handleResize = function () {
  56. if ( this.domElement === document ) {
  57. this.screen.left = 0;
  58. this.screen.top = 0;
  59. this.screen.width = window.innerWidth;
  60. this.screen.height = window.innerHeight;
  61. } else {
  62. var box = this.domElement.getBoundingClientRect();
  63. // adjustments come from similar code in the jquery offset() function
  64. var d = this.domElement.ownerDocument.documentElement;
  65. this.screen.left = box.left + window.pageXOffset - d.clientLeft;
  66. this.screen.top = box.top + window.pageYOffset - d.clientTop;
  67. this.screen.width = box.width;
  68. this.screen.height = box.height;
  69. }
  70. };
  71. var getMouseOnScreen = ( function () {
  72. var vector = new THREE.Vector2();
  73. return function getMouseOnScreen( pageX, pageY ) {
  74. vector.set(
  75. ( pageX - _this.screen.left ) / _this.screen.width,
  76. ( pageY - _this.screen.top ) / _this.screen.height
  77. );
  78. return vector;
  79. };
  80. }() );
  81. var getMouseOnCircle = ( function () {
  82. var vector = new THREE.Vector2();
  83. return function getMouseOnCircle( pageX, pageY ) {
  84. vector.set(
  85. ( ( pageX - _this.screen.width * 0.5 - _this.screen.left ) / ( _this.screen.width * 0.5 ) ),
  86. ( ( _this.screen.height + 2 * ( _this.screen.top - pageY ) ) / _this.screen.width ) // screen.width intentional
  87. );
  88. return vector;
  89. };
  90. }() );
  91. this.rotateCamera = ( function () {
  92. var axis = new THREE.Vector3(),
  93. quaternion = new THREE.Quaternion(),
  94. eyeDirection = new THREE.Vector3(),
  95. objectUpDirection = new THREE.Vector3(),
  96. objectSidewaysDirection = new THREE.Vector3(),
  97. moveDirection = new THREE.Vector3(),
  98. angle;
  99. return function rotateCamera() {
  100. moveDirection.set( _moveCurr.x - _movePrev.x, _moveCurr.y - _movePrev.y, 0 );
  101. angle = moveDirection.length();
  102. if ( angle ) {
  103. _eye.copy( _this.object.position ).sub( _this.target );
  104. eyeDirection.copy( _eye ).normalize();
  105. objectUpDirection.copy( _this.object.up ).normalize();
  106. objectSidewaysDirection.crossVectors( objectUpDirection, eyeDirection ).normalize();
  107. objectUpDirection.setLength( _moveCurr.y - _movePrev.y );
  108. objectSidewaysDirection.setLength( _moveCurr.x - _movePrev.x );
  109. moveDirection.copy( objectUpDirection.add( objectSidewaysDirection ) );
  110. axis.crossVectors( moveDirection, _eye ).normalize();
  111. angle *= _this.rotateSpeed;
  112. quaternion.setFromAxisAngle( axis, angle );
  113. _eye.applyQuaternion( quaternion );
  114. _this.object.up.applyQuaternion( quaternion );
  115. _lastAxis.copy( axis );
  116. _lastAngle = angle;
  117. } else if ( ! _this.staticMoving && _lastAngle ) {
  118. _lastAngle *= Math.sqrt( 1.0 - _this.dynamicDampingFactor );
  119. _eye.copy( _this.object.position ).sub( _this.target );
  120. quaternion.setFromAxisAngle( _lastAxis, _lastAngle );
  121. _eye.applyQuaternion( quaternion );
  122. _this.object.up.applyQuaternion( quaternion );
  123. }
  124. _movePrev.copy( _moveCurr );
  125. };
  126. }() );
  127. this.zoomCamera = function () {
  128. var factor;
  129. if ( _state === STATE.TOUCH_ZOOM_PAN ) {
  130. factor = _touchZoomDistanceStart / _touchZoomDistanceEnd;
  131. _touchZoomDistanceStart = _touchZoomDistanceEnd;
  132. if ( _this.object.isPerspectiveCamera ) {
  133. _eye.multiplyScalar( factor );
  134. } else if ( _this.object.isOrthographicCamera ) {
  135. _this.object.zoom *= factor;
  136. _this.object.updateProjectionMatrix();
  137. } else {
  138. console.warn( 'THREE.TrackballControls: Unsupported camera type' );
  139. }
  140. } else {
  141. factor = 1.0 + ( _zoomEnd.y - _zoomStart.y ) * _this.zoomSpeed;
  142. if ( factor !== 1.0 && factor > 0.0 ) {
  143. if ( _this.object.isPerspectiveCamera ) {
  144. _eye.multiplyScalar( factor );
  145. } else if ( _this.object.isOrthographicCamera ) {
  146. _this.object.zoom /= factor;
  147. _this.object.updateProjectionMatrix();
  148. } else {
  149. console.warn( 'THREE.TrackballControls: Unsupported camera type' );
  150. }
  151. }
  152. if ( _this.staticMoving ) {
  153. _zoomStart.copy( _zoomEnd );
  154. } else {
  155. _zoomStart.y += ( _zoomEnd.y - _zoomStart.y ) * this.dynamicDampingFactor;
  156. }
  157. }
  158. };
  159. this.panCamera = ( function () {
  160. var mouseChange = new THREE.Vector2(),
  161. objectUp = new THREE.Vector3(),
  162. pan = new THREE.Vector3();
  163. return function panCamera() {
  164. mouseChange.copy( _panEnd ).sub( _panStart );
  165. if ( mouseChange.lengthSq() ) {
  166. if ( _this.object.isOrthographicCamera ) {
  167. var scale_x = ( _this.object.right - _this.object.left ) / _this.object.zoom / _this.domElement.clientWidth;
  168. var scale_y = ( _this.object.top - _this.object.bottom ) / _this.object.zoom / _this.domElement.clientWidth;
  169. mouseChange.x *= scale_x;
  170. mouseChange.y *= scale_y;
  171. }
  172. mouseChange.multiplyScalar( _eye.length() * _this.panSpeed );
  173. pan.copy( _eye ).cross( _this.object.up ).setLength( mouseChange.x );
  174. pan.add( objectUp.copy( _this.object.up ).setLength( mouseChange.y ) );
  175. _this.object.position.add( pan );
  176. _this.target.add( pan );
  177. if ( _this.staticMoving ) {
  178. _panStart.copy( _panEnd );
  179. } else {
  180. _panStart.add( mouseChange.subVectors( _panEnd, _panStart ).multiplyScalar( _this.dynamicDampingFactor ) );
  181. }
  182. }
  183. };
  184. }() );
  185. this.checkDistances = function () {
  186. if ( ! _this.noZoom || ! _this.noPan ) {
  187. if ( _eye.lengthSq() > _this.maxDistance * _this.maxDistance ) {
  188. _this.object.position.addVectors( _this.target, _eye.setLength( _this.maxDistance ) );
  189. _zoomStart.copy( _zoomEnd );
  190. }
  191. if ( _eye.lengthSq() < _this.minDistance * _this.minDistance ) {
  192. _this.object.position.addVectors( _this.target, _eye.setLength( _this.minDistance ) );
  193. _zoomStart.copy( _zoomEnd );
  194. }
  195. }
  196. };
  197. this.update = function () {
  198. _eye.subVectors( _this.object.position, _this.target );
  199. if ( ! _this.noRotate ) {
  200. _this.rotateCamera();
  201. }
  202. if ( ! _this.noZoom ) {
  203. _this.zoomCamera();
  204. }
  205. if ( ! _this.noPan ) {
  206. _this.panCamera();
  207. }
  208. _this.object.position.addVectors( _this.target, _eye );
  209. if ( _this.object.isPerspectiveCamera ) {
  210. _this.checkDistances();
  211. _this.object.lookAt( _this.target );
  212. if ( lastPosition.distanceToSquared( _this.object.position ) > EPS ) {
  213. _this.dispatchEvent( changeEvent );
  214. lastPosition.copy( _this.object.position );
  215. }
  216. } else if ( _this.object.isOrthographicCamera ) {
  217. _this.object.lookAt( _this.target );
  218. if ( lastPosition.distanceToSquared( _this.object.position ) > EPS || lastZoom !== _this.object.zoom ) {
  219. _this.dispatchEvent( changeEvent );
  220. lastPosition.copy( _this.object.position );
  221. lastZoom = _this.object.zoom;
  222. }
  223. } else {
  224. console.warn( 'THREE.TrackballControls: Unsupported camera type' );
  225. }
  226. };
  227. this.reset = function () {
  228. _state = STATE.NONE;
  229. _keyState = STATE.NONE;
  230. _this.target.copy( _this.target0 );
  231. _this.object.position.copy( _this.position0 );
  232. _this.object.up.copy( _this.up0 );
  233. _this.object.zoom = _this.zoom0;
  234. _this.object.updateProjectionMatrix();
  235. _eye.subVectors( _this.object.position, _this.target );
  236. _this.object.lookAt( _this.target );
  237. _this.dispatchEvent( changeEvent );
  238. lastPosition.copy( _this.object.position );
  239. lastZoom = _this.object.zoom;
  240. };
  241. // listeners
  242. function keydown( event ) {
  243. if ( _this.enabled === false ) return;
  244. window.removeEventListener( 'keydown', keydown );
  245. if ( _keyState !== STATE.NONE ) {
  246. return;
  247. } else if ( event.keyCode === _this.keys[ STATE.ROTATE ] && ! _this.noRotate ) {
  248. _keyState = STATE.ROTATE;
  249. } else if ( event.keyCode === _this.keys[ STATE.ZOOM ] && ! _this.noZoom ) {
  250. _keyState = STATE.ZOOM;
  251. } else if ( event.keyCode === _this.keys[ STATE.PAN ] && ! _this.noPan ) {
  252. _keyState = STATE.PAN;
  253. }
  254. }
  255. function keyup() {
  256. if ( _this.enabled === false ) return;
  257. _keyState = STATE.NONE;
  258. window.addEventListener( 'keydown', keydown, false );
  259. }
  260. function mousedown( event ) {
  261. if ( _this.enabled === false ) return;
  262. event.preventDefault();
  263. event.stopPropagation();
  264. if ( _state === STATE.NONE ) {
  265. switch ( event.button ) {
  266. case _this.mouseButtons.LEFT:
  267. _state = STATE.ROTATE;
  268. break;
  269. case _this.mouseButtons.MIDDLE:
  270. _state = STATE.ZOOM;
  271. break;
  272. case _this.mouseButtons.RIGHT:
  273. _state = STATE.PAN;
  274. break;
  275. default:
  276. _state = STATE.NONE;
  277. }
  278. }
  279. var state = ( _keyState !== STATE.NONE ) ? _keyState : _state;
  280. if ( state === STATE.ROTATE && ! _this.noRotate ) {
  281. _moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
  282. _movePrev.copy( _moveCurr );
  283. } else if ( state === STATE.ZOOM && ! _this.noZoom ) {
  284. _zoomStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  285. _zoomEnd.copy( _zoomStart );
  286. } else if ( state === STATE.PAN && ! _this.noPan ) {
  287. _panStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  288. _panEnd.copy( _panStart );
  289. }
  290. document.addEventListener( 'mousemove', mousemove, false );
  291. document.addEventListener( 'mouseup', mouseup, false );
  292. _this.dispatchEvent( startEvent );
  293. }
  294. function mousemove( event ) {
  295. if ( _this.enabled === false ) return;
  296. event.preventDefault();
  297. event.stopPropagation();
  298. var state = ( _keyState !== STATE.NONE ) ? _keyState : _state;
  299. if ( state === STATE.ROTATE && ! _this.noRotate ) {
  300. _movePrev.copy( _moveCurr );
  301. _moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
  302. } else if ( state === STATE.ZOOM && ! _this.noZoom ) {
  303. _zoomEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  304. } else if ( state === STATE.PAN && ! _this.noPan ) {
  305. _panEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  306. }
  307. }
  308. function mouseup( event ) {
  309. if ( _this.enabled === false ) return;
  310. event.preventDefault();
  311. event.stopPropagation();
  312. _state = STATE.NONE;
  313. document.removeEventListener( 'mousemove', mousemove );
  314. document.removeEventListener( 'mouseup', mouseup );
  315. _this.dispatchEvent( endEvent );
  316. }
  317. function mousewheel( event ) {
  318. if ( _this.enabled === false ) return;
  319. if ( _this.noZoom === true ) return;
  320. event.preventDefault();
  321. event.stopPropagation();
  322. switch ( event.deltaMode ) {
  323. case 2:
  324. // Zoom in pages
  325. _zoomStart.y -= event.deltaY * 0.025;
  326. break;
  327. case 1:
  328. // Zoom in lines
  329. _zoomStart.y -= event.deltaY * 0.01;
  330. break;
  331. default:
  332. // undefined, 0, assume pixels
  333. _zoomStart.y -= event.deltaY * 0.00025;
  334. break;
  335. }
  336. _this.dispatchEvent( startEvent );
  337. _this.dispatchEvent( endEvent );
  338. }
  339. function touchstart( event ) {
  340. if ( _this.enabled === false ) return;
  341. event.preventDefault();
  342. switch ( event.touches.length ) {
  343. case 1:
  344. _state = STATE.TOUCH_ROTATE;
  345. _moveCurr.copy( getMouseOnCircle( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ) );
  346. _movePrev.copy( _moveCurr );
  347. break;
  348. default: // 2 or more
  349. _state = STATE.TOUCH_ZOOM_PAN;
  350. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  351. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  352. _touchZoomDistanceEnd = _touchZoomDistanceStart = Math.sqrt( dx * dx + dy * dy );
  353. var x = ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX ) / 2;
  354. var y = ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY ) / 2;
  355. _panStart.copy( getMouseOnScreen( x, y ) );
  356. _panEnd.copy( _panStart );
  357. break;
  358. }
  359. _this.dispatchEvent( startEvent );
  360. }
  361. function touchmove( event ) {
  362. if ( _this.enabled === false ) return;
  363. event.preventDefault();
  364. event.stopPropagation();
  365. switch ( event.touches.length ) {
  366. case 1:
  367. _movePrev.copy( _moveCurr );
  368. _moveCurr.copy( getMouseOnCircle( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ) );
  369. break;
  370. default: // 2 or more
  371. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  372. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  373. _touchZoomDistanceEnd = Math.sqrt( dx * dx + dy * dy );
  374. var x = ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX ) / 2;
  375. var y = ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY ) / 2;
  376. _panEnd.copy( getMouseOnScreen( x, y ) );
  377. break;
  378. }
  379. }
  380. function touchend( event ) {
  381. if ( _this.enabled === false ) return;
  382. switch ( event.touches.length ) {
  383. case 0:
  384. _state = STATE.NONE;
  385. break;
  386. case 1:
  387. _state = STATE.TOUCH_ROTATE;
  388. _moveCurr.copy( getMouseOnCircle( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ) );
  389. _movePrev.copy( _moveCurr );
  390. break;
  391. }
  392. _this.dispatchEvent( endEvent );
  393. }
  394. function contextmenu( event ) {
  395. if ( _this.enabled === false ) return;
  396. event.preventDefault();
  397. }
  398. this.dispose = function () {
  399. this.domElement.removeEventListener( 'contextmenu', contextmenu, false );
  400. this.domElement.removeEventListener( 'mousedown', mousedown, false );
  401. this.domElement.removeEventListener( 'wheel', mousewheel, false );
  402. this.domElement.removeEventListener( 'touchstart', touchstart, false );
  403. this.domElement.removeEventListener( 'touchend', touchend, false );
  404. this.domElement.removeEventListener( 'touchmove', touchmove, false );
  405. document.removeEventListener( 'mousemove', mousemove, false );
  406. document.removeEventListener( 'mouseup', mouseup, false );
  407. window.removeEventListener( 'keydown', keydown, false );
  408. window.removeEventListener( 'keyup', keyup, false );
  409. };
  410. this.domElement.addEventListener( 'contextmenu', contextmenu, false );
  411. this.domElement.addEventListener( 'mousedown', mousedown, false );
  412. this.domElement.addEventListener( 'wheel', mousewheel, false );
  413. this.domElement.addEventListener( 'touchstart', touchstart, false );
  414. this.domElement.addEventListener( 'touchend', touchend, false );
  415. this.domElement.addEventListener( 'touchmove', touchmove, false );
  416. window.addEventListener( 'keydown', keydown, false );
  417. window.addEventListener( 'keyup', keyup, false );
  418. this.handleResize();
  419. // force an update at start
  420. this.update();
  421. };
  422. THREE.TrackballControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  423. THREE.TrackballControls.prototype.constructor = THREE.TrackballControls;