TrackableObserver.cs 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 System;
  12. using System.Collections.Generic;
  13. using UnityEngine;
  14. /// <summary> A trackable observer. </summary>
  15. public class TrackableObserver : MonoBehaviour
  16. {
  17. /// <summary> Tracking delegate. </summary>
  18. /// <param name="pos"> The position.</param>
  19. /// <param name="qua"> The qua.</param>
  20. public delegate void TrackingDelegate(Vector3 pos, Quaternion qua);
  21. /// <summary> The found event. </summary>
  22. public event TrackingDelegate FoundEvent;
  23. /// <summary> The lost evnet. </summary>
  24. public event Action LostEvent;
  25. /// <summary> Type of the target. </summary>
  26. public TrackableType TargetType;
  27. /// <summary> The trackable behaviour. </summary>
  28. private NRTrackableBehaviour m_TrackableBehaviour;
  29. /// <summary> The temporary tracking images. </summary>
  30. private List<NRTrackableImage> m_TempTrackingImages = new List<NRTrackableImage>();
  31. /// <summary> The temporary tracking plane. </summary>
  32. private List<NRTrackablePlane> m_TempTrackingPlane = new List<NRTrackablePlane>();
  33. /// <summary> Values that represent trackable types. </summary>
  34. public enum TrackableType
  35. {
  36. /// <summary> An enum constant representing the trackable image option. </summary>
  37. TrackableImage,
  38. /// <summary> An enum constant representing the trackable plane option. </summary>
  39. TrackablePlane,
  40. }
  41. /// <summary> Use this for initialization. </summary>
  42. void Start()
  43. {
  44. m_TrackableBehaviour = GetComponent<NRTrackableBehaviour>();
  45. }
  46. /// <summary> Update is called once per frame. </summary>
  47. void Update()
  48. {
  49. if (TargetType == TrackableType.TrackableImage)
  50. {
  51. NRFrame.GetTrackables<NRTrackableImage>(m_TempTrackingImages, NRTrackableQueryFilter.All);
  52. foreach (var trackableImage in m_TempTrackingImages)
  53. {
  54. if (trackableImage.GetDataBaseIndex() == m_TrackableBehaviour.DatabaseIndex)
  55. {
  56. if (trackableImage.GetTrackingState() == TrackingState.Tracking)
  57. {
  58. FoundEvent?.Invoke(trackableImage.GetCenterPose().position, trackableImage.GetCenterPose().rotation);
  59. }
  60. else
  61. {
  62. LostEvent?.Invoke();
  63. }
  64. break;
  65. }
  66. }
  67. }
  68. else if (TargetType == TrackableType.TrackablePlane)
  69. {
  70. NRFrame.GetTrackables<NRTrackablePlane>(m_TempTrackingPlane, NRTrackableQueryFilter.All);
  71. foreach (var trackablePlane in m_TempTrackingPlane)
  72. {
  73. if (m_TrackableBehaviour.DatabaseIndex == -1)
  74. m_TrackableBehaviour.DatabaseIndex = trackablePlane.GetDataBaseIndex();
  75. if (trackablePlane.GetDataBaseIndex() == m_TrackableBehaviour.DatabaseIndex)
  76. {
  77. if (trackablePlane.GetTrackingState() == TrackingState.Tracking)
  78. {
  79. FoundEvent?.Invoke(trackablePlane.GetCenterPose().position, trackablePlane.GetCenterPose().rotation);
  80. }
  81. else
  82. {
  83. LostEvent?.Invoke();
  84. }
  85. break;
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }