LeapMotion 各种动作识别

本文介绍如何使用LeapMotion传感器在Unity中实现手势识别,包括挥手、物体缩放、旋转和平移等功能。通过分析手部姿势和移动方向,实现交互式动画控制,适用于虚拟现实和增强现实应用。

包含左,右,前,后,上,下挥手,物体放大做小,旋转位移等;
可根据需求自由组合。
一:

using Leap;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Leap.Unity;
public class ControlAnimation : MonoBehaviour
{
    private Frame frame;
    LeapProvider provider;
    public HandModelBase leftHandModel;
    public HandModelBase rightHandModel;
    private const float rotate_sensitive = 1500f;  //旋转灵敏度
    private const float displacement_sensitive = 0.015f; //位移灵敏度
    private const float rotate_initial_value = 0f;  //旋转初始位置值

    [Tooltip ("Velocity (m/s) move toward ")]//速度(m/s)走向 
    const float smallestVelocity = 0.1f;
    [Tooltip("Velocity (m/s) move toward ")]
    protected float deltaVelocity = 0.000001f;

    const float deltaCloseFinger = 0.06f;
    [Tooltip("Delta degree to check 2 vectors same direction")]//三角度检查2个向量的方向相同
    protected float handForwardDegree = 30;
    private float deltaAngleThumb = 90;

    void Start()
    {
        provider = FindObjectOfType<LeapProvider>() as LeapProvider;
    }


    void Update()
    {
        //Scale();
        //Rotation();
        //Position();
        frame = provider.CurrentFrame;
        if (frame.Hands.Count > 0)
        {
            foreach (var item in frame.Hands)
            {
                //Debug.Log(item.Fingers.Count + "@" + item.IsLeft+"#"+item.IsRight);
                if (item.IsLeft)
                {
                    //isThumbDirection(item, Vector3.up);
                    Debug.Log(isThumbDirection(item, item.GetPinchPosition()));
                }
            }
        }

    }


    /// <summary>
    /// 缩小
    /// </summary>
    public void Scale()
    {
        Frame frame = provider.CurrentFrame;
        foreach (Hand hand in frame.Hands)
        {
            if (isOpenFullHand(hand))
            {
                Debug.Log("大");

                Vector3 value = transform.localScale;
                value += new Vector3(value.x * 0.01f, value.y * 0.01f, value.z * 0.01f);
                //    Debug.Log(value);
                transform.localScale = value;

            }
            if (isCloseHand(hand))
            {
                Debug.Log("小");
                Vector3 value = transform.localScale;
                value -= new Vector3(value.x * 0.01f, value.y * 0.01f, value.z * 0.01f);
                //   Debug.Log(value);

                transform.localScale = value;

            }
        }
    }


    /// <summary>
    /// 旋转
    /// </summary>
    public void Rotation()
    {
        Frame frame = provider.CurrentFrame;
        foreach (Hand hand in frame.Hands)
        {
            if (hand.IsLeft || hand.IsRight)
            {
                Vector3 value = transform.localEulerAngles;
                value = new Vector3(hand.PalmPosition.y * rotate_sensitive + rotate_initial_value, hand.PalmPosition.x * rotate_sensitive + rotate_initial_value, 0);
                transform.localEulerAngles = value;
            }
            else
            {
                hand.PalmPosition.y = transform.localEulerAngles.x;
                hand.PalmPosition.x = transform.localEulerAngles.y;
            }
        }
    }


    p
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值