OrthographicTrackballControls.js 12 KB

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