CameraControls.js 26 KB

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