What's the good usage of RenderWithShader?

Hello

I'm trying to use the underrated Camera function called RenderWithShader.

For this, I've made a fake shader that just renders pink:

Shader "pink"
{
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        Pass { Color (1,0,1,0) }
    }
    FallBack "Specular"
}

and I want to use it in order to replace any material that uses a shader tagged "Opaque".

Here's my code attempt:

using UnityEngine;

public class RWStest : MonoBehaviour
{
    Shader repl = null;
    public RenderTexture RT = null;
    void Start( )
    {
        repl = Shader.Find( "pink" );
    }

    void OnPostRender( )
    {
        Debug.Log( "OnPostRender" );
        RT = RenderTexture.GetTemporary( Screen.width / 4, Screen.height / 4 );
        gameObject.camera.targetTexture = RT;
        gameObject.camera.RenderWithShader( repl, "Opaque" );
        gameObject.camera.targetTexture = null;

        RenderTexture.ReleaseTemporary( RT );
    }
}

Running this attached to the main camera produces strange results:

First, all I end up with is a dark grey screen.

Second, in the console, I only see my debug "OnPostRender" appear once. Even though it's still running. And no error message whatsoever.

If I move my RenderTexture.GetTemporary into an Update function, then a new temporary render texture gets created every frame (so Update is definitely called every frame) but none of those gets released (because OnPostRender seems to be called only once).

Finally, if I create a RenderTexture at Start instead of getting a temporary one like so:

public RenderTexture RT = null;
void Start( )
{
    RT = new RenderTexture( Screen.width / 4, Screen.height / 4, 32 );
    RT.Create( );
    repl = Shader.Find( "pink" );
}

I still end up with a dark grey screen, but I can see in the inspector the RenderTexture created (since I made it public) and it's basically my scene if it was totally empty (you know that neutral blue whenever you create a new scene).

Am I doing something wrong here? Is OnPostRender supposed to be called only once like that? Should RenderWithShader be called from a specific place? And don't tell me I can't use RenderTextures with it otherwise I fail to see the point of it :)

Or is this a bug?

Thank you.

It is a bug, it has been reported as such, and it isn't going to be fixed because it might hurt other things.

A workaround is to use another camera.

Are you absolutely sure it's only being called once? It's a common mistake to have "Collapse" turned on in the console log and only see the first time a log is put in the console.