Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to set up a camera so that it doesn't clear the color buffer?

Discussion in 'Universal Render Pipeline' started by pateman, Feb 29, 2020.

  1. pateman

    pateman

    Joined:
    May 3, 2014
    Posts:
    18
    Hello,

    I'm using Unity 2019.3.3f and the latest Universal RP 7.2.1. I have a camera stack set up - the base camera renders my level (layer Default), and the overlay camera renders the "field of view" mesh that I'm generating on the fly (it's on a separate layer called "Mask".)

    Now when I use the stack and combine the outputs of those two cameras, it works correctly, i.e. I can see the rendered field of view mesh overlaid on the base camera's output. But my aim is to have the field of view to unveil the map, so all areas that have already been visited by the player should be visible, and the remaining ones not.

    I think the problem is that the overlay camera clears its color buffer everytime it renders, because it only renders the current "state" of the field of view mesh.

    How do I set up the stack so that I can achieve what I'm looking for?

    If you need any more information, please let me know.

    Thank you,
    Patryk
     
  2. pateman

    pateman

    Joined:
    May 3, 2014
    Posts:
    18
    I decided to try a different setup and show you guys the approach that I'm currently taking.

    Here's my minimap camera. It basically follows the player (that's why it has a script attached to it, background type is set to 'Uninitialized'):
    CameraConfig.PNG

    Here's the renderer configuration that it uses:
    RendererConfig.PNG

    And now let's run the game and see what happens. As you can see, it correctly renders the view mesh. I know that it might look bad in the picture, but believe that it's the expected result:
    RT_1.PNG

    Now let's move to the right so that the field of view mesh can expand:
    RT_2.PNG

    As you can see, the little fragment at the very start is now gone and the current state of the field view mesh is now rendered to the texture. What I would like to achieve is this (this picture's been done in Photoshop):
    RT_Desired.PNG

    The above effect should be easily achieved if the render texture didn't clear every frame, I believe.

    Do you think my setup is wrong somewhere, something is missing, or is that a bug in Unity? Also, if you have a different idea on how to do that "uncovering map" effect, I'm open to learn about them.

    Thank you,
    Patryk
     
    Last edited: Mar 1, 2020
  3. pateman

    pateman

    Joined:
    May 3, 2014
    Posts:
    18
    Another observation is that if I set the camera's solid color to some value and then set it back to Uninitialized, I can still see that color in the render texture.

    Seriously, am I missing some really obvious stuff here or is it clearly a bug?
     
  4. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    584
    You are using the API as intended. I quickly checked the code and it seems like this is behavior that you might experience only in editor. That's a bug. Could you submit I bug report so we keep track of it and I'll push a fix asap?
     
  5. pateman

    pateman

    Joined:
    May 3, 2014
    Posts:
    18
    Sure, how do I do that? :) And what exactly is the problem?
     
  6. pateman

    pateman

    Joined:
    May 3, 2014
    Posts:
    18
    @phil_lira I think that you only want to fix the problem with the editor still showing the camera color in the preview, but the problem is (I think) in the render texture being cleared every frame, even though I don't really want to do that, because I explicitly set 'Background Type' to 'Uninitialized'.
     
  7. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    584
    https://unity3d.com/unity/qa/bug-reporting

    I didn't say how I'm going to fix the problem. Behavior should be if you set to Uninitialized it should not clear, not matter the platform or the render target. :)
    I just said that I cans see already something is wrong with Editor, it might be that there's more things wrong but I'll find out when we triage the bug.
     
  8. pateman

    pateman

    Joined:
    May 3, 2014
    Posts:
    18
    Perfect, let me report it for you!
     
  9. pateman

    pateman

    Joined:
    May 3, 2014
    Posts:
    18
  10. pateman

    pateman

    Joined:
    May 3, 2014
    Posts:
    18
    @phil_lira Do you happen to know if there's any progress on this issue? Do you guys need more information from me?

    Regards,
    Patryk
     
  11. pateman

    pateman

    Joined:
    May 3, 2014
    Posts:
    18
    @phil_lira Could you tell me if you guys fixed the bug, please?
     
  12. phil_lira

    phil_lira

    Unity Technologies

    Joined:
    Dec 17, 2014
    Posts:
    584
    Hey Patryk, I apologize we haven't started worked on the fix for this issue yet. We plan to fix it and backport to 2019 LTS but I can't give you an ETA yet.
     
  13. pateman

    pateman

    Joined:
    May 3, 2014
    Posts:
    18
    @phil_lira Sorry for bugging you guys, but I'm just curious whether there's any progress on this issue. If it's fixed in whichever 2020 version, it's fine, I can upgrade.
     
  14. Shaunyowns

    Shaunyowns

    Joined:
    Nov 4, 2019
    Posts:
    328
    Hey @pateman, I'll see if we can get an update on this for you!
     
  15. pateman

    pateman

    Joined:
    May 3, 2014
    Posts:
    18
  16. pateman

    pateman

    Joined:
    May 3, 2014
    Posts:
    18
  17. Shaunyowns

    Shaunyowns

    Joined:
    Nov 4, 2019
    Posts:
    328
    Hey @pateman, sorry for the delay, I'll see if I can get this looked at again!
     
  18. pateman

    pateman

    Joined:
    May 3, 2014
    Posts:
    18
    @Shaunyowns Thank you! Please get back to me as soon as possible, I'd really love to have it resolved. I don't mind upgrading to a newer version as long as the issue's gone.
     
  19. vincentgravitas

    vincentgravitas

    Joined:
    Jan 25, 2015
    Posts:
    6
    I was also curious about this one, and I made attempts to capture the screen buffer using a blit to a RenderTexture at the end of the render pipeline. I planned to blit the texture at the start of the next frame, as to simulate having Clear Flags: Don't Clear (or what Background Type: Uninitialized could be). I was able to achieve this in a couple of different ways, but it felt like a mess.

    But in the process, I discovered an interesting solution.

    If you use the Blit/pass scripts from Universal Rendering Examples, you can set up a blit feature in your renderer. If you specify a material using the Sprites/Default shader, Event: Before Rendering Prepasses, Destination: Color, it will cause it to behave as if it has Clear Flags: Don't Clear.

    I have no clue why this works, but it really seems to do the trick.
     

    Attached Files:

  20. pateman

    pateman

    Joined:
    May 3, 2014
    Posts:
    18
    Wow, cool! I'll check it out soon :)
     
  21. riveranb

    riveranb

    Joined:
    Jul 16, 2017
    Posts:
    16
    I am using Unity 2019.4.5f1 LTS version.
    And I currently have the same problem, camera keeps clear color buffer with background type = "Uninitialized".
    Hopes this issue get fixed ASAP.
    I can imagine many features need "UNINITIALIZED" / "DON'T CLEAR" background clear type.
     
    pateman likes this.
  22. pateman

    pateman

    Joined:
    May 3, 2014
    Posts:
    18
    robomarti likes this.
  23. Jebtor

    Jebtor

    Unity Technologies

    Joined:
    Apr 18, 2018
    Posts:
    113
    Hey, first of all thank you for reporting this issue. I am looking into the issue and I have already found the cause. I am actively working on a solution. Although it may take a little bit before this becomes public. You can either wait until the fix makes it out, or alternatively you can embed the URP package locally and fix it temporarily by removing or commenting out this line. Do note it will probably cause other issues in the FrameDebugger.
     
  24. Jebtor

    Jebtor

    Unity Technologies

    Joined:
    Apr 18, 2018
    Posts:
    113
    A fix for the issue has landed in 10.1.0-preview.26, 7.5.2 & 8.3.1 of URP.
     
    JesOb likes this.
  25. mohusheng

    mohusheng

    Joined:
    Apr 4, 2018
    Posts:
    3
    Problem still exists. (Tested URP 7.5.2 and 10.2.2)
    ([Background Type] choose [Uninitialized])Editor show normaly, but show solid color when pack APK and running in real android phone.
     

    Attached Files:

  26. Jebtor

    Jebtor

    Unity Technologies

    Joined:
    Apr 18, 2018
    Posts:
    113
    @mohusheng thanks for bringing this to our attention. Could you please file a bug report and post a link to the issue tracker here. I had a quick look at the code and I believe there might be a similar issue for mobile platforms.
     
  27. Danielsantalla

    Danielsantalla

    Joined:
    Jan 7, 2015
    Posts:
    75
    Any news on this? I tested it on Unity 2020.1.3f1 URP 8.2 and the buffer keeps clearing every frame even when set to Uninitialized @Jebtor @pateman
     
  28. Jebtor

    Jebtor

    Unity Technologies

    Joined:
    Apr 18, 2018
    Posts:
    113
    @danielsantanaribeiro2015 the fix landed in URP 8.3.1 as stated in my earlier post. The versions in which the fix landed is also visible on the issue tracker (here) - use one of these versions or higher. Please file a bug report if you are still seeing issues when using a version that should contain the fix.
     
  29. const_cast

    const_cast

    Joined:
    Feb 10, 2020
    Posts:
    3
    Hi,
    I'm still seeing this bug in both URP 8.3.1 (Unity 2020.1.17f1) and URP 10.2.2 (Unity 2020.2.2f1) when running on iOS.
     
  30. Jebtor

    Jebtor

    Unity Technologies

    Joined:
    Apr 18, 2018
    Posts:
    113
    @const_cast could you please report a bug and post a link to the issue tracker here? I am starting to believe there is a related issue that only affects mobile.
     
  31. marnsmith

    marnsmith

    Joined:
    Feb 7, 2013
    Posts:
    29
    Jebtor likes this.
  32. quniform

    quniform

    Joined:
    Nov 12, 2018
    Posts:
    4
    I can also confirm that this is broken on iOS.
     
  33. Jackben

    Jackben

    Joined:
    Sep 30, 2015
    Posts:
    1
    The issue also occurs on Android while it works fine in editor (Unity 2020.2.1f1, URP 10.2.2).
     
  34. quniform

    quniform

    Joined:
    Nov 12, 2018
    Posts:
    4
    Same. It works in editor, but not when packaged for iOS.
     
    Fimiam likes this.
  35. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    I've looked at the case and the problem is because both of the cameras in the scene have their Render Type set to "Base". Same is visible in the screenshots of https://forum.unity.com/threads/how...t-clear-the-color-buffer.837961/#post-6705799 .

    What you are trying to do is "Camera stacking", https://docs.unity3d.com/Packages/c...es.universal@10.3/manual/camera-stacking.html, there should be only one "Base" camera, and other cameras should be set to "Overlay".
    In summary:
    Base Camera - can clear its color buffer to a Skybox, solid color, or use an uninitialized color buffer.
    Overlay Camera - receives a color buffer containing color data from the previous Cameras in the Camera stack. It does not clear the contents of the color buffer.

    As for the discrepancy between platforms with "Uninitialized" background type, it is by design. Don't confuse "don't clear" with "uninitialized". "Uninitialized" means that the contents of color buffer can be anything (previous frame, garbage, any random value), it is specified that the contents can vary between platforms https://docs.unity3d.com/Packages/c...s.universal@10.3/manual/cameras-advanced.html
     
  36. zzajac2

    zzajac2

    Joined:
    Nov 10, 2020
    Posts:
    1
    @rjonaitis But overlay camera do not let to set viewportRect. Is there way to render with overlay camera only on part of base camera viewport?
     
    Last edited: Feb 25, 2021
    songjiho and Fimiam like this.
  37. Fimiam

    Fimiam

    Joined:
    Oct 24, 2016
    Posts:
    13
    Also, overlay cameras can't render to a render texture!

    Have the same problem. On IOS and Android "Uninitialized" background type just doesn't work, looks like it switches to "solid color". In the editor everything is perfect;

    "Uninitialized" or "don't clear" features are great and this is awful we can't use it on Mobiles....

    Tested on URP 10.3.1 with Unity 2020.2.6
     
  38. steffone

    steffone

    Joined:
    Apr 25, 2016
    Posts:
    1
    Since "Uninitialized" is utterly useless since we always want the exact same behavior on any platform. Can you please add another option which is true Don't Clear flag. Don't Clear flag worked perfectly on the Standard renderer (non URP), its very useful when using it for masking, drawing and similar, since it doesn't use Texture2D.Apply or any pixel manipulations.
    Don't get me started on Compute shaders and their shoot and miss compatibility with various devices, Graphics Api, and plafroms.
     
    ArseneySorokin likes this.
  39. Danielsantalla

    Danielsantalla

    Joined:
    Jan 7, 2015
    Posts:
    75
    Any news on this? the developers said that it was fixed but it is not
     
  40. Danielsantalla

    Danielsantalla

    Joined:
    Jan 7, 2015
    Posts:
    75
    Can you guys re-add the ''don't clear flags" to the Camera? I can't understand why such a valuable feature was removed
     
  41. Jebtor

    Jebtor

    Unity Technologies

    Joined:
    Apr 18, 2018
    Posts:
    113
    We are investigating options for a fully supported workflow. Timeline for this is unknown but we hear the need. Until this is in place we believe the same can be achieved writing custom render features. We hope to share a sample on how to do this shortly.

    Just to clarify the original issue in this thread should be fixed. On mobile you do get the clear color - this is considered by design as rjonaitis has explained.
     
  42. ArseneySorokin

    ArseneySorokin

    Joined:
    Jan 23, 2013
    Posts:
    63
    Just wanted to mention that I had a fog of war feature which used the "dont clear" option to keep the previously discovered areas revealed, but due to the fact that my game is for mobile I switched to URP for superior framerate, however, I cannot achieve the fog of war effect anymore.
    Would love to know how I can write my own rendering script to simulate the don't clear effect that is available in the standard render pipeline.
     
    chortaingseng likes this.
  43. chortaingseng

    chortaingseng

    Joined:
    Mar 18, 2021
    Posts:
    3
    Hello. How can I solve this in hdrp camera?
     
  44. Jonas-Mortensen

    Jonas-Mortensen

    Unity Technologies

    Joined:
    Jan 3, 2020
    Posts:
    110
    Hey Everyone.
    Package samples are coming to URP in 2021.2 and in there we have a couple of scenes using a KeepFrame renderer feature that is bundled with the samples. The feature copies scene color to a texture at the end of a frame and redraws before opaque pass in the next frame. This effectively replicates don't clear behavior. The code is here. You can decide on a name for the uncleared color texture and reference it in a shader based on that.
    The feature needs a material for redrawing the frame. The simplest example of that would look like this (unlit shadergraph, name of texture is _FrameCopyTex): upload_2021-9-21_15-3-10.png

    Remember to untick Exposed and override the property declaration to Global in order for the texture to be bound properly.
    For examples of use check out the package samples releasing with 2021.2
     
    florianBrn likes this.
  45. EyeDev44

    EyeDev44

    Joined:
    Apr 8, 2017
    Posts:
    149
    Same on some android models, 2021.1.23.
     
  46. AdamLacko

    AdamLacko

    Joined:
    Oct 10, 2017
    Posts:
    41
    Hi, thank you for this idea. I'm having problems with camera clearing, and this is actually what I need, just done in a different fashion. My question is though - how can I use this with UI Raw Image component? When I preview the material, I can see it indeed does have the don't clear behavior, although I've been unable to project this material onto Raw Image component.
     
  47. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    584
    @Jonas-Mortensen can you explain how the KeepFrameFeature works when it comes to background colors? The example is a mostly empty scene with particles and a black background. Changing the Main Camera to use a Solid background doesn't have any effect and I don't understand where in the script or the shader the background is set.

    I tried changing some of the colors in the shadergraph and in the code here:
    Code (CSharp):
    1.    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    2.     {
    3.         m_OldFrameHandle.Init("_OldFrameRenderTarget");
    4.         m_DrawOldFame.ConfigureClear(ClearFlag.None, Color.red);
    5.  
    6.         m_CopyFrame.Setup(renderer.cameraColorTarget, m_OldFrameHandle);
    7.         renderer.EnqueuePass(m_CopyFrame);
    8.  
    9.         m_DrawOldFame.Setup(settings.displayMaterial, m_OldFrameHandle, String.IsNullOrEmpty(settings.textureName) ? "_FrameCopyTex" : settings.textureName);
    10.         renderer.EnqueuePass(m_DrawOldFame);
    11.     }
    Nothing seems to affect the background. Adding a background quad changes the background, but ruins the effect.
     
  48. PlanktonZJS

    PlanktonZJS

    Joined:
    Apr 20, 2021
    Posts:
    1
    May I ask what the “Compatibility Intermediate Texture” option does, it is not selected,keep Frame not working
     
  49. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    584
    To answer my own question, I did get this to sort of work by making the background quad a transparent material and then playing with Additive, Multiply, etc. settings. It's not the same as my old project using the "Don't Clear" method, but it works. It is nice that this will be compatible across platforms and different computers. That was a real limitation with my old app, it would work on some machines but not others.
     
  50. ContxtualDempsey

    ContxtualDempsey

    Joined:
    Mar 7, 2022
    Posts:
    3
    Still having this issue on 2020.3.25f1 building for android, Trying to find a way to render to a texture and not clear the color buffer each frame. I am looking at the camera stacking approach but I do not think that suites my needs, its getting the camera size from the viewport I cannot set it to custom dimensions. Any tips, updates, or alternative methods would be appreciated.