OrthographicTrackballControls.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /**
  2. * @author Eberhard Graether / http://egraether.com/
  3. * @author Patrick Fuller / http://patrick-fuller.com
  4. */
  5. THREE.OrthographicTrackballControls = function ( object, domElement ) {
  6. var _this = this;
  7. var STATE = { NONE: -1, ROTATE: 0, ZOOM: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_ZOOM: 4, TOUCH_PAN: 5 };
  8. this.object = object;
  9. this.domElement = ( domElement !== undefined ) ? domElement : document;
  10. // API
  11. this.enabled = true;
  12. this.screen = { width: 0, height: 0, 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 _state = STATE.NONE,
  29. _prevState = 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. _zoomFactor = 1,
  36. _touchZoomDistanceStart = 0,
  37. _touchZoomDistanceEnd = 0,
  38. _panStart = new THREE.Vector2(),
  39. _panEnd = new THREE.Vector2();
  40. // for reset
  41. this.target0 = this.target.clone();
  42. this.position0 = this.object.position.clone();
  43. this.up0 = this.object.up.clone();
  44. this.left0 = this.object.left;
  45. this.right0 = this.object.right;
  46. this.top0 = this.object.top;
  47. this.bottom0 = this.object.bottom;
  48. this.center0 = new THREE.Vector2((this.left0 + this.right0) / 2.0, (this.top0 + this.bottom0) / 2.0);
  49. // events
  50. var changeEvent = { type: 'change' };
  51. // methods
  52. this.handleResize = function () {
  53. this.screen.width = window.innerWidth;
  54. this.screen.height = window.innerHeight;
  55. this.screen.offsetLeft = 0;
  56. this.screen.offsetTop = 0;
  57. this.radius = ( this.screen.width + this.screen.height ) / 4;
  58. };
  59. this.handleEvent = function ( event ) {
  60. if ( typeof this[ event.type ] == 'function' ) {
  61. this[ event.type ]( event );
  62. }
  63. };
  64. this.getMouseOnScreen = function ( clientX, clientY ) {
  65. return new THREE.Vector2(
  66. ( clientX - _this.screen.offsetLeft ) / _this.radius * 0.5,
  67. ( clientY - _this.screen.offsetTop ) / _this.radius * 0.5
  68. );
  69. };
  70. this.getMouseProjectionOnBall = function ( clientX, clientY ) {
  71. var mouseOnBall = new THREE.Vector3(
  72. ( clientX - _this.screen.width * 0.5 - _this.screen.offsetLeft ) / _this.radius,
  73. ( _this.screen.height * 0.5 + _this.screen.offsetTop - clientY ) / _this.radius,
  74. 0.0
  75. );
  76. var length = mouseOnBall.length();
  77. if ( length > 1.0 ) {
  78. mouseOnBall.normalize();
  79. } else {
  80. mouseOnBall.z = Math.sqrt( 1.0 - length * length );
  81. }
  82. _eye.copy( _this.object.position ).sub( _this.target );
  83. var projection = _this.object.up.clone().setLength( mouseOnBall.y );
  84. projection.add( _this.object.up.clone().cross( _eye ).setLength( mouseOnBall.x ) );
  85. projection.add( _eye.setLength( mouseOnBall.z ) );
  86. return projection;
  87. };
  88. this.rotateCamera = function () {
  89. var angle = Math.acos( _rotateStart.dot( _rotateEnd ) / _rotateStart.length() / _rotateEnd.length() );
  90. if ( angle ) {
  91. var axis = ( new THREE.Vector3() ).crossVectors( _rotateStart, _rotateEnd ).normalize(),
  92. quaternion = new THREE.Quaternion();
  93. angle *= _this.rotateSpeed;
  94. quaternion.setFromAxisAngle( axis, -angle );
  95. _eye.applyQuaternion( quaternion );
  96. _this.object.up.applyQuaternion( quaternion );
  97. _rotateEnd.applyQuaternion( quaternion );
  98. if ( _this.staticMoving ) {
  99. _rotateStart.copy( _rotateEnd );
  100. } else {
  101. quaternion.setFromAxisAngle( axis, angle * ( _this.dynamicDampingFactor - 1.0 ) );
  102. _rotateStart.applyQuaternion( quaternion );
  103. }
  104. }
  105. };
  106. this.zoomCamera = function () {
  107. if ( _state === STATE.TOUCH_ZOOM ) {
  108. var factor = _touchZoomDistanceStart / _touchZoomDistanceEnd;
  109. _touchZoomDistanceStart = _touchZoomDistanceEnd;
  110. _zoomFactor *= factor;
  111. _this.object.left = _zoomFactor * _this.left0 + ( 1 - _zoomFactor ) * _this.center0.x;
  112. _this.object.right = _zoomFactor * _this.right0 + ( 1 - _zoomFactor ) * _this.center0.x;
  113. _this.object.top = _zoomFactor * _this.top0 + ( 1 - _zoomFactor ) * _this.center0.y;
  114. _this.object.bottom = _zoomFactor * _this.bottom0 + ( 1 - _zoomFactor ) * _this.center0.y;
  115. } else {
  116. var factor = 1.0 + ( _zoomEnd.y - _zoomStart.y ) * _this.zoomSpeed;
  117. if ( factor !== 1.0 && factor > 0.0 ) {
  118. _zoomFactor *= factor;
  119. _this.object.left = _zoomFactor * _this.left0 + ( 1 - _zoomFactor ) * _this.center0.x;
  120. _this.object.right = _zoomFactor * _this.right0 + ( 1 - _zoomFactor ) * _this.center0.x;
  121. _this.object.top = _zoomFactor * _this.top0 + ( 1 - _zoomFactor ) * _this.center0.y;
  122. _this.object.bottom = _zoomFactor * _this.bottom0 + ( 1 - _zoomFactor ) * _this.center0.y;
  123. if ( _this.staticMoving ) {
  124. _zoomStart.copy( _zoomEnd );
  125. } else {
  126. _zoomStart.y += ( _zoomEnd.y - _zoomStart.y ) * this.dynamicDampingFactor;
  127. }
  128. }
  129. }
  130. };
  131. this.panCamera = function () {
  132. var mouseChange = _panEnd.clone().sub( _panStart );
  133. if ( mouseChange.lengthSq() ) {
  134. mouseChange.multiplyScalar( _eye.length() * _this.panSpeed );
  135. var pan = _eye.clone().cross( _this.object.up ).setLength( mouseChange.x );
  136. pan.add( _this.object.up.clone().setLength( mouseChange.y ) );
  137. _this.object.position.add( pan );
  138. _this.target.add( pan );
  139. if ( _this.staticMoving ) {
  140. _panStart = _panEnd;
  141. } else {
  142. _panStart.add( mouseChange.subVectors( _panEnd, _panStart ).multiplyScalar( _this.dynamicDampingFactor ) );
  143. }
  144. }
  145. };
  146. this.checkDistances = function () {
  147. if ( !_this.noZoom || !_this.noPan ) {
  148. if ( _this.object.position.lengthSq() > _this.maxDistance * _this.maxDistance ) {
  149. _this.object.position.setLength( _this.maxDistance );
  150. }
  151. if ( _eye.lengthSq() < _this.minDistance * _this.minDistance ) {
  152. _this.object.position.addVectors( _this.target, _eye.setLength( _this.minDistance ) );
  153. }
  154. }
  155. };
  156. this.update = function () {
  157. _eye.subVectors( _this.object.position, _this.target );
  158. if ( !_this.noRotate ) {
  159. _this.rotateCamera();
  160. }
  161. if ( !_this.noZoom ) {
  162. _this.zoomCamera();
  163. _this.object.updateProjectionMatrix();
  164. }
  165. if ( !_this.noPan ) {
  166. _this.panCamera();
  167. }
  168. _this.object.position.addVectors( _this.target, _eye );
  169. _this.checkDistances();
  170. _this.object.lookAt( _this.target );
  171. if ( lastPosition.distanceToSquared( _this.object.position ) > 0 ) {
  172. _this.dispatchEvent( changeEvent );
  173. lastPosition.copy( _this.object.position );
  174. }
  175. };
  176. this.reset = function () {
  177. _state = STATE.NONE;
  178. _prevState = STATE.NONE;
  179. _this.target.copy( _this.target0 );
  180. _this.object.position.copy( _this.position0 );
  181. _this.object.up.copy( _this.up0 );
  182. _eye.subVectors( _this.object.position, _this.target );
  183. _this.object.left = _this.left0;
  184. _this.object.right = _this.right0;
  185. _this.object.top = _this.top0;
  186. _this.object.bottom = _this.bottom0;
  187. _this.object.lookAt( _this.target );
  188. _this.dispatchEvent( changeEvent );
  189. lastPosition.copy( _this.object.position );
  190. };
  191. // listeners
  192. function keydown( event ) {
  193. if ( _this.enabled === false ) return;
  194. window.removeEventListener( 'keydown', keydown );
  195. _prevState = _state;
  196. if ( _state !== STATE.NONE ) {
  197. return;
  198. } else if ( event.keyCode === _this.keys[ STATE.ROTATE ] && !_this.noRotate ) {
  199. _state = STATE.ROTATE;
  200. } else if ( event.keyCode === _this.keys[ STATE.ZOOM ] && !_this.noZoom ) {
  201. _state = STATE.ZOOM;
  202. } else if ( event.keyCode === _this.keys[ STATE.PAN ] && !_this.noPan ) {
  203. _state = STATE.PAN;
  204. }
  205. }
  206. function keyup( event ) {
  207. if ( _this.enabled === false ) return;
  208. _state = _prevState;
  209. window.addEventListener( 'keydown', keydown, false );
  210. }
  211. function mousedown( event ) {
  212. if ( _this.enabled === false ) return;
  213. event.preventDefault();
  214. event.stopPropagation();
  215. if ( _state === STATE.NONE ) {
  216. _state = event.button;
  217. }
  218. if ( _state === STATE.ROTATE && !_this.noRotate ) {
  219. _rotateStart = _rotateEnd = _this.getMouseProjectionOnBall( event.clientX, event.clientY );
  220. } else if ( _state === STATE.ZOOM && !_this.noZoom ) {
  221. _zoomStart = _zoomEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
  222. } else if ( _state === STATE.PAN && !_this.noPan ) {
  223. _panStart = _panEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
  224. }
  225. document.addEventListener( 'mousemove', mousemove, false );
  226. document.addEventListener( 'mouseup', mouseup, false );
  227. }
  228. function mousemove( event ) {
  229. if ( _this.enabled === false ) return;
  230. event.preventDefault();
  231. event.stopPropagation();
  232. if ( _state === STATE.ROTATE && !_this.noRotate ) {
  233. _rotateEnd = _this.getMouseProjectionOnBall( event.clientX, event.clientY );
  234. } else if ( _state === STATE.ZOOM && !_this.noZoom ) {
  235. _zoomEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
  236. } else if ( _state === STATE.PAN && !_this.noPan ) {
  237. _panEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
  238. }
  239. }
  240. function mouseup( event ) {
  241. if ( _this.enabled === false ) return;
  242. event.preventDefault();
  243. event.stopPropagation();
  244. _state = STATE.NONE;
  245. document.removeEventListener( 'mousemove', mousemove );
  246. document.removeEventListener( 'mouseup', mouseup );
  247. }
  248. function mousewheel( event ) {
  249. if ( _this.enabled === false ) return;
  250. event.preventDefault();
  251. event.stopPropagation();
  252. var delta = 0;
  253. if ( event.wheelDelta ) { // WebKit / Opera / Explorer 9
  254. delta = event.wheelDelta / 40;
  255. } else if ( event.detail ) { // Firefox
  256. delta = - event.detail / 3;
  257. }
  258. _zoomStart.y += delta * 0.01;
  259. }
  260. function touchstart( event ) {
  261. if ( _this.enabled === false ) return;
  262. switch ( event.touches.length ) {
  263. case 1:
  264. _state = STATE.TOUCH_ROTATE;
  265. _rotateStart = _rotateEnd = _this.getMouseProjectionOnBall( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  266. break;
  267. case 2:
  268. _state = STATE.TOUCH_ZOOM;
  269. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  270. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  271. _touchZoomDistanceEnd = _touchZoomDistanceStart = Math.sqrt( dx * dx + dy * dy );
  272. break;
  273. case 3:
  274. _state = STATE.TOUCH_PAN;
  275. _panStart = _panEnd = _this.getMouseOnScreen( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  276. break;
  277. default:
  278. _state = STATE.NONE;
  279. }
  280. }
  281. function touchmove( event ) {
  282. if ( _this.enabled === false ) return;
  283. event.preventDefault();
  284. event.stopPropagation();
  285. switch ( event.touches.length ) {
  286. case 1:
  287. _rotateEnd = _this.getMouseProjectionOnBall( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  288. break;
  289. case 2:
  290. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  291. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  292. _touchZoomDistanceEnd = Math.sqrt( dx * dx + dy * dy )
  293. break;
  294. case 3:
  295. _panEnd = _this.getMouseOnScreen( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  296. break;
  297. default:
  298. _state = STATE.NONE;
  299. }
  300. }
  301. function touchend( event ) {
  302. if ( _this.enabled === false ) return;
  303. switch ( event.touches.length ) {
  304. case 1:
  305. _rotateStart = _rotateEnd = _this.getMouseProjectionOnBall( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  306. break;
  307. case 2:
  308. _touchZoomDistanceStart = _touchZoomDistanceEnd = 0;
  309. break;
  310. case 3:
  311. _panStart = _panEnd = _this.getMouseOnScreen( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  312. break;
  313. }
  314. _state = STATE.NONE;
  315. }
  316. this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  317. this.domElement.addEventListener( 'mousedown', mousedown, false );
  318. this.domElement.addEventListener( 'mousewheel', mousewheel, false );
  319. this.domElement.addEventListener( 'DOMMouseScroll', mousewheel, false ); // firefox
  320. this.domElement.addEventListener( 'touchstart', touchstart, false );
  321. this.domElement.addEventListener( 'touchend', touchend, false );
  322. this.domElement.addEventListener( 'touchmove', touchmove, false );
  323. window.addEventListener( 'keydown', keydown, false );
  324. window.addEventListener( 'keyup', keyup, false );
  325. this.handleResize();
  326. };
  327. THREE.OrthographicTrackballControls.prototype = Object.create( THREE.EventDispatcher.prototype );
粤ICP备19079148号