TrackballControls.js 12 KB

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