在Unity中添加Kinect自定义手势,以添加右手敬礼为例

这篇博文对Kinect 手势开发总结得很棒:Kinect for Unity3D开发 之 手势/姿势(Gesture)识别基础知识

首先,新建 UserGestureDemo脚本并实现其KinectGestures.GestureListenerInterface接口,
在场景中添加 KincetManager和UserGestureDemo组件。
在Unity中添加Kinect自定义手势,以添加右手敬礼为例_第1张图片
其次,在KinectGestures.Gestures中添加敬礼手势的标识符:Salute

添加完敬礼标识符后,我们可以在KinectGestures类中的CheckForGesture()方法中去添加我们的敬礼手势。有一个很长的switch(),每个case处理一个手势的检测,给你的手势添加一个case。

敬礼手势代码如下:

			case Gestures.Salute:// Add by JoeManba
                switch (gestureData.state)
                {
                    case 0:  // gesture detection
                        if (jointsTracked[rightHandIndex] && jointsTracked[rightElbowIndex] && jointsTracked[rightShoulderIndex])
                        {
                            //肘关节与手的向量
                            Vector3 fromVector = jointsPos[rightHandIndex] - jointsPos[rightElbowIndex];
                            //肘关节与肩膀的向量
                            Vector3 toVector = jointsPos[rightShoulderIndex] - jointsPos[rightElbowIndex];
                            //计算两向量的夹角
                            float angle = Vector3.Angle(fromVector, toVector);

                            if (angle > 25 && angle < 45)
                            {
                                //Debug.Log("Salute Angel:" + angle);
                                SetGestureJoint(ref gestureData, timestamp, rightHandIndex, jointsPos[rightHandIndex]);
                            }

                        }
                        break;

                    case 1:  // gesture complete

                        bool isInPose = jointsTracked[rightHandIndex] && jointsTracked[rightElbowIndex] && jointsTracked[rightShoulderIndex];
                        Vector3 jointPos = jointsPos[gestureData.joint];
                        CheckPoseComplete(ref gestureData, timestamp, jointPos, isInPose, KinectInterop.Constants.PoseCompleteDuration);
                        break;
                }
                break;

添加完敬礼手势后,编辑UserGestureDemo脚本:

using System;
using UnityEngine;
public class UserGentureDemo : MonoBehaviour, KinectGestures.GestureListenerInterface
{

    public Action OnSalute;

    public bool GestureCancelled(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint)
    {
        return true;
    }

    public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture, KinectInterop.JointType joint, Vector3 screenPos)
    {
        if (gesture == KinectGestures.Gestures.Salute)
        {
            Debug.Log("-------------------敬礼---------------------------");
            if (OnSalute != null)
            {
                OnSalute();
            }
        }
        return true;
    }

    public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture, float progress, KinectInterop.JointType joint, Vector3 screenPos)
    {
        
    }

    public void UserDetected(long userId, int userIndex)
    {
        KinectManager manager = KinectManager.Instance;

        manager.DetectGesture(userId, KinectGestures.Gestures.Salute);
    }

    public void UserLost(long userId, int userIndex)
    {
        
    }    
}

右手作出敬礼的动作后,打印的结果如下:
在Unity中添加Kinect自定义手势,以添加右手敬礼为例_第2张图片

你可能感兴趣的:(Kinect,Unity)