<p><img src="https://user-images.githubusercontent.com/16706911/34399964-ae3c684a-eba1-11e7-95f1-2696a37b650c.JPG" alt="image"></p>

<p>I used <a href="https://docs.unity3d.com/Manual/SL-Stencil.html" rel="nofollow noreferrer">Stencil Buffer</a> to fixing your problem , you need a way for checking <a href="https://stackoverflow.com/questions/35574227/unity-shader-highlighting-overlaps">overlapping two or more shapes</a></p>

<pre><code> Shader "Custom/SemiTransparent"
{
    Properties
    {
        _Color("Color",Color) = (0,0,1,0.1)
    }
    SubShader
    {
Tags {"Queue"="Transparent" "IgnoreProjector"="true" "RenderType"="Transparent"}
ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Cull Off

        LOD 100

        Pass
        {
            Stencil {
                Ref 0
                Comp Equal
                Pass IncrSat 
                Fail IncrSat 
            }

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            fixed4 _Color;

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = _Color;
                return col;
            }
            ENDCG
        }
    }
}
</code></pre>

CC BY-SA 4.0
2017-12-28T04:17:42.153Z
2018-05-02T13:23:09.923Z
2018-05-02T13:23:09.923Z
Seyed Morteza Kamali
<p>I am working on a 2D lighting system for my game. My approach has been to:</p>

<ol>
<li>Create a transparency quad representing 'darkness'.</li>
<li>Create a stencil shader for light sources to clip from the darkness quad.</li>
<li>Create a shader that blends alpha from 0 -> {darkness alpha} to soften the edges of the light source.</li>
</ol>

<p>This looks like the following:</p>

<p><a href="https://i.sstatic.net/vZ77t.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/vZ77t.png" alt="enter image description here"></a></p>

<p>You can see the problem - the two light source shaders that soften the shadow edges are being blended like normal transparency layers should and causing the colour to intensify. Here is another image without the darkness quad, which might make things a bit clearer:</p>

<p><a href="https://i.sstatic.net/bpC3M.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/bpC3M.png" alt="enter image description here"></a></p>

<p>My question: Is there any sort of alpha blending I can do to make this intersection appear 'normal' i.e. it doesn't intensify the color? If not, is there another approach which could help with this?</p>

<p><strong>Update:</strong></p>

<p>Here is the shader code that generates the fade on the light source - note that the _Radius is set for each light source. </p>

<pre><code>Properties
{
    _Color("Bottom Color", Color) = (1,1,1,1)
    _Radius("Radius", Float) = 1
}

SubShader
{
    Tags
    { 
        "RenderType" = "Transparent" 
        "IgnoreProjector" = "True"
        "Queue" = "Transparent" 
    }

    Cull Back
    Blend One OneMinusSrcAlpha

    Pass
    {
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag

        #include "UnityCG.cginc"

        struct appdata
        {
            float4 vertex : POSITION;
        };

        struct v2f
        {
            float4 vertex : SV_POSITION;
            float distance : float2;
        };

        fixed4 _Color;
        float _Radius;

        v2f vert(appdata v)
        {
            v2f o;
            o.vertex = UnityObjectToClipPos(v.vertex);
            o.distance = distance(float3(0, 0, 0), v.vertex);
            return o;
        }

        fixed4 frag(v2f i) : SV_Target
        {               
            float distance = (i.distance / _Radius); // 0 -&gt; 1 as progressing to outside
            float normalizedDistance = 1 - distance; // 1 -&gt; 0 as progressing to outside 

            // 0 -&gt; 0.43
            fixed colorOffset = lerp(0, _Color.a, distance);

            // return alpha 
            return fixed4(0, 0, 0, colorOffset);
        }

        ENDCG
    }
}
</code></pre>

CC BY-SA 3.0
2017-03-06T15:26:17.937Z
I deleted my answer as I think that a blending solution won't fix this problem.  I think you probably need to utilize the stencil buffer somehow to avoid writing the darkness to the same area twice.
default
CC BY-SA 3.0
2017-03-06T15:28:36.117Z
Yes, I have thought the same. I will pursue that idea for the moment. Thanks for trying.
Alex
CC BY-SA 3.0
2017-03-08T06:41:27.103Z
You could try using Max blending : http://stackoverflow.com/questions/2143690/is-it-possible-to-achieve-maxas-ad-opengl-blending
PaulHK
<shader><transparency><unity><blending>
Alpha blending between two overlapping semi-transparent shapes
Alex
# Alpha blending between two overlapping semi-transparent shapes

