unity-shader-tutorial-kajiyaKayHair

buildin效果图

urp效果图

KajiyaKayHair.shader

KajiyaKayHair.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
Shader "HHF/Tutorial/KajiyaKayHair"
{
Properties
{
[Header(Base)]
_BaseColor("Base Color",color) = (1,1,1,1)
_ShadowColor("Shadow Color",color) = (0,0,0,0)
_BaseMap("BaseMap(RGB) Cutoff(A)", 2D) = "white" {}
_Cutoff("Cutoff",range(0,1)) = 0.3
_AOIntensity("AO Intensity",range(0,1)) = 0.35

[Header(Anisotropic)]
_NoiseMap("Noise", 2D) = "white" {}
_PrimaryColor("PrimaryColor",color) = (1,1,1,1)
_PrimaryStrength("PrimaryStrength",float) = 1.5
_PrimaryFade("PrimaryFade",float) = 80
_PrimaryShift("PrimaryShift",range(-1,1)) = 0
_PrimarySharp("PrimarySharp",range(-2,2)) = 1
_SecondaryColor("SecondaryColor",color) = (1,1,1,1)
_SecondaryStrength("SecondaryStrength",float) = 1.5
_SecondaryFade("SecondaryFade",float) = 80
_SecondaryShift("SecondaryShift",range(-1,1)) = 0.65
_SecondarySharp("SecondarySharp",range(-2,2)) = 1
}

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

Pass
{
Tags{ "LightMode" = "SRPDefaultUnlit"}
ZWrite On
ZTest LEqual
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"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
half4 color : COLOR;
};

struct v2f
{
float4 positionCS : SV_POSITION;
float4 uv : TEXCOORD0;
float3 viewWS : TEXCOORD1;
half4 normalWS : TEXCOORD3;
half4 tangentWS : TEXCOORD4;
half4 bitangentWS : TEXCOORD5;
half4 color : TEXCOORD6;
};

CBUFFER_START(UnityPerMaterial)
half4 _BaseColor,_ShadowColor;
float4 _BaseMap_ST,_NoiseMap_ST;
half _Cutoff;
half _AOIntensity;
half4 _PrimaryColor;
half _PrimaryStrength,_PrimaryFade,_PrimaryShift,_PrimarySharp;
half4 _SecondaryColor;
half _SecondaryStrength,_SecondaryFade,_SecondaryShift,_SecondarySharp;
CBUFFER_END
TEXTURE2D (_BaseMap);SAMPLER(sampler_BaseMap);
TEXTURE2D (_NoiseMap);SAMPLER(sampler_NoiseMap);

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

o.positionCS = TransformObjectToHClip(v.vertex.xyz);
o.uv.xy = TRANSFORM_TEX(v.uv, _BaseMap);
o.uv.zw = TRANSFORM_TEX(v.uv, _NoiseMap);
o.color = v.color;
o.normalWS.xyz = TransformObjectToWorldNormal(v.normal);
return o;
}

half4 frag(v2f i) : SV_Target
{
half4 c;
half4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, i.uv.xy);
clip(baseMap.a - _Cutoff);

Light mainLight = GetMainLight();
half3 L = mainLight.direction;
half3 N = normalize(i.normalWS.xyz);

// 漫反射
half NoL = dot(N,L)*0.4+0.6;
half4 NoLColor = lerp(_ShadowColor, _BaseColor, NoL);
// return half4(NoLColor,1);
half3 diffuse = baseMap.rgb * NoLColor.rgb;
// 顶点颜色r通道控制ao
half ao = lerp(1, i.color.r, _AOIntensity);
diffuse *= ao;
c.rgb = diffuse;

c.a = baseMap.a;

return c;
}
ENDHLSL
}

Pass
{
Tags {"LightMode" = "UniversalForward"}
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
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"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
float4 tangent : TANGENT;
half4 color : COLOR;
};

struct v2f
{
float4 positionCS : SV_POSITION;
float4 uv : TEXCOORD0;
float3 viewWS : TEXCOORD1;
half4 normalWS : TEXCOORD3;
half4 tangentWS : TEXCOORD4;
half4 bitangentWS : TEXCOORD5;
half4 color : TEXCOORD6;
};

CBUFFER_START(UnityPerMaterial)
half4 _BaseColor,_ShadowColor;
float4 _BaseMap_ST,_NoiseMap_ST;
half _Cutoff;
half _AOIntensity;
half4 _PrimaryColor;
half _PrimaryStrength,_PrimaryFade,_PrimaryShift,_PrimarySharp;
half4 _SecondaryColor;
half _SecondaryStrength,_SecondaryFade,_SecondaryShift,_SecondarySharp;
CBUFFER_END
TEXTURE2D (_BaseMap);SAMPLER(sampler_BaseMap);
TEXTURE2D (_NoiseMap);SAMPLER(sampler_NoiseMap);

