Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Blend Zero One/One Zero Question

Discussion in 'Shaders' started by jkrzes, Sep 10, 2016.

  1. jkrzes

    jkrzes

    Joined:
    Sep 10, 2016
    Posts:
    3
    Hi,

    I have a question about blending. I have to planes. First one is a bit on the left and second is on the right and in front of the previous one but they still overlap. The plane on the left has Default Material(Standard Shader) and the one on the right has Material with my custom shader. My custom shader look like this:

    Code (CSharp):
    1. Shader "Custom/Multiply" {
    2.     Properties{
    3.         _Color("Color", Color) = (1,1,1,1)
    4.         _MainTex("Albedo (RGB)", 2D) = "white" {}
    5.     }
    6.         SubShader{
    7.         Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True" }
    8.  
    9.         // inside Pass
    10.         ZWrite Off
    11.         Blend Zero One
    12.  
    13.             CGPROGRAM
    14.         #pragma surface surf Unlit alpha:blend
    15.  
    16.         #pragma target 3.0
    17.  
    18.         sampler2D _MainTex;
    19.  
    20.         struct Input {
    21.             float2 uv_MainTex;
    22.         };
    23.         fixed4 _Color;
    24.  
    25.         fixed4 LightingUnlit(SurfaceOutput s, fixed3 lightDir, float atten)
    26.         {
    27.             fixed4 c;
    28.             c.rgb = s.Albedo;
    29.             c.a = s.Alpha;
    30.  
    31.             return c;
    32.         }
    33.  
    34.         void surf(Input IN, inout SurfaceOutput o) {
    35.             // Albedo comes from a texture tinted by color
    36.             // tex2D(_MainTex, IN.uv_MainTex) *
    37.             fixed4 c = _Color;
    38.             o.Albedo = c.rgb;
    39.             o.Alpha = c.a;
    40.         }
    41.         ENDCG
    42.     }
    43.         FallBack "Diffuse"
    44. }
    45.  
    Currently the Plane with my custom shaders covers the plane behind. With Blend Zero One command I expected that color of the plane in front would be multiplied with zero and added to the destination color so I should see the plane with default material. With Blend One Zero command I expected that the color of the overlapped section of the planes would be the same as color of the plane on the right(section which is not overlapped). Obviously I am wrong. Could someone explain to me how Blend Zero One/One Zero really works. Thanks for any help.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,366
    Blend One Zero is equivalent to no blend, the resulting math is (Src * One) + (Dst * Zero) or (shader output * 1.0) + (frame buffer * 0.0). Inversely Blend Zero One is equivalent to rendering nothing and just showing the background as the shader output is multiplied by zero and the frame buffer is multiplied by one.

    Multiply would be Blend DstColor Zero, which means (shader output * frame buffer) + (frame buffer * zero).

    However surface shaders will override your blend mode when you use the alpha keyword.

    edit: You really want to be using a vertex / fragment shader for this instead of a surface shader. Surface shaders do a lot behind the scenes that you probably don't want.
     
    Last edited: Sep 10, 2016
    look001, Changemoon7 and jkrzes like this.
  3. jkrzes

    jkrzes

    Joined:
    Sep 10, 2016
    Posts:
    3

    I removed alpha keyword from the shader and now it blends as I expected. I've added #pragma debug to my shader and looked at compiled code. There was a line Blend SrcAlpha OneMinusSrcAlpha although I put Blend One Zero to my shader. You were right about alpha keyword overriding my blend command. Thank You very much for help
     
  4. bobozzz

    bobozzz

    Joined:
    May 27, 2015
    Posts:
    38
    Hi bgolus! I know this is an old thread, but I saw this thread when I search for "difference between Blend One Zero and Blend Off". I know the result is the same, but I wonder is there any performance difference between "blend off" and "blend one zero"? Will "blend one zero" cause more calculation? Thank you!
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,366
    At face value if it's actually implemented as described by the spec, ie: Src * SrcFactor + Dst * DstFactor, then "Blend One Zero" whould be more expensive. However it's unlikely most GPUs / graphics drivers actually do that and would likely fall back to "Blend Off" when presented with "Blend One Zero".

    But that doesn't matter since Unity automatically has the logic to disable the blend when a shader is set to "Blend One Zero", even if being set by shader properties.

    So, no, there are no additional calculations. They are exactly the same in practice, at least for Unity.
     
  6. bobozzz

    bobozzz

    Joined:
    May 27, 2015
    Posts:
    38
    Thank you bgolus!!! :)