参考链接:http://blog.csdn.net/stalendp/article/details/30989295
效果图:
Shader "Custom/Lightning"
{
Properties
{
_MainTex ("Main Texture", 2D) = "white" {}
_MaskTex ("Mask Texture", 2D) = "white" {}//闪电遮罩
_MaskTex2 ("Mask2 Texture", 2D) = "white" {}//领巾遮罩
_NoiseTex ("Noise Texture", 2D) = "white" {}//用于领巾的飘动
_Color ("Color", Color) = (1, 1, 1, 1)//闪电颜色
_Speed ("Speed", Range(1, 10)) = 5//闪电速度
_Radian ("Radian", Range(0, 3.14)) = 2.3//闪电弧度
}
SubShader
{
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;
float2 uvMask : TEXCOORD1;
};
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _MaskTex;
float4 _MaskTex_ST;
sampler2D _MaskTex2;
sampler2D _NoiseTex;
fixed4 _Color;
half _Speed;
half _Radian;
v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.uvMask = TRANSFORM_TEX(v.uv, _MaskTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 mainCol = tex2D(_MainTex, i.uv);
fixed4 maskCol = tex2D(_MaskTex, i.uvMask);
fixed4 mask2Col = tex2D(_MaskTex2, i.uv);
fixed4 noiseCol = tex2D(_NoiseTex, i.uv + _Time.xx);
fixed cosV = cos(_Radian);
fixed sinV = sin(_Radian);
//绕z轴旋转的矩阵
fixed4x4 rotMatrix = fixed4x4(cosV, -sinV, 0, 0,
sinV, cosV, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
//旋转是因为闪电是斜着下来的
float2 uv = mul(rotMatrix, float4(i.uvMask, 0, 0)).xy;
fixed alpha = step(uv.y, frac(_Time.y * _Speed));
//领巾动态效果
if(mask2Col.a > 0)
{
//通过噪声图达到领巾随机飘动的效果
mainCol = tex2D(_MainTex, i.uv + noiseCol.xy * 0.05 - 0.025);
return mainCol;
}
//闪电动态效果
return mainCol + maskCol * _Color * alpha;
}
ENDCG
}
}
}
这是unitypackage:
http://pan.baidu.com/s/1c2FQfsc