流光效果是一个非常常见的效果,不仅仅是游戏,一些广告之类的也都会有这种效果。
实现原理:
需要一张流光图,流光图的大部分都是黑色的,然后又一条亮线。
在采样的时候,最终输出叠加上这张图的采样值,根据时间调整采样UV就可以有流光效果。
Shader "Sprite2D/Flash2D"
{
Properties
{
_MainTex("Main Texture", 2D) = "white" {}
_FlashTex("Flash Texture",2D) = "white"{}
_FlashColor("Flash Color",Color) = (1,1,1,1)
_FlashIntensity("Flash Intensity", Range(0, 1)) = 0.6
_Radian("Radian", Range(0, 3.14)) = 2.4
_FlashSpeed("Flash Speed", Range(-5, 5)) = 0
_Limit("Limit Start",float) = 0.5
_Limit2("Limit End",float) = 1.9
_RunDelay("Run Delay",float) = 5
}
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
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
sampler2D _FlashTex;
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _FlashColor;
fixed _FlashIntensity;
fixed _Radian;
fixed _FlashSpeed;
fixed _Limit;
fixed _Limit2;
fixed _RunDelay;
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
{
fixed4 col = tex2D(_MainTex, i.uv);
float2 flashUV = i.uv;
flashUV.y += _Time.y % _RunDelay *_FlashSpeed;
if (flashUV.y >= _Limit && flashUV.y <= _Limit2)
{
fixed cosV = cos(_Radian);
fixed sinV = sin(_Radian);
fixed2x2 rotMatrix = fixed2x2(cosV, -sinV, sinV, cosV);
flashUV = mul(rotMatrix, flashUV);
fixed4 flashValue = tex2D(_FlashTex, flashUV);
fixed3 flashColor = flashValue.rgb * _FlashIntensity * _FlashColor;
col.rgb = flashValue.a*flashColor + col.rgb;
}
return col;
}
ENDCG
}
}
}