Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question How to destroy an tracked image when using ar foundation?

Discussion in 'AR' started by blackbookuser, Jul 28, 2021.

  1. blackbookuser

    blackbookuser

    Joined:
    Aug 29, 2017
    Posts:
    5
    Hi! I am a bit stumped since the documentation says the following:

    You should never Destroy a trackable component or its GameObject directly. For trackables that support manual removal, their manager provides a method to remove it. For example, to remove an anchor, you need to call RemoveAnchor on the ARAnchorManager.
    https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@4.1/manual/trackable-managers.html

    But I can't find a method for destroying tracked images in the trackedImage manager, leaving me wondering how I am supposed to actually go about cleaning up AR stuff, if my scene is running for longer periods of time, generating new image markers that should be cleaned up an no longer tracked.

    Any help is appreciated :)
     
  2. KyryloKuzyk

    KyryloKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    1,159
    You don't have to do any cleaning yourself. If your app is designed to track many reference images, you can split them into multiple reference image libraries and select the one that is currently needed. Setting a new image library (or null) to the ARTrackedImageManager.referenceLibrary will clean resources from the old image library.
     
    andyb-unity likes this.
  3. ilya_m_3ds

    ilya_m_3ds

    Joined:
    Jan 15, 2020
    Posts:
    9
    Bump for this question. I also would like to destroy gameobjects generated from tracked image manager in order to conserve memory, however its unclear how to do this. In my case I am only using one reference image library so it doesnt make sense to change the reference in order to force a cleanup.
     
  4. lillie_aardman

    lillie_aardman

    Joined:
    Jan 8, 2019
    Posts:
    1
    Is there an update on this at all? Anyone figured out how to do it?
     
  5. Sennicam

    Sennicam

    Joined:
    Jul 22, 2017
    Posts:
    1
    I have tried setting the referenceLibrary variable, but it is not clearing the old tracked targets.
            _aRTrackedImageManager.referenceLibrary = null;
    _aRTrackedImageManager.referenceLibrary = _imageLibrary;


    This appears to do nothing... I have also tried CreateRuntimeLibrary() and SetTrackablesActive(), but neither gives the behavior I want.

    What I am looking for is to clear out the tracked images, but if an image is seen again, it should come back.

    Everything I have tried either leaves all the tracked images or removes them all permanently.

    I am using:
    • Unity 2020.3.26f1
    • AR Foundation 4.1.10
    • ARCore 4.1.10
    Any help would be appreciated.
     
    luvcraft likes this.
  6. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    1,135
    Image trackables can only be added or removed by your AR Foundation provider (ie, ARCore or ARKit). If you wish to hide your tracked image content, I recommend deactivating the trackable GameObjects (
    ARTrackedImageManager.trackables
    ), then re-activating them later based on the application logic you are implementing.
     
  7. rsauchuck-gp

    rsauchuck-gp

    Joined:
    Jan 21, 2020
    Posts:
    17
    Are you trying to remove the actual tracked images or the content that spawns when a tracked image is recognized?
     
  8. ashfaqueck

    ashfaqueck

    Joined:
    Jan 25, 2017
    Posts:
    11

    So If I want to track only 1 image at a time, what is the ideal solution?

    - A predefined reference library (Not dynamic)
    - Lets say 2 images are in the camera frame , I wanna track and act on one of them only. Not both.

    I already tried deactivating the ARTrackedImage gameobject of that image. But it is still getting tracked. Or is it the way simulator act? and I can get this working on device?

    @andyb-unity
     
    Last edited: May 16, 2024
  9. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    1,135
  10. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    1,135
    Ok I looked a bit closer at this: https://developer.apple.com/documen...82-maximumnumberoftrackedimages?language=objc

    So this could work for you if the image that you want to track is always in view.

    Otherwise what you would need to do is set up your image libraries like this: 1 library with all the images you want to track, and then n libraries, each containing only 1 image. Then switch between libraries at runtime to prevent the other images from being tracked after you have found the first one.
     
  11. ashfaqueck

    ashfaqueck

    Joined:
    Jan 25, 2017
    Posts:
    11
  12. ashfaqueck

    ashfaqueck

    Joined:
    Jan 25, 2017
    Posts:
    11

    I thought of this reference lib mechanism. But I don't wanted to go this way because this is for sure gonna be effecting the performance at runtime. So for tracking one image at a time, Like what vuforia does,

    After a lot of thoughts and time investments I came up with the following findings and solution.

    1) Set device as the Tracking origin mode on the XROrigin Component.
    2) Create a customized tracked image status manager script and use the

    ARTrackedImageMange's "trackedImagesChanged" action to get

    ARTrackedImagesChangedEventArgs added, updated and removed list of trackables.

    Consider TrackedImageInfoManager in the samples as a starting point for this.

    3) If you are familiar with existing third party image recognitions like vuforia, wikitude, easyAR, zappar etc.
    ARFoundation's image tracker can be mapped as ...

    Added - First Seen. This list will get filled with a specific trackable when it was found for the first time in an ARSession. It will be removed the moment it gets added to the updated list and will never get add again to this list in the same session.

    Updated - Tracked OR Limited (You can consider limited as Track lost) : This will need a logic to manage the states. Apart from added, this will have items coming in and out throughout the experience. When a trackable is in the camera view and being tracked it will be available in the updated list with a trackingState.

    Removed - Is probably happened once the session ends. I never personally had this item updated.

    So talking about the updated list. Until and unless there is only 1 image in the camera view at a time, this updated list will have that single image trackable in the list. The moment a second image comes to the camera view, this list will have that second trackable as the second element in the list. And both will have the trackingState as tracking. Now if you move the camera to get ride of one of them and focus only on one, Then this list will again go back to the 1 count with the current tracking item but in that change call back you can get a tracking limited state for the ignored item. This limited state update happens only once but tracking callback is continuous.

    Now what can be done is this updated list could be used to compare the trackables with their ids and their tracking states. Once we have this handy, to manage the one image at a time scenario, need to add a logic to find the nearest image to the device by calculating the distance of each tracking trackables with the device position. This is why I mentioned to make the device as the origin in the first point. This makes the distance more precise.

    NB: I have come up with all these findings using the XRSimulator and the ARFoundation samples only. Please mind rectifying anything mentioned above.

    So as a conclusion, this way of doing will perform no harm on the existing ARFoundation and we can leverage the full potential of using cross platform openXR runtime features for AR Apps we create. I am working on the one image at a time logic will post soon after testing them on the actual devices with some actual images.


    @andyb-unity Thanks for the quick reply. A humble suggestion would be to include more details about the
    ARTrackedImagesChangedEventArgs in the documentation.
     
    abhilashca and andyb-unity like this.
  13. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    1,135
    Nice writeup! Yes this is all correct. We have a new technical writer this year who has done a lot of work on documentation already. We'll be rolling out more docs updates throughout the year.
     
    ashfaqueck likes this.
  14. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    1,135
    ashfaqueck likes this.
  15. ashfaqueck

    ashfaqueck

    Joined:
    Jan 25, 2017
    Posts:
    11
    Cool. That would be awesome
     
  16. ashfaqueck

    ashfaqueck

    Joined:
    Jan 25, 2017
    Posts:
    11
  17. VirgileSi

    VirgileSi

    Joined:
    Mar 11, 2024
    Posts:
    1
    Hello !

    I have a problem with ARCore, I try to remove an image who is insn't tracked anymore. So I use the State to do that but I have a problem

    Here my code :



    foreach (var trackedImage in eventArgs.updated)
    {
    var imageName = trackedImage.referenceImage.name;
    #if UNITY_ANDROID
    Debug.Log("Android");
    #endif
    Debug.Log(imageName + " is stated " + trackedImage.trackingState);
    #if UNITY_ANDROID
    if (trackedImage.trackingState == TrackingState.Limited)
    {
    Debug.Log($"Image '{imageName}' is no longer tracked. Scheduling destruction.");
    if (_instantiatedPrefabs.ContainsKey(imageName))
    {
    _instantiatedPrefabs[imageName].DestroyPrefabs();
    _instantiatedPrefabs.Remove(imageName);
    }
    }
    #endif




    But, I don't understand why the program never reach the "Debug.Log($"Image '{imageName}' is no longer tracked. Scheduling destruction.");"
    In the log I have :
    Android
    Image is stated Limited

    So I guess the problem is this "trackedImage.trackingState == TrackingState.Limited"

    I don't know the problem and I don't know if it will solve anything

    Sorry for the mistakes and it's not my code so I don't understand all of it

    Thanks in advance