TrackingInfomationTips.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 System.Collections.Generic;
  10. using UnityEngine;
  11. namespace NRKernal.NRExamples
  12. {
  13. /// <summary> A tracking infomation tips. </summary>
  14. [HelpURL("https://developer.nreal.ai/develop/discover/introduction-nrsdk")]
  15. public class TrackingInfomationTips : SingletonBehaviour<TrackingInfomationTips>
  16. {
  17. /// <summary> Dictionary of tips. </summary>
  18. private Dictionary<TipType, GameObject> m_TipsDict = new Dictionary<TipType, GameObject>();
  19. /// <summary> Values that represent tip types. </summary>
  20. public enum TipType
  21. {
  22. /// <summary> An enum constant representing the un initialized option. </summary>
  23. UnInitialized,
  24. /// <summary> An enum constant representing the lost tracking option. </summary>
  25. LostTracking,
  26. /// <summary> An enum constant representing the none option. </summary>
  27. None
  28. }
  29. /// <summary> The tips layer. </summary>
  30. [Tooltip("Camera would only show the layer of TipsLayer when lost tracking.\n" +
  31. "Select the layer which you want to show when lost tracking.")]
  32. [SerializeField]
  33. private LayerMask m_TipsLayer;
  34. /// <summary> The current tip. </summary>
  35. private GameObject m_CurrentTip;
  36. /// <summary> The center camera. </summary>
  37. private Camera centerCamera;
  38. /// <summary> The origin layer l camera. </summary>
  39. private int originLayerLCam;
  40. /// <summary> The origin layer camera. </summary>
  41. private int originLayerRCam;
  42. /// <summary> Starts this object. </summary>
  43. void Start()
  44. {
  45. originLayerLCam = NRSessionManager.Instance.NRHMDPoseTracker.leftCamera.cullingMask;
  46. originLayerRCam = NRSessionManager.Instance.NRHMDPoseTracker.rightCamera.cullingMask;
  47. centerCamera = NRSessionManager.Instance.NRHMDPoseTracker.centerCamera;
  48. ShowTips(TipType.UnInitialized);
  49. }
  50. /// <summary> Executes the 'enable' action. </summary>
  51. private void OnEnable()
  52. {
  53. NRHMDPoseTracker.OnHMDLostTracking += OnHMDLostTracking;
  54. NRHMDPoseTracker.OnHMDPoseReady += OnHMDPoseReady;
  55. }
  56. /// <summary> Executes the 'disable' action. </summary>
  57. private void OnDisable()
  58. {
  59. NRHMDPoseTracker.OnHMDLostTracking -= OnHMDLostTracking;
  60. NRHMDPoseTracker.OnHMDPoseReady -= OnHMDPoseReady;
  61. }
  62. /// <summary> Executes the 'hmd pose ready' action. </summary>
  63. private void OnHMDPoseReady()
  64. {
  65. NRDebugger.Info("[NRHMDPoseTracker] OnHMDPoseReady");
  66. ShowTips(TipType.None);
  67. }
  68. /// <summary> Executes the 'hmd lost tracking' action. </summary>
  69. private void OnHMDLostTracking()
  70. {
  71. NRDebugger.Info("[NRHMDPoseTracker] OnHMDLostTracking:" + NRFrame.LostTrackingReason);
  72. ShowTips(TipType.LostTracking);
  73. }
  74. /// <summary> Shows the tips. </summary>
  75. /// <param name="type"> The type.</param>
  76. public void ShowTips(TipType type)
  77. {
  78. switch (type)
  79. {
  80. case TipType.UnInitialized:
  81. case TipType.LostTracking:
  82. GameObject go;
  83. m_TipsDict.TryGetValue(type, out go);
  84. int layer = LayerMaskToLayer(m_TipsLayer);
  85. if (go == null)
  86. {
  87. go = Instantiate(Resources.Load(type.ToString() + "Tip"), centerCamera.transform) as GameObject;
  88. m_TipsDict.Add(type, go);
  89. go.layer = layer;
  90. foreach (Transform child in go.transform)
  91. {
  92. child.gameObject.layer = layer;
  93. }
  94. }
  95. if (go != m_CurrentTip)
  96. {
  97. m_CurrentTip?.SetActive(false);
  98. go.SetActive(true);
  99. m_CurrentTip = go;
  100. }
  101. NRSessionManager.Instance.NRHMDPoseTracker.leftCamera.cullingMask = 1 << layer;
  102. NRSessionManager.Instance.NRHMDPoseTracker.rightCamera.cullingMask = 1 << layer;
  103. break;
  104. case TipType.None:
  105. if (m_CurrentTip != null)
  106. {
  107. m_CurrentTip.SetActive(false);
  108. }
  109. m_CurrentTip = null;
  110. NRSessionManager.Instance.NRHMDPoseTracker.leftCamera.cullingMask = originLayerLCam;
  111. NRSessionManager.Instance.NRHMDPoseTracker.rightCamera.cullingMask = originLayerRCam;
  112. break;
  113. default:
  114. break;
  115. }
  116. }
  117. /// <summary> Layer mask to layer. </summary>
  118. /// <param name="layerMask"> .</param>
  119. /// <returns> The last layer of the layer mask. </returns>
  120. public static int LayerMaskToLayer(LayerMask layerMask)
  121. {
  122. int layerNumber = 0;
  123. int layer = layerMask.value;
  124. while (layer > 0)
  125. {
  126. layer = layer >> 1;
  127. layerNumber++;
  128. }
  129. return layerNumber - 1;
  130. }
  131. /// <summary>
  132. /// Base OnDestroy method that destroys the Singleton's unique instance. Called by Unity when
  133. /// destroying a MonoBehaviour. Scripts that extend Singleton should be sure to call
  134. /// base.OnDestroy() to ensure the underlying static Instance reference is properly cleaned up. </summary>
  135. new void OnDestroy()
  136. {
  137. if (isDirty) return;
  138. if (m_TipsDict != null)
  139. {
  140. foreach (var item in m_TipsDict)
  141. {
  142. if (item.Value != null)
  143. {
  144. GameObject.Destroy(item.Value);
  145. }
  146. }
  147. }
  148. }
  149. }
  150. }