Search Unity

How to use ArAnchor to track imageTarget position after it is lost?

Discussion in 'AR' started by wurzel-digital, Jan 3, 2020.

  1. wurzel-digital

    wurzel-digital

    Joined:
    Jun 21, 2013
    Posts:
    9
    Hi,

    i try to keep tracking the last known position of an imageTarget by creating an ArAnchor at the position of the imageTarget, but i can not get it to work right.

    I found a workaround, which you can see in the following code, but this is a bad solution and i would like to avoid using this workaround.

    i did simply extend the class AnchorCreator of the arfoundation samples to make a small test project. I added the class in the code below as well as an ArTrackedImageManager to the Anchors scene in the samples.

    The problem is, that i want to create a new Pose at the imageTarget transforms position, but if i do so with

    Pose newPose = new Pose(trackedImage.transform.position, trackedImage.transform.rotation);
    it is returning a different Pose than if i do a raycast to the plane represented by the image. It seems, that the returned Pose of the Raycast is in a different coordinate system (maybe ARSpace). The raycast workaround is only working if the tracked image is known to be vertical or horizontally placed and if a plane is found and that is not always the case if an image is found. There should be a way to create a correct Pose for a trackedImageTarget without doing a raycast, that could also fail.

    I would appreciate any help.


    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR.ARFoundation;
    5. using UnityEngine.XR.ARSubsystems;
    6.  
    7. [RequireComponent(typeof(ARAnchorManager))]
    8. [RequireComponent(typeof(ARRaycastManager))]
    9. [RequireComponent(typeof(ARTrackedImageManager))]
    10. public class AnchorCreatorImageTracking : MonoBehaviour
    11. {
    12.     public void RemoveAllAnchors()
    13.     {
    14.         foreach (var anchor in m_Anchors)
    15.         {
    16.             m_AnchorManager.RemoveAnchor(anchor);
    17.         }
    18.         m_Anchors.Clear();
    19.     }
    20.  
    21.     void Awake()
    22.     {
    23.         m_RaycastManager = GetComponent<ARRaycastManager>();
    24.         m_AnchorManager = GetComponent<ARAnchorManager>();
    25.         m_TrackedImageManager = GetComponent<ARTrackedImageManager>();
    26.  
    27.         m_TrackedImageManager.trackedImagesChanged += OnTrackedImagesChanged;
    28.         m_Anchors = new List<ARAnchor>();
    29.     }
    30.  
    31.     void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
    32.     {
    33.         foreach (var trackedImage in eventArgs.added)
    34.         {
    35.             Pose newPose = new Pose(trackedImage.transform.position, trackedImage.transform.rotation);
    36.  
    37.             if (m_RaycastManager.Raycast( Camera.main.WorldToScreenPoint(trackedImage.transform.position), s_Hits, TrackableType.Planes))
    38.             {
    39.                 Debug.Log("onFound HitTest PLanes");
    40.                 Debug.LogFormat("HitTest Pose position: {0} rotation: {1}",s_Hits[0].pose.position,s_Hits[0].pose.rotation);
    41.                 Debug.LogFormat("TrackedImage position: {0} rotation: {1}",trackedImage.transform.position,trackedImage.transform.rotation);
    42.                 Debug.LogFormat("TrackedImage localPosition: {0} localRotation: {1}",trackedImage.transform.localPosition,trackedImage.transform.localRotation);
    43.                 var hitPose = s_Hits[0].pose;
    44.                 var anchor = m_AnchorManager.AddAnchor(hitPose);
    45.                 m_Anchors.Add(anchor);
    46.             }
    47.          
    48.             var anchor2 = m_AnchorManager.AddAnchor(newPose);        
    49.             m_Anchors.Add(anchor2);
    50.          
    51.         }
    52.  
    53.     }
    54.  
    55.     void Update()
    56.     {
    57.         if (Input.touchCount == 0)
    58.             return;
    59.  
    60.         var touch = Input.GetTouch(0);
    61.         if (touch.phase != TouchPhase.Began)
    62.             return;
    63.  
    64.         if (m_RaycastManager.Raycast(touch.position, s_Hits, TrackableType.FeaturePoint))
    65.         {
    66.             // Raycast hits are sorted by distance, so the first one
    67.             // will be the closest hit.
    68.             var hitPose = s_Hits[0].pose;
    69.             var anchor = m_AnchorManager.AddAnchor(hitPose);
    70.             if (anchor == null)
    71.             {
    72.                 Logger.Log("Error creating anchor");
    73.             }
    74.             else
    75.             {
    76.                 m_Anchors.Add(anchor);
    77.             }
    78.         }
    79.     }
    80.  
    81.     static List<ARRaycastHit> s_Hits = new List<ARRaycastHit>();
    82.  
    83.     List<ARAnchor> m_Anchors;
    84.  
    85.     ARRaycastManager m_RaycastManager;
    86.  
    87.     ARAnchorManager m_AnchorManager;
    88.  
    89.     ARTrackedImageManager m_TrackedImageManager;
    90. }
    91.  
     
    Last edited: Jan 3, 2020
  2. wurzel-digital

    wurzel-digital

    Joined:
    Jun 21, 2013
    Posts:
    9
    ok i found my problem. In case someone have the same problem. The specified marker size in ARFoundation has to match the real image target size exactly. If that is the case the code is working as expected
    Pose newPose = new Pose(trackedImage.transform.position, trackedImage.transform.rotation);

    I still have no solution if you want to use the same marker with a different size.

    for my use case it worked best to create the marker some frames after it was initially found and not on lost. so i used the marker only for initial positioning of the Ar Anchor.
     
    TimHeessels_SHH likes this.
  3. AnthonyJackson81

    AnthonyJackson81

    Joined:
    May 30, 2019
    Posts:
    32
    Hi there. Im really trying to use Anchors for image tracking also. I can see you create a new Pose....... But I am struggling with the rest. The new component for ArAnchors has me confused as to how to create an anchor and then add my content to it?

    cheers.