circular Texture blend

Hallo all you Shader guys
Im a big shader noob and tried to write my first. So far I am pretty happy with my result. The shader does a circular texture blend between 2 texture and the center of this circle is the player. Now the circle has really jagged edges (see link below ). Probably because I calculate it in the vertex programm. I tried to move the calculation to the fregmant part but without luck. Hope you can help me with that

https://dl.dropboxusercontent.com/u/10920821/Unity-Help/texture-blend.png

Shader "Element/RadioTextureBlend"
{
	Properties 
	{
		_MainTex ("Main Texture", 2D) = "white" {}
		_SecondTex ("Second Texture", 2D) = "black" {}
		_Radius ("Radius", Float) = 0
		_MyWorldPos ("my World Position", Vector) = (0, 0, 0, 0)
		_TargetPos ("Target World Position", Vector) = (0, 0, 0, 0)
		
	}

	SubShader {
		Pass
		{
			Tags {"Queue" = "Geometry"}

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			#include "UnityCG.cginc"
			
			uniform sampler2D _MainTex;
			uniform float4 _MainTex_ST;
			uniform sampler2D _SecondTex;
			uniform float4 _SecondTex_ST;
			
			uniform float4 _MyWorldPos;
			uniform float4 _TargetPos;
			uniform float _Radius;
			
			struct vertexInput
			{
				float4 vertex : POSITION;
				float4 texcoord : TEXCOORD0;
			};
			
			struct fragmentInput
			{
				float4 pos : SV_POSITION;
				half2 uv : TEXCOORD0;
				half2 uv2 : TEXCOORD1;
				float dist;
			};
			
			fragmentInput vert(vertexInput i)
			{
				fragmentInput o;
				o.pos = mul(UNITY_MATRIX_MVP, i.vertex);
				o.uv = TRANSFORM_TEX(i.texcoord, _MainTex);
				o.uv2 = TRANSFORM_TEX(i.texcoord, _SecondTex);
				
				//float x = _TargetPos.x - o.pos.x;
				//float y = _TargetPos.y - o.pos.y;
				//float z = _TargetPos.z - o.pos.z;
				//o.dist = sqrt( (x * x) + (y * y) + (z + z) );
				
				o.dist = distance(mul(_Object2World ,i.vertex).xyz, _TargetPos.xyz);
				
				return o;
			}
			
			half4 frag(fragmentInput i) : COLOR
			{
				if(i.dist - _Radius >= 0)
				{
					return tex2D(_MainTex, i.uv);
				}
				else
				{
					return tex2D(_SecondTex, i.uv2);
				}
			}
			
				
			ENDCG
		}
	}
}

I also get a warning about dist (fragmentInput), not to have a semantic. What is the best way to get rid of it?

The circle has jagged edges because 1) You’re doing the distance in the vertex shader, so it’s having to interpolate the distance between vertices (meaning it gets straight edges where the interpolation is) and 2) You’re not aliasing the circle at all, it’s a binary if/else so there isn’t any blending between the options.

The semantic error is because you haven’t given dist a semantic in the v2f struct (as in, everything you pass through needs a TEXCOORD semantic - unless it’s the vertex position which gets it’s own).

Give this a try… it will still have an aliased edge around the circle, but it should be more circle like…

Shader "Element/RadioTextureBlend"
{
    Properties 
    {
        _MainTex ("Main Texture", 2D) = "white" {}
        _SecondTex ("Second Texture", 2D) = "black" {}
        _Radius ("Radius", Float) = 0
        _MyWorldPos ("my World Position", Vector) = (0, 0, 0, 0)
        _TargetPos ("Target World Position", Vector) = (0, 0, 0, 0)
        
    }
 
    SubShader {
        Pass
        {
            Tags {"Queue" = "Geometry"}
 
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"

            uniform sampler2D _MainTex;
            uniform float4 _MainTex_ST;
            uniform sampler2D _SecondTex;
            uniform float4 _SecondTex_ST;
            
            uniform float4 _MyWorldPos;
            uniform float4 _TargetPos;
            uniform float _Radius;
            
            struct vertexInput
            {
                float4 vertex : POSITION;
                float4 texcoord : TEXCOORD0;
            };
            
            struct fragmentInput
            {
                float4 pos : SV_POSITION;
                half2 uv : TEXCOORD0;
                half2 uv2 : TEXCOORD1;
                float3 posW : TEXCOORD2;
            };
            
            fragmentInput vert(vertexInput i)
            {
                fragmentInput o;
                o.pos = mul(UNITY_MATRIX_MVP, i.vertex);
                o.uv = TRANSFORM_TEX(i.texcoord, _MainTex);
                o.uv2 = TRANSFORM_TEX(i.texcoord, _SecondTex);                
                o.posW = mul(_Object2World ,i.vertex).xyz;
                
                return o;
            }
            
            half4 frag(fragmentInput i) : COLOR
            {
                float dist = distance(i.posW, _TargetPos.xyz);
                if(dist - _Radius >= 0)
                {
                    return tex2D(_MainTex, i.uv);
                }
                else
                {
                    return tex2D(_SecondTex, i.uv2);
                }
            }
            
            ENDCG
        }
    }
}

Thanks Farfarer! This works great!