Skip to content

Commit 2ee5b2e

Browse files
committed
Merge branch 'dev'
2 parents 28a311a + ea2cc68 commit 2ee5b2e

File tree

6 files changed

+78
-43
lines changed

6 files changed

+78
-43
lines changed

Assets/ChiliMilkToonShader/Editor/ToonShaderGUI.cs

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private struct Styles
6060
public static readonly GUIContent Specular = new GUIContent("Specular");
6161
public static readonly GUIContent Metallic = new GUIContent("Metallic");
6262
public static readonly GUIContent Smoothness = new GUIContent("Smoothness");
63-
public static readonly GUIContent EnableHairSpecular = new GUIContent("HairSpecular");
63+
public static readonly GUIContent SpecularType = new GUIContent("SpecularType");
6464
public static readonly GUIContent SpecularShiftMap = new GUIContent("HairShiftMap");
6565
public static readonly GUIContent SpecularShift = new GUIContent("SpecularShift");
6666
public static readonly GUIContent SpecularHighlights = new GUIContent("Enable Specular Highlights");
@@ -72,7 +72,8 @@ private struct Styles
7272

7373
//Rim
7474
public static readonly GUIContent RimFlip = new GUIContent("RimFlip");
75-
public static readonly GUIContent RimBlend = new GUIContent("RimBlend");
75+
public static readonly GUIContent RimBlendShadow = new GUIContent("RimBlendShadow");
76+
public static readonly GUIContent RimBlendLdotV = new GUIContent("RimBlendLdotV(BackLight)");
7677
public static readonly GUIContent RimColor = new GUIContent("RimColor");
7778
public static readonly GUIContent RimPow = new GUIContent("RimPow");
7879
public static readonly GUIContent RimStep = new GUIContent("RimStep");
@@ -129,7 +130,7 @@ private struct MPropertyNames
129130
public static readonly string SpecStep = "_SpecularStep";
130131
public static readonly string SpecFeather = "_SpecularFeather";
131132
public static readonly string Smoothness = "_Smoothness";
132-
public static readonly string EnableHairSpecular = "_EnableHairSpecular";
133+
public static readonly string SpecularType = "_SpecularType";
133134
public static readonly string SpecularShiftMap = "_SpecularShiftMap";
134135
public static readonly string SpecularShiftIntensity = "_SpecularShiftIntensity";
135136
public static readonly string SpecularShift = "_SpecularShift";
@@ -150,7 +151,8 @@ private struct MPropertyNames
150151

151152
//Rim
152153
public static readonly string RimFlip = "_RimFlip";
153-
public static readonly string RimBlend = "_RimBlend";
154+
public static readonly string RimBlendShadow = "_RimBlendShadow";
155+
public static readonly string RimBlendLdotV = "_RimBlendLdotV";
154156
public static readonly string RimColor = "_RimColor";
155157
public static readonly string RimStep = "_RimStep";
156158
public static readonly string RimFeather = "_RimFeather";
@@ -197,6 +199,13 @@ public enum ShadowType
197199
FaceSDFShadow = 2
198200
}
199201

202+
public enum SpecularType
203+
{
204+
Default = 0,
205+
HairSpecularViewNormal,
206+
HairSpecularTangent
207+
}
208+
200209
#endregion
201210

202211
#region Fields
@@ -266,7 +275,7 @@ public enum ShadowType
266275
private MaterialProperty m_SpecFeatherProp;
267276
private MaterialProperty m_SmoothnessProp;
268277
private MaterialProperty m_SpecularHighlightsProp;
269-
private MaterialProperty m_EnableHairSpecularProp;
278+
private MaterialProperty m_SpecularTypeProp;
270279
private MaterialProperty m_SpeculatShiftMapProp;
271280
private MaterialProperty m_SpecularShiftIntensityProp;
272281
private MaterialProperty m_SpecularShiftProp;
@@ -279,7 +288,8 @@ public enum ShadowType
279288

280289
//Rim
281290
private MaterialProperty m_RimFlipProp;
282-
private MaterialProperty m_RimBlendProp;
291+
private MaterialProperty m_RimBlendShadowProp;
292+
private MaterialProperty m_RimBlendLdotVProp;
283293
private MaterialProperty m_RimColorProp;
284294
private MaterialProperty m_RimStepProp;
285295
private MaterialProperty m_RimFeatherProp;
@@ -357,7 +367,7 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
357367
m_OcclusionStrengthProp = FindProperty(MPropertyNames.OcclusionStrength, properties, false);
358368
m_EmissionMapProp = FindProperty(MPropertyNames.EmissionMap, properties, false);
359369
m_EmissionColorProp = FindProperty(MPropertyNames.EmissionColor, properties, false);
360-
m_EnableHairSpecularProp = FindProperty(MPropertyNames.EnableHairSpecular, properties, false);
370+
m_SpecularTypeProp = FindProperty(MPropertyNames.SpecularType, properties, false);
361371

