TMP_BaseShaderGUI.cs 18 KB

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