1
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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