Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Change Blend Mode in multi compile

Discussion in 'Shaders' started by joshuacwilde, Sep 21, 2020.

  1. joshuacwilde

    joshuacwilde

    Joined:
    Feb 4, 2018
    Posts:
    725
    Is there anyway I can change a shader blend mode based on multi compile?

    I was thinking something like this would work :

    Code (CSharp):
    1. #ifdef ALPHA_BLEND
    2.         Blend SrcAlpha OneMinusSrcAlpha
    3. #else
    4.         Blend SrcAlpha One
    5. #endif
    but it does not and gives me an error. Any ideas?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    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.
    https://forum.unity.com/threads/configurable-shaders-open-source-project.518966/

    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.
    https://github.com/Unity-Technologi...itor/Mono/Inspector/StandardShaderGUI.cs#L335
     
  3. BigFluffyCookie

    BigFluffyCookie

    Joined:
    Jul 1, 2020
    Posts:
    3
    @bgolus A question about that:
    To switch off blending, the Shaderlab syntax is

    Code (CSharp):
    1. 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

    Code (CSharp):
    1. Blend One Zero
    Or
    Code (CSharp):
    1. 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?
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    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.
     
    Garen_ likes this.