WebXRController.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import { Group } from '../../objects/Group.js';
  2. /**
  3. * @author Mugen87 / https://github.com/Mugen87
  4. */
  5. function WebXRController() {
  6. this._targetRay = null;
  7. this._grip = null;
  8. this._hand = null;
  9. }
  10. Object.assign( WebXRController.prototype, {
  11. constructor: WebXRController,
  12. getHandSpace: function () {
  13. if ( this._hand === null ) {
  14. this._hand = new Group();
  15. this._hand.matrixAutoUpdate = false;
  16. this._hand.visible = false;
  17. this._hand.joints = [];
  18. this._hand.inputState = { pinching: false };
  19. if ( window.XRHand ) {
  20. for ( let i = 0; i <= XRHand.LITTLE_PHALANX_TIP; i ++ ) {
  21. // The transform of this joint will be updated with the joint pose on each frame
  22. let joint = new Group();
  23. joint.matrixAutoUpdate = false;
  24. joint.visible = false;
  25. this._hand.joints.push( joint );
  26. // ??
  27. this._hand.add( joint );
  28. }
  29. }
  30. }
  31. return this._hand;
  32. },
  33. getTargetRaySpace: function () {
  34. if ( this._targetRay === null ) {
  35. this._targetRay = new Group();
  36. this._targetRay.matrixAutoUpdate = false;
  37. this._targetRay.visible = false;
  38. }
  39. return this._targetRay;
  40. },
  41. getGripSpace: function () {
  42. if ( this._grip === null ) {
  43. this._grip = new Group();
  44. this._grip.matrixAutoUpdate = false;
  45. this._grip.visible = false;
  46. }
  47. return this._grip;
  48. },
  49. dispatchEvent: function ( event ) {
  50. if ( this._targetRay !== null ) {
  51. this._targetRay.dispatchEvent( event );
  52. }
  53. if ( this._grip !== null ) {
  54. this._grip.dispatchEvent( event );
  55. }
  56. if ( this._hand !== null ) {
  57. this._hand.dispatchEvent( event );
  58. }
  59. return this;
  60. },
  61. disconnect: function ( inputSource ) {
  62. this.dispatchEvent( { type: 'disconnected', data: inputSource } );
  63. if ( this._targetRay !== null ) {
  64. this._targetRay.visible = false;
  65. }
  66. if ( this._grip !== null ) {
  67. this._grip.visible = false;
  68. }
  69. if ( this._hand !== null ) {
  70. this._hand.visible = false;
  71. }
  72. return this;
  73. },
  74. update: function ( inputSource, frame, referenceSpace ) {
  75. let inputPose = null;
  76. let gripPose = null;
  77. let handPose = null;
  78. const targetRay = this._targetRay;
  79. const grip = this._grip;
  80. const hand = this._hand;
  81. if ( inputSource ) {
  82. if ( inputSource.hand ) {
  83. handPose = true;
  84. for ( let i = 0; i <= XRHand.LITTLE_PHALANX_TIP; i ++ ) {
  85. if ( inputSource.hand[ i ] ) {
  86. // Update the joints groups with the XRJoint poses
  87. let jointPose = frame.getJointPose( inputSource.hand[ i ], referenceSpace );
  88. const joint = hand.joints[ i ];
  89. if ( jointPose !== null ) {
  90. joint.matrix.fromArray( jointPose.transform.matrix );
  91. joint.matrix.decompose( joint.position, joint.rotation, joint.scale );
  92. joint.jointRadius = jointPose.radius;
  93. }
  94. joint.visible = jointPose !== null;
  95. // Custom events
  96. // Check pinch
  97. const indexTip = hand.joints[ XRHand.INDEX_PHALANX_TIP ];
  98. const thumbTip = hand.joints[ XRHand.THUMB_PHALANX_TIP ];
  99. const distance = indexTip.position.distanceTo( thumbTip.position );
  100. const distanceToPinch = 0.02;
  101. if ( hand.inputState.pinching && distance > distanceToPinch ) {
  102. hand.inputState.pinching = false;
  103. this.dispatchEvent( {
  104. type: "pinchend",
  105. target: this
  106. } );
  107. } else if ( ! hand.inputState.pinching && distance <= distanceToPinch ) {
  108. hand.inputState.pinching = true;
  109. this.dispatchEvent( {
  110. type: "pinchstart",
  111. target: this
  112. } );
  113. }
  114. }
  115. }
  116. } else {
  117. if ( targetRay !== null ) {
  118. inputPose = frame.getPose( inputSource.targetRaySpace, referenceSpace );
  119. if ( inputPose !== null ) {
  120. targetRay.matrix.fromArray( inputPose.transform.matrix );
  121. targetRay.matrix.decompose( targetRay.position, targetRay.rotation, targetRay.scale );
  122. }
  123. }
  124. if ( grip !== null && inputSource.gripSpace ) {
  125. gripPose = frame.getPose( inputSource.gripSpace, referenceSpace );
  126. if ( gripPose !== null ) {
  127. grip.matrix.fromArray( gripPose.transform.matrix );
  128. grip.matrix.decompose( grip.position, grip.rotation, grip.scale );
  129. }
  130. }
  131. }
  132. }
  133. if ( targetRay !== null ) {
  134. targetRay.visible = ( inputPose !== null );
  135. }
  136. if ( grip !== null ) {
  137. grip.visible = ( gripPose !== null );
  138. }
  139. if ( hand !== null ) {
  140. hand.visible = ( handPose !== null );
  141. }
  142. return this;
  143. }
  144. } );
  145. export { WebXRController };