OrbitControls.js 25 KB

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