- Home /
Prevent Render Texture clearing
In a nutshell, How can I prevent RenderTextures from being cleared after every frame?
I have a script that writes high intensity values to a render texture and I want to do an image effect with this over multiple frames while constantly drawing more to it; kinda like a camera with no clear flags. The problem with this is that Unity is damn dedicated to clear the RenderTexture on every frame with the camera content. Is it possible to prevent this somehow or make a workaround?
Here's my simple script for reference.
Unity 5.3.1p1
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class TemporalTest : MonoBehaviour {
public Shader temporalTestShader;
public RenderTexture rt;
public Material temporalMaterial;
void Start () {
rt = new RenderTexture(Screen.width, Screen.height, 0);
temporalMaterial = new Material(temporalTestShader);
}
void OnRenderImage(RenderTexture source, RenderTexture destination) {
// rt gets filled correctly here
// but it's cleared every frame for no reason causing nuclear headaches
temporalMaterial.SetTexture("_TemporalMap", rt);
Graphics.Blit(source, rt, temporalMaterial, 0);
}
}
Answer by Kuura · Jan 04, 2016 at 01:25 PM
Ok, after hours of debugging I found out that it was the Blit function that is clearing the rendertexture and not unity in between OnRenderImage calls.
The problem was that you can't do
mat.SetTexture("something", sometex);
Blit(A, B, mat); // Add A to "something" in shader and save to some result B
and expect it to write the result nicely to B. Instead you have to use temporary rendertextures and do
mat.SetTexture("something", sometex);
RenderTexture temp = RenderTexture.GetTemporary(...);
Blit(A, temp, mat); // Add A to "something", save to temp
Blit(temp, B); // Copy temp to some result texture B
RenderTexture.Release(temp);
I don't know why it has to be done this way or why can't I use B straight as a result and so on but at least it works.
Thank you!
We had this exact problem. We're usually are developing on OSX but we had a $$anonymous$$m member joining on Windows and a feature related to this did not work at all. Your solution solved everything. You are my hero for the day!
Your answer
Follow this Question
Related Questions
dynamic hole in layer / texture / camera 0 Answers
Blit isn't writing to render texture 1 Answer
Multiple Blit problem 2 Answers
Blit shader to RenderTexture results in solid black 0 Answers