OrbitControls.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 = 1.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 /* <- */, RIGHT : 39 /* -> */ };
  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 };
  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 ( dist ) {
  81. // make sure minPolarAngle > 0, maxPolarAngle < pi, minDistance > 0
  82. var delta = new THREE.Vector3();
  83. delta.set( this.object.position.z - this.center.z, 0, - this.object.position.x + this.center.x ).setLength( dist );
  84. this.object.position.add( delta );
  85. this.center.add( delta );
  86. };
  87. this.update = function () {
  88. var position = this.object.position;
  89. var offset = position.clone().sub( this.center );
  90. // angle from z-axis around y-axis
  91. var theta = Math.atan2( offset.x, offset.z );
  92. // angle from y-axis
  93. var phi = Math.atan2( Math.sqrt( offset.x * offset.x + offset.z * offset.z ), offset.y );
  94. if ( this.autoRotate ) {
  95. this.rotateLeft( getAutoRotationAngle() );
  96. }
  97. theta += thetaDelta;
  98. phi += phiDelta;
  99. // restrict phi to be between desired limits
  100. phi = Math.max( this.minPolarAngle, Math.min( this.maxPolarAngle, phi ) );
  101. // restrict phi to be betwee EPS and PI-EPS
  102. phi = Math.max( EPS, Math.min( Math.PI - EPS, phi ) );
  103. var radius = offset.length() * scale;
  104. // restrict radius to be between desired limits
  105. radius = Math.max( this.minDistance, Math.min( this.maxDistance, radius ) );
  106. offset.x = radius * Math.sin( phi ) * Math.sin( theta );
  107. offset.y = radius * Math.cos( phi );
  108. offset.z = radius * Math.sin( phi ) * Math.cos( theta );
  109. position.copy( this.center ).add( offset );
  110. this.object.lookAt( this.center );
  111. thetaDelta = 0;
  112. phiDelta = 0;
  113. scale = 1;
  114. if ( lastPosition.distanceTo( this.object.position ) > 0 ) {
  115. this.dispatchEvent( changeEvent );
  116. lastPosition.copy( this.object.position );
  117. }
  118. };
  119. function getAutoRotationAngle() {
  120. return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
  121. }
  122. function getZoomScale() {
  123. return Math.pow( 0.95, scope.userZoomSpeed );
  124. }
  125. function onMouseDown( event ) {
  126. if ( !scope.userRotate ) return;
  127. event.preventDefault();
  128. if ( event.button === 0 || event.button === 2 ) {
  129. state = STATE.ROTATE;
  130. rotateStart.set( event.clientX, event.clientY );
  131. } else if ( event.button === 1 ) {
  132. state = STATE.ZOOM;
  133. zoomStart.set( event.clientX, event.clientY );
  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. }
  156. }
  157. function onMouseUp( event ) {
  158. if ( ! scope.userRotate ) return;
  159. document.removeEventListener( 'mousemove', onMouseMove, false );
  160. document.removeEventListener( 'mouseup', onMouseUp, false );
  161. state = STATE.NONE;
  162. }
  163. function onMouseWheel( event ) {
  164. if ( ! scope.userZoom ) return;
  165. var delta = 0;
  166. if ( event.wheelDelta ) { // WebKit / Opera / Explorer 9
  167. delta = event.wheelDelta;
  168. } else if ( event.detail ) { // Firefox
  169. delta = - event.detail;
  170. }
  171. if ( delta > 0 ) {
  172. scope.zoomOut();
  173. } else {
  174. scope.zoomIn();
  175. }
  176. }
  177. function onKeyDown( event ) {
  178. if ( ! scope.userPan ) return;
  179. if ( event.keyCode === scope.keys.LEFT ) {
  180. scope.pan( - scope.userPanSpeed );
  181. } else if ( event.keyCode === scope.keys.RIGHT ) {
  182. scope.pan( scope.userPanSpeed );
  183. }
  184. }
  185. this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  186. this.domElement.addEventListener( 'mousedown', onMouseDown, false );
  187. this.domElement.addEventListener( 'mousewheel', onMouseWheel, false );
  188. this.domElement.addEventListener( 'DOMMouseScroll', onMouseWheel, false ); // firefox
  189. this.domElement.addEventListener( 'keydown', onKeyDown, false );
  190. };