/**************************************************************************** * Copyright 2019 Nreal Techonology Limited. All rights reserved. * * This file is part of NRSDK. * * https://www.nreal.ai/ * *****************************************************************************/ namespace NRKernal.NRExamples { using System.Collections.Generic; using UnityEngine; /// Controller for TrackingImage example. [HelpURL("https://developer.nreal.ai/develop/unity/image-tracking")] public class TrackingImageExampleController : MonoBehaviour { /// A prefab for visualizing an TrackingImage. public TrackingImageVisualizer TrackingImageVisualizerPrefab; /// The overlay containing the fit to scan user guide. public GameObject FitToScanOverlay; /// The visualizers. private Dictionary m_Visualizers = new Dictionary(); /// The temporary tracking images. private List m_TempTrackingImages = new List(); /// Updates this object. public void Update() { #if !UNITY_EDITOR // Check that motion tracking is tracking. if (NRFrame.SessionStatus != SessionState.Running) { return; } #endif // Get updated augmented images for this frame. NRFrame.GetTrackables(m_TempTrackingImages, NRTrackableQueryFilter.New); // Create visualizers and anchors for updated augmented images that are tracking and do not previously // have a visualizer. Remove visualizers for stopped images. foreach (var image in m_TempTrackingImages) { TrackingImageVisualizer visualizer = null; m_Visualizers.TryGetValue(image.GetDataBaseIndex(), out visualizer); if (image.GetTrackingState() != TrackingState.Stopped && visualizer == null) { NRDebugger.Info("Create new TrackingImageVisualizer!"); // Create an anchor to ensure that NRSDK keeps tracking this augmented image. visualizer = (TrackingImageVisualizer)Instantiate(TrackingImageVisualizerPrefab, image.GetCenterPose().position, image.GetCenterPose().rotation); visualizer.Image = image; visualizer.transform.parent = transform; m_Visualizers.Add(image.GetDataBaseIndex(), visualizer); } else if (image.GetTrackingState() == TrackingState.Stopped && visualizer != null) { m_Visualizers.Remove(image.GetDataBaseIndex()); Destroy(visualizer.gameObject); } FitToScanOverlay.SetActive(false); } } /// Enables the image tracking. public void EnableImageTracking() { var config = NRSessionManager.Instance.NRSessionBehaviour.SessionConfig; config.ImageTrackingMode = TrackableImageFindingMode.ENABLE; NRSessionManager.Instance.SetConfiguration(config); } /// Disables the image tracking. public void DisableImageTracking() { var config = NRSessionManager.Instance.NRSessionBehaviour.SessionConfig; config.ImageTrackingMode = TrackableImageFindingMode.DISABLE; NRSessionManager.Instance.SetConfiguration(config); } } }