Unity-Technologies / arfoundation-samples

Example content for Unity projects based on AR Foundation
Other
3.03k stars 1.13k forks source link

Tracking Issue In Landscape Mode When Post-Processing Enabled #671

Closed dmarashi closed 3 years ago

dmarashi commented 3 years ago

Hello Team,

We are encountering tracking issues when using the device in landscape mode and post-processing on. I found there's a discussion thread with the similar topic in Unity forum here: https://forum.unity.com/threads/arfoundation-post-processing-stack-tracking-drift.593872/ and user dnnkeeper seems provided a workaround by fixing camera's field of view, you can found the code there: https://forum.unity.com/threads/arfoundation-post-processing-stack-tracking-drift.593872/#post-5030378

However, it seems still an existing issue in current AR Foundation (tested in Unity 2020.1.8f1, iPhone X, iOS 14.2) and the workaround has not accepted (or not been noticed?) by the team. I'm wondering if you can fix it in the package as well??

tdmowrer commented 3 years ago

ARFoundation sets the Camera component's entire projection matrix, which implicitly includes the FOV. The camera's fieldOfView property is only used by the Camera component to compute a projection matrix automatically. If, however, the projection matrix is set explicitly (as is the case with ARFoundation), this property has no effect on the Camera component.

However, the post processing package also writes to the Camera's projection matrix (which is okay) but it does so using a calculation based on the Camera's fieldOfView property, which is not automatically recalculated from the projection matrix, and therefore does not reflect the true FOV in cases where the projection matrix is set explicitly.

We can compute and set the field of view on the Camera in future releases. In the meantime, I would not recommend dnnkeeper's workaround as written. dnnkeeper's calculation is correct, but this can be done in a separate MonoBehaviour; you don't need to modify ARFoundation. Something like this on the AR Camera GameObject should do it:

using UnityEngine;
using UnityEngine.XR.ARFoundation;

[RequireComponent(typeof(ARCameraBackground))]
[RequireComponent(typeof(Camera))]
public class SetFov : MonoBehaviour
{
    void Update()
    {
        if (GetComponent<ARCameraBackground>().enabled)
        {
            var camera = GetComponent<Camera>();
            camera.fieldOfView = Mathf.Atan(1 / camera.projectionMatrix[1, 1]) * 2 * Mathf.Rad2Deg;
        }
    }
}

Also note that you need to do this in all cases, not just landscape modes. The effect is simply more exaggerated in landscape since the default Camera FOV is 60, and this is typically pretty close to the portrait FOV you get from AR.

tdmowrer commented 3 years ago

This was fixed in 4.1.3.