ARSessionOrigin transform position and rotation to make created ARTrackedImage become Unity space zero

Viewed 2864

I'm using ARFoundation with tracking images(Image Library has only one image to find). When the image is found I'm subscribing on that event with

[SerializeField]
ARTrackedImageManager _arTrackedImageManager;

void Awake()
{
    _arTrackedImageManager.trackedImagesChanged += ImageChangedHandle;
}

void ImageChangedHandle(ARTrackedImagesChangedEventArgs imgChangedArgs)
{
    foreach (var item in imgChangedArgs.added)
    {
        var go = item.gameObject;

        _arkitChecker.DisplayFoundTrackedImage(item);
    }
}

_arkitChecker is instance of my class with DisplayFoundTrackedImage method :

internal void DisplayFoundTrackedImage(ARTrackedImage targetImageGameObject)
{
    var arOrig = GameObject.FindObjectOfType<ARSessionOrigin>();

    arOrig.transform.Translate(-targetImageGameObject.transform.position, Space.World);

    float babyAngle;
    Vector3 babyAxis;
    targetImageGameObject.transform.rotation.ToAngleAxis(out babyAngle, out babyAxis);

    arOrig.transform.RotateAround(Vector3.zero, babyAxis, -babyAngle);
}

As a result I want to make transformation when created ARTrackedImage becomes Unity space Vector3.zero with Quaternion.identity, but on the device image nothink changes.

I've also tried ARSessionOrigin.MakeContentAppearAt, but didnt work as I want :

arOrig.MakeContentAppearAt(targetImageGameObject.transform, Vector3.zero, Quaternion.identity);
1 Answers

I just had to solve this same problem, I was able to make it work with the ARSessionOrigin MakeContentAppearAt() method, using the tracked image's position and rotation (I'm using ARFoundation v3.0.0 preview 4).

That method is not documented very well, after some trial and error I have found that you need to use the localRotation of the tracked image, not the world-space rotation otherwise the orientation will be incorrect.

Whenever the tracked image is found or updated, I run the following:

void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
{
    if(eventArgs.updated.Count > 0)
    {
        ARTrackedImage trackedImage = eventArgs.updated[0];
        arSessionOrigin.MakeContentAppearAt(worldOrigin.transform, trackedImage.transform.position, trackedImage.transform.localRotation);
    }
}

worldOrigin.transform is just an empty game object at position 0,0,0 with no rotation.

So, this code is saying: I want the worldOrigin transform to appear as if it is located at the trackedImage position/rotation by adjusting the AR camera's transform to compensate (worldOrigin.transform is not modified).

Related