Skip to content

Commit

Permalink
Add SDF Face Shadow
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiliMilk committed Mar 22, 2022
1 parent 1b893e3 commit da5f273
Show file tree
Hide file tree
Showing 28 changed files with 1,307 additions and 78 deletions.
145 changes: 89 additions & 56 deletions Assets/ChiliMilkToonShader/Editor/ToonShaderGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private struct Styles
public static readonly GUIContent Emission = new GUIContent("Emission");

//Shadow
public static readonly GUIContent EnableRampMap = new GUIContent("UseRampMapShadow");
public static readonly GUIContent ShadowType = new GUIContent("ShadowType");
public static readonly GUIContent DiffuseRampMap = new GUIContent("DiffuseRampMap VOffset");
public static readonly GUIContent Shadow1Step = new GUIContent("Shadow1Step");
public static readonly GUIContent Shadow1Feather = new GUIContent("Shadow1Feather");
Expand All @@ -52,6 +52,7 @@ private struct Styles
public static readonly GUIContent ReceiveHairShadowMask = new GUIContent("ReceiveHairShadowMask(Front Hair ShadowMask)");
public static readonly GUIContent ReceiveHairShadowOffset = new GUIContent("ReceiveHairShadowOffset(Screen Space UV Offset)");
public static readonly GUIContent SSAOStrength = new GUIContent("SSAO");
public static readonly GUIContent SDFShadowMap = new GUIContent("SDFShadowMap(Face)");

//Specular
public static readonly GUIContent SpecularStep = new GUIContent("SpecularStep");
Expand Down Expand Up @@ -107,14 +108,15 @@ private struct MPropertyNames
public static readonly string Shadow2Feather = "_Shadow2Feather";
public static readonly string InShadowMap = "_InShadowMap";
public static readonly string InShadowMapStrength = "_InShadowMapStrength";
public static readonly string EnableRampMap = "_EnableRampMap";
public static readonly string ShadowType = "_ShadowType";
public static readonly string DiffuseRampMap = "_DiffuseRampMap";
public static readonly string DiffuseRampV = "_DiffuseRampV";
public static readonly string ReceiveShadows = "_ReceiveShadows";
public static readonly string CastHairShadowMask = "_CastHairShadowMask";
public static readonly string ReceiveHairShadowMask = "_ReceiveHairShadowMask";
public static readonly string ReceiveHairShadowOffset = "_ReceiveHairShadowOffset";
public static readonly string SSAOStrength = "_SSAOStrength";
public static readonly string SDFShadowMap = "_SDFShadowMap";

//Specular
public static readonly string SpecularHighlights = "_SpecularHighlights";
Expand Down Expand Up @@ -182,6 +184,13 @@ public enum StencilType
Out = 1
}

public enum ShadowType
{
DoubleShade = 0,
Ramp = 1,
FaceSDFShadow = 2
}

#endregion

#region Fields
Expand Down Expand Up @@ -222,7 +231,7 @@ public enum StencilType
//private MaterialProperty m_Shadow2MapProp;
private MaterialProperty m_Shadow2StepProp;
private MaterialProperty m_Shadow2FeatherProp;
private MaterialProperty m_EnableRampMapProp;
private MaterialProperty m_ShadowTypeProp;
private MaterialProperty m_DiffuseRampMapProp;
private MaterialProperty m_DiffuseRampVProp;
private MaterialProperty m_InShadowMapProp;
Expand All @@ -232,6 +241,7 @@ public enum StencilType
private MaterialProperty m_ReceiveHairShadowMaskProp;
private MaterialProperty m_ReceiveHairShadowOffsetProp;
private MaterialProperty m_SSAOStrengthProp;
private MaterialProperty m_SDFShadowMapProp;

