Search Unity

How to clear a render texture to transparent color? (All bytes at 0)

Discussion in 'Scripting' started by Spidyy, Aug 14, 2012.

  1. Spidyy

    Spidyy

    Joined:
    Mar 6, 2011
    Posts:
    184
    The question in the title. I'm using render textures to simulate transparent layers, but I have to clear some of them or else I have a trail effect.

    I don't find anything about clearing a render texture, only clearing a camera. How can I do that?
     
  2. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    I can't think of a case where you'd need to clear a render texture outside of rendering to it, at which point you can use the camera's clearflag to clear it as you render into it.
     
    Miscellaneous likes this.
  3. Spidyy

    Spidyy

    Joined:
    Mar 6, 2011
    Posts:
    184
    I'm creating some kind of image effect, and I use a couple of temporary render texture. The fact is, what i'm trying to render is transparent, but the temporary render texture don't clear themselves, it make some kind of unwanted trail effect.
     
  4. andererandre

    andererandre

    Joined:
    Dec 17, 2010
    Posts:
    683
    Set the pixels to Color.clear.

    EDIT: I just read that the color buffer is readonly, so that doesn't work.
     
    Last edited: Aug 16, 2012
  5. UNITY3D_TEAM

    UNITY3D_TEAM

    Joined:
    Apr 23, 2012
    Posts:
    720
    USE blender offensive method
     
  6. Spidyy

    Spidyy

    Joined:
    Mar 6, 2011
    Posts:
    184
    Wich means? :/
     
  7. Spidyy

    Spidyy

    Joined:
    Mar 6, 2011
    Posts:
    184
    At last, I just added a simple
    Code (csharp):
    1.         Pass {
    2.             Color (0, 0, 0, 0)
    3.         }
    4.  
    before the pass of my intermediates shaders. It clear the texture before applying the effect in it.
     
  8. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    Can't you just use GL.Clear?
     
  9. Spidyy

    Spidyy

    Joined:
    Mar 6, 2011
    Posts:
    184
    Ok, but how do I select thre texture I want to clear?
     
  10. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
  11. Miscellaneous

    Miscellaneous

    Joined:
    Sep 24, 2013
    Posts:
    53
    Something like:

    GameObject.Find("CameraRenderTexture").GetComponent<Camera>().clearFlags=CameraClearFlags.SolidColor;
     
  12. MasterLu

    MasterLu

    Joined:
    Nov 23, 2012
    Posts:
    2
    RenderTexture rt = UnityEngine.RenderTexture.active;
    UnityEngine.RenderTexture.active = myRenderTextureToClear;
    GL.Clear(true, true, Color.clear);
    UnityEngine.RenderTexture.active = rt;
     
  13. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,084
  14. yoyobbi

    yoyobbi

    Joined:
    Nov 26, 2013
    Posts:
    16
    Ran into this today when I wanted to clear a render texture used by a VideoPlayer. This fixed the problem, thanks!
     
    magnetic_scho and lassade like this.
  15. pamelacook

    pamelacook

    Joined:
    Dec 13, 2017
    Posts:
    10
    I just successfully implemented MasterLu's solution.
     
  16. KepAmun

    KepAmun

    Joined:
    Feb 22, 2013
    Posts:
    2
    A VideoPlayer component when activated was briefly showing whatever remained in the targetTexture. I found that MasterLu's solution worked for me as well, however I found that myRenderTextureToClear.Release() (https://docs.unity3d.com/ScriptReference/RenderTexture.Release.html), is equally effective at clearing the texture.

    Code (CSharp):
    1. public class ReleaseVideoTexture : MonoBehaviour
    2. {
    3.     VideoPlayer _player;
    4.  
    5.     private void Awake()
    6.     {
    7.         _player = GetComponent<VideoPlayer>();
    8.         _player.targetTexture.Release();
    9.     }
    10.  
    11.     private void OnDisable()
    12.     {
    13.         _player.targetTexture.Release();
    14.     }
    15. }
     
  17. DavidSWu

    DavidSWu

    Joined:
    Jun 20, 2016
    Posts:
    183
    You might just be getting lucky on your platform. I would not rely on Release to clear a texture.
    Especially not on Tiled Renderer base mobile platforms.
     
    Fenikkel, mdrunk and WildStyle69 like this.
  18. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    427
    Just a heads up. For us, calling GL.Clear() on a 3rd generation iPad Pro (in this case using iOS 14.3) crashed the GPU with errors IOAF code 2 and 4. Essentially the whole application continues running but the last drawed frame is displayed forever. Worked fine on older iOS devices. Unity 2019.4.16f. Beware!
     
  19. Maxillagator

    Maxillagator

    Joined:
    Mar 12, 2016
    Posts:
    1
    I've been using a render texture to take a snapshot of multiple image components, which are then applied to a new texture. I've been using a single render texture to take multiple snapshots, one at the end of every frame. This appears to work without issue in editor, but fails in builds. It appears that pixel data from the previous capture is being retained and applied to empty alpha space on the next snapshot. Has anyone encountered anything similar to this?
     
  20. Burn0815

    Burn0815

    Joined:
    Jul 3, 2019
    Posts:
    11
    I have struggled with a similar problem on android build. the last rendered rendertexture was not cleared correctly. but my solution was to avoid clearing the rendertexture - but instead do the next render of the rendertexture twice on different frames (with a coroutine "yield return new WaitForEndOfFrame()"). that worked. of course, this is not ideal, when rendering big geometry...
     
    Last edited: Dec 8, 2022