WebXRController.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. }
  9. Object.assign( WebXRController.prototype, {
  10. constructor: WebXRController,
  11. getTargetRaySpace: function () {
  12. if ( this._targetRay === null ) {
  13. this._targetRay = new Group();
  14. this._targetRay.matrixAutoUpdate = false;
  15. this._targetRay.visible = false;
  16. }
  17. return this._targetRay;
  18. },
  19. getGripSpace: function () {
  20. if ( this._grip === null ) {
  21. this._grip = new Group();
  22. this._grip.matrixAutoUpdate = false;
  23. this._grip.visible = false;
  24. }
  25. return this._grip;
  26. },
  27. dispatchEvent: function ( event ) {
  28. if ( this._targetRay !== null ) {
  29. this._targetRay.dispatchEvent( event );
  30. }
  31. if ( this._grip !== null ) {
  32. this._grip.dispatchEvent( event );
  33. }
  34. return this;
  35. },
  36. disconnect: function ( inputSource ) {
  37. this.dispatchEvent( { type: 'disconnected', data: inputSource } );
  38. if ( this._targetRay !== null ) {
  39. this._targetRay.visible = false;
  40. }
  41. if ( this._grip !== null ) {
  42. this._grip.visible = false;
  43. }
  44. return this;
  45. },
  46. update: function ( inputSource, frame, referenceSpace ) {
  47. let inputPose = null;
  48. let gripPose = null;
  49. const targetRay = this._targetRay;
  50. const grip = this._grip;
  51. if ( inputSource ) {
  52. if ( targetRay !== null ) {
  53. inputPose = frame.getPose( inputSource.targetRaySpace, referenceSpace );
  54. if ( inputPose !== null ) {
  55. targetRay.matrix.fromArray( inputPose.transform.matrix );
  56. targetRay.matrix.decompose( targetRay.position, targetRay.rotation, targetRay.scale );
  57. }
  58. }
  59. if ( grip !== null && inputSource.gripSpace ) {
  60. gripPose = frame.getPose( inputSource.gripSpace, referenceSpace );
  61. if ( gripPose !== null ) {
  62. grip.matrix.fromArray( gripPose.transform.matrix );
  63. grip.matrix.decompose( grip.position, grip.rotation, grip.scale );
  64. }
  65. }
  66. }
  67. if ( targetRay !== null ) {
  68. targetRay.visible = ( inputPose !== null );
  69. }
  70. if ( grip !== null ) {
  71. grip.visible = ( gripPose !== null );
  72. }
  73. return this;
  74. }
  75. } );
  76. export { WebXRController };
粤ICP备19079148号