TrackballControls.js 12 KB

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