Assertion when attaching post effects to a temporary camera with a replacement shader

I have a behaviour that periodically renders the scene with a replacement shader into a render texture. It does this by creating a new temporary camera and calling ‘RenderWithShader’:

var camera = new GameObject("TemporaryCamera", typeof(Camera));
// ...
camera.camera.targetTexture = myTargetTexture;
camera.camera.RenderWithShader(myShader, null);
DestroyImmediate(camera);

This seems to work perfectly.

I would now like to apply some post effects to this camera before the final image is output into the given render texture.

My initial approach was to first create a new behaviour that implements the ‘OnRenderImage’ method:

public class MyPostEffect : MonoBehaviour
{
	public void OnRenderImage( RenderTexture source, RenderTexture destination )
	{
		Debug.Log( "In MyPostEffect::OnRenderImage" );
	}
}

And then add this behaviour to the code that renders the scene into the render texture:

var camera = new GameObject("TemporaryCamera", typeof(Camera), typeof(MyPostEffect));

This approach is causing me some problems:

  • An assertion is triggerred in ‘UnityEngine.Camera:RenderWithShader’ with the following message ‘screenviewcoord[1] < 0 || screenviewcoord[3] < 0’
  • My ‘OnRenderImage’ method doesn’t seem to be getting called (i.e. the Debug.Log line in ‘MyPostEffect:OnRenderImage’ doesn’t appear in the output window)

The assertions do not appear if I comment out the ‘OnRenderImage’ method in ‘MyPostEffect’. I have searched the forum for similar problems - a couple of posts are returned but these either relate to previous versions of Unity, or are reproduced in slightly different conditions to mine.

So I was wondering if this is a known issue and if so, if there is a known workaround for this issue? I am using the latest version of Unity 3 Pro on Windows.

Any help would be much appreciated!

Thanks,

James.

Update: I have now created a minimal project that reproduces this issue and have attached this to a bug report