Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiliMilk committed Jun 2, 2022
2 parents 28a311a + ea2cc68 commit 2ee5b2e
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 43 deletions.
51 changes: 37 additions & 14 deletions Assets/ChiliMilkToonShader/Editor/ToonShaderGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private struct Styles
public static readonly GUIContent Specular = new GUIContent("Specular");
public static readonly GUIContent Metallic = new GUIContent("Metallic");
public static readonly GUIContent Smoothness = new GUIContent("Smoothness");
public static readonly GUIContent EnableHairSpecular = new GUIContent("HairSpecular");
public static readonly GUIContent SpecularType = new GUIContent("SpecularType");
public static readonly GUIContent SpecularShiftMap = new GUIContent("HairShiftMap");
public static readonly GUIContent SpecularShift = new GUIContent("SpecularShift");
public static readonly GUIContent SpecularHighlights = new GUIContent("Enable Specular Highlights");
Expand All @@ -72,7 +72,8 @@ private struct Styles

//Rim
public static readonly GUIContent RimFlip = new GUIContent("RimFlip");
public static readonly GUIContent RimBlend = new GUIContent("RimBlend");
public static readonly GUIContent RimBlendShadow = new GUIContent("RimBlendShadow");
public static readonly GUIContent RimBlendLdotV = new GUIContent("RimBlendLdotV(BackLight)");
public static readonly GUIContent RimColor = new GUIContent("RimColor");
public static readonly GUIContent RimPow = new GUIContent("RimPow");
public static readonly GUIContent RimStep = new GUIContent("RimStep");
Expand Down Expand Up @@ -129,7 +130,7 @@ private struct MPropertyNames
public static readonly string SpecStep = "_SpecularStep";
public static readonly string SpecFeather = "_SpecularFeather";
public static readonly string Smoothness = "_Smoothness";
public static readonly string EnableHairSpecular = "_EnableHairSpecular";
public static readonly string SpecularType = "_SpecularType";
public static readonly string SpecularShiftMap = "_SpecularShiftMap";
public static readonly string SpecularShiftIntensity = "_SpecularShiftIntensity";
public static readonly string SpecularShift = "_SpecularShift";
Expand All @@ -150,7 +151,8 @@ private struct MPropertyNames

//Rim
public static readonly string RimFlip = "_RimFlip";
public static readonly string RimBlend = "_RimBlend";
public static readonly string RimBlendShadow = "_RimBlendShadow";
public static readonly string RimBlendLdotV = "_RimBlendLdotV";
public static readonly string RimColor = "_RimColor";
public static readonly string RimStep = "_RimStep";
public static readonly string RimFeather = "_RimFeather";
Expand Down Expand Up @@ -197,6 +199,13 @@ public enum ShadowType
FaceSDFShadow = 2
}

public enum SpecularType
{
Default = 0,
HairSpecularViewNormal,
HairSpecularTangent
}

#endregion

#region Fields
Expand Down Expand Up @@ -266,7 +275,7 @@ public enum ShadowType
private MaterialProperty m_SpecFeatherProp;
private MaterialProperty m_SmoothnessProp;
private MaterialProperty m_SpecularHighlightsProp;
private MaterialProperty m_EnableHairSpecularProp;
private MaterialProperty m_SpecularTypeProp;
private MaterialProperty m_SpeculatShiftMapProp;
private MaterialProperty m_SpecularShiftIntensityProp;
private MaterialProperty m_SpecularShiftProp;
Expand All @@ -279,7 +288,8 @@ public enum ShadowType

//Rim
private MaterialProperty m_RimFlipProp;
private MaterialProperty m_RimBlendProp;
private MaterialProperty m_RimBlendShadowProp;
private MaterialProperty m_RimBlendLdotVProp;
private MaterialProperty m_RimColorProp;
private MaterialProperty m_RimStepProp;
private MaterialProperty m_RimFeatherProp;
Expand Down Expand Up @@ -357,7 +367,7 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
m_OcclusionStrengthProp = FindProperty(MPropertyNames.OcclusionStrength, properties, false);
m_EmissionMapProp = FindProperty(MPropertyNames.EmissionMap, properties, false);
m_EmissionColorProp = FindProperty(MPropertyNames.EmissionColor, properties, false);
m_EnableHairSpecularProp = FindProperty(MPropertyNames.EnableHairSpecular, properties, false);
m_SpecularTypeProp = FindProperty(MPropertyNames.SpecularType, properties, false);

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

