Optmize shader structure

This commit is contained in:
Misaki
2025-01-31 17:25:19 +09:00
parent f41c6c9b34
commit e8d1d5923a
19 changed files with 287 additions and 95 deletions

View File

@@ -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,

View File

@@ -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<SurfaceOptionsScope>().HasFeature(SurfaceFeatureFlags.AngelRing);
protected override bool ShowSection => SurfaceOptionsScope.HasFeature(SurfaceFeatureFlags.AngelRing);
protected override ShaderGUIExpandable ExpandableBit => ShaderGUIExpandable.AngelRing;

View File

@@ -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<SurfaceOptionsScope>().HasFeature(SurfaceFeatureFlags.Outline);
protected override bool ShowSection => SurfaceOptionsScope.HasFeature(SurfaceFeatureFlags.Outline);
protected override ShaderGUIExpandable ExpandableBit => ShaderGUIExpandable.Outline;

View File

@@ -0,0 +1,150 @@
using Misaki.ShaderGUI;
using UnityEditor;
using UnityEngine;
namespace Misaki.HdrpToon.Editor
{
public class ShadingColorScope : MaterialUIScope<ShaderGUIExpandable>
{
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);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 35d7c7fd9279ea64199f1aa61c24fbcd

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 4434ff1451f044df8fdfbaddeb4ee13b
timeCreated: 1737690087

View File

@@ -0,0 +1,23 @@
using Misaki.ShaderGUI;
using UnityEditor;
using UnityEngine;
namespace Misaki.HdrpToon.Editor
{
public class ShadingGradeScope : MaterialUIScope<ShaderGUIExpandable>
{
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();
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: faf2eabd1c2767d40a2ecfe89cb56d28

View File

@@ -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);

View File

@@ -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());