TrackballControls.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. import {
  2. EventDispatcher,
  3. MathUtils,
  4. MOUSE,
  5. Quaternion,
  6. Vector2,
  7. Vector3
  8. } from 'three';
  9. const _changeEvent = { type: 'change' };
  10. const _startEvent = { type: 'start' };
  11. const _endEvent = { type: 'end' };
  12. class TrackballControls extends EventDispatcher {
  13. constructor( object, domElement ) {
  14. super();
  15. const scope = this;
  16. const STATE = { NONE: - 1, ROTATE: 0, ZOOM: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_ZOOM_PAN: 4 };
  17. this.object = object;
  18. this.domElement = domElement;
  19. this.domElement.style.touchAction = 'none'; // disable touch scroll
  20. // API
  21. this.enabled = true;
  22. this.screen = { left: 0, top: 0, width: 0, height: 0 };
  23. this.rotateSpeed = 1.0;
  24. this.zoomSpeed = 1.2;
  25. this.panSpeed = 0.3;
  26. this.noRotate = false;
  27. this.noZoom = false;
  28. this.noPan = false;
  29. this.staticMoving = false;
  30. this.dynamicDampingFactor = 0.2;
  31. this.minDistance = 0;
  32. this.maxDistance = Infinity;
  33. this.minZoom = 0;
  34. this.maxZoom = Infinity;
  35. this.keys = [ 'KeyA' /*A*/, 'KeyS' /*S*/, 'KeyD' /*D*/ ];
  36. this.mouseButtons = { LEFT: MOUSE.ROTATE, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.PAN };
  37. // internals
  38. this.target = new Vector3();
  39. const EPS = 0.000001;
  40. const lastPosition = new Vector3();
  41. let lastZoom = 1;
  42. let _state = STATE.NONE,
  43. _keyState = STATE.NONE,
  44. _touchZoomDistanceStart = 0,
  45. _touchZoomDistanceEnd = 0,
  46. _lastAngle = 0;
  47. const _eye = new Vector3(),
  48. _movePrev = new Vector2(),
  49. _moveCurr = new Vector2(),
  50. _lastAxis = new Vector3(),
  51. _zoomStart = new Vector2(),
  52. _zoomEnd = new Vector2(),
  53. _panStart = new Vector2(),
  54. _panEnd = new Vector2(),
  55. _pointers = [],
  56. _pointerPositions = {};
  57. // for reset
  58. this.target0 = this.target.clone();
  59. this.position0 = this.object.position.clone();
  60. this.up0 = this.object.up.clone();
  61. this.zoom0 = this.object.zoom;
  62. // methods
  63. this.handleResize = function () {
  64. const box = scope.domElement.getBoundingClientRect();
  65. // adjustments come from similar code in the jquery offset() function
  66. const d = scope.domElement.ownerDocument.documentElement;
  67. scope.screen.left = box.left + window.pageXOffset - d.clientLeft;
  68. scope.screen.top = box.top + window.pageYOffset - d.clientTop;
  69. scope.screen.width = box.width;
  70. scope.screen.height = box.height;
  71. };
  72. const getMouseOnScreen = ( function () {
  73. const vector = new Vector2();
  74. return function getMouseOnScreen( pageX, pageY ) {
  75. vector.set(
  76. ( pageX - scope.screen.left ) / scope.screen.width,
  77. ( pageY - scope.screen.top ) / scope.screen.height
  78. );
  79. return vector;
  80. };
  81. }() );
  82. const getMouseOnCircle = ( function () {
  83. const vector = new Vector2();
  84. return function getMouseOnCircle( pageX, pageY ) {
  85. vector.set(
  86. ( ( pageX - scope.screen.width * 0.5 - scope.screen.left ) / ( scope.screen.width * 0.5 ) ),
  87. ( ( scope.screen.height + 2 * ( scope.screen.top - pageY ) ) / scope.screen.width ) // screen.width intentional
  88. );
  89. return vector;
  90. };
  91. }() );
  92. this.rotateCamera = ( function () {
  93. const axis = new Vector3(),
  94. quaternion = new Quaternion(),
  95. eyeDirection = new Vector3(),
  96. objectUpDirection = new Vector3(),
  97. objectSidewaysDirection = new Vector3(),
  98. moveDirection = new Vector3();
  99. return function rotateCamera() {
  100. moveDirection.set( _moveCurr.x - _movePrev.x, _moveCurr.y - _movePrev.y, 0 );
  101. let angle = moveDirection.length();
  102. if ( angle ) {
  103. _eye.copy( scope.object.position ).sub( scope.target );
  104. eyeDirection.copy( _eye ).normalize();
  105. objectUpDirection.copy( scope.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 *= scope.rotateSpeed;
  112. quaternion.setFromAxisAngle( axis, angle );
  113. _eye.applyQuaternion( quaternion );
  114. scope.object.up.applyQuaternion( quaternion );
  115. _lastAxis.copy( axis );
  116. _lastAngle = angle;
  117. } else if ( ! scope.staticMoving && _lastAngle ) {
  118. _lastAngle *= Math.sqrt( 1.0 - scope.dynamicDampingFactor );
  119. _eye.copy( scope.object.position ).sub( scope.target );
  120. quaternion.setFromAxisAngle( _lastAxis, _lastAngle );
  121. _eye.applyQuaternion( quaternion );
  122. scope.object.up.applyQuaternion( quaternion );
  123. }
  124. _movePrev.copy( _moveCurr );
  125. };
  126. }() );
  127. this.zoomCamera = function () {
  128. let factor;
  129. if ( _state === STATE.TOUCH_ZOOM_PAN ) {
  130. factor = _touchZoomDistanceStart / _touchZoomDistanceEnd;
  131. _touchZoomDistanceStart = _touchZoomDistanceEnd;
  132. if ( scope.object.isPerspectiveCamera ) {
  133. _eye.multiplyScalar( factor );
  134. } else if ( scope.object.isOrthographicCamera ) {
  135. scope.object.zoom = MathUtils.clamp( scope.object.zoom / factor, scope.minZoom, scope.maxZoom );
  136. if ( lastZoom !== scope.object.zoom ) {
  137. scope.object.updateProjectionMatrix();
  138. }
  139. } else {
  140. console.warn( 'THREE.TrackballControls: Unsupported camera type' );
  141. }
  142. } else {
  143. factor = 1.0 + ( _zoomEnd.y - _zoomStart.y ) * scope.zoomSpeed;
  144. if ( factor !== 1.0 && factor > 0.0 ) {
  145. if ( scope.object.isPerspectiveCamera ) {
  146. _eye.multiplyScalar( factor );
  147. } else if ( scope.object.isOrthographicCamera ) {
  148. scope.object.zoom = MathUtils.clamp( scope.object.zoom / factor, scope.minZoom, scope.maxZoom );
  149. if ( lastZoom !== scope.object.zoom ) {
  150. scope.object.updateProjectionMatrix();
  151. }
  152. } else {
  153. console.warn( 'THREE.TrackballControls: Unsupported camera type' );
  154. }
  155. }
  156. if ( scope.staticMoving ) {
  157. _zoomStart.copy( _zoomEnd );
  158. } else {
  159. _zoomStart.y += ( _zoomEnd.y - _zoomStart.y ) * this.dynamicDampingFactor;
  160. }
  161. }
  162. };
  163. this.panCamera = ( function () {
  164. const mouseChange = new Vector2(),
  165. objectUp = new Vector3(),
  166. pan = new Vector3();
  167. return function panCamera() {
  168. mouseChange.copy( _panEnd ).sub( _panStart );
  169. if ( mouseChange.lengthSq() ) {
  170. if ( scope.object.isOrthographicCamera ) {
  171. const scale_x = ( scope.object.right - scope.object.left ) / scope.object.zoom / scope.domElement.clientWidth;
  172. const scale_y = ( scope.object.top - scope.object.bottom ) / scope.object.zoom / scope.domElement.clientWidth;
  173. mouseChange.x *= scale_x;
  174. mouseChange.y *= scale_y;
  175. }
  176. mouseChange.multiplyScalar( _eye.length() * scope.panSpeed );
  177. pan.copy( _eye ).cross( scope.object.up ).setLength( mouseChange.x );
  178. pan.add( objectUp.copy( scope.object.up ).setLength( mouseChange.y ) );
  179. scope.object.position.add( pan );
  180. scope.target.add( pan );
  181. if ( scope.staticMoving ) {
  182. _panStart.copy( _panEnd );
  183. } else {
  184. _panStart.add( mouseChange.subVectors( _panEnd, _panStart ).multiplyScalar( scope.dynamicDampingFactor ) );
  185. }
  186. }
  187. };
  188. }() );
  189. this.checkDistances = function () {
  190. if ( ! scope.noZoom || ! scope.noPan ) {
  191. if ( _eye.lengthSq() > scope.maxDistance * scope.maxDistance ) {
  192. scope.object.position.addVectors( scope.target, _eye.setLength( scope.maxDistance ) );
  193. _zoomStart.copy( _zoomEnd );
  194. }
  195. if ( _eye.lengthSq() < scope.minDistance * scope.minDistance ) {
  196. scope.object.position.addVectors( scope.target, _eye.setLength( scope.minDistance ) );
  197. _zoomStart.copy( _zoomEnd );
  198. }
  199. }
  200. };
  201. this.update = function () {
  202. _eye.subVectors( scope.object.position, scope.target );
  203. if ( ! scope.noRotate ) {
  204. scope.rotateCamera();
  205. }
  206. if ( ! scope.noZoom ) {
  207. scope.zoomCamera();
  208. }
  209. if ( ! scope.noPan ) {
  210. scope.panCamera();
  211. }
  212. scope.object.position.addVectors( scope.target, _eye );
  213. if ( scope.object.isPerspectiveCamera ) {
  214. scope.checkDistances();
  215. scope.object.lookAt( scope.target );
  216. if ( lastPosition.distanceToSquared( scope.object.position ) > EPS ) {
  217. scope.dispatchEvent( _changeEvent );
  218. lastPosition.copy( scope.object.position );
  219. }
  220. } else if ( scope.object.isOrthographicCamera ) {
  221. scope.object.lookAt( scope.target );
  222. if ( lastPosition.distanceToSquared( scope.object.position ) > EPS || lastZoom !== scope.object.zoom ) {
  223. scope.dispatchEvent( _changeEvent );
  224. lastPosition.copy( scope.object.position );
  225. lastZoom = scope.object.zoom;
  226. }
  227. } else {
  228. console.warn( 'THREE.TrackballControls: Unsupported camera type' );
  229. }
  230. };
  231. this.reset = function () {
  232. _state = STATE.NONE;
  233. _keyState = STATE.NONE;
  234. scope.target.copy( scope.target0 );
  235. scope.object.position.copy( scope.position0 );
  236. scope.object.up.copy( scope.up0 );
  237. scope.object.zoom = scope.zoom0;
  238. scope.object.updateProjectionMatrix();
  239. _eye.subVectors( scope.object.position, scope.target );
  240. scope.object.lookAt( scope.target );
  241. scope.dispatchEvent( _changeEvent );
  242. lastPosition.copy( scope.object.position );
  243. lastZoom = scope.object.zoom;
  244. };
  245. // listeners
  246. function onPointerDown( event ) {
  247. if ( scope.enabled === false ) return;
  248. if ( _pointers.length === 0 ) {
  249. scope.domElement.setPointerCapture( event.pointerId );
  250. scope.domElement.addEventListener( 'pointermove', onPointerMove );
  251. scope.domElement.addEventListener( 'pointerup', onPointerUp );
  252. }
  253. //
  254. addPointer( event );
  255. if ( event.pointerType === 'touch' ) {
  256. onTouchStart( event );
  257. } else {
  258. onMouseDown( event );
  259. }
  260. }
  261. function onPointerMove( event ) {
  262. if ( scope.enabled === false ) return;
  263. if ( event.pointerType === 'touch' ) {
  264. onTouchMove( event );
  265. } else {
  266. onMouseMove( event );
  267. }
  268. }
  269. function onPointerUp( event ) {
  270. if ( scope.enabled === false ) return;
  271. if ( event.pointerType === 'touch' ) {
  272. onTouchEnd( event );
  273. } else {
  274. onMouseUp();
  275. }
  276. //
  277. removePointer( event );
  278. if ( _pointers.length === 0 ) {
  279. scope.domElement.releasePointerCapture( event.pointerId );
  280. scope.domElement.removeEventListener( 'pointermove', onPointerMove );
  281. scope.domElement.removeEventListener( 'pointerup', onPointerUp );
  282. }
  283. }
  284. function onPointerCancel( event ) {
  285. removePointer( event );
  286. }
  287. function keydown( event ) {
  288. if ( scope.enabled === false ) return;
  289. window.removeEventListener( 'keydown', keydown );
  290. if ( _keyState !== STATE.NONE ) {
  291. return;
  292. } else if ( event.code === scope.keys[ STATE.ROTATE ] && ! scope.noRotate ) {
  293. _keyState = STATE.ROTATE;
  294. } else if ( event.code === scope.keys[ STATE.ZOOM ] && ! scope.noZoom ) {
  295. _keyState = STATE.ZOOM;
  296. } else if ( event.code === scope.keys[ STATE.PAN ] && ! scope.noPan ) {
  297. _keyState = STATE.PAN;
  298. }
  299. }
  300. function keyup() {
  301. if ( scope.enabled === false ) return;
  302. _keyState = STATE.NONE;
  303. window.addEventListener( 'keydown', keydown );
  304. }
  305. function onMouseDown( event ) {
  306. if ( _state === STATE.NONE ) {
  307. switch ( event.button ) {
  308. case scope.mouseButtons.LEFT:
  309. _state = STATE.ROTATE;
  310. break;
  311. case scope.mouseButtons.MIDDLE:
  312. _state = STATE.ZOOM;
  313. break;
  314. case scope.mouseButtons.RIGHT:
  315. _state = STATE.PAN;
  316. break;
  317. }
  318. }
  319. const state = ( _keyState !== STATE.NONE ) ? _keyState : _state;
  320. if ( state === STATE.ROTATE && ! scope.noRotate ) {
  321. _moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
  322. _movePrev.copy( _moveCurr );
  323. } else if ( state === STATE.ZOOM && ! scope.noZoom ) {
  324. _zoomStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  325. _zoomEnd.copy( _zoomStart );
  326. } else if ( state === STATE.PAN && ! scope.noPan ) {
  327. _panStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  328. _panEnd.copy( _panStart );
  329. }
  330. scope.dispatchEvent( _startEvent );
  331. }
  332. function onMouseMove( event ) {
  333. const state = ( _keyState !== STATE.NONE ) ? _keyState : _state;
  334. if ( state === STATE.ROTATE && ! scope.noRotate ) {
  335. _movePrev.copy( _moveCurr );
  336. _moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
  337. } else if ( state === STATE.ZOOM && ! scope.noZoom ) {
  338. _zoomEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  339. } else if ( state === STATE.PAN && ! scope.noPan ) {
  340. _panEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  341. }
  342. }
  343. function onMouseUp() {
  344. _state = STATE.NONE;
  345. scope.dispatchEvent( _endEvent );
  346. }
  347. function onMouseWheel( event ) {
  348. if ( scope.enabled === false ) return;
  349. if ( scope.noZoom === true ) return;
  350. event.preventDefault();
  351. switch ( event.deltaMode ) {
  352. case 2:
  353. // Zoom in pages
  354. _zoomStart.y -= event.deltaY * 0.025;
  355. break;
  356. case 1:
  357. // Zoom in lines
  358. _zoomStart.y -= event.deltaY * 0.01;
  359. break;
  360. default:
  361. // undefined, 0, assume pixels
  362. _zoomStart.y -= event.deltaY * 0.00025;
  363. break;
  364. }
  365. scope.dispatchEvent( _startEvent );
  366. scope.dispatchEvent( _endEvent );
  367. }
  368. function onTouchStart( event ) {
  369. trackPointer( event );
  370. switch ( _pointers.length ) {
  371. case 1:
  372. _state = STATE.TOUCH_ROTATE;
  373. _moveCurr.copy( getMouseOnCircle( _pointers[ 0 ].pageX, _pointers[ 0 ].pageY ) );
  374. _movePrev.copy( _moveCurr );
  375. break;
  376. default: // 2 or more
  377. _state = STATE.TOUCH_ZOOM_PAN;
  378. const dx = _pointers[ 0 ].pageX - _pointers[ 1 ].pageX;
  379. const dy = _pointers[ 0 ].pageY - _pointers[ 1 ].pageY;
  380. _touchZoomDistanceEnd = _touchZoomDistanceStart = Math.sqrt( dx * dx + dy * dy );
  381. const x = ( _pointers[ 0 ].pageX + _pointers[ 1 ].pageX ) / 2;
  382. const y = ( _pointers[ 0 ].pageY + _pointers[ 1 ].pageY ) / 2;
  383. _panStart.copy( getMouseOnScreen( x, y ) );
  384. _panEnd.copy( _panStart );
  385. break;
  386. }
  387. scope.dispatchEvent( _startEvent );
  388. }
  389. function onTouchMove( event ) {
  390. trackPointer( event );
  391. switch ( _pointers.length ) {
  392. case 1:
  393. _movePrev.copy( _moveCurr );
  394. _moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
  395. break;
  396. default: // 2 or more
  397. const position = getSecondPointerPosition( event );
  398. const dx = event.pageX - position.x;
  399. const dy = event.pageY - position.y;
  400. _touchZoomDistanceEnd = Math.sqrt( dx * dx + dy * dy );
  401. const x = ( event.pageX + position.x ) / 2;
  402. const y = ( event.pageY + position.y ) / 2;
  403. _panEnd.copy( getMouseOnScreen( x, y ) );
  404. break;
  405. }
  406. }
  407. function onTouchEnd( event ) {
  408. switch ( _pointers.length ) {
  409. case 0:
  410. _state = STATE.NONE;
  411. break;
  412. case 1:
  413. _state = STATE.TOUCH_ROTATE;
  414. _moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
  415. _movePrev.copy( _moveCurr );
  416. break;
  417. case 2:
  418. _state = STATE.TOUCH_ZOOM_PAN;
  419. for ( let i = 0; i < _pointers.length; i ++ ) {
  420. if ( _pointers[ i ].pointerId !== event.pointerId ) {
  421. const position = _pointerPositions[ _pointers[ i ].pointerId ];
  422. _moveCurr.copy( getMouseOnCircle( position.x, position.y ) );
  423. _movePrev.copy( _moveCurr );
  424. break;
  425. }
  426. }
  427. break;
  428. }
  429. scope.dispatchEvent( _endEvent );
  430. }
  431. function contextmenu( event ) {
  432. if ( scope.enabled === false ) return;
  433. event.preventDefault();
  434. }
  435. function addPointer( event ) {
  436. _pointers.push( event );
  437. }
  438. function removePointer( event ) {
  439. delete _pointerPositions[ event.pointerId ];
  440. for ( let i = 0; i < _pointers.length; i ++ ) {
  441. if ( _pointers[ i ].pointerId == event.pointerId ) {
  442. _pointers.splice( i, 1 );
  443. return;
  444. }
  445. }
  446. }
  447. function trackPointer( event ) {
  448. let position = _pointerPositions[ event.pointerId ];
  449. if ( position === undefined ) {
  450. position = new Vector2();
  451. _pointerPositions[ event.pointerId ] = position;
  452. }
  453. position.set( event.pageX, event.pageY );
  454. }
  455. function getSecondPointerPosition( event ) {
  456. const pointer = ( event.pointerId === _pointers[ 0 ].pointerId ) ? _pointers[ 1 ] : _pointers[ 0 ];
  457. return _pointerPositions[ pointer.pointerId ];
  458. }
  459. this.dispose = function () {
  460. scope.domElement.removeEventListener( 'contextmenu', contextmenu );
  461. scope.domElement.removeEventListener( 'pointerdown', onPointerDown );
  462. scope.domElement.removeEventListener( 'pointercancel', onPointerCancel );
  463. scope.domElement.removeEventListener( 'wheel', onMouseWheel );
  464. scope.domElement.removeEventListener( 'pointermove', onPointerMove );
  465. scope.domElement.removeEventListener( 'pointerup', onPointerUp );
  466. window.removeEventListener( 'keydown', keydown );
  467. window.removeEventListener( 'keyup', keyup );
  468. };
  469. this.domElement.addEventListener( 'contextmenu', contextmenu );
  470. this.domElement.addEventListener( 'pointerdown', onPointerDown );
  471. this.domElement.addEventListener( 'pointercancel', onPointerCancel );
  472. this.domElement.addEventListener( 'wheel', onMouseWheel, { passive: false } );
  473. window.addEventListener( 'keydown', keydown );
  474. window.addEventListener( 'keyup', keyup );
  475. this.handleResize();
  476. // force an update at start
  477. this.update();
  478. }
  479. }
  480. export { TrackballControls };