OrthographicTrackballControls.js 13 KB

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