LutifyBrowser.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // Lutify - Unity Asset
  2. // Copyright (c) 2015 - Thomas Hourdel
  3. // http://www.thomashourdel.com
  4. #if !(UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_5_0)
  5. #define UNITY_5_1_PLUS
  6. #endif
  7. using UnityEngine;
  8. using UnityEditor;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Reflection;
  14. public class LutifyBrowser : EditorWindow
  15. {
  16. public static LutifyBrowser inst;
  17. protected Lutify m_Lutify;
  18. protected Vector2 m_ScrollView = Vector2.zero;
  19. protected string m_BasePath = "";
  20. protected Dictionary<string, List<Texture2D>> m_Collections;
  21. protected string[] m_CollectionMenu;
  22. protected string[] m_CollectionNames;
  23. protected FilterMode[] m_CollectionFilters;
  24. protected int m_SelectedCollection = 0;
  25. protected Material m_MaterialGamma;
  26. protected Material m_MaterialLinear;
  27. protected Material PreviewMaterial
  28. {
  29. get
  30. {
  31. if (m_MaterialGamma == null)
  32. {
  33. m_MaterialGamma = new Material(Shader.Find("Hidden/Lutify-Preview-Gamma"));
  34. m_MaterialGamma.hideFlags = HideFlags.HideAndDontSave;
  35. }
  36. if (m_MaterialLinear == null)
  37. {
  38. m_MaterialLinear = new Material(Shader.Find("Hidden/Lutify-Preview-Linear"));
  39. m_MaterialLinear.hideFlags = HideFlags.HideAndDontSave;
  40. }
  41. if (m_Lutify != null)
  42. return m_Lutify.IsLinear ? m_MaterialLinear : m_MaterialGamma;
  43. return m_MaterialGamma;
  44. }
  45. }
  46. public static void Init(Lutify inst)
  47. {
  48. LutifyBrowser window = EditorWindow.GetWindow<LutifyBrowser>();
  49. window.Prepare(inst);
  50. window.autoRepaintOnSceneChange = true;
  51. window.minSize = new Vector2(370f, 200f);
  52. window.Show();
  53. }
  54. public static Texture2D LoadIcon()
  55. {
  56. string[] results = AssetDatabase.FindAssets("LutifyBrowser t:Script", null);
  57. if (results.Length > 0)
  58. {
  59. string p = AssetDatabase.GUIDToAssetPath(results[0]);
  60. p = Path.GetDirectoryName(p);
  61. p = p.Substring(0, p.LastIndexOf('/'));
  62. string path = Path.Combine(p, Path.Combine("Editor", "browser-icon.png"));
  63. return LoadAssetAt<Texture2D>(path);
  64. }
  65. return null;
  66. }
  67. void Prepare(Lutify inst)
  68. {
  69. m_Lutify = inst;
  70. m_SelectedCollection = m_Lutify._LastSelectedCategory;
  71. if (m_CollectionMenu == null || m_SelectedCollection >= m_CollectionMenu.Length)
  72. m_SelectedCollection = 0;
  73. }
  74. void OnEnable()
  75. {
  76. // Title icon
  77. #if UNITY_5_1_PLUS
  78. GUIContent windowTitle = new GUIContent(" Lutify", LoadIcon());
  79. titleContent = windowTitle;
  80. #else
  81. title = "Lutify";
  82. GUIContent windowTitle = InternalGUIUtility.GetTitleContent(this);
  83. if (windowTitle != null)
  84. {
  85. windowTitle.text = " Lutify";
  86. windowTitle.image = LoadIcon();
  87. }
  88. #endif
  89. inst = this;
  90. FetchLuts();
  91. }
  92. void OnDestroy()
  93. {
  94. inst = null;
  95. if (m_MaterialGamma)
  96. DestroyImmediate(m_MaterialGamma);
  97. if (m_MaterialLinear)
  98. DestroyImmediate(m_MaterialLinear);
  99. m_Collections.Clear();
  100. m_Collections = null;
  101. }
  102. public static T LoadAssetAt<T>(string path) where T : UnityEngine.Object
  103. {
  104. #if UNITY_5_1_PLUS
  105. return AssetDatabase.LoadAssetAtPath<T>(path);
  106. #else
  107. return Resources.LoadAssetAtPath<T>(path);
  108. #endif
  109. }
  110. void FetchLuts()
  111. {
  112. // Find the lut location
  113. string baseRelativePath = null;
  114. string baseAbsolutePath = null;
  115. string[] results = AssetDatabase.FindAssets("LutifyBrowser t:Script", null);
  116. if (results.Length > 0)
  117. {
  118. string p = AssetDatabase.GUIDToAssetPath(results[0]);
  119. p = Path.GetDirectoryName(p);
  120. p = p.Substring(0, p.LastIndexOf('/'));
  121. m_BasePath = Path.Combine(p, "Luts");
  122. baseRelativePath = Path.Combine(m_BasePath, "Standard");
  123. baseAbsolutePath = Path.Combine(Directory.GetParent(Application.dataPath).FullName, baseRelativePath);
  124. }
  125. else
  126. {
  127. // Should never happen but just in case
  128. Debug.LogError("Couldn't find the LutifyBrowser script");
  129. Close();
  130. }
  131. // Collection listing
  132. if (m_Collections != null)
  133. m_Collections.Clear();
  134. m_Collections = new Dictionary<string, List<Texture2D>>();
  135. string[] dirs = Directory.GetDirectories(baseAbsolutePath);
  136. foreach (string dir in dirs)
  137. {
  138. string[] files = Directory.GetFiles(dir, "*.png");
  139. Array.Sort(files, (x, y) => String.Compare(x, y, CultureInfo.CurrentCulture, CompareOptions.IgnoreSymbols));
  140. // Skip empty directories
  141. if (files.Length == 0)
  142. continue;
  143. string collection = Path.GetFileName(dir);
  144. List<Texture2D> content = null;
  145. foreach (string file in files)
  146. {
  147. string relativePath = Path.Combine(baseRelativePath, Path.Combine(collection, Path.GetFileName(file)));
  148. Texture2D lut = LoadAssetAt<Texture2D>(relativePath);
  149. if (lut == null)
  150. continue;
  151. if (content == null)
  152. content = new List<Texture2D>();
  153. content.Add(lut);
  154. }
  155. if (content.Count == 0)
  156. continue;
  157. m_Collections.Add(collection, content);
  158. }
  159. List<string> menu = new List<string>();
  160. List<string> names = new List<string>();
  161. List<FilterMode> filters = new List<FilterMode>();
  162. foreach (KeyValuePair<string, List<Texture2D>> entry in m_Collections)
  163. {
  164. string name = entry.Key;
  165. string displayName = name + " (" + entry.Value.Count + ")";
  166. menu.Add(displayName);
  167. names.Add(name);
  168. // Filter
  169. string filename = Path.Combine(baseAbsolutePath, Path.Combine(name, "Filtering.txt"));
  170. if (!File.Exists(filename))
  171. {
  172. filters.Add(FilterMode.Bilinear);
  173. }
  174. else
  175. {
  176. string txt = File.ReadAllText(filename);
  177. try
  178. {
  179. FilterMode mode = (FilterMode)Enum.Parse(typeof(FilterMode), txt, true);
  180. filters.Add(mode);
  181. }
  182. catch
  183. {
  184. filters.Add(FilterMode.Bilinear);
  185. }
  186. }
  187. }
  188. m_CollectionMenu = menu.ToArray();
  189. m_CollectionNames = names.ToArray();
  190. m_CollectionFilters = filters.ToArray();
  191. menu.Clear();
  192. names.Clear();
  193. }
  194. void Update()
  195. {
  196. if (m_Lutify == null)
  197. Close();
  198. }
  199. void OnGUI()
  200. {
  201. if (m_Lutify == null)
  202. return; // Can happen when Unity is restarted and the LUT browser is still opened
  203. // Header
  204. GUILayout.BeginHorizontal(EditorStyles.toolbar, GUILayout.ExpandWidth(true));
  205. {
  206. EditorGUI.BeginChangeCheck();
  207. m_SelectedCollection = EditorGUILayout.Popup(m_SelectedCollection, m_CollectionMenu, EditorStyles.toolbarPopup, GUILayout.MaxWidth(170f));
  208. if (EditorGUI.EndChangeCheck())
  209. {
  210. m_ScrollView = Vector2.zero;
  211. m_Lutify._LastSelectedCategory = m_SelectedCollection;
  212. }
  213. GUILayout.FlexibleSpace();
  214. EditorGUI.BeginChangeCheck();
  215. m_Lutify._ThumbWidth = Mathf.RoundToInt(GUILayout.HorizontalSlider(m_Lutify._ThumbWidth, 100f, 300f, new GUIStyle("preSlider"), new GUIStyle("preSliderThumb"), GUILayout.Width(64f)));
  216. if (EditorGUI.EndChangeCheck())
  217. InternalGUIUtility.RepaintGameView();
  218. if (GUILayout.Button("?", EditorStyles.toolbarButton))
  219. Application.OpenURL("https://www.thomashourdel.com/lutify/");
  220. }
  221. GUILayout.EndHorizontal();
  222. // Component check
  223. if (!m_Lutify.enabled)
  224. {
  225. EditorGUILayout.HelpBox("Lutify is disabled ! Please enable the component get the LUT browser to work.", MessageType.Error);
  226. if (GUILayout.Button("Enable"))
  227. m_Lutify.enabled = true;
  228. return;
  229. }
  230. // Gallery
  231. List<Texture2D> luts = null;
  232. if (!m_Collections.TryGetValue(m_CollectionNames[m_SelectedCollection], out luts) || luts.Count == 0)
  233. return;
  234. m_ScrollView = GUILayout.BeginScrollView(m_ScrollView, false, false, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
  235. {
  236. int w = Mathf.FloorToInt(position.width);
  237. int tw = m_Lutify._ThumbWidth;
  238. int th = m_Lutify._ThumbHeight;
  239. int spacing = 4;
  240. int margin = 16;
  241. int c = (w - margin) / (tw + spacing);
  242. GUILayout.BeginVertical();
  243. {
  244. GUILayout.Space(spacing);
  245. FilterMode filterMode = m_CollectionFilters[m_SelectedCollection];
  246. bool layoutState = false;
  247. int j = 0;
  248. for (int i = 0; i < luts.Count; i++)
  249. {
  250. j++;
  251. if (!layoutState)
  252. {
  253. GUILayout.BeginHorizontal();
  254. GUILayout.FlexibleSpace();
  255. layoutState = true;
  256. }
  257. Rect r = GUILayoutUtility.GetRect(tw, th);
  258. if (m_Lutify._PreviewRT != null)
  259. DrawPreview(r, luts[i], filterMode);
  260. if (j < c && c != 1 && i != luts.Count - 1)
  261. GUILayout.Space(spacing);
  262. if (layoutState && j == c)
  263. {
  264. GUILayout.FlexibleSpace();
  265. GUILayout.EndHorizontal();
  266. GUILayout.Space(spacing);
  267. layoutState = false;
  268. j = 0;
  269. }
  270. }
  271. if (layoutState && j < c)
  272. {
  273. GUILayout.FlexibleSpace();
  274. GUILayout.EndHorizontal();
  275. }
  276. GUILayout.Space(spacing);
  277. }
  278. GUILayout.EndVertical();
  279. }
  280. GUILayout.EndScrollView();
  281. }
  282. void DrawPreview(Rect r, Texture2D lut, FilterMode filterMode)
  283. {
  284. Event e = Event.current;
  285. if (e.type == EventType.Repaint)
  286. {
  287. bool selected = lut == m_Lutify.LookupTexture;
  288. // Preview texture
  289. lut.filterMode = filterMode;
  290. float tileSize = Mathf.Sqrt((float)lut.width);
  291. PreviewMaterial.SetTexture("_LookupTex2D", lut);
  292. PreviewMaterial.SetVector("_Params", new Vector4(1f / (float)lut.width, 1f / (float)lut.height, tileSize - 1f, filterMode == FilterMode.Point ? 1f : 0f));
  293. PreviewMaterial.SetFloat("_BottomFrame", 17f * (1f / (float)m_Lutify._ThumbHeight));
  294. PreviewMaterial.SetColor("_FrameColor", selected ? Color.white : Color.black);
  295. EditorGUI.DrawPreviewTexture(r, m_Lutify._PreviewRT, PreviewMaterial);
  296. // Borders
  297. Handles.color = selected ? new Color(1f, 1f, 1f, 2f) : new Color(0f, 0f, 0f, 2f); // Removes default alpha on handles
  298. Handles.DrawLine(new Vector3(r.x, r.y + 1, 0f), new Vector3(r.xMax, r.y + 1, 0f));
  299. Handles.DrawLine(new Vector3(r.x + 1, r.y + 1, 0f), new Vector3(r.x + 1, r.yMax, 0f));
  300. Handles.DrawLine(new Vector3(r.xMax, r.y + 1, 0f), new Vector3(r.xMax, r.yMax, 0f));
  301. // Name overlay (text)
  302. Color oldColor = GUI.color;
  303. GUI.color = selected ? new Color32(20, 20, 20, 255) : new Color32(156, 216, 246, 255);
  304. GUI.Label(new Rect(r.x + 4, r.yMax - 17, r.width - 8, 16), lut.name, EditorStyles.miniLabel);
  305. GUI.color = oldColor;
  306. // Selection shape
  307. if (selected)
  308. {
  309. Vector3[] verts = new Vector3[] {
  310. new Vector3(r.xMax, r.y + 1, 0),
  311. new Vector3(r.xMax, r.y + 18, 0),
  312. new Vector3(r.xMax - 17, r.y + 1, 0),
  313. new Vector3(r.xMax - 17, r.y + 1, 0)
  314. };
  315. Handles.DrawSolidRectangleWithOutline(verts, new Color(1f, 1f, 1f, 2f), new Color(1f, 1f, 1f, 2f));
  316. }
  317. }
  318. else if (e.type == EventType.MouseDown)
  319. {
  320. if (r.Contains(e.mousePosition))
  321. {
  322. if (e.button == 0)
  323. {
  324. Undo.RecordObject(m_Lutify, "Set Lutify LUT");
  325. m_Lutify.LutFiltering = m_CollectionFilters[m_SelectedCollection];
  326. m_Lutify.LookupTexture = lut;
  327. InternalGUIUtility.RepaintGameView();
  328. }
  329. else
  330. {
  331. GenericMenu menu = new GenericMenu();
  332. menu.AddItem(new GUIContent("Show in project (Standard)"), false, () => EditorGUIUtility.PingObject(lut));
  333. menu.AddItem(new GUIContent("Show in project (Scion)"), false, () => PingExtTexture(lut, "Scion"));
  334. menu.AddItem(new GUIContent("Show in project (Amplify)"), false, () => PingExtTexture(lut, "AmplifyColor"));
  335. menu.ShowAsContext();
  336. }
  337. e.Use();
  338. }
  339. }
  340. }
  341. void PingExtTexture(Texture2D lut, string basePath)
  342. {
  343. string loc = Path.Combine(m_BasePath, Path.Combine(basePath, m_CollectionNames[m_SelectedCollection]));
  344. string path = Path.Combine(loc, lut.name + ".png");
  345. Texture2D tex = LoadAssetAt<Texture2D>(path);
  346. if (tex == null)
  347. return;
  348. EditorGUIUtility.PingObject(tex);
  349. }
  350. static class InternalGUIUtility
  351. {
  352. static Type m_GameViewType;
  353. static InternalGUIUtility()
  354. {
  355. // GameView
  356. Assembly assembly = typeof(EditorWindow).Assembly;
  357. m_GameViewType = assembly.GetType("UnityEditor.GameView");
  358. }
  359. public static GUIContent GetTitleContent(EditorWindow editor)
  360. {
  361. const BindingFlags bFlags = BindingFlags.Instance | BindingFlags.NonPublic;
  362. PropertyInfo p = typeof(EditorWindow).GetProperty("cachedTitleContent", bFlags);
  363. if (p == null) return null;
  364. return p.GetValue(editor, null) as GUIContent;
  365. }
  366. public static void RepaintGameView()
  367. {
  368. EditorWindow gameview = EditorWindow.GetWindow(m_GameViewType);
  369. gameview.Repaint();
  370. }
  371. }
  372. }