Search Unity

AR multiple cameras dont line up - Solution

Discussion in 'AR/VR (XR) Discussion' started by RaventurnPatrick, May 20, 2019.

  1. RaventurnPatrick

    RaventurnPatrick

    Joined:
    Aug 9, 2011
    Posts:
    250
    I embarked on a multi-hour debugging session to find out why having multiple cameras with augmented reality does not work as expected (they don't line up).

    However i found the culprit and a solution (hope this helps some of you):
    - The ARCameraBackground changes the projection matrix of the main camera (presumably to get the fov to match)
    - When you have child cameras they have the same position/rotation as the main camera, however the projection matrix is still different

    In order to solve this you can put this script on any child camera in AR:
    Code (CSharp):
    1. [RequireComponent(typeof(Camera))]
    2.   public class UICamera : MonoBehaviour
    3.   {
    4.     private Camera _uiCamera;
    5.     private Camera _sourceCamera;
    6.  
    7.     private void Awake()
    8.     {
    9.       _sourceCamera = transform.parent.GetComponent<Camera>();
    10.       _uiCamera = GetComponent<Camera>();
    11.     }
    12.  
    13.     private void Update()
    14.     {
    15.       _uiCamera.projectionMatrix = _sourceCamera.projectionMatrix;
    16.     }
    17.   }
    @unityteam: Maybe this could be improved -> maybe include a script for child cameras?
    Use case: If you need a separate UI camera because i.e. you don't want post-processing on your ui elements (world space)
     
    Last edited: May 20, 2019