Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  3. Read here for Unity's latest plans on OpenXR.
    Dismiss Notice

Resolved Extended image tracking ARKit

Discussion in 'AR' started by saifshk17, Jan 22, 2021.

  1. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    332
    I am using ARKit's image tracking to place an object at a certain position. If the image is lost, I want the object to stay in the same position as it got tracked initially. But unfortunately the extended tracking is no success. When the image is lost, the object takes in a different position farther away from where it was initially tracked. What should I do in this script?

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.Linq;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Events;
    6. using UnityEngine.XR.ARFoundation;
    7. using UnityEngine.XR.ARSubsystems;
    8.  
    9. public class ARImageAnchor : MonoBehaviour
    10. {
    11.    public string ReferenceImageName;
    12.    private ARTrackedImageManager _TrackedImageManager;
    13.    public Text text_;
    14.  
    15.    public ARTrackedImageManager arTracked;
    16.  
    17.    private void Awake()
    18.    {
    19.        _TrackedImageManager = FindObjectOfType<ARTrackedImageManager>();
    20.    }
    21.  
    22.    private void OnEnable()
    23.    {
    24.        if (_TrackedImageManager != null)
    25.        {
    26.            _TrackedImageManager.trackedImagesChanged += OnTrackedImagesChanged;
    27.        }
    28.    }
    29.  
    30.    private void OnDisable()
    31.    {
    32.        if (_TrackedImageManager != null)
    33.        {
    34.            _TrackedImageManager.trackedImagesChanged -= OnTrackedImagesChanged;
    35.        }
    36.    }
    37.  
    38.    private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs e)
    39.    {
    40.  
    41.        foreach (var trackedImage in e.added)
    42.        {
    43.            Debug.Log($"Tracked image detected: {trackedImage.referenceImage.name} with size: {trackedImage.size}");
    44.  
    45.            if(trackedImage.referenceImage.name == this.ReferenceImageName)
    46.            {
    47.                this.gameObject.GetComponent<MeshRenderer>().enabled = true;
    48.            }
    49.        }
    50.  
    51.        UpdateTrackedImages(e.added);
    52.        UpdateTrackedImages(e.updated);
    53.    }
    54.  
    55.    private void UpdateTrackedImages(IEnumerable<ARTrackedImage> trackedImages)
    56.    {
    57.        // If the same image (ReferenceImageName)
    58.        var trackedImage =
    59.            trackedImages.FirstOrDefault(x => x.referenceImage.name == ReferenceImageName);
    60.        if (trackedImage == null)
    61.        {
    62.            return;
    63.        }
    64.  
    65.        if (trackedImage.trackingState != TrackingState.None)
    66.        {
    67.            var trackedImageTransform = trackedImage.transform;
    68.            transform.SetPositionAndRotation(new Vector3(trackedImageTransform.position.x + 0.5f, trackedImageTransform.position.y, trackedImageTransform.position.z), Quaternion.Euler(0, 0, 0));
    69.        }
    70.    }
    71.  
    72.  
    73.  
    74. }
     
  2. KirillKuzyk

    KirillKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    469
    Try to update image's position only if trackingState == TrackingState.Tracking.
     
  3. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    332
    But I want the image to stay in the same position after the tracking is lost. How can I achieve this?
     
  4. KirillKuzyk

    KirillKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    469
    Your image will stay on the last position which had TrackingState.Tracking state.
     
  5. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    332
    I tried it like this:

    Code (CSharp):
    1. if (trackedImage.trackingState == TrackingState.Tracking)
    2.        {
    3.            var trackedImageTransform = trackedImage.transform;
    4.            transform.SetPositionAndRotation(new Vector3(trackedImageTransform.position.x + 0.5f, trackedImageTransform.position.y, trackedImageTransform.position.z), Quaternion.Euler(0, 0, 0));
    5.        }
    Unfortunately when I hide the image, the object gets move further away.
     
  6. KirillKuzyk

    KirillKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    469
    Tracking drift may occur if there is not enough tracking information available. Please try to move your phone further from the image and ensure there are enough landmark features in your environment.
    For example, ARKit/ARCore has a hard time tracking camera position against solid-color walls or floor.
     
  7. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    332
    Yes initially I tried it on different surfaces. But I just feel it sets a new position when the image is no longer in view.
     
  8. KirillKuzyk

    KirillKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    469
    Maybe you should try a different approach. When you've detected an image, ask a user to confirm the placement and remember the current position.
     
  9. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    332
    Hmm but from this forum: https://forum.unity.com/threads/image-tracking-extended.618202/
    There has been some luck. Dont know how to apply it in my code.
     
  10. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    332
    Hey it works. My mistake was I had given higher size of the image to be tracked. It was suppose to be 5 cm and I gave it as 15cm. Now it works well :)
     
    KirillKuzyk likes this.
unityunity