Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to create some image effect. I've written a script that draws a cube mesh at the render target and then copies the render texture at the screen. I don't want to render anything except Mesh(M) in my RenderTexture(rt):

    void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        RenderTexture rt = RenderTexture.GetTemporary(Screen.width / 1, Screen.height / 1 );

        Graphics.SetRenderTarget(rt);
        Graphics.DrawMeshNow(M, new Vector3(0.0f, 0.0f, -8.0f), Quaternion.identity);
        Graphics.Blit(rt, dest);

        Graphics.SetRenderTarget(null);
        RenderTexture.ReleaseTemporary(rt);
    }

Where M is GameObject.CreatePrimitive(PrimitiveType.Cube).GetComponent<MeshFilter>().sharedMesh;

When I moved my camera, I see this: enter image description here

It seems like the RenderTexture didn't cleared itself when released. Or maybe I did something wrong? Please explain why this happened and how can I fix this.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

That is correct: RenderTextures are not necessarily cleared after being released or reacquired.

According to the documentation:

You can not depend on any particular contents of the RenderTexture you get from GetTemporary function. It might be garbage, or it might be cleared to some color, depending on the platform.

If you want the RenderTexture to be cleared, then you'll need to clear it yourself after acquiring it.

share|improve this answer
1  
+1 Documentation. –  Patrick Hughes Mar 10 at 4:18
    
Yep, docs rules. Thanks a lot! –  Netherwire Mar 10 at 18:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.