Hololens2手部跟踪 / 手势识别 - MRTK2
Hololens2手部跟踪 / 手势识别 - MRTK2
·
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();
}
}
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)