unity-shader-example-energyShield

buildin效果图

urp效果图

EnergyShield.shader

EnergyShield.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
Shader "HHF/Example/EnergyShield"
{
Properties
{
[Header(Base)]
_MainTex("MainTex", 2D) = "white" {}
_MainTexIntensity("MainTexIntensity", float) = 0.05

[Header(Junction)]
_JunctionColor("JunctionColor", Color) = (1,1,1,1)
_JunctionFade("JunctinoFade", float) = 3

[Header(Fresnel)]
_FresnelColor("FresnelColor",Color) = (1,1,1,1)
_FresnelPower("FresnelPower", Range(0, 15)) = 5

[Header(Distort)]
_DistortBrightness("DistortBrightness",float) = 1
_DistortIntensity("DistortIntensity",Range(0,1)) = 0.4
_DistortTiling("DistortTiling", float) = 1
}

SubShader
{
Tags { "Queue"="Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True" "RenderPipeline" = "UniversalPipeline" }

Pass
{
// Blend SrcAlpha OneMinusSrcAlpha
// Blend One One
HLSLPROGRAM
// Required to compile gles 2.0 with standard srp library
#pragma prefer_hlslcc gles
#pragma exclude_renderers d3d11_9x
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
half3 normal : NORMAL;
};

struct v2f
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
float3 positionVS : TEXCOORD2;
half3 normalWS : TEXCOORD3;
half3 viewWS : TEXCOORD4;
};

CBUFFER_START(UnityPerMaterial)
half _MainTexIntensity;
half4 _JunctionColor;
half _JunctionFade;
half4 _FresnelColor;
half _FresnelPower;
half _DistortBrightness;
half _DistortIntensity;
half _DistortTiling;
float4 _MainTex_ST;
CBUFFER_END
TEXTURE2D (_MainTex);SAMPLER(sampler_MainTex);
TEXTURE2D (_CameraDepthTexture);SAMPLER(sampler_CameraDepthTexture);
TEXTURE2D (_CameraOpaqueTexture);SAMPLER(sampler_CameraOpaqueTexture);

v2f vert(appdata v)
{
v2f o = (v2f)0;
float3 worldPos = TransformObjectToWorld(v.vertex.xyz);

o.positionVS = TransformWorldToView(worldPos);
o.normalWS = TransformObjectToWorldNormal(v.normal);
o.viewWS = normalize(_WorldSpaceCameraPos - worldPos);
o.positionCS = TransformWViewToHClip(o.positionVS);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}

half4 frag(v2f i) : SV_Target
{
half4 c = 0;

// 主纹理
half4 mainTex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv + float2(0, _Time.y));
c = mainTex * _MainTexIntensity;

// 交界处处理
float2 screenUV = i.positionCS.xy / _ScreenParams.xy;
half4 depthMap = SAMPLE_TEXTURE2D(_CameraDepthTexture, sampler_CameraDepthTexture, screenUV);
half depthZ = LinearEyeDepth(depthMap.r, _ZBufferParams);
// 片段的z和深度的z坐标系方向不一样,片段的z需要取反才是正确的深度z
half4 junction = depthZ - (-i.positionVS.z);
junction *= _JunctionFade;
junction = 1 - junction;
junction *= _JunctionColor;
junction = saturate(junction);
c += junction;
// return c;

// fresnel外发光
// pow(max(0,dot(N,V)), Intensity)
half3 N = i.normalWS; // normal(i.normalWS);
half3 V = i.viewWS; // normal(i.viewWS);
half NdotV = 1 - saturate(dot(N,V));
half4 fresnel = pow(abs(NdotV), _FresnelPower);
fresnel *= _FresnelColor;
c += fresnel;

// 扭曲
float2 distortUV = lerp(screenUV, mainTex.rr, _DistortIntensity);
half4 opaqueTex = SAMPLE_TEXTURE2D(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, distortUV);
half4 distort = half4(opaqueTex.rgb,1);
half flowMask = frac(i.uv.y * _DistortTiling + _Time.y) * 0.5 + 0.5;
distort *= flowMask * _DistortBrightness;
c += distort;
// return distort;

return c;
}
ENDHLSL
}
}

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

GrabPass{"_GrabTex"}

Pass
{
// Blend SrcAlpha OneMinusSrcAlpha
// Blend One One
CGPROGRAM

#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
half3 normal : NORMAL;
};

struct v2f
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
float3 positionVS : TEXCOORD2;
half3 normalWS : TEXCOORD3;
half3 viewWS : TEXCOORD4;
};

half _MainTexIntensity;
half4 _JunctionColor;
half _JunctionFade;
half4 _FresnelColor;
half _FresnelPower;
half _DistortBrightness;
half _DistortIntensity;
half _DistortTiling;

sampler2D _MainTex;float4 _MainTex_ST;
sampler2D _CameraDepthTexture;
sampler2D _GrabTex;

v2f vert(appdata v)
{
v2f o = (v2f)0;
float3 positionWS = mul(unity_ObjectToWorld, v.vertex);

o.positionVS = UnityObjectToViewPos(v.vertex);
o.normalWS = UnityObjectToWorldNormal(v.normal);
o.viewWS = normalize(_WorldSpaceCameraPos - positionWS);
o.positionCS = mul(UNITY_MATRIX_P, float4(o.positionVS, 1));
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}

half4 frag(v2f i) : SV_Target
{
half4 c = 0;

// 主纹理
half4 baseMap = tex2D(_MainTex, i.uv + float2(0,_Time.y));
c = baseMap * _MainTexIntensity;

// 交界处处理
float2 screenUV = i.positionCS.xy / _ScreenParams.xy;
half4 depthMap = tex2D(_CameraDepthTexture, screenUV);
half depthZ = LinearEyeDepth(depthMap.r);
// 片段的z和深度的z坐标系方向不一样,片段的z需要取反才是正确的深度z
half4 junction = depthZ - (-i.positionVS.z);
junction *= _JunctionFade;
junction = 1 - junction;
junction *= _JunctionColor;
junction = saturate(junction);
c += junction;

// fresnel外发光
// pow(max(0,dot(N,V)),Intensity)
half3 N = i.normalWS;
half3 V = i.viewWS;
half NdotV = 1 - saturate(dot(N,V));
half4 fresnel = pow(abs(NdotV),_FresnelPower);
fresnel *= _FresnelColor;
c += fresnel;

// 扭曲
float2 distortUV = lerp(screenUV, baseMap.rr, _DistortIntensity);
half4 opaqueTex = tex2D(_GrabTex, distortUV);
half4 distort = half4(opaqueTex.rgb, 1);
half flowMask = frac(i.uv.y * _DistortTiling + _Time.y) * 0.5 + 0.5;
distort *= flowMask * _DistortBrightness;
c += distort;

return c;
}
ENDCG
}
}

}