InteractiveGroup.js 2.9 KB

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