OrbitControls.js 25 KB

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