OrbitControls.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. */
  7. THREE.OrbitControls = function ( object, domElement ) {
  8. THREE.EventDispatcher.call( this );
  9. this.object = object;
  10. this.domElement = ( domElement !== undefined ) ? domElement : document;
  11. // API
  12. this.center = new THREE.Vector3();
  13. this.userZoom = true;
  14. this.userZoomSpeed = 1.0;
  15. this.userRotate = true;
  16. this.userRotateSpeed = 1.0;
  17. this.userPan = true;
  18. this.userPanSpeed = 2.0;
  19. this.autoRotate = false;
  20. this.autoRotateSpeed = 2.0; // 30 seconds per round when fps is 60
  21. this.minPolarAngle = 0; // radians
  22. this.maxPolarAngle = Math.PI; // radians
  23. this.minDistance = 0;
  24. this.maxDistance = Infinity;
  25. this.keys = { LEFT: 37, UP: 38, RIGHT: 39, BOTTOM: 40 };
  26. // internals
  27. var scope = this;
  28. var EPS = 0.000001;
  29. var PIXELS_PER_ROUND = 1800;
  30. var rotateStart = new THREE.Vector2();
  31. var rotateEnd = new THREE.Vector2();
  32. var rotateDelta = new THREE.Vector2();
  33. var zoomStart = new THREE.Vector2();
  34. var zoomEnd = new THREE.Vector2();
  35. var zoomDelta = new THREE.Vector2();
  36. var phiDelta = 0;
  37. var thetaDelta = 0;
  38. var scale = 1;
  39. var lastPosition = new THREE.Vector3();
  40. var STATE = { NONE: -1, ROTATE: 0, ZOOM: 1, PAN: 2 };
  41. var state = STATE.NONE;
  42. // events
  43. var changeEvent = { type: 'change' };
  44. this.rotateLeft = function ( angle ) {
  45. if ( angle === undefined ) {
  46. angle = getAutoRotationAngle();
  47. }
  48. thetaDelta -= angle;
  49. };
  50. this.rotateRight = function ( angle ) {
  51. if ( angle === undefined ) {
  52. angle = getAutoRotationAngle();
  53. }
  54. thetaDelta += angle;
  55. };
  56. this.rotateUp = function ( angle ) {
  57. if ( angle === undefined ) {
  58. angle = getAutoRotationAngle();
  59. }
  60. phiDelta -= angle;
  61. };
  62. this.rotateDown = function ( angle ) {
  63. if ( angle === undefined ) {
  64. angle = getAutoRotationAngle();
  65. }
  66. phiDelta += angle;
  67. };
  68. this.zoomIn = function ( zoomScale ) {
  69. if ( zoomScale === undefined ) {
  70. zoomScale = getZoomScale();
  71. }
  72. scale /= zoomScale;
  73. };
  74. this.zoomOut = function ( zoomScale ) {
  75. if ( zoomScale === undefined ) {
  76. zoomScale = getZoomScale();
  77. }
  78. scale *= zoomScale;
  79. };
  80. this.pan = function ( distance ) {
  81. this.object.matrix.rotateAxis( distance );
  82. this.object.position.add( distance );
  83. this.center.add( distance );
  84. };
  85. this.update = function () {
  86. var position = this.object.position;
  87. var offset = position.clone().sub( this.center );
  88. // angle from z-axis around y-axis
  89. var theta = Math.atan2( offset.x, offset.z );
  90. // angle from y-axis
  91. var phi = Math.atan2( Math.sqrt( offset.x * offset.x + offset.z * offset.z ), offset.y );
  92. if ( this.autoRotate ) {
  93. this.rotateLeft( getAutoRotationAngle() );
  94. }
  95. theta += thetaDelta;
  96. phi += phiDelta;
  97. // restrict phi to be between desired limits
  98. phi = Math.max( this.minPolarAngle, Math.min( this.maxPolarAngle, phi ) );
  99. // restrict phi to be betwee EPS and PI-EPS
  100. phi = Math.max( EPS, Math.min( Math.PI - EPS, phi ) );
  101. var radius = offset.length() * scale;
  102. // restrict radius to be between desired limits
  103. radius = Math.max( this.minDistance, Math.min( this.maxDistance, radius ) );
  104. offset.x = radius * Math.sin( phi ) * Math.sin( theta );
  105. offset.y = radius * Math.cos( phi );
  106. offset.z = radius * Math.sin( phi ) * Math.cos( theta );
  107. position.copy( this.center ).add( offset );
  108. this.object.lookAt( this.center );
  109. thetaDelta = 0;
  110. phiDelta = 0;
  111. scale = 1;
  112. if ( lastPosition.distanceTo( this.object.position ) > 0 ) {
  113. this.dispatchEvent( changeEvent );
  114. lastPosition.copy( this.object.position );
  115. }
  116. };
  117. function getAutoRotationAngle() {
  118. return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
  119. }
  120. function getZoomScale() {
  121. return Math.pow( 0.95, scope.userZoomSpeed );
  122. }
  123. function onMouseDown( event ) {
  124. if ( !scope.userRotate ) return;
  125. event.preventDefault();
  126. if ( event.button === 0 ) {
  127. state = STATE.ROTATE;
  128. rotateStart.set( event.clientX, event.clientY );
  129. } else if ( event.button === 1 ) {
  130. state = STATE.ZOOM;
  131. zoomStart.set( event.clientX, event.clientY );
  132. } else if ( event.button === 2 ) {
  133. state = STATE.PAN;
  134. }
  135. document.addEventListener( 'mousemove', onMouseMove, false );
  136. document.addEventListener( 'mouseup', onMouseUp, false );
  137. }
  138. function onMouseMove( event ) {
  139. event.preventDefault();
  140. if ( state === STATE.ROTATE ) {
  141. rotateEnd.set( event.clientX, event.clientY );
  142. rotateDelta.subVectors( rotateEnd, rotateStart );
  143. scope.rotateLeft( 2 * Math.PI * rotateDelta.x / PIXELS_PER_ROUND * scope.userRotateSpeed );
  144. scope.rotateUp( 2 * Math.PI * rotateDelta.y / PIXELS_PER_ROUND * scope.userRotateSpeed );
  145. rotateStart.copy( rotateEnd );
  146. } else if ( state === STATE.ZOOM ) {
  147. zoomEnd.set( event.clientX, event.clientY );
  148. zoomDelta.subVectors( zoomEnd, zoomStart );
  149. if ( zoomDelta.y > 0 ) {
  150. scope.zoomIn();
  151. } else {
  152. scope.zoomOut();
  153. }
  154. zoomStart.copy( zoomEnd );
  155. } else if ( state === STATE.PAN ) {
  156. var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
  157. var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
  158. scope.pan( new THREE.Vector3( - movementX * scope.userPanSpeed, movementY * scope.userPanSpeed, 0 ) );
  159. }
  160. }
  161. function onMouseUp( event ) {
  162. if ( ! scope.userRotate ) return;
  163. document.removeEventListener( 'mousemove', onMouseMove, false );
  164. document.removeEventListener( 'mouseup', onMouseUp, false );
  165. state = STATE.NONE;
  166. }
  167. function onMouseWheel( event ) {
  168. if ( ! scope.userZoom ) return;
  169. var delta = 0;
  170. if ( event.wheelDelta ) { // WebKit / Opera / Explorer 9
  171. delta = event.wheelDelta;
  172. } else if ( event.detail ) { // Firefox
  173. delta = - event.detail;
  174. }
  175. if ( delta > 0 ) {
  176. scope.zoomOut();
  177. } else {
  178. scope.zoomIn();
  179. }
  180. }
  181. function onKeyDown( event ) {
  182. if ( ! scope.userPan ) return;
  183. switch ( event.keyCode ) {
  184. case scope.keys.UP:
  185. scope.pan( new THREE.Vector3( 0, scope.userPanSpeed, 0 ) );
  186. break;
  187. case scope.keys.BOTTOM:
  188. scope.pan( new THREE.Vector3( 0, - scope.userPanSpeed, 0 ) );
  189. break;
  190. case scope.keys.LEFT:
  191. scope.pan( new THREE.Vector3( - scope.userPanSpeed, 0, 0 ) );
  192. break;
  193. case scope.keys.RIGHT:
  194. scope.pan( new THREE.Vector3( scope.userPanSpeed, 0, 0 ) );
  195. break;
  196. }
  197. }
  198. this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  199. this.domElement.addEventListener( 'mousedown', onMouseDown, false );
  200. this.domElement.addEventListener( 'mousewheel', onMouseWheel, false );
  201. this.domElement.addEventListener( 'DOMMouseScroll', onMouseWheel, false ); // firefox
  202. this.domElement.addEventListener( 'keydown', onKeyDown, false );
  203. };