![](https://images2015.cnblogs.com/blog/272643/201509/272643-20150915164532054-506042446.png)
//Character Shader With Hurt Flare and FlareColor
//Author yellowcrayon@163.com
Shader "DreamFaction/Characters/CharacterV3" {
Properties {
[MaterialToggle] _Flare ("Flare", Float ) = 0
_FlareColor ("FlareColor", Color) = (0.5,0.5,0.5,1)
_Cutoff ("Alpha Cutoff", Range (0,.9)) = .5
_MainTex ("MainTex", 2D) = "white" {}
}
SubShader {
Tags {
"IgnoreProjector"="True"
"Queue"="AlphaTest"
"RenderType"="TransparentCutout"
}
Pass {
Name "ForwardBase"
Tags{
"LightMode" = "ForwardBase"
}
Lighting off
Cull off
LOD 200
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform fixed4 _MainTex_ST;
uniform fixed _Flare;
uniform fixed4 _FlareColor;
uniform fixed _Cutoff;
struct VertexInput
{
fixed4 vertex : POSITION;
fixed3 normal : NORMAL;
fixed2 texcoord0 : TEXCOORD0;
};
struct VertexOutput
{
fixed4 pos : SV_POSITION;
fixed2 uv0 : TEXCOORD0;
fixed4 posWorld : TEXCOORD1;
fixed3 normalDir : TEXCOORD2;
};
VertexOutput vert (VertexInput v)
{
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.normalDir = mul(_Object2World, float4(v.normal,0)).xyz;
o.posWorld = mul(_Object2World, v.vertex);
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
}
fixed4 frag(VertexOutput i) : Color
{
i.normalDir = normalize(i.normalDir);
/////// Vectors:
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
float3 normalDirection = i.normalDir;
// Reverse normal if this is a backface
float nSign = sign( dot( viewDirection, i.normalDir ) );
i.normalDir *= nSign;
normalDirection *= nSign;
////// Lighting:
fixed4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
fixed if_leA = step(_Flare, _MainTex_var.rgb); //_Flare > _MainTex_var.rgb return 0 else 1
fixed if_leB = step(_MainTex_var.rgb, _Flare);
fixed3 if_blend = (_MainTex_var.rgb + ( pow(1.0 - max(0,dot(i.normalDir, viewDirection)),2.0) * _FlareColor.rgb*3.0) );
fixed3 finalColor = (if_leA * _MainTex_var.rgb ) + (if_leB * if_blend);
clip(_MainTex_var.a - _Cutoff);
return fixed4(finalColor,_MainTex_var.a);
}
ENDCG
}
}
FallBack "Diffuse"
}
//Author yellowcrayon@163.com
Shader "DreamFaction/Characters/CharacterV3" {
Properties {
[MaterialToggle] _Flare ("Flare", Float ) = 0
_FlareColor ("FlareColor", Color) = (0.5,0.5,0.5,1)
_Cutoff ("Alpha Cutoff", Range (0,.9)) = .5
_MainTex ("MainTex", 2D) = "white" {}
}
SubShader {
Tags {
"IgnoreProjector"="True"
"Queue"="AlphaTest"
"RenderType"="TransparentCutout"
}
Pass {
Name "ForwardBase"
Tags{
"LightMode" = "ForwardBase"
}
Lighting off
Cull off
LOD 200
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform fixed4 _MainTex_ST;
uniform fixed _Flare;
uniform fixed4 _FlareColor;
uniform fixed _Cutoff;
struct VertexInput
{
fixed4 vertex : POSITION;
fixed3 normal : NORMAL;
fixed2 texcoord0 : TEXCOORD0;
};
struct VertexOutput
{
fixed4 pos : SV_POSITION;
fixed2 uv0 : TEXCOORD0;
fixed4 posWorld : TEXCOORD1;
fixed3 normalDir : TEXCOORD2;
};
VertexOutput vert (VertexInput v)
{
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.normalDir = mul(_Object2World, float4(v.normal,0)).xyz;
o.posWorld = mul(_Object2World, v.vertex);
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
}
fixed4 frag(VertexOutput i) : Color
{
i.normalDir = normalize(i.normalDir);
/////// Vectors:
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
float3 normalDirection = i.normalDir;
// Reverse normal if this is a backface
float nSign = sign( dot( viewDirection, i.normalDir ) );
i.normalDir *= nSign;
normalDirection *= nSign;
////// Lighting:
fixed4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
fixed if_leA = step(_Flare, _MainTex_var.rgb); //_Flare > _MainTex_var.rgb return 0 else 1
fixed if_leB = step(_MainTex_var.rgb, _Flare);
fixed3 if_blend = (_MainTex_var.rgb + ( pow(1.0 - max(0,dot(i.normalDir, viewDirection)),2.0) * _FlareColor.rgb*3.0) );
fixed3 finalColor = (if_leA * _MainTex_var.rgb ) + (if_leB * if_blend);
clip(_MainTex_var.a - _Cutoff);
return fixed4(finalColor,_MainTex_var.a);
}
ENDCG
}
}
FallBack "Diffuse"
}