OrbitControls.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. /**
  2. * @author qiao / https://github.com/qiao
  3. * @author mrdoob / http://mrdoob.com
  4. * @author alteredq / http://alteredqualia.com/
  5. * @author WestLangley / http://github.com/WestLangley
  6. * @author erich666 / http://erichaines.com
  7. * @author ScieCode / http://github.com/sciecode
  8. */
  9. import {
  10. EventDispatcher,
  11. MOUSE,
  12. Quaternion,
  13. Spherical,
  14. TOUCH,
  15. Vector2,
  16. Vector3
  17. } from "../../../build/three.module.js";
  18. // This set of controls performs orbiting, dollying (zooming), and panning.
  19. // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  20. //
  21. // Orbit - left mouse / touch: one-finger move
  22. // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
  23. // Pan - right mouse, or left mouse + ctrl/meta/shiftKey, or arrow keys / touch: two-finger move
  24. var OrbitControls = function ( object, domElement ) {
  25. this.object = object;
  26. this.domElement = ( domElement !== undefined ) ? domElement : document;
  27. // Set to false to disable this control
  28. this.enabled = true;
  29. // "target" sets the location of focus, where the object orbits around
  30. this.target = new Vector3();
  31. // How far you can dolly in and out ( PerspectiveCamera only )
  32. this.minDistance = 0;
  33. this.maxDistance = Infinity;
  34. // How far you can zoom in and out ( OrthographicCamera only )
  35. this.minZoom = 0;
  36. this.maxZoom = Infinity;
  37. // How far you can orbit vertically, upper and lower limits.
  38. // Range is 0 to Math.PI radians.
  39. this.minPolarAngle = 0; // radians
  40. this.maxPolarAngle = Math.PI; // radians
  41. // How far you can orbit horizontally, upper and lower limits.
  42. // If set, must be a sub-interval of the interval [ - Math.PI, Math.PI ].
  43. this.minAzimuthAngle = - Infinity; // radians
  44. this.maxAzimuthAngle = Infinity; // radians
  45. // Set to true to enable damping (inertia)
  46. // If damping is enabled, you must call controls.update() in your animation loop
  47. this.enableDamping = false;
  48. this.dampingFactor = 0.25;
  49. // This option actually enables dollying in and out; left as "zoom" for backwards compatibility.
  50. // Set to false to disable zooming
  51. this.enableZoom = true;
  52. this.zoomSpeed = 1.0;
  53. // Set to false to disable rotating
  54. this.enableRotate = true;
  55. this.rotateSpeed = 1.0;
  56. // Set to false to disable panning
  57. this.enablePan = true;
  58. this.panSpeed = 1.0;
  59. this.screenSpacePanning = false; // if true, pan in screen-space
  60. this.keyPanSpeed = 7.0; // pixels moved per arrow key push
  61. // Set to true to automatically rotate around the target
  62. // If auto-rotate is enabled, you must call controls.update() in your animation loop
  63. this.autoRotate = false;
  64. this.autoRotateSpeed = 2.0; // 30 seconds per round when fps is 60
  65. // Set to false to disable use of the keys
  66. this.enableKeys = true;
  67. // The four arrow keys
  68. this.keys = { LEFT: 37, UP: 38, RIGHT: 39, BOTTOM: 40 };
  69. // Mouse buttons
  70. this.mouseButtons = { LEFT: MOUSE.ROTATE, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.PAN };
  71. // Touch fingers
  72. this.touches = { ONE: TOUCH.ROTATE, TWO: TOUCH.DOLLY_PAN };
  73. // for reset
  74. this.target0 = this.target.clone();
  75. this.position0 = this.object.position.clone();
  76. this.zoom0 = this.object.zoom;
  77. //
  78. // public methods
  79. //
  80. this.getPolarAngle = function () {
  81. return spherical.phi;
  82. };
  83. this.getAzimuthalAngle = function () {
  84. return spherical.theta;
  85. };
  86. this.saveState = function () {
  87. scope.target0.copy( scope.target );
  88. scope.position0.copy( scope.object.position );
  89. scope.zoom0 = scope.object.zoom;
  90. };
  91. this.reset = function () {
  92. scope.target.copy( scope.target0 );
  93. scope.object.position.copy( scope.position0 );
  94. scope.object.zoom = scope.zoom0;
  95. scope.object.updateProjectionMatrix();
  96. scope.dispatchEvent( changeEvent );
  97. scope.update();
  98. state = STATE.NONE;
  99. };
  100. // this method is exposed, but perhaps it would be better if we can make it private...
  101. this.update = function () {
  102. var offset = new Vector3();
  103. // so camera.up is the orbit axis
  104. var quat = new Quaternion().setFromUnitVectors( object.up, new Vector3( 0, 1, 0 ) );
  105. var quatInverse = quat.clone().inverse();
  106. var lastPosition = new Vector3();
  107. var lastQuaternion = new Quaternion();
  108. return function update() {
  109. var position = scope.object.position;
  110. offset.copy( position ).sub( scope.target );
  111. // rotate offset to "y-axis-is-up" space
  112. offset.applyQuaternion( quat );
  113. // angle from z-axis around y-axis
  114. spherical.setFromVector3( offset );
  115. if ( scope.autoRotate && state === STATE.NONE ) {
  116. rotateLeft( getAutoRotationAngle() );
  117. }
  118. spherical.theta += sphericalDelta.theta;
  119. spherical.phi += sphericalDelta.phi;
  120. // restrict theta to be between desired limits
  121. spherical.theta = Math.max( scope.minAzimuthAngle, Math.min( scope.maxAzimuthAngle, spherical.theta ) );
  122. // restrict phi to be between desired limits
  123. spherical.phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, spherical.phi ) );
  124. spherical.makeSafe();
  125. spherical.radius *= scale;
  126. // restrict radius to be between desired limits
  127. spherical.radius = Math.max( scope.minDistance, Math.min( scope.maxDistance, spherical.radius ) );
  128. // move target to panned location
  129. scope.target.add( panOffset );
  130. offset.setFromSpherical( spherical );
  131. // rotate offset back to "camera-up-vector-is-up" space
  132. offset.applyQuaternion( quatInverse );
  133. position.copy( scope.target ).add( offset );
  134. scope.object.lookAt( scope.target );
  135. if ( scope.enableDamping === true ) {
  136. sphericalDelta.theta *= ( 1 - scope.dampingFactor );
  137. sphericalDelta.phi *= ( 1 - scope.dampingFactor );
  138. panOffset.multiplyScalar( 1 - scope.dampingFactor );
  139. } else {
  140. sphericalDelta.set( 0, 0, 0 );
  141. panOffset.set( 0, 0, 0 );
  142. }
  143. scale = 1;
  144. // update condition is:
  145. // min(camera displacement, camera rotation in radians)^2 > EPS
  146. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  147. if ( zoomChanged ||
  148. lastPosition.distanceToSquared( scope.object.position ) > EPS ||
  149. 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ) {
  150. scope.dispatchEvent( changeEvent );
  151. lastPosition.copy( scope.object.position );
  152. lastQuaternion.copy( scope.object.quaternion );
  153. zoomChanged = false;
  154. return true;
  155. }
  156. return false;
  157. };
  158. }();
  159. this.dispose = function () {
  160. scope.domElement.removeEventListener( 'contextmenu', onContextMenu, false );
  161. scope.domElement.removeEventListener( 'mousedown', onMouseDown, false );
  162. scope.domElement.removeEventListener( 'wheel', onMouseWheel, false );
  163. scope.domElement.removeEventListener( 'touchstart', onTouchStart, false );
  164. scope.domElement.removeEventListener( 'touchend', onTouchEnd, false );
  165. scope.domElement.removeEventListener( 'touchmove', onTouchMove, false );
  166. document.removeEventListener( 'mousemove', onMouseMove, false );
  167. document.removeEventListener( 'mouseup', onMouseUp, false );
  168. window.removeEventListener( 'keydown', onKeyDown, false );
  169. //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
  170. };
  171. //
  172. // internals
  173. //
  174. var scope = this;
  175. var changeEvent = { type: 'change' };
  176. var startEvent = { type: 'start' };
  177. var endEvent = { type: 'end' };
  178. var STATE = {
  179. NONE: - 1,
  180. ROTATE: 0,
  181. DOLLY: 1,
  182. PAN: 2,
  183. TOUCH_ROTATE: 3,
  184. TOUCH_PAN: 4,
  185. TOUCH_DOLLY_PAN: 5,
  186. TOUCH_DOLLY_ROTATE: 6
  187. };
  188. var state = STATE.NONE;
  189. var EPS = 0.000001;
  190. // current position in spherical coordinates
  191. var spherical = new Spherical();
  192. var sphericalDelta = new Spherical();
  193. var scale = 1;
  194. var panOffset = new Vector3();
  195. var zoomChanged = false;
  196. var rotateStart = new Vector2();
  197. var rotateEnd = new Vector2();
  198. var rotateDelta = new Vector2();
  199. var panStart = new Vector2();
  200. var panEnd = new Vector2();
  201. var panDelta = new Vector2();
  202. var dollyStart = new Vector2();
  203. var dollyEnd = new Vector2();
  204. var dollyDelta = new Vector2();
  205. function getAutoRotationAngle() {
  206. return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
  207. }
  208. function getZoomScale() {
  209. return Math.pow( 0.95, scope.zoomSpeed );
  210. }
  211. function rotateLeft( angle ) {
  212. sphericalDelta.theta -= angle;
  213. }
  214. function rotateUp( angle ) {
  215. sphericalDelta.phi -= angle;
  216. }
  217. var panLeft = function () {
  218. var v = new Vector3();
  219. return function panLeft( distance, objectMatrix ) {
  220. v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
  221. v.multiplyScalar( - distance );
  222. panOffset.add( v );
  223. };
  224. }();
  225. var panUp = function () {
  226. var v = new Vector3();
  227. return function panUp( distance, objectMatrix ) {
  228. if ( scope.screenSpacePanning === true ) {
  229. v.setFromMatrixColumn( objectMatrix, 1 );
  230. } else {
  231. v.setFromMatrixColumn( objectMatrix, 0 );
  232. v.crossVectors( scope.object.up, v );
  233. }
  234. v.multiplyScalar( distance );
  235. panOffset.add( v );
  236. };
  237. }();
  238. // deltaX and deltaY are in pixels; right and down are positive
  239. var pan = function () {
  240. var offset = new Vector3();
  241. return function pan( deltaX, deltaY ) {
  242. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  243. if ( scope.object.isPerspectiveCamera ) {
  244. // perspective
  245. var position = scope.object.position;
  246. offset.copy( position ).sub( scope.target );
  247. var targetDistance = offset.length();
  248. // half of the fov is center to top of screen
  249. targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
  250. // we use only clientHeight here so aspect ratio does not distort speed
  251. panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
  252. panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
  253. } else if ( scope.object.isOrthographicCamera ) {
  254. // orthographic
  255. panLeft( deltaX * ( scope.object.right - scope.object.left ) / scope.object.zoom / element.clientWidth, scope.object.matrix );
  256. panUp( deltaY * ( scope.object.top - scope.object.bottom ) / scope.object.zoom / element.clientHeight, scope.object.matrix );
  257. } else {
  258. // camera neither orthographic nor perspective
  259. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
  260. scope.enablePan = false;
  261. }
  262. };
  263. }();
  264. function dollyIn( dollyScale ) {
  265. if ( scope.object.isPerspectiveCamera ) {
  266. scale /= dollyScale;
  267. } else if ( scope.object.isOrthographicCamera ) {
  268. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom * dollyScale ) );
  269. scope.object.updateProjectionMatrix();
  270. zoomChanged = true;
  271. } else {
  272. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  273. scope.enableZoom = false;
  274. }
  275. }
  276. function dollyOut( dollyScale ) {
  277. if ( scope.object.isPerspectiveCamera ) {
  278. scale *= dollyScale;
  279. } else if ( scope.object.isOrthographicCamera ) {
  280. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / dollyScale ) );
  281. scope.object.updateProjectionMatrix();
  282. zoomChanged = true;
  283. } else {
  284. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  285. scope.enableZoom = false;
  286. }
  287. }
  288. //
  289. // event callbacks - update the object state
  290. //
  291. function handleMouseDownRotate( event ) {
  292. //console.log( 'handleMouseDownRotate' );
  293. rotateStart.set( event.clientX, event.clientY );
  294. }
  295. function handleMouseDownDolly( event ) {
  296. //console.log( 'handleMouseDownDolly' );
  297. dollyStart.set( event.clientX, event.clientY );
  298. }
  299. function handleMouseDownPan( event ) {
  300. //console.log( 'handleMouseDownPan' );
  301. panStart.set( event.clientX, event.clientY );
  302. }
  303. function handleMouseMoveRotate( event ) {
  304. //console.log( 'handleMouseMoveRotate' );
  305. rotateEnd.set( event.clientX, event.clientY );
  306. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  307. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  308. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
  309. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  310. rotateStart.copy( rotateEnd );
  311. scope.update();
  312. }
  313. function handleMouseMoveDolly( event ) {
  314. //console.log( 'handleMouseMoveDolly' );
  315. dollyEnd.set( event.clientX, event.clientY );
  316. dollyDelta.subVectors( dollyEnd, dollyStart );
  317. if ( dollyDelta.y > 0 ) {
  318. dollyIn( getZoomScale() );
  319. } else if ( dollyDelta.y < 0 ) {
  320. dollyOut( getZoomScale() );
  321. }
  322. dollyStart.copy( dollyEnd );
  323. scope.update();
  324. }
  325. function handleMouseMovePan( event ) {
  326. //console.log( 'handleMouseMovePan' );
  327. panEnd.set( event.clientX, event.clientY );
  328. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  329. pan( panDelta.x, panDelta.y );
  330. panStart.copy( panEnd );
  331. scope.update();
  332. }
  333. function handleMouseUp( event ) {
  334. // console.log( 'handleMouseUp' );
  335. }
  336. function handleMouseWheel( event ) {
  337. // console.log( 'handleMouseWheel' );
  338. if ( event.deltaY < 0 ) {
  339. dollyOut( getZoomScale() );
  340. } else if ( event.deltaY > 0 ) {
  341. dollyIn( getZoomScale() );
  342. }
  343. scope.update();
  344. }
  345. function handleKeyDown( event ) {
  346. // console.log( 'handleKeyDown' );
  347. var needsUpdate = false;
  348. switch ( event.keyCode ) {
  349. case scope.keys.UP:
  350. pan( 0, scope.keyPanSpeed );
  351. needsUpdate = true;
  352. break;
  353. case scope.keys.BOTTOM:
  354. pan( 0, - scope.keyPanSpeed );
  355. needsUpdate = true;
  356. break;
  357. case scope.keys.LEFT:
  358. pan( scope.keyPanSpeed, 0 );
  359. needsUpdate = true;
  360. break;
  361. case scope.keys.RIGHT:
  362. pan( - scope.keyPanSpeed, 0 );
  363. needsUpdate = true;
  364. break;
  365. }
  366. if ( needsUpdate ) {
  367. // prevent the browser from scrolling on cursor keys
  368. event.preventDefault();
  369. scope.update();
  370. }
  371. }
  372. function handleTouchStartRotate( event ) {
  373. //console.log( 'handleTouchStartRotate' );
  374. if ( event.touches.length == 1 ) {
  375. rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  376. }
  377. else {
  378. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  379. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  380. rotateStart.set( x, y );
  381. }
  382. }
  383. function handleTouchStartPan( event ) {
  384. //console.log( 'handleTouchStartPan' );
  385. if ( event.touches.length == 1 ) {
  386. panStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  387. }
  388. else {
  389. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  390. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  391. panStart.set( x, y );
  392. }
  393. }
  394. function handleTouchStartDolly( event ) {
  395. //console.log( 'handleTouchStartDolly' );
  396. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  397. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  398. var distance = Math.sqrt( dx * dx + dy * dy );
  399. dollyStart.set( 0, distance );
  400. }
  401. function handleTouchStartDollyPan( event ) {
  402. //console.log( 'handleTouchStartDollyPan' );
  403. if ( scope.enableZoom ) handleTouchStartDolly( event );
  404. if ( scope.enablePan ) handleTouchStartPan( event );
  405. }
  406. function handleTouchStartDollyRotate( event ) {
  407. //console.log( 'handleTouchStartDollyRotate' );
  408. if ( scope.enableZoom ) handleTouchStartDolly( event );
  409. if ( scope.enableRotate ) handleTouchStartRotate( event );
  410. }
  411. function handleTouchMoveRotate( event ) {
  412. //console.log( 'handleTouchMoveRotate' );
  413. if ( event.touches.length == 1 ) {
  414. rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  415. }
  416. else {
  417. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  418. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  419. rotateEnd.set( x, y );
  420. }
  421. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  422. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  423. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
  424. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  425. rotateStart.copy( rotateEnd );
  426. }
  427. function handleTouchMovePan( event ) {
  428. //console.log( 'handleTouchMoveRotate' );
  429. if ( event.touches.length == 1 ) {
  430. panEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  431. }
  432. else {
  433. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  434. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  435. panEnd.set( x, y );
  436. }
  437. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  438. pan( panDelta.x, panDelta.y );
  439. panStart.copy( panEnd );
  440. }
  441. function handleTouchMoveDolly( event ) {
  442. //console.log( 'handleTouchMoveRotate' );
  443. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  444. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  445. var distance = Math.sqrt( dx * dx + dy * dy );
  446. dollyEnd.set( 0, distance );
  447. dollyDelta.set( 0, Math.pow( dollyEnd.y / dollyStart.y, scope.zoomSpeed ) );
  448. dollyIn( dollyDelta.y );
  449. dollyStart.copy( dollyEnd );
  450. }
  451. function handleTouchMoveDollyPan( event ) {
  452. //console.log( 'handleTouchMoveDollyPan' );
  453. if ( scope.enableZoom ) handleTouchMoveDolly( event );
  454. if ( scope.enablePan ) handleTouchMovePan( event );
  455. }
  456. function handleTouchMoveDollyRotate( event ) {
  457. //console.log( 'handleTouchMoveDollyPan' );
  458. if ( scope.enableZoom ) handleTouchMoveDolly( event );
  459. if ( scope.enableRotate ) handleTouchMoveRotate( event );
  460. }
  461. function handleTouchEnd( event ) {
  462. //console.log( 'handleTouchEnd' );
  463. }
  464. //
  465. // event handlers - FSM: listen for events and reset state
  466. //
  467. function onMouseDown( event ) {
  468. if ( scope.enabled === false ) return;
  469. // Prevent the browser from scrolling.
  470. event.preventDefault();
  471. // Manually set the focus since calling preventDefault above
  472. // prevents the browser from setting it automatically.
  473. scope.domElement.focus ? scope.domElement.focus() : window.focus();
  474. switch ( event.button ) {
  475. case 0:
  476. switch ( scope.mouseButtons.LEFT ) {
  477. case MOUSE.ROTATE:
  478. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  479. if ( scope.enablePan === false ) return;
  480. handleMouseDownPan( event );
  481. state = STATE.PAN;
  482. } else {
  483. if ( scope.enableRotate === false ) return;
  484. handleMouseDownRotate( event );
  485. state = STATE.ROTATE;
  486. }
  487. break;
  488. case MOUSE.PAN:
  489. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  490. if ( scope.enableRotate === false ) return;
  491. handleMouseDownRotate( event );
  492. state = STATE.ROTATE;
  493. } else {
  494. if ( scope.enablePan === false ) return;
  495. handleMouseDownPan( event );
  496. state = STATE.PAN;
  497. }
  498. break;
  499. default:
  500. state = STATE.NONE;
  501. }
  502. break;
  503. case 1:
  504. switch ( scope.mouseButtons.MIDDLE ) {
  505. case MOUSE.DOLLY:
  506. if ( scope.enableZoom === false ) return;
  507. handleMouseDownDolly( event );
  508. state = STATE.DOLLY;
  509. break;
  510. default:
  511. state = STATE.NONE;
  512. }
  513. break;
  514. case 2:
  515. switch ( scope.mouseButtons.RIGHT ) {
  516. case MOUSE.ROTATE:
  517. if ( scope.enableRotate === false ) return;
  518. handleMouseDownRotate( event );
  519. state = STATE.ROTATE;
  520. break;
  521. case MOUSE.PAN:
  522. if ( scope.enablePan === false ) return;
  523. handleMouseDownPan( event );
  524. state = STATE.PAN;
  525. break;
  526. default:
  527. state = STATE.NONE;
  528. }
  529. break;
  530. }
  531. if ( state !== STATE.NONE ) {
  532. document.addEventListener( 'mousemove', onMouseMove, false );
  533. document.addEventListener( 'mouseup', onMouseUp, false );
  534. scope.dispatchEvent( startEvent );
  535. }
  536. }
  537. function onMouseMove( event ) {
  538. if ( scope.enabled === false ) return;
  539. event.preventDefault();
  540. switch ( state ) {
  541. case STATE.ROTATE:
  542. if ( scope.enableRotate === false ) return;
  543. handleMouseMoveRotate( event );
  544. break;
  545. case STATE.DOLLY:
  546. if ( scope.enableZoom === false ) return;
  547. handleMouseMoveDolly( event );
  548. break;
  549. case STATE.PAN:
  550. if ( scope.enablePan === false ) return;
  551. handleMouseMovePan( event );
  552. break;
  553. }
  554. }
  555. function onMouseUp( event ) {
  556. if ( scope.enabled === false ) return;
  557. handleMouseUp( event );
  558. document.removeEventListener( 'mousemove', onMouseMove, false );
  559. document.removeEventListener( 'mouseup', onMouseUp, false );
  560. scope.dispatchEvent( endEvent );
  561. state = STATE.NONE;
  562. }
  563. function onMouseWheel( event ) {
  564. if ( scope.enabled === false || scope.enableZoom === false || ( state !== STATE.NONE && state !== STATE.ROTATE ) ) return;
  565. event.preventDefault();
  566. event.stopPropagation();
  567. scope.dispatchEvent( startEvent );
  568. handleMouseWheel( event );
  569. scope.dispatchEvent( endEvent );
  570. }
  571. function onKeyDown( event ) {
  572. if ( scope.enabled === false || scope.enableKeys === false || scope.enablePan === false ) return;
  573. handleKeyDown( event );
  574. }
  575. function onTouchStart( event ) {
  576. if ( scope.enabled === false ) return;
  577. event.preventDefault();
  578. switch ( event.touches.length ) {
  579. case 1:
  580. switch ( scope.touches.ONE ) {
  581. case TOUCH.ROTATE:
  582. if ( scope.enableRotate === false ) return;
  583. handleTouchStartRotate( event );
  584. state = STATE.TOUCH_ROTATE;
  585. break;
  586. case TOUCH.PAN:
  587. if ( scope.enablePan === false ) return;
  588. handleTouchStartPan( event );
  589. state = STATE.TOUCH_PAN;
  590. break;
  591. default:
  592. state = STATE.NONE;
  593. }
  594. break;
  595. case 2:
  596. switch ( scope.touches.TWO ) {
  597. case TOUCH.DOLLY_PAN:
  598. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  599. handleTouchStartDollyPan( event );
  600. state = STATE.TOUCH_DOLLY_PAN;
  601. break;
  602. case TOUCH.DOLLY_ROTATE:
  603. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  604. handleTouchStartDollyRotate( event );
  605. state = STATE.TOUCH_DOLLY_ROTATE;
  606. break;
  607. default:
  608. state = STATE.NONE;
  609. }
  610. break;
  611. default:
  612. state = STATE.NONE;
  613. }
  614. if ( state !== STATE.NONE ) {
  615. scope.dispatchEvent( startEvent );
  616. }
  617. }
  618. function onTouchMove( event ) {
  619. if ( scope.enabled === false ) return;
  620. event.preventDefault();
  621. event.stopPropagation();
  622. switch ( state ) {
  623. case STATE.TOUCH_ROTATE:
  624. if ( scope.enableRotate === false ) return;
  625. handleTouchMoveRotate( event );
  626. scope.update();
  627. break;
  628. case STATE.TOUCH_PAN:
  629. if ( scope.enablePan === false ) return;
  630. handleTouchMovePan( event );
  631. scope.update();
  632. break;
  633. case STATE.TOUCH_DOLLY_PAN:
  634. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  635. handleTouchMoveDollyPan( event );
  636. scope.update();
  637. break;
  638. case STATE.TOUCH_DOLLY_ROTATE:
  639. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  640. handleTouchMoveDollyRotate( event );
  641. scope.update();
  642. break;
  643. default:
  644. state = STATE.NONE;
  645. }
  646. }
  647. function onTouchEnd( event ) {
  648. if ( scope.enabled === false ) return;
  649. handleTouchEnd( event );
  650. scope.dispatchEvent( endEvent );
  651. state = STATE.NONE;
  652. }
  653. function onContextMenu( event ) {
  654. if ( scope.enabled === false ) return;
  655. event.preventDefault();
  656. }
  657. //
  658. scope.domElement.addEventListener( 'contextmenu', onContextMenu, false );
  659. scope.domElement.addEventListener( 'mousedown', onMouseDown, false );
  660. scope.domElement.addEventListener( 'wheel', onMouseWheel, false );
  661. scope.domElement.addEventListener( 'touchstart', onTouchStart, false );
  662. scope.domElement.addEventListener( 'touchend', onTouchEnd, false );
  663. scope.domElement.addEventListener( 'touchmove', onTouchMove, false );
  664. window.addEventListener( 'keydown', onKeyDown, false );
  665. // force an update at start
  666. this.update();
  667. };
  668. OrbitControls.prototype = Object.create( EventDispatcher.prototype );
  669. OrbitControls.prototype.constructor = OrbitControls;
  670. Object.defineProperties( OrbitControls.prototype, {
  671. center: {
  672. get: function () {
  673. console.warn( 'THREE.OrbitControls: .center has been renamed to .target' );
  674. return this.target;
  675. }
  676. },
  677. // backward compatibility
  678. noZoom: {
  679. get: function () {
  680. console.warn( 'THREE.OrbitControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  681. return ! this.enableZoom;
  682. },
  683. set: function ( value ) {
  684. console.warn( 'THREE.OrbitControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  685. this.enableZoom = ! value;
  686. }
  687. },
  688. noRotate: {
  689. get: function () {
  690. console.warn( 'THREE.OrbitControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  691. return ! this.enableRotate;
  692. },
  693. set: function ( value ) {
  694. console.warn( 'THREE.OrbitControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  695. this.enableRotate = ! value;
  696. }
  697. },
  698. noPan: {
  699. get: function () {
  700. console.warn( 'THREE.OrbitControls: .noPan has been deprecated. Use .enablePan instead.' );
  701. return ! this.enablePan;
  702. },
  703. set: function ( value ) {
  704. console.warn( 'THREE.OrbitControls: .noPan has been deprecated. Use .enablePan instead.' );
  705. this.enablePan = ! value;
  706. }
  707. },
  708. noKeys: {
  709. get: function () {
  710. console.warn( 'THREE.OrbitControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  711. return ! this.enableKeys;
  712. },
  713. set: function ( value ) {
  714. console.warn( 'THREE.OrbitControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  715. this.enableKeys = ! value;
  716. }
  717. },
  718. staticMoving: {
  719. get: function () {
  720. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  721. return ! this.enableDamping;
  722. },
  723. set: function ( value ) {
  724. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  725. this.enableDamping = ! value;
  726. }
  727. },
  728. dynamicDampingFactor: {
  729. get: function () {
  730. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  731. return this.dampingFactor;
  732. },
  733. set: function ( value ) {
  734. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  735. this.dampingFactor = value;
  736. }
  737. }
  738. } );
  739. // This set of controls performs orbiting, dollying (zooming), and panning.
  740. // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  741. // This is very similar to OrbitControls, another set of touch behavior
  742. //
  743. // Orbit - right mouse, or left mouse + ctrl/meta/shiftKey / touch: two-finger rotate
  744. // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
  745. // Pan - left mouse, or arrow keys / touch: one-finger move
  746. var MapControls = function ( object, domElement ) {
  747. OrbitControls.call( this, object, domElement );
  748. this.mouseButtons.LEFT = MOUSE.PAN;
  749. this.mouseButtons.RIGHT = MOUSE.ROTATE;
  750. this.touches.ONE = TOUCH.PAN;
  751. this.touches.TWO = TOUCH.DOLLY_ROTATE;
  752. }
  753. MapControls.prototype = Object.create( EventDispatcher.prototype );
  754. MapControls.prototype.constructor = MapControls;
  755. export { OrbitControls, MapControls };