unity-shader-tutorial-translucent

buildin效果图

urp效果图

Translucent.shader

Translucent.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
Shader "HHF/Tutorial/Translucent"
{
Properties
{
_BaseColor("Base Color",color) = (1,1,1,1)
_BaseMap("BaseMap", 2D) = "white" {}
_Specular("Specular",float) = 1
_Shininess("Shininess",float) = 1
[Header(Translucent)]
_ThicknessMap("ThicknessMap",2D) = "white"{}
_Thickness("Thickness",range(0,1)) = 0.5
_NormalDistortion("Normal Distortion",range(0,1)) = 0.5
_Attenuation("Attenuation",float) = 0
_Strength("Strength",float) = 1
}

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

Pass
{
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
#pragma multi_compile _ _ADDITIONAL_LIGHTS
#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 positionOS : POSITION;
float2 uv : TEXCOORD0;
float3 normalOS : NORMAL;
};

struct v2f
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
float3 normalWS : TEXCOORD1;
float3 viewWS : TEXCOORD2;
float3 positionWS : TEXCOORD3;
};

CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
float4 _BaseMap_ST;
half _Specular,_Shininess;
half _NormalDistortion,_Attenuation,_Strength,_Thickness;
CBUFFER_END
TEXTURE2D (_BaseMap);SAMPLER(sampler_BaseMap);
TEXTURE2D (_ThicknessMap);SAMPLER(sampler_ThicknessMap);

// 透射计算
// @lightDir:光照方向
// @viewDir:视线方向
// @normalWS:世界空间的法线
// @color:光照颜色
// @thickness:厚度
half3 LightingTranslucent(float3 lightDir, float3 viewDir, half3 normalWS, half3 color, half thickness)
{
half3 L = lightDir;
half3 V = viewDir;
half3 N = normalWS;
half3 H = L + N * _NormalDistortion;
half _LdotV = dot(-H,V);
half3 I = pow(saturate(_LdotV), _Attenuation) * _Strength;
I *= thickness;
I *= color;
return I;
}

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

o.positionCS = TransformObjectToHClip(v.positionOS.xyz);
o.uv = TRANSFORM_TEX(v.uv, _BaseMap);
o.normalWS = TransformObjectToWorldNormal(v.normalOS);
o.positionWS = TransformObjectToWorld(v.positionOS.xyz);
o.viewWS = normalize(_WorldSpaceCameraPos - o.positionWS);

return o;
}

half4 frag(v2f i) : SV_Target
{
half4 c = 0;
half4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, i.uv);
c = baseMap * _BaseColor;

// 漫反射
// Specular = Ks * pow(max(0,dot(N,H)), Shininess)
float3 N = normalize(i.normalWS);
Light mainLight = GetMainLight();
float3 L = mainLight.direction;
float3 V = i.viewWS;
float3 H = normalize(L + V);
half diffuse = max(0.3,dot(N,L));
c *= diffuse;

// 高光
half NdotH = saturate(dot(N,H));
half specular = _Specular * pow(NdotH, _Shininess);
c += specular;

// 厚度 = 1 - ao图
half thicknessMap = 1 - SAMPLE_TEXTURE2D(_ThicknessMap, sampler_ThicknessMap, i.uv).r;
half thickness = lerp(1,thicknessMap,_Thickness);

// 主光透射
c.rgb += LightingTranslucent(L, i.viewWS,N, mainLight.color, thickness);

// 额外光的透射
#ifdef _ADDITIONAL_LIGHTS
uint pixelLightCount = GetAdditionalLightsCount();
for (uint lightIndex = 0u; lightIndex < pixelLightCount; ++lightIndex)
{
Light light = GetAdditionalLight(lightIndex, i.positionWS);
half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
c.rgb += LightingTranslucent(light.direction,i.viewWS,N,light.color,thickness) * attenuatedLightColor;
}
#endif

return c;
}
ENDHLSL
}
}

SubShader
{
Pass
{
Tags { "Queue"="Geometry" "RenderType" = "Opaque" "IgnoreProjector" = "True" "LightMode" = "ForwardBase"}

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase

#include "UnityCG.cginc"
#include "AutoLight.cginc"
#include "Lighting.cginc"

struct appdata
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
float3 normalOS : NORMAL;
};

struct v2f
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
float3 normalWS : TEXCOORD1;
float3 viewWS : TEXCOORD2;
float3 positionWS : TEXCOORD3;
};

