1
0

DragControls.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. import {
  2. Controls,
  3. Matrix4,
  4. Plane,
  5. Raycaster,
  6. Vector2,
  7. Vector3,
  8. MOUSE,
  9. TOUCH
  10. } from 'three';
  11. const _plane = new Plane();
  12. const _pointer = new Vector2();
  13. const _offset = new Vector3();
  14. const _diff = new Vector2();
  15. const _previousPointer = new Vector2();
  16. const _intersection = new Vector3();
  17. const _worldPosition = new Vector3();
  18. const _inverseMatrix = new Matrix4();
  19. const _up = new Vector3();
  20. const _right = new Vector3();
  21. let _selected = null, _hovered = null;
  22. const _intersections = [];
  23. const STATE = {
  24. NONE: - 1,
  25. PAN: 0,
  26. ROTATE: 1
  27. };
  28. class DragControls extends Controls {
  29. constructor( objects, camera, domElement = null ) {
  30. super( camera, domElement );
  31. this.objects = objects;
  32. this.recursive = true;
  33. this.transformGroup = false;
  34. this.rotateSpeed = 1;
  35. this.raycaster = new Raycaster();
  36. // interaction
  37. this.mouseButtons = { LEFT: MOUSE.PAN, MIDDLE: MOUSE.PAN, RIGHT: MOUSE.ROTATE };
  38. this.touches = { ONE: TOUCH.PAN };
  39. // event listeners
  40. this._onPointerMove = onPointerMove.bind( this );
  41. this._onPointerDown = onPointerDown.bind( this );
  42. this._onPointerCancel = onPointerCancel.bind( this );
  43. this._onContextMenu = onContextMenu.bind( this );
  44. //
  45. if ( domElement !== null ) {
  46. this.connect();
  47. }
  48. }
  49. connect() {
  50. this.domElement.addEventListener( 'pointermove', this._onPointerMove );
  51. this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
  52. this.domElement.addEventListener( 'pointerup', this._onPointerCancel );
  53. this.domElement.addEventListener( 'pointerleave', this._onPointerCancel );
  54. this.domElement.addEventListener( 'contextmenu', this._onContextMenu );
  55. this.domElement.style.touchAction = 'none'; // disable touch scroll
  56. }
  57. disconnect() {
  58. this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  59. this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
  60. this.domElement.removeEventListener( 'pointerup', this._onPointerCancel );
  61. this.domElement.removeEventListener( 'pointerleave', this._onPointerCancel );
  62. this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
  63. this.domElement.style.touchAction = 'auto';
  64. this.domElement.style.cursor = '';
  65. }
  66. dispose() {
  67. this.disconnect();
  68. }
  69. _updatePointer( event ) {
  70. const rect = this.domElement.getBoundingClientRect();
  71. _pointer.x = ( event.clientX - rect.left ) / rect.width * 2 - 1;
  72. _pointer.y = - ( event.clientY - rect.top ) / rect.height * 2 + 1;
  73. }
  74. _updateState( event ) {
  75. // determine action
  76. let action;
  77. if ( event.pointerType === 'touch' ) {
  78. action = this.touches.ONE;
  79. } else {
  80. switch ( event.button ) {
  81. case 0:
  82. action = this.mouseButtons.LEFT;
  83. break;
  84. case 1:
  85. action = this.mouseButtons.MIDDLE;
  86. break;
  87. case 2:
  88. action = this.mouseButtons.RIGHT;
  89. break;
  90. default:
  91. action = null;
  92. }
  93. }
  94. // determine state
  95. switch ( action ) {
  96. case MOUSE.PAN:
  97. case TOUCH.PAN:
  98. this.state = STATE.PAN;
  99. break;
  100. case MOUSE.ROTATE:
  101. case TOUCH.ROTATE:
  102. this.state = STATE.ROTATE;
  103. break;
  104. default:
  105. this.state = STATE.NONE;
  106. }
  107. }
  108. getRaycaster() {
  109. console.warn( 'THREE.DragControls: getRaycaster() has been deprecated. Use controls.raycaster instead.' ); // @deprecated r169
  110. return this.raycaster;
  111. }
  112. setObjects( objects ) {
  113. console.warn( 'THREE.DragControls: setObjects() has been deprecated. Use controls.objects instead.' ); // @deprecated r169
  114. this.objects = objects;
  115. }
  116. getObjects() {
  117. console.warn( 'THREE.DragControls: getObjects() has been deprecated. Use controls.objects instead.' ); // @deprecated r169
  118. return this.objects;
  119. }
  120. activate() {
  121. console.warn( 'THREE.DragControls: activate() has been renamed to connect().' ); // @deprecated r169
  122. this.connect();
  123. }
  124. deactivate() {
  125. console.warn( 'THREE.DragControls: deactivate() has been renamed to disconnect().' ); // @deprecated r169
  126. this.disconnect();
  127. }
  128. set mode( value ) {
  129. console.warn( 'THREE.DragControls: The .mode property has been removed. Define the type of transformation via the .mouseButtons or .touches properties.' ); // @deprecated r169
  130. }
  131. get mode() {
  132. console.warn( 'THREE.DragControls: The .mode property has been removed. Define the type of transformation via the .mouseButtons or .touches properties.' ); // @deprecated r169
  133. }
  134. }
  135. function onPointerMove( event ) {
  136. const camera = this.object;
  137. const domElement = this.domElement;
  138. const raycaster = this.raycaster;
  139. if ( this.enabled === false ) return;
  140. this._updatePointer( event );
  141. raycaster.setFromCamera( _pointer, camera );
  142. if ( _selected ) {
  143. if ( this.state === STATE.PAN ) {
  144. if ( raycaster.ray.intersectPlane( _plane, _intersection ) ) {
  145. _selected.position.copy( _intersection.sub( _offset ).applyMatrix4( _inverseMatrix ) );
  146. }
  147. } else if ( this.state === STATE.ROTATE ) {
  148. _diff.subVectors( _pointer, _previousPointer ).multiplyScalar( this.rotateSpeed );
  149. _selected.rotateOnWorldAxis( _up, _diff.x );
  150. _selected.rotateOnWorldAxis( _right.normalize(), - _diff.y );
  151. }
  152. this.dispatchEvent( { type: 'drag', object: _selected } );
  153. _previousPointer.copy( _pointer );
  154. } else {
  155. // hover support
  156. if ( event.pointerType === 'mouse' || event.pointerType === 'pen' ) {
  157. _intersections.length = 0;
  158. raycaster.setFromCamera( _pointer, camera );
  159. raycaster.intersectObjects( this.objects, this.recursive, _intersections );
  160. if ( _intersections.length > 0 ) {
  161. const object = _intersections[ 0 ].object;
  162. _plane.setFromNormalAndCoplanarPoint( camera.getWorldDirection( _plane.normal ), _worldPosition.setFromMatrixPosition( object.matrixWorld ) );
  163. if ( _hovered !== object && _hovered !== null ) {
  164. this.dispatchEvent( { type: 'hoveroff', object: _hovered } );
  165. domElement.style.cursor = 'auto';
  166. _hovered = null;
  167. }
  168. if ( _hovered !== object ) {
  169. this.dispatchEvent( { type: 'hoveron', object: object } );
  170. domElement.style.cursor = 'pointer';
  171. _hovered = object;
  172. }
  173. } else {
  174. if ( _hovered !== null ) {
  175. this.dispatchEvent( { type: 'hoveroff', object: _hovered } );
  176. domElement.style.cursor = 'auto';
  177. _hovered = null;
  178. }
  179. }
  180. }
  181. }
  182. _previousPointer.copy( _pointer );
  183. }
  184. function onPointerDown( event ) {
  185. const camera = this.object;
  186. const domElement = this.domElement;
  187. const raycaster = this.raycaster;
  188. if ( this.enabled === false ) return;
  189. this._updatePointer( event );
  190. this._updateState( event );
  191. _intersections.length = 0;
  192. raycaster.setFromCamera( _pointer, camera );
  193. raycaster.intersectObjects( this.objects, this.recursive, _intersections );
  194. if ( _intersections.length > 0 ) {
  195. if ( this.transformGroup === true ) {
  196. // look for the outermost group in the object's upper hierarchy
  197. _selected = findGroup( _intersections[ 0 ].object );
  198. } else {
  199. _selected = _intersections[ 0 ].object;
  200. }
  201. _plane.setFromNormalAndCoplanarPoint( camera.getWorldDirection( _plane.normal ), _worldPosition.setFromMatrixPosition( _selected.matrixWorld ) );
  202. if ( raycaster.ray.intersectPlane( _plane, _intersection ) ) {
  203. if ( this.state === STATE.PAN ) {
  204. _inverseMatrix.copy( _selected.parent.matrixWorld ).invert();
  205. _offset.copy( _intersection ).sub( _worldPosition.setFromMatrixPosition( _selected.matrixWorld ) );
  206. } else if ( this.state === STATE.ROTATE ) {
  207. // the controls only support Y+ up
  208. _up.set( 0, 1, 0 ).applyQuaternion( camera.quaternion ).normalize();
  209. _right.set( 1, 0, 0 ).applyQuaternion( camera.quaternion ).normalize();
  210. }
  211. }
  212. domElement.style.cursor = 'move';
  213. this.dispatchEvent( { type: 'dragstart', object: _selected } );
  214. }
  215. _previousPointer.copy( _pointer );
  216. }
  217. function onPointerCancel() {
  218. if ( this.enabled === false ) return;
  219. if ( _selected ) {
  220. this.dispatchEvent( { type: 'dragend', object: _selected } );
  221. _selected = null;
  222. }
  223. this.domElement.style.cursor = _hovered ? 'pointer' : 'auto';
  224. this.state = STATE.NONE;
  225. }
  226. function onContextMenu( event ) {
  227. if ( this.enabled === false ) return;
  228. event.preventDefault();
  229. }
  230. function findGroup( obj, group = null ) {
  231. if ( obj.isGroup ) group = obj;
  232. if ( obj.parent === null ) return group;
  233. return findGroup( obj.parent, group );
  234. }
  235. export { DragControls };