暫無描述
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_BaseShaderGUI.cs 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. float objFieldSize = 60f;
  230. bool smallLayout = EditorGUIUtility.currentViewWidth <= 440f && (withTilingOffset || speedNames != null);
  231. float controlHeight = smallLayout ? objFieldSize * 2 : objFieldSize;
  232. MaterialProperty property = FindProperty(name, m_Properties);
  233. m_Editor.BeginAnimatedCheck(Rect.zero, property);
  234. Rect rect = EditorGUILayout.GetControlRect(true, controlHeight);
  235. float totalWidth = rect.width;
  236. rect.width = EditorGUIUtility.labelWidth + objFieldSize;
  237. rect.height = objFieldSize;
  238. s_TempLabel.text = label;
  239. EditorGUI.BeginChangeCheck();
  240. Object tex = EditorGUI.ObjectField(rect, s_TempLabel, property.textureValue, type, false);
  241. if (EditorGUI.EndChangeCheck())
  242. {
  243. property.textureValue = tex as Texture;
  244. }
  245. float additionalHeight = controlHeight - objFieldSize;
  246. float xOffset = smallLayout ? rect.width - objFieldSize : rect.width;
  247. rect.y += additionalHeight;
  248. rect.x += xOffset;
  249. rect.width = totalWidth - xOffset;
  250. rect.height = EditorGUIUtility.singleLineHeight;
  251. if (withTilingOffset)
  252. {
  253. DoTilingOffset(rect, property);
  254. rect.y += (rect.height + 2f) * 2f;
  255. }
  256. m_Editor.EndAnimatedCheck();
  257. if (speedNames != null)
  258. {
  259. DoUVSpeed(rect, speedNames);
  260. }
  261. }
  262. void DoTilingOffset(Rect rect, MaterialProperty property)
  263. {
  264. float labelWidth = EditorGUIUtility.labelWidth;
  265. int indentLevel = EditorGUI.indentLevel;
  266. EditorGUI.indentLevel = 0;
  267. EditorGUIUtility.labelWidth = Mathf.Min(37f, rect.width * 0.40f);
  268. Vector4 vector = property.textureScaleAndOffset;
  269. bool changed = false;
  270. float[] values = s_TempFloats[2];
  271. s_TempLabel.text = "Tiling";
  272. Rect vectorRect = EditorGUI.PrefixLabel(rect, s_TempLabel);
  273. values[0] = vector.x;
  274. values[1] = vector.y;
  275. EditorGUI.BeginChangeCheck();
  276. EditorGUI.MultiFloatField(vectorRect, s_XywhVectorLabels, values);
  277. if (EditorGUI.EndChangeCheck())
  278. {
  279. vector.x = values[0];
  280. vector.y = values[1];
  281. changed = true;
  282. }
  283. rect.y += rect.height + 2f;
  284. s_TempLabel.text = "Offset";
  285. vectorRect = EditorGUI.PrefixLabel(rect, s_TempLabel);
  286. values[0] = vector.z;
  287. values[1] = vector.w;
  288. EditorGUI.BeginChangeCheck();
  289. EditorGUI.MultiFloatField(vectorRect, s_XywhVectorLabels, values);
  290. if (EditorGUI.EndChangeCheck())
  291. {
  292. vector.z = values[0];
  293. vector.w = values[1];
  294. changed = true;
  295. }
  296. if (changed)
  297. {
  298. property.textureScaleAndOffset = vector;
  299. }
  300. EditorGUIUtility.labelWidth = labelWidth;
  301. EditorGUI.indentLevel = indentLevel;
  302. }
  303. protected void DoUVSpeed(Rect rect, string[] names)
  304. {
  305. float labelWidth = EditorGUIUtility.labelWidth;
  306. int indentLevel = EditorGUI.indentLevel;
  307. EditorGUI.indentLevel = 0;
  308. EditorGUIUtility.labelWidth = Mathf.Min(37f, rect.width * 0.40f);
  309. s_TempLabel.text = "Speed";
  310. rect = EditorGUI.PrefixLabel(rect, s_TempLabel);
  311. EditorGUIUtility.labelWidth = 10f;
  312. rect.width = rect.width * 0.5f - 2f;
  313. if (names.Length == 1)
  314. {
  315. DoFloat2(rect, names[0]);
  316. }
  317. else
  318. {
  319. DoFloat(rect, names[0], "X");
  320. rect.x += rect.width + 4f;
  321. DoFloat(rect, names[1], "Y");
  322. }
  323. EditorGUIUtility.labelWidth = labelWidth;
  324. EditorGUI.indentLevel = indentLevel;
  325. }
  326. protected void DoToggle(string name, string label)
  327. {
  328. MaterialProperty property = BeginProperty(name);
  329. s_TempLabel.text = label;
  330. bool value = EditorGUILayout.Toggle(s_TempLabel, property.floatValue == 1f);
  331. if (EndProperty())
  332. {
  333. property.floatValue = value ? 1f : 0f;
  334. }
  335. }
  336. protected void DoFloat(string name, string label)
  337. {
  338. MaterialProperty property = BeginProperty(name);
  339. Rect rect = EditorGUILayout.GetControlRect();
  340. rect.width = EditorGUIUtility.labelWidth + 55f;
  341. s_TempLabel.text = label;
  342. float value = EditorGUI.FloatField(rect, s_TempLabel, property.floatValue);
  343. if (EndProperty())
  344. {
  345. property.floatValue = value;
  346. }
  347. }
  348. protected void DoColor(string name, string label)
  349. {
  350. MaterialProperty property = BeginProperty(name);
  351. s_TempLabel.text = label;
  352. Color value = EditorGUI.ColorField(EditorGUILayout.GetControlRect(), s_TempLabel, property.colorValue, false, true, true);
  353. if (EndProperty())
  354. {
  355. property.colorValue = value;
  356. }
  357. }
  358. void DoFloat(Rect rect, string name, string label)
  359. {
  360. MaterialProperty property = BeginProperty(name);
  361. s_TempLabel.text = label;
  362. float value = EditorGUI.FloatField(rect, s_TempLabel, property.floatValue);
  363. if (EndProperty())
  364. {
  365. property.floatValue = value;
  366. }
  367. }
  368. void DoFloat2(Rect rect, string name)
  369. {
  370. MaterialProperty property = BeginProperty(name);
  371. float x = EditorGUI.FloatField(rect, "X", property.vectorValue.x);
  372. rect.x += rect.width + 4f;
  373. float y = EditorGUI.FloatField(rect, "Y", property.vectorValue.y);
  374. if (EndProperty())
  375. {
  376. property.vectorValue = new Vector2(x, y);
  377. }
  378. }
  379. protected void DoSlider(string name, string label)
  380. {
  381. MaterialProperty property = BeginProperty(name);
  382. Vector2 range = property.rangeLimits;
  383. s_TempLabel.text = label;
  384. float value = EditorGUI.Slider(EditorGUILayout.GetControlRect(), s_TempLabel, property.floatValue, range.x, range.y);
  385. if (EndProperty())
  386. {
  387. property.floatValue = value;
  388. }
  389. }
  390. protected void DoSlider(string propertyName, string propertyField, string label)
  391. {
  392. MaterialProperty property = BeginProperty(propertyName);
  393. Vector2 range = property.rangeLimits;
  394. s_TempLabel.text = label;
  395. Vector4 value = property.vectorValue;
  396. switch (propertyField)
  397. {
  398. case "X":
  399. value.x = EditorGUI.Slider(EditorGUILayout.GetControlRect(), s_TempLabel, value.x, range.x, range.y);
  400. break;
  401. case "Y":
  402. value.y = EditorGUI.Slider(EditorGUILayout.GetControlRect(), s_TempLabel, value.y, range.x, range.y);
  403. break;
  404. case "Z":
  405. value.z = EditorGUI.Slider(EditorGUILayout.GetControlRect(), s_TempLabel, value.z, range.x, range.y);
  406. break;
  407. case "W":
  408. value.w = EditorGUI.Slider(EditorGUILayout.GetControlRect(), s_TempLabel, value.w, range.x, range.y);
  409. break;
  410. }
  411. if (EndProperty())
  412. {
  413. property.vectorValue = value;
  414. }
  415. }
  416. protected void DoVector2(string name, string label)
  417. {
  418. MaterialProperty property = BeginProperty(name);
  419. s_TempLabel.text = label;
  420. Vector4 value = EditorGUILayout.Vector3Field(s_TempLabel, property.vectorValue);
  421. if (EndProperty())
  422. {
  423. property.vectorValue = value;
  424. }
  425. }
  426. protected void DoVector3(string name, string label)
  427. {
  428. MaterialProperty property = BeginProperty(name);
  429. s_TempLabel.text = label;
  430. Vector4 value = EditorGUILayout.Vector3Field(s_TempLabel, property.vectorValue);
  431. if (EndProperty())
  432. {
  433. property.vectorValue = value;
  434. }
  435. }
  436. protected void DoVector(string name, string label, GUIContent[] subLabels)
  437. {
  438. MaterialProperty property = BeginProperty(name);
  439. Rect rect = EditorGUILayout.GetControlRect();
  440. s_TempLabel.text = label;
  441. rect = EditorGUI.PrefixLabel(rect, s_TempLabel);
  442. Vector4 vector = property.vectorValue;
  443. float[] values = s_TempFloats[subLabels.Length];
  444. for (int i = 0; i < subLabels.Length; i++)
  445. {
  446. values[i] = vector[i];
  447. }
  448. EditorGUI.MultiFloatField(rect, subLabels, values);
  449. if (EndProperty())
  450. {
  451. for (int i = 0; i < subLabels.Length; i++)
  452. {
  453. vector[i] = values[i];
  454. }
  455. property.vectorValue = vector;
  456. }
  457. }
  458. void DoDragAndDropBegin()
  459. {
  460. m_DragAndDropMinY = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true)).y;
  461. }
  462. void DoDragAndDropEnd()
  463. {
  464. Rect rect = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true));
  465. Event evt = Event.current;
  466. if (evt.type == EventType.DragUpdated)
  467. {
  468. DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
  469. evt.Use();
  470. }
  471. else if (
  472. evt.type == EventType.DragPerform &&
  473. Rect.MinMaxRect(rect.xMin, m_DragAndDropMinY, rect.xMax, rect.yMax).Contains(evt.mousePosition)
  474. )
  475. {
  476. DragAndDrop.AcceptDrag();
  477. evt.Use();
  478. Material droppedMaterial = DragAndDrop.objectReferences[0] as Material;
  479. if (droppedMaterial && droppedMaterial != m_Material)
  480. {
  481. PerformDrop(droppedMaterial);
  482. }
  483. }
  484. }
  485. void PerformDrop(Material droppedMaterial)
  486. {
  487. Texture droppedTex = droppedMaterial.GetTexture(ShaderUtilities.ID_MainTex);
  488. if (!droppedTex)
  489. {
  490. return;
  491. }
  492. Texture currentTex = m_Material.GetTexture(ShaderUtilities.ID_MainTex);
  493. TMP_FontAsset requiredFontAsset = null;
  494. if (droppedTex != currentTex)
  495. {
  496. requiredFontAsset = TMP_EditorUtility.FindMatchingFontAsset(droppedMaterial);
  497. if (!requiredFontAsset)
  498. {
  499. return;
  500. }
  501. }
  502. foreach (GameObject o in Selection.gameObjects)
  503. {
  504. if (requiredFontAsset)
  505. {
  506. TMP_Text textComponent = o.GetComponent<TMP_Text>();
  507. if (textComponent)
  508. {
  509. Undo.RecordObject(textComponent, "Font Asset Change");
  510. textComponent.font = requiredFontAsset;
  511. }
  512. }
  513. TMPro_EventManager.ON_DRAG_AND_DROP_MATERIAL_CHANGED(o, m_Material, droppedMaterial);
  514. EditorUtility.SetDirty(o);
  515. }
  516. }
  517. }
  518. }