SteamVR_GazeTracker.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using UnityEngine;
  3. public class SteamVR_GazeTracker : MonoBehaviour
  4. {
  5. public event GazeEventHandler GazeOn;
  6. public event GazeEventHandler GazeOff;
  7. private void Start()
  8. {
  9. }
  10. public virtual void OnGazeOn(GazeEventArgs e)
  11. {
  12. if (this.GazeOn != null)
  13. {
  14. this.GazeOn(this, e);
  15. }
  16. }
  17. public virtual void OnGazeOff(GazeEventArgs e)
  18. {
  19. if (this.GazeOff != null)
  20. {
  21. this.GazeOff(this, e);
  22. }
  23. }
  24. private void Update()
  25. {
  26. if (this.hmdTrackedObject == null)
  27. {
  28. SteamVR_TrackedObject[] array = UnityEngine.Object.FindObjectsOfType<SteamVR_TrackedObject>();
  29. foreach (SteamVR_TrackedObject steamVR_TrackedObject in array)
  30. {
  31. if (steamVR_TrackedObject.index == SteamVR_TrackedObject.EIndex.Hmd)
  32. {
  33. this.hmdTrackedObject = steamVR_TrackedObject.transform;
  34. break;
  35. }
  36. }
  37. }
  38. if (this.hmdTrackedObject)
  39. {
  40. Ray ray = new Ray(this.hmdTrackedObject.position, this.hmdTrackedObject.forward);
  41. Plane plane = new Plane(this.hmdTrackedObject.forward, base.transform.position);
  42. float d = 0f;
  43. if (plane.Raycast(ray, out d))
  44. {
  45. Vector3 a = this.hmdTrackedObject.position + this.hmdTrackedObject.forward * d;
  46. float num = Vector3.Distance(a, base.transform.position);
  47. if (num < this.gazeInCutoff && !this.isInGaze)
  48. {
  49. this.isInGaze = true;
  50. GazeEventArgs e;
  51. e.distance = num;
  52. this.OnGazeOn(e);
  53. }
  54. else if (num >= this.gazeOutCutoff && this.isInGaze)
  55. {
  56. this.isInGaze = false;
  57. GazeEventArgs e2;
  58. e2.distance = num;
  59. this.OnGazeOff(e2);
  60. }
  61. }
  62. }
  63. }
  64. public bool isInGaze;
  65. public float gazeInCutoff = 0.15f;
  66. public float gazeOutCutoff = 0.4f;
  67. private Transform hmdTrackedObject;
  68. }