ImageTrackingAnchorTool.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.Persistence
  10. {
  11. using System;
  12. using UnityEngine;
  13. using System.Collections.Generic;
  14. /// <summary> An image tracking anchor tool. </summary>
  15. public class ImageTrackingAnchorTool : MonoBehaviour
  16. {
  17. /// <summary> Event queue for all listeners interested in OnAnchorLoaded events. </summary>
  18. public event Action<string, NRWorldAnchor> OnAnchorLoaded;
  19. /// <summary> Event queue for all listeners interested in OnAnchorPoseUpdate events. </summary>
  20. public event Action<Pose> OnAnchorPoseUpdate;
  21. /// <summary> The tracking image visualizer prefab. </summary>
  22. public TrackingImageVisualizerForAnchor TrackingImageVisualizerPrefab;
  23. /// <summary> Zero-based index of the used image. </summary>
  24. public int usedImageIndex = 0;
  25. /// <summary> The visualizer. </summary>
  26. TrackingImageVisualizerForAnchor visualizer;
  27. private bool m_IsStart = true;
  28. /// <summary> True to use map. </summary>
  29. public bool useMap = false;
  30. /// <summary> The anchor key. </summary>
  31. public string anchorKey;
  32. /// <summary> The anchor store. </summary>
  33. private NRWorldAnchorStore anchorStore;
  34. /// <summary> The current anchor. </summary>
  35. private NRWorldAnchor currentAnchor;
  36. void Start()
  37. {
  38. if (useMap)
  39. {
  40. this.LoadAnchor();
  41. }
  42. }
  43. private void LoadAnchor()
  44. {
  45. NRWorldAnchorStore.GetAsync((store) =>
  46. {
  47. NRDebugger.Info("[ImageTrackingAnchorTool] Get world anchor map success.");
  48. this.anchorStore = store;
  49. var keys = anchorStore.GetAllIds();
  50. if (keys == null)
  51. {
  52. NRDebugger.Info("[ImageTrackingAnchorTool] Can not get any keys...");
  53. return;
  54. }
  55. foreach (var item in keys)
  56. {
  57. if (item.Equals(anchorKey))
  58. {
  59. GameObject go = new GameObject(anchorKey);
  60. currentAnchor = anchorStore.Load(anchorKey, go);
  61. NRDebugger.Info("[ImageTrackingAnchorTool] Find the anchor :" + anchorKey);
  62. OnAnchorLoaded?.Invoke(anchorKey, currentAnchor);
  63. break;
  64. }
  65. }
  66. });
  67. }
  68. private void Update()
  69. {
  70. if (!m_IsStart)
  71. {
  72. return;
  73. }
  74. List<NRTrackableImage> trackableImages = new List<NRTrackableImage>();
  75. NRFrame.GetTrackables<NRTrackableImage>(trackableImages, NRTrackableQueryFilter.All);
  76. Pose pose;
  77. foreach (var image in trackableImages)
  78. {
  79. if (image.GetDataBaseIndex() == usedImageIndex)
  80. {
  81. pose = image.GetCenterPose();
  82. if (visualizer == null)
  83. {
  84. visualizer = (TrackingImageVisualizerForAnchor)Instantiate(TrackingImageVisualizerPrefab, pose.position, pose.rotation);
  85. visualizer.Image = image;
  86. if (useMap)
  87. {
  88. visualizer.OnConfirm += SaveAnchor;
  89. }
  90. }
  91. break;
  92. }
  93. }
  94. // Update pose.
  95. if (useMap)
  96. {
  97. if (currentAnchor != null && currentAnchor.GetTrackingState() == TrackingState.Tracking)
  98. {
  99. pose = currentAnchor.GetCenterPose();
  100. OnAnchorPoseUpdate?.Invoke(pose);
  101. }
  102. }
  103. else
  104. {
  105. if (visualizer != null && visualizer.Image.GetTrackingState() == TrackingState.Tracking)
  106. {
  107. pose = visualizer.Image.GetCenterPose();
  108. OnAnchorPoseUpdate?.Invoke(pose);
  109. }
  110. }
  111. }
  112. /// <summary> True to lock, false to unlock the save. </summary>
  113. private bool saveLock = false;
  114. /// <summary> Saves the anchor. </summary>
  115. private void SaveAnchor()
  116. {
  117. if (anchorStore == null || saveLock || visualizer == null || string.IsNullOrEmpty(anchorKey)
  118. || visualizer.Image.GetTrackingState() != TrackingState.Tracking)
  119. {
  120. NRDebugger.Warning("[ImageTrackingAnchorTool] Can not save the anchor!");
  121. return;
  122. }
  123. Invoke("Unlock", 1f);
  124. anchorStore.Delete(anchorKey);
  125. NRDebugger.Info("[ImageTrackingAnchorTool] Save the anchor.");
  126. // Avoid to frequent calls.
  127. saveLock = true;
  128. currentAnchor = anchorStore.AddAnchor(anchorKey,
  129. new Pose(visualizer.transform.position, visualizer.transform.rotation));
  130. OnAnchorLoaded?.Invoke(anchorKey, currentAnchor);
  131. }
  132. /// <summary> Unlocks this object. </summary>
  133. private void Unlock()
  134. {
  135. saveLock = false;
  136. }
  137. /// <summary> Switch trackable image. </summary>
  138. /// <param name="isopen"> True to isopen.</param>
  139. public void SwitchTrackableImage(bool isopen)
  140. {
  141. NRDebugger.Info("[ImageTrackingAnchorTool] SwitchTrackableImage:" + isopen);
  142. m_IsStart = isopen;
  143. visualizer?.gameObject.SetActive(isopen);
  144. //var config = NRSessionManager.Instance.NRSessionBehaviour.SessionConfig;
  145. //config.ImageTrackingMode = isopen ? TrackableImageFindingMode.ENABLE : TrackableImageFindingMode.DISABLE;
  146. //NRSessionManager.Instance.SetConfiguration(config);
  147. }
  148. }
  149. }