OrbitControls.js 17 KB

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