暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SimpleLitGUI.cs 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Rendering;
  4. using UnityEngine.Scripting.APIUpdating;
  5. namespace UnityEditor.Rendering.Universal.ShaderGUI
  6. {
  7. /// <summary>
  8. /// Editor script for the SimpleLit material inspector.
  9. /// </summary>
  10. public static class SimpleLitGUI
  11. {
  12. /// <summary>
  13. /// Options for specular source.
  14. /// </summary>
  15. public enum SpecularSource
  16. {
  17. /// <summary>
  18. /// Use this to use specular texture and color.
  19. /// </summary>
  20. SpecularTextureAndColor,
  21. /// <summary>
  22. /// Use this when not using specular.
  23. /// </summary>
  24. NoSpecular
  25. }
  26. /// <summary>
  27. /// Options to select the texture channel where the smoothness value is stored.
  28. /// </summary>
  29. public enum SmoothnessMapChannel
  30. {
  31. /// <summary>
  32. /// Use this when smoothness is stored in the alpha channel of the Specular Map.
  33. /// </summary>
  34. SpecularAlpha,
  35. /// <summary>
  36. /// Use this when smoothness is stored in the alpha channel of the Albedo Map.
  37. /// </summary>
  38. AlbedoAlpha,
  39. }
  40. /// <summary>
  41. /// Container for the text and tooltips used to display the shader.
  42. /// </summary>
  43. public static class Styles
  44. {
  45. /// <summary>
  46. /// The text and tooltip for the specular map GUI.
  47. /// </summary>
  48. public static GUIContent specularMapText =
  49. EditorGUIUtility.TrTextContent("Specular Map", "Designates a Specular Map and specular color determining the apperance of reflections on this Material's surface.");
  50. }
  51. /// <summary>
  52. /// Container for the properties used in the <c>SimpleLitGUI</c> editor script.
  53. /// </summary>
  54. public struct SimpleLitProperties
  55. {
  56. // Surface Input Props
  57. /// <summary>
  58. /// The MaterialProperty for specular color.
  59. /// </summary>
  60. public MaterialProperty specColor;
  61. /// <summary>
  62. /// The MaterialProperty for specular smoothness map.
  63. /// </summary>
  64. public MaterialProperty specGlossMap;
  65. /// <summary>
  66. /// The MaterialProperty for specular highlights.
  67. /// </summary>
  68. public MaterialProperty specHighlights;
  69. /// <summary>
  70. /// The MaterialProperty for smoothness alpha channel.
  71. /// </summary>
  72. public MaterialProperty smoothnessMapChannel;
  73. /// <summary>
  74. /// The MaterialProperty for smoothness value.
  75. /// </summary>
  76. public MaterialProperty smoothness;
  77. /// <summary>
  78. /// The MaterialProperty for normal map.
  79. /// </summary>
  80. public MaterialProperty bumpMapProp;
  81. /// <summary>
  82. /// Constructor for the <c>SimpleLitProperties</c> container struct.
  83. /// </summary>
  84. /// <param name="properties"></param>
  85. public SimpleLitProperties(MaterialProperty[] properties)
  86. {
  87. // Surface Input Props
  88. specColor = BaseShaderGUI.FindProperty("_SpecColor", properties);
  89. specGlossMap = BaseShaderGUI.FindProperty("_SpecGlossMap", properties, false);
  90. specHighlights = BaseShaderGUI.FindProperty("_SpecularHighlights", properties, false);
  91. smoothnessMapChannel = BaseShaderGUI.FindProperty("_SmoothnessSource", properties, false);
  92. smoothness = BaseShaderGUI.FindProperty("_Smoothness", properties, false);
  93. bumpMapProp = BaseShaderGUI.FindProperty("_BumpMap", properties, false);
  94. }
  95. }
  96. /// <summary>
  97. /// Draws the surface inputs GUI.
  98. /// </summary>
  99. /// <param name="properties"></param>
  100. /// <param name="materialEditor"></param>
  101. /// <param name="material">The material to use.</param>
  102. public static void Inputs(SimpleLitProperties properties, MaterialEditor materialEditor, Material material)
  103. {
  104. DoSpecularArea(properties, materialEditor, material);
  105. BaseShaderGUI.DrawNormalArea(materialEditor, properties.bumpMapProp);
  106. }
  107. /// <summary>
  108. /// Draws the advanced GUI.
  109. /// </summary>
  110. /// <param name="properties"></param>
  111. public static void Advanced(SimpleLitProperties properties)
  112. {
  113. SpecularSource specularSource = (SpecularSource)properties.specHighlights.floatValue;
  114. EditorGUI.BeginChangeCheck();
  115. EditorGUI.showMixedValue = properties.specHighlights.hasMixedValue;
  116. bool enabled = EditorGUILayout.Toggle(LitGUI.Styles.highlightsText, specularSource == SpecularSource.SpecularTextureAndColor);
  117. if (EditorGUI.EndChangeCheck())
  118. properties.specHighlights.floatValue = enabled ? (float)SpecularSource.SpecularTextureAndColor : (float)SpecularSource.NoSpecular;
  119. EditorGUI.showMixedValue = false;
  120. }
  121. /// <summary>
  122. /// Draws the specular area GUI.
  123. /// </summary>
  124. /// <param name="properties"></param>
  125. /// <param name="materialEditor"></param>
  126. /// <param name="material">The material to use.</param>
  127. public static void DoSpecularArea(SimpleLitProperties properties, MaterialEditor materialEditor, Material material)
  128. {
  129. SpecularSource specSource = (SpecularSource)properties.specHighlights.floatValue;
  130. EditorGUI.BeginDisabledGroup(specSource == SpecularSource.NoSpecular);
  131. BaseShaderGUI.TextureColorProps(materialEditor, Styles.specularMapText, properties.specGlossMap, properties.specColor, true);
  132. LitGUI.DoSmoothness(materialEditor, material, properties.smoothness, properties.smoothnessMapChannel, LitGUI.Styles.specularSmoothnessChannelNames);
  133. EditorGUI.EndDisabledGroup();
  134. }
  135. /// <summary>
  136. /// Sets up the keywords for the material and shader.
  137. /// </summary>
  138. /// <param name="material">The material to use.</param>
  139. public static void SetMaterialKeywords(Material material)
  140. {
  141. UpdateMaterialSpecularSource(material);
  142. }
  143. private static void UpdateMaterialSpecularSource(Material material)
  144. {
  145. var opaque = ((BaseShaderGUI.SurfaceType)material.GetFloat("_Surface") ==
  146. BaseShaderGUI.SurfaceType.Opaque);
  147. SpecularSource specSource = (SpecularSource)material.GetFloat("_SpecularHighlights");
  148. if (specSource == SpecularSource.NoSpecular)
  149. {
  150. CoreUtils.SetKeyword(material, "_SPECGLOSSMAP", false);
  151. CoreUtils.SetKeyword(material, "_SPECULAR_COLOR", false);
  152. CoreUtils.SetKeyword(material, "_GLOSSINESS_FROM_BASE_ALPHA", false);
  153. }
  154. else
  155. {
  156. var smoothnessSource = (SmoothnessMapChannel)material.GetFloat("_SmoothnessSource");
  157. bool hasMap = material.GetTexture("_SpecGlossMap");
  158. CoreUtils.SetKeyword(material, "_SPECGLOSSMAP", hasMap);
  159. CoreUtils.SetKeyword(material, "_SPECULAR_COLOR", !hasMap);
  160. if (opaque)
  161. CoreUtils.SetKeyword(material, "_GLOSSINESS_FROM_BASE_ALPHA", smoothnessSource == SmoothnessMapChannel.AlbedoAlpha);
  162. else
  163. CoreUtils.SetKeyword(material, "_GLOSSINESS_FROM_BASE_ALPHA", false);
  164. string color;
  165. if (smoothnessSource != SmoothnessMapChannel.AlbedoAlpha || !opaque)
  166. color = "_SpecColor";
  167. else
  168. color = "_BaseColor";
  169. var col = material.GetColor(color);
  170. float smoothness = material.GetFloat("_Smoothness");
  171. if (smoothness != col.a)
  172. {
  173. col.a = smoothness;
  174. material.SetColor(color, col);
  175. }
  176. }
  177. }
  178. }
  179. }