OculusHandModel.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { Object3D, Sphere, Box3 } from 'three';
  2. import { XRHandMeshModel } from './XRHandMeshModel.js';
  3. const TOUCH_RADIUS = 0.01;
  4. const POINTING_JOINT = 'index-finger-tip';
  5. /**
  6. * Represents an Oculus hand model.
  7. *
  8. * @augments Object3D
  9. */
  10. class OculusHandModel extends Object3D {
  11. /**
  12. * Constructs a new Oculus hand model.
  13. *
  14. * @param {Group} controller - The hand controller.
  15. * @param {?Loader} [loader=null] - A loader that is used to load hand models.
  16. * @param {?Function} [onLoad=null] - A callback that is executed when a hand model has been loaded.
  17. */
  18. constructor( controller, loader = null, onLoad = null ) {
  19. super();
  20. /**
  21. * The hand controller.
  22. *
  23. * @type {Group}
  24. */
  25. this.controller = controller;
  26. /**
  27. * The motion controller.
  28. *
  29. * @type {?MotionController}
  30. * @default null
  31. */
  32. this.motionController = null;
  33. /**
  34. * The model's environment map.
  35. *
  36. * @type {?Texture}
  37. * @default null
  38. */
  39. this.envMap = null;
  40. /**
  41. * A loader that is used to load hand models.
  42. *
  43. * @type {?Loader}
  44. * @default null
  45. */
  46. this.loader = loader;
  47. /**
  48. * A callback that is executed when a hand model has been loaded.
  49. *
  50. * @type {?Function}
  51. * @default null
  52. */
  53. this.onLoad = onLoad;
  54. /**
  55. * The path to the model repository.
  56. *
  57. * @type {?string}
  58. * @default null
  59. */
  60. this.path = null;
  61. /**
  62. * The model mesh.
  63. *
  64. * @type {Mesh}
  65. * @default null
  66. */
  67. this.mesh = null;
  68. controller.addEventListener( 'connected', ( event ) => {
  69. const xrInputSource = event.data;
  70. if ( xrInputSource.hand && ! this.motionController ) {
  71. this.xrInputSource = xrInputSource;
  72. this.motionController = new XRHandMeshModel( this, controller, this.path, xrInputSource.handedness, this.loader, this.onLoad );
  73. }
  74. } );
  75. controller.addEventListener( 'disconnected', () => {
  76. this.clear();
  77. this.motionController = null;
  78. } );
  79. }
  80. /**
  81. * Overwritten with a custom implementation. Makes sure the motion controller updates the mesh.
  82. *
  83. * @param {boolean} [force=false] - When set to `true`, a recomputation of world matrices is forced even
  84. * when {@link Object3D#matrixWorldAutoUpdate} is set to `false`.
  85. */
  86. updateMatrixWorld( force ) {
  87. super.updateMatrixWorld( force );
  88. if ( this.motionController ) {
  89. this.motionController.updateMesh();
  90. }
  91. }
  92. /**
  93. * Returns the pointer position which is the position of the index finger tip.
  94. *
  95. * @return {Vector3|null} The pointer position. Returns `null` if not index finger tip joint was found.
  96. */
  97. getPointerPosition() {
  98. const indexFingerTip = this.controller.joints[ POINTING_JOINT ];
  99. if ( indexFingerTip ) {
  100. return indexFingerTip.position;
  101. } else {
  102. return null;
  103. }
  104. }
  105. /**
  106. * Returns `true` if the current pointer position (the index finger tip) intersections
  107. * with the given box object.
  108. *
  109. * @param {Mesh} boxObject - The box object.
  110. * @return {boolean} Whether an intersection was found or not.
  111. */
  112. intersectBoxObject( boxObject ) {
  113. const pointerPosition = this.getPointerPosition();
  114. if ( pointerPosition ) {
  115. const indexSphere = new Sphere( pointerPosition, TOUCH_RADIUS );
  116. const box = new Box3().setFromObject( boxObject );
  117. return indexSphere.intersectsBox( box );
  118. } else {
  119. return false;
  120. }
  121. }
  122. /**
  123. * Executed actions depending on the interaction state with
  124. * the given button.
  125. *
  126. * @param {Object} button - The button.
  127. */
  128. checkButton( button ) {
  129. if ( this.intersectBoxObject( button ) ) {
  130. button.onPress();
  131. } else {
  132. button.onClear();
  133. }
  134. if ( button.isPressed() ) {
  135. button.whilePressed();
  136. }
  137. }
  138. }
  139. export { OculusHandModel };
粤ICP备19079148号