Change Blend Mode in multi compile

Is there anyway I can change a shader blend mode based on multi compile?

I was thinking something like this would work :

#ifdef ALPHA_BLEND
        Blend SrcAlpha OneMinusSrcAlpha
#else
        Blend SrcAlpha One
#endif

but it does not and gives me an error. Any ideas?

Nope. You can’t set render state, like the blend mode, via multi compiles. Those only work on things inside the CGPROGRAM / HLSLPROGRAM, and even then only things that aren’t other #pragma lines (at least until 2020.1).

What you can do is you can set the blend mode source and destination functions via material properties. For example see the configurable shader project for an example of that.

But that of course then requires the user to set the appropriate blend functions themselves. The solution to that is too use a custom material editor, like the built in Standard shaders use.

1 Like

@bgolus A question about that:
To switch off blending, the Shaderlab syntax is

Blend Off

but when supplying the blend factors via material properties in that way, one has to specify two enum values (or four, if setting separate blend functions for color and alpha). The visually equivalent Blend setting to no blending at all of course is

Blend One Zero

Or

Blend One Zero, One Zero

But does setting this actually disable blending, or will this still incur the performance hit of blending? Of course it’s possible (and desirable) that the driver catches this - but does Unity already guarantee that in this case blending isn’t turned on at all?

Last I checked it seems like either Unity or possibly more likely the graphics API is smart enough to recognize either as being “no blend”, as when I check those kinds of materials in RenderDoc or Nvidia Nsight they show as having the blend disabled.

1 Like