Search Unity

Question Similar property to ARCamera.transform in ARFoundation?

Discussion in 'AR' started by metigel94, Mar 12, 2021.

  1. metigel94

    metigel94

    Joined:
    Oct 11, 2019
    Posts:
    12
    Hi all.

    I'm working on a project for which I need the transformation matrix of the camera of the iOS device.
    Is there any equivilant to this https://developer.apple.com/documentation/arkit/arcamera in Unity / ARFoundation? Or any other way to access the current transformation matrix of the camera for an image / frame?

    Any help is greatly appreciated.
     
  2. KyryloKuzyk

    KyryloKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    1,142
    AR Foundation doesn't expose this property. But you can get the camera position directly from the 'AR Camera' game object (the one with ARPoseDrive.cs attached to it).

    But there is one catch! To get the synchronized camera position/rotation with the current camera frame you have to wait until ARPoseDriver updates the camera position.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.XR.ARFoundation;
    3. using UnityEngine.XR.ARSubsystems;
    4.  
    5.  
    6. public class CameraTransformationMatrix : MonoBehaviour {
    7.     [SerializeField] ARCameraManager cameraManager = null;
    8.     [SerializeField] Camera arCamera = null;
    9.  
    10.  
    11.     /// <summary>
    12.     /// ARPoseDriver updates the camera position in Update(), so we need to execute this code after it, hence we use LateUpdate()
    13.     /// </summary>
    14.     void LateUpdate() {
    15.         var cameraParams = new XRCameraParams {
    16.             zNear = arCamera.nearClipPlane,
    17.             zFar = arCamera.farClipPlane,
    18.             screenWidth = Screen.width,
    19.             screenHeight = Screen.height,
    20.             screenOrientation = Screen.orientation
    21.         };
    22.      
    23.         if (cameraManager.subsystem.TryGetLatestFrame(cameraParams, out var cameraFrame)) {
    24.             var cameraPositionInSyncWithCameraFrame = arCamera.transform.position;
    25.             var cameraRotationInSyncWithCameraFrame = arCamera.transform.rotation;
    26.         }
    27.     }
    28. }
    29.