TrackballControls.js 11 KB

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