MarkerDetecter.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 marker detecter. </summary>
  14. public class MarkerDetecter : MonoBehaviour
  15. {
  16. /// <summary> A prefab for tracking and visualizing detected markers. </summary>
  17. public GameObject DetectedMarkerPrefab;
  18. /// <summary>
  19. /// A list to hold new planes NRSDK began tracking in the current frame. This object is used
  20. /// across the application to avoid per-frame allocations. </summary>
  21. private List<NRTrackableImage> m_NewMarkers = new List<NRTrackableImage>();
  22. /// <summary> Updates this object. </summary>
  23. public void Update()
  24. {
  25. NRFrame.GetTrackables<NRTrackableImage>(m_NewMarkers, NRTrackableQueryFilter.New);
  26. for (int i = 0; i < m_NewMarkers.Count; i++)
  27. {
  28. NRDebugger.Info("[MarkerDetecter] Get New TrackableImages!! " + m_NewMarkers[i].TrackableNativeHandle);
  29. // Instantiate a visualization marker.
  30. NRAnchor anchor = m_NewMarkers[i].CreateAnchor();
  31. GameObject markerObject = Instantiate(DetectedMarkerPrefab, Vector3.zero, Quaternion.identity, anchor.transform);
  32. markerObject.GetComponent<TrackableImageBehaviour>().Initialize(m_NewMarkers[i]);
  33. }
  34. }
  35. /// <summary> Switch image tracking mode. </summary>
  36. /// <param name="on"> True to on.</param>
  37. public void SwitchImageTrackingMode(bool on)
  38. {
  39. var config = NRSessionManager.Instance.NRSessionBehaviour.SessionConfig;
  40. config.ImageTrackingMode = on ? TrackableImageFindingMode.ENABLE : TrackableImageFindingMode.DISABLE;
  41. NRSessionManager.Instance.SetConfiguration(config);
  42. }
  43. }
  44. }