//Rim
m_RimFlipProp = FindProperty(MPropertyNames.RimFlip, properties, false);
m_RimBlendProp = FindProperty(MPropertyNames.RimBlend, properties, false);
m_RimBlendShadowProp = FindProperty(MPropertyNames.RimBlendShadow, properties, false);
m_RimBlendLdotVProp = FindProperty(MPropertyNames.RimBlendLdotV, properties, false);
m_RimColorProp = FindProperty(MPropertyNames.RimColor, properties, false);
m_RimStepProp = FindProperty(MPropertyNames.RimStep, properties, false);
m_RimFeatherProp = FindProperty(MPropertyNames.RimFeather, properties, false);
Expand Down Expand Up @@ -473,7 +484,8 @@ private void SetMaterialKeywords(Material material)
SetKeyword(material,"_EMISSION", hasEmissionMap || emissionColor != Color.black);

// HairSpecular
SetKeyword(material,"_HAIRSPECULAR", material.HasProperty(MPropertyNames.EnableHairSpecular) && material.GetFloat(MPropertyNames.EnableHairSpecular) == 1.0f);
SetKeyword(material, "_HAIRSPECULAR", material.HasProperty(MPropertyNames.SpecularType) && material.GetFloat(MPropertyNames.SpecularType) == 2.0f);
SetKeyword(material, "_HAIRSPECULARVIEWNORMAL", material.HasProperty(MPropertyNames.SpecularType) && material.GetFloat(MPropertyNames.SpecularType) == 1.0f);

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

