SteamVR_GazeTracker.cs 1.8 KB

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