TMP_BaseShaderGUI.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace TMPro.EditorUtilities
  4. {
  5. /// <summary>Base class for TextMesh Pro shader GUIs.</summary>
  6. public abstract class TMP_BaseShaderGUI : ShaderGUI
  7. {
  8. /// <summary>Representation of a #pragma shader_feature.</summary>
  9. /// <description>It is assumed that the first feature option is for no keyword (underscores).</description>
  10. protected class ShaderFeature
  11. {
  12. public string undoLabel;
  13. public GUIContent label;
  14. /// <summary>The keyword labels, for display. Include the no-keyword as the first option.</summary>
  15. public GUIContent[] keywordLabels;
  16. /// <summary>The shader keywords. Exclude the no-keyword option.</summary>
  17. public string[] keywords;
  18. int m_State;
  19. public bool Active
  20. {
  21. get { return m_State >= 0; }
  22. }
  23. public int State
  24. {
  25. get { return m_State; }
  26. }
  27. public void ReadState(Material material)
  28. {
  29. for (int i = 0; i < keywords.Length; i++)
  30. {
  31. if (material.IsKeywordEnabled(keywords[i]))
  32. {
  33. m_State = i;
  34. return;
  35. }
  36. }
  37. m_State = -1;
  38. }
  39. public void SetActive(bool active, Material material)
  40. {
  41. m_State = active ? 0 : -1;
  42. SetStateKeywords(material);
  43. }
  44. public void DoPopup(MaterialEditor editor, Material material)
  45. {
  46. EditorGUI.BeginChangeCheck();
  47. int selection = EditorGUILayout.Popup(label, m_State + 1, keywordLabels);
  48. if (EditorGUI.EndChangeCheck())
  49. {
  50. m_State = selection - 1;
  51. editor.RegisterPropertyChangeUndo(undoLabel);
  52. SetStateKeywords(material);
  53. }
  54. }
  55. void SetStateKeywords(Material material)
  56. {
  57. for (int i = 0; i < keywords.Length; i++)
  58. {
  59. if (i == m_State)
  60. {
  61. material.EnableKeyword(keywords[i]);
  62. }
  63. else
  64. {
  65. material.DisableKeyword(keywords[i]);
  66. }
  67. }
  68. }
  69. }
  70. static GUIContent s_TempLabel = new GUIContent();
  71. protected static bool s_DebugExtended;
  72. static int s_UndoRedoCount, s_LastSeenUndoRedoCount;
  73. static float[][] s_TempFloats =
  74. {
  75. null, new float[1], new float[2], new float[3], new float[4]
  76. };
  77. protected static GUIContent[] s_XywhVectorLabels =
  78. {
  79. new GUIContent("X"),
  80. new GUIContent("Y"),
  81. new GUIContent("W", "Width"),
  82. new GUIContent("H", "Height")
  83. };
  84. protected static GUIContent[] s_LbrtVectorLabels =
  85. {
  86. new GUIContent("L", "Left"),
  87. new GUIContent("B", "Bottom"),
  88. new GUIContent("R", "Right"),
  89. new GUIContent("T", "Top")
  90. };
  91. protected static GUIContent[] s_CullingTypeLabels =
  92. {
  93. new GUIContent("Off"),
  94. new GUIContent("Front"),
  95. new GUIContent("Back")
  96. };
  97. static TMP_BaseShaderGUI()
  98. {
  99. // Keep track of how many undo/redo events happened.
  100. Undo.undoRedoPerformed += () => s_UndoRedoCount += 1;
  101. }
  102. bool m_IsNewGUI = true;
  103. float m_DragAndDropMinY;
  104. protected MaterialEditor m_Editor;
  105. protected Material m_Material;
  106. protected MaterialProperty[] m_Properties;
  107. void PrepareGUI()
  108. {
  109. m_IsNewGUI = false;
  110. ShaderUtilities.GetShaderPropertyIDs();
  111. // New GUI just got constructed. This happens in response to a selection,
  112. // but also after undo/redo events.
  113. if (s_LastSeenUndoRedoCount != s_UndoRedoCount)
  114. {
  115. // There's been at least one undo/redo since the last time this GUI got constructed.
  116. // Maybe the undo/redo was for this material? Assume that is was.
  117. TMPro_EventManager.ON_MATERIAL_PROPERTY_CHANGED(true, m_Material as Material);
  118. }
  119. s_LastSeenUndoRedoCount = s_UndoRedoCount;
  120. }
  121. public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
  122. {
  123. m_Editor = materialEditor;
  124. m_Material = materialEditor.target as Material;
  125. this.m_Properties = properties;
  126. if (m_IsNewGUI)
  127. {
  128. PrepareGUI();
  129. }
  130. DoDragAndDropBegin();
  131. EditorGUI.BeginChangeCheck();
  132. DoGUI();
  133. if (EditorGUI.EndChangeCheck())
  134. {
  135. TMPro_EventManager.ON_MATERIAL_PROPERTY_CHANGED(true, m_Material);
  136. }
  137. DoDragAndDropEnd();
  138. }
  139. /// <summary>Override this method to create the specific shader GUI.</summary>
  140. protected abstract void DoGUI();
  141. static string[] s_PanelStateLabel = new string[] { "\t- <i>Click to collapse</i> -", "\t- <i>Click to expand</i> -" };
  142. protected bool BeginPanel(string panel, bool expanded)
  143. {
  144. EditorGUI.indentLevel = 0;
  145. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  146. Rect r = EditorGUI.IndentedRect(GUILayoutUtility.GetRect(20, 18));
  147. r.x += 20;
  148. r.width += 6;
  149. bool enabled = GUI.enabled;
  150. GUI.enabled = true;
  151. expanded = TMP_EditorUtility.EditorToggle(r, expanded, new GUIContent(panel), TMP_UIStyleManager.panelTitle);
  152. r.width -= 30;
  153. EditorGUI.LabelField(r, new GUIContent(expanded ? s_PanelStateLabel[0] : s_PanelStateLabel[1]), TMP_UIStyleManager.rightLabel);
  154. GUI.enabled = enabled;
  155. EditorGUI.indentLevel += 1;
  156. EditorGUI.BeginDisabledGroup(false);
  157. return expanded;
  158. }
  159. protected bool BeginPanel(string panel, ShaderFeature feature, bool expanded, bool readState = true)
  160. {
  161. EditorGUI.indentLevel = 0;
  162. if (readState)
  163. {
  164. feature.ReadState(m_Material);
  165. }
  166. EditorGUI.BeginChangeCheck();
  167. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  168. GUILayout.BeginHorizontal();
  169. Rect r = EditorGUI.IndentedRect(GUILayoutUtility.GetRect(20, 20, GUILayout.Width(20f)));
  170. bool active = EditorGUI.Toggle(r, feature.Active);
  171. if (EditorGUI.EndChangeCheck())
  172. {
  173. m_Editor.RegisterPropertyChangeUndo(feature.undoLabel);
  174. feature.SetActive(active, m_Material);
  175. }
  176. r = EditorGUI.IndentedRect(GUILayoutUtility.GetRect(20, 18));
  177. r.width += 6;
  178. bool enabled = GUI.enabled;
  179. GUI.enabled = true;
  180. expanded = TMP_EditorUtility.EditorToggle(r, expanded, new GUIContent(panel), TMP_UIStyleManager.panelTitle);
  181. r.width -= 10;
  182. EditorGUI.LabelField(r, new GUIContent(expanded ? s_PanelStateLabel[0] : s_PanelStateLabel[1]), TMP_UIStyleManager.rightLabel);
  183. GUI.enabled = enabled;
  184. GUILayout.EndHorizontal();
  185. EditorGUI.indentLevel += 1;
  186. EditorGUI.BeginDisabledGroup(!active);
  187. return expanded;
  188. }
  189. public void EndPanel()
  190. {
  191. EditorGUI.EndDisabledGroup();
  192. EditorGUI.indentLevel -= 1;
  193. EditorGUILayout.EndVertical();
  194. }
  195. MaterialProperty BeginProperty(string name)
  196. {
  197. MaterialProperty property = FindProperty(name, m_Properties);
  198. EditorGUI.BeginChangeCheck();
  199. EditorGUI.showMixedValue = property.hasMixedValue;
  200. m_Editor.BeginAnimatedCheck(Rect.zero, property);
  201. return property;
  202. }
  203. bool EndProperty()
  204. {
  205. m_Editor.EndAnimatedCheck();
  206. EditorGUI.showMixedValue = false;
  207. return EditorGUI.EndChangeCheck();
  208. }
  209. protected void DoPopup(string name, string label, GUIContent[] options)
  210. {
  211. MaterialProperty property = BeginProperty(name);
  212. s_TempLabel.text = label;
  213. int index = EditorGUILayout.Popup(s_TempLabel, (int)property.floatValue, options);
  214. if (EndProperty())
  215. {
  216. property.floatValue = index;
  217. }
  218. }
  219. protected void DoCubeMap(string name, string label)
  220. {
  221. DoTexture(name, label, typeof(Cubemap));
  222. }
  223. protected void DoTexture2D(string name, string label, bool withTilingOffset = false, string[] speedNames = null)
  224. {
  225. DoTexture(name, label, typeof(Texture2D), withTilingOffset, speedNames);
  226. }
  227. void DoTexture(string name, string label, System.Type type, bool withTilingOffset = false, string[] speedNames = null)
  228. {
  229. MaterialProperty property = FindProperty(name, m_Properties);
  230. m_Editor.BeginAnimatedCheck(Rect.zero, property);
  231. Rect rect = EditorGUILayout.GetControlRect(true, 60f);
  232. float totalWidth = rect.width;
  233. rect.width = EditorGUIUtility.labelWidth + 60f;
  234. s_TempLabel.text = label;
  235. EditorGUI.BeginChangeCheck();
  236. Object tex = EditorGUI.ObjectField(rect, s_TempLabel, property.textureValue, type, false);
  237. if (EditorGUI.EndChangeCheck())
  238. {
  239. property.textureValue = tex as Texture;
  240. }
  241. rect.x += rect.width + 4f;
  242. rect.width = totalWidth - rect.width - 4f;
  243. rect.height = EditorGUIUtility.singleLineHeight;
  244. if (withTilingOffset)
  245. {
  246. DoTilingOffset(rect, property);
  247. rect.y += (rect.height + 2f) * 2f;
  248. }
  249. m_Editor.EndAnimatedCheck();
  250. if (speedNames != null)
  251. {
  252. DoUVSpeed(rect, speedNames);
  253. }
  254. }
  255. void DoTilingOffset(Rect rect, MaterialProperty property)
  256. {
  257. float labelWidth = EditorGUIUtility.labelWidth;
  258. int indentLevel = EditorGUI.indentLevel;
  259. EditorGUI.indentLevel = 0;
  260. EditorGUIUtility.labelWidth = Mathf.Min(40f, rect.width * 0.20f);
  261. Vector4 vector = property.textureScaleAndOffset;
  262. bool changed = false;
  263. float[] values = s_TempFloats[2];
  264. s_TempLabel.text = "Tiling";
  265. Rect vectorRect = EditorGUI.PrefixLabel(rect, s_TempLabel);
  266. values[0] = vector.x;
  267. values[1] = vector.y;
  268. EditorGUI.BeginChangeCheck();
  269. EditorGUI.MultiFloatField(vectorRect, s_XywhVectorLabels, values);
  270. if (EditorGUI.EndChangeCheck())
  271. {
  272. vector.x = values[0];
  273. vector.y = values[1];
  274. changed = true;
  275. }
  276. rect.y += rect.height + 2f;
  277. s_TempLabel.text = "Offset";
  278. vectorRect = EditorGUI.PrefixLabel(rect, s_TempLabel);
  279. values[0] = vector.z;
  280. values[1] = vector.w;
  281. EditorGUI.BeginChangeCheck();
  282. EditorGUI.MultiFloatField(vectorRect, s_XywhVectorLabels, values);
  283. if (EditorGUI.EndChangeCheck())
  284. {
  285. vector.z = values[0];
  286. vector.w = values[1];
  287. changed = true;
  288. }
  289. if (changed)
  290. {
  291. property.textureScaleAndOffset = vector;
  292. }
  293. EditorGUIUtility.labelWidth = labelWidth;
  294. EditorGUI.indentLevel = indentLevel;
  295. }
  296. protected void DoUVSpeed(Rect rect, string[] names)
  297. {
  298. float labelWidth = EditorGUIUtility.labelWidth;
  299. int indentLevel = EditorGUI.indentLevel;
  300. EditorGUI.indentLevel = 0;
  301. EditorGUIUtility.labelWidth = Mathf.Min(40f, rect.width * 0.20f);
  302. s_TempLabel.text = "Speed";
  303. rect = EditorGUI.PrefixLabel(rect, s_TempLabel);
  304. EditorGUIUtility.labelWidth = 13f;
  305. rect.width = rect.width * 0.5f - 1f;
  306. DoFloat(rect, names[0], "X");
  307. rect.x += rect.width + 2f;
  308. DoFloat(rect, names[1], "Y");
  309. EditorGUIUtility.labelWidth = labelWidth;
  310. EditorGUI.indentLevel = indentLevel;
  311. }
  312. protected void DoToggle(string name, string label)
  313. {
  314. MaterialProperty property = BeginProperty(name);
  315. s_TempLabel.text = label;
  316. bool value = EditorGUILayout.Toggle(s_TempLabel, property.floatValue == 1f);
  317. if (EndProperty())
  318. {
  319. property.floatValue = value ? 1f : 0f;
  320. }
  321. }
  322. protected void DoFloat(string name, string label)
  323. {
  324. MaterialProperty property = BeginProperty(name);
  325. Rect rect = EditorGUILayout.GetControlRect();
  326. rect.width = EditorGUIUtility.labelWidth + 55f;
  327. s_TempLabel.text = label;
  328. float value = EditorGUI.FloatField(rect, s_TempLabel, property.floatValue);
  329. if (EndProperty())
  330. {
  331. property.floatValue = value;
  332. }
  333. }
  334. protected void DoColor(string name, string label)
  335. {
  336. MaterialProperty property = BeginProperty(name);
  337. s_TempLabel.text = label;
  338. Color value = EditorGUI.ColorField(EditorGUILayout.GetControlRect(), s_TempLabel, property.colorValue, false, true, true);
  339. if (EndProperty())
  340. {
  341. property.colorValue = value;
  342. }
  343. }
  344. void DoFloat(Rect rect, string name, string label)
  345. {
  346. MaterialProperty property = BeginProperty(name);
  347. s_TempLabel.text = label;
  348. float value = EditorGUI.FloatField(rect, s_TempLabel, property.floatValue);
  349. if (EndProperty())
  350. {
  351. property.floatValue = value;
  352. }
  353. }
  354. protected void DoSlider(string name, string label)
  355. {
  356. MaterialProperty property = BeginProperty(name);
  357. Vector2 range = property.rangeLimits;
  358. s_TempLabel.text = label;
  359. float value = EditorGUI.Slider(
  360. EditorGUILayout.GetControlRect(), s_TempLabel, property.floatValue, range.x, range.y
  361. );
  362. if (EndProperty())
  363. {
  364. property.floatValue = value;
  365. }
  366. }
  367. protected void DoVector3(string name, string label)
  368. {
  369. MaterialProperty property = BeginProperty(name);
  370. s_TempLabel.text = label;
  371. Vector4 value = EditorGUILayout.Vector3Field(s_TempLabel, property.vectorValue);
  372. if (EndProperty())
  373. {
  374. property.vectorValue = value;
  375. }
  376. }
  377. protected void DoVector(string name, string label, GUIContent[] subLabels)
  378. {
  379. MaterialProperty property = BeginProperty(name);
  380. Rect rect = EditorGUILayout.GetControlRect();
  381. s_TempLabel.text = label;
  382. rect = EditorGUI.PrefixLabel(rect, s_TempLabel);
  383. Vector4 vector = property.vectorValue;
  384. float[] values = s_TempFloats[subLabels.Length];
  385. for (int i = 0; i < subLabels.Length; i++)
  386. {
  387. values[i] = vector[i];
  388. }
  389. EditorGUI.MultiFloatField(rect, subLabels, values);
  390. if (EndProperty())
  391. {
  392. for (int i = 0; i < subLabels.Length; i++)
  393. {
  394. vector[i] = values[i];
  395. }
  396. property.vectorValue = vector;
  397. }
  398. }
  399. void DoDragAndDropBegin()
  400. {
  401. m_DragAndDropMinY = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true)).y;
  402. }
  403. void DoDragAndDropEnd()
  404. {
  405. Rect rect = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true));
  406. Event evt = Event.current;
  407. if (evt.type == EventType.DragUpdated)
  408. {
  409. DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
  410. evt.Use();
  411. }
  412. else if (
  413. evt.type == EventType.DragPerform &&
  414. Rect.MinMaxRect(rect.xMin, m_DragAndDropMinY, rect.xMax, rect.yMax).Contains(evt.mousePosition)
  415. )
  416. {
  417. DragAndDrop.AcceptDrag();
  418. evt.Use();
  419. Material droppedMaterial = DragAndDrop.objectReferences[0] as Material;
  420. if (droppedMaterial && droppedMaterial != m_Material)
  421. {
  422. PerformDrop(droppedMaterial);
  423. }
  424. }
  425. }
  426. void PerformDrop(Material droppedMaterial)
  427. {
  428. Texture droppedTex = droppedMaterial.GetTexture(ShaderUtilities.ID_MainTex);
  429. if (!droppedTex)
  430. {
  431. return;
  432. }
  433. Texture currentTex = m_Material.GetTexture(ShaderUtilities.ID_MainTex);
  434. TMP_FontAsset requiredFontAsset = null;
  435. if (droppedTex != currentTex)
  436. {
  437. requiredFontAsset = TMP_EditorUtility.FindMatchingFontAsset(droppedMaterial);
  438. if (!requiredFontAsset)
  439. {
  440. return;
  441. }
  442. }
  443. foreach (GameObject o in Selection.gameObjects)
  444. {
  445. if (requiredFontAsset)
  446. {
  447. TMP_Text textComponent = o.GetComponent<TMP_Text>();
  448. if (textComponent)
  449. {
  450. Undo.RecordObject(textComponent, "Font Asset Change");
  451. textComponent.font = requiredFontAsset;
  452. }
  453. }
  454. TMPro_EventManager.ON_DRAG_AND_DROP_MATERIAL_CHANGED(o, m_Material, droppedMaterial);
  455. EditorUtility.SetDirty(o);
  456. }
  457. }
  458. }
  459. }