Skip to content

Commit

Permalink
Add Lambert Shading and Optimize subDraw
Browse files Browse the repository at this point in the history
  • Loading branch information
DJ-Huang committed Sep 26, 2022
1 parent fed5c27 commit 8d93498
Show file tree
Hide file tree
Showing 6 changed files with 489 additions and 247 deletions.
5 changes: 3 additions & 2 deletions Assets/Shader/NPR Standard/NPRStandard.shader
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Shader "NPRRenderPipeline/URP/NPRStandard"
[Space()]
[KWEnum(Diffuse, CelShading, _CELLSHADING, RampShading, _RAMPSHADING, PBRShading, _LAMBERTIAN)] _enum ("Shading Mode", float) = 0
[SubToggle(Diffuse)] _UseHalfLambert ("Use HalfLambert (More Flatter)", float) = 0
[Sub(Diffuse_CELLSHADING)] [HDR] _HighColor ("Hight Color", Color) = (1,1,1,1)
[Sub(Diffuse_CELLSHADING)] _DarkColor ("Dark Color", Color) = (0.2,0.2,0.2,1)
[Sub(Diffuse_LAMBERTIAN._CELLSHADING)] [HDR] _HighColor ("Hight Color", Color) = (1,1,1,1)
[Sub(Diffuse_LAMBERTIAN._CELLSHADING)] _DarkColor ("Dark Color", Color) = (0.2,0.2,0.2,1)
[Sub(Diffuse_CELLSHADING)] _CELLThreshold ("Cell Threshold", Range(0.01,1)) = 0.5
[Sub(Diffuse_CELLSHADING)] _CELLSmoothing ("Cell Smoothing", Range(0.001,0.5)) = 0.001

Expand Down Expand Up @@ -57,6 +57,7 @@ Shader "NPRRenderPipeline/URP/NPRStandard"

// -------------------------------------
// Material Keywords
#pragma shader_feature_local _LAMBERTIAN _CELLSHADING _RAMPSHADING

// -------------------------------------
// Universal Pipeline keywords
Expand Down
22 changes: 20 additions & 2 deletions Assets/Shader/NPR Standard/NPRStandardForwardPass.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ void InitializeInputData(Varyings input, half3 normalTS, out InputData inputData
#endif
}

///////////////////////////////////////////////////////////////////////////////
// Shading Function //
///////////////////////////////////////////////////////////////////////////////

half3 LightingDiffuse(half radiance)
{
half3 diffuse = 0;

#if _CELLSHADING
diffuse = StylizedDiffuse(radiance, _CELLThreshold, _CELLSmoothing, _HighColor, _DarkColor);
#elif _LAMBERTIAN
diffuse = lerp(_DarkColor, _HighColor, radiance);
#endif

return diffuse;
}



///////////////////////////////////////////////////////////////////////////////
// Vertex and Fragment functions //
///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -220,8 +239,7 @@ half4 LitPassFragment(Varyings input) : SV_Target
half radiance = LightingRadiance(mainLight.direction, inputData.normalWS, _UseHalfLambert);

half3 diffuse = 0;

StylizedDiffuse(radiance, _CELLThreshold, _CELLSmoothing, _HighColor, _DarkColor, diffuse);
diffuse = LightingDiffuse(radiance);

half4 color = 0;
color.rgb = diffuse;
Expand Down
8 changes: 3 additions & 5 deletions Assets/Shader/NPR Standard/Universal Render Pipeline_Lit.mat
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ Material:
m_PrefabAsset: {fileID: 0}
m_Name: Universal Render Pipeline_Lit
m_Shader: {fileID: 4800000, guid: 335dcad6d9fdba943a91b0a9295ee10e, type: 3}
m_ValidKeywords: []
m_ValidKeywords:
- _LAMBERTIAN
m_InvalidKeywords:
- _CELLSHADING
- _KEY1
- _LAMBERTIAN
- _RAMPSHADING
- _USEHALFLAMBERT_ON
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
Expand Down Expand Up @@ -116,7 +114,7 @@ Material:
- _Surface: 0
- _UseHalfLambert: 0
- _ZWrite: 1
- _enum: 0
- _enum: 2
- _group: 0
- _group1: 1
m_Colors:
Expand Down
4 changes: 3 additions & 1 deletion Assets/Shader/ShaderLibrary/NPRLighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ half LightingRadiance(half3 lightDir, half3 normal, half useHalfLambert)
* \param shadowSmooth
* \param diffuse [Out]
*/
inline void StylizedDiffuse(inout half radiance, half cellhreshold, half cellSmooth, half3 highColor, half3 darkColor, out half3 diffuse)
inline half3 StylizedDiffuse(inout half radiance, half cellhreshold, half cellSmooth, half3 highColor, half3 darkColor)
{
half3 diffuse = 0;
cellSmooth *= 0.5;
radiance = saturate(1 + (radiance - cellhreshold - cellSmooth) / max(cellSmooth, 1e-3));
diffuse = lerp(darkColor.rgb, highColor.rgb, radiance);
return diffuse;
}

half3 VertexLighting(float3 positionWS, half3 normalWS)
Expand Down
15 changes: 12 additions & 3 deletions Packages/[email protected]/Editor/DrawerUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,21 @@ public static bool IsVisible(string group)
// prefix = group name, suffix = keyWord
if (group.Contains(prefix))
{
// Determine if the current keyword has been used
string suffix = group.Substring(prefix.Length, group.Length - prefix.Length).ToUpperInvariant();
if (GUIData.keyWord.ContainsKey(suffix))
string[] suffixSplit = suffix.Split('.');
bool isKeywordActive = false;
foreach (var suf in suffixSplit)
{
// visible when keyword is activated and group is not folding
return GUIData.keyWord[suffix] && !GUIData.group[prefix];
if (GUIData.keyWord.ContainsKey(suf))
{
isKeywordActive = GUIData.keyWord[suf];
if(isKeywordActive) break;
//return GUIData.keyWord[suf] && !GUIData.group[prefix];
}
}
// visible when keyword is activated and group is not folding
return isKeywordActive && !GUIData.group[prefix];
}
}
return false;
Expand Down
Loading

0 comments on commit 8d93498

Please sign in to comment.