Leapmotion 感悟

来源:互联网 发布:域名注册godaddy 编辑:程序博客网 时间:2024/06/14 17:40

自己学的其实很简单,很浅显,但是决定还是写下来

我觉得首先要了解leapmotion自带的 core asset中的相关内容

下载后导入到unity中基本上就是两个文件夹

这里写图片描述
在scene场景中导入Leap_Hands_Demo_Desktop场景,即桌面式的leapmotion,就是把leapmotion直接放到电脑前面就可以的。
这里写图片描述
这就是它在hierarchy视图中的结构。我们来一个个的分析一下

这里写图片描述

这个是leaphandcontroller的inspector视图显示的东西
可以看到它其实就是一个empty object里面加了两个脚本控制。
(1)leap hand controller脚本:
打开后先看到是一个类的定义,

public class LeapHandController : MonoBehaviour{...}//说明leaphandcontroller继承自monobehaviour类,后面有很多变量说明等等的,我挑几个主要的方法定义来说,如下://(1)start方法,一旦电脑安好leapmotion的驱动,unity中也匹配好了的时候,那么你插入leapmotion,并且在unity运行的时候就会调用start()方法用作初始化    protected virtual void Start() {      Provider = GetComponent<LeapProvider>();      Factory = GetComponent<HandFactory>();    }//(2)update方法,就是每一帧调用一次,大概也就是几微秒吧,我也不知道其实    /** Updates the graphics HandRepresentations. */    protected virtual void Update() {      Frame frame = Provider.CurrentFrame;//frame里面包含了所有的信息,譬如手的位置,角度等等      if (frame.Id != prev_graphics_id_ && graphicsEnabled) {        UpdateHandRepresentations(graphicsReps, ModelType.Graphics, frame);//说明每一帧都会调用这个函数哦        prev_graphics_id_ = frame.Id;      }    }//(3)fixedupdate()也是每一个隔一定的物理时间就会不断调用,貌似是也是几微秒的样子    /** Updates the physics HandRepresentations. */    protected virtual void FixedUpdate() {      Frame fixedFrame = Provider.CurrentFixedFrame;      if (fixedFrame.Id != prev_physics_id_ && physicsEnabled) {        UpdateHandRepresentations(physicsReps, ModelType.Physics, fixedFrame);//也是一样不断调用这个函数        prev_physics_id_ = fixedFrame.Id;      }    }//(4)终于看到了上面不断调用的函数是个啥,这里有代码解释,给了一部分这个函数,觉得就是对手啊什么的识别判断一下的    /**     * Updates HandRepresentations based in the specified HandRepresentation Dictionary.    * Active HandRepresentation instances are updated if the hand they represent is still    * present in the Provider's CurrentFrame; otherwise, the HandRepresentation is removed. If new    * Leap Hand objects are present in the Leap HandRepresentation Dictionary, new HandRepresentations are     * created and added to the dictionary.     * @param all_hand_reps = A dictionary of Leap Hand ID's with a paired HandRepresentation    * @param modelType Filters for a type of hand model, for example, physics or graphics hands.    * @param frame The Leap Frame containing Leap Hand data for each currently tracked hand    */    void UpdateHandRepresentations(Dictionary<int, HandRepresentation> all_hand_reps, ModelType modelType, Frame frame) {      foreach (Leap.Hand curHand in frame.Hands) {        HandRepresentation rep;        if (!all_hand_reps.TryGetValue(curHand.Id, out rep)) {          rep = Factory.MakeHandRepresentation(curHand, modelType);          if (rep != null) {            all_hand_reps.Add(curHand.Id, rep);          }        }        if (rep != null) {          rep.IsMarked = true;          rep.UpdateRepresentation(curHand, modelType);          rep.LastUpdatedTime = (int)frame.Timestamp;        }        .......      }

(2)还有一个leap service provider 脚本

leapmotion官网上的解释是:
provides access to the tracking data and images as well as access to the Leap.|Controller|_ object. The LeapServiceProvider transforms the tracking data so that the coordinates are relative to the transform of the Unity GameObject to which it is attached. For example, if you rotate the GameObject, the hands are also rotated in the Unity scene. Likewise, a hand 200mm above the Leap Motion device is placed 200mm above the GameObject in the Unity scene. The LeapServiceProvider also converts the coordinate system from the right-handed convention used by the Leap Motion software to the left-handed convention used by Unity。

(3)最后一个handpool脚本
其实也是一个class定义里面,像我这种学的浅的没有关注过太具体的代码,知道它大概干嘛的就好了。

下面就是leaphandcontroller的四个子目录了
这里写图片描述
四个其实是两个嘛,左右手都一样的其实
capsulehand与rigidroundhand。

其中capsulehand其实就是图形中的手
其组间有叫capsulehand的脚本如下:

//也是一个类 public class CapsuleHand : IHandModel { //前面都是一堆变量声明,对于手的大小啊什么乱七八糟的说明    private const int THUMB_BASE_INDEX = (int)Finger.FingerType.TYPE_THUMB * 4 ;    private const int PINKY_BASE_INDEX = (int)Finger.FingerType.TYPE_PINKY * 4 ;    private const float SPHERE_RADIUS = 0.008f;    private const float CYLINDER_RADIUS = 0.006f;    private const float PALM_RADIUS = 0.015f;    private static int _colorIndex = 0;    private static Color[] _colorList = { Color.blue, Color.green, Color.magenta, Color.cyan, Color.red, Color.yellow };    .......    //下面单独说几个重要的方法    public override void InitHand(){...}//初始化    public override void UpdateHand()//每帧调用     {     updateSpheres();     updateArm();     updateCapsules();     ......     }

还有一个hand enable disable脚本顾名思义了吧就

再来看看rigidroundhand
这里写图片描述
这个rigidroundhand也是一个empty object,它含有的脚本如下这里写图片描述
其中关键的就是这个rigid hand脚本了

public override void UpdateHand() //里面含有这个关键的方法{   for (int f = 0; f < fingers.Length; ++f) {        if (fingers[f] != null) {          fingers[f].UpdateFinger();//这个updatefinger()函数我们后面再看看        }      }  ......}

再往下走就是五个手指还有什么手掌的了,这里只举一个thumb的例子

thumb 含有一个rigid finger 脚本

再往下走,就是thumb下的三个bone,都含有rigidbody和capsule collider组间

0 0
原创粉丝点击