TMP_SDFShaderGUI.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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_Outline2, 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_Outline2Color))
  65. {
  66. s_Outline2 = BeginPanel("Outline 2", s_OutlineFeature, s_Outline2);
  67. if (s_Outline2)
  68. {
  69. DoOutline2Panel();
  70. }
  71. EndPanel();
  72. }
  73. if (m_Material.HasProperty(ShaderUtilities.ID_UnderlayColor))
  74. {
  75. s_Underlay = BeginPanel("Underlay", s_UnderlayFeature, s_Underlay);
  76. if (s_Underlay)
  77. {
  78. DoUnderlayPanel();
  79. }
  80. EndPanel();
  81. }
  82. if (m_Material.HasProperty("_SpecularColor"))
  83. {
  84. s_Lighting = BeginPanel("Lighting", s_BevelFeature, s_Lighting);
  85. if (s_Lighting)
  86. {
  87. s_Bevel = BeginPanel("Bevel", s_Bevel);
  88. if (s_Bevel)
  89. {
  90. DoBevelPanel();
  91. }
  92. EndPanel();
  93. s_Light = BeginPanel("Local Lighting", s_Light);
  94. if (s_Light)
  95. {
  96. DoLocalLightingPanel();
  97. }
  98. EndPanel();
  99. s_Bump = BeginPanel("Bump Map", s_Bump);
  100. if (s_Bump)
  101. {
  102. DoBumpMapPanel();
  103. }
  104. EndPanel();
  105. s_Env = BeginPanel("Environment Map", s_Env);
  106. if (s_Env)
  107. {
  108. DoEnvMapPanel();
  109. }
  110. EndPanel();
  111. }
  112. EndPanel();
  113. }
  114. else if (m_Material.HasProperty("_SpecColor"))
  115. {
  116. s_Bevel = BeginPanel("Bevel", s_Bevel);
  117. if (s_Bevel)
  118. {
  119. DoBevelPanel();
  120. }
  121. EndPanel();
  122. s_Light = BeginPanel("Surface Lighting", s_Light);
  123. if (s_Light)
  124. {
  125. DoSurfaceLightingPanel();
  126. }
  127. EndPanel();
  128. s_Bump = BeginPanel("Bump Map", s_Bump);
  129. if (s_Bump)
  130. {
  131. DoBumpMapPanel();
  132. }
  133. EndPanel();
  134. s_Env = BeginPanel("Environment Map", s_Env);
  135. if (s_Env)
  136. {
  137. DoEnvMapPanel();
  138. }
  139. EndPanel();
  140. }
  141. if (m_Material.HasProperty(ShaderUtilities.ID_GlowColor))
  142. {
  143. s_Glow = BeginPanel("Glow", s_GlowFeature, s_Glow);
  144. if (s_Glow)
  145. {
  146. DoGlowPanel();
  147. }
  148. EndPanel();
  149. }
  150. s_DebugExtended = BeginPanel("Debug Settings", s_DebugExtended);
  151. if (s_DebugExtended)
  152. {
  153. DoDebugPanel();
  154. }
  155. EndPanel();
  156. }
  157. void DoFacePanel()
  158. {
  159. EditorGUI.indentLevel += 1;
  160. DoColor("_FaceColor", "Color");
  161. if (m_Material.HasProperty(ShaderUtilities.ID_FaceTex))
  162. {
  163. if (m_Material.HasProperty("_FaceUVSpeedX"))
  164. {
  165. DoTexture2D("_FaceTex", "Texture", true, s_FaceUvSpeedNames);
  166. }
  167. else
  168. {
  169. DoTexture2D("_FaceTex", "Texture", true);
  170. }
  171. }
  172. DoSlider("_OutlineSoftness", "Softness");
  173. DoSlider("_FaceDilate", "Dilate");
  174. if (m_Material.HasProperty(ShaderUtilities.ID_Shininess))
  175. {
  176. DoSlider("_FaceShininess", "Gloss");
  177. }
  178. EditorGUI.indentLevel -= 1;
  179. EditorGUILayout.Space();
  180. }
  181. void DoOutlinePanel()
  182. {
  183. EditorGUI.indentLevel += 1;
  184. DoColor("_OutlineColor", "Color");
  185. if (m_Material.HasProperty(ShaderUtilities.ID_OutlineTex))
  186. {
  187. if (m_Material.HasProperty("_OutlineUVSpeedX"))
  188. {
  189. DoTexture2D("_OutlineTex", "Texture", true, s_OutlineUvSpeedNames);
  190. }
  191. else
  192. {
  193. DoTexture2D("_OutlineTex", "Texture", true);
  194. }
  195. }
  196. DoSlider("_OutlineWidth", "Thickness");
  197. if (m_Material.HasProperty("_OutlineShininess"))
  198. {
  199. DoSlider("_OutlineShininess", "Gloss");
  200. }
  201. EditorGUI.indentLevel -= 1;
  202. EditorGUILayout.Space();
  203. }
  204. void DoOutline2Panel()
  205. {
  206. EditorGUI.indentLevel += 1;
  207. DoColor("_Outline2Color", "Color");
  208. //if (m_Material.HasProperty(ShaderUtilities.ID_OutlineTex))
  209. //{
  210. // if (m_Material.HasProperty("_OutlineUVSpeedX"))
  211. // {
  212. // DoTexture2D("_OutlineTex", "Texture", true, s_OutlineUvSpeedNames);
  213. // }
  214. // else
  215. // {
  216. // DoTexture2D("_OutlineTex", "Texture", true);
  217. // }
  218. //}
  219. DoSlider("_Outline2Width", "Thickness");
  220. //if (m_Material.HasProperty("_OutlineShininess"))
  221. //{
  222. // DoSlider("_OutlineShininess", "Gloss");
  223. //}
  224. EditorGUI.indentLevel -= 1;
  225. EditorGUILayout.Space();
  226. }
  227. void DoUnderlayPanel()
  228. {
  229. EditorGUI.indentLevel += 1;
  230. s_UnderlayFeature.DoPopup(m_Editor, m_Material);
  231. DoColor("_UnderlayColor", "Color");
  232. DoSlider("_UnderlayOffsetX", "Offset X");
  233. DoSlider("_UnderlayOffsetY", "Offset Y");
  234. DoSlider("_UnderlayDilate", "Dilate");
  235. DoSlider("_UnderlaySoftness", "Softness");
  236. EditorGUI.indentLevel -= 1;
  237. EditorGUILayout.Space();
  238. }
  239. static GUIContent[] s_BevelTypeLabels =
  240. {
  241. new GUIContent("Outer Bevel"),
  242. new GUIContent("Inner Bevel")
  243. };
  244. void DoBevelPanel()
  245. {
  246. EditorGUI.indentLevel += 1;
  247. DoPopup("_ShaderFlags", "Type", s_BevelTypeLabels);
  248. DoSlider("_Bevel", "Amount");
  249. DoSlider("_BevelOffset", "Offset");
  250. DoSlider("_BevelWidth", "Width");
  251. DoSlider("_BevelRoundness", "Roundness");
  252. DoSlider("_BevelClamp", "Clamp");
  253. EditorGUI.indentLevel -= 1;
  254. EditorGUILayout.Space();
  255. }
  256. void DoLocalLightingPanel()
  257. {
  258. EditorGUI.indentLevel += 1;
  259. DoSlider("_LightAngle", "Light Angle");
  260. DoColor("_SpecularColor", "Specular Color");
  261. DoSlider("_SpecularPower", "Specular Power");
  262. DoSlider("_Reflectivity", "Reflectivity Power");
  263. DoSlider("_Diffuse", "Diffuse Shadow");
  264. DoSlider("_Ambient", "Ambient Shadow");
  265. EditorGUI.indentLevel -= 1;
  266. EditorGUILayout.Space();
  267. }
  268. void DoSurfaceLightingPanel()
  269. {
  270. EditorGUI.indentLevel += 1;
  271. DoColor("_SpecColor", "Specular Color");
  272. EditorGUI.indentLevel -= 1;
  273. EditorGUILayout.Space();
  274. }
  275. void DoBumpMapPanel()
  276. {
  277. EditorGUI.indentLevel += 1;
  278. DoTexture2D("_BumpMap", "Texture");
  279. DoSlider("_BumpFace", "Face");
  280. DoSlider("_BumpOutline", "Outline");
  281. EditorGUI.indentLevel -= 1;
  282. EditorGUILayout.Space();
  283. }
  284. void DoEnvMapPanel()
  285. {
  286. EditorGUI.indentLevel += 1;
  287. DoColor("_ReflectFaceColor", "Face Color");
  288. DoColor("_ReflectOutlineColor", "Outline Color");
  289. DoCubeMap("_Cube", "Texture");
  290. DoVector3("_EnvMatrixRotation", "Rotation");
  291. EditorGUI.indentLevel -= 1;
  292. EditorGUILayout.Space();
  293. }
  294. void DoGlowPanel()
  295. {
  296. EditorGUI.indentLevel += 1;
  297. DoColor("_GlowColor", "Color");
  298. DoSlider("_GlowOffset", "Offset");
  299. DoSlider("_GlowInner", "Inner");
  300. DoSlider("_GlowOuter", "Outer");
  301. DoSlider("_GlowPower", "Power");
  302. EditorGUI.indentLevel -= 1;
  303. EditorGUILayout.Space();
  304. }
  305. void DoDebugPanel()
  306. {
  307. EditorGUI.indentLevel += 1;
  308. DoTexture2D("_MainTex", "Font Atlas");
  309. DoFloat("_GradientScale", "Gradient Scale");
  310. DoFloat("_TextureWidth", "Texture Width");
  311. DoFloat("_TextureHeight", "Texture Height");
  312. EditorGUILayout.Space();
  313. DoFloat("_ScaleX", "Scale X");
  314. DoFloat("_ScaleY", "Scale Y");
  315. if (m_Material.HasProperty(ShaderUtilities.ID_Sharpness))
  316. DoSlider("_Sharpness", "Sharpness");
  317. DoSlider("_PerspectiveFilter", "Perspective Filter");
  318. EditorGUILayout.Space();
  319. DoFloat("_VertexOffsetX", "Offset X");
  320. DoFloat("_VertexOffsetY", "Offset Y");
  321. if (m_Material.HasProperty(ShaderUtilities.ID_MaskCoord))
  322. {
  323. EditorGUILayout.Space();
  324. s_MaskFeature.ReadState(m_Material);
  325. s_MaskFeature.DoPopup(m_Editor, m_Material);
  326. if (s_MaskFeature.Active)
  327. {
  328. DoMaskSubgroup();
  329. }
  330. EditorGUILayout.Space();
  331. DoVector("_ClipRect", "Clip Rect", s_LbrtVectorLabels);
  332. }
  333. else if (m_Material.HasProperty("_MaskTex"))
  334. {
  335. DoMaskTexSubgroup();
  336. }
  337. else if (m_Material.HasProperty(ShaderUtilities.ID_MaskSoftnessX))
  338. {
  339. EditorGUILayout.Space();
  340. DoFloat("_MaskSoftnessX", "Softness X");
  341. DoFloat("_MaskSoftnessY", "Softness Y");
  342. DoVector("_ClipRect", "Clip Rect", s_LbrtVectorLabels);
  343. }
  344. if (m_Material.HasProperty(ShaderUtilities.ID_StencilID))
  345. {
  346. EditorGUILayout.Space();
  347. DoFloat("_Stencil", "Stencil ID");
  348. DoFloat("_StencilComp", "Stencil Comp");
  349. }
  350. EditorGUILayout.Space();
  351. EditorGUI.BeginChangeCheck();
  352. bool useRatios = EditorGUILayout.Toggle("Use Ratios", !m_Material.IsKeywordEnabled("RATIOS_OFF"));
  353. if (EditorGUI.EndChangeCheck())
  354. {
  355. m_Editor.RegisterPropertyChangeUndo("Use Ratios");
  356. if (useRatios)
  357. {
  358. m_Material.DisableKeyword("RATIOS_OFF");
  359. }
  360. else
  361. {
  362. m_Material.EnableKeyword("RATIOS_OFF");
  363. }
  364. }
  365. if (m_Material.HasProperty(ShaderUtilities.ShaderTag_CullMode))
  366. {
  367. EditorGUILayout.Space();
  368. DoPopup("_CullMode", "Cull Mode", s_CullingTypeLabels);
  369. }
  370. EditorGUILayout.Space();
  371. EditorGUI.BeginDisabledGroup(true);
  372. DoFloat("_ScaleRatioA", "Scale Ratio A");
  373. DoFloat("_ScaleRatioB", "Scale Ratio B");
  374. DoFloat("_ScaleRatioC", "Scale Ratio C");
  375. EditorGUI.EndDisabledGroup();
  376. EditorGUI.indentLevel -= 1;
  377. EditorGUILayout.Space();
  378. }
  379. void DoMaskSubgroup()
  380. {
  381. DoVector("_MaskCoord", "Mask Bounds", s_XywhVectorLabels);
  382. if (Selection.activeGameObject != null)
  383. {
  384. Renderer renderer = Selection.activeGameObject.GetComponent<Renderer>();
  385. if (renderer != null)
  386. {
  387. Rect rect = EditorGUILayout.GetControlRect();
  388. rect.x += EditorGUIUtility.labelWidth;
  389. rect.width -= EditorGUIUtility.labelWidth;
  390. if (GUI.Button(rect, "Match Renderer Bounds"))
  391. {
  392. FindProperty("_MaskCoord", m_Properties).vectorValue = new Vector4(
  393. 0,
  394. 0,
  395. Mathf.Round(renderer.bounds.extents.x * 1000) / 1000,
  396. Mathf.Round(renderer.bounds.extents.y * 1000) / 1000
  397. );
  398. }
  399. }
  400. }
  401. if (s_MaskFeature.State == 1)
  402. {
  403. DoFloat("_MaskSoftnessX", "Softness X");
  404. DoFloat("_MaskSoftnessY", "Softness Y");
  405. }
  406. }
  407. void DoMaskTexSubgroup()
  408. {
  409. EditorGUILayout.Space();
  410. DoTexture2D("_MaskTex", "Mask Texture");
  411. DoToggle("_MaskInverse", "Inverse Mask");
  412. DoColor("_MaskEdgeColor", "Edge Color");
  413. DoSlider("_MaskEdgeSoftness", "Edge Softness");
  414. DoSlider("_MaskWipeControl", "Wipe Position");
  415. DoFloat("_MaskSoftnessX", "Softness X");
  416. DoFloat("_MaskSoftnessY", "Softness Y");
  417. DoVector("_ClipRect", "Clip Rect", s_LbrtVectorLabels);
  418. }
  419. }
  420. }