TrackballControls.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /**
  2. * @author Eberhard Graether / http://egraether.com/
  3. * @author Mark Lundin / http://mark-lundin.com
  4. */
  5. THREE.TrackballControls = function ( object, domElement ) {
  6. var _this = this;
  7. var STATE = { NONE: -1, ROTATE: 0, ZOOM: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_ZOOM: 4, TOUCH_PAN: 5 };
  8. this.object = object;
  9. this.domElement = ( domElement !== undefined ) ? domElement : document;
  10. // API
  11. this.enabled = true;
  12. this.screen = { left: 0, top: 0, width: 0, height: 0 };
  13. this.rotateSpeed = 1.0;
  14. this.zoomSpeed = 1.2;
  15. this.panSpeed = 0.3;
  16. this.noRotate = false;
  17. this.noZoom = false;
  18. this.noPan = false;
  19. this.noRoll = false;
  20. this.staticMoving = false;
  21. this.dynamicDampingFactor = 0.2;
  22. this.minDistance = 0;
  23. this.maxDistance = Infinity;
  24. this.keys = [ 65 /*A*/, 83 /*S*/, 68 /*D*/ ];
  25. // internals
  26. this.target = new THREE.Vector3();
  27. var lastPosition = new THREE.Vector3();
  28. var _state = STATE.NONE,
  29. _prevState = STATE.NONE,
  30. _eye = new THREE.Vector3(),
  31. _rotateStart = new THREE.Vector3(),
  32. _rotateEnd = new THREE.Vector3(),
  33. _zoomStart = new THREE.Vector2(),
  34. _zoomEnd = new THREE.Vector2(),
  35. _touchZoomDistanceStart = 0,
  36. _touchZoomDistanceEnd = 0,
  37. _panStart = new THREE.Vector2(),
  38. _panEnd = new THREE.Vector2();
  39. // for reset
  40. this.target0 = this.target.clone();
  41. this.position0 = this.object.position.clone();
  42. this.up0 = this.object.up.clone();
  43. // events
  44. var changeEvent = { type: 'change' };
  45. var startEvent = { type: 'start'};
  46. var endEvent = { type: 'end'};
  47. // methods
  48. this.handleResize = function () {
  49. if ( this.domElement === document ) {
  50. this.screen.left = 0;
  51. this.screen.top = 0;
  52. this.screen.width = window.innerWidth;
  53. this.screen.height = window.innerHeight;
  54. } else {
  55. this.screen = this.domElement.getBoundingClientRect();
  56. }
  57. };
  58. this.handleEvent = function ( event ) {
  59. if ( typeof this[ event.type ] == 'function' ) {
  60. this[ event.type ]( event );
  61. }
  62. };
  63. this.getMouseOnScreen = function ( clientX, clientY, optionalTarget ) {
  64. return ( optionalTarget || new THREE.Vector2() ).set(
  65. ( clientX - _this.screen.left ) / _this.screen.width,
  66. ( clientY - _this.screen.top ) / _this.screen.height
  67. );
  68. };
  69. this.getMouseProjectionOnBall = (function(){
  70. var objectUp = new THREE.Vector3();
  71. return function ( clientX, clientY, projection ) {
  72. var mouseOnBall = new THREE.Vector3(
  73. ( clientX - _this.screen.width * 0.5 - _this.screen.left ) / (_this.screen.width*.5),
  74. ( _this.screen.height * 0.5 + _this.screen.top - clientY ) / (_this.screen.height*.5),
  75. 0.0
  76. );
  77. var length = mouseOnBall.length();
  78. if ( _this.noRoll ) {
  79. if ( length < Math.SQRT1_2 ) {
  80. mouseOnBall.z = Math.sqrt( 1.0 - length*length );
  81. } else {
  82. mouseOnBall.z = .5 / length;
  83. }
  84. } else if ( length > 1.0 ) {
  85. mouseOnBall.normalize();
  86. } else {
  87. mouseOnBall.z = Math.sqrt( 1.0 - length * length );
  88. }
  89. _eye.copy( _this.object.position ).sub( _this.target );
  90. projection.copy( _this.object.up ).setLength( mouseOnBall.y )
  91. projection.add( objectUp.copy( _this.object.up ).cross( _eye ).setLength( mouseOnBall.x ) );
  92. projection.add( _eye.setLength( mouseOnBall.z ) );
  93. return projection;
  94. }
  95. }());
  96. this.rotateCamera = (function(){
  97. var axis = new THREE.Vector3(),
  98. quaternion = new THREE.Quaternion();
  99. return function () {
  100. var angle = Math.acos( _rotateStart.dot( _rotateEnd ) / _rotateStart.length() / _rotateEnd.length() );
  101. if ( angle ) {
  102. axis.crossVectors( _rotateStart, _rotateEnd ).normalize();
  103. angle *= _this.rotateSpeed;
  104. quaternion.setFromAxisAngle( axis, -angle );
  105. _eye.applyQuaternion( quaternion );
  106. _this.object.up.applyQuaternion( quaternion );
  107. _rotateEnd.applyQuaternion( quaternion );
  108. if ( _this.staticMoving ) {
  109. _rotateStart.copy( _rotateEnd );
  110. } else {
  111. quaternion.setFromAxisAngle( axis, angle * ( _this.dynamicDampingFactor - 1.0 ) );
  112. _rotateStart.applyQuaternion( quaternion );
  113. }
  114. }
  115. }
  116. }());
  117. this.zoomCamera = function () {
  118. if ( _state === STATE.TOUCH_ZOOM ) {
  119. var factor = _touchZoomDistanceStart / _touchZoomDistanceEnd;
  120. _touchZoomDistanceStart = _touchZoomDistanceEnd;
  121. _eye.multiplyScalar( factor );
  122. } else {
  123. var factor = 1.0 + ( _zoomEnd.y - _zoomStart.y ) * _this.zoomSpeed;
  124. if ( factor !== 1.0 && factor > 0.0 ) {
  125. _eye.multiplyScalar( factor );
  126. if ( _this.staticMoving ) {
  127. _zoomStart.copy( _zoomEnd );
  128. } else {
  129. _zoomStart.y += ( _zoomEnd.y - _zoomStart.y ) * this.dynamicDampingFactor;
  130. }
  131. }
  132. }
  133. };
  134. this.panCamera = (function(){
  135. var mouseChange = new THREE.Vector2(),
  136. objectUp = new THREE.Vector3(),
  137. pan = new THREE.Vector3();
  138. return function () {
  139. mouseChange.copy( _panEnd ).sub( _panStart );
  140. if ( mouseChange.lengthSq() ) {
  141. mouseChange.multiplyScalar( _eye.length() * _this.panSpeed );
  142. pan.copy( _eye ).cross( _this.object.up ).setLength( mouseChange.x );
  143. pan.add( objectUp.copy( _this.object.up ).setLength( mouseChange.y ) );
  144. _this.object.position.add( pan );
  145. _this.target.add( pan );
  146. if ( _this.staticMoving ) {
  147. _panStart.copy( _panEnd );
  148. } else {
  149. _panStart.add( mouseChange.subVectors( _panEnd, _panStart ).multiplyScalar( _this.dynamicDampingFactor ) );
  150. }
  151. }
  152. }
  153. }());
  154. this.checkDistances = function () {
  155. if ( !_this.noZoom || !_this.noPan ) {
  156. if ( _eye.lengthSq() > _this.maxDistance * _this.maxDistance ) {
  157. _this.object.position.addVectors( _this.target, _eye.setLength( _this.maxDistance ) );
  158. }
  159. if ( _eye.lengthSq() < _this.minDistance * _this.minDistance ) {
  160. _this.object.position.addVectors( _this.target, _eye.setLength( _this.minDistance ) );
  161. }
  162. }
  163. };
  164. this.update = function () {
  165. _eye.subVectors( _this.object.position, _this.target );
  166. if ( !_this.noRotate ) {
  167. _this.rotateCamera();
  168. }
  169. if ( !_this.noZoom ) {
  170. _this.zoomCamera();
  171. }
  172. if ( !_this.noPan ) {
  173. _this.panCamera();
  174. }
  175. _this.object.position.addVectors( _this.target, _eye );
  176. _this.checkDistances();
  177. _this.object.lookAt( _this.target );
  178. if ( lastPosition.distanceToSquared( _this.object.position ) > 0 ) {
  179. _this.dispatchEvent( changeEvent );
  180. lastPosition.copy( _this.object.position );
  181. }
  182. };
  183. this.reset = function () {
  184. _state = STATE.NONE;
  185. _prevState = STATE.NONE;
  186. _this.target.copy( _this.target0 );
  187. _this.object.position.copy( _this.position0 );
  188. _this.object.up.copy( _this.up0 );
  189. _eye.subVectors( _this.object.position, _this.target );
  190. _this.object.lookAt( _this.target );
  191. _this.dispatchEvent( changeEvent );
  192. lastPosition.copy( _this.object.position );
  193. };
  194. // listeners
  195. function keydown( event ) {
  196. if ( _this.enabled === false ) return;
  197. window.removeEventListener( 'keydown', keydown );
  198. _prevState = _state;
  199. if ( _state !== STATE.NONE ) {
  200. return;
  201. } else if ( event.keyCode === _this.keys[ STATE.ROTATE ] && !_this.noRotate ) {
  202. _state = STATE.ROTATE;
  203. } else if ( event.keyCode === _this.keys[ STATE.ZOOM ] && !_this.noZoom ) {
  204. _state = STATE.ZOOM;
  205. } else if ( event.keyCode === _this.keys[ STATE.PAN ] && !_this.noPan ) {
  206. _state = STATE.PAN;
  207. }
  208. }
  209. function keyup( event ) {
  210. if ( _this.enabled === false ) return;
  211. _state = _prevState;
  212. window.addEventListener( 'keydown', keydown, false );
  213. }
  214. function mousedown( event ) {
  215. if ( _this.enabled === false ) return;
  216. event.preventDefault();
  217. event.stopPropagation();
  218. if ( _state === STATE.NONE ) {
  219. _state = event.button;
  220. }
  221. if ( _state === STATE.ROTATE && !_this.noRotate ) {
  222. _rotateStart = _this.getMouseProjectionOnBall( event.clientX, event.clientY, _rotateStart );
  223. _rotateEnd.copy(_rotateStart)
  224. } else if ( _state === STATE.ZOOM && !_this.noZoom ) {
  225. _zoomStart = _this.getMouseOnScreen( event.clientX, event.clientY, _zoomStart );
  226. _zoomEnd.copy(_zoomStart);
  227. } else if ( _state === STATE.PAN && !_this.noPan ) {
  228. _panStart = _this.getMouseOnScreen( event.clientX, event.clientY, _panStart);
  229. _panEnd.copy(_panStart)
  230. }
  231. document.addEventListener( 'mousemove', mousemove, false );
  232. document.addEventListener( 'mouseup', mouseup, false );
  233. _this.dispatchEvent( startEvent );
  234. }
  235. function mousemove( event ) {
  236. if ( _this.enabled === false ) return;
  237. event.preventDefault();
  238. event.stopPropagation();
  239. if ( _state === STATE.ROTATE && !_this.noRotate ) {
  240. _rotateEnd = _this.getMouseProjectionOnBall( event.clientX, event.clientY, _rotateEnd );
  241. } else if ( _state === STATE.ZOOM && !_this.noZoom ) {
  242. _zoomEnd = _this.getMouseOnScreen( event.clientX, event.clientY, _zoomEnd );
  243. } else if ( _state === STATE.PAN && !_this.noPan ) {
  244. _panEnd = _this.getMouseOnScreen( event.clientX, event.clientY, _panEnd );
  245. }
  246. }
  247. function mouseup( event ) {
  248. if ( _this.enabled === false ) return;
  249. event.preventDefault();
  250. event.stopPropagation();
  251. _state = STATE.NONE;
  252. document.removeEventListener( 'mousemove', mousemove );
  253. document.removeEventListener( 'mouseup', mouseup );
  254. _this.dispatchEvent( endEvent );
  255. }
  256. function mousewheel( event ) {
  257. if ( _this.enabled === false ) return;
  258. event.preventDefault();
  259. event.stopPropagation();
  260. var delta = 0;
  261. if ( event.wheelDelta ) { // WebKit / Opera / Explorer 9
  262. delta = event.wheelDelta / 40;
  263. } else if ( event.detail ) { // Firefox
  264. delta = - event.detail / 3;
  265. }
  266. _zoomStart.y += delta * 0.01;
  267. _this.dispatchEvent( startEvent );
  268. _this.dispatchEvent( endEvent );
  269. }
  270. function touchstart( event ) {
  271. if ( _this.enabled === false ) return;
  272. switch ( event.touches.length ) {
  273. case 1:
  274. _state = STATE.TOUCH_ROTATE;
  275. _rotateEnd.copy( _this.getMouseProjectionOnBall( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY, _rotateStart ));
  276. break;
  277. case 2:
  278. _state = STATE.TOUCH_ZOOM;
  279. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  280. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  281. _touchZoomDistanceEnd = _touchZoomDistanceStart = Math.sqrt( dx * dx + dy * dy );
  282. break;
  283. case 3:
  284. _state = STATE.TOUCH_PAN;
  285. _panEnd.copy( _this.getMouseOnScreen( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY, _panStart ));
  286. break;
  287. default:
  288. _state = STATE.NONE;
  289. }
  290. _this.dispatchEvent( startEvent );
  291. }
  292. function touchmove( event ) {
  293. if ( _this.enabled === false ) return;
  294. event.preventDefault();
  295. event.stopPropagation();
  296. switch ( event.touches.length ) {
  297. case 1:
  298. _rotateEnd = _this.getMouseProjectionOnBall( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY, _rotateEnd );
  299. break;
  300. case 2:
  301. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  302. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  303. _touchZoomDistanceEnd = Math.sqrt( dx * dx + dy * dy )
  304. break;
  305. case 3:
  306. _panEnd = _this.getMouseOnScreen( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY, _panEnd );
  307. break;
  308. default:
  309. _state = STATE.NONE;
  310. }
  311. }
  312. function touchend( event ) {
  313. if ( _this.enabled === false ) return;
  314. switch ( event.touches.length ) {
  315. case 1:
  316. _rotateStart.copy( _this.getMouseProjectionOnBall( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY, _rotateEnd ));
  317. break;
  318. case 2:
  319. _touchZoomDistanceStart = _touchZoomDistanceEnd = 0;
  320. break;
  321. case 3:
  322. _panStart.copy( _this.getMouseOnScreen( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY, _panEnd ));
  323. break;
  324. }
  325. _state = STATE.NONE;
  326. _this.dispatchEvent( endEvent );
  327. }
  328. this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  329. this.domElement.addEventListener( 'mousedown', mousedown, false );
  330. this.domElement.addEventListener( 'mousewheel', mousewheel, false );
  331. this.domElement.addEventListener( 'DOMMouseScroll', mousewheel, false ); // firefox
  332. this.domElement.addEventListener( 'touchstart', touchstart, false );
  333. this.domElement.addEventListener( 'touchend', touchend, false );
  334. this.domElement.addEventListener( 'touchmove', touchmove, false );
  335. window.addEventListener( 'keydown', keydown, false );
  336. window.addEventListener( 'keyup', keyup, false );
  337. this.handleResize();
  338. };
  339. THREE.TrackballControls.prototype = Object.create( THREE.EventDispatcher.prototype );