TrackballControls.js 14 KB

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