TrackballControls.js 16 KB

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