Search Unity

Turn off main camera rendering in multi camera setup

Discussion in 'General Graphics' started by coldwarrior5, Mar 22, 2019.

  1. coldwarrior5

    coldwarrior5

    Joined:
    Nov 9, 2016
    Posts:
    12
    Hi,

    could somebody tell me how and if it is possible to stop rendering to the viewport anything while keeping cameras active?
    I have several cameras placed in the scene along with the main camera, to try and squeeze even more performance I would like to stop rendering the main camera, but when I deactivate the main camera, my view just switches to another camera in the scene. I would like a completely dark screen while other cameras do their internal rendering.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    There has to be some camera active, but that camera doesn't need to render anything. If you add a camera with it's culling mask set to nothing and clear to black, that'll be what shows. Just make sure you're still disabling any other camera that's not needed.
     
  3. coldwarrior5

    coldwarrior5

    Joined:
    Nov 9, 2016
    Posts:
    12
    Hi bgolus, thank you for the help, I found the solution.

    What I did is the following:

    First I noticed that by assigning depth I can control which camera is displayed next, the camera with the highest depth number is always shown.

    Then I decided not to completely disable the main camera because then I would see GUI elements piling up i.e. the scene did not remove old GUI elements (I had FPS counter in the corner). And the GUI canvas did not work so I couldn't get to pause menu, which is bad. To do that I set clear flags to 0 and culling mask to CameraClearFlags.Color:
    _renderingCamera.cullingMask = 0;
    _renderingCamera.clearFlags = CameraClearFlags.Color;
    I gained the performance and everything was working properly. And when I wanted to go back to rendering I just set the clear flags and culling mask to the previous one that I saved.

    To stop other cameras from rendering to the scene, and only to the rendertexture I set the camera rendertexture OnPreRender, and then I just do not blit from the texture to the camera's destination texture in OnPostRender. That way I gained 5% of performance.