TrackballControls.js 11 KB

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