Tanoda
PalmDirectionDetector.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 {
14
38 [Units("seconds")]
39 [Tooltip("The interval in seconds at which to check this detector's conditions.")]
40 [MinValue(0)]
41 public float Period = .1f; //seconds
47 [Tooltip("The hand model to watch. Set automatically if detector is on a hand.")]
48 public HandModelBase HandModel = null;
49
65 [Header("Direction Settings")]
66 [Tooltip("How to treat the target direction.")]
67 public PointingType PointingType = PointingType.RelativeToHorizon;
68
74 [Tooltip("The target direction.")]
75 [DisableIf("PointingType", isEqualTo: PointingType.AtTarget)]
76 public Vector3 PointingDirection = Vector3.forward;
77
81 [Tooltip("A target object(optional). Use PointingType.AtTarget")]
82 [DisableIf("PointingType", isNotEqualTo: PointingType.AtTarget)]
83 public Transform TargetObject = null;
84
90 [Tooltip("The angle in degrees from the target direction at which to turn on.")]
91 [Range(0, 180)]
92 public float OnAngle = 45; // degrees
93
99 [Tooltip("The angle in degrees from the target direction at which to turn off.")]
100 [Range(0, 180)]
101 public float OffAngle = 65; //degrees
102
106 [Header("")]
107 [Tooltip("Draw this detector's Gizmos, if any. (Gizmos must be on in Unity edtor, too.)")]
108 public bool ShowGizmos = true;
109
110 private IEnumerator watcherCoroutine;
111
112 private void OnValidate(){
113 if( OffAngle < OnAngle){
115 }
116 }
117
118 private void Awake () {
119 watcherCoroutine = palmWatcher();
120 }
121
122 private void OnEnable () {
123 StartCoroutine(watcherCoroutine);
124 }
125
126 private void OnDisable () {
127 StopCoroutine(watcherCoroutine);
128 Deactivate();
129 }
130
131 private IEnumerator palmWatcher() {
132 Hand hand;
133 Vector3 normal;
134 while(true){
135 if(HandModel != null){
136 hand = HandModel.GetLeapHand();
137 if(hand != null){
138 normal = hand.PalmNormal.ToVector3();
139 float angleTo = Vector3.Angle(normal, selectedDirection(hand.PalmPosition.ToVector3()));
140 if(angleTo <= OnAngle){
141 Activate();
142 } else if(angleTo > OffAngle) {
143 Deactivate();
144 }
145 }
146 }
147 yield return new WaitForSeconds(Period);
148 }
149 }
150
151 private Vector3 selectedDirection (Vector3 tipPosition) {
152 switch (PointingType) {
153 case PointingType.RelativeToHorizon:
154 Quaternion cameraRot = Camera.main.transform.rotation;
155 float cameraYaw = cameraRot.eulerAngles.y;
156 Quaternion rotator = Quaternion.AngleAxis(cameraYaw, Vector3.up);
157 return rotator * PointingDirection;
158 case PointingType.RelativeToCamera:
159 return Camera.main.transform.TransformDirection(PointingDirection);
160 case PointingType.RelativeToWorld:
161 return PointingDirection;
162 case PointingType.AtTarget:
163 if (TargetObject != null)
164 return TargetObject.position - tipPosition;
165 else return Vector3.zero;
166 default:
167 return PointingDirection;
168 }
169 }
170
171 #if UNITY_EDITOR
172 private void OnDrawGizmos(){
173 if(ShowGizmos && HandModel != null && HandModel.IsTracked){
174 Color centerColor;
175 if (IsActive) {
176 centerColor = OnColor;
177 } else {
178 centerColor = OffColor;
179 }
180 Hand hand = HandModel.GetLeapHand();
181 Utils.DrawCone(hand.PalmPosition.ToVector3(), hand.PalmNormal.ToVector3(), OnAngle, hand.PalmWidth, centerColor, 8);
182 Utils.DrawCone(hand.PalmPosition.ToVector3(), hand.PalmNormal.ToVector3(), OffAngle, hand.PalmWidth, LimitColor, 8);
183 Gizmos.color = DirectionColor;
184 Gizmos.DrawRay(hand.PalmPosition.ToVector3(), selectedDirection(hand.PalmPosition.ToVector3()));
185 }
186 }
187 #endif
188 }
189
201}
UnityEngine.Color Color
Definition: TestScript.cs:32
Vector PalmNormal
The normal vector to the palm. If your hand is flat, this vector will point downward,...
Definition: Hand.cs:184
virtual void Deactivate()
Definition: Detector.cs:66
Color DirectionColor
Definition: Detector.cs:77
virtual void Activate()
Definition: Detector.cs:54
abstract Hand GetLeapHand()