- Home /
Multiple Blit problem
I'm having a weird problem when trying to do a Blit to a renderTexture followed by a Blit to destination:
Void OnRenderImage (RenderTexture source, RenderTexture destination)
{
RenderTexture render1 = new RenderTexture (source.width,
source.height, source.depth);
Graphics.Blit(source, render1, mat1);
mat2.SetTexture("_SecTexture", render1);
Graphics.Blit(source, destination, mat2);
render1=null;
}
This is causing an increase slow down that eventually just crashes unity. Doing only one of the Blits seems to work normally, but when i do two, i'm having problems. I searched a lot for an answer and didn't find it. Help!
Answer by carlosrcp · Aug 30, 2018 at 01:46 PM
Leaving this to help anyone with the same problem, the way to do this is using temporary rendertextures. Something like this will do it:
RenderTexture renderTemp = RenderTexture.GetTemporary(source.width, source.height);
with
RenderTexture.ReleaseTemporary(renderTemp);
at the end. I hope this helps :D
Answer by Looooooooong · Apr 30, 2020 at 05:43 PM
Because RenderTexture
is an Unity's Object
, it doesn't get garbage-collected even if you set render1=null
. Multiple call to new RenderTexture()
(by calling OnRenderImage
multiple times in this case) will eat up your RAM, hence, it will crash Unity. To free up the space consumed by RenderTexture
, you should call Destroy(render1)
.
Or alternatively, as you already know, just use temporary RenderTexture
.