TrackballControls.js 17 KB

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