Search Unity

ARFoundation Image Tracking - How to position spawned objects properly.

Discussion in 'AR' started by jiungerich, Apr 7, 2020.

  1. jiungerich

    jiungerich

    Joined:
    Jul 26, 2019
    Posts:
    18
    I am writing this because this was a difficult issue for me as this is currently a bit clunky to do. It wasn't until I opened an issue on the GitHub ARFoundation page that I found an answer. So I am sharing what I found out with the community in a new thread so hopefully others will not have to spend as much time.

    This is an active open issue on the ARFoundation Github page so this may change in future versions (hopefully near future) but this is what works as of this writing.

    You can follow this issue here to see if there is a change or update:
    https://github.com/Unity-Technologies/arfoundation-samples/issues/444

    Some Background:

    I am currently using 2019.3.7f1 with ARFoundation versions 3 and 4 beta to get this to work. If you are using 2019.2 there are some specific issues with 2D object spawn locations that I found but this was resolved when I began building in 2019.3. I did not test earlier versions.

    Basically, in the versions mentioned above, if one just uses a simple plane primitive as prefab in the Image Tracking prefab box, the plane will spawn directly on the image with the plane centroid at the image centroid. However, if you put any image or video texture etc. on that plane it will be upside-down to the image. You can scale the plane but you cannot rotate or use positioning to move the plane off-center.

    Likewise if one was to use a cube primitive in the Image Tracking prefab box, the code will do some calculation to offset the centroid of the cube so the base lands on the image. If you try to transform the cube etc. by changing its rotation or position, these controls do not respond - but scaling does.

    How to solve this:

    For your image tracking prefab first create an empty object that will be your Image Tracking Prefab.

    Then as a child of the empty object, create a plane. It may be helpful to drop your tracked image onto the plane and scale the plane according to the image so you can reference the image as one is adding objects to spawn when the image is found. To align the plane properly with the image target when it spawns, one also needs to rotate this base plane 180 degrees around the Y axis. Keep all other settings at the origin (unless you want a purposeful offset etc.).

    Place any other objects you want to spawn as children of the plane above. If you need to transform the whole prefab scene, you can then transform the plane that all the children are dependent on to do a whole scene transform.

    Also if your image is not 1 x 1 square you may want to separate any reference image from the base plane and then scale - as any scaling of the base plane will scale objects dropped on it. If you do not want to show the base plane, you would need to hide it or make it transparent etc. after you are done adding other objects. One could also take objects off the plane (so they are no longer children) and then turn the plane off, but you will lose the ability to move or rotate all the objects together by just transforming the base plane.

    If you want to have tracked images render different prefabs. Build a plane like above for each prefab (or alternately make all the prefabs children of a main location plane) and then activate them individually with a script depending on which image is detected(ARFoundation has methods for this).

    For examples, you can look the ARFoundation Github page here (the current Image tracking prefab is in the samples is built the way I describe above - again this may change over time).

    https://github.com/Unity-Technologies/arfoundation-samples
     
    hdemby, lilzubr, sebi115 and 5 others like this.