From e8d1d5923a7360d0b80090e5acac36ac18798e10 Mon Sep 17 00:00:00 2001 From: Misaki Date: Fri, 31 Jan 2025 17:25:19 +0900 Subject: [PATCH] Optmize shader structure --- Editor/MeterialEditor/ShaderGUIExpandable.cs | 4 +- .../MeterialEditor/UIScopes/AngelRingScope.cs | 2 +- .../MeterialEditor/UIScopes/OutlineScope.cs | 2 +- .../UIScopes/ShadingColorScope.cs | 150 ++++++++++++++++++ .../UIScopes/ShadingColorScope.cs.meta | 2 + .../ShadingColorSettingsScope.cs.meta | 3 - .../UIScopes/ShadingGradeScope.cs | 23 +++ .../UIScopes/ShadingGradeScope.cs.meta | 2 + .../UIScopes/SurfaceOptionsScope.cs | 11 +- Editor/MeterialEditor/UTSShaderGUI.cs | 2 +- Runtime/Models/ShadingMode.cs | 9 ++ Runtime/Models/ShadingMode.cs.meta | 2 + Runtime/Shaders/HDRPToon.shader | 56 +++---- Runtime/Shaders/Includes/Common/UtsHead.hlsl | 19 ++- .../Common/UtsMaterialEvaluation.hlsl | 31 ++-- .../Properties/UtsHdrpProperties.hlsl | 22 --- .../Includes/Properties/UtsTextures.hlsl | 10 +- .../Properties/UtsUnityPerMaterial.hlsl | 30 ++-- .../UTSToonmapping/UTSTonemapping.cs | 2 +- 19 files changed, 287 insertions(+), 95 deletions(-) create mode 100644 Editor/MeterialEditor/UIScopes/ShadingColorScope.cs create mode 100644 Editor/MeterialEditor/UIScopes/ShadingColorScope.cs.meta delete mode 100644 Editor/MeterialEditor/UIScopes/ShadingColorSettingsScope.cs.meta create mode 100644 Editor/MeterialEditor/UIScopes/ShadingGradeScope.cs create mode 100644 Editor/MeterialEditor/UIScopes/ShadingGradeScope.cs.meta create mode 100644 Runtime/Models/ShadingMode.cs create mode 100644 Runtime/Models/ShadingMode.cs.meta diff --git a/Editor/MeterialEditor/ShaderGUIExpandable.cs b/Editor/MeterialEditor/ShaderGUIExpandable.cs index abadb39..c12147c 100644 --- a/Editor/MeterialEditor/ShaderGUIExpandable.cs +++ b/Editor/MeterialEditor/ShaderGUIExpandable.cs @@ -3,8 +3,8 @@ namespace Misaki.HdrpToon.Editor public enum ShaderGUIExpandable : uint { SurfaceOptions = 1 << 0, - Basic = 1 << 1, - ShadingStepAndFeather = 1 << 2, + ShadingColor = 1 << 1, + ShadingGrade = 1 << 2, MaterialFeature = 1 << 3, PBR = 1 << 4, AmbientMode = 1 << 5, diff --git a/Editor/MeterialEditor/UIScopes/AngelRingScope.cs b/Editor/MeterialEditor/UIScopes/AngelRingScope.cs index cf9ad26..8a50851 100644 --- a/Editor/MeterialEditor/UIScopes/AngelRingScope.cs +++ b/Editor/MeterialEditor/UIScopes/AngelRingScope.cs @@ -23,7 +23,7 @@ namespace Misaki.HdrpToon.Editor public static readonly GUIContent angelRingOffsetVText = new("Angel Ring Offset V", "Specifies the offset of the angel ring in the V direction."); } - protected override bool ShowSection => owner.GetUIScope().HasFeature(SurfaceFeatureFlags.AngelRing); + protected override bool ShowSection => SurfaceOptionsScope.HasFeature(SurfaceFeatureFlags.AngelRing); protected override ShaderGUIExpandable ExpandableBit => ShaderGUIExpandable.AngelRing; diff --git a/Editor/MeterialEditor/UIScopes/OutlineScope.cs b/Editor/MeterialEditor/UIScopes/OutlineScope.cs index 121dcd0..c546722 100644 --- a/Editor/MeterialEditor/UIScopes/OutlineScope.cs +++ b/Editor/MeterialEditor/UIScopes/OutlineScope.cs @@ -31,7 +31,7 @@ namespace Misaki.HdrpToon.Editor public static readonly GUIContent useSmoothedNormalText = new("Use Smoothed Normal", "Enable to use smoothed normal(that packed in uv2) for outline calculation."); } - protected override bool ShowSection => owner.GetUIScope().HasFeature(SurfaceFeatureFlags.Outline); + protected override bool ShowSection => SurfaceOptionsScope.HasFeature(SurfaceFeatureFlags.Outline); protected override ShaderGUIExpandable ExpandableBit => ShaderGUIExpandable.Outline; diff --git a/Editor/MeterialEditor/UIScopes/ShadingColorScope.cs b/Editor/MeterialEditor/UIScopes/ShadingColorScope.cs new file mode 100644 index 0000000..59ee659 --- /dev/null +++ b/Editor/MeterialEditor/UIScopes/ShadingColorScope.cs @@ -0,0 +1,150 @@ +using Misaki.ShaderGUI; +using UnityEditor; +using UnityEngine; + +namespace Misaki.HdrpToon.Editor +{ + public class ShadingColorScope : MaterialUIScope + { + private static class Properties + { + public static MaterialProperty baseColor; + public static MaterialProperty baseColorMap; + public static MaterialProperty applyTo1stShadingMapEnable; + public static MaterialProperty firstShadingColor; + public static MaterialProperty firstShadingMap; + public static MaterialProperty applyTo2ndShadingMapEnable; + public static MaterialProperty secondShadingColor; + public static MaterialProperty secondShadingMap; + + public static MaterialProperty sdfShadowMap; + public static MaterialProperty sdfShadowLevel; + public static MaterialProperty sdfSmoothLevel; + public static MaterialProperty sdfHighlightStrength; + public static MaterialProperty sdfHighlightSmoothLevel; + + public static MaterialProperty shadingGradeMap; + public static MaterialProperty shadingIndex; + } + + private static class Styles + { + public static readonly GUIContent baseMapText = new("Base Map", "Base Color : Texture(sRGB) x Color(RGB) Default:White"); + public static readonly GUIContent applyTo1stShadingMapText = new("Apply to 1st shading map", "Apply Base map to the 1st shading map."); + public static readonly GUIContent firstShadingMapText = new("1st Shading Map", "1st shading map."); + public static readonly GUIContent firstShadeColorText = new("1st Shading Color", "1st shading color."); + + public static readonly GUIContent applyTo2ndShadingMapText = new("Apply to 2nd shading map", "Apply Base map or the 1st shading map to the 2st shading map."); + public static readonly GUIContent secondShadingMapText = new("2nd Shading Map", "2nd shading map."); + public static readonly GUIContent secondShadeColorText = new("2nd Shading Color", "2nd shading color."); + + public static readonly GUIContent sdfShadowMapText = new("SDF Shadow Map", "SDF Shadow Map"); + public static readonly GUIContent sdfShadowLevelText = new("SDF Shadow Level", "SDF Shadow Level"); + public static readonly GUIContent sdfSmoothLevelText = new("SDF Smooth Level", "SDF Smooth Level"); + public static readonly GUIContent sdfHighlightStrengthText = new("SDF Highlight Strength", "SDF Highlight Strength"); + public static readonly GUIContent sdfHighlightSmoothLevelText = new("SDF Highlight Smooth Level", "SDF Highlight Smooth Level"); + + public static readonly GUIContent shadingGradeMapText = new("Shading Grade Map", "Shading Grade Map"); + public static readonly GUIContent shadingIndexText = new("Shading Index", "The index to choose when sampling the texture 2d array."); + } + + protected override ShaderGUIExpandable ExpandableBit => ShaderGUIExpandable.ShadingColor; + protected override GUIContent Header => EditorGUIUtility.TrTextContent("Shading Color Settings"); + + private void DrawSecondShadingMapProperties() + { + EditorGUI.indentLevel += 2; + editor.ShaderProperty(Properties.applyTo2ndShadingMapEnable, Styles.applyTo2ndShadingMapText); + EditorGUI.indentLevel -= 2; + if (Properties.applyTo2ndShadingMapEnable.floatValue == 1) + { + EditorGUI.indentLevel += 2; + editor.ColorProperty(Properties.secondShadingColor, Styles.secondShadeColorText.text); + EditorGUI.indentLevel -= 2; + } + else + { + editor.TexturePropertySingleLine(Styles.secondShadingMapText, Properties.secondShadingMap, + Properties.secondShadingColor); + } + } + + private void DrawFirstShadingMapProperties() + { + EditorGUI.indentLevel += 2; + editor.ShaderProperty(Properties.applyTo1stShadingMapEnable, Styles.applyTo1stShadingMapText); + EditorGUI.indentLevel -= 2; + if (Properties.applyTo1stShadingMapEnable.floatValue == 1) + { + EditorGUI.indentLevel += 2; + editor.ColorProperty(Properties.firstShadingColor, Styles.firstShadeColorText.text); + EditorGUI.indentLevel -= 2; + } + else + { + editor.TexturePropertySingleLine(Styles.firstShadingMapText, Properties.firstShadingMap, + Properties.firstShadingColor); + } + } + + private void DrawSDFProperties() + { + editor.TexturePropertySingleLine(Styles.sdfShadowMapText, Properties.sdfShadowMap); + editor.ShaderProperty(Properties.sdfShadowLevel, Styles.sdfShadowLevelText); + editor.ShaderProperty(Properties.sdfSmoothLevel, Styles.sdfSmoothLevelText); + editor.ShaderProperty(Properties.sdfHighlightStrength, Styles.sdfHighlightStrengthText); + editor.ShaderProperty(Properties.sdfHighlightSmoothLevel, Styles.sdfHighlightSmoothLevelText); + } + + private void DrawShadingGradeProperties() + { + editor.TexturePropertySingleLine(Styles.shadingGradeMapText, Properties.shadingGradeMap); + editor.ShaderProperty(Properties.shadingIndex, Styles.shadingIndexText); + } + + public override void LoadMaterialProperties() + { + Properties.baseColor = FindProperty("_BaseColor"); + Properties.baseColorMap = FindProperty("_BaseColorMap"); + Properties.applyTo1stShadingMapEnable = FindProperty("_UseBaseAs1st"); + Properties.firstShadingColor = FindProperty("_1stShadeColor"); + Properties.firstShadingMap = FindProperty("_1stShadeColorMap"); + Properties.applyTo2ndShadingMapEnable = FindProperty("_Use1stAs2nd"); + Properties.secondShadingColor = FindProperty("_2ndShadeColor"); + Properties.secondShadingMap = FindProperty("_2ndShadeColorMap"); + + Properties.sdfShadowMap = FindProperty("_SDFShadowMap"); + Properties.sdfShadowLevel = FindProperty("_SDFShadowLevel"); + Properties.sdfSmoothLevel = FindProperty("_SDFSmoothLevel"); + Properties.sdfHighlightStrength = FindProperty("_SDFHighlightStrength"); + Properties.sdfHighlightSmoothLevel = FindProperty("_SDFHighlightSmoothLevel"); + + Properties.shadingGradeMap = FindProperty("_ShadingGradeMap"); + Properties.shadingIndex = FindProperty("_ShadingIndex"); + } + + protected override void DrawContent() + { + editor.TexturePropertySingleLine(Styles.baseMapText, Properties.baseColorMap, Properties.baseColor); + + switch (SurfaceOptionsScope.GetShadingMode()) + { + case ShadingMode.ThreeColorStep: + DrawFirstShadingMapProperties(); + DrawSecondShadingMapProperties(); + break; + case ShadingMode.SDF: + DrawSDFProperties(); + break; + case ShadingMode.Ramp: + DrawShadingGradeProperties(); + break; + default: + break; + } + + EditorGUILayout.Space(); + editor.TextureScaleOffsetProperty(Properties.baseColorMap); + } + } +} \ No newline at end of file diff --git a/Editor/MeterialEditor/UIScopes/ShadingColorScope.cs.meta b/Editor/MeterialEditor/UIScopes/ShadingColorScope.cs.meta new file mode 100644 index 0000000..acf6165 --- /dev/null +++ b/Editor/MeterialEditor/UIScopes/ShadingColorScope.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 35d7c7fd9279ea64199f1aa61c24fbcd \ No newline at end of file diff --git a/Editor/MeterialEditor/UIScopes/ShadingColorSettingsScope.cs.meta b/Editor/MeterialEditor/UIScopes/ShadingColorSettingsScope.cs.meta deleted file mode 100644 index 4d5bb55..0000000 --- a/Editor/MeterialEditor/UIScopes/ShadingColorSettingsScope.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 4434ff1451f044df8fdfbaddeb4ee13b -timeCreated: 1737690087 \ No newline at end of file diff --git a/Editor/MeterialEditor/UIScopes/ShadingGradeScope.cs b/Editor/MeterialEditor/UIScopes/ShadingGradeScope.cs new file mode 100644 index 0000000..7489b12 --- /dev/null +++ b/Editor/MeterialEditor/UIScopes/ShadingGradeScope.cs @@ -0,0 +1,23 @@ +using Misaki.ShaderGUI; +using UnityEditor; +using UnityEngine; + +namespace Misaki.HdrpToon.Editor +{ + public class ShadingGradeScope : MaterialUIScope + { + protected override ShaderGUIExpandable ExpandableBit => ShaderGUIExpandable.ShadingGrade; + + protected override GUIContent Header => EditorGUIUtility.TrTextContent("Shading Grade Settings"); + + public override void LoadMaterialProperties() + { + throw new System.NotImplementedException(); + } + + protected override void DrawContent() + { + throw new System.NotImplementedException(); + } + } +} diff --git a/Editor/MeterialEditor/UIScopes/ShadingGradeScope.cs.meta b/Editor/MeterialEditor/UIScopes/ShadingGradeScope.cs.meta new file mode 100644 index 0000000..f5f976e --- /dev/null +++ b/Editor/MeterialEditor/UIScopes/ShadingGradeScope.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: faf2eabd1c2767d40a2ecfe89cb56d28 \ No newline at end of file diff --git a/Editor/MeterialEditor/UIScopes/SurfaceOptionsScope.cs b/Editor/MeterialEditor/UIScopes/SurfaceOptionsScope.cs index 40c4cef..5b94529 100644 --- a/Editor/MeterialEditor/UIScopes/SurfaceOptionsScope.cs +++ b/Editor/MeterialEditor/UIScopes/SurfaceOptionsScope.cs @@ -14,6 +14,7 @@ namespace Misaki.HdrpToon.Editor public static MaterialProperty alphaClip; public static MaterialProperty cullMode; + public static MaterialProperty shadingMode; public static MaterialProperty materialType; public static MaterialProperty pbrMode; @@ -30,6 +31,7 @@ namespace Misaki.HdrpToon.Editor public static readonly GUIContent alphaClipText = new("Alpha Clipping Threshold", "Threshold for alpha clipping."); public static readonly GUIContent cullingModeText = new("Culling Mode", "Controls the sides of polygons that should not be drawn (culled)."); + public static readonly GUIContent shadingModeText = new("Shading Mode", "Specifies the shading grade mode."); public static readonly GUIContent materialTypeText = new("Material Type", "Specifies the material type."); public static readonly GUIContent pbrModeText = new("PBR Mode", "Specifies PBR model mode."); @@ -42,7 +44,12 @@ namespace Misaki.HdrpToon.Editor protected override GUIContent Header => EditorGUIUtility.TrTextContent("Surface Options"); - public bool HasFeature(SurfaceFeatureFlags feature) + public static ShadingMode GetShadingMode() + { + return (ShadingMode)Properties.shadingMode.floatValue; + } + + public static bool HasFeature(SurfaceFeatureFlags feature) { return ((SurfaceFeatureFlags)Properties.surfaceFeatures.floatValue & feature) != 0; } @@ -55,6 +62,7 @@ namespace Misaki.HdrpToon.Editor Properties.alphaClip = FindProperty("_AlphaCutoff"); Properties.cullMode = FindProperty("_CullMode"); + Properties.shadingMode = FindProperty("_Shading_Mode"); Properties.materialType = FindProperty("_Material_Type"); Properties.pbrMode = FindProperty("_PBR_Mode"); @@ -76,6 +84,7 @@ namespace Misaki.HdrpToon.Editor } editor.ShaderProperty(Properties.cullMode, Styles.cullingModeText); + editor.ShaderProperty(Properties.shadingMode, Styles.shadingModeText); editor.ShaderProperty(Properties.materialType, Styles.materialTypeText); editor.ShaderProperty(Properties.pbrMode, Styles.pbrModeText); diff --git a/Editor/MeterialEditor/UTSShaderGUI.cs b/Editor/MeterialEditor/UTSShaderGUI.cs index 9c66320..045bf74 100644 --- a/Editor/MeterialEditor/UTSShaderGUI.cs +++ b/Editor/MeterialEditor/UTSShaderGUI.cs @@ -25,7 +25,7 @@ namespace Misaki.HdrpToon.Editor private void OnInitialize(MaterialEditor materialEditor, MaterialProperty[] properties) { AddUIScope(new SurfaceOptionsScope()); - AddUIScope(new ShadingColorSettingsScope()); + AddUIScope(new ShadingColorScope()); AddUIScope(new AngelRingScope()); AddUIScope(new OutlineScope()); diff --git a/Runtime/Models/ShadingMode.cs b/Runtime/Models/ShadingMode.cs new file mode 100644 index 0000000..098db51 --- /dev/null +++ b/Runtime/Models/ShadingMode.cs @@ -0,0 +1,9 @@ +namespace Misaki.HdrpToon +{ + internal enum ShadingMode + { + ThreeColorStep, + SDF, + Ramp + } +} \ No newline at end of file diff --git a/Runtime/Models/ShadingMode.cs.meta b/Runtime/Models/ShadingMode.cs.meta new file mode 100644 index 0000000..c3dc778 --- /dev/null +++ b/Runtime/Models/ShadingMode.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: b5065e386905cb248bdf1ded804260b4 \ No newline at end of file diff --git a/Runtime/Shaders/HDRPToon.shader b/Runtime/Shaders/HDRPToon.shader index 1d27375..f906565 100644 --- a/Runtime/Shaders/HDRPToon.shader +++ b/Runtime/Shaders/HDRPToon.shader @@ -16,12 +16,6 @@ Shader "HDRP/Toon" [HideInInspector] _utsVersionY("VersionY", Float) = 7 [HideInInspector] _utsVersionZ("VersionZ", Float) = 6 - - // Reminder. Color here are in linear but the UI (color picker) do the conversion sRGB to linear - _BaseColor("BaseColor", Color) = (1,1,1,1) - _BaseColorMap("BaseColorMap", 2D) = "white" {} - [HideInInspector] _BaseColorMap_MipInfo("_BaseColorMap_MipInfo", Vector) = (0, 0, 0, 0) - _HairBlendingMap("HairBlendingMap", 2D) = "black" {} _Metallic("_Metallic", Range(0.0, 1.0)) = 0 @@ -267,17 +261,37 @@ Shader "HDRP/Toon" // ----------------------------------------------------------------------- // Surface Options - [Enum(Off, 0, On, 1)] _TransparentEnabled("Transparent Mode", int) = 0 [ToggleUI] _AlphaCutoffEnable("Alpha Cutoff Enable", Float) = 0.0 _AlphaCutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5 [Enum(Off, 0, FRONT, 1, BACK, 2)] _CullMode("Cull Mode", int) = 2 //OFF/FRONT/BACK + [KeywordEnum(ThreeColorStep, SDF, Ramp)] _Shading_Mode("Shade Grade mode", Float) = 0 [KeywordEnum(Standard, FrontHair, Face, Eye)] _Material_Type("Material Type", int) = 0 - [KeywordEnum(Off, Standard, Anisotropy, Hair, Toon)] _PBR_Mode("PBR MODE", int) = 0 + [KeywordEnum(Off, Standard, Anisotropy, Hair, Toon)] _PBR_Mode("PBR Mode", int) = 0 [Toggle] _Receive_Hair_Shadow("Receive HairS hadow", Float) = 0 [ToggleUI] _HairBlendingTarget("Hair Blending Target", Float) = 0 _SurfaceFeatures("Surface Features", Float) = 0 + // Shading Color + _BaseColor("BaseColor", Color) = (1,1,1,1) + _BaseColorMap("BaseColorMap", 2D) = "white" {} + [HideInInspector] _BaseColorMap_MipInfo("_BaseColorMap_MipInfo", Vector) = (0, 0, 0, 0) + _1stShadeColor("1st_ShadeColor", Color) = (0, 0, 0, 0) + _1stShadeColorMap("1st_ShadeMap", 2D) = "white" {} + [ToggleUI] _UseBaseAs1st("Use BaseMap as 1st_ShadeMap", Float) = 1 + _2ndShadeColor("2nd_ShadeColor", Color) = (0, 0, 0, 0) + _2ndShadeColorMap("2nd_ShadeMap", 2D) = "white" {} + [ToggleUI] _Use1stAs2nd("Use 1st_ShadeMap as 2nd_ShadeMap", Float) = 1 + + _SDFShadowMap("SDFShadowMap", 2D) = "white" {} + _SDFShadowLevel("SDFShadowLevel", Range(0.0, 1.0)) = 0.25 + _SDFSmoothLevel("SDFSmoothGamma", Range(0.0, 0.1)) = 0.02 + _SDFHighlightStrength("SDFHighlightStrength", Range(0.0, 5.0)) = 1.25 + _SDFHighlightSmoothLevel("SDFHighlightSmoothLevel", Range(0.0, 0.1)) = 0.02 + + _ShadingGradeMap("ShadingGradeMap", 2DArray) = "white" {} + _ShadingIndex("ShadingIndex", Int) = 0 + // Angel Rings _AngelRingColor("Angel Ring Color", Color) = (1, 1, 1, 1) _AngelRingColorMap("Angel Ring Color Map", 2D) = "black" {} @@ -335,28 +349,10 @@ Shader "HDRP/Toon" //v.2.0.5 : Clipping/TransClipping for SSAO Problems in PostProcessing Stack. //If you want to go back the former SSAO results, comment out the below line. [HideInInspector] _Color("Color", Color) = (1, 1, 1, 1) - // - [Toggle(_)] _Is_LightColor_Base("Is_LightColor_Base", Float) = 1 - _1st_ShadeMap("1st_ShadeMap", 2D) = "white" {} - //v.2.0.5 - [Toggle(_)] _Use_BaseAs1st("Use BaseMap as 1st_ShadeMap", Float) = 0 - _1st_ShadeColor("1st_ShadeColor", Color) = (1, 1, 1, 1) - [Toggle(_)] _Is_LightColor_1st_Shade("Is_LightColor_1st_Shade", Float) = 1 - _2nd_ShadeMap("2nd_ShadeMap", 2D) = "white" {} - //v.2.0.5 - [Toggle(_)] _Use_1stAs2nd("Use 1st_ShadeMap as 2nd_ShadeMap", Float) = 0 - _2nd_ShadeColor("2nd_ShadeColor", Color) = (1, 1, 1, 1) - [Toggle(_)] _Is_LightColor_2nd_Shade("Is_LightColor_2nd_Shade", Float) = 1 -// _NormalMap("NormalMap", 2D) = "bump" {} + + // _NormalMap("NormalMap", 2D) = "bump" {} _BumpScale("Normal Scale", Range(0, 1)) = 1 [Toggle(_)] _Is_NormalMapToBase("Is_NormalMapToBase", Float) = 0 - //v.2.0.4.4 - [KeywordEnum(NORMAL, SDF)] _Shadow_Mode("Shadow Mode", Float) = 0 - _SDFShadowTex("SDFShadowTex", 2D) = "white" {} - _SDFSmoothGamma("SDFSmoothGamma", Range(0.0, 0.1)) = 0.02 - _SDFShadowLevel("SDFShadowLevel", Range(0.0, 1.0)) = 0.25 - _SDFNoseHighlightCoef("SDFNoseHighlightCoef", Range(0.0, 5.0)) = 1.25 - _SDFNoseHighlightSmoothRange("SDFNoseHighlightSmoothRange", Range(0.0, 0.1)) = 0.02 // Hair Shadow [Toggle(_)] _Cast_Hair_Shadow("CastHairShadow", Float) = 0 @@ -388,7 +384,6 @@ Shader "HDRP/Toon" // _Set_1st_ShadePosition("Set_1st_ShadePosition", 2D) = "white" {} _Set_2nd_ShadePosition("Set_2nd_ShadePosition", 2D) = "white" {} - _ShadingGradeMap("ShadingGradeMap", 2D) = "white" {} //v.2.0.6 _Tweak_ShadingGradeMapLevel("Tweak_ShadingGradeMapLevel", Range(-0.5, 0.5)) = 0 _BlurLevelSGM("Blur Level of ShadingGradeMap", Range(0, 10)) = 0 @@ -996,8 +991,7 @@ Shader "HDRP/Toon" #pragma shader_feature ENABLE_UTS_HAIR_SHAOW #pragma shader_feature ENABLE_UTS_HAIR_BLENDING - #pragma shader_feature_local_fragment _SHADOW_MODE_NORMAL _SHADOW_MODE_SDF - + #pragma shader_feature_local_fragment _SHADING_MODE_THREECOLORSTEP _SHADING_MODE_SDF _SHADING_MODE_RAMP #pragma shader_feature_local_fragment _MATERIAL_TYPE_STANDARD _MATERIAL_TYPE_FRONTHAIR _MATERIAL_TYPE_FACE _MATERIAL_TYPE_EYE #pragma shader_feature_local_fragment _PBR_MODE_OFF _PBR_MODE_STANDARD _PBR_MODE_ANISOTROPY _PBR_MODE_HAIR _PBR_MODE_TOON diff --git a/Runtime/Shaders/Includes/Common/UtsHead.hlsl b/Runtime/Shaders/Includes/Common/UtsHead.hlsl index d4fceb6..3a2f3b9 100644 --- a/Runtime/Shaders/Includes/Common/UtsHead.hlsl +++ b/Runtime/Shaders/Includes/Common/UtsHead.hlsl @@ -107,8 +107,8 @@ UTSSurfaceData ConvertSurfaceDataToUTSSurfaceData(SurfaceData surfaceData) UTSSurfaceData GetUTSSurfaceData(FragInputs input, float3 V) { + // Not zero initialized to make sure all fields are set. UTSSurfaceData output; - //ZERO_INITIALIZE(UTSSurfaceData, output); output.surfaceFeatures = _SurfaceFeatures; @@ -116,10 +116,19 @@ UTSSurfaceData GetUTSSurfaceData(FragInputs input, float3 V) output.baseColor = mainTexture.rgb * _BaseColor.rgb; output.alpha = mainTexture.a; - float4 firstShadingTexture = SAMPLE_TEXTURE2D(_1st_ShadeMap, sampler_BaseColorMap, TRANSFORM_TEX(input.texCoord0, _BaseColorMap)); - float4 secondShadingTexture = SAMPLE_TEXTURE2D(_1st_ShadeMap, sampler_BaseColorMap, TRANSFORM_TEX(input.texCoord0, _BaseColorMap)); - output.firstShadingColor = lerp(firstShadingTexture.rgb, mainTexture.rgb, _Use_BaseAs1st) * _1st_ShadeColor.rgb; - output.secondShadingColor = lerp(secondShadingTexture.rgb, output.firstShadingColor, _Use_1stAs2nd) * _2nd_ShadeColor.rgb; +#if _SHADING_MODE_THREECOLORSTEP || _SHADING_MODE_SDF + float4 firstShadingTexture = SAMPLE_TEXTURE2D(_1stShadeColorMap, sampler_BaseColorMap, TRANSFORM_TEX(input.texCoord0, _BaseColorMap)); + output.firstShadingColor = lerp(firstShadingTexture.rgb, mainTexture.rgb, _UseBaseAs1st) * _1stShadeColor.rgb; +#if _SHADING_MODE_THREECOLORSTEP + float4 secondShadingTexture = SAMPLE_TEXTURE2D(_2ndShadeColorMap, sampler_BaseColorMap, TRANSFORM_TEX(input.texCoord0, _BaseColorMap)); + output.secondShadingColor = lerp(secondShadingTexture.rgb, output.firstShadingColor, _Use1stAs2nd) * _2ndShadeColor.rgb; +#else + output.secondShadingColor = 0.0; +#endif +#elif _SHADING_MODE_RAMP + output.firstShadingColor = 0.0; + output.secondShadingColor = 0.0; +#endif float4 normalLocal = float4(0, 0, 1.0, 1.0); #if _NORMAL_MAP diff --git a/Runtime/Shaders/Includes/Common/UtsMaterialEvaluation.hlsl b/Runtime/Shaders/Includes/Common/UtsMaterialEvaluation.hlsl index be2d6ed..37954ea 100644 --- a/Runtime/Shaders/Includes/Common/UtsMaterialEvaluation.hlsl +++ b/Runtime/Shaders/Includes/Common/UtsMaterialEvaluation.hlsl @@ -114,11 +114,11 @@ half3 FitWithCurveApprox(half NdotL, half Curvature) float3 SampleSDFTexture(float3 L, float2 uv, out float angle) { float2 right_uv = float2(1 - uv.x, uv.y); - float3 left_SDFTex = SAMPLE_TEXTURE2D(_SDFShadowTex, sampler_SDFShadowTex, uv).rgb; - float3 right_SDFTex = SAMPLE_TEXTURE2D(_SDFShadowTex, sampler_SDFShadowTex, right_uv).rgb; + float3 left_SDFTex = SAMPLE_TEXTURE2D(_SDFShadowMap, sampler_SDFShadowMap, uv).rgb; + float3 right_SDFTex = SAMPLE_TEXTURE2D(_SDFShadowMap, sampler_SDFShadowMap, right_uv).rgb; - float2 leftVector = normalize(mul(UNITY_MATRIX_M, float4(1, 0, 0, 0)).xz); - float2 forwardVector = normalize(mul(UNITY_MATRIX_M, float4(0, 0, 1, 0)).xz); + float2 leftVector = normalize(mul(UNITY_MATRIX_M, float4(1.0, 0.0, 0.0, 0.0))); + float2 forwardVector = normalize(mul(UNITY_MATRIX_M, float4(0.0, 0.0, 1.0, 0.0))); float2 lightDirection = normalize(L.xz); angle = 1.0 - (dot(forwardVector, lightDirection) * 0.5 + 0.5); @@ -162,7 +162,7 @@ UtsShadeMask GetShadeMak(PositionInputs posInput, float3 normalWS, float3 L, SHA { UtsShadeMask shadeMask; -#if _SHADOW_MODE_NORMAL +#if _SHADING_MODE_THREECOLORSTEP float NdotL = dot(normalWS, L); float halfLambert = 0.5 * NdotL + 0.5; @@ -176,18 +176,18 @@ UtsShadeMask GetShadeMak(PositionInputs posInput, float3 normalWS, float3 L, SHA shadeMask.firstShadeMask = saturate((halfLambert - (_2nd_ShadeColor_Step - secondColorFeatherForMask)) / (_2nd_ShadeColor_Step - (_2nd_ShadeColor_Step - secondColorFeatherForMask))); additionalSpecular = 0; -#elif _SHADOW_MODE_SDF +#elif _SHADING_MODE_SDF float angle; - float smoothGamma = _SDFSmoothGamma / 10.0f; + float smoothLevel = _SDFSmoothLevel / 10.0f; float shadowLevel = _SDFShadowLevel / 10.0f; float3 sdfTexture = SampleSDFTexture(L, uv, angle); // r: sdf shadow, g: sdf noise highlight, b: fixed shadow - float sdfShadowMask = 1.0 - smoothstep(sdfTexture.r - smoothGamma, sdfTexture.r + smoothGamma, angle - shadowLevel); + float sdfShadowMask = smoothstep(sdfTexture.r - smoothLevel, sdfTexture.r + smoothLevel, angle - shadowLevel); - shadeMask.baseShadeMask = sdfShadowMask * sdfTexture.b; + shadeMask.baseShadeMask = sdfShadowMask; shadeMask.firstShadeMask = 1.0; - additionalSpecular = sdfTexture.g; + additionalSpecular = smoothstep(sdfTexture.g - _SDFHighlightSmoothLevel, sdfTexture.g + _SDFHighlightSmoothLevel, angle - shadowLevel) * _SDFHighlightStrength; #endif shadow = smoothstep(0.4, 0.6, shadow); @@ -209,11 +209,20 @@ DirectLighting UtsShadeSurface(PositionInputs posInput, UtsBSDFData bsdfData, Pr if (Max3(lightColor.r, lightColor.g, lightColor.b) > 0.0) { + #if _SHADING_MODE_RAMP + float NdotL = dot(bsdfData.normalWS, L); + float halfLambert = 0.5 * NdotL + 0.5; + + float3 rampColor = SAMPLE_TEXTURE2D_ARRAY_LOD(_ShadingGradeMap, s_linear_clamp_sampler, float2(halfLambert, 0.0), _ShadingIndex, 0.0).rgb; + float3 diffuseTerm = bsdfData.diffuseColor * rampColor; + float3 specularTerm = ComputeSpecularTerm(bsdfData, preLightData, V, L) * saturate(NdotL); + #else float additionalSpecular; UtsShadeMask shadeMask = GetShadeMak(posInput, bsdfData.normalWS, L, shadow, uv, additionalSpecular); float3 diffuseTerm = lerp(lerp(bsdfData.secondShadingDiffuseColor, bsdfData.firstShadingDiffuseColor, shadeMask.firstShadeMask), bsdfData.diffuseColor, shadeMask.baseShadeMask); float3 specularTerm = (ComputeSpecularTerm(bsdfData, preLightData, V, L) + additionalSpecular) * shadeMask.baseShadeMask; + #endif lighting.diffuse += diffuseTerm * lightColor * diffuseDimmer; lighting.specular += specularTerm * lightColor * specularDimmer; @@ -313,7 +322,7 @@ DirectLighting UtsEvaluateAngelRing(FragInputs input, float3 normalWS, float3 V) float uvMask=lerp(cutU, 1 - cutU, rightside);//discard half of the sdf we sampled (Only one side of highlight wanted) float lightAtten = pow(max(0, angle - (_SDFShadowLevel / 10.0f)), 0.8); - return smoothstep(lightAtten-_SDFNoseHighlightSmoothRange,lightAtten+_SDFNoseHighlightSmoothRange , uvMask * tex_value) * tex_value; // Safeguard, return 0 when tex_value = 0 + return smoothstep(lightAtten-_SDFHighlightSmoothLevel,lightAtten+_SDFHighlightSmoothLevel , uvMask * tex_value) * tex_value; // Safeguard, return 0 when tex_value = 0 } #endif diff --git a/Runtime/Shaders/Includes/Properties/UtsHdrpProperties.hlsl b/Runtime/Shaders/Includes/Properties/UtsHdrpProperties.hlsl index dfbf4b3..245b566 100644 --- a/Runtime/Shaders/Includes/Properties/UtsHdrpProperties.hlsl +++ b/Runtime/Shaders/Includes/Properties/UtsHdrpProperties.hlsl @@ -80,9 +80,6 @@ SAMPLER(sampler_CoatMaskMap); TEXTURE2D(_SSSLutMap); //SAMPLER(sampler_SSSLutMap); //Use s_linear_clamp_sampler instead -TEXTURE2D(_SDFShadowTex); -SAMPLER(sampler_SDFShadowTex); - TEXTURE2D(_HairShadowTex); TEXTURE2D(_HairBlendingTex); @@ -184,10 +181,6 @@ float _SpecularAAThreshold; #ifndef LAYERED_LIT_SHADER // Set of users variables -float4 _BaseColor; -float4 _BaseColorMap_ST; -float4 _BaseColorMap_TexelSize; -float4 _BaseColorMap_MipInfo; float _Metallic; float _MetallicRemapMin; @@ -325,21 +318,6 @@ PROP_DECL(float, _LinkDetailsWithBase); #endif // LAYERED_LIT_SHADER - - -// Tessellation specific - -#ifdef TESSELLATION_ON -float _TessellationFactor; -float _TessellationFactorMinDistance; -float _TessellationFactorMaxDistance; -float _TessellationFactorTriangleSize; -float _TessellationShapeFactor; -float _TessellationBackFaceCullEpsilon; -float _TessellationObjectScale; -float _TessellationTilingScale; -#endif - // Following two variables are feeded by the C++ Editor for Scene selection int _ObjectId; int _PassValue; diff --git a/Runtime/Shaders/Includes/Properties/UtsTextures.hlsl b/Runtime/Shaders/Includes/Properties/UtsTextures.hlsl index d5e81c4..d1da7a9 100644 --- a/Runtime/Shaders/Includes/Properties/UtsTextures.hlsl +++ b/Runtime/Shaders/Includes/Properties/UtsTextures.hlsl @@ -1,14 +1,18 @@ TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex); -TEXTURE2D(_1st_ShadeMap); -TEXTURE2D(_2nd_ShadeMap); +TEXTURE2D(_1stShadeColorMap); +TEXTURE2D(_2ndShadeColorMap); + +TEXTURE2D(_SDFShadowMap); +SAMPLER(sampler_SDFShadowMap); + +TEXTURE2D_ARRAY(_ShadingGradeMap); TEXTURE2D(_MatCapMap); sampler _Set_1st_ShadePosition; sampler _Set_2nd_ShadePosition; -sampler _ShadingGradeMap; sampler _HighColor_Tex; sampler _Set_HighColorMask; sampler _Set_RimLightMask; diff --git a/Runtime/Shaders/Includes/Properties/UtsUnityPerMaterial.hlsl b/Runtime/Shaders/Includes/Properties/UtsUnityPerMaterial.hlsl index 3858ad8..f3af5fc 100644 --- a/Runtime/Shaders/Includes/Properties/UtsUnityPerMaterial.hlsl +++ b/Runtime/Shaders/Includes/Properties/UtsUnityPerMaterial.hlsl @@ -1,25 +1,29 @@ -// cosntant in Unity Toon Shader +// Shading Color +float4 _BaseColor; +float4 _BaseColorMap_ST; +float4 _BaseColorMap_TexelSize; +float4 _BaseColorMap_MipInfo; +float4 _1stShadeColor; +float4 _2ndShadeColor; +fixed _UseBaseAs1st; +fixed _Use1stAs2nd; + +float _SDFShadowLevel; +float _SDFSmoothLevel; +float _SDFHighlightStrength; +float _SDFHighlightSmoothLevel; + +float _ShadingIndex; + float _utsTechnique; float4 _Color; -fixed _Use_BaseAs1st; -fixed _Use_1stAs2nd; -fixed _Is_LightColor_Base; -float4 _1st_ShadeColor; -fixed _Is_LightColor_1st_Shade; -float4 _2nd_ShadeColor; -fixed _Is_LightColor_2nd_Shade; -fixed _Is_NormalMapToBase; fixed _Set_SystemShadowsToBase; float _SurfaceFeatures; float _Tweak_SystemShadowsLevel; float _ShadowBias; -float _SDFShadowLevel; -float _SDFSmoothGamma; -float _SDFNoseHighlightCoef; -float _SDFNoseHighlightSmoothRange; float _EyeParallaxAmount; float _HairBlendingFactor; diff --git a/Runtime/UTS Renderer/UTSToonmapping/UTSTonemapping.cs b/Runtime/UTS Renderer/UTSToonmapping/UTSTonemapping.cs index d23bafa..5137192 100644 --- a/Runtime/UTS Renderer/UTSToonmapping/UTSTonemapping.cs +++ b/Runtime/UTS Renderer/UTSToonmapping/UTSTonemapping.cs @@ -7,7 +7,7 @@ using UnityEngine.Rendering.HighDefinition; public sealed class UTSTonemapping : CustomPostProcessVolumeComponent, IPostProcessComponent { [Tooltip("Controls the intensity of the effect.")] - public BoolParameter state = new BoolParameter(true, BoolParameter.DisplayType.EnumPopup); + public BoolParameter state = new BoolParameter(false, BoolParameter.DisplayType.EnumPopup); private Material _material;