PalmDirectionDetector.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections;
  3. using Leap.Unity.Attributes;
  4. using UnityEngine;
  5. namespace Leap.Unity
  6. {
  7. public class PalmDirectionDetector : Detector
  8. {
  9. private void OnValidate()
  10. {
  11. if (this.OffAngle < this.OnAngle)
  12. {
  13. this.OffAngle = this.OnAngle;
  14. }
  15. }
  16. private void Awake()
  17. {
  18. this.watcherCoroutine = this.palmWatcher();
  19. }
  20. private void OnEnable()
  21. {
  22. base.StartCoroutine(this.watcherCoroutine);
  23. }
  24. private void OnDisable()
  25. {
  26. base.StopCoroutine(this.watcherCoroutine);
  27. this.Deactivate();
  28. }
  29. private IEnumerator palmWatcher()
  30. {
  31. for (;;)
  32. {
  33. if (this.HandModel != null)
  34. {
  35. Hand hand = this.HandModel.GetLeapHand();
  36. if (hand != null)
  37. {
  38. Vector3 normal = hand.PalmNormal.ToVector3();
  39. float num = Vector3.Angle(normal, this.selectedDirection(hand.PalmPosition.ToVector3()));
  40. if (num <= this.OnAngle)
  41. {
  42. this.Activate();
  43. }
  44. else if (num > this.OffAngle)
  45. {
  46. this.Deactivate();
  47. }
  48. }
  49. }
  50. yield return new WaitForSeconds(this.Period);
  51. }
  52. yield break;
  53. }
  54. private Vector3 selectedDirection(Vector3 tipPosition)
  55. {
  56. switch (this.PointingType)
  57. {
  58. case PointingType.RelativeToCamera:
  59. return Camera.main.transform.TransformDirection(this.PointingDirection);
  60. case PointingType.RelativeToHorizon:
  61. {
  62. float y = Camera.main.transform.rotation.eulerAngles.y;
  63. Quaternion rotation = Quaternion.AngleAxis(y, Vector3.up);
  64. return rotation * this.PointingDirection;
  65. }
  66. case PointingType.RelativeToWorld:
  67. return this.PointingDirection;
  68. case PointingType.AtTarget:
  69. return this.TargetObject.position - tipPosition;
  70. default:
  71. return this.PointingDirection;
  72. }
  73. }
  74. [Tooltip("The interval in seconds at which to check this detector's conditions.")]
  75. public float Period = 0.1f;
  76. [AutoFind(AutoFindLocations.Parents)]
  77. [Tooltip("The hand model to watch. Set automatically if detector is on a hand.")]
  78. public IHandModel HandModel;
  79. [Tooltip("The target direction.")]
  80. public Vector3 PointingDirection = Vector3.forward;
  81. [Tooltip("How to treat the target direction.")]
  82. public PointingType PointingType = PointingType.RelativeToHorizon;
  83. [Tooltip("A target object(optional). Use PointingType.AtTarget")]
  84. public Transform TargetObject;
  85. [Tooltip("The angle in degrees from the target direction at which to turn on.")]
  86. [Range(0f, 360f)]
  87. public float OnAngle = 45f;
  88. [Tooltip("The angle in degrees from the target direction at which to turn off.")]
  89. [Range(0f, 360f)]
  90. public float OffAngle = 65f;
  91. private IEnumerator watcherCoroutine;
  92. }
  93. }