Search Unity

Sync multiple cameras with ARFoundation

Discussion in 'AR' started by Thomas_Toys, Jul 30, 2019.

Thread Status:
Not open for further replies.
  1. Thomas_Toys

    Thomas_Toys

    Joined:
    Feb 15, 2017
    Posts:
    3
    Hi,

    I' m using ARfoundation 2.1.1 with Unity 2019.1.11f1, and I have integrated an outline effect wich uses a second camera attached to the main ARCamera to render only the "outlined objects" layer. The effect works fine, except there is an offset between the render ot the 2 cameras. To be more specific, the outline effect is slightly off the main object render because the 2 cameras are not synced (see the 2 images attached for illustration). The first one is the one taken from my mobile (see the offset between the object and the effect) and the seconds is taken from the editor (where it is fine).

    What I tried to solve the problem :
    - Parent the "outline" camera to the "ARCamera" and copying all AR camera components parameters to its child (fov, projection matrix, etc..)
    - Make the second camera an all new ARCamera with all necessary components (tracked pose driver, arsessionorigin, arcamera manager).
    - And many combinations of the 2 previous main solutions...

    Results are still the same any way the offset between the effect and the object remains.
    My observation is that the offset increases when the object is closer to the fov limits (border of the screen).
    I think I'm missing something about the way the ARCamera works. If you have more informations about this...

    Thanks in advance for the help,

    Thomas
     

    Attached Files:

    Last edited: Jul 30, 2019
    AbleArcher likes this.
  2. ryancookyeti

    ryancookyeti

    Joined:
    Mar 6, 2017
    Posts:
    5
    I too have suffering from this problem. I've got a second camera that uses some depth masking objects to reveal rendered elements of the camera beneath it, but they are offset exactly has described and shown in the images above.
     
  3. ryancookyeti

    ryancookyeti

    Joined:
    Mar 6, 2017
    Posts:
    5
    OK I just came up with a solution.

    Attach the script below to the any cameras parented to the ARCamera and they will stay synced up with it during rendering. I have no idea which one of the properties being copied over is actually causing this to work, because its 1:30 AM and I needed to get this solved for a client demo meeting later today.


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteAlways]
    4. public class ARCameraMatcher : MonoBehaviour {
    5.     public Camera sourceCamera;
    6.  
    7.     private Camera _myCamera;
    8.  
    9.     private void OnEnable() {
    10.         _myCamera = GetComponent<Camera>();
    11.     }
    12.  
    13.     private void OnPreRender() {
    14.         if (_myCamera == null) {
    15.             return;
    16.         }
    17.         if (sourceCamera == null) {
    18.             return;
    19.         }
    20.  
    21.         _myCamera.nearClipPlane = sourceCamera.nearClipPlane;
    22.  
    23.         _myCamera.farClipPlane = sourceCamera.farClipPlane;
    24.  
    25.         _myCamera.orthographic = sourceCamera.orthographic;
    26.         _myCamera.orthographicSize = sourceCamera.orthographicSize;
    27.         _myCamera.fieldOfView = sourceCamera.fieldOfView;
    28.  
    29.         _myCamera.cullingMatrix = sourceCamera.cullingMatrix;
    30.         _myCamera.nonJitteredProjectionMatrix = sourceCamera.nonJitteredProjectionMatrix;
    31.         _myCamera.projectionMatrix = sourceCamera.projectionMatrix;
    32.         _myCamera.useJitteredProjectionMatrixForTransparentRendering = sourceCamera.useJitteredProjectionMatrixForTransparentRendering;
    33.         _myCamera.worldToCameraMatrix = sourceCamera.worldToCameraMatrix;
    34.  
    35.         _myCamera.stereoConvergence = sourceCamera.stereoConvergence;
    36.         _myCamera.stereoSeparation = sourceCamera.stereoSeparation;
    37.         _myCamera.stereoTargetEye = sourceCamera.stereoTargetEye;
    38.  
    39.         _myCamera.sensorSize = sourceCamera.sensorSize;
    40.         _myCamera.gateFit = sourceCamera.gateFit;
    41.     }
    42. }
     
    agpatel0007 and Thomas_Toys like this.
  4. Thomas_Toys

    Thomas_Toys

    Joined:
    Feb 15, 2017
    Posts:
    3
    Thank you @ryancookyeti,

    You solved the problem with OnPreRender method.
    I suspected the projectionMatrix parameter to be the solution but I made the change in the Update method before.
    Now it works perfectly.

    So here the final script to attach to the child camera :

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteAlways]
    4. public class ARCameraMatcher : MonoBehaviour
    5. {
    6.     public Camera sourceCamera;
    7.  
    8.     private Camera _myCamera;
    9.  
    10.     private void OnEnable()
    11.     {
    12.         _myCamera = GetComponent<Camera>();
    13.     }
    14.  
    15.     private void OnPreRender()
    16.     {
    17.         if (_myCamera == null)
    18.         {
    19.             return;
    20.         }
    21.         if (sourceCamera == null)
    22.         {
    23.             return;
    24.         }
    25.         _myCamera.projectionMatrix = sourceCamera.projectionMatrix;
    26.  
    27.     }
    28. }
     
    agpatel0007, Viniterra and Jokonut like this.
  5. Yearwood

    Yearwood

    Joined:
    Jul 31, 2018
    Posts:
    9
    Man... @ryancookyeti and @Thomas_Toys ya'll are awesome. I was up till 3am last night trying to figure this out. Worked on first try. Thanks!!!
     
  6. agpatel0007

    agpatel0007

    Joined:
    Aug 21, 2014
    Posts:
    14
    you guys are genius, thanks a lot
     
  7. boruds

    boruds

    Joined:
    Aug 7, 2020
    Posts:
    28
    This is just what I wanted. Thanks you so much guys!!!
     
Thread Status:
Not open for further replies.