Legacy Particles Alpha Blended Shader Add Fog Of War

原因

游戏内有战争迷雾(Fog of War), 原本直接使用的是自带的 Mobile/Particles/Alpha Blended. 后来突然发现 粒子在迷雾里面无法隐藏. 就有了想单独写一个Shader的想法

问题

但是看了下原始Shader的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)

// Simplified Alpha Blended Particle shader. Differences from regular Alpha Blended Particle one:
// - no Tint color
// - no Smooth particle support
// - no AlphaTest
// - no ColorMask

Shader "Mobile/Particles/Alpha Blended" {
Properties {
_MainTex ("Particle Texture", 2D) = "white" {}
}

Category {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" }
Blend SrcAlpha OneMinusSrcAlpha
Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) }

BindChannels {
Bind "Color", color
Bind "Vertex", vertex
Bind "TexCoord", texcoord
}

SubShader {
Pass {
SetTexture [_MainTex] {
combine texture * primary
}
}
}
}
}

这个Shader用的是Legacy的方法. 里面的primary不知道从哪里设置的.

后来查了一下文档,应该是用的Texture Combiners 然后读取的是顶点Color

剩下的就简单了

Tag设置为Transparent,然后Blend选择 One OneMinusSrcAlpha. (同里Additive Particle 选择Blend SrcAlpha One)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
Shader "Eran/FOW/Alpha Blended"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags
{
"Queue"="Transparent" <==这里
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
}

Cull Off
Lighting Off
ZWrite Off
Blend One OneMinusSrcAlpha <==这里


Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float4 color : COLOR; <==顶点Color
};

struct v2f
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
float2 wpos : TEXCOORD1;
fixed4 color : COLOR; <==顶点Color
};

sampler2D _MainTex;
float4 _MainTex_ST;

sampler2D _FowTex;
half4 _FowColor;
float _FowInvisibleAreaAlpha;

float _FowWorldWidth;
float _FowWorldHeight;


v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.color = v.color; //传入顶点Color

//计算Fow的坐标
o.wpos = mul (unity_ObjectToWorld, v.vertex).xz;
o.wpos.x = o.wpos.x / _FowWorldWidth;
o.wpos.y = o.wpos.y / _FowWorldHeight;

return o;
}

fixed4 frag (v2f i) : SV_Target
{
//计算Fow
fixed4 fow = tex2D(_FowTex,i.wpos);
fixed4 a = lerp(_FowColor,fixed4(1,1,1,1) * _FowInvisibleAreaAlpha,fow.r);
fixed4 b = lerp(_FowColor,fixed4(1,1,1,1),fow.g);

//原始颜色
fixed4 col = tex2D(_MainTex, i.uv) * i.color; //这里乘一下顶点颜色
col.rgb *= col.a;


col.rgb *= min((a+b),fixed4(1,1,1,1));
return col;

}
ENDCG
}
}
}
坚持原创技术分享,您的支持将鼓励我继续创作!