- Home /
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.
I realized I made a mistake in the way I was using the function. I replaced "Opaque" by "RenderType" like so: gameObject.camera.RenderWithShader( repl, "RenderType" and now my geometry is being rendered pink in the render texture. But only once. If I move the camera a bit, the content of the render texture doesn't change because OnPostRender isn't called more than once.
Replacing OnPostRender with OnPreRender makes no difference to those symptoms.
Answer by taoa · Jun 15, 2010 at 01:05 PM
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.
Hello. I know this is an old answer, but I'm having a similar issue. Could you clarify which one of taoa's issues is a bug? The OnPostRender thing or using RenderWithShader entirely?
Answer by Tetrad · Jun 06, 2010 at 09:41 PM
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.
Your answer
Follow this Question
Related Questions
Will Camera.RenderWithShader work with Graphics.DrawMesh (and DrawMeshInstanced)? 0 Answers
I cannot pass parameters to my custom shaders... help please... 2 Answers
Displaying a different shader ONLY where a spotlight hits an object. 1 Answer
render to framebuffer twice - how? 0 Answers
Setting Material Tag at runtime for replacement shader tricks 1 Answer