TrackballControls.js 7.7 KB

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