362372
//Outline
363373
m_EnableOutlineProp = FindProperty(MPropertyNames.EnableOutline, properties, false);
@@ -367,7 +377,8 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
367377

368378
//Rim
369379
m_RimFlipProp = FindProperty(MPropertyNames.RimFlip, properties, false);
370-
m_RimBlendProp = FindProperty(MPropertyNames.RimBlend, properties, false);
380+
m_RimBlendShadowProp = FindProperty(MPropertyNames.RimBlendShadow, properties, false);
381+
m_RimBlendLdotVProp = FindProperty(MPropertyNames.RimBlendLdotV, properties, false);
371382
m_RimColorProp = FindProperty(MPropertyNames.RimColor, properties, false);
372383
m_RimStepProp = FindProperty(MPropertyNames.RimStep, properties, false);
373384
m_RimFeatherProp = FindProperty(MPropertyNames.RimFeather, properties, false);
@@ -473,7 +484,8 @@ private void SetMaterialKeywords(Material material)
473484
SetKeyword(material,"_EMISSION", hasEmissionMap || emissionColor != Color.black);
474485

475486
// HairSpecular
476-
SetKeyword(material,"_HAIRSPECULAR", material.HasProperty(MPropertyNames.EnableHairSpecular) && material.GetFloat(MPropertyNames.EnableHairSpecular) == 1.0f);
487+
SetKeyword(material, "_HAIRSPECULAR", material.HasProperty(MPropertyNames.SpecularType) && material.GetFloat(MPropertyNames.SpecularType) == 2.0f);
488+
SetKeyword(material, "_HAIRSPECULARVIEWNORMAL", material.HasProperty(MPropertyNames.SpecularType) && material.GetFloat(MPropertyNames.SpecularType) == 1.0f);
477489

478490
//Outline
479491
SetKeyword(material,"_USESMOOTHNORMAL", material.GetFloat(MPropertyNames.UseSmoothNormal) == 1.0);
@@ -887,10 +899,19 @@ private void DrawSpecularProperties(MaterialEditor materialEditor)
887899
}
888900

