TrackballControls.js 11 KB

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