Unity中,Leapmotion一共由两个组件组成,在leapmotion的untiy官方包中有预制物体,一个HandModels一个Leap Motion Controller,在HandModels中更换手的样式,具体编程和算法需参考官方SDK,链接如下:
https://developer-archive.leapmotion.com/documentation/csharp/api/Leap.Hand.html?proglang=csharp
在其中选择自己适合的编程语言,值得注意的是,我看了官方SDK的说明后,发现把很多朋友说的Gesture这个类,好像是取消了,找了很久都没找到这个类,有待后期挖掘。
在SDK的操作中,如果需要对finger或者Bone操作,整体的思路就是首先要在Update中寻找CurrueFrame.然后,再在Frame中对手进行遍历,寻找到自己需要的左右或者右手,再把自己需要操作的手进行遍历寻找到finger,再把finger遍历寻找bone,这样一层一层的找下去,在对应的层中,才能对相应的对象进行操作。按照SDK中的解说,可对每一项进行向量或者float,length的操作。
如需要使用粒子系统跟踪手的位置,需要对粒子进行操作的话,我这里实验了很久,参照官方API,先获取粒子,然后对粒子进行遍历,在遍历中对粒子的positon进行设置,试了很久都不行。也不知道为什么,可能是我本人的方法不对。最后,我改变方向,获取粒子后,对粒子的速度进行设置,设置粒子的速度为tartget.postion-particles.postion,这样达到了我的预期,粒子会跟着手动了,完美解决。
如在leapmotion中需要摄像机跟着leapmotion的手指方向进行旋转,只需要将摄像机LookAt(finger.TipPosition)即可。但是,这样会旋转的非常快,灵敏度很高,如真的在开发中使用此功能,需要优化。
Vector3 fingerTip = finger.TipPosition.ToVector3();
gameObject.transform.LookAt(fingerTip);
现将代码贴出来,分享给大家:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Leap;
using Leap.Unity;
public class particleSystemStrip : MonoBehaviour
{
ParticleSystem m_System;
LeapProvider provider;
ParticleSystem.Particle[] m_Particles;
public float m_Drift = 0.01f;
public float speed = 0.01f;
private void Start()
{
//m_System = FindObjectOfType<ParticleSystem>();
m_System = gameObject.GetComponent<ParticleSystem>(); //获取粒子系统
provider = FindObjectOfType<LeapProvider>() as LeapProvider; //寻找leapmotion控制器
}
private void Update()
{
Frame mFrame = provider.CurrentFrame; //获取Leapmotion的Frame
m_Particles = new ParticleSystem.Particle[m_System.particleCount];
int cout = m_System.GetParticles(m_Particles); //获取当前活着的粒子数
{
if (hand.IsLeft)
{
{
if (finger.Type==Finger.FingerType.TYPE_INDEX&finger.IsExtended)
{
Vector3 tipFinger = finger.TipPosition.ToVector3();
for (var i = 0; i < cout; i++)
{
m_Particles[i].velocity = tipFinger - m_Particles[i].position; //设置每一个粒子的速度,方向
m_System.SetParticles(m_Particles, cout); //设置该粒子系统的粒子
}
}
}
}
}
}
}