889901
// HairSpecular
890-
if (material.HasProperty(MPropertyNames.EnableHairSpecular))
902+
if (material.HasProperty(MPropertyNames.SpecularType))
891903
{
892-
materialEditor.ShaderProperty(m_EnableHairSpecularProp, Styles.EnableHairSpecular);
893-
if (m_EnableHairSpecularProp.floatValue == 1.0)
904+
EditorGUI.showMixedValue = m_SpecularTypeProp.hasMixedValue;
905+
EditorGUI.BeginChangeCheck();
906+
var specularType = EditorGUILayout.Popup(Styles.SpecularType, (int)m_SpecularTypeProp.floatValue, Enum.GetNames(typeof(SpecularType)));
907+
if (EditorGUI.EndChangeCheck())
908+
{
909+
materialEditor.RegisterPropertyChangeUndo(Styles.SpecularType.text);
910+
m_SpecularTypeProp.floatValue = specularType;
911+
}
912+
EditorGUI.showMixedValue = false;
913+
914+
if (m_SpecularTypeProp.floatValue == 2.0)
894915
{
895916
materialEditor.TexturePropertySingleLine(Styles.SpecularShiftMap, m_SpeculatShiftMapProp, m_SpecularShiftIntensityProp);
896917
materialEditor.TextureScaleOffsetProperty(m_SpeculatShiftMapProp);
@@ -919,13 +940,15 @@ private void DrawRimProperties(MaterialEditor materialEditor)
919940
materialEditor.ShaderProperty(m_RimFlipProp, Styles.RimFlip);
920941
EditorGUI.BeginChangeCheck();
921942
EditorGUI.indentLevel += 2;
922-
var rimBlend = EditorGUILayout.Slider(Styles.RimBlend, m_RimBlendProp.floatValue, 0f, 1f);
943+
var rimBlendShadow = EditorGUILayout.Slider(Styles.RimBlendShadow, m_RimBlendShadowProp.floatValue, 0f, 1f);
944+
var rimBlendLdotV = EditorGUILayout.Slider(Styles.RimBlendLdotV, m_RimBlendLdotVProp.floatValue, 0f, 1f);
923945
var rimStep = EditorGUILayout.Slider(Styles.RimStep, m_RimStepProp.floatValue, 0f, 1f);
924946
var rimFeather = EditorGUILayout.Slider(Styles.RimFeather, m_RimFeatherProp.floatValue, 0f, 1f);
925947
EditorGUI.indentLevel -= 2;
926948
if (EditorGUI.EndChangeCheck())
927949
{
928-
m_RimBlendProp.floatValue = rimBlend;
950+
m_RimBlendShadowProp.floatValue = rimBlendShadow;
951+
m_RimBlendLdotVProp.floatValue = rimBlendLdotV;
929952
m_RimStepProp.floatValue = rimStep;
930953
m_RimFeatherProp.floatValue = rimFeather;
931954
}

Assets/ChiliMilkToonShader/Include/ToonFunction.hlsl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ float3 GetSmoothedWorldNormal(float2 uv7, float3x3 t_tbn)
1515
//Use for HairSpecular,Roughness To BlinnPhong
1616
float RoughnessToBlinnPhongSpecularExponent(float roughness)
1717
{
18-
#ifdef _HAIRSPECULAR
1918
return clamp(2 * rcp(roughness * roughness) - 2, FLT_EPS, rcp(FLT_EPS));
20-
#else
21-
return 0;
22-
#endif
2319
}
2420

2521
//AntiAliasing,we use to calculate shadow
@@ -29,8 +25,7 @@ half StepAntiAliasing(half x, half y)
2925
return saturate(v / (fwidth(v)+HALF_MIN));//fwidth(x) = abs(ddx(x) + ddy(x))
3026
}
3127

32-
//Use for Toon Diffuse
33-
half DiffuseRadianceToon(half value,half step,half feather)
28+
inline half StepFeatherToon(half value,half step,half feather)
3429
{
3530
return saturate((value-step+feather)/feather);
3631
}

Assets/ChiliMilkToonShader/Include/ToonInput.hlsl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ half _SpecularShift;
3838
half _SpecularShiftIntensity;
3939
float4 _SpecularShiftMap_ST;
4040

41-
half _RimBlend;
41+
half _RimBlendShadow;
42+
half _RimBlendLdotV;
4243
half3 _RimColor;
4344
half _RimFlip;
4445
half _RimStep;
@@ -141,7 +142,8 @@ struct ToonData
141142
half rimFlip;
142143
half rimStep;
143144
half rimFeather;
144-
half rimBlend;
145+
half rimBlendShadow;
146+
half rimBlendLdotV;
145147
};
146148

147149
#ifdef _MATCAP
@@ -156,7 +158,7 @@ half SampleAmbientOcclusionToon(float2 normalizedScreenSpaceUV, half occlusion)
156158
half ssao = 1.0;
157159
#if defined(_SCREEN_SPACE_OCCLUSION)
158160
ssao = SampleAmbientOcclusion(normalizedScreenSpaceUV);
159-
ssao = DiffuseRadianceToon(ssao, _Shadow1Step, _Shadow1Feather) * _SSAOStrength;
161+
ssao = StepFeatherToon(ssao, _Shadow1Step, _Shadow1Feather) * _SSAOStrength;
160162
#endif
161163
ssao = min(ssao, occlusion);
162164
return ssao;
@@ -265,7 +267,7 @@ half SampleSpecularShift(float2 uv)
265267
#endif
266268
}
267269

