先上代码
Shader "UI/Loading_Tint"
{
Properties
{
[PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
_Color("Tint", Color) = (1,1,1,1)
_Progress("进度",Range(0,1)) = 0
_Lerp("渐变范围",Range(0,1)) = 0
_Strong("强度",Range(0.01,0.99)) = 0
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType" = "Plane"
"CanUseSpriteAtlas" = "True"
}
Cull Off
Lighting Off
ZWrite Off
ZTest[unity_GUIZTestMode]
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
Name "Default"
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#include "UnityCG.cginc"
#include "UnityUI.cginc"
#pragma multi_compile __ UNITY_UI_CLIP_RECT
#pragma multi_compile __ UNITY_UI_ALPHACLIP
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 uv : TEXCOORD0;
float4 worldPosition : TEXCOORD1;
UNITY_VERTEX_OUTPUT_STEREO
};
sampler2D _MainTex;
fixed4 _Color;
float4 _ClipRect;
float4 _MainTex_ST;
float _Progress;
float _Lerp;
float _Strong;
// 1/(2*PI)
#define halfPI1 0.15915
v2f vert(appdata_t v)
{
v2f o;
o.worldPosition = v.vertex;
o.vertex = UnityObjectToClipPos(o.worldPosition);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.color = v.color * _Color;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
half4 color = (tex2D(_MainTex, i.uv)) * i.color;
//color.a *= step(i.uv.x, _Progress);
//计算 进度位子
float2 uv = i.uv - float2(0.5, 0.5);
//讲旋转值 归到 0-1之间,此处 以 下为起点。 如果 以左为起点, 调换x ,y位子。 0.5为 半圈
float radian = (atan2(uv.x, uv.y) *halfPI1 + 0.5);
//渐变范围处理
float s1 = step(_Progress - _Lerp, radian);
//处理渐变值
float s2 = lerp(0 , 1, (1/ _Lerp)*(radian- _Progress+ _Lerp))*s1;
//判定是否需要显示颜色
color.a *= step(radian, _Progress);
//叠加渐变
color.a *= (_Strong +(1- _Strong)* s2);
return color;
}
ENDCG
}
}
}