Is there some way in unity can let us write a opaque version and a transparent version shader in one shader file?
Currently, i write a opaque shader for character. The character need to be transparent in game in some case. I can achieve this by simply copy the shader code write an other transparent version. but the code of the two shader almost the same except the blend mode is different.
So, my question is that is there some possible way to turn blend mode on and off by script in game dynamically. In this way, only one shader file is enough, then i don’t need another copy to make a transparent version.
Or, for a simple method which might be good for you just create an include with all your code it in and a simple wrapper for the Transparent and Opaque versions.
thank you for you reply, I do read the standard shader。 there something in the standard shader :
The Properties section have those lines
// Blending state
[HideInInspector] _Mode (“__mode”, Float) = 0.0
[HideInInspector] _SrcBlend (“__src”, Float) = 1.0
[HideInInspector] _DstBlend (“__dst”, Float) = 0.0
[HideInInspector] _ZWrite (“__zw”, Float) = 1.0
and the Base forward pass section have this
Pass
{
Name “FORWARD”
Tags { “LightMode” = “ForwardBase” }
Blend [_SrcBlend] [_DstBlend]
I think standard shader contorl the _SrcBlend and _DstBlend, by select different _Mode.
My doubt is that in the standard shader, whether is the Blend operation always turn on, even if it is Opaque mode ?
The default blend mode for the Standard shader is Blend Zero One Blend One Zero; or Blend 1.0 0.0, and yes I know that’s confusing. It’s because the numbers being set are enum keys and “One” is the first key (0) and “Zero” is the second (1). As this blend mode has identical results to not having a blend mode I would hope Unity’s code under the hood is smart enough to know to turn off blending.
Even if it’s not a simple blend like that isn’t likely to cause any problems. Most of the performance benefits of opaque geometry rendering actually comes from the zwrite and front to back rendering order.
edit: Derp, had those flipped. Not as confusing as it seems, the documentation just lists them out of order. They’re still an enum key, but it is actually in an order such that one and zero make sense.
That’s probably not even needed. The graphics driver probably does that as an optimization. (It does on desktop.) It’s a very common topic here and I’m also not 100% sure for mobile platforms. But I’m betting they all turn it off automatically.
Sorry for coming back late, I’ve been on holiday, but anyway, the bit you need to look at is the custom editor, have a look at the section SetupMaterialWithBlendMode all will hopefully become clear, basically you need the custom editor as it sets up all the defines for the shader.
I also make an experiment on ipad air 2 with unity 4.6.7 and unity 5.1.2.
It seems unity is smart enough to turn alpha blending off when the blend factor is one zero.
the custom editor solution can solve my problem.
If in doubt to what Unity is doing, click on the shader in Unity, then select compile and show code, it will generate the code for all the variations in GLSL, you can then review and see what it’s doing, it’s a great way to optimise your shaders.