OrbitControls.js 15 KB

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