123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- import { Group } from '../../objects/Group.js';
- /**
- * @author Mugen87 / https://github.com/Mugen87
- */
- function WebXRController() {
- this._targetRay = null;
- this._grip = null;
- this._hand = null;
- }
- Object.assign( WebXRController.prototype, {
- constructor: WebXRController,
- getHandSpace: function () {
- if ( this._hand === null ) {
- this._hand = new Group();
- this._hand.matrixAutoUpdate = false;
- this._hand.visible = false;
- this._hand.joints = [];
- this._hand.inputState = { pinching: false };
- if ( window.XRHand ) {
- for ( let i = 0; i <= XRHand.LITTLE_PHALANX_TIP; i ++ ) {
- // The transform of this joint will be updated with the joint pose on each frame
- let joint = new Group();
- joint.matrixAutoUpdate = false;
- joint.visible = false;
- this._hand.joints.push( joint );
- // ??
- this._hand.add( joint );
- }
- }
- }
- return this._hand;
- },
- getTargetRaySpace: function () {
- if ( this._targetRay === null ) {
- this._targetRay = new Group();
- this._targetRay.matrixAutoUpdate = false;
- this._targetRay.visible = false;
- }
- return this._targetRay;
- },
- getGripSpace: function () {
- if ( this._grip === null ) {
- this._grip = new Group();
- this._grip.matrixAutoUpdate = false;
- this._grip.visible = false;
- }
- return this._grip;
- },
- dispatchEvent: function ( event ) {
- if ( this._targetRay !== null ) {
- this._targetRay.dispatchEvent( event );
- }
- if ( this._grip !== null ) {
- this._grip.dispatchEvent( event );
- }
- if ( this._hand !== null ) {
- this._hand.dispatchEvent( event );
- }
- return this;
- },
- disconnect: function ( inputSource ) {
- this.dispatchEvent( { type: 'disconnected', data: inputSource } );
- if ( this._targetRay !== null ) {
- this._targetRay.visible = false;
- }
- if ( this._grip !== null ) {
- this._grip.visible = false;
- }
- if ( this._hand !== null ) {
- this._hand.visible = false;
- }
- return this;
- },
- update: function ( inputSource, frame, referenceSpace ) {
- let inputPose = null;
- let gripPose = null;
- let handPose = null;
- const targetRay = this._targetRay;
- const grip = this._grip;
- const hand = this._hand;
- if ( inputSource ) {
- if ( inputSource.hand ) {
- handPose = true;
- for ( let i = 0; i <= XRHand.LITTLE_PHALANX_TIP; i ++ ) {
- if ( inputSource.hand[ i ] ) {
- // Update the joints groups with the XRJoint poses
- let jointPose = frame.getJointPose( inputSource.hand[ i ], referenceSpace );
- const joint = hand.joints[ i ];
- if ( jointPose !== null ) {
- joint.matrix.fromArray( jointPose.transform.matrix );
- joint.matrix.decompose( joint.position, joint.rotation, joint.scale );
- joint.jointRadius = jointPose.radius;
- }
- joint.visible = jointPose !== null;
- // Custom events
- // Check pinch
- const indexTip = hand.joints[ XRHand.INDEX_PHALANX_TIP ];
- const thumbTip = hand.joints[ XRHand.THUMB_PHALANX_TIP ];
- const distance = indexTip.position.distanceTo( thumbTip.position );
- const distanceToPinch = 0.02;
- if ( hand.inputState.pinching && distance > distanceToPinch ) {
- hand.inputState.pinching = false;
- this.dispatchEvent( {
- type: "pinchend",
- target: this
- } );
- } else if ( ! hand.inputState.pinching && distance <= distanceToPinch ) {
- hand.inputState.pinching = true;
- this.dispatchEvent( {
- type: "pinchstart",
- target: this
- } );
- }
- }
- }
- } else {
- if ( targetRay !== null ) {
- inputPose = frame.getPose( inputSource.targetRaySpace, referenceSpace );
- if ( inputPose !== null ) {
- targetRay.matrix.fromArray( inputPose.transform.matrix );
- targetRay.matrix.decompose( targetRay.position, targetRay.rotation, targetRay.scale );
- }
- }
- if ( grip !== null && inputSource.gripSpace ) {
- gripPose = frame.getPose( inputSource.gripSpace, referenceSpace );
- if ( gripPose !== null ) {
- grip.matrix.fromArray( gripPose.transform.matrix );
- grip.matrix.decompose( grip.position, grip.rotation, grip.scale );
- }
- }
- }
- }
- if ( targetRay !== null ) {
- targetRay.visible = ( inputPose !== null );
- }
- if ( grip !== null ) {
- grip.visible = ( gripPose !== null );
- }
- if ( hand !== null ) {
- hand.visible = ( handPose !== null );
- }
- return this;
- }
- } );
- export { WebXRController };
|