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.

438 lines
14 KiB

  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. DoSlider("_PerspectiveFilter", "Perspective Filter");
  284. EditorGUILayout.Space();
  285. DoFloat("_VertexOffsetX", "Offset X");
  286. DoFloat("_VertexOffsetY", "Offset Y");
  287. if (m_Material.HasProperty(ShaderUtilities.ID_MaskCoord))
  288. {
  289. EditorGUILayout.Space();
  290. s_MaskFeature.ReadState(m_Material);
  291. s_MaskFeature.DoPopup(m_Editor, m_Material);
  292. if (s_MaskFeature.Active)
  293. {
  294. DoMaskSubgroup();
  295. }
  296. EditorGUILayout.Space();
  297. DoVector("_ClipRect", "Clip Rect", s_LbrtVectorLabels);
  298. }
  299. else if (m_Material.HasProperty("_MaskTex"))
  300. {
  301. DoMaskTexSubgroup();
  302. }
  303. else if (m_Material.HasProperty(ShaderUtilities.ID_MaskSoftnessX))
  304. {
  305. EditorGUILayout.Space();
  306. DoFloat("_MaskSoftnessX", "Softness X");
  307. DoFloat("_MaskSoftnessY", "Softness Y");
  308. DoVector("_ClipRect", "Clip Rect", s_LbrtVectorLabels);
  309. }
  310. if (m_Material.HasProperty(ShaderUtilities.ID_StencilID))
  311. {
  312. EditorGUILayout.Space();
  313. DoFloat("_Stencil", "Stencil ID");
  314. DoFloat("_StencilComp", "Stencil Comp");
  315. }
  316. EditorGUILayout.Space();
  317. EditorGUI.BeginChangeCheck();
  318. bool useRatios = EditorGUILayout.Toggle("Use Ratios", !m_Material.IsKeywordEnabled("RATIOS_OFF"));
  319. if (EditorGUI.EndChangeCheck())
  320. {
  321. m_Editor.RegisterPropertyChangeUndo("Use Ratios");
  322. if (useRatios)
  323. {
  324. m_Material.DisableKeyword("RATIOS_OFF");
  325. }
  326. else
  327. {
  328. m_Material.EnableKeyword("RATIOS_OFF");
  329. }
  330. }
  331. EditorGUI.BeginDisabledGroup(true);
  332. DoFloat("_ScaleRatioA", "Scale Ratio A");
  333. DoFloat("_ScaleRatioB", "Scale Ratio B");
  334. DoFloat("_ScaleRatioC", "Scale Ratio C");
  335. EditorGUI.EndDisabledGroup();
  336. EditorGUI.indentLevel -= 1;
  337. EditorGUILayout.Space();
  338. }
  339. void DoMaskSubgroup()
  340. {
  341. DoVector("_MaskCoord", "Mask Bounds", s_XywhVectorLabels);
  342. if (Selection.activeGameObject != null)
  343. {
  344. Renderer renderer = Selection.activeGameObject.GetComponent<Renderer>();
  345. if (renderer != null)
  346. {
  347. Rect rect = EditorGUILayout.GetControlRect();
  348. rect.x += EditorGUIUtility.labelWidth;
  349. rect.width -= EditorGUIUtility.labelWidth;
  350. if (GUI.Button(rect, "Match Renderer Bounds"))
  351. {
  352. FindProperty("_MaskCoord", m_Properties).vectorValue = new Vector4(
  353. 0,
  354. 0,
  355. Mathf.Round(renderer.bounds.extents.x * 1000) / 1000,
  356. Mathf.Round(renderer.bounds.extents.y * 1000) / 1000
  357. );
  358. }
  359. }
  360. }
  361. if (s_MaskFeature.State == 1)
  362. {
  363. DoFloat("_MaskSoftnessX", "Softness X");
  364. DoFloat("_MaskSoftnessY", "Softness Y");
  365. }
  366. }
  367. void DoMaskTexSubgroup()
  368. {
  369. EditorGUILayout.Space();
  370. DoTexture2D("_MaskTex", "Mask Texture");
  371. DoToggle("_MaskInverse", "Inverse Mask");
  372. DoColor("_MaskEdgeColor", "Edge Color");
  373. DoSlider("_MaskEdgeSoftness", "Edge Softness");
  374. DoSlider("_MaskWipeControl", "Wipe Position");
  375. DoFloat("_MaskSoftnessX", "Softness X");
  376. DoFloat("_MaskSoftnessY", "Softness Y");
  377. DoVector("_ClipRect", "Clip Rect", s_LbrtVectorLabels);
  378. }
  379. }
  380. }