- Home /
Unwanted render texture clear
Hi !
I have a RenderEffect attached to a camera with a RenderTexture that I fill up with a nice red color in OnPreRender(), but when I display it in OnRenderImage(), the texture is cleared to black...
Does Unity perform unsolicited clears of textures ? Is there a catch about some internal recycle mechanism of render textures I'm not aware of ? Did I make a mistake at some point ?
Thanks for your time.
Answer by Patapom · Sep 22, 2012 at 12:48 PM
Hello again,
I finally found the actual bug with that issue. It was so twisted that it made me report the bug wrongly.
So the actual bug is this :
1] Clear a RenderTexture to RED in OnPreRender()
2] Display it in OnRenderImage(), the texture is cleared to some other value
Why does it do this ?
=> Because I forgot to reset the RenderTexture.active state when I left my OnPreRender() so Unity goes on with its pipeline and clears the RenderTexture.active with the default solid color you chose for your camera ! (or skybox or whatever)
You could think it's my fault, that people at Unity were nice and offered the opportunity for the user to specify the RenderTexture where the scene will be rendered but it's really a bug since after the clear occurs, the scene is rendered to the real RenderTexture the scene should render to.
It means the Unity pipeline is faulty and goes like this :
1] Call user's OnPreRender()
=> User changes RenderTexture.active and returns
2] Clear RenderTexture.active with solid color/sky box/whatever
(**without** bothering to reset the RenderTexture.active to the actual scene RenderTexture, so it clears YOUR RenderTexture)
3] Sets the actual scene RenderTexture (finally)
4] Renders the scene
5] Call user's OnRenderImage()
=> At this point, re-using the last RenderTexture set in OnPreRender() yields a wrong result
For now, I go around the bug by doing this :
void OnPreRender()
{
RenderTexture Old = RenderTexture.active; // Backup
(...)
Do some stuff with RenderTextures
(...)
RenderTexture.active = Old; // Restore. **Don't** set RenderTexture.active to null or your scene's RenderTexture won't be cleared !
}
Hope that helps !
Your answer
Follow this Question
Related Questions
Prevent Render Texture clearing 1 Answer
CommandBuffer.ClearRenderTarget(true, false, Color.clear, 1f) produces black depth texture? 0 Answers
OnRenderImage disable MRT(Multiple Render Targets)? 0 Answers
My Render Texture is not rendering World Space Canvas UI objects 0 Answers
glReadPixels from a RenderTexture 3 Answers