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