Search Unity

Question Ar 2D Image Traking can't disable object when new object appear and cover the same positon too fast

Discussion in 'AR' started by fbergonzinigames, Jun 3, 2022.

  1. fbergonzinigames

    fbergonzinigames

    Joined:
    Apr 3, 2020
    Posts:
    12
    Good morning everyone. I am writing to know if you can help me with a strange bug that I am facing working with Ar foundation, specifically with 2D Image traking.
    My application is based on the tracking of a series of images (of the same size) which when framed generate a prefab with text and other contents, basic use of image traking.
    To manage the despown of unframed images I used the classic trakedimage approach with the difference that I manage it directly from the prefab child of the trakedImage and not ARTrackedImageManager eventArgs.updated.


    Code (CSharp):
    1. private void VisibilityCheck_Automatic()
    2.         {
    3.             if (m_trakedImage == null)
    4.                 return;
    5.             if (!imageIsVisible)
    6.             {
    7.                 if (m_trakedImage.trackingState == UnityEngine.XR.ARSubsystems.TrackingState.Tracking)
    8.                 {
    9.                     imageIsVisible = true;            
    10.                     // active object
    11.                 }
    12.             }
    13.             else
    14.             {
    15.                 if (m_trakedImage.trackingState == UnityEngine.XR.ARSubsystems.TrackingState.Limited ||
    16.                     m_trakedImage.trackingState == UnityEngine.XR.ARSubsystems.TrackingState.None)
    17.                 {
    18.                     imageIsVisible = false;
    19.                     // disable object
    20.                 }
    21.             }          
    22.         }
    Now the bug is this.
    When I am scrolling through the images recognized by the AP from the desktop (there are more than 100) it often happens that if two images alternate immediately (imagine that I have an image open, I press the right arrow to view the next image) the app fails to deactivate the first prefab that will remain present, under the newly spawned second prefab of the new card.

    So the second image is recognized and the prefab is spawned, but the first prefab is not deactivated even if its image is no longer visible.

    The strange thing is that this only happens if the second image is just immediately in the place where it was before. For example, if the second image takes a while to load, at that time the app no longer sees the first image deactivates it correctly and the bug does not happen. If the two images are not in the same place or one image is moved, there is no problem.

    To fix the bug I just stop framing the image for a second and when I get back the first prefab is correctly gone, but it's very annoying. I have also tried to manage the deactivation with other methods like a raycast, but the problem persists on android and IOS.

    Ar foundation 4.19, Unity 2020.3.28.f1
     
  2. fbergonzinigames

    fbergonzinigames

    Joined:
    Apr 3, 2020
    Posts:
    12
    Ok I did some tests and the problem is that the traking state of the image remains in trakingstate.traking and therefore the image behaves as if it were still active, instead of disappearing. Ideas on how to fix?
     
  3. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    1,062
    fbergonzinigames likes this.
  4. fbergonzinigames

    fbergonzinigames

    Joined:
    Apr 3, 2020
    Posts:
    12
    Yes, I was able to find the bug in your sample as well.

    I upload a demonstration video. Compared to your code I have simply changed so that when the trakingstate is not traking I deactivate both the plane and the canvas, so it is more clear if the tracking state is changing or not.

    I also added a third image so that the problem is more visible.

    Code (CSharp):
    1. void UpdateInfo(ARTrackedImage trackedImage)
    2.         {
    3.             // Set canvas camera
    4.             var canvas = trackedImage.GetComponentInChildren<Canvas>();
    5.             canvas.worldCamera = worldSpaceCanvasCamera;
    6.          
    7.             //CHANGE
    8.             canvas.enabled = true;
    9.  
    10.             // Update information about the tracked image
    11.             var text = canvas.GetComponentInChildren<Text>();
    12.             text.text = string.Format(
    13.                 "{0}\ntrackingState: {1}\nGUID: {2}\nReference size: {3} cm\nDetected size: {4} cm",
    14.                 trackedImage.referenceImage.name,
    15.                 trackedImage.trackingState,
    16.                 trackedImage.referenceImage.guid,
    17.                 trackedImage.referenceImage.size * 100f,
    18.                 trackedImage.size * 100f);
    19.  
    20.             var planeParentGo = trackedImage.transform.GetChild(0).gameObject;
    21.             var planeGo = planeParentGo.transform.GetChild(0).gameObject;
    22.  
    23.             // CHANGE
    24.             if (trackedImage.trackingState == TrackingState.Tracking)
    25.             {
    26.                 planeGo.SetActive(true);
    27.  
    28.                 // The image extents is only valid when the image is being tracked
    29.                 trackedImage.transform.localScale = new Vector3(trackedImage.size.x, 1f, trackedImage.size.y);
    30.  
    31.                 // Set the texture
    32.                 var material = planeGo.GetComponentInChildren<MeshRenderer>().material;
    33.                 material.mainTexture = (trackedImage.referenceImage.texture == null) ? defaultTexture : trackedImage.referenceImage.texture;
    34.             }
    35.             else
    36.             {
    37.                 planeGo.SetActive(false);
    38.                 canvas.enabled = false;
    39.  
    40.             }
    41.  
    42.         }

    As shown in the video, when I frame an image and switch to the next one, for some reason the traking state of the first image remains in traking when it should go into limited since the image is no longer visible.

     
  5. fbergonzinigames

    fbergonzinigames

    Joined:
    Apr 3, 2020
    Posts:
    12