TrackballControls.js 11 KB

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