TrackballControls.js 12 KB

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