Hey!
I want to blur a rendered image. Somehow it works and somehow not. Here is what (I think) I do:
- Blur the image from the camera horizontal (with the first pass of a shader).
- Blur the result of 1. vertical (with the second pass of a shader).
- return the result to a rendertexture.
But the result seems to be…
- returns a black image (??)
- returns a vertical blurred image from the camera (ignore input from 1.)
A few minutes ago I suddenly had a normal map from the mesh the camera is rendering in the resulting texture? Somehow the textures seem to mix up…
The C# code:
void OnRenderImage(RenderTexture source, RenderTexture dest)
{
RenderTexture blurUBuffer = RenderTexture.GetTemporary(512, 512, 0);
RenderTexture blurVBuffer = RenderTexture.GetTemporary(512, 512, 0);
//blur horizontal
blurDirectional(source, blurUBuffer, true);
//blur vertical
blurDirectional(blurUBuffer, blurVBuffer, false);
ImageEffects.Blit(blurVBuffer, dest);
blurUBuffer.Release();
blurVBuffer.Release();
}
private void blurDirectional(RenderTexture blurSource, RenderTexture blurDest, bool direction)
{
RenderTexture.active = blurDest;
float offset = 1.0f / (float)blurSource.width;
GL.PushMatrix();
GL.LoadOrtho();
//first pass: horizontal blur
//second pass: vertical blur
blurMaterial.SetPass(direction ? 0 : 1);
blurMaterial.SetFloat("_Offset", offset);
blurMaterial.SetTexture("_SourceTex", blurSource);
RenderQuad();
GL.PopMatrix();
}
private void RenderQuad()
{
GL.Begin(GL.QUADS);
GL.TexCoord2(0.0f, 0.0f); GL.Vertex3(-1.0f, -1.0f, 0.1f);
GL.TexCoord2(1.0f, 0.0f); GL.Vertex3(1.0f, -1.0f, 0.1f);
GL.TexCoord2(1.0f, 1.0f); GL.Vertex3(1.0f, 1.0f, 0.1f);
GL.TexCoord2(0.0f, 1.0f); GL.Vertex3(-1.0f, 1.0f, 0.1f);
GL.End();
}
Any major mistake here?
The blur shader basically only gets 5 weighted samples in one direction (one in each pass) and returns the sum.
Is it correct, that for a RenderTexture I need a texRECT sampler and for a normal Texture2D a tex2D sampler?