AttachmentPointBehaviour.cs
7.66 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/******************************************************************************
* 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 Leap.Unity.Attributes;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Leap.Unity.Attachments {
/// <summary>
/// Simple container class for storing a reference to the attachment point this
/// transform corresponds to within an AttachmentHand. Also contains mappings from
/// a single AttachmentPointFlags flag constant to the relevant bone on a Leap.Hand;
/// these mappings can be accessed statically via GetLeapHandPointData().
///
/// Can also be used to refer to a single AttachmentPointFlags flag constant (implicit conversion).
/// </summary>
[AddComponentMenu("")]
[ExecuteInEditMode]
public class AttachmentPointBehaviour : MonoBehaviour {
[Tooltip("The AttachmentHand associated with this AttachmentPointBehaviour. AttachmentPointBehaviours "
+ "should be beneath their AttachmentHand object in the hierarchy.")]
[Disable]
public AttachmentHand attachmentHand;
[Tooltip("To change which attachment points are available on an AttachmentHand, refer to the "
+ "inspector for the parent AttachmentHands object.")]
[Disable]
public AttachmentPointFlags attachmentPoint;
void OnValidate() {
if (!attachmentPoint.IsSinglePoint() && attachmentPoint != AttachmentPointFlags.None) {
Debug.LogError("AttachmentPointBehaviours should refer to a single attachmentPoint flag.", this.gameObject);
attachmentPoint = AttachmentPointFlags.None;
}
}
void OnDestroy() {
if (attachmentHand != null) {
attachmentHand.notifyPointBehaviourDeleted(this);
}
}
public static implicit operator AttachmentPointFlags(AttachmentPointBehaviour p) {
if (p == null) return AttachmentPointFlags.None;
return p.attachmentPoint;
}
public void SetTransformUsingHand(Leap.Hand hand) {
if (hand == null) {
//Debug.LogError("Unable to set transform with a null hand.", this.gameObject);
return;
}
Vector3 position = Vector3.zero;
Quaternion rotation = Quaternion.identity;
GetLeapHandPointData(hand, this.attachmentPoint, out position, out rotation);
this.transform.position = position;
this.transform.rotation = rotation;
}
public static void GetLeapHandPointData(Leap.Hand hand, AttachmentPointFlags singlePoint, out Vector3 position, out Quaternion rotation) {
position = Vector3.zero;
rotation = Quaternion.identity;
if (!singlePoint.IsSinglePoint()) {
Debug.LogError("Cannot get attachment point data for an AttachmentPointFlags argument consisting of more than one set flag.");
return;
}
switch (singlePoint) {
case AttachmentPointFlags.None:
Debug.LogError("Unable to set transform; this AttachmentPointBehaviour does not have its attachment point flag set.");
return;
case AttachmentPointFlags.Wrist:
position = hand.WristPosition.ToVector3();
rotation = hand.Arm.Basis.rotation.ToQuaternion();
break;
case AttachmentPointFlags.Palm:
position = hand.PalmPosition.ToVector3();
rotation = hand.Basis.rotation.ToQuaternion();
break;
case AttachmentPointFlags.ThumbProximalJoint:
position = hand.Fingers[0].bones[1].NextJoint.ToVector3();
rotation = hand.Fingers[0].bones[2].Rotation.ToQuaternion();
break;
case AttachmentPointFlags.ThumbDistalJoint:
position = hand.Fingers[0].bones[2].NextJoint.ToVector3();
rotation = hand.Fingers[0].bones[3].Rotation.ToQuaternion();
break;
case AttachmentPointFlags.ThumbTip:
position = hand.Fingers[0].bones[3].NextJoint.ToVector3();
rotation = hand.Fingers[0].bones[3].Rotation.ToQuaternion();
break;
case AttachmentPointFlags.IndexKnuckle:
position = hand.Fingers[1].bones[0].NextJoint.ToVector3();
rotation = hand.Fingers[1].bones[1].Rotation.ToQuaternion();
break;
case AttachmentPointFlags.IndexMiddleJoint:
position = hand.Fingers[1].bones[1].NextJoint.ToVector3();
rotation = hand.Fingers[1].bones[2].Rotation.ToQuaternion();
break;
case AttachmentPointFlags.IndexDistalJoint:
position = hand.Fingers[1].bones[2].NextJoint.ToVector3();
rotation = hand.Fingers[1].bones[3].Rotation.ToQuaternion();
break;
case AttachmentPointFlags.IndexTip:
position = hand.Fingers[1].bones[3].NextJoint.ToVector3();
rotation = hand.Fingers[1].bones[3].Rotation.ToQuaternion();
break;
case AttachmentPointFlags.MiddleKnuckle:
position = hand.Fingers[2].bones[0].NextJoint.ToVector3();
rotation = hand.Fingers[2].bones[1].Rotation.ToQuaternion();
break;
case AttachmentPointFlags.MiddleMiddleJoint:
position = hand.Fingers[2].bones[1].NextJoint.ToVector3();
rotation = hand.Fingers[2].bones[2].Rotation.ToQuaternion();
break;
case AttachmentPointFlags.MiddleDistalJoint:
position = hand.Fingers[2].bones[2].NextJoint.ToVector3();
rotation = hand.Fingers[2].bones[3].Rotation.ToQuaternion();
break;
case AttachmentPointFlags.MiddleTip:
position = hand.Fingers[2].bones[3].NextJoint.ToVector3();
rotation = hand.Fingers[2].bones[3].Rotation.ToQuaternion();
break;
case AttachmentPointFlags.RingKnuckle:
position = hand.Fingers[3].bones[0].NextJoint.ToVector3();
rotation = hand.Fingers[3].bones[1].Rotation.ToQuaternion();
break;
case AttachmentPointFlags.RingMiddleJoint:
position = hand.Fingers[3].bones[1].NextJoint.ToVector3();
rotation = hand.Fingers[3].bones[2].Rotation.ToQuaternion();
break;
case AttachmentPointFlags.RingDistalJoint:
position = hand.Fingers[3].bones[2].NextJoint.ToVector3();
rotation = hand.Fingers[3].bones[3].Rotation.ToQuaternion();
break;
case AttachmentPointFlags.RingTip:
position = hand.Fingers[3].bones[3].NextJoint.ToVector3();
rotation = hand.Fingers[3].bones[3].Rotation.ToQuaternion();
break;
case AttachmentPointFlags.PinkyKnuckle:
position = hand.Fingers[4].bones[0].NextJoint.ToVector3();
rotation = hand.Fingers[4].bones[1].Rotation.ToQuaternion();
break;
case AttachmentPointFlags.PinkyMiddleJoint:
position = hand.Fingers[4].bones[1].NextJoint.ToVector3();
rotation = hand.Fingers[4].bones[2].Rotation.ToQuaternion();
break;
case AttachmentPointFlags.PinkyDistalJoint:
position = hand.Fingers[4].bones[2].NextJoint.ToVector3();
rotation = hand.Fingers[4].bones[3].Rotation.ToQuaternion();
break;
case AttachmentPointFlags.PinkyTip:
position = hand.Fingers[4].bones[3].NextJoint.ToVector3();
rotation = hand.Fingers[4].bones[3].Rotation.ToQuaternion();
break;
}
}
}
}