Tanoda
FingerDirectionDetector.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
9using UnityEngine;
10using System.Collections;
12
13namespace Leap.Unity {
35 [Units("seconds")]
36 [Tooltip("The interval in seconds at which to check this detector's conditions.")]
37 [MinValue(0)]
38 public float Period = .1f; //seconds
39
45 [Tooltip("The hand model to watch. Set automatically if detector is on a hand.")]
46 public HandModelBase HandModel = null;
47
52 [Tooltip("The finger to observe.")]
54
55
71 [Header("Direction Settings")]
72 [Tooltip("How to treat the target direction.")]
73 public PointingType PointingType = PointingType.RelativeToHorizon;
74
80 [Tooltip("The target direction.")]
81 [DisableIf("PointingType", isEqualTo: PointingType.AtTarget)]
82 public Vector3 PointingDirection = Vector3.forward;
83
87 [Tooltip("A target object(optional). Use PointingType.AtTarget")]
88 [DisableIf("PointingType", isNotEqualTo: PointingType.AtTarget)]
89 public Transform TargetObject = null;
95 [Tooltip("The angle in degrees from the target direction at which to turn on.")]
96 [Range(0, 180)]
97 public float OnAngle = 15f; //degrees
98
104 [Tooltip("The angle in degrees from the target direction at which to turn off.")]
105 [Range(0, 180)]
106 public float OffAngle = 25f; //degrees
110 [Header("")]
111 [Tooltip("Draw this detector's Gizmos, if any. (Gizmos must be on in Unity edtor, too.)")]
112 public bool ShowGizmos = true;
113
114 private IEnumerator watcherCoroutine;
115
116 private void OnValidate(){
117 if( OffAngle < OnAngle){
119 }
120 }
121
122 private void Awake () {
123 watcherCoroutine = fingerPointingWatcher();
124 }
125
126 private void OnEnable () {
127 StartCoroutine(watcherCoroutine);
128 }
129
130 private void OnDisable () {
131 StopCoroutine(watcherCoroutine);
132 Deactivate();
133 }
134
135 private IEnumerator fingerPointingWatcher() {
136 Hand hand;
137 Vector3 fingerDirection;
138 Vector3 targetDirection;
139 int selectedFinger = selectedFingerOrdinal();
140 while(true){
141 if(HandModel != null && HandModel.IsTracked){
142 hand = HandModel.GetLeapHand();
143 if(hand != null){
144 targetDirection = selectedDirection(hand.Fingers[selectedFinger].TipPosition.ToVector3());
145 fingerDirection = hand.Fingers[selectedFinger].Bone(Bone.BoneType.TYPE_DISTAL).Direction.ToVector3();
146 float angleTo = Vector3.Angle(fingerDirection, targetDirection);
147 if(HandModel.IsTracked && angleTo <= OnAngle){
148 Activate();
149 } else if (!HandModel.IsTracked || angleTo >= OffAngle) {
150 Deactivate();
151 }
152 }
153 }
154 yield return new WaitForSeconds(Period);
155 }
156 }
157
158 private Vector3 selectedDirection(Vector3 tipPosition){
159 switch(PointingType){
160 case PointingType.RelativeToHorizon:
161 Quaternion cameraRot = Camera.main.transform.rotation;
162 float cameraYaw = cameraRot.eulerAngles.y;
163 Quaternion rotator = Quaternion.AngleAxis(cameraYaw, Vector3.up);
164 return rotator * PointingDirection;
165 case PointingType.RelativeToCamera:
166 return Camera.main.transform.TransformDirection(PointingDirection);
167 case PointingType.RelativeToWorld:
168 return PointingDirection;
169 case PointingType.AtTarget:
170 return TargetObject.position - tipPosition;
171 default:
172 return PointingDirection;
173 }
174 }
175
176 private int selectedFingerOrdinal(){
177 switch(FingerName){
178 case Finger.FingerType.TYPE_INDEX:
179 return 1;
180 case Finger.FingerType.TYPE_MIDDLE:
181 return 2;
182 case Finger.FingerType.TYPE_PINKY:
183 return 4;
184 case Finger.FingerType.TYPE_RING:
185 return 3;
186 case Finger.FingerType.TYPE_THUMB:
187 return 0;
188 default:
189 return 1;
190 }
191 }
192
193 #if UNITY_EDITOR
194 private void OnDrawGizmos () {
195 if (ShowGizmos && HandModel != null && HandModel.IsTracked) {
196 Color innerColor;
197 if (IsActive) {
198 innerColor = OnColor;
199 } else {
200 innerColor = OffColor;
201 }
202 Finger finger = HandModel.GetLeapHand().Fingers[selectedFingerOrdinal()];
203 Vector3 fingerDirection = finger.Bone(Bone.BoneType.TYPE_DISTAL).Direction.ToVector3();
204 Utils.DrawCone(finger.TipPosition.ToVector3(), fingerDirection, OnAngle, finger.Length, innerColor);
205 Utils.DrawCone(finger.TipPosition.ToVector3(), fingerDirection, OffAngle, finger.Length, LimitColor);
206 Gizmos.color = DirectionColor;
207 Gizmos.DrawRay(finger.TipPosition.ToVector3(), selectedDirection(finger.TipPosition.ToVector3()));
208 }
209 }
210 #endif
211 }
212}
UnityEngine.Color Color
Definition: TestScript.cs:32
The Finger class represents a tracked finger.
Definition: Finger.cs:20
FingerType
Enumerates the names of the fingers.
Definition: Finger.cs:167
List< Finger > Fingers
The list of Finger objects detected in this frame that are attached to this hand, given in order from...
Definition: Hand.cs:159
virtual void Deactivate()
Definition: Detector.cs:66
Color DirectionColor
Definition: Detector.cs:77
virtual void Activate()
Definition: Detector.cs:54
abstract Hand GetLeapHand()