TrackballControls.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /**
  2. * @author Eberhard Graether / http://egraether.com/
  3. * @author Mark Lundin / http://mark-lundin.com
  4. * @author Simone Manini / http://daron1337.github.io
  5. * @author Luca Antiga / http://lantiga.github.io
  6. */
  7. THREE.TrackballControls = function ( object, domElement ) {
  8. var _this = this;
  9. var STATE = { NONE: -1, ROTATE: 0, ZOOM: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_ZOOM_PAN: 4 };
  10. this.object = object;
  11. this.domElement = ( domElement !== undefined ) ? domElement : document;
  12. // API
  13. this.enabled = true;
  14. this.screen = { left: 0, top: 0, width: 0, height: 0 };
  15. this.rotateSpeed = 1.0;
  16. this.zoomSpeed = 1.2;
  17. this.panSpeed = 0.3;
  18. this.noRotate = false;
  19. this.noZoom = false;
  20. this.noPan = false;
  21. this.noRoll = false;
  22. this.staticMoving = false;
  23. this.dynamicDampingFactor = 0.2;
  24. this.cylindricalRotation = true;
  25. this.minDistance = 0;
  26. this.maxDistance = Infinity;
  27. this.keys = [ 65 /*A*/, 83 /*S*/, 68 /*D*/ ];
  28. // internals
  29. this.target = new THREE.Vector3();
  30. var EPS = 0.000001;
  31. var lastPosition = new THREE.Vector3();
  32. var _state = STATE.NONE,
  33. _prevState = STATE.NONE,
  34. _eye = new THREE.Vector3(),
  35. _rotateStart = new THREE.Vector3(),
  36. _rotateEnd = new THREE.Vector3(),
  37. _movePrev = new THREE.Vector2(),
  38. _moveCurr = new THREE.Vector2(),
  39. _lastAxis = new THREE.Vector3(),
  40. _lastAngle = 0,
  41. _zoomStart = new THREE.Vector2(),
  42. _zoomEnd = new THREE.Vector2(),
  43. _touchZoomDistanceStart = 0,
  44. _touchZoomDistanceEnd = 0,
  45. _panStart = new THREE.Vector2(),
  46. _panEnd = new THREE.Vector2();
  47. // for reset
  48. this.target0 = this.target.clone();
  49. this.position0 = this.object.position.clone();
  50. this.up0 = this.object.up.clone();
  51. // events
  52. var changeEvent = { type: 'change' };
  53. var startEvent = { type: 'start'};
  54. var endEvent = { type: 'end'};
  55. // methods
  56. this.handleResize = function () {
  57. if ( this.domElement === document ) {
  58. this.screen.left = 0;
  59. this.screen.top = 0;
  60. this.screen.width = window.innerWidth;
  61. this.screen.height = window.innerHeight;
  62. } else {
  63. var box = this.domElement.getBoundingClientRect();
  64. // adjustments come from similar code in the jquery offset() function
  65. var d = this.domElement.ownerDocument.documentElement;
  66. this.screen.left = box.left + window.pageXOffset - d.clientLeft;
  67. this.screen.top = box.top + window.pageYOffset - d.clientTop;
  68. this.screen.width = box.width;
  69. this.screen.height = box.height;
  70. }
  71. };
  72. this.handleEvent = function ( event ) {
  73. if ( typeof this[ event.type ] == 'function' ) {
  74. this[ event.type ]( event );
  75. }
  76. };
  77. var getMouseOnScreen = ( function () {
  78. var vector = new THREE.Vector2();
  79. return function ( pageX, pageY ) {
  80. vector.set(
  81. ( pageX - _this.screen.left ) / _this.screen.width,
  82. ( pageY - _this.screen.top ) / _this.screen.height
  83. );
  84. return vector;
  85. };
  86. }() );
  87. var getMouseOnCircle = ( function () {
  88. var vector = new THREE.Vector2();
  89. return function ( pageX, pageY ) {
  90. vector.set(
  91. ( ( pageX - _this.screen.width * 0.5 - _this.screen.left ) / ( _this.screen.width * 0.5 ) ),
  92. ( ( _this.screen.height * 0.5 + _this.screen.top - pageY ) / ( _this.screen.height * 0.5 ) / _this.screen.width * _this.screen.height )
  93. );
  94. return vector;
  95. };
  96. }() );
  97. var getMouseProjectionOnBall = ( function () {
  98. var vector = new THREE.Vector3();
  99. var objectUp = new THREE.Vector3();
  100. var mouseOnBall = new THREE.Vector3();
  101. return function ( pageX, pageY ) {
  102. mouseOnBall.set(
  103. ( pageX - _this.screen.width * 0.5 - _this.screen.left ) / ( _this.screen.width * 0.5 ),
  104. ( _this.screen.height * 0.5 + _this.screen.top - pageY ) / ( _this.screen.height * 0.5 ),
  105. 0.0
  106. );
  107. var length = mouseOnBall.length();
  108. if ( _this.noRoll ) {
  109. if ( length < Math.SQRT1_2 ) {
  110. mouseOnBall.z = Math.sqrt( 1.0 - length*length );
  111. } else {
  112. mouseOnBall.z = 0.5 / length;
  113. }
  114. } else if ( length > 1.0 ) {
  115. mouseOnBall.normalize();
  116. } else {
  117. mouseOnBall.z = Math.sqrt( 1.0 - length * length );
  118. }
  119. _eye.copy( _this.object.position ).sub( _this.target );
  120. vector.copy( _this.object.up ).setLength( mouseOnBall.y );
  121. vector.add( objectUp.copy( _this.object.up ).cross( _eye ).setLength( mouseOnBall.x ) );
  122. vector.add( _eye.setLength( mouseOnBall.z ) );
  123. return vector;
  124. };
  125. }() );
  126. this.rotateCamera = (function(){
  127. var axis = new THREE.Vector3(),
  128. quaternion = new THREE.Quaternion(),
  129. eyeDirection = new THREE.Vector3(),
  130. objectUpDirection = new THREE.Vector3(),
  131. objectSidewaysDirection = new THREE.Vector3(),
  132. moveDirection = new THREE.Vector3(),
  133. angle;
  134. return function () {
  135. if ( _this.cylindricalRotation ) {
  136. moveDirection.set( _moveCurr.x - _movePrev.x, _moveCurr.y - _movePrev.y, 0 );
  137. angle = moveDirection.length();
  138. if ( angle ) {
  139. _eye.copy( _this.object.position ).sub( _this.target );
  140. eyeDirection.copy( _eye ).normalize();
  141. objectUpDirection.copy( _this.object.up ).normalize();
  142. objectSidewaysDirection.crossVectors( objectUpDirection, eyeDirection ).normalize();
  143. objectUpDirection.setLength( _moveCurr.y - _movePrev.y );
  144. objectSidewaysDirection.setLength( _moveCurr.x - _movePrev.x );
  145. moveDirection.copy( objectUpDirection.add( objectSidewaysDirection ) );
  146. axis.crossVectors( moveDirection, _eye ).normalize();
  147. angle *= _this.rotateSpeed;
  148. quaternion.setFromAxisAngle( axis, angle );
  149. _eye.applyQuaternion( quaternion );
  150. _this.object.up.applyQuaternion( quaternion );
  151. _lastAxis.copy( axis );
  152. _lastAngle = angle;
  153. }
  154. else if ( !_this.staticMoving && _lastAngle ) {
  155. _lastAngle *= Math.sqrt( 1.0 - _this.dynamicDampingFactor );
  156. _eye.copy( _this.object.position ).sub( _this.target );
  157. quaternion.setFromAxisAngle( _lastAxis, _lastAngle );
  158. _eye.applyQuaternion( quaternion );
  159. _this.object.up.applyQuaternion( quaternion );
  160. }
  161. _movePrev.copy( _moveCurr );
  162. } else {
  163. angle = Math.acos( _rotateStart.dot( _rotateEnd ) / _rotateStart.length() / _rotateEnd.length() );
  164. if ( angle ) {
  165. axis.crossVectors( _rotateStart, _rotateEnd ).normalize();
  166. angle *= _this.rotateSpeed;
  167. quaternion.setFromAxisAngle( axis, -angle );
  168. _eye.applyQuaternion( quaternion );
  169. _this.object.up.applyQuaternion( quaternion );
  170. _rotateEnd.applyQuaternion( quaternion );
  171. if ( _this.staticMoving ) {
  172. _rotateStart.copy( _rotateEnd );
  173. } else {
  174. quaternion.setFromAxisAngle( axis, angle * ( _this.dynamicDampingFactor - 1.0 ) );
  175. _rotateStart.applyQuaternion( quaternion );
  176. }
  177. }
  178. }
  179. };
  180. }());
  181. this.zoomCamera = function () {
  182. var factor;
  183. if ( _state === STATE.TOUCH_ZOOM_PAN ) {
  184. factor = _touchZoomDistanceStart / _touchZoomDistanceEnd;
  185. _touchZoomDistanceStart = _touchZoomDistanceEnd;
  186. _eye.multiplyScalar( factor );
  187. } else {
  188. factor = 1.0 + ( _zoomEnd.y - _zoomStart.y ) * _this.zoomSpeed;
  189. if ( factor !== 1.0 && factor > 0.0 ) {
  190. _eye.multiplyScalar( factor );
  191. if ( _this.staticMoving ) {
  192. _zoomStart.copy( _zoomEnd );
  193. } else {
  194. _zoomStart.y += ( _zoomEnd.y - _zoomStart.y ) * this.dynamicDampingFactor;
  195. }
  196. }
  197. }
  198. };
  199. this.panCamera = (function(){
  200. var mouseChange = new THREE.Vector2(),
  201. objectUp = new THREE.Vector3(),
  202. pan = new THREE.Vector3();
  203. return function () {
  204. mouseChange.copy( _panEnd ).sub( _panStart );
  205. if ( mouseChange.lengthSq() ) {
  206. mouseChange.multiplyScalar( _eye.length() * _this.panSpeed );
  207. pan.copy( _eye ).cross( _this.object.up ).setLength( mouseChange.x );
  208. pan.add( objectUp.copy( _this.object.up ).setLength( mouseChange.y ) );
  209. _this.object.position.add( pan );
  210. _this.target.add( pan );
  211. if ( _this.staticMoving ) {
  212. _panStart.copy( _panEnd );
  213. } else {
  214. _panStart.add( mouseChange.subVectors( _panEnd, _panStart ).multiplyScalar( _this.dynamicDampingFactor ) );
  215. }
  216. }
  217. };
  218. }());
  219. this.checkDistances = function () {
  220. if ( !_this.noZoom || !_this.noPan ) {
  221. if ( _eye.lengthSq() > _this.maxDistance * _this.maxDistance ) {
  222. _this.object.position.addVectors( _this.target, _eye.setLength( _this.maxDistance ) );
  223. }
  224. if ( _eye.lengthSq() < _this.minDistance * _this.minDistance ) {
  225. _this.object.position.addVectors( _this.target, _eye.setLength( _this.minDistance ) );
  226. }
  227. }
  228. };
  229. this.update = function () {
  230. _eye.subVectors( _this.object.position, _this.target );
  231. if ( !_this.noRotate ) {
  232. _this.rotateCamera();
  233. }
  234. if ( !_this.noZoom ) {
  235. _this.zoomCamera();
  236. }
  237. if ( !_this.noPan ) {
  238. _this.panCamera();
  239. }
  240. _this.object.position.addVectors( _this.target, _eye );
  241. _this.checkDistances();
  242. _this.object.lookAt( _this.target );
  243. if ( lastPosition.distanceToSquared( _this.object.position ) > EPS ) {
  244. _this.dispatchEvent( changeEvent );
  245. lastPosition.copy( _this.object.position );
  246. }
  247. };
  248. this.reset = function () {
  249. _state = STATE.NONE;
  250. _prevState = STATE.NONE;
  251. _this.target.copy( _this.target0 );
  252. _this.object.position.copy( _this.position0 );
  253. _this.object.up.copy( _this.up0 );
  254. _eye.subVectors( _this.object.position, _this.target );
  255. _this.object.lookAt( _this.target );
  256. _this.dispatchEvent( changeEvent );
  257. lastPosition.copy( _this.object.position );
  258. };
  259. // listeners
  260. function keydown( event ) {
  261. if ( _this.enabled === false ) return;
  262. window.removeEventListener( 'keydown', keydown );
  263. _prevState = _state;
  264. if ( _state !== STATE.NONE ) {
  265. return;
  266. } else if ( event.keyCode === _this.keys[ STATE.ROTATE ] && !_this.noRotate ) {
  267. _state = STATE.ROTATE;
  268. } else if ( event.keyCode === _this.keys[ STATE.ZOOM ] && !_this.noZoom ) {
  269. _state = STATE.ZOOM;
  270. } else if ( event.keyCode === _this.keys[ STATE.PAN ] && !_this.noPan ) {
  271. _state = STATE.PAN;
  272. }
  273. }
  274. function keyup( event ) {
  275. if ( _this.enabled === false ) return;
  276. _state = _prevState;
  277. window.addEventListener( 'keydown', keydown, false );
  278. }
  279. function mousedown( event ) {
  280. if ( _this.enabled === false ) return;
  281. event.preventDefault();
  282. event.stopPropagation();
  283. if ( _state === STATE.NONE ) {
  284. _state = event.button;
  285. }
  286. if ( _state === STATE.ROTATE && !_this.noRotate ) {
  287. if ( _this.cylindricalRotation ) {
  288. _moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
  289. _movePrev.copy(_moveCurr);
  290. } else {
  291. _rotateStart.copy( getMouseProjectionOnBall( event.pageX, event.pageY ) );
  292. _rotateEnd.copy( _rotateStart );
  293. }
  294. } else if ( _state === STATE.ZOOM && !_this.noZoom ) {
  295. _zoomStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  296. _zoomEnd.copy(_zoomStart);
  297. } else if ( _state === STATE.PAN && !_this.noPan ) {
  298. _panStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  299. _panEnd.copy(_panStart);
  300. }
  301. document.addEventListener( 'mousemove', mousemove, false );
  302. document.addEventListener( 'mouseup', mouseup, false );
  303. _this.dispatchEvent( startEvent );
  304. }
  305. function mousemove( event ) {
  306. if ( _this.enabled === false ) return;
  307. event.preventDefault();
  308. event.stopPropagation();
  309. if ( _state === STATE.ROTATE && !_this.noRotate ) {
  310. if ( _this.cylindricalRotation ) {
  311. _movePrev.copy(_moveCurr);
  312. _moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
  313. } else {
  314. _rotateEnd.copy( getMouseProjectionOnBall( event.pageX, event.pageY ) );
  315. }
  316. } else if ( _state === STATE.ZOOM && !_this.noZoom ) {
  317. _zoomEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  318. } else if ( _state === STATE.PAN && !_this.noPan ) {
  319. _panEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
  320. }
  321. }
  322. function mouseup( event ) {
  323. if ( _this.enabled === false ) return;
  324. event.preventDefault();
  325. event.stopPropagation();
  326. _state = STATE.NONE;
  327. document.removeEventListener( 'mousemove', mousemove );
  328. document.removeEventListener( 'mouseup', mouseup );
  329. _this.dispatchEvent( endEvent );
  330. }
  331. function mousewheel( event ) {
  332. if ( _this.enabled === false ) return;
  333. event.preventDefault();
  334. event.stopPropagation();
  335. var delta = 0;
  336. if ( event.wheelDelta ) { // WebKit / Opera / Explorer 9
  337. delta = event.wheelDelta / 40;
  338. } else if ( event.detail ) { // Firefox
  339. delta = - event.detail / 3;
  340. }
  341. _zoomStart.y += delta * 0.01;
  342. _this.dispatchEvent( startEvent );
  343. _this.dispatchEvent( endEvent );
  344. }
  345. function touchstart( event ) {
  346. if ( _this.enabled === false ) return;
  347. switch ( event.touches.length ) {
  348. case 1:
  349. _state = STATE.TOUCH_ROTATE;
  350. if ( _this.cylindricalRotation ) {
  351. _moveCurr.copy( getMouseOnCircle( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ) );
  352. _movePrev.copy(_moveCurr);
  353. } else {
  354. _rotateStart.copy( getMouseProjectionOnBall( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ) );
  355. _rotateEnd.copy( _rotateStart );
  356. }
  357. break;
  358. case 2:
  359. _state = STATE.TOUCH_ZOOM_PAN;
  360. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  361. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  362. _touchZoomDistanceEnd = _touchZoomDistanceStart = Math.sqrt( dx * dx + dy * dy );
  363. var x = ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX ) / 2;
  364. var y = ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY ) / 2;
  365. _panStart.copy( getMouseOnScreen( x, y ) );
  366. _panEnd.copy( _panStart );
  367. break;
  368. default:
  369. _state = STATE.NONE;
  370. }
  371. _this.dispatchEvent( startEvent );
  372. }
  373. function touchmove( event ) {
  374. if ( _this.enabled === false ) return;
  375. event.preventDefault();
  376. event.stopPropagation();
  377. switch ( event.touches.length ) {
  378. case 1:
  379. if ( _this.cylindricalRotation ) {
  380. _movePrev.copy(_moveCurr);
  381. _moveCurr.copy( getMouseOnCircle( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ) );
  382. } else {
  383. _rotateEnd.copy( getMouseProjectionOnBall( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ) );
  384. }
  385. break;
  386. case 2:
  387. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  388. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  389. _touchZoomDistanceEnd = Math.sqrt( dx * dx + dy * dy );
  390. var x = ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX ) / 2;
  391. var y = ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY ) / 2;
  392. _panEnd.copy( getMouseOnScreen( x, y ) );
  393. break;
  394. default:
  395. _state = STATE.NONE;
  396. }
  397. }
  398. function touchend( event ) {
  399. if ( _this.enabled === false ) return;
  400. switch ( event.touches.length ) {
  401. case 1:
  402. if ( _this.cylindricalRotation ) {
  403. _movePrev.copy(_moveCurr);
  404. _moveCurr.copy( getMouseOnCircle( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ) );
  405. } else {
  406. _rotateEnd.copy( getMouseProjectionOnBall( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ) );
  407. _rotateStart.copy( _rotateEnd );
  408. }
  409. break;
  410. case 2:
  411. _touchZoomDistanceStart = _touchZoomDistanceEnd = 0;
  412. var x = ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX ) / 2;
  413. var y = ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY ) / 2;
  414. _panEnd.copy( getMouseOnScreen( x, y ) );
  415. _panStart.copy( _panEnd );
  416. break;
  417. }
  418. _state = STATE.NONE;
  419. _this.dispatchEvent( endEvent );
  420. }
  421. this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  422. this.domElement.addEventListener( 'mousedown', mousedown, false );
  423. this.domElement.addEventListener( 'mousewheel', mousewheel, false );
  424. this.domElement.addEventListener( 'DOMMouseScroll', mousewheel, false ); // firefox
  425. this.domElement.addEventListener( 'touchstart', touchstart, false );
  426. this.domElement.addEventListener( 'touchend', touchend, false );
  427. this.domElement.addEventListener( 'touchmove', touchmove, false );
  428. window.addEventListener( 'keydown', keydown, false );
  429. window.addEventListener( 'keyup', keyup, false );
  430. this.handleResize();
  431. // force an update at start
  432. this.update();
  433. };
  434. THREE.TrackballControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  435. THREE.TrackballControls.prototype.constructor = THREE.TrackballControls;