OrbitControls.js 15 KB

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