ToonShaderGUI.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using UnityEditor;
  2. using UnityEditor.Rendering;
  3. using UnityEngine;
  4. namespace CiroContinisio.ToonShader.Editor
  5. {
  6. public class ToonShaderGUI : ShaderGUI
  7. {
  8. private enum ShadingStyle
  9. {
  10. SkinAndTextiles,
  11. HairAndMetal,
  12. }
  13. private ShadingStyle _shadingStyle;
  14. private bool _toonPropsVisible = true;
  15. private bool _emission;
  16. private const string SkinKeyword = "_SHADING_STYLE_SKIN_AND_TEXTILES";
  17. private const string HairKeyword = "_SHADING_STYLE_HAIR_AND_METAL";
  18. public override void OnGUI (MaterialEditor materialEditor, MaterialProperty[] properties)
  19. {
  20. Material targetMaterial = materialEditor.target as Material;
  21. //======= Base Properties
  22. GUILayout.Label("Base properties", EditorStyles.boldLabel);
  23. EditorGUI.indentLevel++;
  24. materialEditor.TextureProperty(FindProperty("_BaseMap", properties), "Base map");
  25. materialEditor.ColorProperty(FindProperty("_BaseColor", properties), "Tint");
  26. materialEditor.TextureProperty(FindProperty("_NormalMap", properties), "Normal map");
  27. //Emission property
  28. bool emissionPreviousValue = _emission;
  29. _emission = DrawEmissionToggle(materialEditor, FindProperty("_UseEmission", properties));
  30. if (_emission)
  31. {
  32. EditorGUI.indentLevel++;
  33. materialEditor.TextureProperty(FindProperty("_EmissionMap", properties), "Emission map");
  34. materialEditor.ColorProperty(FindProperty("_EmissionColor", properties), "Emission tint");
  35. EditorGUI.indentLevel--;
  36. if (emissionPreviousValue != _emission)
  37. {
  38. targetMaterial.EnableKeyword("_EMISSION");
  39. targetMaterial.globalIlluminationFlags = MaterialGlobalIlluminationFlags.None;
  40. }
  41. }
  42. else
  43. {
  44. if (emissionPreviousValue != _emission)
  45. {
  46. targetMaterial.DisableKeyword("_EMISSION");
  47. targetMaterial.globalIlluminationFlags = MaterialGlobalIlluminationFlags.EmissiveIsBlack;
  48. }
  49. }
  50. materialEditor.RangeProperty(FindProperty("_Smoothness", properties), "Smoothness");
  51. materialEditor.ColorProperty(FindProperty("_SpecularColor", properties), "Specular color");
  52. EditorGUI.indentLevel--;
  53. //======= Shading style
  54. EditorGUILayout.Space(4f);
  55. EditorGUILayout.LabelField("Shading style", EditorStyles.boldLabel);
  56. EditorGUILayout.Space(-3f);
  57. EditorGUI.indentLevel++;
  58. string[] options = {"Skin or Textiles", "Hair or Metal"};
  59. _shadingStyle = (ShadingStyle)materialEditor.PopupShaderProperty(FindProperty("_SHADING_STYLE", properties), new GUIContent("Material"), options);
  60. switch (_shadingStyle)
  61. {
  62. case ShadingStyle.SkinAndTextiles:
  63. materialEditor.RangeProperty(FindProperty("_PaintbrushStrokesSize", properties), "Paintbrush strokes size");
  64. materialEditor.RangeProperty(FindProperty("_PaintbrushStrokesAngle", properties), "Paintbrush strokes angle");
  65. break;
  66. case ShadingStyle.HairAndMetal:
  67. materialEditor.RangeProperty(FindProperty("_SpecularNoiseSize", properties), "Specular noise size");
  68. materialEditor.RangeProperty(FindProperty("_ExtraBandThickness", properties), "Extra band thickness");
  69. break;
  70. }
  71. targetMaterial.EnableKeyword(_shadingStyle == ShadingStyle.SkinAndTextiles ? SkinKeyword : HairKeyword);
  72. targetMaterial.DisableKeyword(_shadingStyle == ShadingStyle.SkinAndTextiles ? HairKeyword : SkinKeyword);
  73. EditorGUI.indentLevel--;
  74. //======= Toon Properties
  75. EditorGUILayout.Space(4f);
  76. _toonPropsVisible = EditorGUILayout.Foldout(_toonPropsVisible, "Toon properties", EditorStyles.foldoutHeader);
  77. if (_toonPropsVisible)
  78. {
  79. EditorGUI.indentLevel++;
  80. materialEditor.RangeProperty(FindProperty("_ToonCutoffPoint", properties), "Toon cutoff");
  81. materialEditor.RangeProperty(FindProperty("_AdditionalLightCutoffPoint", properties), "Additional light cutoff");
  82. materialEditor.RangeProperty(FindProperty("_ShadowsFloor", properties), "Shadows floor value");
  83. materialEditor.RangeProperty(FindProperty("_StrongRimStrength", properties), "Strong rim strength");
  84. EditorGUI.indentLevel--;
  85. }
  86. }
  87. private bool DrawEmissionToggle(MaterialEditor materialEditor, MaterialProperty prop)
  88. {
  89. bool value = (prop.floatValue != 0.0f);
  90. EditorGUI.BeginChangeCheck();
  91. EditorGUI.showMixedValue = prop.hasMixedValue;
  92. // Show the toggle control
  93. value = EditorGUILayout.Toggle("Emission", value);
  94. EditorGUI.showMixedValue = false;
  95. if (EditorGUI.EndChangeCheck())
  96. {
  97. prop.floatValue = value ? 1.0f : 0.0f;
  98. }
  99. return value && !prop.hasMixedValue;
  100. }
  101. }
  102. }