//Base
private MaterialProperty m_BumpMapProp;
Expand Down Expand Up @@ -306,7 +316,7 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
//m_Shadow2MapProp = FindProperty(MPropertyNames.Shadow2Map, properties, false);
m_Shadow2StepProp = FindProperty(MPropertyNames.Shadow2Step, properties, false);
m_Shadow2FeatherProp = FindProperty(MPropertyNames.Shadow2Feather, properties, false);
m_EnableRampMapProp = FindProperty(MPropertyNames.EnableRampMap, properties, false);
m_ShadowTypeProp = FindProperty(MPropertyNames.ShadowType, properties, false);
m_DiffuseRampMapProp = FindProperty(MPropertyNames.DiffuseRampMap, properties, false);
m_DiffuseRampVProp = FindProperty(MPropertyNames.DiffuseRampV, properties, false);
m_InShadowMapProp = FindProperty(MPropertyNames.InShadowMap, properties, false);
Expand All @@ -316,6 +326,7 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
m_ReceiveHairShadowMaskProp = FindProperty(MPropertyNames.ReceiveHairShadowMask, properties, false);
m_ReceiveHairShadowOffsetProp = FindProperty(MPropertyNames.ReceiveHairShadowOffset, properties, false);
m_SSAOStrengthProp = FindProperty(MPropertyNames.SSAOStrength, properties, false);
m_SDFShadowMapProp = FindProperty(MPropertyNames.SDFShadowMap, properties, false);

//Specular
m_MetallicProp = FindProperty(MPropertyNames.Metallic, properties);
Expand Down Expand Up @@ -458,19 +469,23 @@ private void SetMaterialKeywords(Material material)
SetKeyword(material,"_EMISSION", hasEmissionMap || emissionColor != Color.black);

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

//Outline
SetKeyword(material,"_USESMOOTHNORMAL", material.GetFloat(MPropertyNames.UseSmoothNormal) == 1.0);

//RampMap
SetKeyword(material,"_DIFFUSERAMPMAP", material.GetFloat(MPropertyNames.EnableRampMap)==1.0f);
SetKeyword(material,"_DIFFUSERAMPMAP", material.HasProperty(MPropertyNames.ShadowType) && material.GetFloat(MPropertyNames.ShadowType) == 1.0f);
SetKeyword(material, "_SDFSHADOWMAP", material.HasProperty(MPropertyNames.ShadowType) && material.GetFloat(MPropertyNames.ShadowType) == 2.0f);
}

private void SetPass(Material material)
{
material.SetShaderPassEnabled("Outline", material.GetFloat(MPropertyNames.EnableOutline) == 1.0f);
material.SetShaderPassEnabled("HairShadowMask", material.GetFloat(MPropertyNames.CastHairShadowMask) == 1.0f);
if (material.HasProperty(MPropertyNames.CastHairShadowMask))
{
material.SetShaderPassEnabled("HairShadowMask", material.GetFloat(MPropertyNames.CastHairShadowMask) == 1.0f);
}
}

#endregion
Expand Down Expand Up @@ -564,21 +579,6 @@ private void DrawSurfaceOptions(MaterialEditor materialEditor)
// Workflow Mode
DoPopup(Styles.WorkflowMode, m_WorkflowModeProp, Enum.GetNames(typeof(WorkflowMode)),materialEditor);

//// Render Face
//if (material.HasProperty(MPropertyNames.Cull))
//{
// EditorGUI.showMixedValue = m_CullProp.hasMixedValue;
// EditorGUI.BeginChangeCheck();
// int renderFace = EditorGUILayout.Popup(Styles.RenderFace, (int)m_CullProp.floatValue, Enum.GetNames(typeof(RenderFace)));
// if (EditorGUI.EndChangeCheck())
// {
// materialEditor.RegisterPropertyChangeUndo(Styles.RenderFace.text);
// m_CullProp.floatValue = renderFace;
// material.doubleSidedGI = (RenderFace)m_CullProp.floatValue != RenderFace.Front;
// }
// EditorGUI.showMixedValue = false;
//}

