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