OrbitControls.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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. // This set of controls performs orbiting, dollying (zooming), and panning.
  10. // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  11. //
  12. // Orbit - left mouse / touch: one-finger move
  13. // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
  14. // Pan - right mouse, or left mouse + ctrl/meta/shiftKey, or arrow keys / touch: two-finger move
  15. THREE.OrbitControls = function ( object, domElement ) {
  16. this.object = object;
  17. this.domElement = ( domElement !== undefined ) ? domElement : document;
  18. // Set to false to disable this control
  19. this.enabled = true;
  20. // "target" sets the location of focus, where the object orbits around
  21. this.target = new THREE.Vector3();
  22. // How far you can dolly in and out ( PerspectiveCamera only )
  23. this.minDistance = 0;
  24. this.maxDistance = Infinity;
  25. // How far you can zoom in and out ( OrthographicCamera only )
  26. this.minZoom = 0;
  27. this.maxZoom = Infinity;
  28. // How far you can orbit vertically, upper and lower limits.
  29. // Range is 0 to Math.PI radians.
  30. this.minPolarAngle = 0; // radians
  31. this.maxPolarAngle = Math.PI; // radians
  32. // How far you can orbit horizontally, upper and lower limits.
  33. // If set, must be a sub-interval of the interval [ - Math.PI, Math.PI ].
  34. this.minAzimuthAngle = - Infinity; // radians
  35. this.maxAzimuthAngle = Infinity; // radians
  36. // Set to true to enable damping (inertia)
  37. // If damping is enabled, you must call controls.update() in your animation loop
  38. this.enableDamping = false;
  39. this.dampingFactor = 0.05;
  40. // This option actually enables dollying in and out; left as "zoom" for backwards compatibility.
  41. // Set to false to disable zooming
  42. this.enableZoom = true;
  43. this.zoomSpeed = 1.0;
  44. // Set to false to disable rotating
  45. this.enableRotate = true;
  46. this.rotateSpeed = 1.0;
  47. // Set to false to disable panning
  48. this.enablePan = true;
  49. this.panSpeed = 1.0;
  50. this.screenSpacePanning = false; // if true, pan in screen-space
  51. this.keyPanSpeed = 7.0; // pixels moved per arrow key push
  52. // Set to true to automatically rotate around the target
  53. // If auto-rotate is enabled, you must call controls.update() in your animation loop
  54. this.autoRotate = false;
  55. this.autoRotateSpeed = 2.0; // 30 seconds per round when fps is 60
  56. // Set to false to disable use of the keys
  57. this.enableKeys = true;
  58. // The four arrow keys
  59. this.keys = { LEFT: 37, UP: 38, RIGHT: 39, BOTTOM: 40 };
  60. // Mouse buttons
  61. this.mouseButtons = { LEFT: THREE.MOUSE.ROTATE, MIDDLE: THREE.MOUSE.DOLLY, RIGHT: THREE.MOUSE.PAN };
  62. // Touch fingers
  63. this.touches = { ONE: THREE.TOUCH.ROTATE, TWO: THREE.TOUCH.DOLLY_PAN };
  64. // for reset
  65. this.target0 = this.target.clone();
  66. this.position0 = this.object.position.clone();
  67. this.zoom0 = this.object.zoom;
  68. //
  69. // public methods
  70. //
  71. this.getPolarAngle = function () {
  72. return spherical.phi;
  73. };
  74. this.getAzimuthalAngle = function () {
  75. return spherical.theta;
  76. };
  77. this.saveState = function () {
  78. scope.target0.copy( scope.target );
  79. scope.position0.copy( scope.object.position );
  80. scope.zoom0 = scope.object.zoom;
  81. };
  82. this.reset = function () {
  83. scope.target.copy( scope.target0 );
  84. scope.object.position.copy( scope.position0 );
  85. scope.object.zoom = scope.zoom0;
  86. scope.object.updateProjectionMatrix();
  87. scope.dispatchEvent( changeEvent );
  88. scope.update();
  89. state = STATE.NONE;
  90. };
  91. // this method is exposed, but perhaps it would be better if we can make it private...
  92. this.update = function () {
  93. var offset = new THREE.Vector3();
  94. // so camera.up is the orbit axis
  95. var quat = new THREE.Quaternion().setFromUnitVectors( object.up, new THREE.Vector3( 0, 1, 0 ) );
  96. var quatInverse = quat.clone().inverse();
  97. var lastPosition = new THREE.Vector3();
  98. var lastQuaternion = new THREE.Quaternion();
  99. return function update() {
  100. var position = scope.object.position;
  101. offset.copy( position ).sub( scope.target );
  102. // rotate offset to "y-axis-is-up" space
  103. offset.applyQuaternion( quat );
  104. // angle from z-axis around y-axis
  105. spherical.setFromVector3( offset );
  106. if ( scope.autoRotate && state === STATE.NONE ) {
  107. rotateLeft( getAutoRotationAngle() );
  108. }
  109. if ( scope.enableDamping ) {
  110. spherical.theta += sphericalDelta.theta * scope.dampingFactor;
  111. spherical.phi += sphericalDelta.phi * scope.dampingFactor;
  112. } else {
  113. spherical.theta += sphericalDelta.theta;
  114. spherical.phi += sphericalDelta.phi;
  115. }
  116. // restrict theta to be between desired limits
  117. spherical.theta = Math.max( scope.minAzimuthAngle, Math.min( scope.maxAzimuthAngle, spherical.theta ) );
  118. // restrict phi to be between desired limits
  119. spherical.phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, spherical.phi ) );
  120. spherical.makeSafe();
  121. spherical.radius *= scale;
  122. // restrict radius to be between desired limits
  123. spherical.radius = Math.max( scope.minDistance, Math.min( scope.maxDistance, spherical.radius ) );
  124. // move target to panned location
  125. if ( scope.enableDamping === true ) {
  126. scope.target.addScaledVector( panOffset, scope.dampingFactor );
  127. } else {
  128. scope.target.add( panOffset );
  129. }
  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 THREE.Spherical();
  192. var sphericalDelta = new THREE.Spherical();
  193. var scale = 1;
  194. var panOffset = new THREE.Vector3();
  195. var zoomChanged = false;
  196. var rotateStart = new THREE.Vector2();
  197. var rotateEnd = new THREE.Vector2();
  198. var rotateDelta = new THREE.Vector2();
  199. var panStart = new THREE.Vector2();
  200. var panEnd = new THREE.Vector2();
  201. var panDelta = new THREE.Vector2();
  202. var dollyStart = new THREE.Vector2();
  203. var dollyEnd = new THREE.Vector2();
  204. var dollyDelta = new THREE.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 THREE.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 THREE.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 THREE.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. rotateStart.set( event.clientX, event.clientY );
  293. }
  294. function handleMouseDownDolly( event ) {
  295. dollyStart.set( event.clientX, event.clientY );
  296. }
  297. function handleMouseDownPan( event ) {
  298. panStart.set( event.clientX, event.clientY );
  299. }
  300. function handleMouseMoveRotate( event ) {
  301. rotateEnd.set( event.clientX, event.clientY );
  302. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  303. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  304. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
  305. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  306. rotateStart.copy( rotateEnd );
  307. scope.update();
  308. }
  309. function handleMouseMoveDolly( event ) {
  310. dollyEnd.set( event.clientX, event.clientY );
  311. dollyDelta.subVectors( dollyEnd, dollyStart );
  312. if ( dollyDelta.y > 0 ) {
  313. dollyIn( getZoomScale() );
  314. } else if ( dollyDelta.y < 0 ) {
  315. dollyOut( getZoomScale() );
  316. }
  317. dollyStart.copy( dollyEnd );
  318. scope.update();
  319. }
  320. function handleMouseMovePan( event ) {
  321. panEnd.set( event.clientX, event.clientY );
  322. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  323. pan( panDelta.x, panDelta.y );
  324. panStart.copy( panEnd );
  325. scope.update();
  326. }
  327. function handleMouseUp( /*event*/ ) {
  328. // no-op
  329. }
  330. function handleMouseWheel( event ) {
  331. if ( event.deltaY < 0 ) {
  332. dollyOut( getZoomScale() );
  333. } else if ( event.deltaY > 0 ) {
  334. dollyIn( getZoomScale() );
  335. }
  336. scope.update();
  337. }
  338. function handleKeyDown( event ) {
  339. var needsUpdate = false;
  340. switch ( event.keyCode ) {
  341. case scope.keys.UP:
  342. pan( 0, scope.keyPanSpeed );
  343. needsUpdate = true;
  344. break;
  345. case scope.keys.BOTTOM:
  346. pan( 0, - scope.keyPanSpeed );
  347. needsUpdate = true;
  348. break;
  349. case scope.keys.LEFT:
  350. pan( scope.keyPanSpeed, 0 );
  351. needsUpdate = true;
  352. break;
  353. case scope.keys.RIGHT:
  354. pan( - scope.keyPanSpeed, 0 );
  355. needsUpdate = true;
  356. break;
  357. }
  358. if ( needsUpdate ) {
  359. // prevent the browser from scrolling on cursor keys
  360. event.preventDefault();
  361. scope.update();
  362. }
  363. }
  364. function handleTouchStartRotate( event ) {
  365. if ( event.touches.length == 1 ) {
  366. rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  367. } else {
  368. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  369. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  370. rotateStart.set( x, y );
  371. }
  372. }
  373. function handleTouchStartPan( event ) {
  374. if ( event.touches.length == 1 ) {
  375. panStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  376. } else {
  377. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  378. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  379. panStart.set( x, y );
  380. }
  381. }
  382. function handleTouchStartDolly( event ) {
  383. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  384. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  385. var distance = Math.sqrt( dx * dx + dy * dy );
  386. dollyStart.set( 0, distance );
  387. }
  388. function handleTouchStartDollyPan( event ) {
  389. if ( scope.enableZoom ) handleTouchStartDolly( event );
  390. if ( scope.enablePan ) handleTouchStartPan( event );
  391. }
  392. function handleTouchStartDollyRotate( event ) {
  393. if ( scope.enableZoom ) handleTouchStartDolly( event );
  394. if ( scope.enableRotate ) handleTouchStartRotate( event );
  395. }
  396. function handleTouchMoveRotate( event ) {
  397. if ( event.touches.length == 1 ) {
  398. rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  399. } else {
  400. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  401. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  402. rotateEnd.set( x, y );
  403. }
  404. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  405. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  406. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
  407. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  408. rotateStart.copy( rotateEnd );
  409. }
  410. function handleTouchMovePan( event ) {
  411. if ( event.touches.length == 1 ) {
  412. panEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  413. } else {
  414. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  415. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  416. panEnd.set( x, y );
  417. }
  418. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  419. pan( panDelta.x, panDelta.y );
  420. panStart.copy( panEnd );
  421. }
  422. function handleTouchMoveDolly( event ) {
  423. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  424. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  425. var distance = Math.sqrt( dx * dx + dy * dy );
  426. dollyEnd.set( 0, distance );
  427. dollyDelta.set( 0, Math.pow( dollyEnd.y / dollyStart.y, scope.zoomSpeed ) );
  428. dollyIn( dollyDelta.y );
  429. dollyStart.copy( dollyEnd );
  430. }
  431. function handleTouchMoveDollyPan( event ) {
  432. if ( scope.enableZoom ) handleTouchMoveDolly( event );
  433. if ( scope.enablePan ) handleTouchMovePan( event );
  434. }
  435. function handleTouchMoveDollyRotate( event ) {
  436. if ( scope.enableZoom ) handleTouchMoveDolly( event );
  437. if ( scope.enableRotate ) handleTouchMoveRotate( event );
  438. }
  439. function handleTouchEnd( /*event*/ ) {
  440. // no-op
  441. }
  442. //
  443. // event handlers - FSM: listen for events and reset state
  444. //
  445. function onMouseDown( event ) {
  446. if ( scope.enabled === false ) return;
  447. // Prevent the browser from scrolling.
  448. event.preventDefault();
  449. // Manually set the focus since calling preventDefault above
  450. // prevents the browser from setting it automatically.
  451. scope.domElement.focus ? scope.domElement.focus() : window.focus();
  452. switch ( event.button ) {
  453. case 0:
  454. switch ( scope.mouseButtons.LEFT ) {
  455. case THREE.MOUSE.ROTATE:
  456. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  457. if ( scope.enablePan === false ) return;
  458. handleMouseDownPan( event );
  459. state = STATE.PAN;
  460. } else {
  461. if ( scope.enableRotate === false ) return;
  462. handleMouseDownRotate( event );
  463. state = STATE.ROTATE;
  464. }
  465. break;
  466. case THREE.MOUSE.PAN:
  467. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  468. if ( scope.enableRotate === false ) return;
  469. handleMouseDownRotate( event );
  470. state = STATE.ROTATE;
  471. } else {
  472. if ( scope.enablePan === false ) return;
  473. handleMouseDownPan( event );
  474. state = STATE.PAN;
  475. }
  476. break;
  477. default:
  478. state = STATE.NONE;
  479. }
  480. break;
  481. case 1:
  482. switch ( scope.mouseButtons.MIDDLE ) {
  483. case THREE.MOUSE.DOLLY:
  484. if ( scope.enableZoom === false ) return;
  485. handleMouseDownDolly( event );
  486. state = STATE.DOLLY;
  487. break;
  488. default:
  489. state = STATE.NONE;
  490. }
  491. break;
  492. case 2:
  493. switch ( scope.mouseButtons.RIGHT ) {
  494. case THREE.MOUSE.ROTATE:
  495. if ( scope.enableRotate === false ) return;
  496. handleMouseDownRotate( event );
  497. state = STATE.ROTATE;
  498. break;
  499. case THREE.MOUSE.PAN:
  500. if ( scope.enablePan === false ) return;
  501. handleMouseDownPan( event );
  502. state = STATE.PAN;
  503. break;
  504. default:
  505. state = STATE.NONE;
  506. }
  507. break;
  508. }
  509. if ( state !== STATE.NONE ) {
  510. document.addEventListener( 'mousemove', onMouseMove, false );
  511. document.addEventListener( 'mouseup', onMouseUp, false );
  512. scope.dispatchEvent( startEvent );
  513. }
  514. }
  515. function onMouseMove( event ) {
  516. if ( scope.enabled === false ) return;
  517. event.preventDefault();
  518. switch ( state ) {
  519. case STATE.ROTATE:
  520. if ( scope.enableRotate === false ) return;
  521. handleMouseMoveRotate( event );
  522. break;
  523. case STATE.DOLLY:
  524. if ( scope.enableZoom === false ) return;
  525. handleMouseMoveDolly( event );
  526. break;
  527. case STATE.PAN:
  528. if ( scope.enablePan === false ) return;
  529. handleMouseMovePan( event );
  530. break;
  531. }
  532. }
  533. function onMouseUp( event ) {
  534. if ( scope.enabled === false ) return;
  535. handleMouseUp( event );
  536. document.removeEventListener( 'mousemove', onMouseMove, false );
  537. document.removeEventListener( 'mouseup', onMouseUp, false );
  538. scope.dispatchEvent( endEvent );
  539. state = STATE.NONE;
  540. }
  541. function onMouseWheel( event ) {
  542. if ( scope.enabled === false || scope.enableZoom === false || ( state !== STATE.NONE && state !== STATE.ROTATE ) ) return;
  543. event.preventDefault();
  544. event.stopPropagation();
  545. scope.dispatchEvent( startEvent );
  546. handleMouseWheel( event );
  547. scope.dispatchEvent( endEvent );
  548. }
  549. function onKeyDown( event ) {
  550. if ( scope.enabled === false || scope.enableKeys === false || scope.enablePan === false ) return;
  551. handleKeyDown( event );
  552. }
  553. function onTouchStart( event ) {
  554. if ( scope.enabled === false ) return;
  555. event.preventDefault();
  556. switch ( event.touches.length ) {
  557. case 1:
  558. switch ( scope.touches.ONE ) {
  559. case THREE.TOUCH.ROTATE:
  560. if ( scope.enableRotate === false ) return;
  561. handleTouchStartRotate( event );
  562. state = STATE.TOUCH_ROTATE;
  563. break;
  564. case THREE.TOUCH.PAN:
  565. if ( scope.enablePan === false ) return;
  566. handleTouchStartPan( event );
  567. state = STATE.TOUCH_PAN;
  568. break;
  569. default:
  570. state = STATE.NONE;
  571. }
  572. break;
  573. case 2:
  574. switch ( scope.touches.TWO ) {
  575. case THREE.TOUCH.DOLLY_PAN:
  576. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  577. handleTouchStartDollyPan( event );
  578. state = STATE.TOUCH_DOLLY_PAN;
  579. break;
  580. case THREE.TOUCH.DOLLY_ROTATE:
  581. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  582. handleTouchStartDollyRotate( event );
  583. state = STATE.TOUCH_DOLLY_ROTATE;
  584. break;
  585. default:
  586. state = STATE.NONE;
  587. }
  588. break;
  589. default:
  590. state = STATE.NONE;
  591. }
  592. if ( state !== STATE.NONE ) {
  593. scope.dispatchEvent( startEvent );
  594. }
  595. }
  596. function onTouchMove( event ) {
  597. if ( scope.enabled === false ) return;
  598. event.preventDefault();
  599. event.stopPropagation();
  600. switch ( state ) {
  601. case STATE.TOUCH_ROTATE:
  602. if ( scope.enableRotate === false ) return;
  603. handleTouchMoveRotate( event );
  604. scope.update();
  605. break;
  606. case STATE.TOUCH_PAN:
  607. if ( scope.enablePan === false ) return;
  608. handleTouchMovePan( event );
  609. scope.update();
  610. break;
  611. case STATE.TOUCH_DOLLY_PAN:
  612. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  613. handleTouchMoveDollyPan( event );
  614. scope.update();
  615. break;
  616. case STATE.TOUCH_DOLLY_ROTATE:
  617. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  618. handleTouchMoveDollyRotate( event );
  619. scope.update();
  620. break;
  621. default:
  622. state = STATE.NONE;
  623. }
  624. }
  625. function onTouchEnd( event ) {
  626. if ( scope.enabled === false ) return;
  627. handleTouchEnd( event );
  628. scope.dispatchEvent( endEvent );
  629. state = STATE.NONE;
  630. }
  631. function onContextMenu( event ) {
  632. if ( scope.enabled === false ) return;
  633. event.preventDefault();
  634. }
  635. //
  636. scope.domElement.addEventListener( 'contextmenu', onContextMenu, false );
  637. scope.domElement.addEventListener( 'mousedown', onMouseDown, false );
  638. scope.domElement.addEventListener( 'wheel', onMouseWheel, false );
  639. scope.domElement.addEventListener( 'touchstart', onTouchStart, false );
  640. scope.domElement.addEventListener( 'touchend', onTouchEnd, false );
  641. scope.domElement.addEventListener( 'touchmove', onTouchMove, false );
  642. window.addEventListener( 'keydown', onKeyDown, false );
  643. // force an update at start
  644. this.update();
  645. };
  646. THREE.OrbitControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  647. THREE.OrbitControls.prototype.constructor = THREE.OrbitControls;
  648. // This set of controls performs orbiting, dollying (zooming), and panning.
  649. // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  650. // This is very similar to OrbitControls, another set of touch behavior
  651. //
  652. // Orbit - right mouse, or left mouse + ctrl/meta/shiftKey / touch: two-finger rotate
  653. // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
  654. // Pan - left mouse, or arrow keys / touch: one-finger move
  655. THREE.MapControls = function ( object, domElement ) {
  656. THREE.OrbitControls.call( this, object, domElement );
  657. this.mouseButtons.LEFT = THREE.MOUSE.PAN;
  658. this.mouseButtons.RIGHT = THREE.MOUSE.ROTATE;
  659. this.touches.ONE = THREE.TOUCH.PAN;
  660. this.touches.TWO = THREE.TOUCH.DOLLY_ROTATE;
  661. };
  662. THREE.MapControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  663. THREE.MapControls.prototype.constructor = THREE.MapControls;