1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- /****************************************************************************
- * 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;
- /// <summary> Controller for TrackingImage example. </summary>
- [HelpURL("https://developer.nreal.ai/develop/unity/image-tracking")]
- public class TrackingImageExampleController : MonoBehaviour
- {
- /// <summary> A prefab for visualizing an TrackingImage. </summary>
- public TrackingImageVisualizer TrackingImageVisualizerPrefab;
- /// <summary> The overlay containing the fit to scan user guide. </summary>
- public GameObject FitToScanOverlay;
- /// <summary> The visualizers. </summary>
- private Dictionary<int, TrackingImageVisualizer> m_Visualizers
- = new Dictionary<int, TrackingImageVisualizer>();
- /// <summary> The temporary tracking images. </summary>
- private List<NRTrackableImage> m_TempTrackingImages = new List<NRTrackableImage>();
- /// <summary> Updates this object. </summary>
- 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<NRTrackableImage>(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);
- }
- }
- /// <summary> Enables the image tracking. </summary>
- public void EnableImageTracking()
- {
- var config = NRSessionManager.Instance.NRSessionBehaviour.SessionConfig;
- config.ImageTrackingMode = TrackableImageFindingMode.ENABLE;
- NRSessionManager.Instance.SetConfiguration(config);
- }
- /// <summary> Disables the image tracking. </summary>
- public void DisableImageTracking()
- {
- var config = NRSessionManager.Instance.NRSessionBehaviour.SessionConfig;
- config.ImageTrackingMode = TrackableImageFindingMode.DISABLE;
- NRSessionManager.Instance.SetConfiguration(config);
- }
- }
- }
|