Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

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. Cameron860

    Cameron860

    Joined:
    Jun 1, 2009
    Posts:
    764
    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:
    680
    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:
    719
    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:
    40
    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:
    831
  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!
     
  15. pamelacook

    pamelacook

    Joined:
    Dec 13, 2017
    Posts:
    5
    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. }
     
    frank-ijsfontein likes this.
  17. DavidSWu

    DavidSWu

    Joined:
    Jun 20, 2016
    Posts:
    135
    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.