Shader流光+渐变灰色
Properties面板
Properties
{
_MainTex ("Base (RGB)", 2D) = "black" {}
_FlashColor ("Flash Color", Color) = (1,1,1,0)
_Alpha ("Flash Alpha", Range(0, 1)) = 0.7
_Angle ("Flash Angle", Range(0, 360)) = 45
_Width ("Flash Width", Range(0, 1)) = 0.2
_Speed ("Speed", Float) = 1
_Interval ("Time Interval", Float) = 2
_Gary("Gary",Range(-3,3)) = 2
}
Tags标签
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"DisableBatching" = "True"
}
Pass
Pass
{
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Offset -1, -1
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _FlashColor;
float _Alpha;
float _Angle;
float _Width;
float _Speed;
float _Interval;
float _Gary;
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
fixed4 color : COLOR;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
fixed4 color : COLOR;
UNITY_VERTEX_OUTPUT_STEREO
};
v2f o;
float inFlash(half2 uv)
{
float brightness = 0;
float angleInRad = _Angle;
float tanInverseInRad = 1.0 / acos(angleInRad);
float currentTime = _Time.y;
bool onLeft = (tanInverseInRad > 0);
float xBottomFarLeft = onLeft? 0.0 : tanInverseInRad;
float xBottomFarRight = onLeft? (1.0 + tanInverseInRad) : 1.0;
float xBottomRightBound = xBottomFarLeft + _Speed * (xBottomFarRight - xBottomFarLeft);
float xBottomLeftBound = xBottomRightBound - _Width;
float xProj = uv.x + uv.y * -tanInverseInRad;
if(xProj > xBottomLeftBound && xProj < xBottomRightBound)
{
brightness = 1.0 - abs(2.0 * xProj - (xBottomLeftBound + xBottomRightBound)) / _Width;
}
return brightness;
}
v2f vert (appdata_t v)
{
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord = v.texcoord;
o.color = v.color;
return o;
}
fixed4 frag (v2f IN) : SV_Target
{
fixed4 col = tex2D(_MainTex, IN.texcoord) * IN.color;
if(col.a == 1)
{
col += inFlash(IN.texcoord) * _Alpha;
}
float gray = 0.2125 * col.r + 0.7154 * col.g + 0.0721 * col.b;
float3 grayColor = fixed3(gray, gray, gray);
float3 avgcolor = float3(0.5, 0.5, 0.5);
col.rgb = lerp(grayColor, col.rgb, saturate(1 - IN.texcoord.y - _Gary));
col.rgb = lerp(grayColor, col.rgb, saturate(IN.texcoord.x - _Gary));
return col;
}
ENDCG
}
解释inFlash函数<用于计算流光>:
float inFlash(half2 uv)
{
float brightness = 0;
float angleInRad = _Angle;
float tanInverseInRad = 1.0 / acos(angleInRad);
float currentTime = _Time.y;
bool onLeft = (tanInverseInRad > 0);
float xBottomFarLeft = onLeft? 0.0 : tanInverseInRad;
float xBottomFarRight = onLeft? (1.0 + tanInverseInRad) : 1.0;
float xBottomRightBound = xBottomFarLeft + _Speed * (xBottomFarRight - xBottomFarLeft);
float xBottomLeftBound = xBottomRightBound - _Width;
float xProj = uv.x + uv.y * -tanInverseInRad;
if(xProj > xBottomLeftBound && xProj < xBottomRightBound)
{
brightness = 1.0 - abs(2.0 * xProj - (xBottomLeftBound + xBottomRightBound)) / _Width;
}
return brightness;
}
解释灰色渐变:
float gray = 0.2125 * col.r + 0.7154 * col.g + 0.0721 * col.b;
float3 grayColor = fixed3(gray, gray, gray);
float3 avgcolor = float3(0.5, 0.5, 0.5);
col.rgb = lerp(grayColor, col.rgb, saturate(1 - IN.texcoord.y - _Gary));
col.rgb = lerp(grayColor, col.rgb, saturate(IN.texcoord.x - _Gary));
shader重要的不是实现效果,而是在于shader的性能!