// 各向异性高光
// noise:毛发噪波度
// @color:高光颜色
// @strength:高光强度
// @fade:高光范围
// @shift:高光偏移
// @sharp:锐化程度
half3 AnisotropicSpecular(half noise, half3 color, half strength, half fade, half shift, half sharp, half3 T,half3 H,half3 N,half3 V)
{
half3 specular;
half offset = noise.r * sharp + shift;
T = normalize(T + offset * H);
half ToH = dot(T,H);
half sinTH = sqrt(1-ToH*ToH);
// dirAtten:kajiyakay原公式,处理头发顶部暗底部亮衰减
// half dirAtten = smoothstep(-1,0,ToH);
// atten:非尼尔公式,处理头发中间亮两边暗衰减
half atten = saturate(dot(N,V));
specular = atten * strength * pow(saturate(sinTH), fade);
specular *= color.rgb;
return specular;
}

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

o.positionCS = TransformObjectToHClip(v.vertex.xyz);
o.uv.xy = TRANSFORM_TEX(v.uv, _BaseMap);
o.uv.zw = TRANSFORM_TEX(v.uv, _NoiseMap);
o.color = v.color;
float3 positionWS = TransformObjectToWorld(v.vertex.xyz);
o.viewWS = _WorldSpaceCameraPos - positionWS;
// o.tangentWS.xyz = TransformObjectToWorldDir(v.tangentOS.xyz);
o.normalWS.xyz = TransformObjectToWorldNormal(v.normal);
o.tangentWS.xyz = TransformObjectToWorldDir(v.tangent.xyz);
// half sign = v.tangentOS.w * GetOddNegativeScale();
// o.bitangentWS.xyz = cross(o.normalWS, o.tangentWS) * sign;
return o;
}

half4 frag(v2f i) : SV_Target
{
half4 c;
half4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, i.uv.xy);
half4 noiseMap = SAMPLE_TEXTURE2D(_NoiseMap, sampler_NoiseMap, i.uv.zw) - 0.5;

// 使用切线还是副切线由模型决定,模型uv决定,因为uv决定切线方向,uv是横着的时候使用负切线,竖着的时候使用切线
// 建议使用切线,相比副切线少了点计算量
// half3 T = normalize(i.bitangentWS);
half3 T = normalize(i.tangentWS.xyz);
Light mainLight = GetMainLight();
half3 L = mainLight.direction;
half3 N = normalize(i.normalWS.xyz);
half3 V = normalize(i.viewWS);
half3 H = normalize(L+V);

// 漫反射
half NoL = dot(N,L) * 0.4 + 0.6;
half4 NoLColor = lerp(_ShadowColor, _BaseColor, NoL);
// return half4(NoLColor,1);
half3 diffuse = baseMap.rgb * NoLColor.rgb;
// 顶点颜色控制ao
half ao = lerp(1, i.color.r, _AOIntensity);
diffuse *= ao;

// 各向异性高光
half3 primarySpecular = AnisotropicSpecular(noiseMap.r, _PrimaryColor.rgb, _PrimaryStrength, _PrimaryFade, _PrimaryShift, _PrimarySharp,T,H,N,V);
half3 secondarySpecular = AnisotropicSpecular(noiseMap.r, _SecondaryColor.rgb, _SecondaryStrength, _SecondaryFade, _SecondaryShift, _SecondarySharp,T,H,N,V);

c.rgb = diffuse + primarySpecular + secondarySpecular;

c.a = baseMap.a;

return c;
}
ENDHLSL
}
}

SubShader
{
Tags { "Queue"="Transparent" }
Cull Off

Pass
{
ZWrite On
ZTest LEqual

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

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
half4 color : COLOR;
};

struct v2f
{
float4 positionCS : SV_POSITION;
float4 uv : TEXCOORD0;
float3 viewWS : TEXCOORD1;
half4 normalWS : TEXCOORD3;
half4 tangentWS : TEXCOORD4;
half4 bitangentWS : TEXCOORD5;
half4 color : TEXCOORD6;
};

half4 _BaseColor,_ShadowColor;
sampler2D _BaseMap; float4 _BaseMap_ST;
sampler2D _NoiseMap; float4 _NoiseMap_ST;
half _Cutoff;
half _AOIntensity;
half4 _PrimaryColor;
half _PrimaryStrength,_PrimaryFade,_PrimaryShift,_PrimarySharp;
half4 _SecondaryColor;
half _SecondaryStrength,_SecondaryFade,_SecondaryShift,_SecondarySharp;

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

o.positionCS = UnityObjectToClipPos(v.vertex.xyz);
o.uv.xy = TRANSFORM_TEX(v.uv, _BaseMap);
o.uv.zw = TRANSFORM_TEX(v.uv, _NoiseMap);
o.color = v.color;
o.normalWS.xyz = UnityObjectToWorldNormal(v.normal);
return o;
}

