Tanoda
PinchDetector.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;
11using UnityEngine.Serialization;
12
13namespace Leap.Unity {
14
20 protected const float MM_TO_M = 0.001f;
21
22 [Tooltip("The distance at which to enter the pinching state.")]
23 [Header("Distance Settings")]
24 [MinValue(0)]
25 [Units("meters")]
26 [FormerlySerializedAs("_activatePinchDist")]
27 public float ActivateDistance = .03f; //meters
28 [Tooltip("The distance at which to leave the pinching state.")]
29 [MinValue(0)]
30 [Units("meters")]
31 [FormerlySerializedAs("_deactivatePinchDist")]
32 public float DeactivateDistance = .04f; //meters
33
34 public bool IsPinching { get { return this.IsHolding; } }
35 public bool DidStartPinch { get { return this.DidStartHold; } }
36 public bool DidEndPinch { get { return this.DidRelease; } }
37
38 protected bool _isPinching = false;
39
40 protected float _lastPinchTime = 0.0f;
41 protected float _lastUnpinchTime = 0.0f;
42
43 protected Vector3 _pinchPos;
44 protected Quaternion _pinchRotation;
45
46 protected virtual void OnValidate() {
47 ActivateDistance = Mathf.Max(0, ActivateDistance);
49
50 //Activate value cannot be less than deactivate value
53 }
54 }
55
56 private float GetPinchDistance(Hand hand) {
57 var indexTipPosition = hand.GetIndex().TipPosition.ToVector3();
58 var thumbTipPosition = hand.GetThumb().TipPosition.ToVector3();
59 return Vector3.Distance(indexTipPosition, thumbTipPosition);
60 }
61
62 protected override void ensureUpToDate() {
63 if (Time.frameCount == _lastUpdateFrame) {
64 return;
65 }
66 _lastUpdateFrame = Time.frameCount;
67
68 _didChange = false;
69
71
72 if (hand == null || !_handModel.IsTracked) {
73 changeState(false);
74 return;
75 }
76
77 _distance = GetPinchDistance(hand);
78 _rotation = hand.Basis.CalculateRotation();
79 _position = ((hand.Fingers[0].TipPosition + hand.Fingers[1].TipPosition) * .5f).ToVector3();
80
81 if (IsActive) {
83 changeState(false);
84 //return;
85 }
86 } else {
88 changeState(true);
89 }
90 }
91
92 if (IsActive) {
98 }
100 transform.position = _position;
101 transform.rotation = _rotation;
102 }
103 }
104
105#if UNITY_EDITOR
106 protected override void OnDrawGizmos () {
107 if (ShowGizmos && _handModel != null && _handModel.IsTracked) {
108 Color centerColor = Color.clear;
109 Vector3 centerPosition = Vector3.zero;
110 Quaternion circleRotation = Quaternion.identity;
111 if (IsHolding) {
112 centerColor = Color.green;
113 centerPosition = Position;
114 circleRotation = Rotation;
115 } else {
116 Hand hand = _handModel.GetLeapHand();
117 if (hand != null) {
118 Finger thumb = hand.Fingers[0];
119 Finger index = hand.Fingers[1];
120 centerColor = Color.red;
121 centerPosition = ((thumb.Bone(Bone.BoneType.TYPE_DISTAL).NextJoint + index.Bone(Bone.BoneType.TYPE_DISTAL).NextJoint) / 2).ToVector3();
122 circleRotation = hand.Basis.CalculateRotation();
123 }
124 }
125 Vector3 axis;
126 float angle;
127 circleRotation.ToAngleAxis(out angle, out axis);
128 Utils.DrawCircle(centerPosition, axis, ActivateDistance / 2, centerColor);
129 Utils.DrawCircle(centerPosition, axis, DeactivateDistance / 2, Color.blue);
130 }
131 }
132 #endif
133 }
134}
UnityEngine.Color Color
Definition: TestScript.cs:32
The Bone class represents a tracked bone.
Definition: Bone.cs:26
BoneType
Enumerates the type of bones.
Definition: Bone.cs:168
Vector NextJoint
The end of the bone, closest to the finger tip. In anatomical terms, this is the distal end of the bo...
Definition: Bone.cs:90
The Finger class represents a tracked finger.
Definition: Finger.cs:20
Bone Bone(Bone.BoneType boneIx)
The bone at a given bone index on this finger.
Definition: Finger.cs:79
The Hand class reports the physical characteristics of a detected hand.
Definition: Hand.cs:26
LeapTransform Basis
The transform of the hand.
Definition: Hand.cs:204
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 changeState(bool shouldBeActive)
virtual bool IsHolding
Returns whether or not the dectector is currently detecting a pinch or grab.
Vector3 Position
Returns the position value of the detected pinch or grab. If a pinch or grab is not currently being d...
virtual bool DidStartHold
Returns whether or not the value of IsHolding changed to true between this frame and the previous.
Quaternion Rotation
Returns the rotation value of the detected pinch or grab. If a pinch or grab is not currently being d...
virtual bool DidRelease
Returns whether or not the value of IsHolding changed to false between this frame and the previous.
abstract Hand GetLeapHand()
A basic utility class to aid in creating pinch based actions. Once linked with a HandModelBase,...
virtual void OnValidate()
override void ensureUpToDate()