TrackingImageExampleController.cs 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.NRExamples
  10. {
  11. using System.Collections.Generic;
  12. using UnityEngine;
  13. /// <summary> Controller for TrackingImage example. </summary>
  14. [HelpURL("https://developer.nreal.ai/develop/unity/image-tracking")]
  15. public class TrackingImageExampleController : MonoBehaviour
  16. {
  17. /// <summary> A prefab for visualizing an TrackingImage. </summary>
  18. public TrackingImageVisualizer TrackingImageVisualizerPrefab;
  19. /// <summary> The overlay containing the fit to scan user guide. </summary>
  20. public GameObject FitToScanOverlay;
  21. /// <summary> The visualizers. </summary>
  22. private Dictionary<int, TrackingImageVisualizer> m_Visualizers
  23. = new Dictionary<int, TrackingImageVisualizer>();
  24. /// <summary> The temporary tracking images. </summary>
  25. private List<NRTrackableImage> m_TempTrackingImages = new List<NRTrackableImage>();
  26. /// <summary> Updates this object. </summary>
  27. public void Update()
  28. {
  29. #if !UNITY_EDITOR
  30. // Check that motion tracking is tracking.
  31. if (NRFrame.SessionStatus != SessionState.Running)
  32. {
  33. return;
  34. }
  35. #endif
  36. // Get updated augmented images for this frame.
  37. NRFrame.GetTrackables<NRTrackableImage>(m_TempTrackingImages, NRTrackableQueryFilter.New);
  38. // Create visualizers and anchors for updated augmented images that are tracking and do not previously
  39. // have a visualizer. Remove visualizers for stopped images.
  40. foreach (var image in m_TempTrackingImages)
  41. {
  42. TrackingImageVisualizer visualizer = null;
  43. m_Visualizers.TryGetValue(image.GetDataBaseIndex(), out visualizer);
  44. if (image.GetTrackingState() != TrackingState.Stopped && visualizer == null)
  45. {
  46. NRDebugger.Info("Create new TrackingImageVisualizer!");
  47. // Create an anchor to ensure that NRSDK keeps tracking this augmented image.
  48. visualizer = (TrackingImageVisualizer)Instantiate(TrackingImageVisualizerPrefab, image.GetCenterPose().position, image.GetCenterPose().rotation);
  49. visualizer.Image = image;
  50. visualizer.transform.parent = transform;
  51. m_Visualizers.Add(image.GetDataBaseIndex(), visualizer);
  52. }
  53. else if (image.GetTrackingState() == TrackingState.Stopped && visualizer != null)
  54. {
  55. m_Visualizers.Remove(image.GetDataBaseIndex());
  56. Destroy(visualizer.gameObject);
  57. }
  58. FitToScanOverlay.SetActive(false);
  59. }
  60. }
  61. /// <summary> Enables the image tracking. </summary>
  62. public void EnableImageTracking()
  63. {
  64. var config = NRSessionManager.Instance.NRSessionBehaviour.SessionConfig;
  65. config.ImageTrackingMode = TrackableImageFindingMode.ENABLE;
  66. NRSessionManager.Instance.SetConfiguration(config);
  67. }
  68. /// <summary> Disables the image tracking. </summary>
  69. public void DisableImageTracking()
  70. {
  71. var config = NRSessionManager.Instance.NRSessionBehaviour.SessionConfig;
  72. config.ImageTrackingMode = TrackableImageFindingMode.DISABLE;
  73. NRSessionManager.Instance.SetConfiguration(config);
  74. }
  75. }
  76. }