OrbitControls.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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. /*global THREE, console */
  9. // This set of controls performs orbiting, dollying (zooming), and panning. It maintains
  10. // the "up" direction as +Y, unlike the TrackballControls. Touch on tablet and phones is
  11. // supported.
  12. //
  13. // Orbit - left mouse / touch: one finger move
  14. // Zoom - middle mouse, or mousewheel / touch: two finger spread or squish
  15. // Pan - right mouse, or arrow keys / touch: three finter swipe
  16. THREE.OrbitControls = function ( object, domElement ) {
  17. this.object = object;
  18. this.domElement = ( domElement !== undefined ) ? domElement : document;
  19. // API
  20. // Set to false to disable this control
  21. this.enabled = true;
  22. // "target" sets the location of focus, where the control orbits around
  23. // and where it pans with respect to.
  24. this.target = new THREE.Vector3();
  25. // center is old, deprecated; use "target" instead
  26. this.center = this.target;
  27. // This option actually enables dollying in and out; left as "zoom" for
  28. // backwards compatibility
  29. this.noZoom = false;
  30. this.zoomSpeed = 1.0;
  31. // Limits to how far you can dolly in and out ( PerspectiveCamera only )
  32. this.minDistance = 0;
  33. this.maxDistance = Infinity;
  34. // Limits to how far you can zoom in and out ( OrthographicCamera only )
  35. this.minZoom = 0;
  36. this.maxZoom = Infinity;
  37. // Set to true to disable this control
  38. this.noRotate = false;
  39. this.rotateSpeed = 1.0;
  40. // Set to true to disable this control
  41. this.noPan = false;
  42. this.keyPanSpeed = 7.0; // pixels moved per arrow key push
  43. // Set to true to automatically rotate around the target
  44. this.autoRotate = false;
  45. this.autoRotateSpeed = 2.0; // 30 seconds per round when fps is 60
  46. // How far you can orbit vertically, upper and lower limits.
  47. // Range is 0 to Math.PI radians.
  48. this.minPolarAngle = 0; // radians
  49. this.maxPolarAngle = Math.PI; // radians
  50. // How far you can orbit horizontally, upper and lower limits.
  51. // If set, must be a sub-interval of the interval [ - Math.PI, Math.PI ].
  52. this.minAzimuthAngle = - Infinity; // radians
  53. this.maxAzimuthAngle = Infinity; // radians
  54. // Set to true to disable use of the keys
  55. this.noKeys = false;
  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. ////////////
  61. // internals
  62. var scope = this;
  63. var EPS = 0.000001;
  64. var rotateStart = new THREE.Vector2();
  65. var rotateEnd = new THREE.Vector2();
  66. var rotateDelta = new THREE.Vector2();
  67. var panStart = new THREE.Vector2();
  68. var panEnd = new THREE.Vector2();
  69. var panDelta = new THREE.Vector2();
  70. var panOffset = new THREE.Vector3();
  71. var offset = new THREE.Vector3();
  72. var dollyStart = new THREE.Vector2();
  73. var dollyEnd = new THREE.Vector2();
  74. var dollyDelta = new THREE.Vector2();
  75. var theta;
  76. var phi;
  77. var phiDelta = 0;
  78. var thetaDelta = 0;
  79. var scale = 1;
  80. var pan = new THREE.Vector3();
  81. var lastPosition = new THREE.Vector3();
  82. var lastQuaternion = new THREE.Quaternion();
  83. var STATE = { NONE : -1, ROTATE : 0, DOLLY : 1, PAN : 2, TOUCH_ROTATE : 3, TOUCH_DOLLY : 4, TOUCH_PAN : 5 };
  84. var state = STATE.NONE;
  85. // for reset
  86. this.target0 = this.target.clone();
  87. this.position0 = this.object.position.clone();
  88. this.zoom0 = this.object.zoom;
  89. // so camera.up is the orbit axis
  90. var quat = new THREE.Quaternion().setFromUnitVectors( object.up, new THREE.Vector3( 0, 1, 0 ) );
  91. var quatInverse = quat.clone().inverse();
  92. // events
  93. var changeEvent = { type: 'change' };
  94. var startEvent = { type: 'start' };
  95. var endEvent = { type: 'end' };
  96. this.rotateLeft = function ( angle ) {
  97. if ( angle === undefined ) {
  98. angle = getAutoRotationAngle();
  99. }
  100. thetaDelta -= angle;
  101. };
  102. this.rotateUp = function ( angle ) {
  103. if ( angle === undefined ) {
  104. angle = getAutoRotationAngle();
  105. }
  106. phiDelta -= angle;
  107. };
  108. // pass in distance in world space to move left
  109. this.panLeft = function ( distance ) {
  110. var te = this.object.matrix.elements;
  111. // get X column of matrix
  112. panOffset.set( te[ 0 ], te[ 1 ], te[ 2 ] );
  113. panOffset.multiplyScalar( - distance );
  114. pan.add( panOffset );
  115. };
  116. // pass in distance in world space to move up
  117. this.panUp = function ( distance ) {
  118. var te = this.object.matrix.elements;
  119. // get Y column of matrix
  120. panOffset.set( te[ 4 ], te[ 5 ], te[ 6 ] );
  121. panOffset.multiplyScalar( distance );
  122. pan.add( panOffset );
  123. };
  124. // pass in x,y of change desired in pixel space,
  125. // right and down are positive
  126. this.pan = function ( deltaX, deltaY ) {
  127. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  128. if ( scope.object instanceof THREE.PerspectiveCamera ) {
  129. // perspective
  130. var position = scope.object.position;
  131. var offset = position.clone().sub( scope.target );
  132. var targetDistance = offset.length();
  133. // half of the fov is center to top of screen
  134. targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
  135. // we actually don't use screenWidth, since perspective camera is fixed to screen height
  136. scope.panLeft( 2 * deltaX * targetDistance / element.clientHeight );
  137. scope.panUp( 2 * deltaY * targetDistance / element.clientHeight );
  138. } else if ( scope.object instanceof THREE.OrthographicCamera ) {
  139. // orthographic
  140. scope.panLeft( deltaX * (scope.object.right - scope.object.left) / element.clientWidth );
  141. scope.panUp( deltaY * (scope.object.top - scope.object.bottom) / element.clientHeight );
  142. } else {
  143. // camera neither orthographic or perspective
  144. THREE.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
  145. }
  146. };
  147. this.dollyIn = function ( dollyScale ) {
  148. if ( dollyScale === undefined ) {
  149. dollyScale = getZoomScale();
  150. }
  151. if ( scope.object instanceof THREE.PerspectiveCamera ) {
  152. scale /= dollyScale;
  153. } else if ( scope.object instanceof THREE.OrthographicCamera ) {
  154. scope.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom * dollyScale ) );
  155. scope.object.updateProjectionMatrix();
  156. scope.dispatchEvent( changeEvent );
  157. } else {
  158. THREE.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  159. }
  160. };
  161. this.dollyOut = function ( dollyScale ) {
  162. if ( dollyScale === undefined ) {
  163. dollyScale = getZoomScale();
  164. }
  165. if ( scope.object instanceof THREE.PerspectiveCamera ) {
  166. scale *= dollyScale;
  167. } else if ( scope.object instanceof THREE.OrthographicCamera ) {
  168. scope.object.zoom = Math.max( this.minZoom, Math.min( this.maxZoom, this.object.zoom / dollyScale ) );
  169. scope.object.updateProjectionMatrix();
  170. scope.dispatchEvent( changeEvent );
  171. } else {
  172. THREE.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  173. }
  174. };
  175. this.update = function () {
  176. var position = this.object.position;
  177. offset.copy( position ).sub( this.target );
  178. // rotate offset to "y-axis-is-up" space
  179. offset.applyQuaternion( quat );
  180. // angle from z-axis around y-axis
  181. theta = Math.atan2( offset.x, offset.z );
  182. // angle from y-axis
  183. phi = Math.atan2( Math.sqrt( offset.x * offset.x + offset.z * offset.z ), offset.y );
  184. if ( this.autoRotate && state === STATE.NONE ) {
  185. this.rotateLeft( getAutoRotationAngle() );
  186. }
  187. theta += thetaDelta;
  188. phi += phiDelta;
  189. // restrict theta to be between desired limits
  190. theta = Math.max( this.minAzimuthAngle, Math.min( this.maxAzimuthAngle, theta ) );
  191. // restrict phi to be between desired limits
  192. phi = Math.max( this.minPolarAngle, Math.min( this.maxPolarAngle, phi ) );
  193. // restrict phi to be betwee EPS and PI-EPS
  194. phi = Math.max( EPS, Math.min( Math.PI - EPS, phi ) );
  195. var radius = offset.length() * scale;
  196. // restrict radius to be between desired limits
  197. radius = Math.max( this.minDistance, Math.min( this.maxDistance, radius ) );
  198. // move target to panned location
  199. this.target.add( pan );
  200. offset.x = radius * Math.sin( phi ) * Math.sin( theta );
  201. offset.y = radius * Math.cos( phi );
  202. offset.z = radius * Math.sin( phi ) * Math.cos( theta );
  203. // rotate offset back to "camera-up-vector-is-up" space
  204. offset.applyQuaternion( quatInverse );
  205. position.copy( this.target ).add( offset );
  206. this.object.lookAt( this.target );
  207. thetaDelta = 0;
  208. phiDelta = 0;
  209. scale = 1;
  210. pan.set( 0, 0, 0 );
  211. // update condition is:
  212. // min(camera displacement, camera rotation in radians)^2 > EPS
  213. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  214. if ( lastPosition.distanceToSquared( this.object.position ) > EPS
  215. || 8 * (1 - lastQuaternion.dot(this.object.quaternion)) > EPS ) {
  216. this.dispatchEvent( changeEvent );
  217. lastPosition.copy( this.object.position );
  218. lastQuaternion.copy (this.object.quaternion );
  219. }
  220. };
  221. this.reset = function () {
  222. state = STATE.NONE;
  223. this.target.copy( this.target0 );
  224. this.object.position.copy( this.position0 );
  225. this.object.zoom = this.zoom0;
  226. this.object.updateProjectionMatrix();
  227. this.dispatchEvent( changeEvent );
  228. this.update();
  229. };
  230. this.getPolarAngle = function () {
  231. return phi;
  232. };
  233. this.getAzimuthalAngle = function () {
  234. return theta
  235. };
  236. function getAutoRotationAngle() {
  237. return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
  238. }
  239. function getZoomScale() {
  240. return Math.pow( 0.95, scope.zoomSpeed );
  241. }
  242. function onMouseDown( event ) {
  243. if ( scope.enabled === false ) return;
  244. event.preventDefault();
  245. if ( event.button === scope.mouseButtons.ORBIT ) {
  246. if ( scope.noRotate === true ) return;
  247. state = STATE.ROTATE;
  248. rotateStart.set( event.clientX, event.clientY );
  249. } else if ( event.button === scope.mouseButtons.ZOOM ) {
  250. if ( scope.noZoom === true ) return;
  251. state = STATE.DOLLY;
  252. dollyStart.set( event.clientX, event.clientY );
  253. } else if ( event.button === scope.mouseButtons.PAN ) {
  254. if ( scope.noPan === true ) return;
  255. state = STATE.PAN;
  256. panStart.set( event.clientX, event.clientY );
  257. }
  258. if ( state !== STATE.NONE ) {
  259. document.addEventListener( 'mousemove', onMouseMove, false );
  260. document.addEventListener( 'mouseup', onMouseUp, false );
  261. scope.dispatchEvent( startEvent );
  262. }
  263. }
  264. function onMouseMove( event ) {
  265. if ( scope.enabled === false ) return;
  266. event.preventDefault();
  267. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  268. if ( state === STATE.ROTATE ) {
  269. if ( scope.noRotate === true ) return;
  270. rotateEnd.set( event.clientX, event.clientY );
  271. rotateDelta.subVectors( rotateEnd, rotateStart );
  272. // rotating across whole screen goes 360 degrees around
  273. scope.rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth * scope.rotateSpeed );
  274. // rotating up and down along whole screen attempts to go 360, but limited to 180
  275. scope.rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight * scope.rotateSpeed );
  276. rotateStart.copy( rotateEnd );
  277. } else if ( state === STATE.DOLLY ) {
  278. if ( scope.noZoom === true ) return;
  279. dollyEnd.set( event.clientX, event.clientY );
  280. dollyDelta.subVectors( dollyEnd, dollyStart );
  281. if ( dollyDelta.y > 0 ) {
  282. scope.dollyIn();
  283. } else if ( dollyDelta.y < 0 ) {
  284. scope.dollyOut();
  285. }
  286. dollyStart.copy( dollyEnd );
  287. } else if ( state === STATE.PAN ) {
  288. if ( scope.noPan === true ) return;
  289. panEnd.set( event.clientX, event.clientY );
  290. panDelta.subVectors( panEnd, panStart );
  291. scope.pan( panDelta.x, panDelta.y );
  292. panStart.copy( panEnd );
  293. }
  294. if ( state !== STATE.NONE ) scope.update();
  295. }
  296. function onMouseUp( /* event */ ) {
  297. if ( scope.enabled === false ) return;
  298. document.removeEventListener( 'mousemove', onMouseMove, false );
  299. document.removeEventListener( 'mouseup', onMouseUp, false );
  300. scope.dispatchEvent( endEvent );
  301. state = STATE.NONE;
  302. }
  303. function onMouseWheel( event ) {
  304. if ( scope.enabled === false || scope.noZoom === true || state !== STATE.NONE ) return;
  305. event.preventDefault();
  306. event.stopPropagation();
  307. var delta = 0;
  308. if ( event.wheelDelta !== undefined ) { // WebKit / Opera / Explorer 9
  309. delta = event.wheelDelta;
  310. } else if ( event.detail !== undefined ) { // Firefox
  311. delta = - event.detail;
  312. }
  313. if ( delta > 0 ) {
  314. scope.dollyOut();
  315. } else if ( delta < 0 ) {
  316. scope.dollyIn();
  317. }
  318. scope.update();
  319. scope.dispatchEvent( startEvent );
  320. scope.dispatchEvent( endEvent );
  321. }
  322. function onKeyDown( event ) {
  323. if ( scope.enabled === false || scope.noKeys === true || scope.noPan === true ) return;
  324. switch ( event.keyCode ) {
  325. case scope.keys.UP:
  326. scope.pan( 0, scope.keyPanSpeed );
  327. scope.update();
  328. break;
  329. case scope.keys.BOTTOM:
  330. scope.pan( 0, - scope.keyPanSpeed );
  331. scope.update();
  332. break;
  333. case scope.keys.LEFT:
  334. scope.pan( scope.keyPanSpeed, 0 );
  335. scope.update();
  336. break;
  337. case scope.keys.RIGHT:
  338. scope.pan( - scope.keyPanSpeed, 0 );
  339. scope.update();
  340. break;
  341. }
  342. }
  343. function touchstart( event ) {
  344. if ( scope.enabled === false ) return;
  345. switch ( event.touches.length ) {
  346. case 1: // one-fingered touch: rotate
  347. if ( scope.noRotate === true ) return;
  348. state = STATE.TOUCH_ROTATE;
  349. rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  350. break;
  351. case 2: // two-fingered touch: dolly
  352. if ( scope.noZoom === true ) return;
  353. state = STATE.TOUCH_DOLLY;
  354. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  355. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  356. var distance = Math.sqrt( dx * dx + dy * dy );
  357. dollyStart.set( 0, distance );
  358. break;
  359. case 3: // three-fingered touch: pan
  360. if ( scope.noPan === true ) return;
  361. state = STATE.TOUCH_PAN;
  362. panStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  363. break;
  364. default:
  365. state = STATE.NONE;
  366. }
  367. if ( state !== STATE.NONE ) scope.dispatchEvent( startEvent );
  368. }
  369. function touchmove( event ) {
  370. if ( scope.enabled === false ) return;
  371. event.preventDefault();
  372. event.stopPropagation();
  373. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  374. switch ( event.touches.length ) {
  375. case 1: // one-fingered touch: rotate
  376. if ( scope.noRotate === true ) return;
  377. if ( state !== STATE.TOUCH_ROTATE ) return;
  378. rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  379. rotateDelta.subVectors( rotateEnd, rotateStart );
  380. // rotating across whole screen goes 360 degrees around
  381. scope.rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth * scope.rotateSpeed );
  382. // rotating up and down along whole screen attempts to go 360, but limited to 180
  383. scope.rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight * scope.rotateSpeed );
  384. rotateStart.copy( rotateEnd );
  385. scope.update();
  386. break;
  387. case 2: // two-fingered touch: dolly
  388. if ( scope.noZoom === true ) return;
  389. if ( state !== STATE.TOUCH_DOLLY ) return;
  390. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  391. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  392. var distance = Math.sqrt( dx * dx + dy * dy );
  393. dollyEnd.set( 0, distance );
  394. dollyDelta.subVectors( dollyEnd, dollyStart );
  395. if ( dollyDelta.y > 0 ) {
  396. scope.dollyOut();
  397. } else if ( dollyDelta.y < 0 ) {
  398. scope.dollyIn();
  399. }
  400. dollyStart.copy( dollyEnd );
  401. scope.update();
  402. break;
  403. case 3: // three-fingered touch: pan
  404. if ( scope.noPan === true ) return;
  405. if ( state !== STATE.TOUCH_PAN ) return;
  406. panEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  407. panDelta.subVectors( panEnd, panStart );
  408. scope.pan( panDelta.x, panDelta.y );
  409. panStart.copy( panEnd );
  410. scope.update();
  411. break;
  412. default:
  413. state = STATE.NONE;
  414. }
  415. }
  416. function touchend( /* event */ ) {
  417. if ( scope.enabled === false ) return;
  418. scope.dispatchEvent( endEvent );
  419. state = STATE.NONE;
  420. }
  421. this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  422. this.domElement.addEventListener( 'mousedown', onMouseDown, false );
  423. this.domElement.addEventListener( 'mousewheel', onMouseWheel, false );
  424. this.domElement.addEventListener( 'DOMMouseScroll', onMouseWheel, false ); // firefox
  425. this.domElement.addEventListener( 'touchstart', touchstart, false );
  426. this.domElement.addEventListener( 'touchend', touchend, false );
  427. this.domElement.addEventListener( 'touchmove', touchmove, false );
  428. window.addEventListener( 'keydown', onKeyDown, false );
  429. // force an update at start
  430. this.update();
  431. };
  432. THREE.OrbitControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  433. THREE.OrbitControls.prototype.constructor = THREE.OrbitControls;