SimpleLitGUI.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Rendering;
  4. using UnityEngine.Scripting.APIUpdating;
  5. namespace UnityEditor.Rendering.Universal.ShaderGUI
  6. {
  7. [MovedFrom("UnityEditor.Rendering.LWRP.ShaderGUI")] public static class SimpleLitGUI
  8. {
  9. public enum SpecularSource
  10. {
  11. SpecularTextureAndColor,
  12. NoSpecular
  13. }
  14. public enum SmoothnessMapChannel
  15. {
  16. SpecularAlpha,
  17. AlbedoAlpha,
  18. }
  19. public static class Styles
  20. {
  21. public static GUIContent specularMapText =
  22. new GUIContent("Specular Map", "Sets and configures a Specular map and color for your Material.");
  23. public static GUIContent smoothnessText = new GUIContent("Smoothness",
  24. "Controls the spread of highlights and reflections on the surface.");
  25. public static GUIContent smoothnessMapChannelText =
  26. new GUIContent("Source",
  27. "Specifies where to sample a smoothness map from. By default, uses the alpha channel for your map.");
  28. public static GUIContent highlightsText = new GUIContent("Specular Highlights",
  29. "When enabled, the Material reflects the shine from direct lighting.");
  30. }
  31. public struct SimpleLitProperties
  32. {
  33. // Surface Input Props
  34. public MaterialProperty specColor;
  35. public MaterialProperty specGlossMap;
  36. public MaterialProperty specHighlights;
  37. public MaterialProperty smoothnessMapChannel;
  38. public MaterialProperty smoothness;
  39. public MaterialProperty bumpMapProp;
  40. public SimpleLitProperties(MaterialProperty[] properties)
  41. {
  42. // Surface Input Props
  43. specColor = BaseShaderGUI.FindProperty("_SpecColor", properties);
  44. specGlossMap = BaseShaderGUI.FindProperty("_SpecGlossMap", properties, false);
  45. specHighlights = BaseShaderGUI.FindProperty("_SpecularHighlights", properties, false);
  46. smoothnessMapChannel = BaseShaderGUI.FindProperty("_SmoothnessSource", properties, false);
  47. smoothness = BaseShaderGUI.FindProperty("_Smoothness", properties, false);
  48. bumpMapProp = BaseShaderGUI.FindProperty("_BumpMap", properties, false);
  49. }
  50. }
  51. public static void Inputs(SimpleLitProperties properties, MaterialEditor materialEditor, Material material)
  52. {
  53. DoSpecularArea(properties, materialEditor, material);
  54. BaseShaderGUI.DrawNormalArea(materialEditor, properties.bumpMapProp);
  55. }
  56. public static void Advanced(SimpleLitProperties properties)
  57. {
  58. SpecularSource specularSource = (SpecularSource)properties.specHighlights.floatValue;
  59. EditorGUI.BeginChangeCheck();
  60. EditorGUI.showMixedValue = properties.specHighlights.hasMixedValue;
  61. bool enabled = EditorGUILayout.Toggle(Styles.highlightsText, specularSource == SpecularSource.SpecularTextureAndColor);
  62. if (EditorGUI.EndChangeCheck())
  63. properties.specHighlights.floatValue = enabled ? (float)SpecularSource.SpecularTextureAndColor : (float)SpecularSource.NoSpecular;
  64. EditorGUI.showMixedValue = false;
  65. }
  66. public static void DoSpecularArea(SimpleLitProperties properties, MaterialEditor materialEditor, Material material)
  67. {
  68. SpecularSource specSource = (SpecularSource)properties.specHighlights.floatValue;
  69. EditorGUI.BeginDisabledGroup(specSource == SpecularSource.NoSpecular);
  70. BaseShaderGUI.TextureColorProps(materialEditor, Styles.specularMapText, properties.specGlossMap,properties.specColor, true);
  71. DoSmoothness(properties, material);
  72. EditorGUI.EndDisabledGroup();
  73. }
  74. public static void DoSmoothness(SimpleLitProperties properties, Material material)
  75. {
  76. var opaque = ((BaseShaderGUI.SurfaceType) material.GetFloat("_Surface") ==
  77. BaseShaderGUI.SurfaceType.Opaque);
  78. EditorGUI.indentLevel += 2;
  79. EditorGUI.BeginChangeCheck();
  80. EditorGUI.showMixedValue = properties.smoothness.hasMixedValue;
  81. var smoothnessSource = (int)properties.smoothnessMapChannel.floatValue;
  82. var smoothness = properties.smoothness.floatValue;
  83. smoothness = EditorGUILayout.Slider(Styles.smoothnessText, smoothness, 0f, 1f);
  84. if (EditorGUI.EndChangeCheck())
  85. {
  86. properties.smoothness.floatValue = smoothness;
  87. }
  88. EditorGUI.showMixedValue = false;
  89. EditorGUI.indentLevel++;
  90. EditorGUI.BeginDisabledGroup(!opaque);
  91. EditorGUI.BeginChangeCheck();
  92. EditorGUI.showMixedValue = properties.smoothnessMapChannel.hasMixedValue;
  93. if(opaque)
  94. smoothnessSource = EditorGUILayout.Popup(Styles.smoothnessMapChannelText, smoothnessSource, Enum.GetNames(typeof(SmoothnessMapChannel)));
  95. else
  96. EditorGUILayout.Popup(Styles.smoothnessMapChannelText, 0, Enum.GetNames(typeof(SmoothnessMapChannel)));
  97. if (EditorGUI.EndChangeCheck())
  98. properties.smoothnessMapChannel.floatValue = smoothnessSource;
  99. EditorGUI.showMixedValue = false;
  100. EditorGUI.indentLevel -= 3;
  101. EditorGUI.EndDisabledGroup();
  102. }
  103. public static void SetMaterialKeywords(Material material)
  104. {
  105. UpdateMaterialSpecularSource(material);
  106. }
  107. private static void UpdateMaterialSpecularSource(Material material)
  108. {
  109. var opaque = ((BaseShaderGUI.SurfaceType) material.GetFloat("_Surface") ==
  110. BaseShaderGUI.SurfaceType.Opaque);
  111. SpecularSource specSource = (SpecularSource)material.GetFloat("_SpecularHighlights");
  112. if (specSource == SpecularSource.NoSpecular)
  113. {
  114. CoreUtils.SetKeyword(material, "_SPECGLOSSMAP", false);
  115. CoreUtils.SetKeyword(material, "_SPECULAR_COLOR", false);
  116. CoreUtils.SetKeyword(material, "_GLOSSINESS_FROM_BASE_ALPHA", false);
  117. }
  118. else
  119. {
  120. var smoothnessSource = (SmoothnessMapChannel)material.GetFloat("_SmoothnessSource");
  121. bool hasMap = material.GetTexture("_SpecGlossMap");
  122. CoreUtils.SetKeyword(material, "_SPECGLOSSMAP", hasMap);
  123. CoreUtils.SetKeyword(material, "_SPECULAR_COLOR", !hasMap);
  124. if(opaque)
  125. CoreUtils.SetKeyword(material, "_GLOSSINESS_FROM_BASE_ALPHA", smoothnessSource == SmoothnessMapChannel.AlbedoAlpha);
  126. else
  127. CoreUtils.SetKeyword(material, "_GLOSSINESS_FROM_BASE_ALPHA", false);
  128. string color;
  129. if (smoothnessSource != SmoothnessMapChannel.AlbedoAlpha || !opaque)
  130. color = "_SpecColor";
  131. else
  132. color = "_BaseColor";
  133. var col = material.GetColor(color);
  134. col.a = material.GetFloat("_Smoothness");
  135. material.SetColor(color, col);
  136. }
  137. }
  138. }
  139. }