/**************************************************************************** * Copyright 2019 Nreal Techonology Limited. All rights reserved. * * This file is part of NRSDK. * * https://www.nreal.ai/ * *****************************************************************************/ namespace NRKernal { using System; using System.Collections.Generic; using UnityEngine; /// A trackable observer. public class TrackableObserver : MonoBehaviour { /// Tracking delegate. /// The position. /// The qua. public delegate void TrackingDelegate(Vector3 pos, Quaternion qua); /// The found event. public event TrackingDelegate FoundEvent; /// The lost evnet. public event Action LostEvent; /// Type of the target. public TrackableType TargetType; /// The trackable behaviour. private NRTrackableBehaviour m_TrackableBehaviour; /// The temporary tracking images. private List m_TempTrackingImages = new List(); /// The temporary tracking plane. private List m_TempTrackingPlane = new List(); /// Values that represent trackable types. public enum TrackableType { /// An enum constant representing the trackable image option. TrackableImage, /// An enum constant representing the trackable plane option. TrackablePlane, } /// Use this for initialization. void Start() { m_TrackableBehaviour = GetComponent(); } /// Update is called once per frame. void Update() { if (TargetType == TrackableType.TrackableImage) { NRFrame.GetTrackables(m_TempTrackingImages, NRTrackableQueryFilter.All); foreach (var trackableImage in m_TempTrackingImages) { if (trackableImage.GetDataBaseIndex() == m_TrackableBehaviour.DatabaseIndex) { if (trackableImage.GetTrackingState() == TrackingState.Tracking) { FoundEvent?.Invoke(trackableImage.GetCenterPose().position, trackableImage.GetCenterPose().rotation); } else { LostEvent?.Invoke(); } break; } } } else if (TargetType == TrackableType.TrackablePlane) { NRFrame.GetTrackables(m_TempTrackingPlane, NRTrackableQueryFilter.All); foreach (var trackablePlane in m_TempTrackingPlane) { if (m_TrackableBehaviour.DatabaseIndex == -1) m_TrackableBehaviour.DatabaseIndex = trackablePlane.GetDataBaseIndex(); if (trackablePlane.GetDataBaseIndex() == m_TrackableBehaviour.DatabaseIndex) { if (trackablePlane.GetTrackingState() == TrackingState.Tracking) { FoundEvent?.Invoke(trackablePlane.GetCenterPose().position, trackablePlane.GetCenterPose().rotation); } else { LostEvent?.Invoke(); } break; } } } } } }