PinchDetector.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using Leap.Unity.Attributes;
  3. using UnityEngine;
  4. namespace Leap.Unity
  5. {
  6. public class PinchDetector : AbstractHoldDetector
  7. {
  8. public bool IsPinching
  9. {
  10. get
  11. {
  12. return this.IsHolding;
  13. }
  14. }
  15. public bool DidStartPinch
  16. {
  17. get
  18. {
  19. return this.DidStartHold;
  20. }
  21. }
  22. public bool DidEndPinch
  23. {
  24. get
  25. {
  26. return this.DidRelease;
  27. }
  28. }
  29. protected virtual void OnValidate()
  30. {
  31. this.ActivateDistance = Mathf.Max(0f, this.ActivateDistance);
  32. this.DeactivateDistance = Mathf.Max(0f, this.DeactivateDistance);
  33. if (this.DeactivateDistance < this.ActivateDistance)
  34. {
  35. this.DeactivateDistance = this.ActivateDistance;
  36. }
  37. }
  38. protected override void ensureUpToDate()
  39. {
  40. if (Time.frameCount == this._lastUpdateFrame)
  41. {
  42. return;
  43. }
  44. this._lastUpdateFrame = Time.frameCount;
  45. this._didChange = false;
  46. Hand leapHand = this._handModel.GetLeapHand();
  47. if (leapHand == null || !this._handModel.IsTracked)
  48. {
  49. this.changeState(false);
  50. return;
  51. }
  52. this._distance = leapHand.PinchDistance * 0.001f;
  53. this._rotation = leapHand.Basis.CalculateRotation();
  54. this._position = ((leapHand.Fingers[0].TipPosition + leapHand.Fingers[1].TipPosition) * 0.5f).ToVector3();
  55. if (base.IsActive)
  56. {
  57. if (this._distance > this.DeactivateDistance)
  58. {
  59. this.changeState(false);
  60. }
  61. }
  62. else if (this._distance < this.ActivateDistance)
  63. {
  64. this.changeState(true);
  65. }
  66. if (base.IsActive)
  67. {
  68. this._lastPosition = this._position;
  69. this._lastRotation = this._rotation;
  70. this._lastDistance = this._distance;
  71. this._lastDirection = this._direction;
  72. this._lastNormal = this._normal;
  73. }
  74. if (this.ControlsTransform)
  75. {
  76. base.transform.position = this._position;
  77. base.transform.rotation = this._rotation;
  78. }
  79. }
  80. protected const float MM_TO_M = 0.001f;
  81. public float ActivateDistance = 0.03f;
  82. public float DeactivateDistance = 0.04f;
  83. [MinValue(0f)]
  84. [SerializeField]
  85. protected float _activatePinchDist = 0.03f;
  86. [MinValue(0f)]
  87. [SerializeField]
  88. protected float _deactivatePinchDist = 0.04f;
  89. protected bool _isPinching;
  90. protected float _lastPinchTime;
  91. protected float _lastUnpinchTime;
  92. protected Vector3 _pinchPos;
  93. protected Quaternion _pinchRotation;
  94. }
  95. }