TrackballControls.js 7.2 KB

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