RiggedFinger.cs
2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/******************************************************************************
* Copyright (C) Leap Motion, Inc. 2011-2017. *
* Leap Motion proprietary and confidential. *
* *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
******************************************************************************/
using UnityEngine;
using System.Collections;
using Leap;
namespace Leap.Unity {
/**
* Manages the orientation of the bones in a model rigged for skeletal animation.
*
* The class expects that the graphics model bones corresponding to bones in the Leap Motion
* hand model are in the same order in the bones array.
*/
public class RiggedFinger : FingerModel {
/** Allows the mesh to be stretched to align with finger joint positions
* Only set to true when mesh is not visible
*/
[HideInInspector]
public bool deformPosition = false;
public Vector3 modelFingerPointing = Vector3.forward;
public Vector3 modelPalmFacing = -Vector3.up;
public Quaternion Reorientation() {
return Quaternion.Inverse(Quaternion.LookRotation(modelFingerPointing, -modelPalmFacing));
}
private RiggedHand riggedHand;
/** Updates the bone rotations. */
public override void UpdateFinger() {
for (int i = 0; i < bones.Length; ++i) {
if (bones[i] != null) {
bones[i].rotation = GetBoneRotation(i) * Reorientation();
if (deformPosition) {
bones[i].position = GetJointPosition(i);
}
}
}
}
public void SetupRiggedFinger (bool useMetaCarpals) {
findBoneTransforms(useMetaCarpals);
modelFingerPointing = calulateModelFingerPointing();
}
private void findBoneTransforms(bool useMetaCarpals) {
if (!useMetaCarpals || fingerType == Finger.FingerType.TYPE_THUMB) {
bones[1] = transform;
bones[2] = transform.GetChild(0).transform;
bones[3] = transform.GetChild(0).transform.GetChild(0).transform;
}
else {
bones[0] = transform;
bones[1] = transform.GetChild(0).transform;
bones[2] = transform.GetChild(0).transform.GetChild(0).transform;
bones[3] = transform.GetChild(0).transform.GetChild(0).transform.GetChild(0).transform;
}
}
private Vector3 calulateModelFingerPointing() {
Vector3 distance = transform.InverseTransformPoint(transform.position) - transform.InverseTransformPoint(transform.GetChild(0).transform.position);
Vector3 zeroed = RiggedHand.CalculateZeroedVector(distance);
return zeroed;
}
}
}