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

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

首先,新建 UserGestureDemo脚本并实现其KinectGestures.GestureListenerInterface接口,
在场景中添加 KincetManager和UserGestureDemo组件。
挂载脚本
其次,在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)
    {
        
    }    
}

右手作出敬礼的动作后,打印的结果如下:
运行结果

  • 6
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值