TrackballControls.js 13 KB

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