TrackballControls.js 17 KB

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