Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

One tone transparent blending

Discussion in 'Shaders' started by mouurusai, Feb 11, 2014.

  1. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    349
    Hello everyone!
    How to mix transparent sprites to achieve something like this?
    $discard.jpg
    I think i tried all variants "Blend SrcFactor DstFactor" and result almost in all cases like this(Darkest color in place where sprite overlayed each other).
    $alpha.jpg
    I do the first picture with 2 cameras and this shader, bud it's creepy.
    Code (csharp):
    1.  
    2. Shader "Unlit/TransparentAlphatestShadow"
    3. {
    4. Properties
    5. {
    6.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    7.     _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    8.     _Alpha ("Alpha", Range(0,1)) = 0.5
    9. }
    10.  
    11. SubShader
    12. {
    13.     Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    14.    
    15.     ZWrite On
    16.     ColorMask 0
    17.    
    18.     Pass
    19.     {
    20.         CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.            
    24.             #include "UnityCG.cginc"
    25.  
    26.             struct appdata_t
    27.             {
    28.                 float4 vertex : POSITION;
    29.                 float2 texcoord : TEXCOORD0;
    30.             };
    31.  
    32.             struct v2f
    33.             {
    34.                 float4 vertex : SV_POSITION;
    35.                 half2 texcoord : TEXCOORD0;
    36.             };
    37.  
    38.             sampler2D _MainTex;
    39.             float4 _MainTex_ST;
    40.             fixed _Cutoff;
    41.            
    42.             v2f vert (appdata_t v)
    43.             {
    44.                 v2f o;
    45.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    46.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    47.                 return o;
    48.             }
    49.            
    50.             fixed4 frag (v2f i) : COLOR
    51.             {
    52.                 fixed4 col = tex2D(_MainTex, i.texcoord);
    53.                 clip(col.a - _Cutoff);
    54.                 return 0;
    55.             }
    56.         ENDCG
    57.     }
    58.  
    59.  
    60.     ZWrite Off
    61.     Blend DstAlpha OneMinusSrcAlpha
    62.    
    63.     ColorMask rgba
    64.    
    65.     Pass
    66.     {      
    67.         CGPROGRAM
    68.             #pragma vertex vert
    69.             #pragma fragment frag
    70.            
    71.             #include "UnityCG.cginc"
    72.  
    73.             struct appdata_t {
    74.                 float4 vertex : POSITION;
    75.                 float2 texcoord : TEXCOORD0;
    76.             };
    77.  
    78.             struct v2f {
    79.                 float4 vertex : SV_POSITION;
    80.                 half2 texcoord : TEXCOORD0;
    81.             };
    82.  
    83.             sampler2D _MainTex;
    84.             float4 _MainTex_ST;
    85.             fixed _Cutoff;
    86.             fixed _Alpha;
    87.            
    88.             v2f vert (appdata_t v)
    89.             {
    90.                 v2f o;
    91.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    92.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    93.                 return o;
    94.             }
    95.            
    96.             fixed4 frag (v2f i) : COLOR
    97.             {
    98.                 fixed4 col = tex2D(_MainTex, i.texcoord);
    99.                 return fixed4(0, 0, 0, _Alpha*step(0, col.a - _Cutoff));
    100.             }
    101.         ENDCG
    102.     }
    103. }
    104.  
    105. }
    106.  
    Have any way to get one tone transparent shadows without render to texture?
    Thanks in advance!
     
    Last edited: Feb 11, 2014
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    Two cameras is a reasonable solution to the issue. If you want a single camera solution, and have pro, you can use the stencil buffer to avoid writing to the framebuffer in an area that has already been written to.
     
  3. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    349
    kru, thank you very much. It's good reason to learn how stencil stuff work.
     
  4. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    In the pictured case, you could use BlendOp Min to avoid darkening any fragment with more than one result.

    Unfortunately (and this isn't mentioned in the documentation), the Min operation doesn't take the Blend parameters into account. So unless you have the background available in your shader, as it could be in your example, there's no way to use Min to get what you want.
     
  5. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    You know.. you could also simply turn on ZWrite and set ZTest to LEqual
     
  6. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    349
    Dolkar, I can't understand mechanism of this method. May you explain little more detailed?
    upd
    Nevermind, I got it. Thank you!
     
    Last edited: Feb 12, 2014