OrbitControls.js 26 KB

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