// HairSpecular
if (material.HasProperty(MPropertyNames.EnableHairSpecular))
if (material.HasProperty(MPropertyNames.SpecularType))
{
materialEditor.ShaderProperty(m_EnableHairSpecularProp, Styles.EnableHairSpecular);
if (m_EnableHairSpecularProp.floatValue == 1.0)
EditorGUI.showMixedValue = m_SpecularTypeProp.hasMixedValue;
EditorGUI.BeginChangeCheck();
var specularType = EditorGUILayout.Popup(Styles.SpecularType, (int)m_SpecularTypeProp.floatValue, Enum.GetNames(typeof(SpecularType)));
if (EditorGUI.EndChangeCheck())
{
materialEditor.RegisterPropertyChangeUndo(Styles.SpecularType.text);
m_SpecularTypeProp.floatValue = specularType;
}
EditorGUI.showMixedValue = false;

if (m_SpecularTypeProp.floatValue == 2.0)
{
materialEditor.TexturePropertySingleLine(Styles.SpecularShiftMap, m_SpeculatShiftMapProp, m_SpecularShiftIntensityProp);
materialEditor.TextureScaleOffsetProperty(m_SpeculatShiftMapProp);
Expand Down Expand Up @@ -919,13 +940,15 @@ private void DrawRimProperties(MaterialEditor materialEditor)
materialEditor.ShaderProperty(m_RimFlipProp, Styles.RimFlip);
EditorGUI.BeginChangeCheck();
EditorGUI.indentLevel += 2;
var rimBlend = EditorGUILayout.Slider(Styles.RimBlend, m_RimBlendProp.floatValue, 0f, 1f);
var rimBlendShadow = EditorGUILayout.Slider(Styles.RimBlendShadow, m_RimBlendShadowProp.floatValue, 0f, 1f);
var rimBlendLdotV = EditorGUILayout.Slider(Styles.RimBlendLdotV, m_RimBlendLdotVProp.floatValue, 0f, 1f);
var rimStep = EditorGUILayout.Slider(Styles.RimStep, m_RimStepProp.floatValue, 0f, 1f);
var rimFeather = EditorGUILayout.Slider(Styles.RimFeather, m_RimFeatherProp.floatValue, 0f, 1f);
EditorGUI.indentLevel -= 2;
if (EditorGUI.EndChangeCheck())
{
m_RimBlendProp.floatValue = rimBlend;
m_RimBlendShadowProp.floatValue = rimBlendShadow;
m_RimBlendLdotVProp.floatValue = rimBlendLdotV;
m_RimStepProp.floatValue = rimStep;
m_RimFeatherProp.floatValue = rimFeather;
}
Expand Down
7 changes: 1 addition & 6 deletions Assets/ChiliMilkToonShader/Include/ToonFunction.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ float3 GetSmoothedWorldNormal(float2 uv7, float3x3 t_tbn)
//Use for HairSpecular,Roughness To BlinnPhong
float RoughnessToBlinnPhongSpecularExponent(float roughness)
{
#ifdef _HAIRSPECULAR
return clamp(2 * rcp(roughness * roughness) - 2, FLT_EPS, rcp(FLT_EPS));
#else
return 0;
#endif
}

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

//Use for Toon Diffuse
half DiffuseRadianceToon(half value,half step,half feather)
inline half StepFeatherToon(half value,half step,half feather)
{
return saturate((value-step+feather)/feather);
}
Expand Down
15 changes: 9 additions & 6 deletions Assets/ChiliMilkToonShader/Include/ToonInput.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ half _SpecularShift;
half _SpecularShiftIntensity;
float4 _SpecularShiftMap_ST;

half _RimBlend;
half _RimBlendShadow;
half _RimBlendLdotV;
half3 _RimColor;
half _RimFlip;
half _RimStep;
Expand Down Expand Up @@ -141,7 +142,8 @@ struct ToonData
half rimFlip;
half rimStep;
half rimFeather;
half rimBlend;
half rimBlendShadow;
half rimBlendLdotV;
};

#ifdef _MATCAP
Expand All @@ -156,7 +158,7 @@ half SampleAmbientOcclusionToon(float2 normalizedScreenSpaceUV, half occlusion)
half ssao = 1.0;
#if defined(_SCREEN_SPACE_OCCLUSION)
ssao = SampleAmbientOcclusion(normalizedScreenSpaceUV);
ssao = DiffuseRadianceToon(ssao, _Shadow1Step, _Shadow1Feather) * _SSAOStrength;
ssao = StepFeatherToon(ssao, _Shadow1Step, _Shadow1Feather) * _SSAOStrength;
#endif
ssao = min(ssao, occlusion);
return ssao;
Expand Down Expand Up @@ -265,7 +267,7 @@ half SampleSpecularShift(float2 uv)
#endif
}

