GazeWatcher.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class GazeWatcher {
  5. /**
  6. * Given a camera, tracks its position and which object is in its gaze
  7. *
  8. * Usage:
  9. *
  10. * var watcher = new GazeWatcher(Camera.main);
  11. * watcher.position // Vector3 position of the camera
  12. * watcher.direction // Vector3 direction normal of where the camera is looking
  13. */
  14. // Handlers
  15. public delegate void GazeWatcherHandler(GazeWatcher gazeWatcher);
  16. // Properties
  17. private GameLooper _looper;
  18. private RaycastHit _lastHit;
  19. private Camera _camera;
  20. private Vector3 _position;
  21. private Vector3 _direction;
  22. private bool _isHitting;
  23. private Vector3 _hitPosition;
  24. private Vector3 _hitDirection;
  25. private GameObject _hitObject;
  26. // Public
  27. public event GazeWatcherHandler onObjectHitStart;
  28. public event GazeWatcherHandler onObjectHitMove;
  29. public event GazeWatcherHandler onObjectHitEnd;
  30. // ================================================================================================================
  31. // CONSTRUCTOR ----------------------------------------------------------------------------------------------------
  32. public GazeWatcher(Camera camera) {
  33. _camera = camera;
  34. _looper = new GameLooper();
  35. _looper.onUpdate += onUpdate;
  36. }
  37. // ================================================================================================================
  38. // PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
  39. public Vector3 position {
  40. get {
  41. return _position;
  42. }
  43. }
  44. public Vector3 direction {
  45. get {
  46. return _direction;
  47. }
  48. }
  49. public bool isHitting {
  50. get {
  51. return _isHitting;
  52. }
  53. }
  54. public Vector3 hitPosition {
  55. get {
  56. return _hitPosition;
  57. }
  58. }
  59. public Vector3 hitDirection {
  60. get {
  61. return _hitDirection;
  62. }
  63. }
  64. public GameObject hitObject {
  65. get {
  66. return _hitObject;
  67. }
  68. }
  69. // ================================================================================================================
  70. // INTERNAL INTERFACE ---------------------------------------------------------------------------------------------
  71. private void onUpdate(GameLooper gameLooper) {
  72. // Do a raycast into the world based on camera position and orientation
  73. if (_camera == null) return;
  74. _position = _camera.transform.position;
  75. _direction = _camera.transform.forward;
  76. bool didHit = Physics.Raycast(_position, _direction, out _lastHit);
  77. if ((!didHit || _lastHit.transform.gameObject != _hitObject) && _isHitting) {
  78. // Ended a hit
  79. if (onObjectHitEnd != null) onObjectHitEnd(this);
  80. _isHitting = false;
  81. _hitPosition = Vector3.zero;
  82. _hitDirection = Vector3.zero;
  83. _hitObject = null;
  84. }
  85. if (didHit) {
  86. // It is hitting
  87. _hitPosition = _lastHit.point;
  88. _hitDirection = _lastHit.normal;
  89. if (!_isHitting) {
  90. // Started a new hit
  91. _isHitting = true;
  92. _hitObject = _lastHit.transform.gameObject;
  93. if (onObjectHitStart != null) onObjectHitStart(this);
  94. }
  95. if (onObjectHitMove != null) onObjectHitMove(this);
  96. }
  97. }
  98. }