half4 _BaseColor;
sampler2D _BaseMap;float4 _BaseMap_ST;
half _Specular,_Shininess;
half _NormalDistortion,_Attenuation,_Strength,_Thickness;
sampler2D _ThicknessMap;float4 _ThicknessMap_ST;

// 透射计算
// @lightDir:光照方向
// @viewDir:视线方向
// @normalWS:世界空间的法线
// @color:光照颜色
// @thickness:厚度
half3 LightingTranslucent(float3 lightDir, float3 viewDir, half3 normalWS, half3 color, half thickness)
{
half3 L = lightDir;
half3 V = viewDir;
half3 N = normalWS;
half3 H = L + N * _NormalDistortion;
half _LdotV = dot(-H,V);
half3 I = pow(saturate(_LdotV),_Attenuation) * _Strength;
I *= thickness;
I *= color;
return I;
}

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

o.positionCS = UnityObjectToClipPos(v.positionOS);
o.uv = TRANSFORM_TEX(v.uv, _BaseMap);
o.normalWS = UnityObjectToWorldNormal(v.normalOS);
o.positionWS = mul(unity_ObjectToWorld, v.positionOS);
o.viewWS = normalize(_WorldSpaceCameraPos - o.positionWS);

return o;
}

half4 frag(v2f i) : SV_Target
{
half4 c = 0;
half4 baseMap = tex2D(_BaseMap, i.uv);
c = baseMap * _BaseColor;

// 漫反射
// Specular = Ks * pow(max(0,dot(N,H)), Shininess)
float3 N = normalize(i.normalWS);
float3 L = _WorldSpaceLightPos0;
float3 V = i.viewWS;
float3 H = normalize(L + V);
half diffuse = max(0.3,dot(N,L));
c *= diffuse;

// 高光
half NdotH = saturate(dot(N,H));
half specular = _Specular * pow(NdotH, _Shininess);
c += specular;

// 厚度 = 1 - ao图
half thicknessMap = 1 - tex2D(_ThicknessMap, i.uv).r;
half thickness = lerp(1,thicknessMap,_Thickness);

// 主光透射
c.rgb += LightingTranslucent(L, i.viewWS,N, _LightColor0, thickness);
return c;
}
ENDCG
}

Pass
{
Tags { "Queue"="Geometry" "RenderType" = "Opaque" "IgnoreProjector" = "True" "LightMode" = "ForwardAdd"}
Blend One One

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdadd
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"

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

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

half _NormalDistortion,_Attenuation,_Strength,_Thickness;
sampler2D _ThicknessMap;float4 _ThicknessMap_ST;

// 透射计算
// @lightDir:光照方向
// @viewDir:视线方向
// @normalWS:世界空间的法线
// @color:光照颜色
// @thickness:厚度
half3 LightingTranslucent(float3 lightDir, float3 viewDir, half3 normalWS, half3 color, half thickness)
{
half3 L = lightDir;
half3 V = viewDir;
half3 N = normalWS;
half3 H = L + N * _NormalDistortion;
half _LdotV = dot(-H,V);
half3 I = pow(saturate(_LdotV),_Attenuation) * _Strength;
I *= thickness;
I *= color;
return I;
}

v2f vert (appdata v)
{
v2f o;
o.positionCS = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.normalWS = UnityObjectToWorldNormal(v.normal);
o.positionWS = mul(unity_ObjectToWorld,v.vertex);
o.viewWS = normalize(_WorldSpaceCameraPos - o.positionWS);
return o;
}

fixed4 frag (v2f i) : SV_Target
{
half4 c = 0;
UNITY_LIGHT_ATTENUATION(atten, 0, i.positionWS)

// 厚度 = 1 - ao图
half thicknessMap = 1 - tex2D(_ThicknessMap, i.uv).r;
half thickness = lerp(1, thicknessMap, _Thickness);

// 额外光投射
fixed3 N = normalize(i.normalWS);
c.rgb += LightingTranslucent(_WorldSpaceLightPos0, i.viewWS,N,_LightColor0 * atten, thickness) ;

return c;
}

ENDCG
}

}

}