Tanoda
AttachmentPointBehaviour.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
10using System;
11using System.Collections;
12using System.Collections.Generic;
13using UnityEngine;
14
15namespace Leap.Unity.Attachments {
16
25 [AddComponentMenu("")]
26 [ExecuteInEditMode]
27 public class AttachmentPointBehaviour : MonoBehaviour {
28
29 [Tooltip("The AttachmentHand associated with this AttachmentPointBehaviour. AttachmentPointBehaviours "
30 + "should be beneath their AttachmentHand object in the hierarchy.")]
31 [Disable]
33
34 [Tooltip("To change which attachment points are available on an AttachmentHand, refer to the "
35 + "inspector for the parent AttachmentHands object.")]
36 [Disable]
38
39 void OnValidate() {
40 if (!attachmentPoint.IsSinglePoint() && attachmentPoint != AttachmentPointFlags.None) {
41 Debug.LogError("AttachmentPointBehaviours should refer to a single attachmentPoint flag.", this.gameObject);
43 }
44 }
45
46 void OnDestroy() {
47 if (attachmentHand != null) {
49 }
50 }
51
52 public static implicit operator AttachmentPointFlags(AttachmentPointBehaviour p) {
53 if (p == null) return AttachmentPointFlags.None;
54 return p.attachmentPoint;
55 }
56
57 public void SetTransformUsingHand(Leap.Hand hand) {
58 if (hand == null) {
59 //Debug.LogError("Unable to set transform with a null hand.", this.gameObject);
60 return;
61 }
62
63 Vector3 position = Vector3.zero;
64 Quaternion rotation = Quaternion.identity;
65
66 GetLeapHandPointData(hand, this.attachmentPoint, out position, out rotation);
67
68 this.transform.position = position;
69 this.transform.rotation = rotation;
70 }
71
72 public static void GetLeapHandPointData(Leap.Hand hand, AttachmentPointFlags singlePoint, out Vector3 position, out Quaternion rotation) {
73 position = Vector3.zero;
74 rotation = Quaternion.identity;
75
76 if (singlePoint != AttachmentPointFlags.None && !singlePoint.IsSinglePoint()) {
77 Debug.LogError("Cannot get attachment point data for an AttachmentPointFlags argument consisting of more than one set flag.");
78 return;
79 }
80
81 switch (singlePoint) {
82 case AttachmentPointFlags.None:
83 return;
84
85 case AttachmentPointFlags.Wrist:
86 position = hand.WristPosition.ToVector3();
87 rotation = hand.Arm.Basis.rotation.ToQuaternion();
88 break;
89 case AttachmentPointFlags.Palm:
90 position = hand.PalmPosition.ToVector3();
91 rotation = hand.Basis.rotation.ToQuaternion();
92 break;
93
94 case AttachmentPointFlags.ThumbProximalJoint:
95 position = hand.Fingers[0].bones[1].NextJoint.ToVector3();
96 rotation = hand.Fingers[0].bones[2].Rotation.ToQuaternion();
97 break;
98 case AttachmentPointFlags.ThumbDistalJoint:
99 position = hand.Fingers[0].bones[2].NextJoint.ToVector3();
100 rotation = hand.Fingers[0].bones[3].Rotation.ToQuaternion();
101 break;
102 case AttachmentPointFlags.ThumbTip:
103 position = hand.Fingers[0].bones[3].NextJoint.ToVector3();
104 rotation = hand.Fingers[0].bones[3].Rotation.ToQuaternion();
105 break;
106
107 case AttachmentPointFlags.IndexKnuckle:
108 position = hand.Fingers[1].bones[0].NextJoint.ToVector3();
109 rotation = hand.Fingers[1].bones[1].Rotation.ToQuaternion();
110 break;
111 case AttachmentPointFlags.IndexMiddleJoint:
112 position = hand.Fingers[1].bones[1].NextJoint.ToVector3();
113 rotation = hand.Fingers[1].bones[2].Rotation.ToQuaternion();
114 break;
115 case AttachmentPointFlags.IndexDistalJoint:
116 position = hand.Fingers[1].bones[2].NextJoint.ToVector3();
117 rotation = hand.Fingers[1].bones[3].Rotation.ToQuaternion();
118 break;
119 case AttachmentPointFlags.IndexTip:
120 position = hand.Fingers[1].bones[3].NextJoint.ToVector3();
121 rotation = hand.Fingers[1].bones[3].Rotation.ToQuaternion();
122 break;
123
124 case AttachmentPointFlags.MiddleKnuckle:
125 position = hand.Fingers[2].bones[0].NextJoint.ToVector3();
126 rotation = hand.Fingers[2].bones[1].Rotation.ToQuaternion();
127 break;
128 case AttachmentPointFlags.MiddleMiddleJoint:
129 position = hand.Fingers[2].bones[1].NextJoint.ToVector3();
130 rotation = hand.Fingers[2].bones[2].Rotation.ToQuaternion();
131 break;
132 case AttachmentPointFlags.MiddleDistalJoint:
133 position = hand.Fingers[2].bones[2].NextJoint.ToVector3();
134 rotation = hand.Fingers[2].bones[3].Rotation.ToQuaternion();
135 break;
136 case AttachmentPointFlags.MiddleTip:
137 position = hand.Fingers[2].bones[3].NextJoint.ToVector3();
138 rotation = hand.Fingers[2].bones[3].Rotation.ToQuaternion();
139 break;
140
141 case AttachmentPointFlags.RingKnuckle:
142 position = hand.Fingers[3].bones[0].NextJoint.ToVector3();
143 rotation = hand.Fingers[3].bones[1].Rotation.ToQuaternion();
144 break;
145 case AttachmentPointFlags.RingMiddleJoint:
146 position = hand.Fingers[3].bones[1].NextJoint.ToVector3();
147 rotation = hand.Fingers[3].bones[2].Rotation.ToQuaternion();
148 break;
149 case AttachmentPointFlags.RingDistalJoint:
150 position = hand.Fingers[3].bones[2].NextJoint.ToVector3();
151 rotation = hand.Fingers[3].bones[3].Rotation.ToQuaternion();
152 break;
153 case AttachmentPointFlags.RingTip:
154 position = hand.Fingers[3].bones[3].NextJoint.ToVector3();
155 rotation = hand.Fingers[3].bones[3].Rotation.ToQuaternion();
156 break;
157
158 case AttachmentPointFlags.PinkyKnuckle:
159 position = hand.Fingers[4].bones[0].NextJoint.ToVector3();
160 rotation = hand.Fingers[4].bones[1].Rotation.ToQuaternion();
161 break;
162 case AttachmentPointFlags.PinkyMiddleJoint:
163 position = hand.Fingers[4].bones[1].NextJoint.ToVector3();
164 rotation = hand.Fingers[4].bones[2].Rotation.ToQuaternion();
165 break;
166 case AttachmentPointFlags.PinkyDistalJoint:
167 position = hand.Fingers[4].bones[2].NextJoint.ToVector3();
168 rotation = hand.Fingers[4].bones[3].Rotation.ToQuaternion();
169 break;
170 case AttachmentPointFlags.PinkyTip:
171 position = hand.Fingers[4].bones[3].NextJoint.ToVector3();
172 rotation = hand.Fingers[4].bones[3].Rotation.ToQuaternion();
173 break;
174 }
175 }
176
177 }
178
179}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
The Hand class reports the physical characteristics of a detected hand.
Definition: Hand.cs:26
This MonoBehaviour is managed by an AttachmentHands component on a parent MonoBehaviour....
void notifyPointBehaviourDeleted(AttachmentPointBehaviour point)
Simple container class for storing a reference to the attachment point this transform corresponds to ...
static void GetLeapHandPointData(Leap.Hand hand, AttachmentPointFlags singlePoint, out Vector3 position, out Quaternion rotation)
AttachmentPointFlags
Flags for attachment points on the hand.