half4 SampleMetallicSpecGloss(float2 uv, half albedoAlpha, half smoothness)
half4 SampleMetallicSpecGloss(float2 uv, half smoothness)
{
half4 specGloss = SAMPLE_METALLICSPECULAR(uv);
#if _SPECULAR_SETUP
Expand Down Expand Up @@ -296,7 +298,7 @@ inline void InitializeSurfaceDataToon(float2 uv,out SurfaceDataToon outSurfaceDa
{
half4 albedoAlpha = SampleAlbedoAlpha(uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));
outSurfaceData.alpha = Alpha(albedoAlpha.a, _BaseColor, _Cutoff);
half4 specGloss = SampleMetallicSpecGloss(uv, albedoAlpha.a, _Smoothness);
half4 specGloss = SampleMetallicSpecGloss(uv, _Smoothness);
outSurfaceData.albedo = albedoAlpha.rgb * _BaseColor.rgb;
#if _SPECULAR_SETUP
outSurfaceData.metallic = 1.0h;
Expand Down Expand Up @@ -335,7 +337,8 @@ inline void InitializeToonData(float2 uv, float2 normalizedScreenSpaceUV,float3
outToonData.rimColor = _RimColor;
outToonData.rimStep = _RimStep;
outToonData.rimFeather = _RimFeather;
outToonData.rimBlend = _RimBlend;
outToonData.rimBlendShadow = _RimBlendShadow;
outToonData.rimFlip = _RimFlip;
outToonData.rimBlendLdotV = _RimBlendLdotV;
}
#endif
31 changes: 22 additions & 9 deletions Assets/ChiliMilkToonShader/Include/ToonLighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct BRDFDataToon
half grazingTerm;
half normalizationTerm; // roughness * 4.0 + 2.0
half roughness2MinusOne; // roughness^2 - 1.0
#ifdef _HAIRSPECULAR
#if defined _HAIRSPECULAR || defined _HAIRSPECULARVIEWNORMAL
half specularExponent;
#endif
};
Expand All @@ -38,15 +38,15 @@ inline void InitializeBRDFDataToon(SurfaceDataToon surfaceData, out BRDFDataToon
outBRDFData.perceptualRoughness = PerceptualSmoothnessToPerceptualRoughness(surfaceData.smoothness);
outBRDFData.roughness = max(PerceptualRoughnessToRoughness(outBRDFData.perceptualRoughness), HALF_MIN_SQRT);
outBRDFData.roughness2 = max(outBRDFData.roughness * outBRDFData.roughness, HALF_MIN);
#ifdef _HAIRSPECULAR
#if defined _HAIRSPECULAR || defined _HAIRSPECULARVIEWNORMAL
outBRDFData.specularExponent = RoughnessToBlinnPhongSpecularExponent(outBRDFData.roughness);
#endif
outBRDFData.normalizationTerm = outBRDFData.roughness * 4.0h + 2.0h;
outBRDFData.roughness2MinusOne = outBRDFData.roughness2 - 1.0h;
}

#ifdef _HAIRSPECULAR
half2 DirectSpecularHairToon(half specularExponent, half specularShift, half3 normalWS, half3 lightDirectionWS, half3 viewDirectionWS, half3 bitangentWS,half specularStep, half specularFeather)
half DirectSpecularHairToon(half specularExponent, half specularShift, half3 normalWS, half3 lightDirectionWS, half3 viewDirectionWS, half3 bitangentWS,half specularStep, half specularFeather)
{
half3 t = ShiftTangent(bitangentWS,normalWS,specularShift);
half LdotV = dot(lightDirectionWS,viewDirectionWS);
Expand All @@ -57,6 +57,14 @@ half2 DirectSpecularHairToon(half specularExponent, half specularShift, half3 no
half s = StepFeatherToon(spec,maxSpec,specularStep,specularFeather);
return s;
}
#elif defined _HAIRSPECULARVIEWNORMAL
half DirectSpecularHairViewNormalToon(half specularExponent, half3 normalWS, half3 viewDirectionWS, half specularStep, half specularFeather)
{
half NdotV = saturate(dot(normalize(normalWS.xz), normalize(viewDirectionWS.xz)));
half s = pow(NdotV, specularExponent);

return StepFeatherToon(s, specularStep, specularFeather);
}
#else
half DirectSpecularToon(BRDFDataToon brdfData, half3 normalWS, half3 lightDirectionWS, half3 viewDirectionWS, half step, half feather)
{
Expand All @@ -80,6 +88,8 @@ half3 SpecularBDRFToon(BRDFDataToon brdfData, half3 normalWS, half3 lightDirecti
#ifndef _SPECULARHIGHLIGHTS_OFF
#ifdef _HAIRSPECULAR
half specularTerm = DirectSpecularHairToon(brdfData.specularExponent,specularShift,normalWS,lightDirectionWS,viewDirectionWS,bitangentWS,specularStep,specularFeather);
#elif defined _HAIRSPECULARVIEWNORMAL
half specularTerm = DirectSpecularHairViewNormalToon(brdfData.specularExponent, normalWS, viewDirectionWS, specularStep, specularFeather);
#else
half specularTerm = DirectSpecularToon(brdfData, normalWS, lightDirectionWS, viewDirectionWS, specularStep, specularFeather);
#endif
Expand Down Expand Up @@ -152,8 +162,8 @@ half2 RadianceToon(half3 normalWS, half3 lightDirectionWS, half lightAttenuation
lightAttenuation = lerp(StepAntiAliasing(lightAttenuation,0.5),lightAttenuation,shadow1Feather);
lightAttenuation = saturate(lightAttenuation * inShadow);
half H_Lambert = 0.5 * dot(normalWS, lightDirectionWS) + 0.5;
radiance.x = DiffuseRadianceToon(H_Lambert, shadow1Step, shadow1Feather) * lightAttenuation * HairShadowMaskAtten(hairShadowMask, H_Lambert);
radiance.y = DiffuseRadianceToon(H_Lambert,shadow2Step,shadow2Feather);
radiance.x = StepFeatherToon(H_Lambert, shadow1Step, shadow1Feather) * lightAttenuation * HairShadowMaskAtten(hairShadowMask, H_Lambert);
radiance.y = StepFeatherToon(H_Lambert,shadow2Step,shadow2Feather);
return radiance;
}
#else
Expand All @@ -169,13 +179,16 @@ half3 RampRadianceToon(half3 normalWS, half3 lightDirectionWS, half lightAttenua
#endif
#endif

float3 RimLight(half3 rimColor, half3 normalWS, half3 viewDirectionWS, half rimStep, half rimFeather, half rimBlend, half rimFlip, half radiance)
float3 RimLight(half3 rimColor, half3 normalWS, half3 viewDirectionWS, half3 lightDirectionWS, half rimStep, half rimFeather, half rimBlendShadow, half rimBlendLdotV, half rimFlip, half radiance)
{
half LdotV = dot(-lightDirectionWS, viewDirectionWS) * 0.5 + 0.5;

half fresnel = (1.0 - saturate(dot(normalWS, viewDirectionWS)));
fresnel = DiffuseRadianceToon(fresnel, rimStep, rimFeather);
fresnel = StepFeatherToon(fresnel, rimStep, rimFeather);
fresnel = lerp(fresnel, fresnel * LdotV, rimBlendLdotV);
half3 color = rimColor * fresnel;
radiance = lerp(radiance, 1 - radiance, rimFlip);
color = lerp(color, color * radiance, rimBlend);
color = lerp(color, color * radiance, rimBlendShadow);

return color;
}
Expand Down Expand Up @@ -217,7 +230,7 @@ half3 LightingToon(BRDFDataToon brdfData, SurfaceDataToon surfaceData, InputData
color += specularColor + diffuseColor;
#endif
#endif
color += isMainLight * RimLight(toonData.rimColor, inputData.normalWS, inputData.viewDirectionWS, toonData.rimStep, toonData.rimFeather, toonData.rimBlend, toonData.rimFlip ,radiance.x);
color += isMainLight * RimLight(toonData.rimColor, inputData.normalWS, inputData.viewDirectionWS, light.direction, toonData.rimStep, toonData.rimFeather, toonData.rimBlendShadow, toonData.rimBlendLdotV,toonData.rimFlip ,radiance.x);
color += isMainLight * MatCapLight(inputData.normalWS, inputData.viewDirectionWS, radiance.x);
return color * light.color;
}
Expand Down
5 changes: 3 additions & 2 deletions Assets/ChiliMilkToonShader/Include/ToonOutlinePass.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct Attributes
float4 tangentOS : TANGENT;
float2 texcoord7 : TEXCOORD7;
#endif
float4 color : COLOR;
UNITY_VERTEX_INPUT_INSTANCE_ID
};

Expand Down Expand Up @@ -52,9 +53,9 @@ Varyings Vertex(Attributes input)
float3 bitangentDir = normalize(cross(normalDir, tangentDir) * input.tangentOS.w);
float3x3 t_tbn = float3x3(tangentDir,bitangentDir,normalDir);
float3 bakeNormal = GetSmoothedWorldNormal(input.texcoord7,t_tbn);
output.positionCS = TransformOutlineToHClipScreenSpace(input.positionOS, bakeNormal, _OutlineWidth);
output.positionCS = TransformOutlineToHClipScreenSpace(input.positionOS, bakeNormal, _OutlineWidth * input.color.r);
#else
output.positionCS = TransformOutlineToHClipScreenSpace(input.positionOS, input.normalOS, _OutlineWidth);
output.positionCS = TransformOutlineToHClipScreenSpace(input.positionOS, input.normalOS, _OutlineWidth * input.color.r);
#endif
return output;
}
Expand Down
Loading

0 comments on commit 2ee5b2e

Please sign in to comment.