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

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?

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.

1 Like

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.

Set the pixels to Color.clear.

EDIT: I just read that the color buffer is readonly, so that doesn’t work.

USE blender offensive method

Wich means? :confused:

At last, I just added a simple

		Pass {
			Color (0, 0, 0, 0)
		}

before the pass of my intermediates shaders. It clear the texture before applying the effect in it.

Can’t you just use GL.Clear?

Ok, but how do I select thre texture I want to clear?

Something like:

GameObject.Find(“CameraRenderTexture”).GetComponent().clearFlags=CameraClearFlags.SolidColor;

RenderTexture rt = UnityEngine.RenderTexture.active;
UnityEngine.RenderTexture.active = myRenderTextureToClear;
GL.Clear(true, true, Color.clear);
UnityEngine.RenderTexture.active = rt;

25 Likes

Setting clear flags to color and alpha=0 seems to also do the trick

Ran into this today when I wanted to clear a render texture used by a VideoPlayer. This fixed the problem, thanks!

3 Likes

I just successfully implemented MasterLu’s solution.

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() (Unity - Scripting API: RenderTexture.Release), is equally effective at clearing the texture.

public class ReleaseVideoTexture : MonoBehaviour
{
    VideoPlayer _player;

    private void Awake()
    {
        _player = GetComponent<VideoPlayer>();
        _player.targetTexture.Release();
    }

    private void OnDisable()
    {
        _player.targetTexture.Release();
    }
}
4 Likes

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.

3 Likes

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!

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?

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…