TrackballControls.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /**
  2. * @author Eberhard Graether / http://egraether.com/
  3. */
  4. THREE.TrackballControls = function ( object, domElement ) {
  5. THREE.EventTarget.call( this );
  6. var _this = this,
  7. STATE = { NONE : -1, ROTATE : 0, ZOOM : 1, PAN : 2 };
  8. this.object = object;
  9. this.domElement = ( domElement !== undefined ) ? domElement : document;
  10. // API
  11. this.enabled = true;
  12. this.screen = { width: window.innerWidth, height: window.innerHeight, offsetLeft: 0, offsetTop: 0 };
  13. this.radius = ( this.screen.width + this.screen.height ) / 4;
  14. this.rotateSpeed = 1.0;
  15. this.zoomSpeed = 1.2;
  16. this.panSpeed = 0.3;
  17. this.noRotate = false;
  18. this.noZoom = false;
  19. this.noPan = false;
  20. this.staticMoving = false;
  21. this.dynamicDampingFactor = 0.2;
  22. this.minDistance = 0;
  23. this.maxDistance = Infinity;
  24. this.keys = [ 65 /*A*/, 83 /*S*/, 68 /*D*/ ];
  25. // internals
  26. this.target = new THREE.Vector3();
  27. var lastPosition = new THREE.Vector3();
  28. var _keyPressed = false,
  29. _state = STATE.NONE,
  30. _eye = new THREE.Vector3(),
  31. _rotateStart = new THREE.Vector3(),
  32. _rotateEnd = new THREE.Vector3(),
  33. _zoomStart = new THREE.Vector2(),
  34. _zoomEnd = new THREE.Vector2(),
  35. _panStart = new THREE.Vector2(),
  36. _panEnd = new THREE.Vector2();
  37. // methods
  38. this.handleEvent = function ( event ) {
  39. if ( typeof this[ event.type ] == 'function' ) {
  40. this[ event.type ]( event );
  41. }
  42. };
  43. this.getMouseOnScreen = function( clientX, clientY ) {
  44. return new THREE.Vector2(
  45. ( clientX - _this.screen.offsetLeft ) / _this.radius * 0.5,
  46. ( clientY - _this.screen.offsetTop ) / _this.radius * 0.5
  47. );
  48. };
  49. this.getMouseProjectionOnBall = function( clientX, clientY ) {
  50. var mouseOnBall = new THREE.Vector3(
  51. ( clientX - _this.screen.width * 0.5 - _this.screen.offsetLeft ) / _this.radius,
  52. ( _this.screen.height * 0.5 + _this.screen.offsetTop - clientY ) / _this.radius,
  53. 0.0
  54. );
  55. var length = mouseOnBall.length();
  56. if ( length > 1.0 ) {
  57. mouseOnBall.normalize();
  58. } else {
  59. mouseOnBall.z = Math.sqrt( 1.0 - length * length );
  60. }
  61. _eye.copy( _this.object.position ).subSelf( _this.target );
  62. var projection = _this.object.up.clone().setLength( mouseOnBall.y );
  63. projection.addSelf( _this.object.up.clone().crossSelf( _eye ).setLength( mouseOnBall.x ) );
  64. projection.addSelf( _eye.setLength( mouseOnBall.z ) );
  65. return projection;
  66. };
  67. this.rotateCamera = function() {
  68. var angle = Math.acos( _rotateStart.dot( _rotateEnd ) / _rotateStart.length() / _rotateEnd.length() );
  69. if ( angle ) {
  70. var axis = ( new THREE.Vector3() ).cross( _rotateStart, _rotateEnd ).normalize(),
  71. quaternion = new THREE.Quaternion();
  72. angle *= _this.rotateSpeed;
  73. quaternion.setFromAxisAngle( axis, -angle );
  74. quaternion.multiplyVector3( _eye );
  75. quaternion.multiplyVector3( _this.object.up );
  76. quaternion.multiplyVector3( _rotateEnd );
  77. if ( _this.staticMoving ) {
  78. _rotateStart = _rotateEnd;
  79. } else {
  80. quaternion.setFromAxisAngle( axis, angle * ( _this.dynamicDampingFactor - 1.0 ) );
  81. quaternion.multiplyVector3( _rotateStart );
  82. }
  83. }
  84. };
  85. this.zoomCamera = function() {
  86. var factor = 1.0 + ( _zoomEnd.y - _zoomStart.y ) * _this.zoomSpeed;
  87. if ( factor !== 1.0 && factor > 0.0 ) {
  88. _eye.multiplyScalar( factor );
  89. if ( _this.staticMoving ) {
  90. _zoomStart = _zoomEnd;
  91. } else {
  92. _zoomStart.y += ( _zoomEnd.y - _zoomStart.y ) * this.dynamicDampingFactor;
  93. }
  94. }
  95. };
  96. this.panCamera = function() {
  97. var mouseChange = _panEnd.clone().subSelf( _panStart );
  98. if ( mouseChange.lengthSq() ) {
  99. mouseChange.multiplyScalar( _eye.length() * _this.panSpeed );
  100. var pan = _eye.clone().crossSelf( _this.object.up ).setLength( mouseChange.x );
  101. pan.addSelf( _this.object.up.clone().setLength( mouseChange.y ) );
  102. _this.object.position.addSelf( pan );
  103. _this.target.addSelf( pan );
  104. if ( _this.staticMoving ) {
  105. _panStart = _panEnd;
  106. } else {
  107. _panStart.addSelf( mouseChange.sub( _panEnd, _panStart ).multiplyScalar( _this.dynamicDampingFactor ) );
  108. }
  109. }
  110. };
  111. this.checkDistances = function() {
  112. if ( !_this.noZoom || !_this.noPan ) {
  113. if ( _this.object.position.lengthSq() > _this.maxDistance * _this.maxDistance ) {
  114. _this.object.position.setLength( _this.maxDistance );
  115. }
  116. if ( _eye.lengthSq() < _this.minDistance * _this.minDistance ) {
  117. _this.object.position.add( _this.target, _eye.setLength( _this.minDistance ) );
  118. }
  119. }
  120. };
  121. this.update = function() {
  122. _eye.copy( _this.object.position ).subSelf( _this.target );
  123. if ( !_this.noRotate ) {
  124. _this.rotateCamera();
  125. }
  126. if ( !_this.noZoom ) {
  127. _this.zoomCamera();
  128. }
  129. if ( !_this.noPan ) {
  130. _this.panCamera();
  131. }
  132. _this.object.position.add( _this.target, _eye );
  133. _this.checkDistances();
  134. _this.object.lookAt( _this.target );
  135. if ( lastPosition.distanceTo( _this.object.position ) > 0 ) {
  136. _this.dispatchEvent( { type: 'change' } );
  137. lastPosition.copy( _this.object.position );
  138. }
  139. };
  140. // listeners
  141. function keydown( event ) {
  142. if ( ! _this.enabled ) return;
  143. if ( _state !== STATE.NONE ) {
  144. return;
  145. } else if ( event.keyCode === _this.keys[ STATE.ROTATE ] && !_this.noRotate ) {
  146. _state = STATE.ROTATE;
  147. } else if ( event.keyCode === _this.keys[ STATE.ZOOM ] && !_this.noZoom ) {
  148. _state = STATE.ZOOM;
  149. } else if ( event.keyCode === _this.keys[ STATE.PAN ] && !_this.noPan ) {
  150. _state = STATE.PAN;
  151. }
  152. if ( _state !== STATE.NONE ) {
  153. _keyPressed = true;
  154. }
  155. };
  156. function keyup( event ) {
  157. if ( ! _this.enabled ) return;
  158. if ( _state !== STATE.NONE ) {
  159. _state = STATE.NONE;
  160. }
  161. };
  162. function mousedown( event ) {
  163. if ( ! _this.enabled ) return;
  164. event.preventDefault();
  165. event.stopPropagation();
  166. if ( _state === STATE.NONE ) {
  167. _state = event.button;
  168. if ( _state === STATE.ROTATE && !_this.noRotate ) {
  169. _rotateStart = _rotateEnd = _this.getMouseProjectionOnBall( event.clientX, event.clientY );
  170. } else if ( _state === STATE.ZOOM && !_this.noZoom ) {
  171. _zoomStart = _zoomEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
  172. } else if ( !this.noPan ) {
  173. _panStart = _panEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
  174. }
  175. }
  176. };
  177. function mousemove( event ) {
  178. if ( ! _this.enabled ) return;
  179. if ( _keyPressed ) {
  180. _rotateStart = _rotateEnd = _this.getMouseProjectionOnBall( event.clientX, event.clientY );
  181. _zoomStart = _zoomEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
  182. _panStart = _panEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
  183. _keyPressed = false;
  184. }
  185. if ( _state === STATE.NONE ) {
  186. return;
  187. } else if ( _state === STATE.ROTATE && !_this.noRotate ) {
  188. _rotateEnd = _this.getMouseProjectionOnBall( event.clientX, event.clientY );
  189. } else if ( _state === STATE.ZOOM && !_this.noZoom ) {
  190. _zoomEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
  191. } else if ( _state === STATE.PAN && !_this.noPan ) {
  192. _panEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
  193. }
  194. };
  195. function mouseup( event ) {
  196. if ( ! _this.enabled ) return;
  197. event.preventDefault();
  198. event.stopPropagation();
  199. _state = STATE.NONE;
  200. };
  201. this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  202. this.domElement.addEventListener( 'mousemove', mousemove, false );
  203. this.domElement.addEventListener( 'mousedown', mousedown, false );
  204. this.domElement.addEventListener( 'mouseup', mouseup, false );
  205. window.addEventListener( 'keydown', keydown, false );
  206. window.addEventListener( 'keyup', keyup, false );
  207. };