(Asked by: username_0 on 2017-03-03)

I am working on a 2D lighting system for my game. My approach has been to:

1. Create a transparency quad representing 'darkness'.
2. Create a stencil shader for light sources to clip from the darkness quad.
3. Create a shader that blends alpha from 0 -> {darkness alpha} to soften the edges of the light source.

This looks like the following:

[![enter image description here](https://i.sstatic.net/vZ77t.png)](https://i.sstatic.net/vZ77t.png)

You can see the problem - the two light source shaders that soften the shadow edges are being blended like normal transparency layers should and causing the colour to intensify. Here is another image without the darkness quad, which might make things a bit clearer:

[![enter image description here](https://i.sstatic.net/bpC3M.png)](https://i.sstatic.net/bpC3M.png)

My question: Is there any sort of alpha blending I can do to make this intersection appear 'normal' i.e. it doesn't intensify the color? If not, is there another approach which could help with this?

**Update:**

Here is the shader code that generates the fade on the light source - note that the \_Radius is set for each light source.

```
Properties
{
    _Color("Bottom Color", Color) = (1,1,1,1)
    _Radius("Radius", Float) = 1
}

SubShader
{
    Tags
    { 
        "RenderType" = "Transparent" 
        "IgnoreProjector" = "True"
        "Queue" = "Transparent" 
    }

    Cull Back
    Blend One OneMinusSrcAlpha

    Pass
    {
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag

        #include "UnityCG.cginc"

        struct appdata
        {
            float4 vertex : POSITION;
        };

        struct v2f
        {
            float4 vertex : SV_POSITION;
            float distance : float2;
        };

        fixed4 _Color;
        float _Radius;

        v2f vert(appdata v)
        {
            v2f o;
            o.vertex = UnityObjectToClipPos(v.vertex);
            o.distance = distance(float3(0, 0, 0), v.vertex);
            return o;
        }

        fixed4 frag(v2f i) : SV_Target
        {               
            float distance = (i.distance / _Radius); // 0 -> 1 as progressing to outside
            float normalizedDistance = 1 - distance; // 1 -> 0 as progressing to outside 

            // 0 -> 0.43
            fixed colorOffset = lerp(0, _Color.a, distance);

            // return alpha 
            return fixed4(0, 0, 0, colorOffset);
        }

        ENDCG
    }
}

```

**Tags:** shader, transparency, unity, blending

> **Comments:**
> **username_1**: I deleted my answer as I think that a blending solution won't fix this problem.  I think you probably need to utilize the stencil buffer somehow to avoid writing the darkness to the same area twice.  
> **username_0**: Yes, I have thought the same. I will pursue that idea for the moment. Thanks for trying.  
> **username_2**: You could try using Max blending : http://stackoverflow.com/questions/2143690/is-it-possible-to-achieve-maxas-ad-opengl-blending  

---

## 1 answers

### Answer by username_3 on 2017-12-28. Score: 1

![image](https://user-images.githubusercontent.com/16706911/34399964-ae3c684a-eba1-11e7-95f1-2696a37b650c.JPG)

I used [Stencil Buffer](https://docs.unity3d.com/Manual/SL-Stencil.html) to fixing your problem , you need a way for checking [overlapping two or more shapes](https://stackoverflow.com/questions/35574227/unity-shader-highlighting-overlaps)

```
 Shader "Custom/SemiTransparent"
{
    Properties
    {
        _Color("Color",Color) = (0,0,1,0.1)
    }
    SubShader
    {
Tags {"Queue"="Transparent" "IgnoreProjector"="true" "RenderType"="Transparent"}
ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Cull Off

        LOD 100

        Pass
        {
            Stencil {
                Ref 0
                Comp Equal
                Pass IncrSat 
                Fail IncrSat 
            }

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            fixed4 _Color;

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = _Color;
                return col;
            }
            ENDCG
        }
    }
}

```


---

