OrbitControls.js 18 KB

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