NREventCenter.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal.Experimental
  10. {
  11. using System.Collections.Generic;
  12. using UnityEngine;
  13. /// <summary> A nr event center. </summary>
  14. public class NREventCenter
  15. {
  16. /// <summary> Gets current raycast target. </summary>
  17. /// <returns> The current raycast target. </returns>
  18. public static GameObject GetCurrentRaycastTarget()
  19. {
  20. NRPointerRaycaster raycaster = GetRaycaster(NRInput.DomainHand);
  21. if (raycaster == null)
  22. return null;
  23. var result = raycaster.FirstRaycastResult();
  24. if (!result.isValid)
  25. return null;
  26. return result.gameObject;
  27. }
  28. /// <summary> Dictionary of raycasters. </summary>
  29. private static Dictionary<ControllerAnchorEnum, NRPointerRaycaster> raycasterDict;
  30. /// <summary> Gets a raycaster. </summary>
  31. /// <param name="handEnum"> The hand enum.</param>
  32. /// <returns> The raycaster. </returns>
  33. public static NRPointerRaycaster GetRaycaster(ControllerHandEnum handEnum)
  34. {
  35. if (raycasterDict == null)
  36. {
  37. raycasterDict = new Dictionary<ControllerAnchorEnum, NRPointerRaycaster>();
  38. NRPointerRaycaster _raycaster = NRInput.AnchorsHelper.GetAnchor(ControllerAnchorEnum.GazePoseTrackerAnchor).GetComponentInChildren<NRPointerRaycaster>(true);
  39. raycasterDict.Add(ControllerAnchorEnum.GazePoseTrackerAnchor, _raycaster);
  40. _raycaster = NRInput.AnchorsHelper.GetAnchor(ControllerAnchorEnum.RightLaserAnchor).GetComponentInChildren<NRPointerRaycaster>(true);
  41. raycasterDict.Add(ControllerAnchorEnum.RightLaserAnchor, _raycaster);
  42. _raycaster = NRInput.AnchorsHelper.GetAnchor(ControllerAnchorEnum.LeftLaserAnchor).GetComponentInChildren<NRPointerRaycaster>(true);
  43. raycasterDict.Add(ControllerAnchorEnum.LeftLaserAnchor, _raycaster);
  44. }
  45. NRPointerRaycaster raycaster = null;
  46. if (NRInput.RaycastMode == RaycastModeEnum.Gaze)
  47. raycasterDict.TryGetValue(ControllerAnchorEnum.GazePoseTrackerAnchor, out raycaster);
  48. else if (NRInput.DomainHand == ControllerHandEnum.Right)
  49. raycasterDict.TryGetValue(ControllerAnchorEnum.RightLaserAnchor, out raycaster);
  50. else if (NRInput.DomainHand == ControllerHandEnum.Left)
  51. raycasterDict.TryGetValue(ControllerAnchorEnum.LeftLaserAnchor, out raycaster);
  52. return raycaster;
  53. }
  54. }
  55. }