Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Camera loses AR Camera Background if disabled

Discussion in 'AR' started by KISP, Nov 19, 2018.

  1. KISP

    KISP

    Joined:
    Sep 13, 2018
    Posts:
    32
    Hello,

    I am creating an application where you can switch between an AR view and a non-AR view of a scene. I tried switching between these two modes by creating two separate cameras and enabling/disabling them as required. However, I've found that once the AR camera is disabled, it loses the camera background and begins rendering the skybox once it is enabled again. The tracking is still functional though.

    How do I correctly switch between an AR and non-AR camera without losing the background?
     
  2. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    605
    I tried this quickly and it worked as expected. Can you submit a bit of sample code to show how you're switching between the cameras?
     
  3. KISP

    KISP

    Joined:
    Sep 13, 2018
    Posts:
    32
    Here is the code I am using to switch to the non-AR camera:
    Code (CSharp):
    1.     private void SwitchTo()
    2.     {
    3. // calculate position of topdown camera
    4.         topdownCameraObj.transform.position = newPos;
    5.         topdownCameraObject.SetActive(true);
    6.         ARCamera.gameObject.SetActive(false);
    7.     }
    8.  
    The calculation of the topdown camera position is independent of the AR. This is the code switching back to the AR camera:
    Code (CSharp):
    1.     public void Close()
    2.     {
    3.         topdownCameraObject.SetActive(false);
    4.         ARCamera.gameObject.SetActive(true);
    5.  
    6.         if (onClose != null)
    7.             onClose.Invoke();
    8.         onClose = null;
    9.     }
    Where onClose is a delegate. There is no other interaction with AR features while the topdown camera is active.

    AR Camera set up is included in the image attachment.
     

    Attached Files:

  4. Mal_Duffin

    Mal_Duffin

    Joined:
    Jan 22, 2015
    Posts:
    71
    I had an issue earlier when switching scenes, and on the 2nd reload the camera didn't work.
    I disabled the "Light Estimation" on the "AR Session" game object and it then worked.

    I'll try to re-create and debug this, but disabling it is worth a go to see if it works.
     
    KISP likes this.
  5. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    605
    This matches my test as well. Have you checked logcat to make sure there are no other errors, like an uncaught exception, which is preventing some of your code from executing?
     
  6. Mal_Duffin

    Mal_Duffin

    Joined:
    Jan 22, 2015
    Posts:
    71
    This is the solution to my problem.

    In LightEstimation.cs, change...

    Code (CSharp):
    1. void Awake ()
    2. {
    3.   m_Light = GetComponent<Light>();
    4.   ARSubsystemManager.cameraFrameReceived += FrameChanged;
    5. }
    to

    Code (CSharp):
    1. void Awake ()
    2. {
    3.   m_Light = GetComponent<Light>();
    4. }
    5.  
    6. void OnEnable()
    7. {
    8.     ARSubsystemManager.cameraFrameReceived += FrameChanged;
    9. }
    10. private void OnDisable()
    11. {
    12.    ARSubsystemManager.cameraFrameReceived -= FrameChanged;
    13. }
    When the AR setup was initialising the 2nd time, the old FrameChanged event was still running, and it's m_Light was null.

    If it's confirmed as a fix, I'll push a fix to the git repo.
     
    farahzehranaqvi likes this.
  7. tdmowrer

    tdmowrer

    Unity Technologies

    Joined:
    Apr 21, 2017
    Posts:
    605
    I suppose this sample code was not designed to be enabled and disabled. Feel free to submit a pull request with the fix. Thanks!
     
  8. Mal_Duffin

    Mal_Duffin

    Joined:
    Jan 22, 2015
    Posts:
    71
  9. KISP

    KISP

    Joined:
    Sep 13, 2018
    Posts:
    32
    @Mal_Duffin Yep. That was exactly what the problem was. Thanks for the tip!

    edit: Quick question. Is there a way to apply that change to my project? I don't appear to have access to the script in question.
     
    Last edited: Nov 21, 2018
  10. Mal_Duffin

    Mal_Duffin

    Joined:
    Jan 22, 2015
    Posts:
    71
    @ausernme, if you're using the packages, I think you'll have to wait for the fix to be integrated and then download the latest package with the fix in it.

    You can also download the ARFoundation source from GitHub, then make the changes from the patch locally ( if it hasn't been committed to the main branch ). It'll take a bit of time to change over, but is an option if you really need the Light Estimate stuff to work now.