NRLaserReticle.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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
  10. {
  11. using UnityEngine;
  12. /// <summary> A nr laser reticle. </summary>
  13. public class NRLaserReticle : MonoBehaviour
  14. {
  15. /// <summary> Values that represent reticle states. </summary>
  16. public enum ReticleState
  17. {
  18. /// <summary> An enum constant representing the hide option. </summary>
  19. Hide,
  20. /// <summary> An enum constant representing the normal option. </summary>
  21. Normal,
  22. /// <summary> An enum constant representing the hover option. </summary>
  23. Hover,
  24. }
  25. /// <summary> The raycaster. </summary>
  26. [SerializeField]
  27. private NRPointerRaycaster m_Raycaster;
  28. /// <summary> The default visual. </summary>
  29. [SerializeField]
  30. private GameObject m_DefaultVisual;
  31. /// <summary> The hover visual. </summary>
  32. [SerializeField]
  33. private GameObject m_HoverVisual;
  34. /// <summary> The hit target. </summary>
  35. private GameObject m_HitTarget;
  36. /// <summary> True if is visible, false if not. </summary>
  37. private bool m_IsVisible = true;
  38. /// <summary> The default distance. </summary>
  39. public float defaultDistance = 2.5f;
  40. /// <summary> The reticle size ratio. </summary>
  41. public float reticleSizeRatio = 0.02f;
  42. /// <summary> Gets the camera root. </summary>
  43. /// <value> The m camera root. </value>
  44. private Transform m_CameraRoot
  45. {
  46. get
  47. {
  48. return NRInput.CameraCenter;
  49. }
  50. }
  51. /// <summary> Gets the hit target. </summary>
  52. /// <value> The hit target. </value>
  53. public GameObject HitTarget
  54. {
  55. get
  56. {
  57. return m_HitTarget;
  58. }
  59. }
  60. /// <summary> Awakes this object. </summary>
  61. private void Awake()
  62. {
  63. SwitchReticleState(ReticleState.Hide);
  64. defaultDistance = Mathf.Clamp(defaultDistance, m_Raycaster.NearDistance, m_Raycaster.FarDistance);
  65. }
  66. /// <summary> Late update. </summary>
  67. protected virtual void LateUpdate()
  68. {
  69. if (!m_IsVisible || !NRInput.ReticleVisualActive)
  70. {
  71. SwitchReticleState(ReticleState.Hide);
  72. return;
  73. }
  74. var result = m_Raycaster.FirstRaycastResult();
  75. var points = m_Raycaster.BreakPoints;
  76. var pointCount = points.Count;
  77. if (result.isValid)
  78. {
  79. SwitchReticleState(ReticleState.Hover);
  80. transform.position = result.worldPosition;
  81. transform.rotation = Quaternion.LookRotation(result.worldNormal, m_Raycaster.transform.forward);
  82. m_HitTarget = result.gameObject;
  83. }
  84. else
  85. {
  86. SwitchReticleState(ReticleState.Normal);
  87. if (pointCount != 0)
  88. {
  89. transform.localPosition = Vector3.forward * defaultDistance;
  90. transform.localRotation = Quaternion.identity;
  91. }
  92. m_HitTarget = null;
  93. }
  94. if (m_CameraRoot)
  95. transform.localScale = Vector3.one * reticleSizeRatio * (transform.position - m_CameraRoot.transform.position).magnitude;
  96. }
  97. /// <summary> Executes the 'disable' action. </summary>
  98. private void OnDisable()
  99. {
  100. SwitchReticleState(ReticleState.Hide);
  101. }
  102. /// <summary> Switch reticle state. </summary>
  103. /// <param name="state"> The state.</param>
  104. private void SwitchReticleState(ReticleState state)
  105. {
  106. switch (state)
  107. {
  108. case ReticleState.Hide:
  109. m_DefaultVisual.SetActive(false);
  110. m_HoverVisual.SetActive(false);
  111. break;
  112. case ReticleState.Normal:
  113. m_DefaultVisual.SetActive(true);
  114. m_HoverVisual.SetActive(false);
  115. break;
  116. case ReticleState.Hover:
  117. m_DefaultVisual.SetActive(false);
  118. m_HoverVisual.SetActive(true);
  119. break;
  120. default:
  121. break;
  122. }
  123. }
  124. /// <summary> Sets a visible. </summary>
  125. /// <param name="isVisible"> True if is visible, false if not.</param>
  126. public void SetVisible(bool isVisible)
  127. {
  128. this.m_IsVisible = isVisible;
  129. }
  130. }
  131. }