OrbitControls.js 15 KB

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