1 Shader "Hidden/VFX/GradientDashedBorder"
5 _Border("Border",float) = 1
6 _Radius("Radius",float) = 1
7 _PixelScale("PixelScale",float) = 1
8 _Size("Size",Vector) = (100,100,0,0)
9 _ColorStart("ColorStart",Color) = (1,1,0,1)
10 _ColorEnd("ColorEnd", Color) = (0,1,1,1)
11 _ColorMiddle("ColorMiddle", Color) = (0,1,1,1)
15 Tags { "RenderType"="Transparent" "Queue"="Transparent"}
20 Blend SrcAlpha OneMinusSrcAlpha
28 #include "UnityCG.cginc"
32 float4 vertex : POSITION;
33 float2 uv : TEXCOORD0;
34 float2 uv2 : TEXCOORD1;
39 float2 uv : TEXCOORD0;
40 float4 vertex : SV_POSITION;
41 float4 pos : TEXCOORD2;
42 float2 clipUV : TEXCOORD1;
43 float height : TEXCOORD3;
44 float distance : TEXCOORD4;
55 uniform float4x4 unity_GUIClipTextureMatrix;
56 sampler2D _GUIClipTexture;
62 float2 size = _Size - float2(_Radius,_Radius);
64 float margingScale = 2 + (_Border/_Radius /_PixelScale);
66 o.pos = float4(v.vertex.xy * size + v.uv* margingScale * v.vertex.xy* _Radius, 0, 0);
67 o.height = (v.vertex.y + 1)* 0.5;
68 o.vertex = UnityObjectToClipPos(o.pos);
69 o.uv = v.uv*margingScale;
70 float3 eyePos = UnityObjectToViewPos(o.pos );
71 o.clipUV = mul(unity_GUIClipTextureMatrix, float4(eyePos.xy, 0, 1.0));
74 float2 dist = v.uv2 * _Size;
75 o.distance = dist.y + dist.x;
79 fixed4 frag(v2f i) : SV_Target
81 if (fmod(i.distance + 10,20) > 10)
84 float pixelScale = 1.0f/abs(ddx(i.pos.x));
86 float realRadius = (_Radius - _Border * 0.5 - 0.5); // radius at the center of the line. -0.5 to keep space for AA
87 float2 uvCenter = i.uv * _Radius / realRadius; // uv expressed in realRadius instead of _Radius
88 //float uvDist = 1-abs(1-length(uvCenter)); //
89 float uvDist = length(uvCenter); // distance to center expressed in realRadius
90 float uvBorder = _Border*0.5f / realRadius; // half border width expressed in realdRadius
91 float borderDist = abs((uvDist-1) / uvBorder); // distance from center of line expressed in half border
93 if( borderDist > 1) // possible optim : is the early discard is more profitable than the branch ?
96 float clipA = tex2D(_GUIClipTexture, i.clipUV).a;
97 float pixelBorderSize = _Border*0.5 * pixelScale; // half border expressed on transformed pixel
98 borderDist = pixelBorderSize * (1 - borderDist) + 0.5; // signed distance from edge of line in transformed pixel
100 //float height = 0.5 + i.pos.y / i.height * 0.5; // height expressed in size.y
104 color = lerp(_ColorMiddle,_ColorEnd , (i.height - 0.5f) * 2);
106 color = lerp(_ColorStart, _ColorMiddle, i.height * 2);
108 return float4(color.rgb,color.a*saturate(borderDist)*clipA);