OrbitControls.js 14 KB

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