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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Alpha blending issues

Discussion in 'Shaders' started by LoK4t, Jan 9, 2016.

  1. LoK4t

    LoK4t

    Joined:
    May 27, 2015
    Posts:
    9
    I've been googling around and trying a million different solutions, but I simply can't wrap my head around the solution of this problem I'm having.

    What I'm doing is, using a mesh I mask out areas of a sprite (using stencil buffer). I then proceed to mask out an area of where I want the mesh to be visible. Then in a separate shader, attached in a material to the same mesh as the one with the first mask, I colour it in and based on the distance from the center of the object, and a falloff factor I blend the color from _Color to _ShadowColor. In order to make it blend nicely in with the sprite i masked out, I clamp the alpha value to make it display properly. However the problem comes when two meshes overlap each other as visible here:


    Obviously there's an alpha blending problem going on, and I can't figure out how to work around it. Any help is appreciated!

    EDIT:
    Here's the shader code I'm using for the colouring of the lightMesh:
    Code (CSharp):
    1. Shader "Custom/Light Map"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Tint", Color) = (1,1,1,1)
    6.         _ShadowColor ("Shadow color", Color) = (0, 0, 0, 1)
    7.         _Ref ("Stencil ref", Int) = 1
    8.         _Mask ("Stencil mask", Int) = 1
    9.         _LightCenter ("Light center", Vector) = (0, 0, 0, 1)
    10.         _LightRadius ("Light radius", Float) = 5
    11.         _Falloff("Falloff", Float) = 1
    12.         _Tex ("Tex", 2D) = "white" {}
    13.     }
    14.     SubShader
    15.     {
    16.         Tags
    17.         {
    18.             "Queue"="Transparent"
    19.             "IgnoreProjector"="True"
    20.             "RenderType"="Transparent"
    21.         }
    22.  
    23.         Cull Back
    24.         Lighting Off
    25.         ZWrite Off
    26.         Blend SrcAlpha OneMinusSrcAlpha
    27.  
    28.         Stencil
    29.         {
    30.             Ref 1
    31.             ReadMask 1
    32.             Comp equal
    33.         }
    34.  
    35.         Stencil
    36.         {
    37.             Ref 2
    38.             ReadMask 2
    39.             Comp equal
    40.         }
    41.  
    42.         Pass
    43.         {
    44.         CGPROGRAM
    45.             #pragma vertex vert
    46.             #pragma fragment frag
    47.             #include "UnityCG.cginc"
    48.            
    49.             struct appdata_t
    50.             {
    51.                 float4 vertex   : POSITION;
    52.                 float4 color    : COLOR;
    53.             };
    54.  
    55.             struct v2f
    56.             {
    57.                 float4 vertex   : SV_POSITION;
    58.                 fixed4 color    : COLOR;
    59.                 float2 dist : TEXCOORD1;
    60.             };
    61.  
    62.             sampler2D _Tex;
    63.             fixed4 _Color;
    64.             fixed4 _ShadowColor;
    65.             uniform float4 _LightCenter;
    66.             uniform float _LightRadius;
    67.             uniform float _Falloff;
    68.  
    69.             v2f vert(appdata_t IN)
    70.             {
    71.                 v2f OUT;
    72.                 OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
    73.                 OUT.color = IN.color * _Color;
    74.                 OUT.dist.x = distance(mul(_Object2World, IN.vertex).xy, _LightCenter.xy);
    75.                 return OUT;
    76.             }
    77.  
    78.             fixed4 frag(v2f IN) : SV_Target
    79.             {
    80.                 float dist = IN.dist.x;
    81.                 fixed4 sCol = _ShadowColor;
    82.                 fixed4 col = lerp(_Color, sCol, _Falloff *  dist / _LightRadius);
    83.                 if(_Color.a > _ShadowColor.a)
    84.                 {
    85.                     col.a *= smoothstep(sCol.a, _Color.a, col.a);
    86.                     col.a = clamp(col.a, sCol.a, _Color.a);
    87.                 }
    88.                 else
    89.                 {
    90.                     col.a *= smoothstep(_Color.a, sCol.a, col.a);
    91.                     col.a = clamp(col.a, _Color.a, sCol.a);
    92.                 }
    93.                 fixed4 c = tex2D(_Tex, float2(0, 0)) * col;
    94.                 c.rgb *= c.a;
    95.                 return c;
    96.             }
    97.         ENDCG
    98.         }
    99.     }
    100. }
    101.  
     
    Last edited: Jan 9, 2016
  2. LoK4t

    LoK4t

    Joined:
    May 27, 2015
    Posts:
    9
    Bump :/