TrackballControlsExperimental.js 7.8 KB

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