123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- import { Group } from '../../objects/Group.js';
- const _moveEvent = { type: 'move' };
- class WebXRController {
- constructor() {
- this._targetRay = null;
- this._grip = null;
- this._hand = null;
- }
- getHandSpace() {
- if ( this._hand === null ) {
- this._hand = new Group();
- this._hand.matrixAutoUpdate = false;
- this._hand.visible = false;
- this._hand.joints = {};
- this._hand.inputState = { pinching: false };
- }
- return this._hand;
- }
- getTargetRaySpace() {
- if ( this._targetRay === null ) {
- this._targetRay = new Group();
- this._targetRay.matrixAutoUpdate = false;
- this._targetRay.visible = false;
- }
- return this._targetRay;
- }
- getGripSpace() {
- if ( this._grip === null ) {
- this._grip = new Group();
- this._grip.matrixAutoUpdate = false;
- this._grip.visible = false;
- }
- return this._grip;
- }
- dispatchEvent( 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( 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( 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 && frame.session.visibilityState !== 'visible-blurred' ) {
- 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 );
- this.dispatchEvent( _moveEvent );
- }
- }
- if ( hand && inputSource.hand ) {
- handPose = true;
- for ( const inputjoint of inputSource.hand.values() ) {
- // Update the joints groups with the XRJoint poses
- const jointPose = frame.getJointPose( inputjoint, referenceSpace );
- if ( hand.joints[ inputjoint.jointName ] === undefined ) {
- // The transform of this joint will be updated with the joint pose on each frame
- const joint = new Group();
- joint.matrixAutoUpdate = false;
- joint.visible = false;
- hand.joints[ inputjoint.jointName ] = joint;
- // ??
- hand.add( joint );
- }
- const joint = hand.joints[ inputjoint.jointName ];
- 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 pinchz
- const indexTip = hand.joints[ 'index-finger-tip' ];
- const thumbTip = hand.joints[ 'thumb-tip' ];
- const distance = indexTip.position.distanceTo( thumbTip.position );
- const distanceToPinch = 0.02;
- const threshold = 0.005;
- if ( hand.inputState.pinching && distance > distanceToPinch + threshold ) {
- hand.inputState.pinching = false;
- this.dispatchEvent( {
- type: 'pinchend',
- handedness: inputSource.handedness,
- target: this
- } );
- } else if ( ! hand.inputState.pinching && distance <= distanceToPinch - threshold ) {
- hand.inputState.pinching = true;
- this.dispatchEvent( {
- type: 'pinchstart',
- handedness: inputSource.handedness,
- target: this
- } );
- }
- } else {
- 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 };
|