Search Unity

ARFoundation 2 image tracking with many ref images and many objects

Discussion in 'Handheld AR' started by SpiderJones, May 18, 2019.

  1. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    111
    sroq likes this.
  2. Dennisrudolph

    Dennisrudolph

    Joined:
    Aug 24, 2017
    Posts:
    1
    I am having the same problem here. Any help would be much appreciated.
     
  3. PTW_AR

    PTW_AR

    Joined:
    Jan 13, 2019
    Posts:
    11
    I'm really confused, why they haven't included that feature in the first place; image tracking is kinda useless if you cant generate different prefabs for each reference image. Maybe I'm just too spoiled from the ARKit...

    Anyway, I followed the little guide by @Skeketor23 ( https://forum.unity.com/threads/arfoundation-and-2d-image-target-tracking.538062/page-3#post-4556173 ), but couldn't make it work.

    I simply put my prefabs in the ressources folder and named theme accordingly to their respective image in the referenceImageLibrary.
    Working with the trackableID might be better, but I'm not entirely sure how to use them in that case (are they known a priori? Meaning before building my project?). That would not solve the issue though, I would still face the same problem:
    I can't find a way to assign the prefabs to their respective image. The documentation isn't really helping either in that regard.

    (It's hard to tell what's actually going on because of the lack of a remote for the editor.)
     
  4. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    111
    I use the reference image name and instantiate a prefab

    Code (CSharp):
    1. private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
    2.         {
    3.             for (int i = 0; i < eventArgs.added.Count; i++)
    4.             {
    5.                 eventArgs.added[i].referenceImage.name;
    6. // use that to get the gname
    7. }
    8. }
    Here's an example of using the tracked image event -> https://github.com/Unity-Technologi.../ImageTracking/TrackedImageInfoManager.cs#L97
     
    PTW_AR likes this.
  5. drewfjacobs

    drewfjacobs

    Joined:
    Mar 12, 2019
    Posts:
    3
    I too am trying to add this functionality.
    So Spider did you end up making your own or amending the Tracked Image Manager? Would you mind posting the whole class? Thanks!
     
  6. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    111
    You should never add code to a framework, you should create classes that encapsulate and adapt the framework. Otherwise you make it impossible to easily update the framework. So yes, I created my own class. My class has a lot of code that is specific to my project so I'm not going to share it. I just added a listener to the event, just like here -> https://github.com/Unity-Technologi.../ImageTracking/TrackedImageInfoManager.cs#L97

    Then, iterated over the other Lists like I said.

    Another tip, when I instantiate my GameObject, I am adding the ARTrackedImage as the parent.
     
  7. drewfjacobs

    drewfjacobs

    Joined:
    Mar 12, 2019
    Posts:
    3
    Thanks, Spider. I am fairly new to C# and OOP as I'm more of a tech artist being tasked with implementing AR in Unity. I'm concurrently going through these tuts: http://rbwhitaker.wikidot.com/c-sharp-tutorials as I try to dev this app. Wish me luck! :D
     
  8. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    111
    Maybe this will work for you ->

    Code (CSharp):
    1.  
    2. private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
    3.         {
    4.             ARTrackedImage trackedImage = null;
    5.  
    6.             for (int i = 0; i < eventArgs.added.Count; i++)
    7.             {
    8.                 trackedImage = eventArgs.added[i];
    9.                 // instantiate AR object, set trackedImage.transform
    10.                 // use a Dictionary, the key could be the trackedImage, or the name of the reference image -> trackedImage.referenceImage.name
    11.                 // the value of the Dictionary is the AR object you instantiate.
    12.             }
    13.  
    14.             for (int i = 0; i < eventArgs.updated.Count; i++)
    15.             {
    16.                 trackedImage = eventArgs.updated[i];
    17.                 if (trackedImage.trackingState == TrackingState.Tracking)
    18.                 //if (trackedImage.trackingState != TrackingState.None)
    19.                 {
    20.                     // set AR object to active, use Dictionary to get AR object based on trackedImage
    21.                     // you can also include TrackingState.Limited by checking for None
    22.                 }
    23.                 else
    24.                 {
    25.                     // set active to false
    26.                 }
    27.             }
    28.  
    29.             for (int i = 0; i < eventArgs.removed.Count; i++)
    30.             {
    31.                 // destroy AR object, or set active to false. Use Dictionary.
    32.             }
    33.         }
    34.  
     
  9. PTW_AR

    PTW_AR

    Joined:
    Jan 13, 2019
    Posts:
    11
    Sorry to dig this thread up over and over, but I think it might be helpful for other people working with ImageTracking in the future:

    trackedImage.trackingState (in your example): Does it ever change from "tracking" to "none" for you guys? Although the camera loses the image, the TrackingState remains "tracking" (which is really weird to me).

    I managed to instantiate different prefabs for different images quite easily after having the name avialable (referenceImage.name), load them from the resources folder and assign them their respective parent, but they remain active, even though the imageAnchor is nowhere to be found.

    Do I misunderstand the concept of the TrackingStates or is it simply buggy?
     
  10. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    111
    use this to deactivate or hide you AR Objects:

    Code (CSharp):
    1. for (int i = 0; i < eventArgs.removed.Count; i++)
    2.             {
    3.                 // destroy AR object, or set active to false. Use Dictionary.
    4.             }
     
  11. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    111
    TrackingState has three states:
    TrackingState.Limited
    TrackingState.None
    TrackingState.Tracking
     
  12. PTW_AR

    PTW_AR

    Joined:
    Jan 13, 2019
    Posts:
    11
    Thanks for the quick reply! I am aware of the three TrackingStates. Even with your attempt, it doesn't want to work for me (my guess is that it's never changing to the trackingState none (or limited)):

    Code (CSharp):
    1. for (int i = 0; i < eventArgs.removed.Count; i++)
    2.         {
    3.             trackedImage = eventArgs.removed[i];
    4.             NamePrefab = trackedImage.referenceImage.name;
    5.             TempPrefab = trackedImage.transform.Find(NamePrefab).gameObject;
    6.             Destroy(TempPrefab);
    7.         }
    8.  
     
  13. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    111
    The code you shared is not using the "updated" List. You are using the "removed" List. So the "none" state not working is irrelevant, right? I have not tested the "none" state, but I do know that for me the "limited" state does work, and that is why I'm ignoring it, because my ARObjects would do strange things if they were visible and the TrackingState was "limited". Also, using Find is cpu intensive. You should store a local reference to your AR GameObject. Use a Dictionary with the referenceImage.name as the key, and the AR GameObject as the value. and maybe there is an issue with how you are trying to find the AR GameObject, are you using the referenceImage.name as the name for your AR GameObject? When you instantiate a Prefab it adds [clone] (or something like that) to the end of the name. Regardless, I recommend that you do not use this approach, use a Dictionary.
     
  14. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    111
  15. PTW_AR

    PTW_AR

    Joined:
    Jan 13, 2019
    Posts:
    11
    First of all: Thanks for the idea with the dictionary. For me, this only solved my issue to an extend, because it will only switch to trackingState.limited, if you lay down the device on a table and wait for ~2 seconds (found it out through pure chance with some debug logs). The problem is: only then the prefab.setactive is set to be false. The ARKit directly changes the state directly after losing its ImageAnchor, 2 seconds seem to be quite long (besides the annoying laying down part, which is extremely weird). I assume you didn't have the same experience?

    I created a video to further show my problem:
    https://drive.google.com/open?id=1XP4dkKymXubgTILautDcOvTu1eNBtijf

    The debug log shows the following:
    "we are in the updateCycle with trackingstate Tracking"
    or
    "we are in the updateCycle with trackingstate!= Tracking"

    2nd one only appears after laying down the device. Holding it still without image in the camera view doesnt result in a trackingState change to limited... -.-

    Your app looks very clean, good job!
     
    Last edited: Jun 23, 2019 at 10:36 AM
  16. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    111
    Thank you for your compliment. I'm sorry, but I don't fully understand your issue. In my app, when users show the camera the image marker (playing card), the AR object shows, when the card is hidden, the object doesn't show. There are 13 AR objects in my app that show for each type of card. My app works like Vuforia (on iOS only because ARKit tracks moving images while ARCore does not). I am using the approach I shared, and I'm ignoring the "Limited" state. When it's "Tracking" I show the ARObject, when it's "None" or "Limited" I hide it. And I use the "remove" list to hide AR Objects. And it works perfectly. I do not know what else I can do to help you. Good luck!
     
    PTW_AR likes this.