InteractiveGroup.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import {
  2. Group,
  3. Matrix4,
  4. Raycaster,
  5. Vector2
  6. } from 'three';
  7. const _pointer = new Vector2();
  8. const _event = { type: '', data: _pointer };
  9. class InteractiveGroup extends Group {
  10. constructor( renderer, camera ) {
  11. super();
  12. const scope = this;
  13. const raycaster = new Raycaster();
  14. const tempMatrix = new Matrix4();
  15. // Pointer Events
  16. const element = renderer.domElement;
  17. function onPointerEvent( event ) {
  18. event.stopPropagation();
  19. _pointer.x = ( event.clientX / element.clientWidth ) * 2 - 1;
  20. _pointer.y = - ( event.clientY / element.clientHeight ) * 2 + 1;
  21. raycaster.setFromCamera( _pointer, camera );
  22. const intersects = raycaster.intersectObjects( scope.children, false );
  23. if ( intersects.length > 0 ) {
  24. const intersection = intersects[ 0 ];
  25. const object = intersection.object;
  26. const uv = intersection.uv;
  27. _event.type = event.type;
  28. _event.data.set( uv.x, 1 - uv.y );
  29. object.dispatchEvent( _event );
  30. }
  31. }
  32. element.addEventListener( 'pointerdown', onPointerEvent );
  33. element.addEventListener( 'pointerup', onPointerEvent );
  34. element.addEventListener( 'pointermove', onPointerEvent );
  35. element.addEventListener( 'mousedown', onPointerEvent );
  36. element.addEventListener( 'mouseup', onPointerEvent );
  37. element.addEventListener( 'mousemove', onPointerEvent );
  38. element.addEventListener( 'click', onPointerEvent );
  39. // WebXR Controller Events
  40. // TODO: Dispatch pointerevents too
  41. const events = {
  42. 'move': 'mousemove',
  43. 'select': 'click',
  44. 'selectstart': 'mousedown',
  45. 'selectend': 'mouseup'
  46. };
  47. function onXRControllerEvent( event ) {
  48. const controller = event.target;
  49. tempMatrix.identity().extractRotation( controller.matrixWorld );
  50. raycaster.ray.origin.setFromMatrixPosition( controller.matrixWorld );
  51. raycaster.ray.direction.set( 0, 0, - 1 ).applyMatrix4( tempMatrix );
  52. const intersections = raycaster.intersectObjects( scope.children, false );
  53. if ( intersections.length > 0 ) {
  54. const intersection = intersections[ 0 ];
  55. const object = intersection.object;
  56. const uv = intersection.uv;
  57. _event.type = events[ event.type ];
  58. _event.data.set( uv.x, 1 - uv.y );
  59. object.dispatchEvent( _event );
  60. }
  61. }
  62. const controller1 = renderer.xr.getController( 0 );
  63. controller1.addEventListener( 'move', onXRControllerEvent );
  64. controller1.addEventListener( 'select', onXRControllerEvent );
  65. controller1.addEventListener( 'selectstart', onXRControllerEvent );
  66. controller1.addEventListener( 'selectend', onXRControllerEvent );
  67. const controller2 = renderer.xr.getController( 1 );
  68. controller2.addEventListener( 'move', onXRControllerEvent );
  69. controller2.addEventListener( 'select', onXRControllerEvent );
  70. controller2.addEventListener( 'selectstart', onXRControllerEvent );
  71. controller2.addEventListener( 'selectend', onXRControllerEvent );
  72. }
  73. }
  74. export { InteractiveGroup };