OrthographicTrackballControls.js 14 KB

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