unity-shader-tutorial-effect

Effect.shader

Effect.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
Shader "HHF/Tutorial/Effect"
{
Properties
{
[Header(Render)]
[Enum(UnityEngine.Rendering.BlendMode)]_SrcBlend("SrcBlend", Float) = 1.0
[Enum(UnityEngine.Rendering.BlendMode)]_DstBlend("DstBlend", Float) = 0.0
[Enum(UnityEngine.Rendering.CullMode)]_Cull("Cull Mode", Float) = 2
[Enum(Off, 0, On, 1)]_ZWrite("ZWrite",int) = 0
[Enum(UnityEngine.Rendering.CompareFunction)]_ZTest("ZTest",int) = 0

[Header(Stencil)]
_StencilRef("Ref", Float) = 1
[Enum(UnityEngine.Rendering.CompareFunction)]_StencilComp("Comp", Float) = 0
[Enum(UnityEngine.Rendering.StencilOp)]_StencilPass("Pass", Float) = 0
[Enum(UnityEngine.Rendering.StencilOp)]_StencilFail("Fail", Float) = 0
[Enum(UnityEngine.Rendering.StencilOp)]_StencilZFail("ZFail", Float) = 0

[Header(Base)]
_TintColor("Tint Color", Color) = (1, 1, 1, 1)
_MainTex("MainTex", 2D) = "black" {}
_Main_Speed_Intensity_Rotate("Speed(XY) Intensity(Z) Rotate(W)",vector) = (0,0,1,0)
// [Toggle]_AlphaChannel("AlphaChannel", float) = 0
// [HideInInspector]_Alpha("Alpha", Range(0, 1)) = 1 //程序做淡入淡出功能用
[KeywordEnum(Repeat,Clamp)]_WarpMode("Warp Mode", int) = 0


[Header(Mask)]
[Toggle]_MaskToggle("Enabled", int) = 0
_MaskTex("MaskTex(R)", 2D) = "white" {}
_Mask_Speed("Speed(XY)",vector) = (0,0,0,0)

[Header(Dissolve)]
[Toggle]_DissolveToggle("Enabled", Float) = 0
_DissolveTex("Dissolve(R)",2D) = "white"{}
//[Toggle]_CutoffOverLifetimeEnabled("CutoffOverLifetimeEnabled", int) = 0
_Cutoff("Dissolve (MaskTex.R)", Range(0, 1)) = 0.5

[Header(Distortion)]
[Toggle] _DistortionToggle("Enabled", Float) = 0
_DistortionTex("DistortionTex(R)", 2D) = "white" {}
_Distort_Speed("Speed(XY)",vector) = (0,0,0,0)
_DistortionPower("Intensity",Range(0,1)) = 0.5

[Header(Rim)]
[Toggle]_RimToggle("Enabled", float) = 0
[Toggle]_Invert("Invert", float) = 0
_RimColor("Rim Color", Color) = (1, 0.65, 0, 1)
_Rim_Scale_Power("Fade(X) Power(Y)",vector) = (1,1,0,0)

// [KeywordEnum(None,Single,Mesh)]_AnimationUVType("Animation Type", int) = 0
// [IntRange]_Tiles("Tiles",range(1,20)) = 8
}

SubShader
{
Tags {"Queue" = "Transparent" "RenderType" = "Transparent"}

Blend[_SrcBlend][_DstBlend]
Cull[_Cull]
ZWrite [_ZWrite]
Ztest [_ZTest]

Stencil
{
Ref[_StencilRef]
Comp[_StencilComp]
Pass[_StencilPass]
Fail[_StencilFail]
ZFail[_StencilZFail]
}

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

#pragma target 2.0
#pragma multi_compile_fog
#pragma skip_variants FOG_EXP FOG_EXP2
#pragma shader_feature _ _MASKTOGGLE_ON
#pragma shader_feature _ _DISSOLVETOGGLE_ON
#pragma shader_feature _ _DISTORTIONTOGGLE_ON
#pragma shader_feature _ _RIMTOGGLE_ON

// #pragma shader_feature _ _WARPMODE_REPEAT _WARPMODE_CLAMP
// #pragma shader_feature _ _ANIMATIONUVTYPE_NONE _ANIMATIONUVTYPE_SINGLE _ANIMATIONUVTYPE_MESH
// #pragma multi_compile _ UNITY_UI_CLIP_RECT

fixed4 _TintColor;
fixed _Alpha;
fixed _AlphaChannel;
sampler2D _MainTex; float4 _MainTex_ST;
float4 _Main_Speed_Intensity_Rotate;
fixed _WarpMode;

#if _MASKTOGGLE_ON
sampler2D _MaskTex;
float4 _MaskTex_ST;
float4 _Mask_Speed;
#endif

#if _DISTORTIONTOGGLE_ON
sampler2D _DistortionTex;
float4 _DistortionTex_ST;
fixed _DistortionPower;
float4 _Distort_Speed;
#endif

#if _DISSOLVETOGGLE_ON
sampler2D _DissolveTex;
float4 _DissolveTex_ST;
fixed _Cutoff;
#endif

#if _RIMTOGGLE_ON
fixed _Invert;
fixed4 _RimColor;
half4 _Rim_Scale_Power;
#endif

// float4 _ClipRect;
// int _Tiles;

struct appdata
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float4 texcoord0 : TEXCOORD0;

#if _MASKTOGGLE_ON
float4 texcoord1 : TEXCOORD1;
#endif

#if _DISTORTIONTOGGLE_ON
float4 texcoord2 :TEXCOORD2;
#endif

#if _RIMTOGGLE_ON
half3 normal : NORMAL;
#endif
};

struct v2f
{
float4 positionCS : SV_POSITION;
fixed4 color : COLOR;
float4 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)

#if _MASKTOGGLE_ON
float2 uv_mask : TEXCOORD2;
#endif

#if _DISSOLVETOGGLE_ON
float2 uv_dissolve : TEXCOORD3;
#endif

#if _DISTORTIONTOGGLE_ON
float2 uv_distortion : TEXCOORD4;
#endif

#if _RIMTOGGLE_ON
half3 normalWS : TEXCOORD5;
float3 positionWS : TEXCOORD6;
#endif
};

