SetFocusPlaneTool.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. using UnityEngine;
  10. namespace NRKernal.NRExamples
  11. {
  12. public class SetFocusPlaneTool : MonoBehaviour
  13. {
  14. private Transform m_HeadTransfrom;
  15. private Vector3 m_FocusPosition;
  16. RaycastHit hitResult;
  17. private const float MaxDistance = 500;
  18. void Update()
  19. {
  20. if (m_HeadTransfrom == null)
  21. {
  22. m_HeadTransfrom = NRSessionManager.Instance.CenterCameraAnchor;
  23. if (m_HeadTransfrom == null)
  24. {
  25. return;
  26. }
  27. }
  28. if (Physics.Raycast(new Ray(m_HeadTransfrom.position, m_HeadTransfrom.forward), out hitResult, MaxDistance))
  29. {
  30. m_FocusPosition = m_HeadTransfrom.InverseTransformPoint(hitResult.point);
  31. NRSessionManager.Instance.NRRenderer?.SetFocusDistance(m_FocusPosition.magnitude);
  32. }
  33. }
  34. }
  35. }