EditorControls.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**
  2. * @author qiao / https://github.com/qiao
  3. * @author mrdoob / http://mrdoob.com
  4. * @author alteredq / http://alteredqualia.com/
  5. * @author WestLangley / http://github.com/WestLangley
  6. */
  7. THREE.EditorControls = function ( object, domElement ) {
  8. domElement = ( domElement !== undefined ) ? domElement : document;
  9. // API
  10. this.enabled = true;
  11. this.center = new THREE.Vector3();
  12. this.panSpeed = 0.001;
  13. this.zoomSpeed = 0.001;
  14. this.rotationSpeed = 0.005;
  15. // internals
  16. var scope = this;
  17. var vector = new THREE.Vector3();
  18. var STATE = { NONE: - 1, ROTATE: 0, ZOOM: 1, PAN: 2 };
  19. var state = STATE.NONE;
  20. var center = this.center;
  21. var normalMatrix = new THREE.Matrix3();
  22. var pointer = new THREE.Vector2();
  23. var pointerOld = new THREE.Vector2();
  24. var spherical = new THREE.Spherical();
  25. // events
  26. var changeEvent = { type: 'change' };
  27. this.focus = function ( target ) {
  28. var box = new THREE.Box3().setFromObject( target );
  29. object.lookAt( center.copy( box.getCenter() ) );
  30. scope.dispatchEvent( changeEvent );
  31. };
  32. this.pan = function ( delta ) {
  33. var distance = object.position.distanceTo( center );
  34. delta.multiplyScalar( distance * scope.panSpeed );
  35. delta.applyMatrix3( normalMatrix.getNormalMatrix( object.matrix ) );
  36. object.position.add( delta );
  37. center.add( delta );
  38. scope.dispatchEvent( changeEvent );
  39. };
  40. this.zoom = function ( delta ) {
  41. var distance = object.position.distanceTo( center );
  42. delta.multiplyScalar( distance * scope.zoomSpeed );
  43. if ( delta.length() > distance ) return;
  44. delta.applyMatrix3( normalMatrix.getNormalMatrix( object.matrix ) );
  45. object.position.add( delta );
  46. scope.dispatchEvent( changeEvent );
  47. };
  48. this.rotate = function ( delta ) {
  49. vector.copy( object.position ).sub( center );
  50. spherical.setFromVector3( vector );
  51. spherical.theta += delta.x;
  52. spherical.phi += delta.y;
  53. spherical.makeSafe();
  54. vector.setFromSpherical( spherical );
  55. object.position.copy( center ).add( vector );
  56. object.lookAt( center );
  57. scope.dispatchEvent( changeEvent );
  58. };
  59. // mouse
  60. function onMouseDown( event ) {
  61. if ( scope.enabled === false ) return;
  62. if ( event.button === 0 ) {
  63. state = STATE.ROTATE;
  64. } else if ( event.button === 1 ) {
  65. state = STATE.ZOOM;
  66. } else if ( event.button === 2 ) {
  67. state = STATE.PAN;
  68. }
  69. pointerOld.set( event.clientX, event.clientY );
  70. domElement.addEventListener( 'mousemove', onMouseMove, false );
  71. domElement.addEventListener( 'mouseup', onMouseUp, false );
  72. domElement.addEventListener( 'mouseout', onMouseUp, false );
  73. domElement.addEventListener( 'dblclick', onMouseUp, false );
  74. }
  75. function onMouseMove( event ) {
  76. if ( scope.enabled === false ) return;
  77. pointer.set( event.clientX, event.clientY );
  78. var movementX = pointer.x - pointerOld.x;
  79. var movementY = pointer.y - pointerOld.y;
  80. if ( state === STATE.ROTATE ) {
  81. scope.rotate( new THREE.Vector3( - movementX * scope.rotationSpeed, - movementY * scope.rotationSpeed, 0 ) );
  82. } else if ( state === STATE.ZOOM ) {
  83. scope.zoom( new THREE.Vector3( 0, 0, movementY ) );
  84. } else if ( state === STATE.PAN ) {
  85. scope.pan( new THREE.Vector3( - movementX, movementY, 0 ) );
  86. }
  87. pointerOld.set( event.clientX, event.clientY );
  88. }
  89. function onMouseUp( event ) {
  90. domElement.removeEventListener( 'mousemove', onMouseMove, false );
  91. domElement.removeEventListener( 'mouseup', onMouseUp, false );
  92. domElement.removeEventListener( 'mouseout', onMouseUp, false );
  93. domElement.removeEventListener( 'dblclick', onMouseUp, false );
  94. state = STATE.NONE;
  95. }
  96. function onMouseWheel( event ) {
  97. event.preventDefault();
  98. // if ( scope.enabled === false ) return;
  99. scope.zoom( new THREE.Vector3( 0, 0, event.deltaY ) );
  100. }
  101. function contextmenu( event ) {
  102. event.preventDefault();
  103. }
  104. this.dispose = function() {
  105. domElement.removeEventListener( 'contextmenu', contextmenu, false );
  106. domElement.removeEventListener( 'mousedown', onMouseDown, false );
  107. domElement.removeEventListener( 'wheel', onMouseWheel, false );
  108. domElement.removeEventListener( 'mousemove', onMouseMove, false );
  109. domElement.removeEventListener( 'mouseup', onMouseUp, false );
  110. domElement.removeEventListener( 'mouseout', onMouseUp, false );
  111. domElement.removeEventListener( 'dblclick', onMouseUp, false );
  112. domElement.removeEventListener( 'touchstart', touchStart, false );
  113. domElement.removeEventListener( 'touchmove', touchMove, false );
  114. };
  115. domElement.addEventListener( 'contextmenu', contextmenu, false );
  116. domElement.addEventListener( 'mousedown', onMouseDown, false );
  117. domElement.addEventListener( 'wheel', onMouseWheel, false );
  118. // touch
  119. var touch = new THREE.Vector3();
  120. var touches = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  121. var prevTouches = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  122. var prevDistance = null;
  123. function touchStart( event ) {
  124. if ( scope.enabled === false ) return;
  125. switch ( event.touches.length ) {
  126. case 1:
  127. touches[ 0 ].set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY, 0 );
  128. touches[ 1 ].set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY, 0 );
  129. break;
  130. case 2:
  131. touches[ 0 ].set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY, 0 );
  132. touches[ 1 ].set( event.touches[ 1 ].pageX, event.touches[ 1 ].pageY, 0 );
  133. prevDistance = touches[ 0 ].distanceTo( touches[ 1 ] );
  134. break;
  135. }
  136. prevTouches[ 0 ].copy( touches[ 0 ] );
  137. prevTouches[ 1 ].copy( touches[ 1 ] );
  138. }
  139. function touchMove( event ) {
  140. if ( scope.enabled === false ) return;
  141. event.preventDefault();
  142. event.stopPropagation();
  143. function getClosest( touch, touches ) {
  144. var closest = touches[ 0 ];
  145. for ( var i in touches ) {
  146. if ( closest.distanceTo( touch ) > touches[ i ].distanceTo( touch ) ) closest = touches[ i ];
  147. }
  148. return closest;
  149. }
  150. switch ( event.touches.length ) {
  151. case 1:
  152. touches[ 0 ].set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY, 0 );
  153. touches[ 1 ].set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY, 0 );
  154. scope.rotate( touches[ 0 ].sub( getClosest( touches[ 0 ], prevTouches ) ).multiplyScalar( - scope.rotationSpeed ) );
  155. break;
  156. case 2:
  157. touches[ 0 ].set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY, 0 );
  158. touches[ 1 ].set( event.touches[ 1 ].pageX, event.touches[ 1 ].pageY, 0 );
  159. distance = touches[ 0 ].distanceTo( touches[ 1 ] );
  160. scope.zoom( new THREE.Vector3( 0, 0, prevDistance - distance ) );
  161. prevDistance = distance;
  162. var offset0 = touches[ 0 ].clone().sub( getClosest( touches[ 0 ], prevTouches ) );
  163. var offset1 = touches[ 1 ].clone().sub( getClosest( touches[ 1 ], prevTouches ) );
  164. offset0.x = - offset0.x;
  165. offset1.x = - offset1.x;
  166. scope.pan( offset0.add( offset1 ).multiplyScalar( 0.5 ) );
  167. break;
  168. }
  169. prevTouches[ 0 ].copy( touches[ 0 ] );
  170. prevTouches[ 1 ].copy( touches[ 1 ] );
  171. }
  172. domElement.addEventListener( 'touchstart', touchStart, false );
  173. domElement.addEventListener( 'touchmove', touchMove, false );
  174. };
  175. THREE.EditorControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  176. THREE.EditorControls.prototype.constructor = THREE.EditorControls;