OrbitControls.js 14 KB

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