参考连接 https://blog.csdn.net/stalendp/article/details/21993227
Shader "Custom/ColorRingShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
float2 p =(2.0 * i.uv.xy - 1);//修改uv区间在(-1,1)
//p = float2(p.x *_ScreenParams.x , p.y* _ScreenParams.y);
float tau = 3.1415926535 * 2;//2Π
float a = atan2(p.x,p.y);//a表示p点绕原点的角度,范围为[-π,π];所以uv.x = a/tau的范围为[-1/2, 1/2];
float r = length(p) * 0.75;//半径
float2 uv = float2(a / tau,r);
float iTime = _Time.z;
//get the color
float xCol =(uv.x - (iTime / 3.0)) * 3.0;
xCol = sign(xCol) * fmod(xCol,3.0);//设置xCol范围[0,3]
float3 horColour = float3(0.25,0.25,0.25);//基础色
if (xCol < 1.0) //[0,3]分三份,分别做颜色的混合
{
horColour.r += 1.0 - xCol;
horColour.g += xCol;
}
else if (xCol < 2.0) {
xCol -= 1.0;
horColour.g += 1.0 - xCol;
horColour.b += xCol;
}
else {
xCol -= 2.0;
horColour.b += 1.0 - xCol;
horColour.r += xCol;
}
// draw color beam
uv = (2.0 * uv) - 1.0;
//光圈动画
float tt = 5.0 + 10 * cos(iTime);
float param = clamp(floor(tt),0,10);//设置在[0,10]之间的整数
float rimLight = 3;//0.7;//轮廓光晕
float beamWidth = (rimLight +0.5*cos(uv.x * 10.0 * tau * 0.15 * param));
beamWidth= beamWidth*abs(1.0 / (30.0 * uv.y));
float3 horBeam = float3(beamWidth, beamWidth, beamWidth);
return float4(((horBeam)*horColour), 1.0);
}
ENDCG
}
}
}