Blood 血液闪烁特效
1、红色光线从两边闪出
2、红色从两边倒中间渐变淡(通过圆心的距离判别渐变的距离)
Shader 代码:
Shader "Hidden/Blood"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Blood("Length",float)=1
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
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;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
float _Blood;
fixed4 frag (v2f i) : SV_Target
{
float2 tmpUV=i.uv;
float tempLength=0;
if(tmpUV.x<0.5)
{
tempLength=length(tmpUV-float2(-0.5,0.5));//求向量的模长
}
else
{
tempLength=length(float2(1.5,0.5)-tmpUV);
}
tempLength *=_Blood;
fixed4 col = tex2D(_MainTex, i.uv);
col=lerp(fixed4(1,0,0,1),col,clamp(tempLength,0,1));//讲tempLength限制在0——1之间
// just invert the colors
//col.rgb = 1 - col.rgb;
return col;
}
ENDCG
}
}
}
C#代码:
public class Blood : MonoBehaviour
{
public Material graphicMat;
public float rangValue=0.5f;
public float tempCount=0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
tempCount+=Time.deltaTime*10;
rangValue=Mathf.Cos(tempCount);
rangValue=1+(rangValue+1)*0.25f-0.25f; //范围0.75~1.25
}
private void OnRenderImage(RenderTexture src, RenderTexture dest) {
graphicMat.SetFloat("_Blood",rangValue); //设置Shader参数
Graphics.Blit(src,dest,graphicMat);
}
}