Unity shader 實現(xiàn)氮氣加速特效
1:材質(zhì)和shader
Shader 是一種給GPU執(zhí)行的代碼,GPU的渲染流水線,為了方便開發(fā)人員定制效果,開放出接口給程序員編寫代碼來控制,這種程序叫作shader, shader開發(fā)語言,cocos采用的是GLSL編程語言。開發(fā)人員可以在下圖頂點Shader和著色Shader來插入代碼。

材質(zhì)是一種配置文件,選擇好一個Shader(算法), 并給這個Shader提供必要的參數(shù),當(dāng)游戲引擎繪制物體的時候,先讀取材質(zhì),根據(jù)材質(zhì), 給GPU配置shader和shader要的參數(shù), 這樣管道流水線就可以完成的繪制出來這個物體。
2: 準(zhǔn)備工作
準(zhǔn)備一個子彈頭模型(子彈列車^_^)

準(zhǔn)備一個加速特效的火焰與透明漸變的貼圖:

3: 實現(xiàn)的效果:

4: 上代碼:
Shader "Custom/additiveTex_2" {
Properties {
_TintColor ("Tint Color", Color) = (0.5, 0.5, 0.5, 0.5)
_Intensity ("Intensity", Float) = 1.0
_MainTexture ("Base (RGB) Alpha(A)", 2D) = "white" {}
_Mask ("Mask (ARGB or Grayscale)", 2D) = "white" {}
_speed("speed",Float)=5
}
Category {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
Blend SrcAlpha One
AlphaTest Greater 0.01
ColorMask RGB
Cull Off
Lighting Off
ZWrite Off
// Fog { Color (0,0,0,0) }
/*BindChannels {
Bind "Color", color
Bind "Vertex", vertex
Bind "TexCoord", texcoord
}*/
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
fixed4 _TintColor;
float _Intensity;
sampler2D _MainTexture;
sampler2D _Mask;
float _speed;
float4 _MainTexture_ST;
float4 _Mask_ST;
struct appdata_t {
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
float2 texcoord2 : TEXCOORD1;
};
struct v2f {
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
float2 texcoord2 : TEXCOORD1;
};
v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.color = v.color;
o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTexture);
o.texcoord2 = TRANSFORM_TEX(v.texcoord2,_Mask);
return o;
}
fixed4 frag (v2f i) : COLOR
{
i.texcoord.x+=_Time*_speed;
i.texcoord.y-=(_Time*_speed*16);
half4 c = i.color * _TintColor * tex2D(_MainTexture, i.texcoord);
half4 mask = tex2D(_Mask, i.texcoord2);
c *= mask.a;
return _Intensity * c;
}
ENDCG
}
}
}
FallBack "Diffuse"
}
氮氣加速特效詳細(xì)的視頻詳細(xì)講解,和項目工程,可以加學(xué)習(xí)討論群,今天的分享都到這里結(jié)束了,謝謝大家。