Hololens2手部跟踪 / 手势识别 - MRTK2

using Microsoft.MixedReality.Toolkit.Input;
using Microsoft.MixedReality.Toolkit.Utilities;
using Microsoft.MixedReality.Toolkit;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class GlobalHandListenerExample : MonoBehaviour,
    IMixedRealitySourceStateHandler, // 处理检测到的以及丢失的源
    IMixedRealityHandJointHandler //  处理手的关节位置更新
{
    IMixedRealityHand hand;
    private float lastGestureTime = 0.0f;

    public UnityEvent unityEvent;
    private void OnEnable()
    {
        // 指示输入系统我们希望接收所有类型的输入事件
        // IMixedRealitySourceStateHandler 和 IMixedRealityHandJointHandler
        CoreServices.InputSystem?.RegisterHandler<IMixedRealitySourceStateHandler>(this);
        CoreServices.InputSystem?.RegisterHandler<IMixedRealityHandJointHandler>(this);
    }

    private void OnDisable()
    {
        // 该组件被销毁
        // 指示输入系统无视我们来进行输入事件处理
        CoreServices.InputSystem?.UnregisterHandler<IMixedRealitySourceStateHandler>(this);
        CoreServices.InputSystem?.UnregisterHandler<IMixedRealityHandJointHandler>(this);
    }

    // IMixedRealitySourceStateHandler 接口
    public void OnSourceDetected(SourceStateEventData eventData)
    {
        hand = eventData.Controller as IMixedRealityHand;

        // 仅对全关节手输入源做出响应
        if (hand != null)
        {
            Debug.Log("Source detected: " + hand.ControllerHandedness);
        }
    }

    public void OnSourceLost(SourceStateEventData eventData)
    {
        hand = eventData.Controller as IMixedRealityHand;

        // 仅对全关节手输入源做出响应
        if (hand != null)
        {
            Debug.Log("Source lost: " + hand.ControllerHandedness);
        }
    }

    public void OnHandJointsUpdated(
                InputEventData<IDictionary<TrackedHandJoint, MixedRealityPose>> eventData)
    {
        MixedRealityPose ThumbTipPose;
        MixedRealityPose PinkyTipPose;

        if (eventData.InputData.TryGetValue(TrackedHandJoint.ThumbTip, out ThumbTipPose))
        {
            //Debug.Log("Hand Joint IndexTipPose Updated: " + IndexTipPose.Position);
        }

        if (eventData.InputData.TryGetValue(TrackedHandJoint.PinkyTip, out PinkyTipPose))
        {
            //Debug.Log("Hand Joint MiddleTipPose Updated: " + MiddleTipPose.Position);
        }
        //两指距离小于0.05f
        if (Vector3.Distance(ThumbTipPose.Position, PinkyTipPose.Position) < 0.05f)
        {
            // 触发相应的功能
            float currentTime = Time.time;
            //???
            //Debug.Log("ThumbTipPose && PinkyTipPose");
            // 如果当前时间与上次手势检测时间的差大于2秒
            if (currentTime - lastGestureTime > 2.0f)
            {
                // 更新上次手势检测时间
                lastGestureTime = currentTime;

                // 触发相应的功能
                Debug.Log("ThumbTipPose && PinkyTipPose");
                unityEvent.Invoke();
            }
        }
    }
}
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