FocusManager.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using NRKernal;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. public class FocusManager : MonoBehaviour
  5. {
  6. private Transform m_HeadTransfrom;
  7. private Vector3 m_FocusPosition;
  8. RaycastHit hitResult;
  9. private FocusItem currentFocusItem;
  10. void Start()
  11. {
  12. m_HeadTransfrom = NRSessionManager.Instance.CenterCameraAnchor;
  13. //NRInput.ReticleVisualActive = false;
  14. //NRInput.LaserVisualActive = false;
  15. }
  16. private System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
  17. void Update()
  18. {
  19. if (Physics.Raycast(new Ray(m_HeadTransfrom.position, m_HeadTransfrom.forward), out hitResult, 100))
  20. {
  21. m_FocusPosition = m_HeadTransfrom.InverseTransformPoint(hitResult.point);
  22. var item = hitResult.collider.GetComponent<FocusItem>();
  23. if (item != null && currentFocusItem != item)
  24. {
  25. currentFocusItem?.OnOut();
  26. currentFocusItem = item;
  27. currentFocusItem.OnEnter();
  28. }
  29. #if USING_XR_SDK && !UNITY_EDITOR
  30. // Debug.LogFormat("SetFocusPlane position:{0} normal:{1}", m_FocusPosition.ToString(), hitResult.normal.ToString());
  31. NRSessionManager.Instance.XRDisplaySubsystem?.SetFocusPlane(m_FocusPosition,hitResult.normal,Vector3.zero);
  32. #else
  33. NRSessionManager.Instance.NRRenderer?.SetFocusDistance(m_FocusPosition.magnitude);
  34. #endif
  35. }
  36. else
  37. {
  38. currentFocusItem?.OnOut();
  39. currentFocusItem = null;
  40. }
  41. if (Time.frameCount % 100 == 0 || stopwatch.ElapsedMilliseconds >= 20)
  42. {
  43. Debug.Log("time cost a frame:" + stopwatch.ElapsedMilliseconds);
  44. }
  45. stopwatch.Reset();
  46. stopwatch.Start();
  47. }
  48. void OnDrawGizmos()
  49. {
  50. if (hitResult.collider != null)
  51. {
  52. Gizmos.DrawSphere(hitResult.point, 0.1f);
  53. Gizmos.DrawLine(m_HeadTransfrom.position, hitResult.point);
  54. }
  55. }
  56. }