- Home /
Additional pass in surface shader (enable/disable pass)
Can't find overview info in docs about this case:
I have surface shader that implements custom Lighting function. After shading I want to render additional semi-transparent pass with custom fragment shader. How to do that?
And one more thing - I want to enable/disable this additional pass from scripts. Looks like tags can be used in this case, but how exactly?
Would it be good enough to set the condition in each fragment/vertex shader? You can easily do that with an int although I admit is not as clean as a conditional pass, which I would also love to see.
'if' statement in shader is not good approach - it will cause computation of two branches AFAI$$anonymous$$. I develop for mobile devices so performance is very important.
Answer by tanoshimi · Sep 29, 2013 at 07:06 AM
Just write your additional pass after your surface shader code:
Shader {
SubShader {
CGPROGRAM
// Surface shader
ENDCG
Pass {
CGPROGRAM
// Additional Vert/Frag shader
ENDCG
}
}
}
You can add custom keywords to a shader using the #pragma directive:
#pragma MY_KEYWORD
You can then add conditional code within the shader based on whether the keyword is defined, as:
#ifdef MY_KEYWORD
...
#endif
And you can change the keyword from script, using
Shader.EnableKeyword("MY_KEYWORD");
Shader.DisableKeyword("MY_KEYWORD");
(I've never personally tried enabling/disabling an entire pass like this, but I guess it would work).
Additional pass works when I put it after surface shader code. But #pragma and #ifdef doesn't work (compile-time syntax error). I have found that it's not possible to enable/disable predefines at runtime. Still looking for a way to toggle pass in runtime...