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.

[2D] issue with overlapping transparent objects

Discussion in 'Shaders' started by SleepyDevil, Sep 15, 2015.

  1. SleepyDevil

    SleepyDevil

    Joined:
    Aug 5, 2013
    Posts:
    30
    Hey there Internet,

    i have hand drawn shadows in my 2D game, but the problem is that if two of those shadows overlap the transparency (of course) adds up. So somehow i need to prevent them from that.



    The only solution i found so far is to make use of the stencil buffer.
    But on my research i stumbled upon this link: https://mispy.me/unity-alpha-blending-overlap/
    The results don´t look that satisfying.
    Can anybody help me with this?
     
    mmonly likes this.
  2. Undertaker-Infinity

    Undertaker-Infinity

    Joined:
    May 2, 2014
    Posts:
    112
    Hi

    I would additively draw all lights to a separate buffer, then apply this buffer additively on top of the rendered scene.
    Bonus: you get to shrink the lights buffer to make everything faster.
     
  3. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,520
    A quick n dirty hack.
    Texture gives the shape, Color rgb the color and Color a the transparency.
    ClipAlpha works as is for Color black, needs adjustments for other colors.
    Code (CSharp):
    1. Shader "multiply2D"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _Color ("Tint", Color) = (1,1,1,1)
    7.         _MainTex ("Base (RGB), Alpha (A)", 2D) = "white" {}
    8.         _Stencil ("Stencil ID", Float) = 0
    9.         _ColorMask ("Color Mask", Float) = 15
    10.         _ClipAlpha ("ClipAlpha", Float) = 0.2
    11.     }
    12.  
    13.     SubShader
    14.     {
    15.         Tags
    16.         {
    17.             "Queue"="Transparent"
    18.             "IgnoreProjector"="True"
    19.             "RenderType"="Transparent"
    20.             "PreviewType"="Plane"
    21.             "CanUseSpriteAtlas"="True"
    22.         }
    23.      
    24.         Stencil
    25.         {
    26.             Ref [_Stencil]
    27.             Comp NotEqual
    28.             Pass Replace
    29.         }
    30.  
    31.         Cull Off
    32.         Lighting Off
    33.         ZWrite Off
    34.         ZTest [unity_GUIZTestMode]
    35.         Blend SrcAlpha OneMinusSrcAlpha
    36.         ColorMask [_ColorMask]
    37.  
    38.         Pass
    39.         {
    40.         CGPROGRAM
    41.             #pragma vertex vert
    42.             #pragma fragment frag
    43.  
    44.             #include "UnityCG.cginc"
    45.             #include "UnityUI.cginc"
    46.          
    47.             struct appdata_t
    48.             {
    49.                 float4 vertex   : POSITION;
    50.                 float4 color    : COLOR;
    51.                 float2 texcoord : TEXCOORD0;
    52.             };
    53.  
    54.             struct v2f
    55.             {
    56.                 float4 vertex   : SV_POSITION;
    57.                 fixed4 color    : COLOR;
    58.                 half2 texcoord  : TEXCOORD0;
    59.                 float4 worldPosition : TEXCOORD1;
    60.             };
    61.          
    62.             fixed4 _Color;
    63.             fixed4 _TextureSampleAdd;
    64.             fixed _ClipAlpha;
    65.  
    66.             v2f vert(appdata_t IN)
    67.             {
    68.                 v2f OUT;
    69.                 OUT.worldPosition = IN.vertex;
    70.                 OUT.vertex = mul(UNITY_MATRIX_MVP, OUT.worldPosition);
    71.  
    72.                 OUT.texcoord = IN.texcoord;
    73.              
    74.                 #ifdef UNITY_HALF_TEXEL_OFFSET
    75.                 OUT.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1);
    76.                 #endif
    77.              
    78.                 OUT.color = IN.color * _Color;
    79.                 return OUT;
    80.             }
    81.  
    82.             sampler2D _MainTex;
    83.  
    84.             fixed4 frag(v2f IN) : SV_Target
    85.             {
    86.                 half4 color = (tex2D(_MainTex, IN.texcoord) );
    87.                 color.rgb=_Color.rgb;
    88.                 color.a*=_Color.a;
    89.                 clip (color.a - _ClipAlpha);
    90.                 return color;
    91.             }
    92.         ENDCG
    93.         }
    94.     }
    95. }
     
    Vostrugin likes this.
  4. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    80
    https://computergraphics.stackexcha...tween-two-overlapping-semi-transparent-shapes
     
    Vostrugin likes this.
unityunity