[Solved] Problem getting custom blur image effect to work

Hey everyone,

I’ve been coding a very simple blur image effect to serve as a liquid renderer. Basically, a blur the image containing the liquid primitives and get all the pixels within a certain luminosity threshold. This way, the primitives seem to connect, giving a nice liquid-like appearence.

I’ve been doing this on my Mac running 5.4 just fine. Now, I’ve just copied the same script and the same configuration to Windows running the same Unity version, and the for loop to generate the blur simply, doesn’t work.

Here’s my code snippet:

public class LiquidImageEffect : MonoBehaviour 
{
    public Camera liquidCamera;
    private RenderTexture liquidCameraRT;
    private Material _imgEffectMat;
    public int blurPasses;
    [Range (0, 1)]
    public float luminanceThreshold;
    public float blurAmount;

    void Start ()
    {
        _imgEffectMat = new Material (Shader.Find ("Hidden/Liquid"));
        liquidCameraRT = new RenderTexture ((int)(Screen.width * 0.25F), (int)(Screen.height * 0.25F), 16);
        liquidCamera.targetTexture = liquidCameraRT;
    }

    void OnRenderImage (RenderTexture src, RenderTexture dst)
    {
        _imgEffectMat.SetFloat ("_Threshold", luminanceThreshold);
        RenderTexture liquid = RenderTexture.GetTemporary ((int)(Screen.width * 0.25F), (int)(Screen.height * 0.25F));
        liquid = liquidCameraRT;
        for (int i = 0; i < blurPasses; i++)
        {
			Graphics.Blit (liquid, liquid, _imgEffectMat, 0);
        }
        _imgEffectMat.SetTexture ("_ProcessedMilk", liquid);
        _imgEffectMat.SetFloat ("_BlurAmount", blurAmount);
        Graphics.Blit (src, dst, _imgEffectMat, 1);
    }	
}

*Code quality is questionable. The main purpose is to get it working first :slight_smile:

When I set blurPasses to 0, it “works”, meaning that the ‘liquid’ render texture gets combined with the screen texture on the line ‘Graphics.Blit (src, dst, _imgEffectMat, 1);’ but when it’s greather than zero, the liquid image simply disappears.

89504-blurpasses0.png
The effect above appears by setting blurPasses to 0. This is the ‘desired’ effect. The liquid being combined with the rest of the scene. The resolution of the liquid is lower on purpose.


This happens when I set blurPasses to something greather than 0. The white cubes are gone.

There’s definitely something wrong with the code, maybe because I’m passing source and destination as being ‘liquid’? But I’ve seen this before and it also works fine on my mac.

Any tips on how to get it to work right?

Solved it.

It is a little bit obvious, but in order to do this you need an array of RenderTexture RenderTexture[] passes, with Graphics.Blit (passes*, passes[i + 1], ...);*
Just don’t forget to allocate all the RenderTextures in the array as well.
It’s a silly mistake, but I’m not sure why it worked on the Mac.