2
0

OculusHandModel.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { Object3D, Sphere, Box3 } from "../../../build/three.module.js";
  2. import { fetchProfile } from '../libs/motion-controllers.module.js';
  3. import { XRHandMeshModel } from "./XRHandMeshModel.js";
  4. const DEFAULT_PROFILES_PATH = 'https://cdn.jsdelivr.net/npm/@webxr-input-profiles/[email protected]/dist/profiles';
  5. const DEFAULT_PROFILE = 'generic-hand';
  6. class OculusHandModel extends Object3D {
  7. constructor(controller) {
  8. super();
  9. this.controller = controller;
  10. this.motionController = null;
  11. this.envMap = null;
  12. this.mesh = null;
  13. controller.addEventListener("connected", (event) => {
  14. const xrInputSource = event.data;
  15. if (xrInputSource.hand && !this.motionController) {
  16. this.visible = true;
  17. this.xrInputSource = xrInputSource;
  18. fetchProfile(xrInputSource, DEFAULT_PROFILES_PATH, DEFAULT_PROFILE).then(({ profile, assetPath }) => {
  19. this.motionController = new XRHandMeshModel(
  20. this,
  21. controller,
  22. assetPath
  23. );
  24. }).catch((err) => {
  25. console.warn(err);
  26. });
  27. }
  28. });
  29. controller.addEventListener("disconnected", () => {
  30. this.clear();
  31. this.motionController = null;
  32. })
  33. }
  34. updateMatrixWorld(force) {
  35. super.updateMatrixWorld(force);
  36. if (this.motionController) {
  37. this.motionController.updateMesh();
  38. }
  39. }
  40. getPointerPosition() {
  41. let indexFingerTip = this.controller.joints[POINTING_JOINT];
  42. if (indexFingerTip) {
  43. return indexFingerTip.position;
  44. } else {
  45. return null;
  46. }
  47. }
  48. intersectBoxObject(boxObject) {
  49. let pointerPosition = this.getPointerPosition();
  50. if (pointerPosition) {
  51. let indexSphere = new Sphere(pointerPosition, TOUCH_RADIUS);
  52. let box = new Box3().setFromObject(boxObject);
  53. return indexSphere.intersectsBox(box);
  54. } else {
  55. return false;
  56. }
  57. }
  58. checkButton(button) {
  59. if (this.intersectBoxObject(button)) {
  60. button.onPress();
  61. } else {
  62. button.onClear();
  63. }
  64. if (button.isPressed()) {
  65. button.whilePressed();
  66. }
  67. }
  68. }
  69. export { OculusHandModel };