先上效果图:
GIF是视频转的,帧率感人;
原理介绍:
float curLightPos=_Time.y*_Speed;
//-----先定义一个变量来计算移动值
if(curLightPos<i.uv.x)
{
col = tex2D(_LogoTex, i.uv);
}
//----然后将移动的值和图片UV的水平方向的值比较,属于另一张图片;
完整shader代码如下
Shader "Unlit/LogoShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_LogoTex("闪光图",2D)= "white" {}
_Width("闪光宽度",Range(0,0.5))=0.2
_Speed("光照速度",Range(-1,1))=0.2
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
//blend SrcColor OneMinusSrcColor
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;
sampler2D _LogoTex;
float _Speed;
float _Width;
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
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
float curLightPos=_Time.y*_Speed;
if(curLightPos<i.uv.x)
{
col = tex2D(_LogoTex, i.uv);
}
return col;
}
ENDCG
}
}
}