Shader "Bamboo/CircularProgress" { Properties { _MainTex ("Texture", 2D) = "white" {} _Progress ("Progress", Range(0, 1)) = 0 _Color ("Color", Color) = (1,1,1,1) _BackgroundColor ("Background Color", Color) = (0,0,0,0) } SubShader { Tags { "Queue"="Transparent" "RenderType"="Transparent" } Blend SrcAlpha OneMinusSrcAlpha Cull Off ZWrite Off Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; }; sampler2D _MainTex; float4 _MainTex_ST; float _Progress; fixed4 _Color; fixed4 _BackgroundColor; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); return o; } fixed4 frag (v2f i) : SV_Target { // 获取UV坐标相对于中心的位置 float2 center = float2(0.5, 0.5); float2 dir = i.uv - center; // 计算当前点到圆心的距离 float dist = length(dir); // 计算当前点的角度(从上方开始顺时针方向) // atan2返回[-π, π],我们转换为[0, 1]范围 float angle = atan2(dir.x, dir.y) / (2.0 * 3.14159265359); angle = angle < 0 ? angle + 1.0 : angle; // 采样纹理 fixed4 col = tex2D(_MainTex, i.uv); // 根据进度决定是否显示 float visible = (dist <= 0.5) && (angle <= _Progress); // 混合颜色 col.rgb = col.rgb * _Color.rgb; col.a = col.a * _Color.a * visible; // 如果不可见,使用背景色 return lerp(_BackgroundColor, col, visible); } ENDCG } } }