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