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 ARCamera | Apple Developer Documentation 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.
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.
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
public class CameraTransformationMatrix : MonoBehaviour {
[SerializeField] ARCameraManager cameraManager = null;
[SerializeField] Camera arCamera = null;
/// <summary>
/// ARPoseDriver updates the camera position in Update(), so we need to execute this code after it, hence we use LateUpdate()
/// </summary>
void LateUpdate() {
var cameraParams = new XRCameraParams {
zNear = arCamera.nearClipPlane,
zFar = arCamera.farClipPlane,
screenWidth = Screen.width,
screenHeight = Screen.height,
screenOrientation = Screen.orientation
};
if (cameraManager.subsystem.TryGetLatestFrame(cameraParams, out var cameraFrame)) {
var cameraPositionInSyncWithCameraFrame = arCamera.transform.position;
var cameraRotationInSyncWithCameraFrame = arCamera.transform.rotation;
}
}
}