No Description
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.

TMP_SDFShaderGUI.cs 16KB

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