268-
half4 SampleMetallicSpecGloss(float2 uv, half albedoAlpha, half smoothness)
270+
half4 SampleMetallicSpecGloss(float2 uv, half smoothness)
269271
{
270272
half4 specGloss = SAMPLE_METALLICSPECULAR(uv);
271273
#if _SPECULAR_SETUP
@@ -296,7 +298,7 @@ inline void InitializeSurfaceDataToon(float2 uv,out SurfaceDataToon outSurfaceDa
296298
{
297299
half4 albedoAlpha = SampleAlbedoAlpha(uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));
298300
outSurfaceData.alpha = Alpha(albedoAlpha.a, _BaseColor, _Cutoff);
299-
half4 specGloss = SampleMetallicSpecGloss(uv, albedoAlpha.a, _Smoothness);
301+
half4 specGloss = SampleMetallicSpecGloss(uv, _Smoothness);
300302
outSurfaceData.albedo = albedoAlpha.rgb * _BaseColor.rgb;
301303
#if _SPECULAR_SETUP
302304
outSurfaceData.metallic = 1.0h;
@@ -335,7 +337,8 @@ inline void InitializeToonData(float2 uv, float2 normalizedScreenSpaceUV,float3
335337
outToonData.rimColor = _RimColor;
336338
outToonData.rimStep = _RimStep;
337339
outToonData.rimFeather = _RimFeather;
338-
outToonData.rimBlend = _RimBlend;
340+
outToonData.rimBlendShadow = _RimBlendShadow;
339341
outToonData.rimFlip = _RimFlip;
342+
outToonData.rimBlendLdotV = _RimBlendLdotV;
340343
}
341344
#endif

Assets/ChiliMilkToonShader/Include/ToonLighting.hlsl

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct BRDFDataToon
1414
half grazingTerm;
1515
half normalizationTerm; // roughness * 4.0 + 2.0
1616
half roughness2MinusOne; // roughness^2 - 1.0
17-
#ifdef _HAIRSPECULAR
17+
#if defined _HAIRSPECULAR || defined _HAIRSPECULARVIEWNORMAL
1818
half specularExponent;
1919
#endif
2020
};
@@ -38,15 +38,15 @@ inline void InitializeBRDFDataToon(SurfaceDataToon surfaceData, out BRDFDataToon
3838
outBRDFData.perceptualRoughness = PerceptualSmoothnessToPerceptualRoughness(surfaceData.smoothness);
3939
outBRDFData.roughness = max(PerceptualRoughnessToRoughness(outBRDFData.perceptualRoughness), HALF_MIN_SQRT);
4040
outBRDFData.roughness2 = max(outBRDFData.roughness * outBRDFData.roughness, HALF_MIN);
41-
#ifdef _HAIRSPECULAR
41+
#if defined _HAIRSPECULAR || defined _HAIRSPECULARVIEWNORMAL
4242
outBRDFData.specularExponent = RoughnessToBlinnPhongSpecularExponent(outBRDFData.roughness);
4343
#endif
4444
outBRDFData.normalizationTerm = outBRDFData.roughness * 4.0h + 2.0h;
4545
outBRDFData.roughness2MinusOne = outBRDFData.roughness2 - 1.0h;
4646
}
4747

4848
#ifdef _HAIRSPECULAR
49-
half2 DirectSpecularHairToon(half specularExponent, half specularShift, half3 normalWS, half3 lightDirectionWS, half3 viewDirectionWS, half3 bitangentWS,half specularStep, half specularFeather)
49+
half DirectSpecularHairToon(half specularExponent, half specularShift, half3 normalWS, half3 lightDirectionWS, half3 viewDirectionWS, half3 bitangentWS,half specularStep, half specularFeather)
5050
{
5151
half3 t = ShiftTangent(bitangentWS,normalWS,specularShift);
5252
half LdotV = dot(lightDirectionWS,viewDirectionWS);
@@ -57,6 +57,14 @@ half2 DirectSpecularHairToon(half specularExponent, half specularShift, half3 no
5757
half s = StepFeatherToon(spec,maxSpec,specularStep,specularFeather);
5858
return s;
5959
}
60+
#elif defined _HAIRSPECULARVIEWNORMAL
61+
half DirectSpecularHairViewNormalToon(half specularExponent, half3 normalWS, half3 viewDirectionWS, half specularStep, half specularFeather)
62+
{
63+
half NdotV = saturate(dot(normalize(normalWS.xz), normalize(viewDirectionWS.xz)));
64+
half s = pow(NdotV, specularExponent);
65+
66+
return StepFeatherToon(s, specularStep, specularFeather);
67+
}
6068
#else
6169
half DirectSpecularToon(BRDFDataToon brdfData, half3 normalWS, half3 lightDirectionWS, half3 viewDirectionWS, half step, half feather)
6270
{
@@ -80,6 +88,8 @@ half3 SpecularBDRFToon(BRDFDataToon brdfData, half3 normalWS, half3 lightDirecti
8088
#ifndef _SPECULARHIGHLIGHTS_OFF
8189
#ifdef _HAIRSPECULAR
8290
half specularTerm = DirectSpecularHairToon(brdfData.specularExponent,specularShift,normalWS,lightDirectionWS,viewDirectionWS,bitangentWS,specularStep,specularFeather);
91+
#elif defined _HAIRSPECULARVIEWNORMAL
92+
half specularTerm = DirectSpecularHairViewNormalToon(brdfData.specularExponent, normalWS, viewDirectionWS, specularStep, specularFeather);
8393
#else
8494
half specularTerm = DirectSpecularToon(brdfData, normalWS, lightDirectionWS, viewDirectionWS, specularStep, specularFeather);
8595
#endif
@@ -152,8 +162,8 @@ half2 RadianceToon(half3 normalWS, half3 lightDirectionWS, half lightAttenuation
152162
lightAttenuation = lerp(StepAntiAliasing(lightAttenuation,0.5),lightAttenuation,shadow1Feather);
153163
lightAttenuation = saturate(lightAttenuation * inShadow);
154164
half H_Lambert = 0.5 * dot(normalWS, lightDirectionWS) + 0.5;
155-
radiance.x = DiffuseRadianceToon(H_Lambert, shadow1Step, shadow1Feather) * lightAttenuation * HairShadowMaskAtten(hairShadowMask, H_Lambert);
156-
radiance.y = DiffuseRadianceToon(H_Lambert,shadow2Step,shadow2Feather);
165+
radiance.x = StepFeatherToon(H_Lambert, shadow1Step, shadow1Feather) * lightAttenuation * HairShadowMaskAtten(hairShadowMask, H_Lambert);
166+
radiance.y = StepFeatherToon(H_Lambert,shadow2Step,shadow2Feather);
157167
return radiance;
158168
}
159169
#else
@@ -169,13 +179,16 @@ half3 RampRadianceToon(half3 normalWS, half3 lightDirectionWS, half lightAttenua
169179
#endif
170180
#endif
171181

172-
float3 RimLight(half3 rimColor, half3 normalWS, half3 viewDirectionWS, half rimStep, half rimFeather, half rimBlend, half rimFlip, half radiance)
182+
float3 RimLight(half3 rimColor, half3 normalWS, half3 viewDirectionWS, half3 lightDirectionWS, half rimStep, half rimFeather, half rimBlendShadow, half rimBlendLdotV, half rimFlip, half radiance)
173183
{
184+
half LdotV = dot(-lightDirectionWS, viewDirectionWS) * 0.5 + 0.5;
185+
174186
half fresnel = (1.0 - saturate(dot(normalWS, viewDirectionWS)));
175-
fresnel = DiffuseRadianceToon(fresnel, rimStep, rimFeather);
187+
fresnel = StepFeatherToon(fresnel, rimStep, rimFeather);
188+
fresnel = lerp(fresnel, fresnel * LdotV, rimBlendLdotV);
176189
half3 color = rimColor * fresnel;
177190
radiance = lerp(radiance, 1 - radiance, rimFlip);
178-
color = lerp(color, color * radiance, rimBlend);
191+
color = lerp(color, color * radiance, rimBlendShadow);
179192

180193
return color;
181194
}
@@ -217,7 +230,7 @@ half3 LightingToon(BRDFDataToon brdfData, SurfaceDataToon surfaceData, InputData
217230
color += specularColor + diffuseColor;
218231
#endif
219232
#endif
220-
color += isMainLight * RimLight(toonData.rimColor, inputData.normalWS, inputData.viewDirectionWS, toonData.rimStep, toonData.rimFeather, toonData.rimBlend, toonData.rimFlip ,radiance.x);
233+
color += isMainLight * RimLight(toonData.rimColor, inputData.normalWS, inputData.viewDirectionWS, light.direction, toonData.rimStep, toonData.rimFeather, toonData.rimBlendShadow, toonData.rimBlendLdotV,toonData.rimFlip ,radiance.x);
221234
color += isMainLight * MatCapLight(inputData.normalWS, inputData.viewDirectionWS, radiance.x);
222235
return color * light.color;
223236
}

Assets/ChiliMilkToonShader/Include/ToonOutlinePass.hlsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct Attributes
1111
float4 tangentOS : TANGENT;
1212
float2 texcoord7 : TEXCOORD7;
1313
#endif
14+
float4 color : COLOR;
1415
UNITY_VERTEX_INPUT_INSTANCE_ID
1516
};
1617

@@ -52,9 +53,9 @@ Varyings Vertex(Attributes input)
5253
float3 bitangentDir = normalize(cross(normalDir, tangentDir) * input.tangentOS.w);
5354
float3x3 t_tbn = float3x3(tangentDir,bitangentDir,normalDir);
5455
float3 bakeNormal = GetSmoothedWorldNormal(input.texcoord7,t_tbn);
55-
output.positionCS = TransformOutlineToHClipScreenSpace(input.positionOS, bakeNormal, _OutlineWidth);
56+
output.positionCS = TransformOutlineToHClipScreenSpace(input.positionOS, bakeNormal, _OutlineWidth * input.color.r);
5657
#else
57-
output.positionCS = TransformOutlineToHClipScreenSpace(input.positionOS, input.normalOS, _OutlineWidth);
58+
output.positionCS = TransformOutlineToHClipScreenSpace(input.positionOS, input.normalOS, _OutlineWidth * input.color.r);
5859
#endif
5960
return output;
6061
}

0 commit comments

Comments
 (0)