After upgrade to Unity 5, my post effect fails

Hi and thanks for all your help. I’m not sure where to begin with this one.

Basically, inside of an image effect, during the OnRenderImage method, I create a camera, render with special shader, then delete it. This worked in Unity 4.

Can we no longer render a new camera within OnRenderImage of the main camera?

Here is the shader code:

Shader "Lemon Joker/Image Effects/LJ_AOFactor"
{
	Properties
	{
		_AOIntensity ("AO Intensity", Float) = 1.0
	}
	SubShader
	{
		Tags { "AOFactorType"="Properties" }
		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			float _AOIntensity;

			float4 vert(float4 v : POSITION) : SV_POSITION
			{
				return mul(UNITY_MATRIX_MVP, v);
			}

			float4 frag() : COLOR
			{
				return float4(_AOIntensity, 0, 0, 0);
			}
			ENDCG
		}
	}
}

here is the OnRenderImage code:

		[ImageEffectOpaque]
		void OnRenderImage (RenderTexture source, RenderTexture destination)
		{
            // Adjust size
            int height = Screen.height;
            int width = Screen.width;
            if (destination != null)
            {
                height = destination.height;
                width = destination.width;
            }

            if (outputTex.width != width || outputTex.height != height)
            {
                RecreateSurfaces(width, height);
            }

            var aoFactorTex = RenderTexture.GetTemporary(Screen.width, Screen.height, 16, RenderTextureFormat.RFloat);
            RenderAOFactor(aoFactorTex);

            Graphics.Blit(source, depthNormalTex, material);
 
            RenderHBAO(outputTex.GetNativeTexturePtr(), Util.MatrixToArray(GetComponent<Camera>().projectionMatrix),
                radius, bias, exponent, blurEnable, (int)blurIterations, sharpness, coarseAO, detailAO);

            Material blendMat = new Material(Shader.Find("Lemon Joker/Image Effects/BlendAO"));
            blendMat.mainTexture = source;
            blendMat.SetTexture("_AOTex", outputTex);
            blendMat.SetTexture("_AOFactorTex", aoFactorTex);
            Graphics.Blit(source, destination, blendMat);

            RenderTexture.ReleaseTemporary(aoFactorTex);
        }

and here is the code which is called from OnRenderImage:

private void RenderAOFactor(RenderTexture target)
        {
            var go = new GameObject("", typeof(Camera));
            Camera cam = go.GetComponent<Camera>();
            cam.CopyFrom(GetComponent<Camera>());
            cam.renderingPath = RenderingPath.Forward;
            cam.depthTextureMode = DepthTextureMode.None;
            cam.backgroundColor = Color.white;
            cam.targetTexture = target;
            cam.enabled = true;
            cam.RenderWithShader(Shader.Find("Lemon Joker/Image Effects/LJ_AOFactor"), "AOFactorType");
            DestroyImmediate(go);                
        }

If I remove RenderAOFactor, everything else works as expected. And here is a video of what the result of this camera render looks like. Its an RFloat texture format.

Bug Video

Then you get this image sometimes… what is the tire texture doing here?
It appears to me as some kind of bad memory pointer somewhere. Thank you for your help! I’ll be sure to return the favor.

Try creating the gameobject+camera only once. You can still call copyfrom and change all the other settings, and you can set enabled=false to prevent it from drawing as part of the normal pipeline.