using UnityEngine;
namespace Stary.Evo.RKTools
{
#if Evo_Rokid
public abstract class GestureRokidBase : MonoBehaviour
{
private Rokid.UXR.Interaction.GestureBean leftBean = null;
private Rokid.UXR.Interaction.GestureBean rightBean = null;
protected void OnEnable()
{
Rokid.UXR.Interaction.GesEventInput.OnTrackedSuccess += OnTrackedSuccess;
Rokid.UXR.Interaction.GesEventInput.OnTrackedFailed += OnTrackedFailed;
Rokid.UXR.Interaction.GesEventInput.OnRenderHand += OnRenderHand;
}
protected void OnDisable()
{
Rokid.UXR.Interaction.GesEventInput.OnTrackedSuccess -= OnTrackedSuccess;
Rokid.UXR.Interaction.GesEventInput.OnTrackedFailed -= OnTrackedFailed;
Rokid.UXR.Interaction.GesEventInput.OnRenderHand -= OnRenderHand;
}
protected void FixedUpdate()
{
if (leftBean != null)
{
Rokid.UXR.Interaction.GestureType type =
Rokid.UXR.Interaction.GesEventInput.Instance.GetGestureType(Rokid.UXR.Interaction.HandType.LeftHand);
Vector3 handForward =
GetSkeletonPose(Rokid.UXR.Interaction.SkeletonIndexFlag.PALM, Rokid.UXR.Interaction.HandType.LeftHand).forward;
GestureLeftSuccess(type, handForward);
}
else
{
GestureLeftFail();
}
if (rightBean != null)
{
Rokid.UXR.Interaction.GestureType type =
Rokid.UXR.Interaction.GesEventInput.Instance.GetGestureType(Rokid.UXR.Interaction.HandType.RightHand);
Vector3 handForward =
GetSkeletonPose(Rokid.UXR.Interaction.SkeletonIndexFlag.PALM, Rokid.UXR.Interaction.HandType.RightHand).forward;
GestureRightSuccess(type, handForward);
}
else
{
GestureRightFail();
}
}
private void OnTrackedSuccess(Rokid.UXR.Interaction.HandType handType)
{
}
private void OnTrackedFailed(Rokid.UXR.Interaction.HandType handType)
{
if (handType == Rokid.UXR.Interaction.HandType.None)
{
leftBean = null;
rightBean = null;
}
if (handType == Rokid.UXR.Interaction.HandType.RightHand)
{
rightBean = null;
}
if (handType == Rokid.UXR.Interaction.HandType.LeftHand)
{
leftBean = null;
}
}
private void OnRenderHand(Rokid.UXR.Interaction.HandType handType, Rokid.UXR.Interaction.GestureBean gestureBean)
{
if (handType == Rokid.UXR.Interaction.HandType.RightHand)
{
rightBean = gestureBean;
}
if (handType == Rokid.UXR.Interaction.HandType.LeftHand)
{
leftBean = gestureBean;
}
if (handType == Rokid.UXR.Interaction.HandType.None)
{
rightBean = null;
leftBean = null;
}
}
///
/// 获取骨架点位置
///
/// 骨架序号
/// 哪只手
///
public Pose GetSkeletonPose(Rokid.UXR.Interaction.SkeletonIndexFlag index, Rokid.UXR.Interaction.HandType hand)
{
return Rokid.UXR.Interaction.GesEventInput.Instance.GetSkeletonPose(index, hand);
}
///
/// 获取手地朝向
///
/// 哪只手
///
public Vector3 GetHandForward(Rokid.UXR.Interaction.HandType handType)
{
Vector3 handForward =
(GetSkeletonPose(Rokid.UXR.Interaction.SkeletonIndexFlag.MIDDLE_FINGER_MCP, handType).position -
GetSkeletonPose(Rokid.UXR.Interaction.SkeletonIndexFlag.WRIST, handType).position);
return handForward;
}
#region 需要被重写的方法
///
/// 左手成功识别到手势不断执行操作
///
/// 左手手势类型
/// 左手掌心朝向
public abstract void GestureLeftSuccess(Rokid.UXR.Interaction.GestureType gestureType, Vector3 handForward);
///
/// 右手成功识别到手势不断执行操作
///
/// 右手手势类型
/// 右手掌心朝向
public abstract void GestureRightSuccess(Rokid.UXR.Interaction.GestureType gestureType, Vector3 handForward);
///
/// 左手未识别到手势执行操作
///
public abstract void GestureLeftFail();
///
/// 右手未识别到手势执行操作
///
public abstract void GestureRightFail();
#endregion
}
#endif
}