- Home /
Blit isn't writing to render texture
Hello,
I found a nice Gaussian Blur shader online and am attempting to use it to blur a render texture using blit(). However, the render texture remains unblurred. I know for a fact the blur shader works as I've used it to blur the main camera, but it just doesn't seem to want to blit() my render texture.
C# Blur Script:
void Update()
{
if (_material == null)
{
_material = new Material(_shader);
_material.hideFlags = HideFlags.HideAndDontSave;
}
RenderTexture rt1, rt2;
if (_downSampleMode == DownSampleMode.Half)
{
rt1 = RenderTexture.GetTemporary(source.width / 2, source.height / 2);
rt2 = RenderTexture.GetTemporary(source.width / 2, source.height / 2);
Graphics.Blit(source, rt1);
}
else if (_downSampleMode == DownSampleMode.Quarter)
{
rt1 = RenderTexture.GetTemporary(source.width / 4, source.height / 4);
rt2 = RenderTexture.GetTemporary(source.width / 4, source.height / 4);
Graphics.Blit(source, rt1, _material, 0);
}
else
{
rt1 = RenderTexture.GetTemporary(source.width, source.height);
rt2 = RenderTexture.GetTemporary(source.width, source.height);
Graphics.Blit(source, rt1);
}
for (var i = 0; i < _iteration; i++)
{
Graphics.Blit(rt1, rt2, _material, 1);
Graphics.Blit(rt2, rt1, _material, 2);
}
Graphics.Blit(rt1, source);
RenderTexture.ReleaseTemporary(rt1);
RenderTexture.ReleaseTemporary(rt2);
}
Potentially Useful information:
The render texture is being used as a depth map
it is constantly being written to
Answer by Namey5 · Jan 09, 2019 at 10:15 AM
I'm assuming 'source' is just a global variable that you assign in the inspector. If that is the case, I would recommend having a separate RenderTexture as an output. I'm not entirely sure of the outcome of blurring recursively into the same texture. I'm also not sure how well Graphics.Blit () works in Update. It is generally meant to be used in OnRenderImage (which would be how you blurred the camera output before). If the script is attached to the camera, you could probably get away with;
void OnRenderImage(RenderTexture src, RenderTexture dest)
{
if (_material == null)
{
_material = new Material(_shader);
_material.hideFlags = HideFlags.HideAndDontSave;
}
RenderTexture rt1, rt2;
if (_downSampleMode == DownSampleMode.Half)
{
rt1 = RenderTexture.GetTemporary(source.width / 2, source.height / 2);
rt2 = RenderTexture.GetTemporary(source.width / 2, source.height / 2);
Graphics.Blit(source, rt1);
}
else if (_downSampleMode == DownSampleMode.Quarter)
{
rt1 = RenderTexture.GetTemporary(source.width / 4, source.height / 4);
rt2 = RenderTexture.GetTemporary(source.width / 4, source.height / 4);
Graphics.Blit(source, rt1, _material, 0);
}
else
{
rt1 = RenderTexture.GetTemporary(source.width, source.height);
rt2 = RenderTexture.GetTemporary(source.width, source.height);
Graphics.Blit(source, rt1);
}
for (var i = 0; i < _iteration; i++)
{
Graphics.Blit(rt1, rt2, _material, 1);
Graphics.Blit(rt2, rt1, _material, 2);
}
Graphics.Blit(rt1, source);
Graphics.Blit (src, dest);
RenderTexture.ReleaseTemporary(rt1);
RenderTexture.ReleaseTemporary(rt2);
}