WebXRController.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. }
  18. return this._hand;
  19. },
  20. getTargetRaySpace: function () {
  21. if ( this._targetRay === null ) {
  22. this._targetRay = new Group();
  23. this._targetRay.matrixAutoUpdate = false;
  24. this._targetRay.visible = false;
  25. }
  26. return this._targetRay;
  27. },
  28. getGripSpace: function () {
  29. if ( this._grip === null ) {
  30. this._grip = new Group();
  31. this._grip.matrixAutoUpdate = false;
  32. this._grip.visible = false;
  33. }
  34. return this._grip;
  35. },
  36. dispatchEvent: function ( event ) {
  37. if ( this._targetRay !== null ) {
  38. this._targetRay.dispatchEvent( event );
  39. }
  40. if ( this._grip !== null ) {
  41. this._grip.dispatchEvent( event );
  42. }
  43. if ( this._hand !== null ) {
  44. this._hand.dispatchEvent( event );
  45. }
  46. return this;
  47. },
  48. disconnect: function ( inputSource ) {
  49. this.dispatchEvent( { type: 'disconnected', data: inputSource } );
  50. if ( this._targetRay !== null ) {
  51. this._targetRay.visible = false;
  52. }
  53. if ( this._grip !== null ) {
  54. this._grip.visible = false;
  55. }
  56. if ( this._hand !== null ) {
  57. this._hand.visible = false;
  58. }
  59. return this;
  60. },
  61. update: function ( inputSource, frame, referenceSpace ) {
  62. let inputPose = null;
  63. let gripPose = null;
  64. let handPose = null;
  65. const targetRay = this._targetRay;
  66. const grip = this._grip;
  67. const hand = this._hand;
  68. if ( inputSource ) {
  69. if ( inputSource.hand ) {
  70. } else {
  71. if ( targetRay !== null ) {
  72. inputPose = frame.getPose( inputSource.targetRaySpace, referenceSpace );
  73. if ( inputPose !== null ) {
  74. targetRay.matrix.fromArray( inputPose.transform.matrix );
  75. targetRay.matrix.decompose( targetRay.position, targetRay.rotation, targetRay.scale );
  76. }
  77. }
  78. if ( grip !== null && inputSource.gripSpace ) {
  79. gripPose = frame.getPose( inputSource.gripSpace, referenceSpace );
  80. if ( gripPose !== null ) {
  81. grip.matrix.fromArray( gripPose.transform.matrix );
  82. grip.matrix.decompose( grip.position, grip.rotation, grip.scale );
  83. }
  84. }
  85. }
  86. }
  87. if ( targetRay !== null ) {
  88. targetRay.visible = ( inputPose !== null );
  89. }
  90. if ( grip !== null ) {
  91. grip.visible = ( gripPose !== null );
  92. }
  93. if ( hand !== null ) {
  94. hand.visible = ( handPose !== null );
  95. }
  96. return this;
  97. }
  98. } );
  99. export { WebXRController };