half4 frag(v2f i) : SV_Target
{
half4 c;
half4 baseMap = tex2D(_BaseMap, i.uv.xy);
clip(baseMap.a - _Cutoff);

half3 L = _WorldSpaceLightPos0;
half3 N = normalize(i.normalWS.xyz);

// 漫反射
half NoL = dot(N,L)*0.4+0.6;
half4 NoLColor = lerp(_ShadowColor, _BaseColor, NoL);
// return half4(NoLColor,1);
half3 diffuse = baseMap.rgb * NoLColor.rgb;
// 顶点颜色r通道控制ao
half ao = lerp(1, i.color.r, _AOIntensity);
diffuse *= ao;
c.rgb = diffuse;

c.a = baseMap.a;

return c;
}

ENDCG
}

Pass
{
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off

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

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
float4 tangent : TANGENT;
half4 color : COLOR;
};

struct v2f
{
float4 positionCS : SV_POSITION;
float4 uv : TEXCOORD0;
float3 viewWS : TEXCOORD1;
half4 normalWS : TEXCOORD3;
half4 tangentWS : TEXCOORD4;
half4 bitangentWS : TEXCOORD5;
half4 color : TEXCOORD6;
};

half4 _BaseColor,_ShadowColor;
sampler2D _BaseMap; float4 _BaseMap_ST;
sampler2D _NoiseMap; float4 _NoiseMap_ST;
half _Cutoff;
half _AOIntensity;
half4 _PrimaryColor;
half _PrimaryStrength,_PrimaryFade,_PrimaryShift,_PrimarySharp;
half4 _SecondaryColor;
half _SecondaryStrength,_SecondaryFade,_SecondaryShift,_SecondarySharp;

// 各向异性高光
// noise:毛发噪波度
// @color:高光颜色
// @strength:高光强度
// @fade:高光范围
// @shift:高光偏移
// @sharp:锐化程度
half3 AnisotropicSpecular(half noise, half3 color, half strength, half fade, half shift, half sharp, half3 T,half3 H,half3 N,half3 V)
{
half3 specular;
half offset = noise.r * sharp + shift;
T = normalize(T + offset * H);
half ToH = dot(T,H);
half sinTH = sqrt(1-ToH*ToH);
// dirAtten:kajiyakay原公式,处理头发顶部暗底部亮衰减
// half dirAtten = smoothstep(-1,0,ToH);
// atten:非尼尔公式,处理头发中间亮两边暗衰减
half atten = saturate(dot(N,V));
specular = atten * strength * pow(saturate(sinTH), fade);
specular *= color.rgb;
return specular;
}

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

o.positionCS = UnityObjectToClipPos(v.vertex.xyz);
o.uv.xy = TRANSFORM_TEX(v.uv, _BaseMap);
o.uv.zw = TRANSFORM_TEX(v.uv, _NoiseMap);
o.color = v.color;
float3 positionWS = mul(UNITY_MATRIX_M, v.vertex);
o.viewWS = _WorldSpaceCameraPos - positionWS;
// o.tangentWS.xyz = TransformObjectToWorldDir(v.tangentOS.xyz);
o.normalWS.xyz = UnityObjectToWorldNormal(v.normal);
o.tangentWS.xyz = UnityObjectToWorldDir(v.tangent);
// half sign = v.tangentOS.w * GetOddNegativeScale();
// o.bitangentWS.xyz = cross(o.normalWS, o.tangentWS) * sign;
return o;
}

half4 frag(v2f i) : SV_Target
{
half4 c;
half4 baseMap = tex2D(_BaseMap, i.uv.xy);
half4 noiseMap = tex2D(_NoiseMap, i.uv.zw) - 0.5;

// 使用切线还是副切线由模型决定,模型uv决定,因为uv决定切线方向,uv是横着的时候使用负切线,竖着的时候使用切线
// 建议使用切线,相比副切线少了点计算量
// half3 T = normalize(i.bitangentWS);
half3 T = normalize(i.tangentWS.xyz);
half3 L = _WorldSpaceLightPos0;
half3 N = normalize(i.normalWS.xyz);
half3 V = normalize(i.viewWS);
half3 H = normalize(L+V);

// 漫反射
half NoL = dot(N,L) * 0.4 + 0.6;
half4 NoLColor = lerp(_ShadowColor, _BaseColor, NoL);
// return half4(NoLColor,1);
half3 diffuse = baseMap.rgb * NoLColor.rgb;
// 顶点颜色控制ao
half ao = lerp(1, i.color.r, _AOIntensity);
diffuse *= ao;

// 各向异性高光
half3 primarySpecular = AnisotropicSpecular(noiseMap.r, _PrimaryColor.rgb, _PrimaryStrength, _PrimaryFade, _PrimaryShift, _PrimarySharp,T,H,N,V);
half3 secondarySpecular = AnisotropicSpecular(noiseMap.r, _SecondaryColor.rgb, _SecondaryStrength, _SecondaryFade, _SecondaryShift, _SecondarySharp,T,H,N,V);

c.rgb = diffuse + primarySpecular + secondarySpecular;

c.a = baseMap.a;

return c;
}

ENDCG
}
}

}