OrbitControls.js 15 KB

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