TrackballControls.js 14 KB

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