TrackballControls.js 11 KB

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