TMP_SDFShaderGUI.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace TMPro.EditorUtilities
  4. {
  5. public class TMP_SDFShaderGUI : TMP_BaseShaderGUI
  6. {
  7. static ShaderFeature s_OutlineFeature, s_UnderlayFeature, s_BevelFeature, s_GlowFeature, s_MaskFeature;
  8. static bool s_Face = true, s_Outline = true, s_Underlay, s_Lighting, s_Glow, s_Bevel, s_Light, s_Bump, s_Env;
  9. static string[]
  10. s_FaceUvSpeedNames = { "_FaceUVSpeedX", "_FaceUVSpeedY" },
  11. s_OutlineUvSpeedNames = { "_OutlineUVSpeedX", "_OutlineUVSpeedY" };
  12. static TMP_SDFShaderGUI()
  13. {
  14. s_OutlineFeature = new ShaderFeature()
  15. {
  16. undoLabel = "Outline",
  17. keywords = new[] { "OUTLINE_ON" }
  18. };
  19. s_UnderlayFeature = new ShaderFeature()
  20. {
  21. undoLabel = "Underlay",
  22. keywords = new[] { "UNDERLAY_ON", "UNDERLAY_INNER" },
  23. label = new GUIContent("Underlay Type"),
  24. keywordLabels = new[]
  25. {
  26. new GUIContent("None"), new GUIContent("Normal"), new GUIContent("Inner")
  27. }
  28. };
  29. s_BevelFeature = new ShaderFeature()
  30. {
  31. undoLabel = "Bevel",
  32. keywords = new[] { "BEVEL_ON" }
  33. };
  34. s_GlowFeature = new ShaderFeature()
  35. {
  36. undoLabel = "Glow",
  37. keywords = new[] { "GLOW_ON" }
  38. };
  39. s_MaskFeature = new ShaderFeature()
  40. {
  41. undoLabel = "Mask",
  42. keywords = new[] { "MASK_HARD", "MASK_SOFT" },
  43. label = new GUIContent("Mask"),
  44. keywordLabels = new[]
  45. {
  46. new GUIContent("Mask Off"), new GUIContent("Mask Hard"), new GUIContent("Mask Soft")
  47. }
  48. };
  49. }
  50. protected override void DoGUI()
  51. {
  52. s_Face = BeginPanel("Face", s_Face);
  53. if (s_Face)
  54. {
  55. DoFacePanel();
  56. }
  57. EndPanel();
  58. s_Outline = m_Material.HasProperty(ShaderUtilities.ID_OutlineTex) ? BeginPanel("Outline", s_Outline) : BeginPanel("Outline", s_OutlineFeature, s_Outline);
  59. if (s_Outline)
  60. {
  61. DoOutlinePanel();
  62. }
  63. EndPanel();
  64. if (m_Material.HasProperty(ShaderUtilities.ID_UnderlayColor))
  65. {
  66. s_Underlay = BeginPanel("Underlay", s_UnderlayFeature, s_Underlay);
  67. if (s_Underlay)
  68. {
  69. DoUnderlayPanel();
  70. }
  71. EndPanel();
  72. }
  73. if (m_Material.HasProperty("_SpecularColor"))
  74. {
  75. s_Lighting = BeginPanel("Lighting", s_BevelFeature, s_Lighting);
  76. if (s_Lighting)
  77. {
  78. s_Bevel = BeginPanel("Bevel", s_Bevel);
  79. if (s_Bevel)
  80. {
  81. DoBevelPanel();
  82. }
  83. EndPanel();
  84. s_Light = BeginPanel("Local Lighting", s_Light);
  85. if (s_Light)
  86. {
  87. DoLocalLightingPanel();
  88. }
  89. EndPanel();
  90. s_Bump = BeginPanel("Bump Map", s_Bump);
  91. if (s_Bump)
  92. {
  93. DoBumpMapPanel();
  94. }
  95. EndPanel();
  96. s_Env = BeginPanel("Environment Map", s_Env);
  97. if (s_Env)
  98. {
  99. DoEnvMapPanel();
  100. }
  101. EndPanel();
  102. }
  103. EndPanel();
  104. }
  105. else if (m_Material.HasProperty("_SpecColor"))
  106. {
  107. s_Bevel = BeginPanel("Bevel", s_Bevel);
  108. if (s_Bevel)
  109. {
  110. DoBevelPanel();
  111. }
  112. EndPanel();
  113. s_Light = BeginPanel("Surface Lighting", s_Light);
  114. if (s_Light)
  115. {
  116. DoSurfaceLightingPanel();
  117. }
  118. EndPanel();
  119. s_Bump = BeginPanel("Bump Map", s_Bump);
  120. if (s_Bump)
  121. {
  122. DoBumpMapPanel();
  123. }
  124. EndPanel();
  125. s_Env = BeginPanel("Environment Map", s_Env);
  126. if (s_Env)
  127. {
  128. DoEnvMapPanel();
  129. }
  130. EndPanel();
  131. }
  132. if (m_Material.HasProperty(ShaderUtilities.ID_GlowColor))
  133. {
  134. s_Glow = BeginPanel("Glow", s_GlowFeature, s_Glow);
  135. if (s_Glow)
  136. {
  137. DoGlowPanel();
  138. }
  139. EndPanel();
  140. }
  141. s_DebugExtended = BeginPanel("Debug Settings", s_DebugExtended);
  142. if (s_DebugExtended)
  143. {
  144. DoDebugPanel();
  145. }
  146. EndPanel();
  147. }
  148. void DoFacePanel()
  149. {
  150. EditorGUI.indentLevel += 1;
  151. DoColor("_FaceColor", "Color");
  152. if (m_Material.HasProperty(ShaderUtilities.ID_FaceTex))
  153. {
  154. if (m_Material.HasProperty("_FaceUVSpeedX"))
  155. {
  156. DoTexture2D("_FaceTex", "Texture", true, s_FaceUvSpeedNames);
  157. }
  158. else
  159. {
  160. DoTexture2D("_FaceTex", "Texture", true);
  161. }
  162. }
  163. DoSlider("_OutlineSoftness", "Softness");
  164. DoSlider("_FaceDilate", "Dilate");
  165. if (m_Material.HasProperty(ShaderUtilities.ID_Shininess))
  166. {
  167. DoSlider("_FaceShininess", "Gloss");
  168. }
  169. EditorGUI.indentLevel -= 1;
  170. EditorGUILayout.Space();
  171. }
  172. void DoOutlinePanel()
  173. {
  174. EditorGUI.indentLevel += 1;
  175. DoColor("_OutlineColor", "Color");
  176. if (m_Material.HasProperty(ShaderUtilities.ID_OutlineTex))
  177. {
  178. if (m_Material.HasProperty("_OutlineUVSpeedX"))
  179. {
  180. DoTexture2D("_OutlineTex", "Texture", true, s_OutlineUvSpeedNames);
  181. }
  182. else
  183. {
  184. DoTexture2D("_OutlineTex", "Texture", true);
  185. }
  186. }
  187. DoSlider("_OutlineWidth", "Thickness");
  188. if (m_Material.HasProperty("_OutlineShininess"))
  189. {
  190. DoSlider("_OutlineShininess", "Gloss");
  191. }
  192. EditorGUI.indentLevel -= 1;
  193. EditorGUILayout.Space();
  194. }
  195. void DoUnderlayPanel()
  196. {
  197. EditorGUI.indentLevel += 1;
  198. s_UnderlayFeature.DoPopup(m_Editor, m_Material);
  199. DoColor("_UnderlayColor", "Color");
  200. DoSlider("_UnderlayOffsetX", "Offset X");
  201. DoSlider("_UnderlayOffsetY", "Offset Y");
  202. DoSlider("_UnderlayDilate", "Dilate");
  203. DoSlider("_UnderlaySoftness", "Softness");
  204. EditorGUI.indentLevel -= 1;
  205. EditorGUILayout.Space();
  206. }
  207. static GUIContent[] s_BevelTypeLabels =
  208. {
  209. new GUIContent("Outer Bevel"),
  210. new GUIContent("Inner Bevel")
  211. };
  212. void DoBevelPanel()
  213. {
  214. EditorGUI.indentLevel += 1;
  215. DoPopup("_ShaderFlags", "Type", s_BevelTypeLabels);
  216. DoSlider("_Bevel", "Amount");
  217. DoSlider("_BevelOffset", "Offset");
  218. DoSlider("_BevelWidth", "Width");
  219. DoSlider("_BevelRoundness", "Roundness");
  220. DoSlider("_BevelClamp", "Clamp");
  221. EditorGUI.indentLevel -= 1;
  222. EditorGUILayout.Space();
  223. }
  224. void DoLocalLightingPanel()
  225. {
  226. EditorGUI.indentLevel += 1;
  227. DoSlider("_LightAngle", "Light Angle");
  228. DoColor("_SpecularColor", "Specular Color");
  229. DoSlider("_SpecularPower", "Specular Power");
  230. DoSlider("_Reflectivity", "Reflectivity Power");
  231. DoSlider("_Diffuse", "Diffuse Shadow");
  232. DoSlider("_Ambient", "Ambient Shadow");
  233. EditorGUI.indentLevel -= 1;
  234. EditorGUILayout.Space();
  235. }
  236. void DoSurfaceLightingPanel()
  237. {
  238. EditorGUI.indentLevel += 1;
  239. DoColor("_SpecColor", "Specular Color");
  240. EditorGUI.indentLevel -= 1;
  241. EditorGUILayout.Space();
  242. }
  243. void DoBumpMapPanel()
  244. {
  245. EditorGUI.indentLevel += 1;
  246. DoTexture2D("_BumpMap", "Texture");
  247. DoSlider("_BumpFace", "Face");
  248. DoSlider("_BumpOutline", "Outline");
  249. EditorGUI.indentLevel -= 1;
  250. EditorGUILayout.Space();
  251. }
  252. void DoEnvMapPanel()
  253. {
  254. EditorGUI.indentLevel += 1;
  255. DoColor("_ReflectFaceColor", "Face Color");
  256. DoColor("_ReflectOutlineColor", "Outline Color");
  257. DoCubeMap("_Cube", "Texture");
  258. DoVector3("_EnvMatrixRotation", "Rotation");
  259. EditorGUI.indentLevel -= 1;
  260. EditorGUILayout.Space();
  261. }
  262. void DoGlowPanel()
  263. {
  264. EditorGUI.indentLevel += 1;
  265. DoColor("_GlowColor", "Color");
  266. DoSlider("_GlowOffset", "Offset");
  267. DoSlider("_GlowInner", "Inner");
  268. DoSlider("_GlowOuter", "Outer");
  269. DoSlider("_GlowPower", "Power");
  270. EditorGUI.indentLevel -= 1;
  271. EditorGUILayout.Space();
  272. }
  273. void DoDebugPanel()
  274. {
  275. EditorGUI.indentLevel += 1;
  276. DoTexture2D("_MainTex", "Font Atlas");
  277. DoFloat("_GradientScale", "Gradient Scale");
  278. DoFloat("_TextureWidth", "Texture Width");
  279. DoFloat("_TextureHeight", "Texture Height");
  280. EditorGUILayout.Space();
  281. DoFloat("_ScaleX", "Scale X");
  282. DoFloat("_ScaleY", "Scale Y");
  283. if (m_Material.HasProperty(ShaderUtilities.ID_Sharpness))
  284. DoSlider("_Sharpness", "Sharpness");
  285. DoSlider("_PerspectiveFilter", "Perspective Filter");
  286. EditorGUILayout.Space();
  287. DoFloat("_VertexOffsetX", "Offset X");
  288. DoFloat("_VertexOffsetY", "Offset Y");
  289. if (m_Material.HasProperty(ShaderUtilities.ID_MaskCoord))
  290. {
  291. EditorGUILayout.Space();
  292. s_MaskFeature.ReadState(m_Material);
  293. s_MaskFeature.DoPopup(m_Editor, m_Material);
  294. if (s_MaskFeature.Active)
  295. {
  296. DoMaskSubgroup();
  297. }
  298. EditorGUILayout.Space();
  299. DoVector("_ClipRect", "Clip Rect", s_LbrtVectorLabels);
  300. }
  301. else if (m_Material.HasProperty("_MaskTex"))
  302. {
  303. DoMaskTexSubgroup();
  304. }
  305. else if (m_Material.HasProperty(ShaderUtilities.ID_MaskSoftnessX))
  306. {
  307. EditorGUILayout.Space();
  308. DoFloat("_MaskSoftnessX", "Softness X");
  309. DoFloat("_MaskSoftnessY", "Softness Y");
  310. DoVector("_ClipRect", "Clip Rect", s_LbrtVectorLabels);
  311. }
  312. if (m_Material.HasProperty(ShaderUtilities.ID_StencilID))
  313. {
  314. EditorGUILayout.Space();
  315. DoFloat("_Stencil", "Stencil ID");
  316. DoFloat("_StencilComp", "Stencil Comp");
  317. }
  318. EditorGUILayout.Space();
  319. EditorGUI.BeginChangeCheck();
  320. bool useRatios = EditorGUILayout.Toggle("Use Ratios", !m_Material.IsKeywordEnabled("RATIOS_OFF"));
  321. if (EditorGUI.EndChangeCheck())
  322. {
  323. m_Editor.RegisterPropertyChangeUndo("Use Ratios");
  324. if (useRatios)
  325. {
  326. m_Material.DisableKeyword("RATIOS_OFF");
  327. }
  328. else
  329. {
  330. m_Material.EnableKeyword("RATIOS_OFF");
  331. }
  332. }
  333. EditorGUI.BeginDisabledGroup(true);
  334. DoFloat("_ScaleRatioA", "Scale Ratio A");
  335. DoFloat("_ScaleRatioB", "Scale Ratio B");
  336. DoFloat("_ScaleRatioC", "Scale Ratio C");
  337. EditorGUI.EndDisabledGroup();
  338. EditorGUI.indentLevel -= 1;
  339. EditorGUILayout.Space();
  340. }
  341. void DoMaskSubgroup()
  342. {
  343. DoVector("_MaskCoord", "Mask Bounds", s_XywhVectorLabels);
  344. if (Selection.activeGameObject != null)
  345. {
  346. Renderer renderer = Selection.activeGameObject.GetComponent<Renderer>();
  347. if (renderer != null)
  348. {
  349. Rect rect = EditorGUILayout.GetControlRect();
  350. rect.x += EditorGUIUtility.labelWidth;
  351. rect.width -= EditorGUIUtility.labelWidth;
  352. if (GUI.Button(rect, "Match Renderer Bounds"))
  353. {
  354. FindProperty("_MaskCoord", m_Properties).vectorValue = new Vector4(
  355. 0,
  356. 0,
  357. Mathf.Round(renderer.bounds.extents.x * 1000) / 1000,
  358. Mathf.Round(renderer.bounds.extents.y * 1000) / 1000
  359. );
  360. }
  361. }
  362. }
  363. if (s_MaskFeature.State == 1)
  364. {
  365. DoFloat("_MaskSoftnessX", "Softness X");
  366. DoFloat("_MaskSoftnessY", "Softness Y");
  367. }
  368. }
  369. void DoMaskTexSubgroup()
  370. {
  371. EditorGUILayout.Space();
  372. DoTexture2D("_MaskTex", "Mask Texture");
  373. DoToggle("_MaskInverse", "Inverse Mask");
  374. DoColor("_MaskEdgeColor", "Edge Color");
  375. DoSlider("_MaskEdgeSoftness", "Edge Softness");
  376. DoSlider("_MaskWipeControl", "Wipe Position");
  377. DoFloat("_MaskSoftnessX", "Softness X");
  378. DoFloat("_MaskSoftnessY", "Softness Y");
  379. DoVector("_ClipRect", "Clip Rect", s_LbrtVectorLabels);
  380. }
  381. }
  382. }