OrbitControls.js 27 KB

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