TrackballControls.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /**
  2. * @author Eberhard Graether / http://egraether.com/
  3. */
  4. THREE.TrackballControls = function ( object, domElement ) {
  5. THREE.EventDispatcher.call( this );
  6. var _this = this;
  7. var STATE = { NONE: -1, ROTATE: 0, ZOOM: 1, PAN: 2 };
  8. this.object = object;
  9. this.domElement = ( domElement !== undefined ) ? domElement : document;
  10. // API
  11. this.enabled = true;
  12. this.screen = { width: 0, height: 0, offsetLeft: 0, offsetTop: 0 };
  13. this.radius = ( this.screen.width + this.screen.height ) / 4;
  14. this.rotateSpeed = 1.0;
  15. this.zoomSpeed = 1.2;
  16. this.panSpeed = 0.3;
  17. this.noRotate = false;
  18. this.noZoom = false;
  19. this.noPan = 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. _panStart = new THREE.Vector2(),
  36. _panEnd = new THREE.Vector2();
  37. // events
  38. var changeEvent = { type: 'change' };
  39. // methods
  40. this.handleResize = function () {
  41. this.screen.width = window.innerWidth;
  42. this.screen.height = window.innerHeight;
  43. this.screen.offsetLeft = 0;
  44. this.screen.offsetTop = 0;
  45. this.radius = ( this.screen.width + this.screen.height ) / 4;
  46. };
  47. this.handleEvent = function ( event ) {
  48. if ( typeof this[ event.type ] == 'function' ) {
  49. this[ event.type ]( event );
  50. }
  51. };
  52. this.getMouseOnScreen = function ( clientX, clientY ) {
  53. return new THREE.Vector2(
  54. ( clientX - _this.screen.offsetLeft ) / _this.radius * 0.5,
  55. ( clientY - _this.screen.offsetTop ) / _this.radius * 0.5
  56. );
  57. };
  58. this.getMouseProjectionOnBall = function ( clientX, clientY ) {
  59. var mouseOnBall = new THREE.Vector3(
  60. ( clientX - _this.screen.width * 0.5 - _this.screen.offsetLeft ) / _this.radius,
  61. ( _this.screen.height * 0.5 + _this.screen.offsetTop - clientY ) / _this.radius,
  62. 0.0
  63. );
  64. var length = mouseOnBall.length();
  65. if ( length > 1.0 ) {
  66. mouseOnBall.normalize();
  67. } else {
  68. mouseOnBall.z = Math.sqrt( 1.0 - length * length );
  69. }
  70. _eye.copy( _this.object.position ).subSelf( _this.target );
  71. var projection = _this.object.up.clone().setLength( mouseOnBall.y );
  72. projection.addSelf( _this.object.up.clone().crossSelf( _eye ).setLength( mouseOnBall.x ) );
  73. projection.addSelf( _eye.setLength( mouseOnBall.z ) );
  74. return projection;
  75. };
  76. this.rotateCamera = function () {
  77. var angle = Math.acos( _rotateStart.dot( _rotateEnd ) / _rotateStart.length() / _rotateEnd.length() );
  78. if ( angle ) {
  79. var axis = ( new THREE.Vector3() ).cross( _rotateStart, _rotateEnd ).normalize(),
  80. quaternion = new THREE.Quaternion();
  81. angle *= _this.rotateSpeed;
  82. quaternion.setFromAxisAngle( axis, -angle );
  83. quaternion.multiplyVector3( _eye );
  84. quaternion.multiplyVector3( _this.object.up );
  85. quaternion.multiplyVector3( _rotateEnd );
  86. if ( _this.staticMoving ) {
  87. _rotateStart.copy( _rotateEnd );
  88. } else {
  89. quaternion.setFromAxisAngle( axis, angle * ( _this.dynamicDampingFactor - 1.0 ) );
  90. quaternion.multiplyVector3( _rotateStart );
  91. }
  92. }
  93. };
  94. this.zoomCamera = function () {
  95. var factor = 1.0 + ( _zoomEnd.y - _zoomStart.y ) * _this.zoomSpeed;
  96. if ( factor !== 1.0 && factor > 0.0 ) {
  97. _eye.multiplyScalar( factor );
  98. if ( _this.staticMoving ) {
  99. _zoomStart.copy( _zoomEnd );
  100. } else {
  101. _zoomStart.y += ( _zoomEnd.y - _zoomStart.y ) * this.dynamicDampingFactor;
  102. }
  103. }
  104. };
  105. this.panCamera = function () {
  106. var mouseChange = _panEnd.clone().subSelf( _panStart );
  107. if ( mouseChange.lengthSq() ) {
  108. mouseChange.multiplyScalar( _eye.length() * _this.panSpeed );
  109. var pan = _eye.clone().crossSelf( _this.object.up ).setLength( mouseChange.x );
  110. pan.addSelf( _this.object.up.clone().setLength( mouseChange.y ) );
  111. _this.object.position.addSelf( pan );
  112. _this.target.addSelf( pan );
  113. if ( _this.staticMoving ) {
  114. _panStart = _panEnd;
  115. } else {
  116. _panStart.addSelf( mouseChange.sub( _panEnd, _panStart ).multiplyScalar( _this.dynamicDampingFactor ) );
  117. }
  118. }
  119. };
  120. this.checkDistances = function () {
  121. if ( !_this.noZoom || !_this.noPan ) {
  122. if ( _this.object.position.lengthSq() > _this.maxDistance * _this.maxDistance ) {
  123. _this.object.position.setLength( _this.maxDistance );
  124. }
  125. if ( _eye.lengthSq() < _this.minDistance * _this.minDistance ) {
  126. _this.object.position.add( _this.target, _eye.setLength( _this.minDistance ) );
  127. }
  128. }
  129. };
  130. this.update = function () {
  131. _eye.copy( _this.object.position ).subSelf( _this.target );
  132. if ( !_this.noRotate ) {
  133. _this.rotateCamera();
  134. }
  135. if ( !_this.noZoom ) {
  136. _this.zoomCamera();
  137. }
  138. if ( !_this.noPan ) {
  139. _this.panCamera();
  140. }
  141. _this.object.position.add( _this.target, _eye );
  142. _this.checkDistances();
  143. _this.object.lookAt( _this.target );
  144. if ( lastPosition.distanceToSquared( _this.object.position ) > 0 ) {
  145. _this.dispatchEvent( changeEvent );
  146. lastPosition.copy( _this.object.position );
  147. }
  148. };
  149. // listeners
  150. function keydown( event ) {
  151. if ( ! _this.enabled ) return;
  152. window.removeEventListener( 'keydown', keydown );
  153. _prevState = _state;
  154. if ( _state !== STATE.NONE ) {
  155. return;
  156. } else if ( event.keyCode === _this.keys[ STATE.ROTATE ] && !_this.noRotate ) {
  157. _state = STATE.ROTATE;
  158. } else if ( event.keyCode === _this.keys[ STATE.ZOOM ] && !_this.noZoom ) {
  159. _state = STATE.ZOOM;
  160. } else if ( event.keyCode === _this.keys[ STATE.PAN ] && !_this.noPan ) {
  161. _state = STATE.PAN;
  162. }
  163. }
  164. function keyup( event ) {
  165. if ( ! _this.enabled ) return;
  166. _state = _prevState;
  167. window.addEventListener( 'keydown', keydown, false );
  168. }
  169. function mousedown( event ) {
  170. if ( ! _this.enabled ) return;
  171. event.preventDefault();
  172. event.stopPropagation();
  173. if ( _state === STATE.NONE ) {
  174. _state = event.button;
  175. }
  176. if ( _state === STATE.ROTATE && !_this.noRotate ) {
  177. _rotateStart = _rotateEnd = _this.getMouseProjectionOnBall( event.clientX, event.clientY );
  178. } else if ( _state === STATE.ZOOM && !_this.noZoom ) {
  179. _zoomStart = _zoomEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
  180. } else if ( _state === STATE.PAN && !_this.noPan ) {
  181. _panStart = _panEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
  182. }
  183. document.addEventListener( 'mousemove', mousemove, false );
  184. document.addEventListener( 'mouseup', mouseup, false );
  185. }
  186. function mousemove( event ) {
  187. if ( ! _this.enabled ) return;
  188. if ( _state === STATE.ROTATE && !_this.noRotate ) {
  189. _rotateEnd = _this.getMouseProjectionOnBall( event.clientX, event.clientY );
  190. } else if ( _state === STATE.ZOOM && !_this.noZoom ) {
  191. _zoomEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
  192. } else if ( _state === STATE.PAN && !_this.noPan ) {
  193. _panEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
  194. }
  195. }
  196. function mouseup( event ) {
  197. if ( ! _this.enabled ) return;
  198. event.preventDefault();
  199. event.stopPropagation();
  200. _state = STATE.NONE;
  201. document.removeEventListener( 'mousemove', mousemove );
  202. document.removeEventListener( 'mouseup', mouseup );
  203. }
  204. function mousewheel( event ) {
  205. if ( ! _this.enabled ) return;
  206. event.preventDefault();
  207. event.stopPropagation();
  208. var delta = 0;
  209. if ( event.wheelDelta ) { // WebKit / Opera / Explorer 9
  210. delta = event.wheelDelta / 40;
  211. } else if ( event.detail ) { // Firefox
  212. delta = - event.detail / 3;
  213. }
  214. _zoomStart.y += ( 1 / delta ) * 0.05;
  215. }
  216. function touchstart( event ) {
  217. if ( ! _this.enabled ) return;
  218. event.preventDefault();
  219. switch ( event.touches.length ) {
  220. case 1:
  221. _rotateStart = _rotateEnd = _this.getMouseProjectionOnBall( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  222. break;
  223. case 2:
  224. _zoomStart = _zoomEnd = _this.getMouseOnScreen( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  225. break;
  226. case 3:
  227. _panStart = _panEnd = _this.getMouseOnScreen( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  228. break;
  229. }
  230. }
  231. function touchmove( event ) {
  232. if ( ! _this.enabled ) return;
  233. event.preventDefault();
  234. switch ( event.touches.length ) {
  235. case 1:
  236. _rotateEnd = _this.getMouseProjectionOnBall( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  237. break;
  238. case 2:
  239. _zoomEnd = _this.getMouseOnScreen( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  240. break;
  241. case 3:
  242. _panEnd = _this.getMouseOnScreen( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  243. break;
  244. }
  245. }
  246. this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  247. this.domElement.addEventListener( 'mousedown', mousedown, false );
  248. this.domElement.addEventListener( 'mousewheel', mousewheel, false );
  249. this.domElement.addEventListener( 'DOMMouseScroll', mousewheel, false ); // firefox
  250. this.domElement.addEventListener( 'touchstart', touchstart, false );
  251. this.domElement.addEventListener( 'touchend', touchstart, false );
  252. this.domElement.addEventListener( 'touchmove', touchmove, false );
  253. window.addEventListener( 'keydown', keydown, false );
  254. window.addEventListener( 'keyup', keyup, false );
  255. this.handleResize();
  256. };