Search Unity

How to hide the prefab when the augmented image is not shown in ARcore?

Discussion in 'AR' started by zyonneo, Nov 27, 2018.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    Hi,I was testing Augmented Image example scene .The problem I faced was I cannot hide the Prefab once it comes.When I show mu camera to Augmented Image which I set ,the prefab instantiates. Once it comes, It wont go even after I remove the image.How to stop that.I only want the prefab to come when the corresponding image is present.


    Code (CSharp):
    1. namespace GoogleARCore.Examples.AugmentedImage
    2. {
    3.     using System.Collections.Generic;
    4.     using System.Runtime.InteropServices;
    5.     using GoogleARCore;
    6.     using UnityEngine;
    7.     using UnityEngine.UI;
    8.  
    9.     /// <summary>
    10.     /// Controller for AugmentedImage example.
    11.     /// </summary>
    12.     public class AugmentedImageExampleController : MonoBehaviour
    13.     {
    14.         /// <summary>
    15.         /// A prefab for visualizing an AugmentedImage.
    16.         /// </summary>
    17.         //  public AugmentedImageVisualizer  AugmentedImageVisualizerPrefab;
    18.         public List<AugmentVisualize> prefabs = new List<AugmentVisualize>();
    19.  
    20.         /// <summary>
    21.         /// The overlay containing the fit to scan user guide.
    22.         /// </summary>
    23.         public GameObject FitToScanOverlay;
    24.  
    25.         private Dictionary<int, AugmentVisualize> m_Visualizers
    26.             = new Dictionary<int, AugmentVisualize>();
    27.  
    28.         private List<AugmentedImage> m_TempAugmentedImages = new List<AugmentedImage>();
    29.  
    30.         /// <summary>
    31.         /// The Unity Update method.
    32.         /// </summary>
    33.         public void Update()
    34.         {
    35.             // Exit the app when the 'back' button is pressed.
    36.             if (Input.GetKey(KeyCode.Escape))
    37.             {
    38.                 Application.Quit();
    39.             }
    40.  
    41.             // Check that motion tracking is tracking.
    42.             if (Session.Status != SessionStatus.Tracking)
    43.             {
    44.                 return;
    45.             }
    46.  
    47.             // Get updated augmented images for this frame.
    48.             Session.GetTrackables<AugmentedImage>(m_TempAugmentedImages, TrackableQueryFilter.Updated);
    49.  
    50.             // Create visualizers and anchors for updated augmented images that are tracking and do not previously
    51.             // have a visualizer. Remove visualizers for stopped images.
    52.             foreach (var image in m_TempAugmentedImages)
    53.             {
    54.                 AugmentVisualize visualizer = null;
    55.                 m_Visualizers.TryGetValue(image.DatabaseIndex, out visualizer);
    56.                 if (image.TrackingState == TrackingState.Tracking && visualizer == null)
    57.                 {
    58.                     // Create an anchor to ensure that ARCore keeps tracking this augmented image.
    59.                     Anchor anchor = image.CreateAnchor(image.CenterPose);
    60.                     visualizer = (AugmentVisualize)Instantiate(prefabs[image.DatabaseIndex], anchor.transform);
    61.                     visualizer.Image = image;
    62.                     m_Visualizers.Add(image.DatabaseIndex, visualizer);
    63.                 }
    64.                 else if (image.TrackingState == TrackingState.Stopped && visualizer != null)
    65.                 {
    66.                     m_Visualizers.Remove(image.DatabaseIndex);
    67.                     GameObject.Destroy(visualizer.gameObject);
    68.                 }
    69.             }
    70.  
    71.             // Show the fit-to-scan overlay if there are no images that are Tracking.
    72.             foreach (var visualizer in m_Visualizers.Values)
    73.             {
    74.                 if (visualizer.Image.TrackingState == TrackingState.Tracking)
    75.                 {
    76.                     FitToScanOverlay.SetActive(false);
    77.                     return;
    78.                 }
    79.             }
    80.  
    81.             FitToScanOverlay.SetActive(true);
    82.         }
    83.     }
    84. }


    Code (CSharp):
    1. public class AugmentVisualize : MonoBehaviour {
    2.  
    3.  
    4.     public AugmentedImage Image;
    5.  
    6.     public GameObject [] Listprefabs;
    7.  
    8.     public Text Showtext;
    9.  
    10.     // Use this for initialization
    11.     void Start ()
    12.     {
    13.      
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update ()
    18.     {
    19.  
    20.         if (Image == null || Image.TrackingState == TrackingState.Paused)
    21.         {
    22.             //Listprefabs[Image.DatabaseIndex].SetActive(false);
    23.             Listprefabs[0].SetActive(false);
    24.             Listprefabs[1].SetActive(false);
    25.             Showtext.text = "Paused";
    26.  
    27.             return;
    28.         }
    29.  
    30.         else if(Image == null || Image.TrackingState == TrackingState.Stopped)
    31.             {
    32.             //Listprefabs[Image.DatabaseIndex].SetActive(false);
    33.             Listprefabs[0].SetActive(false);
    34.             Listprefabs[1].SetActive(false);
    35.             Showtext.text = "Stopped";
    36.             return;
    37.         }
    38.  
    39.  
    40.         else if (Image == null || Image.TrackingState != TrackingState.Tracking)
    41.         {
    42.             //Listprefabs[Image.DatabaseIndex].SetActive(false);
    43.             Listprefabs[0].SetActive(false);
    44.             Listprefabs[1].SetActive(false);
    45.             Showtext.text = "not tracking";
    46.             return;
    47.         }
    48.  
    49.         else if(Image == null || Image.TrackingState == TrackingState.Tracking)
    50.         {
    51.             Listprefabs[Image.DatabaseIndex].SetActive(true);
    52.             Showtext.text = "Prefab showing";
    53.         }
    54.  
    55.     }
    56. }
     
    alikanat likes this.
  2. mwalus

    mwalus

    Joined:
    Dec 7, 2017
    Posts:
    25
    Hello.
    I have the same problem. Image tracking state never gets a value of TrackingState.Stopped.
    Even running example in "AugmentedImage.scene" from "Examples" in package, image (ex. 0000/earth photo) gets the frame corners placed correctly in space and I can move the phone to look around. But when I remove image from camera view frame corners still stays on the screen.
    I'm using: Unity 2018.3 and arcore-unity-sdk-v1.6.0 and running an app in Galaxy S8 with Android 8.0 and latest ARCore from Play Store.

    I found this info and suggested "workaround" here.
     
    Last edited: Dec 28, 2018
  3. amit-chai

    amit-chai

    Joined:
    Jul 2, 2012
    Posts:
    80
    Any solution??
     
  4. GuruJeya14

    GuruJeya14

    Joined:
    Jun 29, 2017
    Posts:
    9
    Use the following code to achieve the same scenario as of TrackingState.Tracking and TrackingState.Stopped. Works fine, and tested with different images.
    Its just here I've used AugmentedImageTrackingMethod.FullTracking and AugmentedImageTrackingMethod.LastKnownPose.

    Reference is from here.

    Code (CSharp):
    1. //Marker is being tracked
    2.             if(image.TrackingState == TrackingState.Tracking && viz == null && image.TrackingMethod == AugmentedImageTrackingMethod.FullTracking)
    3.             {
    4.                 AddVisualizer(image);
    5.             }
    6.             //marker is not tracked in camera output
    7.             else if(image.TrackingMethod == AugmentedImageTrackingMethod.LastKnownPose && viz != null)
    8.             {
    9.                 RemoveVisualizer(image, viz);
    10.             }
     
    Last edited: Jun 7, 2019
    pachermann likes this.