TrackballControls.js 14 KB

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