OrbitControls.js.bak 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. */
  8. // This set of controls performs orbiting, dollying (zooming), and panning.
  9. // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  10. //
  11. // Orbit - left mouse / touch: one finger move
  12. // Zoom - middle mouse, or mousewheel / touch: two finger spread or squish
  13. // Pan - right mouse, or arrow keys / touch: three finger swipe
  14. //import THREE from 'THREE';
  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.keyPanSpeed = 7.0; // pixels moved per arrow key push
  50. // Set to true to automatically rotate around the target
  51. // If auto-rotate is enabled, you must call controls.update() in your animation loop
  52. this.autoRotate = false;
  53. this.autoRotateSpeed = 2.0; // 30 seconds per round when fps is 60
  54. // Set to false to disable use of the keys
  55. this.enableKeys = true;
  56. // The four arrow keys
  57. this.keys = { LEFT: 37, UP: 38, RIGHT: 39, BOTTOM: 40 };
  58. // Mouse buttons
  59. this.mouseButtons = { ORBIT: THREE.MOUSE.LEFT, ZOOM: THREE.MOUSE.MIDDLE, PAN: THREE.MOUSE.RIGHT };
  60. // for reset
  61. this.target0 = this.target.clone();
  62. this.position0 = this.object.position.clone();
  63. this.zoom0 = this.object.zoom;
  64. //
  65. // public methods
  66. //
  67. this.getPolarAngle = function () {
  68. return spherical.phi;
  69. };
  70. this.getAzimuthalAngle = function () {
  71. return spherical.theta;
  72. };
  73. this.reset = function () {
  74. scope.target.copy( scope.target0 );
  75. scope.object.position.copy( scope.position0 );
  76. scope.object.zoom = scope.zoom0;
  77. scope.object.updateProjectionMatrix();
  78. scope.dispatchEvent( changeEvent );
  79. scope.update();
  80. state = STATE.NONE;
  81. };
  82. // this method is exposed, but perhaps it would be better if we can make it private...
  83. this.update = function () {
  84. var offset = new THREE.Vector3();
  85. // 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().inverse();
  88. var lastPosition = new THREE.Vector3();
  89. var lastQuaternion = new THREE.Quaternion();
  90. return function update() {
  91. var position = scope.object.position;
  92. offset.copy( position ).sub( scope.target );
  93. // rotate offset to "y-axis-is-up" space
  94. offset.applyQuaternion( quat );
  95. // angle from z-axis around y-axis
  96. spherical.setFromVector3( offset );
  97. if ( scope.autoRotate && state === STATE.NONE ) {
  98. rotateLeft( getAutoRotationAngle() );
  99. }
  100. spherical.theta += sphericalDelta.theta;
  101. spherical.phi += sphericalDelta.phi;
  102. // restrict theta to be between desired limits
  103. spherical.theta = Math.max( scope.minAzimuthAngle, Math.min( scope.maxAzimuthAngle, spherical.theta ) );
  104. // restrict phi to be between desired limits
  105. spherical.phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, spherical.phi ) );
  106. spherical.makeSafe();
  107. spherical.radius *= scale;
  108. // restrict radius to be between desired limits
  109. spherical.radius = Math.max( scope.minDistance, Math.min( scope.maxDistance, spherical.radius ) );
  110. // move target to panned location
  111. scope.target.add( panOffset );
  112. offset.setFromSpherical( spherical );
  113. // rotate offset back to "camera-up-vector-is-up" space
  114. offset.applyQuaternion( quatInverse );
  115. position.copy( scope.target ).add( offset );
  116. scope.object.lookAt( scope.target );
  117. if ( scope.enableDamping === true ) {
  118. sphericalDelta.theta *= ( 1 - scope.dampingFactor );
  119. sphericalDelta.phi *= ( 1 - scope.dampingFactor );
  120. } else {
  121. sphericalDelta.set( 0, 0, 0 );
  122. }
  123. scale = 1;
  124. panOffset.set( 0, 0, 0 );
  125. // update condition is:
  126. // min(camera displacement, camera rotation in radians)^2 > EPS
  127. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  128. if ( zoomChanged ||
  129. lastPosition.distanceToSquared( scope.object.position ) > EPS ||
  130. 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ) {
  131. scope.dispatchEvent( changeEvent );
  132. lastPosition.copy( scope.object.position );
  133. lastQuaternion.copy( scope.object.quaternion );
  134. zoomChanged = false;
  135. return true;
  136. }
  137. return false;
  138. };
  139. }();
  140. this.dispose = function () {
  141. scope.domElement.removeEventListener( 'contextmenu', onContextMenu, false );
  142. scope.domElement.removeEventListener( 'mousedown', onMouseDown, false );
  143. scope.domElement.removeEventListener( 'wheel', onMouseWheel, false );
  144. scope.domElement.removeEventListener( 'touchstart', onTouchStart, false );
  145. scope.domElement.removeEventListener( 'touchend', onTouchEnd, false );
  146. scope.domElement.removeEventListener( 'touchmove', onTouchMove, false );
  147. document.removeEventListener( 'mousemove', onMouseMove, false );
  148. document.removeEventListener( 'mouseup', onMouseUp, false );
  149. window.removeEventListener( 'keydown', onKeyDown, false );
  150. //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
  151. };
  152. //
  153. // internals
  154. //
  155. var scope = this;
  156. var changeEvent = { type: 'change' };
  157. var startEvent = { type: 'start' };
  158. var endEvent = { type: 'end' };
  159. var STATE = { NONE: - 1, ROTATE: 0, DOLLY: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_DOLLY: 4, TOUCH_PAN: 5 };
  160. var state = STATE.NONE;
  161. var EPS = 0.000001;
  162. // current position in spherical coordinates
  163. var spherical = new THREE.Spherical();
  164. var sphericalDelta = new THREE.Spherical();
  165. var scale = 1;
  166. var panOffset = new THREE.Vector3();
  167. var zoomChanged = false;
  168. var rotateStart = new THREE.Vector2();
  169. var rotateEnd = new THREE.Vector2();
  170. var rotateDelta = new THREE.Vector2();
  171. var panStart = new THREE.Vector2();
  172. var panEnd = new THREE.Vector2();
  173. var panDelta = new THREE.Vector2();
  174. var dollyStart = new THREE.Vector2();
  175. var dollyEnd = new THREE.Vector2();
  176. var dollyDelta = new THREE.Vector2();
  177. function getAutoRotationAngle() {
  178. return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
  179. }
  180. function getZoomScale() {
  181. return Math.pow( 0.95, scope.zoomSpeed );
  182. }
  183. function rotateLeft( angle ) {
  184. sphericalDelta.theta -= angle;
  185. }
  186. function rotateUp( angle ) {
  187. sphericalDelta.phi -= angle;
  188. }
  189. var panLeft = function () {
  190. var v = new THREE.Vector3();
  191. return function panLeft( distance, objectMatrix ) {
  192. v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
  193. v.multiplyScalar( - distance );
  194. panOffset.add( v );
  195. };
  196. }();
  197. var panUp = function () {
  198. var v = new THREE.Vector3();
  199. return function panUp( distance, objectMatrix ) {
  200. v.setFromMatrixColumn( objectMatrix, 1 ); // get Y column of objectMatrix
  201. v.multiplyScalar( distance );
  202. panOffset.add( v );
  203. };
  204. }();
  205. // deltaX and deltaY are in pixels; right and down are positive
  206. var pan = function () {
  207. var offset = new THREE.Vector3();
  208. return function pan( deltaX, deltaY ) {
  209. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  210. if ( scope.object instanceof THREE.PerspectiveCamera ) {
  211. // perspective
  212. var position = scope.object.position;
  213. offset.copy( position ).sub( scope.target );
  214. var targetDistance = offset.length();
  215. // half of the fov is center to top of screen
  216. targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
  217. // we actually don't use screenWidth, since perspective camera is fixed to screen height
  218. panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
  219. panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
  220. } else if ( scope.object instanceof THREE.OrthographicCamera ) {
  221. // orthographic
  222. panLeft( deltaX * ( scope.object.right - scope.object.left ) / scope.object.zoom / element.clientWidth, scope.object.matrix );
  223. panUp( deltaY * ( scope.object.top - scope.object.bottom ) / scope.object.zoom / element.clientHeight, scope.object.matrix );
  224. } else {
  225. // camera neither orthographic nor perspective
  226. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
  227. scope.enablePan = false;
  228. }
  229. };
  230. }();
  231. function dollyIn( dollyScale ) {
  232. if ( scope.object instanceof THREE.PerspectiveCamera ) {
  233. scale /= dollyScale;
  234. } else if ( scope.object instanceof THREE.OrthographicCamera ) {
  235. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom * dollyScale ) );
  236. scope.object.updateProjectionMatrix();
  237. zoomChanged = true;
  238. } else {
  239. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  240. scope.enableZoom = false;
  241. }
  242. }
  243. function dollyOut( dollyScale ) {
  244. if ( scope.object instanceof THREE.PerspectiveCamera ) {
  245. scale *= dollyScale;
  246. } else if ( scope.object instanceof THREE.OrthographicCamera ) {
  247. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / dollyScale ) );
  248. scope.object.updateProjectionMatrix();
  249. zoomChanged = true;
  250. } else {
  251. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  252. scope.enableZoom = false;
  253. }
  254. }
  255. //
  256. // event callbacks - update the object state
  257. //
  258. function handleMouseDownRotate( event ) {
  259. //console.log( 'handleMouseDownRotate' );
  260. rotateStart.set( event.clientX, event.clientY );
  261. }
  262. function handleMouseDownDolly( event ) {
  263. //console.log( 'handleMouseDownDolly' );
  264. dollyStart.set( event.clientX, event.clientY );
  265. }
  266. function handleMouseDownPan( event ) {
  267. //console.log( 'handleMouseDownPan' );
  268. panStart.set( event.clientX, event.clientY );
  269. }
  270. function handleMouseMoveRotate( event ) {
  271. //console.log( 'handleMouseMoveRotate' );
  272. rotateEnd.set( event.clientX, event.clientY );
  273. rotateDelta.subVectors( rotateEnd, rotateStart );
  274. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  275. // rotating across whole screen goes 360 degrees around
  276. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth * scope.rotateSpeed );
  277. // rotating up and down along whole screen attempts to go 360, but limited to 180
  278. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight * scope.rotateSpeed );
  279. rotateStart.copy( rotateEnd );
  280. scope.update();
  281. }
  282. function handleMouseMoveDolly( event ) {
  283. //console.log( 'handleMouseMoveDolly' );
  284. dollyEnd.set( event.clientX, event.clientY );
  285. dollyDelta.subVectors( dollyEnd, dollyStart );
  286. if ( dollyDelta.y > 0 ) {
  287. dollyIn( getZoomScale() );
  288. } else if ( dollyDelta.y < 0 ) {
  289. dollyOut( getZoomScale() );
  290. }
  291. dollyStart.copy( dollyEnd );
  292. scope.update();
  293. }
  294. function handleMouseMovePan( event ) {
  295. //console.log( 'handleMouseMovePan' );
  296. panEnd.set( event.clientX, event.clientY );
  297. panDelta.subVectors( panEnd, panStart );
  298. pan( panDelta.x, panDelta.y );
  299. panStart.copy( panEnd );
  300. scope.update();
  301. }
  302. function handleMouseUp( event ) {
  303. // console.log( 'handleMouseUp' );
  304. }
  305. function handleMouseWheel( event ) {
  306. // console.log( 'handleMouseWheel' );
  307. if ( event.deltaY < 0 ) {
  308. dollyOut( getZoomScale() );
  309. } else if ( event.deltaY > 0 ) {
  310. dollyIn( getZoomScale() );
  311. }
  312. scope.update();
  313. }
  314. function handleKeyDown( event ) {
  315. //console.log( 'handleKeyDown' );
  316. switch ( event.keyCode ) {
  317. case scope.keys.UP:
  318. pan( 0, scope.keyPanSpeed );
  319. scope.update();
  320. break;
  321. case scope.keys.BOTTOM:
  322. pan( 0, - scope.keyPanSpeed );
  323. scope.update();
  324. break;
  325. case scope.keys.LEFT:
  326. pan( scope.keyPanSpeed, 0 );
  327. scope.update();
  328. break;
  329. case scope.keys.RIGHT:
  330. pan( - scope.keyPanSpeed, 0 );
  331. scope.update();
  332. break;
  333. }
  334. }
  335. function handleTouchStartRotate( event ) {
  336. //console.log( 'handleTouchStartRotate' );
  337. rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  338. }
  339. function handleTouchStartDolly( event ) {
  340. //console.log( 'handleTouchStartDolly' );
  341. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  342. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  343. var distance = Math.sqrt( dx * dx + dy * dy );
  344. dollyStart.set( 0, distance );
  345. }
  346. function handleTouchStartPan( event ) {
  347. //console.log( 'handleTouchStartPan' );
  348. panStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  349. }
  350. function handleTouchMoveRotate( event ) {
  351. //console.log( 'handleTouchMoveRotate' );
  352. rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  353. rotateDelta.subVectors( rotateEnd, rotateStart );
  354. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  355. // rotating across whole screen goes 360 degrees around
  356. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth * scope.rotateSpeed );
  357. // rotating up and down along whole screen attempts to go 360, but limited to 180
  358. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight * scope.rotateSpeed );
  359. rotateStart.copy( rotateEnd );
  360. scope.update();
  361. }
  362. function handleTouchMoveDolly( event ) {
  363. //console.log( 'handleTouchMoveDolly' );
  364. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  365. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  366. var distance = Math.sqrt( dx * dx + dy * dy );
  367. dollyEnd.set( 0, distance );
  368. dollyDelta.subVectors( dollyEnd, dollyStart );
  369. if ( dollyDelta.y > 0 ) {
  370. dollyOut( getZoomScale() );
  371. } else if ( dollyDelta.y < 0 ) {
  372. dollyIn( getZoomScale() );
  373. }
  374. dollyStart.copy( dollyEnd );
  375. scope.update();
  376. }
  377. function handleTouchMovePan( event ) {
  378. //console.log( 'handleTouchMovePan' );
  379. panEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  380. panDelta.subVectors( panEnd, panStart );
  381. pan( panDelta.x, panDelta.y );
  382. panStart.copy( panEnd );
  383. scope.update();
  384. }
  385. function handleTouchEnd( event ) {
  386. //console.log( 'handleTouchEnd' );
  387. }
  388. //
  389. // event handlers - FSM: listen for events and reset state
  390. //
  391. function onMouseDown( event ) {
  392. if ( scope.enabled === false ) return;
  393. event.preventDefault();
  394. if ( event.button === scope.mouseButtons.ORBIT ) {
  395. if ( scope.enableRotate === false ) return;
  396. handleMouseDownRotate( event );
  397. state = STATE.ROTATE;
  398. } else if ( event.button === scope.mouseButtons.ZOOM ) {
  399. if ( scope.enableZoom === false ) return;
  400. handleMouseDownDolly( event );
  401. state = STATE.DOLLY;
  402. } else if ( event.button === scope.mouseButtons.PAN ) {
  403. if ( scope.enablePan === false ) return;
  404. handleMouseDownPan( event );
  405. state = STATE.PAN;
  406. }
  407. if ( state !== STATE.NONE ) {
  408. document.addEventListener( 'mousemove', onMouseMove, false );
  409. document.addEventListener( 'mouseup', onMouseUp, false );
  410. scope.dispatchEvent( startEvent );
  411. }
  412. }
  413. function onMouseMove( event ) {
  414. if ( scope.enabled === false ) return;
  415. event.preventDefault();
  416. if ( state === STATE.ROTATE ) {
  417. if ( scope.enableRotate === false ) return;
  418. handleMouseMoveRotate( event );
  419. } else if ( state === STATE.DOLLY ) {
  420. if ( scope.enableZoom === false ) return;
  421. handleMouseMoveDolly( event );
  422. } else if ( state === STATE.PAN ) {
  423. if ( scope.enablePan === false ) return;
  424. handleMouseMovePan( event );
  425. }
  426. }
  427. function onMouseUp( event ) {
  428. if ( scope.enabled === false ) return;
  429. handleMouseUp( event );
  430. document.removeEventListener( 'mousemove', onMouseMove, false );
  431. document.removeEventListener( 'mouseup', onMouseUp, false );
  432. scope.dispatchEvent( endEvent );
  433. state = STATE.NONE;
  434. }
  435. function onMouseWheel( event ) {
  436. if ( scope.enabled === false || scope.enableZoom === false || ( state !== STATE.NONE && state !== STATE.ROTATE ) ) return;
  437. event.preventDefault();
  438. event.stopPropagation();
  439. handleMouseWheel( event );
  440. scope.dispatchEvent( startEvent ); // not sure why these are here...
  441. scope.dispatchEvent( endEvent );
  442. }
  443. function onKeyDown( event ) {
  444. if ( scope.enabled === false || scope.enableKeys === false || scope.enablePan === false ) return;
  445. handleKeyDown( event );
  446. }
  447. function onTouchStart( event ) {
  448. if ( scope.enabled === false ) return;
  449. switch ( event.touches.length ) {
  450. case 1: // one-fingered touch: rotate
  451. if ( scope.enableRotate === false ) return;
  452. handleTouchStartRotate( event );
  453. state = STATE.TOUCH_ROTATE;
  454. break;
  455. case 2: // two-fingered touch: dolly
  456. if ( scope.enableZoom === false ) return;
  457. handleTouchStartDolly( event );
  458. state = STATE.TOUCH_DOLLY;
  459. break;
  460. case 3: // three-fingered touch: pan
  461. if ( scope.enablePan === false ) return;
  462. handleTouchStartPan( event );
  463. state = STATE.TOUCH_PAN;
  464. break;
  465. default:
  466. state = STATE.NONE;
  467. }
  468. if ( state !== STATE.NONE ) {
  469. scope.dispatchEvent( startEvent );
  470. }
  471. }
  472. function onTouchMove( event ) {
  473. if ( scope.enabled === false ) return;
  474. event.preventDefault();
  475. event.stopPropagation();
  476. switch ( event.touches.length ) {
  477. case 1: // one-fingered touch: rotate
  478. if ( scope.enableRotate === false ) return;
  479. if ( state !== STATE.TOUCH_ROTATE ) return; // is this needed?...
  480. handleTouchMoveRotate( event );
  481. break;
  482. case 2: // two-fingered touch: dolly
  483. if ( scope.enableZoom === false ) return;
  484. if ( state !== STATE.TOUCH_DOLLY ) return; // is this needed?...
  485. handleTouchMoveDolly( event );
  486. break;
  487. case 3: // three-fingered touch: pan
  488. if ( scope.enablePan === false ) return;
  489. if ( state !== STATE.TOUCH_PAN ) return; // is this needed?...
  490. handleTouchMovePan( event );
  491. break;
  492. default:
  493. state = STATE.NONE;
  494. }
  495. }
  496. function onTouchEnd( event ) {
  497. if ( scope.enabled === false ) return;
  498. handleTouchEnd( event );
  499. scope.dispatchEvent( endEvent );
  500. state = STATE.NONE;
  501. }
  502. function onContextMenu( event ) {
  503. event.preventDefault();
  504. }
  505. //
  506. scope.domElement.addEventListener( 'contextmenu', onContextMenu, false );
  507. scope.domElement.addEventListener( 'mousedown', onMouseDown, false );
  508. scope.domElement.addEventListener( 'wheel', onMouseWheel, false );
  509. scope.domElement.addEventListener( 'touchstart', onTouchStart, false );
  510. scope.domElement.addEventListener( 'touchend', onTouchEnd, false );
  511. scope.domElement.addEventListener( 'touchmove', onTouchMove, false );
  512. window.addEventListener( 'keydown', onKeyDown, false );
  513. // force an update at start
  514. this.update();
  515. };
  516. THREE.OrbitControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  517. THREE.OrbitControls.prototype.constructor = THREE.OrbitControls;
  518. Object.defineProperties( THREE.OrbitControls.prototype, {
  519. center: {
  520. get: function () {
  521. console.warn( 'THREE.OrbitControls: .center has been renamed to .target' );
  522. return this.target;
  523. }
  524. },
  525. // backward compatibility
  526. noZoom: {
  527. get: function () {
  528. console.warn( 'THREE.OrbitControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  529. return ! this.enableZoom;
  530. },
  531. set: function ( value ) {
  532. console.warn( 'THREE.OrbitControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  533. this.enableZoom = ! value;
  534. }
  535. },
  536. noRotate: {
  537. get: function () {
  538. console.warn( 'THREE.OrbitControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  539. return ! this.enableRotate;
  540. },
  541. set: function ( value ) {
  542. console.warn( 'THREE.OrbitControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  543. this.enableRotate = ! value;
  544. }
  545. },
  546. noPan: {
  547. get: function () {
  548. console.warn( 'THREE.OrbitControls: .noPan has been deprecated. Use .enablePan instead.' );
  549. return ! this.enablePan;
  550. },
  551. set: function ( value ) {
  552. console.warn( 'THREE.OrbitControls: .noPan has been deprecated. Use .enablePan instead.' );
  553. this.enablePan = ! value;
  554. }
  555. },
  556. noKeys: {
  557. get: function () {
  558. console.warn( 'THREE.OrbitControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  559. return ! this.enableKeys;
  560. },
  561. set: function ( value ) {
  562. console.warn( 'THREE.OrbitControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  563. this.enableKeys = ! value;
  564. }
  565. },
  566. staticMoving: {
  567. get: function () {
  568. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  569. return ! this.enableDamping;
  570. },
  571. set: function ( value ) {
  572. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  573. this.enableDamping = ! value;
  574. }
  575. },
  576. dynamicDampingFactor: {
  577. get: function () {
  578. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  579. return this.dampingFactor;
  580. },
  581. set: function ( value ) {
  582. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  583. this.dampingFactor = value;
  584. }
  585. }
  586. } );