Wrong alpha blending in shader

Wrong alpha blending in shader

Hi, i’m trying to achieve simple effect: given color of texture to be transparent. I wrote following shader for this:

Shader "alpha" 
{
    Properties 
	{
		 _MainTex ("Base (RGB)", 2D) = "white" { }
    }
    SubShader 
	{
        Pass 
		{
			Blend SrcAlpha OneMinusSrcAlpha // turn on alpha blending
					
			CGPROGRAM

#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#include "UnityCG.cginc"

sampler2D _MainTex;

struct v2f {
    V2F_POS_FOG;
    float2  uv : TEXCOORD0;
};


v2f vert (appdata_base v)
{
    v2f o;
	PositionFog( v.vertex, o.pos, o.fog );
    o.uv = TRANSFORM_UV(0);
    return o;
}

half4 frag (v2f i) : COLOR
{
	half4 texcol = tex2D( _MainTex, i.uv );
	if (texcol.x == 1  texcol.z == 1) texcol.w = 0.0; // here i use (255,0,255) color as a mask
	return texcol;
}

ENDCG

        }
    }
}

Unfortunately the results I’m getting are weird, instead of alpha blending I get artefacts on the screen (look screen shot).

Have I missed something?

P.S. I know all about TGA/PSD/ALpha channel and etc. In this example I need to make transparence by some color in picture (some kind of REAR Projection).

258675--9296--$weird_alpha1_264.jpg

Just so I’m sure: the picture you posted is of one mesh with your shader on it, right? You’re seeing black where you expect transparency?

YES!

Next image will see that actually alpha exists on image, cause we can see objects follows behind the plane with image (look at the red arrows), but transparent area have too much black color.

How to make it completely transparent?

P.S. I left source picture at the bottom.

258881--9307--$weird_alpha2_341.jpg
258881--9308--$image_a_116.gif

Ok, you probably need to turn ZWrite Off (default is on) and set the queue to transparent so your object gets drawn after opaque things. Add the following code to your Subshader block:

ZWrite Off
Tags {"Queue" = "Transparent" }

You’ll still have a magenta fringe to deal with. I think all you can do is turn the filtering mode of the source texture to “point” in the texture importer.

Thanks!

Standart shader “vegetation 2 pass” was solved all problems.