if (material.HasProperty(MPropertyNames.SurfaceType))
{
EditorGUI.showMixedValue = m_SurfaceTypeProp.hasMixedValue;
Expand Down Expand Up @@ -722,29 +722,58 @@ private void DrawShadowProperties(MaterialEditor materialEditor)
materialEditor.TexturePropertySingleLine(Styles.InShadowMap, m_InShadowMapProp, m_InShadowMapStrengthProp);

//Diffuse
materialEditor.ShaderProperty(m_EnableRampMapProp, Styles.EnableRampMap);
if (m_EnableRampMapProp.floatValue == 1.0)
{
//Use Ramp
materialEditor.TexturePropertySingleLine(Styles.DiffuseRampMap, m_DiffuseRampMapProp, m_DiffuseRampVProp);
}
else
if (material.HasProperty(MPropertyNames.ShadowType))
{
//materialEditor.TexturePropertySingleLine(Styles.Shadow1Map, m_Shadow1MapProp, m_Shadow1ColorProp);
//materialEditor.TexturePropertySingleLine(Styles.Shadow2Map, m_Shadow2MapProp, m_Shadow2ColorProp);
materialEditor.ColorProperty(m_Shadow1ColorProp, "Shadow1Color");
materialEditor.ColorProperty(m_Shadow2ColorProp, "Shadow2Color");
EditorGUI.showMixedValue = m_SurfaceTypeProp.hasMixedValue;
EditorGUI.BeginChangeCheck();
var step1 = EditorGUILayout.Slider(Styles.Shadow1Step, m_Shadow1StepProp.floatValue, 0f, 1f);
var feather1 = EditorGUILayout.Slider(Styles.Shadow1Feather,m_Shadow1FeatherProp.floatValue,0f,1f);
var step2 = EditorGUILayout.Slider(Styles.Shadow2Step, m_Shadow2StepProp.floatValue, 0f, 1f);
var feather2 = EditorGUILayout.Slider(Styles.Shadow2Feather,m_Shadow2FeatherProp.floatValue,0f,1f);
var shadowType = EditorGUILayout.Popup(Styles.ShadowType, (int)m_ShadowTypeProp.floatValue, Enum.GetNames(typeof(ShadowType)));
if (EditorGUI.EndChangeCheck())
{
m_Shadow1StepProp.floatValue = step1;
m_Shadow1FeatherProp.floatValue = feather1;
m_Shadow2StepProp.floatValue = step2;
m_Shadow2FeatherProp.floatValue = feather2;
materialEditor.RegisterPropertyChangeUndo(Styles.ShadowType.text);
m_ShadowTypeProp.floatValue = shadowType;
}
EditorGUI.showMixedValue = false;

if (m_ShadowTypeProp.floatValue == 1.0)
{
//Use Ramp
materialEditor.TexturePropertySingleLine(Styles.DiffuseRampMap, m_DiffuseRampMapProp, m_DiffuseRampVProp);
}
else if(m_ShadowTypeProp.floatValue == 0)
{
//materialEditor.TexturePropertySingleLine(Styles.Shadow1Map, m_Shadow1MapProp, m_Shadow1ColorProp);
//materialEditor.TexturePropertySingleLine(Styles.Shadow2Map, m_Shadow2MapProp, m_Shadow2ColorProp);
materialEditor.ColorProperty(m_Shadow1ColorProp, "Shadow1Color");
materialEditor.ColorProperty(m_Shadow2ColorProp, "Shadow2Color");
EditorGUI.BeginChangeCheck();
var step1 = EditorGUILayout.Slider(Styles.Shadow1Step, m_Shadow1StepProp.floatValue, 0f, 1f);
var feather1 = EditorGUILayout.Slider(Styles.Shadow1Feather, m_Shadow1FeatherProp.floatValue, 0f, 1f);
var step2 = EditorGUILayout.Slider(Styles.Shadow2Step, m_Shadow2StepProp.floatValue, 0f, 1f);
var feather2 = EditorGUILayout.Slider(Styles.Shadow2Feather, m_Shadow2FeatherProp.floatValue, 0f, 1f);
if (EditorGUI.EndChangeCheck())
{
m_Shadow1StepProp.floatValue = step1;
m_Shadow1FeatherProp.floatValue = feather1;
m_Shadow2StepProp.floatValue = step2;
m_Shadow2FeatherProp.floatValue = feather2;
}
}
else
{
//SDF_Face
if (material.HasProperty(MPropertyNames.SDFShadowMap))
{
materialEditor.TexturePropertySingleLine(Styles.SDFShadowMap, m_SDFShadowMapProp);
materialEditor.ColorProperty(m_Shadow1ColorProp, "Shadow1Color");
EditorGUI.BeginChangeCheck();
var step1 = EditorGUILayout.Slider(Styles.Shadow1Step, m_Shadow1StepProp.floatValue, 0f, 1f);
var feather1 = EditorGUILayout.Slider(Styles.Shadow1Feather, m_Shadow1FeatherProp.floatValue, 0f, 1f);
if (EditorGUI.EndChangeCheck())
{
m_Shadow1StepProp.floatValue = step1;
m_Shadow1FeatherProp.floatValue = feather1;
}
}
}
}

Expand Down Expand Up @@ -833,26 +862,30 @@ private void DrawSpecularProperties(MaterialEditor materialEditor)
{
m_SmoothnessProp.floatValue = smooth;
}

// HairSpecular
materialEditor.ShaderProperty(m_EnableHairSpecularProp, Styles.EnableHairSpecular);
if (m_EnableHairSpecularProp.floatValue == 1.0)
if (material.HasProperty(MPropertyNames.EnableHairSpecular))
{
materialEditor.TexturePropertySingleLine(Styles.SpecularShiftMap, m_SpeculatShiftMapProp, m_SpecularShiftIntensityProp);
materialEditor.TextureScaleOffsetProperty(m_SpeculatShiftMapProp);
EditorGUI.BeginChangeCheck();
EditorGUI.indentLevel += 2;
var shift1 = EditorGUILayout.Slider(Styles.SpecularShift, m_SpecularShift1Prop.floatValue, -1f, 1f);
var shift2 = EditorGUILayout.Slider(Styles.SpecularShiftSec, m_SpecularShift2Prop.floatValue, -1f, 1f);
var specular2mul = EditorGUILayout.Slider(Styles.SpecularSecMul, m_Specular2MulProp.floatValue, 0f, 1f);
if (EditorGUI.EndChangeCheck())
materialEditor.ShaderProperty(m_EnableHairSpecularProp, Styles.EnableHairSpecular);
if (m_EnableHairSpecularProp.floatValue == 1.0)
{
m_SpecularShift1Prop.floatValue = shift1;
m_SpecularShift2Prop.floatValue = shift2;
m_Specular2MulProp.floatValue = specular2mul;
materialEditor.TexturePropertySingleLine(Styles.SpecularShiftMap, m_SpeculatShiftMapProp, m_SpecularShiftIntensityProp);
materialEditor.TextureScaleOffsetProperty(m_SpeculatShiftMapProp);
EditorGUI.BeginChangeCheck();
EditorGUI.indentLevel += 2;
var shift1 = EditorGUILayout.Slider(Styles.SpecularShift, m_SpecularShift1Prop.floatValue, -1f, 1f);
var shift2 = EditorGUILayout.Slider(Styles.SpecularShiftSec, m_SpecularShift2Prop.floatValue, -1f, 1f);
var specular2mul = EditorGUILayout.Slider(Styles.SpecularSecMul, m_Specular2MulProp.floatValue, 0f, 1f);
if (EditorGUI.EndChangeCheck())
{
m_SpecularShift1Prop.floatValue = shift1;
m_SpecularShift2Prop.floatValue = shift2;
m_Specular2MulProp.floatValue = specular2mul;
}
EditorGUI.indentLevel -= 2;
}
EditorGUI.indentLevel -= 2;
}

// Highlights
if (material.HasProperty(MPropertyNames.SpecularHighlights))
{
Expand Down
12 changes: 12 additions & 0 deletions Assets/ChiliMilkToonShader/Include/ToonForwardPass.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ struct Varyings
float4 shadowCoord : TEXCOORD7;
#endif

#ifdef _SDFSHADOWMAP
half2 LdotFL : TEXCOORD8;
#endif
float4 positionCS : SV_POSITION;
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
Expand Down Expand Up @@ -82,6 +85,12 @@ void InitializeInputDataToon(Varyings input, half3 normalTS, out InputDataToon i
#if defined(_RECEIVE_HAIRSHADOWMASK) && defined(_HAIRSHADOWMASK)
inputData.depth = input.positionCS.z/input.positionCS.w;
#endif

#ifdef _SDFSHADOWMAP
inputData.LdotFL = input.LdotFL;
inputData.uv = input.uv;
#endif

}

Varyings ToonForwardPassVertex(Attributes input)
Expand Down Expand Up @@ -120,6 +129,9 @@ Varyings ToonForwardPassVertex(Attributes input)
output.shadowCoord = GetShadowCoord(vertexInput);
#endif

#ifdef _SDFSHADOWMAP
output.LdotFL = LightDotObjectFL();
#endif
output.positionCS = vertexInput.positionCS;

return output;
Expand Down
22 changes: 18 additions & 4 deletions Assets/ChiliMilkToonShader/Include/ToonInput.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ TEXTURE2D(_BaseMap); SAMPLER(sampler_BaseMap);
TEXTURE2D(_BumpMap); SAMPLER(sampler_BumpMap);
TEXTURE2D(_EmissionMap); SAMPLER(sampler_EmissionMap);


CBUFFER_START(UnityPerMaterial)
float4 _BaseMap_ST;
half4 _BaseColor;
Expand Down Expand Up @@ -48,6 +47,9 @@ half _RimFeather;

half _OutlineWidth;
half4 _OutlineColor;

half3 _ForwardDirWS;
half3 _LeftDirWS;
CBUFFER_END

TEXTURE2D(_ClipMask);
Expand All @@ -57,7 +59,14 @@ TEXTURE2D(_InShadowMap);
TEXTURE2D(_OcclusionMap); SAMPLER(sampler_OcclusionMap);
TEXTURE2D(_MetallicGlossMap); SAMPLER(sampler_MetallicGlossMap);
TEXTURE2D(_SpecGlossMap); SAMPLER(sampler_SpecGlossMap);

#ifdef _SDFSHADOWMAP
TEXTURE2D(_SDFShadowMap); SAMPLER(sampler_SDFShadowMap);
#endif

#ifdef _HAIRSPECULAR
TEXTURE2D(_SpecularShiftMap); SAMPLER(sampler_SpecularShiftMap);
#endif

#ifdef _DIFFUSERAMPMAP
TEXTURE2D( _DiffuseRampMap); SAMPLER(sampler_LinearClamp);
Expand Down Expand Up @@ -105,11 +114,15 @@ struct InputDataToon
half3 bakedGI;
float2 normalizedScreenSpaceUV;
half4 shadowMask;
#ifdef _HAIRSPECULAR
//#ifdef _HAIRSPECULAR
half3 tangentWS;
half3 bitangentWS;
#endif
//#endif
float depth;
#ifdef _SDFSHADOWMAP
half2 LdotFL;
float2 uv;
#endif
};

half SampleClipMask(float2 uv)
Expand Down Expand Up @@ -171,11 +184,13 @@ half SampleInShadow(float2 uv)
return 1 - inShadow;
}

#ifdef _HAIRSPECULAR
half SampleSpecularShift(float2 uv,half shiftAdd)
{
half specularShift = SAMPLE_TEXTURE2D(_SpecularShiftMap,sampler_SpecularShiftMap,uv*_SpecularShiftMap_ST.xy+_SpecularShiftMap_ST.zw).r*_SpecularShiftIntensity+shiftAdd;
return specularShift;
}
#endif

half4 SampleMetallicSpecGloss(float2 uv, half albedoAlpha,half smoothness)
{
Expand Down Expand Up @@ -235,7 +250,6 @@ inline void InitializeSurfaceDataToon(float2 uv,out SurfaceDataToon outSurfaceDa
outSurfaceData.specularShift1 = SampleSpecularShift(uv,_SpecularShift1);
outSurfaceData.specularShift2 = SampleSpecularShift(uv,_SpecularShift2);
#endif


}
#endif
Loading

0 comments on commit da5f273

Please sign in to comment.