TrackballControls.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. if ( this.object instanceof THREE.PerspectiveCamera ) {
  117. _eye.multiplyScalar( factor );
  118. } else {
  119. this.object.left *= factor;
  120. this.object.right *= factor;
  121. this.object.top *= factor;
  122. this.object.bottom *= factor;
  123. this.object.updateProjectionMatrix();
  124. }
  125. if ( _this.staticMoving ) {
  126. _zoomStart.copy( _zoomEnd );
  127. } else {
  128. _zoomStart.y += ( _zoomEnd.y - _zoomStart.y ) * this.dynamicDampingFactor;
  129. }
  130. }
  131. }
  132. };
  133. this.panCamera = function () {
  134. var mouseChange = _panEnd.clone().sub( _panStart );
  135. if ( mouseChange.lengthSq() ) {
  136. mouseChange.multiplyScalar( _eye.length() * _this.panSpeed );
  137. var pan = _eye.clone().cross( _this.object.up ).setLength( mouseChange.x );
  138. pan.add( _this.object.up.clone().setLength( mouseChange.y ) );
  139. _this.object.position.add( pan );
  140. _this.target.add( pan );
  141. if ( _this.staticMoving ) {
  142. _panStart = _panEnd;
  143. } else {
  144. _panStart.add( mouseChange.subVectors( _panEnd, _panStart ).multiplyScalar( _this.dynamicDampingFactor ) );
  145. }
  146. }
  147. };
  148. this.checkDistances = function () {
  149. if ( !_this.noZoom || !_this.noPan ) {
  150. if ( _this.object.position.lengthSq() > _this.maxDistance * _this.maxDistance ) {
  151. _this.object.position.setLength( _this.maxDistance );
  152. }
  153. if ( _eye.lengthSq() < _this.minDistance * _this.minDistance ) {
  154. _this.object.position.addVectors( _this.target, _eye.setLength( _this.minDistance ) );
  155. }
  156. }
  157. };
  158. this.update = function () {
  159. _eye.subVectors( _this.object.position, _this.target );
  160. if ( !_this.noRotate ) {
  161. _this.rotateCamera();
  162. }
  163. if ( !_this.noZoom ) {
  164. _this.zoomCamera();
  165. }
  166. if ( !_this.noPan ) {
  167. _this.panCamera();
  168. }
  169. _this.object.position.addVectors( _this.target, _eye );
  170. _this.checkDistances();
  171. _this.object.lookAt( _this.target );
  172. if ( lastPosition.distanceToSquared( _this.object.position ) > 0 ) {
  173. _this.dispatchEvent( changeEvent );
  174. lastPosition.copy( _this.object.position );
  175. }
  176. };
  177. this.reset = function () {
  178. _state = STATE.NONE;
  179. _prevState = STATE.NONE;
  180. _this.target.copy( _this.target0 );
  181. _this.object.position.copy( _this.position0 );
  182. _this.object.up.copy( _this.up0 );
  183. _eye.subVectors( _this.object.position, _this.target );
  184. _this.object.lookAt( _this.target );
  185. _this.dispatchEvent( changeEvent );
  186. lastPosition.copy( _this.object.position );
  187. };
  188. // listeners
  189. function keydown( event ) {
  190. if ( _this.enabled === false ) return;
  191. window.removeEventListener( 'keydown', keydown );
  192. _prevState = _state;
  193. if ( _state !== STATE.NONE ) {
  194. return;
  195. } else if ( event.keyCode === _this.keys[ STATE.ROTATE ] && !_this.noRotate ) {
  196. _state = STATE.ROTATE;
  197. } else if ( event.keyCode === _this.keys[ STATE.ZOOM ] && !_this.noZoom ) {
  198. _state = STATE.ZOOM;
  199. } else if ( event.keyCode === _this.keys[ STATE.PAN ] && !_this.noPan ) {
  200. _state = STATE.PAN;
  201. }
  202. }
  203. function keyup( event ) {
  204. if ( _this.enabled === false ) return;
  205. _state = _prevState;
  206. window.addEventListener( 'keydown', keydown, false );
  207. }
  208. function mousedown( event ) {
  209. if ( _this.enabled === false ) return;
  210. event.preventDefault();
  211. event.stopPropagation();
  212. if ( _state === STATE.NONE ) {
  213. _state = event.button;
  214. }
  215. if ( _state === STATE.ROTATE && !_this.noRotate ) {
  216. _rotateStart = _rotateEnd = _this.getMouseProjectionOnBall( event.clientX, event.clientY );
  217. } else if ( _state === STATE.ZOOM && !_this.noZoom ) {
  218. _zoomStart = _zoomEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
  219. } else if ( _state === STATE.PAN && !_this.noPan ) {
  220. _panStart = _panEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
  221. }
  222. document.addEventListener( 'mousemove', mousemove, false );
  223. document.addEventListener( 'mouseup', mouseup, false );
  224. }
  225. function mousemove( event ) {
  226. if ( _this.enabled === false ) return;
  227. event.preventDefault();
  228. event.stopPropagation();
  229. if ( _state === STATE.ROTATE && !_this.noRotate ) {
  230. _rotateEnd = _this.getMouseProjectionOnBall( event.clientX, event.clientY );
  231. } else if ( _state === STATE.ZOOM && !_this.noZoom ) {
  232. _zoomEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
  233. } else if ( _state === STATE.PAN && !_this.noPan ) {
  234. _panEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
  235. }
  236. }
  237. function mouseup( event ) {
  238. if ( _this.enabled === false ) return;
  239. event.preventDefault();
  240. event.stopPropagation();
  241. _state = STATE.NONE;
  242. document.removeEventListener( 'mousemove', mousemove );
  243. document.removeEventListener( 'mouseup', mouseup );
  244. }
  245. function mousewheel( event ) {
  246. if ( _this.enabled === false ) return;
  247. event.preventDefault();
  248. event.stopPropagation();
  249. var delta = 0;
  250. if ( event.wheelDelta ) { // WebKit / Opera / Explorer 9
  251. delta = event.wheelDelta / 40;
  252. } else if ( event.detail ) { // Firefox
  253. delta = - event.detail / 3;
  254. }
  255. _zoomStart.y += delta * 0.01;
  256. }
  257. function touchstart( event ) {
  258. if ( _this.enabled === false ) return;
  259. switch ( event.touches.length ) {
  260. case 1:
  261. _state = STATE.TOUCH_ROTATE;
  262. _rotateStart = _rotateEnd = _this.getMouseProjectionOnBall( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  263. break;
  264. case 2:
  265. _state = STATE.TOUCH_ZOOM;
  266. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  267. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  268. _touchZoomDistanceEnd = _touchZoomDistanceStart = Math.sqrt( dx * dx + dy * dy );
  269. break;
  270. case 3:
  271. _state = STATE.TOUCH_PAN;
  272. _panStart = _panEnd = _this.getMouseOnScreen( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  273. break;
  274. default:
  275. _state = STATE.NONE;
  276. }
  277. }
  278. function touchmove( event ) {
  279. if ( _this.enabled === false ) return;
  280. event.preventDefault();
  281. event.stopPropagation();
  282. switch ( event.touches.length ) {
  283. case 1:
  284. _rotateEnd = _this.getMouseProjectionOnBall( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  285. break;
  286. case 2:
  287. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  288. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  289. _touchZoomDistanceEnd = Math.sqrt( dx * dx + dy * dy )
  290. break;
  291. case 3:
  292. _panEnd = _this.getMouseOnScreen( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  293. break;
  294. default:
  295. _state = STATE.NONE;
  296. }
  297. }
  298. function touchend( event ) {
  299. if ( _this.enabled === false ) return;
  300. switch ( event.touches.length ) {
  301. case 1:
  302. _rotateStart = _rotateEnd = _this.getMouseProjectionOnBall( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  303. break;
  304. case 2:
  305. _touchZoomDistanceStart = _touchZoomDistanceEnd = 0;
  306. break;
  307. case 3:
  308. _panStart = _panEnd = _this.getMouseOnScreen( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  309. break;
  310. }
  311. _state = STATE.NONE;
  312. }
  313. this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  314. this.domElement.addEventListener( 'mousedown', mousedown, false );
  315. this.domElement.addEventListener( 'mousewheel', mousewheel, false );
  316. this.domElement.addEventListener( 'DOMMouseScroll', mousewheel, false ); // firefox
  317. this.domElement.addEventListener( 'touchstart', touchstart, false );
  318. this.domElement.addEventListener( 'touchend', touchend, false );
  319. this.domElement.addEventListener( 'touchmove', touchmove, false );
  320. window.addEventListener( 'keydown', keydown, false );
  321. window.addEventListener( 'keyup', keyup, false );
  322. this.handleResize();
  323. };
  324. THREE.TrackballControls.prototype = Object.create( THREE.EventDispatcher.prototype );