v2f vert(appdata v)
{
v2f o = (v2f)0;

o.positionCS = UnityObjectToClipPos(v.vertex);
o.color = v.color;

// o.uv.zw = v.texcoord0.xy;//originUV;
o.uv.xy = TRANSFORM_TEX(v.texcoord0.xy, _MainTex) + _Main_Speed_Intensity_Rotate.xy * _Time.y;

#if _MASKTOGGLE_ON
o.uv_mask.xy = TRANSFORM_TEX(v.texcoord0.xy, _MaskTex) + _Mask_Speed.xy * _Time.y;
#endif

#if _DISSOLVETOGGLE_ON
o.uv_dissolve.xy = TRANSFORM_TEX(v.texcoord0.xy, _DissolveTex);
#endif

#if _DISTORTIONTOGGLE_ON
o.uv_distortion.xy += TRANSFORM_TEX(v.texcoord0.xy, _DistortionTex) + _Distort_Speed.xy * _Time.y;
#endif

#if _RIMTOGGLE_ON
o.normalWS = UnityObjectToWorldNormal(v.normal);
o.positionWS = mul(unity_ObjectToWorld,v.vertex);
#endif

// // UNITY_TRANSFER_FOG(o, o.vertex);
// UNITY_TRANSFER_FOG_COMBINED_WITH_WORLD_POS(o, o.vertex);
return o;
}

fixed4 frag(v2f i) : SV_Target
{
fixed4 c;
// UNITY_EXTRACT_FOG_FROM_WORLD_POS(i);
float2 uv = i.uv.xy;
float2 uv_clamp = clamp(uv,0,0.99);
float2 uv_repeat = frac(uv);
uv = lerp(uv_repeat,uv_clamp,_WarpMode);

//旋转UV
float cosA = cos(_Main_Speed_Intensity_Rotate.w);
float sinA = sin(_Main_Speed_Intensity_Rotate.w);
float2x2 M_rotationZ = float2x2(
cosA,sinA,
-sinA,cosA);
uv = mul(M_rotationZ,uv-0.5)+0.5;

#if _DISTORTIONTOGGLE_ON
fixed4 distortTex = tex2D(_DistortionTex, i.uv_distortion);
uv = lerp(uv, distortTex, _DistortionPower);//A + alpha*(B-A)
#endif

// #if _WARPMODE_REPEAT
// #elif _WARPMODE_CLAMP
// uv = saturate(uv);
// #endif


fixed4 mainTex = tex2D(_MainTex, uv);
c = mainTex * _TintColor * _Main_Speed_Intensity_Rotate.z * i.color;
// fixed4 col = mainTex * i.color * _TintColor;
// fixed alphaCommon = i.color.a*_TintColor.a*_Main_Speed_Intensity_Alpha.z*_Alpha;
// fixed alphaChannel = alphaCommon*mainTex.a;
// fixed notAlphaChannel = alphaCommon*mainTex.g;
// // col.a = lerp(notAlphaChannel, alphaChannel, _AlphaChannel);
// #if _MASKTOGGLE_ON
// fixed4 maskTex = tex2D(_MaskTex, i.uv_mask.xy);
// #if _DISSOLVETOGGLE_ON
// fixed4 dissolveTex = tex2D(_DissolveTex,i.uv_dissolve);
// return dissolveTex;
// // float a = 0;
// // a = 1.0 - _Cutoff;

// // //#if _CUTOFFOVERLIFETIMEENABLED_ON
// // // a = i.color.a;
// // //#endif

// // float clipCol = (a * 1.2 - 0.6) + maskTex.r;
// // clip(clipCol - 0.5);
// #else
// c *= maskTex.r;
// #endif
// #endif

#if _MASKTOGGLE_ON
fixed4 maskTex = tex2D(_MaskTex, i.uv_mask.xy);
c *= maskTex.r;
#endif

#if _DISSOLVETOGGLE_ON
fixed4 dissolveTex = tex2D(_DissolveTex,i.uv_dissolve);
// fixed4 maskTex = tex2D(_MaskTex, i.uv_mask.xy);
clip(dissolveTex.r-_Cutoff);
// float a = 0;
// a = 1.0 - _Cutoff;

// //#if _CUTOFFOVERLIFETIMEENABLED_ON
// // a = i.color.a;
// //#endif

// float clipCol = (a * 1.2 - 0.6) + maskTex.r;
// clip(clipCol - 0.5);
#endif

#if _RIMTOGGLE_ON
half3 V = normalize(_WorldSpaceCameraPos.xyz - i.positionWS.xyz);
half3 N = normalize(i.normalWS);
fixed NdotV = saturate(dot(N,V));
NdotV = lerp(1-NdotV,NdotV,_Invert);
fixed3 fresnel = _Rim_Scale_Power.x * pow(NdotV, _Rim_Scale_Power.y) * _RimColor;
c.rgb += fresnel;
#endif

// //雾效,统一改透明来实现Additive与AlphaBlend的消失
// #if FOG_LINEAR
// col.a = lerp (0, col.a, _unity_fogCoord);
// #endif

return c;
}
ENDCG
}
}

FallBack Off
}