Search Unity

Question Clearing the Viewport Background

Discussion in 'Editor & General Support' started by CThayer, Jan 27, 2021.

  1. CThayer

    CThayer

    Joined:
    Oct 21, 2018
    Posts:
    7
    Hi Everyone,

    I'm not completely sure if this is the right place for this question, so please let me know if I should put it in a different section.

    TL;DR: I'm having trouble with "old frames" showing in the background when I switch between a camera that uses 100% of the viewport and another camera that uses ~70% of the viewport.

    Full Story:
    I have a main camera that follows the player and then I have other cameras that I switch to for specific things like using an apparatus. The non-player cameras use a smaller viewport rect size (~70% of screen size) so that it fits nicely in the space not occupied by the context specific UI. The issue is that there are "gaps" in between the UI elements and things that were rendered previously (e.g. from the character camera) show through the cracks. For example:
    GapsDetail1.jpg GapsFullscreen1.png

    I apologize for the example images because they are using placeholder assets so it isn't as clear as it could be. It might look like the camera that is simply at 100% of the viewport rect and is showing through, but I promise this is not the case. The current camera is only rendering to the upper left area.

    I thought that there must be a way to clear the viewport when switching cameras so that the "background" goes to a solid color but after an exhaustive search I haven't found anything that allows me to explicitly clear it. So far the best solution that I have been able to find is to use this process:
    1. Set the main camera to the lowest depth value (e.g. -1).
    2. Set the depth value to a higher value (e.g. +1) for the other cameras that I switch to.
    3. Add a "intermediary camera" with a depth value in between the main camera and the other cameras (e.g. 0)
    4. Put the "intermediary camera" somewhere out of sight of everything else and aim it at a completely black plane.
    5. Whenever a switch between cameras occurs:
      1. Deactivate the main camera
      2. Activate the "intermediary camera"
      3. Activate the camera that I am switching to.
      4. Invoke a method to deactivate the "intermediary camera" after delay of a few fractions of a second.
    This "fixes" the issue as you can see here:
    GapsIssueFixed.jpg

    However, it seems far from ideal. For one thing, it requires the extra camera and logic. Second, I have to put it somewhere out of sight since I am using a piece of geometry to get it to render full black. Third, it seems really inefficient. And fourth, it seems like the viewport shouldn't keep the rendered data from a camera that is deactivated OR at the very least, there should be a way to clear it.

    Does anybody know of a better way to accomplish this?
     
    Last edited: Jan 27, 2021
  2. CThayer

    CThayer

    Joined:
    Oct 21, 2018
    Posts:
    7
    Quick Followup:
    The "fix" that I listed isn't 100% correct. In order to completely remove the issue of artifacts in the background, the intermediary camera has to be left active. In other words, step 5.4 is incorrect and has to be removed to get the desired behavior.
     
  3. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    529
    I was able to fix this by calling GL.Clear(true, true, Color.black); from an OnPreCull() method of a MonoBehaviour attached to the camera just before making changes to the viewport rect of that camera.