OrbitControls.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. this.object = object;
  9. this.domElement = ( domElement !== undefined ) ? domElement : document;
  10. // API
  11. this.center = new THREE.Vector3();
  12. this.userZoom = true;
  13. this.userZoomSpeed = 1.0;
  14. this.userRotate = true;
  15. this.userRotateSpeed = 1.0;
  16. this.userPan = true;
  17. this.userPanSpeed = 2.0;
  18. this.autoRotate = false;
  19. this.autoRotateSpeed = 2.0; // 30 seconds per round when fps is 60
  20. this.minPolarAngle = 0; // radians
  21. this.maxPolarAngle = Math.PI; // radians
  22. this.minDistance = 0;
  23. this.maxDistance = Infinity;
  24. this.keys = { LEFT: 37, UP: 38, RIGHT: 39, BOTTOM: 40 };
  25. // internals
  26. var scope = this;
  27. var EPS = 0.000001;
  28. var PIXELS_PER_ROUND = 1800;
  29. var rotateStart = new THREE.Vector2();
  30. var rotateEnd = new THREE.Vector2();
  31. var rotateDelta = new THREE.Vector2();
  32. var zoomStart = new THREE.Vector2();
  33. var zoomEnd = new THREE.Vector2();
  34. var zoomDelta = new THREE.Vector2();
  35. var phiDelta = 0;
  36. var thetaDelta = 0;
  37. var scale = 1;
  38. var lastPosition = new THREE.Vector3();
  39. var STATE = { NONE: -1, ROTATE: 0, ZOOM: 1, PAN: 2 };
  40. var state = STATE.NONE;
  41. // events
  42. var changeEvent = { type: 'change' };
  43. this.rotateLeft = function ( angle ) {
  44. if ( angle === undefined ) {
  45. angle = getAutoRotationAngle();
  46. }
  47. thetaDelta -= angle;
  48. };
  49. this.rotateRight = function ( angle ) {
  50. if ( angle === undefined ) {
  51. angle = getAutoRotationAngle();
  52. }
  53. thetaDelta += angle;
  54. };
  55. this.rotateUp = function ( angle ) {
  56. if ( angle === undefined ) {
  57. angle = getAutoRotationAngle();
  58. }
  59. phiDelta -= angle;
  60. };
  61. this.rotateDown = function ( angle ) {
  62. if ( angle === undefined ) {
  63. angle = getAutoRotationAngle();
  64. }
  65. phiDelta += angle;
  66. };
  67. this.zoomIn = function ( zoomScale ) {
  68. if ( zoomScale === undefined ) {
  69. zoomScale = getZoomScale();
  70. }
  71. scale /= zoomScale;
  72. };
  73. this.zoomOut = function ( zoomScale ) {
  74. if ( zoomScale === undefined ) {
  75. zoomScale = getZoomScale();
  76. }
  77. scale *= zoomScale;
  78. };
  79. this.pan = function ( distance ) {
  80. distance.transformDirection( this.object.matrix );
  81. distance.multiplyScalar( scope.userPanSpeed );
  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, movementY, 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, 1, 0 ) );
  186. break;
  187. case scope.keys.BOTTOM:
  188. scope.pan( new THREE.Vector3( 0, - 1, 0 ) );
  189. break;
  190. case scope.keys.LEFT:
  191. scope.pan( new THREE.Vector3( - 1, 0, 0 ) );
  192. break;
  193. case scope.keys.RIGHT:
  194. scope.pan( new THREE.Vector3( 1, 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. };
  204. THREE.OrbitControls.prototype = Object.create( THREE.EventDispatcher.prototype );