LookDev.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using UnityEngine.Rendering;
  2. using UnityEngine.Rendering.LookDev;
  3. using UnityEditorInternal;
  4. using UnityEngine;
  5. using System.Linq;
  6. namespace UnityEditor.Rendering.LookDev
  7. {
  8. /// <summary>
  9. /// Main entry point for scripting LookDev
  10. /// </summary>
  11. public static class LookDev
  12. {
  13. const string lastRenderingDataSavePath = "Library/LookDevConfig.asset";
  14. //TODO: ensure only one displayer at time for the moment
  15. static IViewDisplayer s_ViewDisplayer;
  16. static IEnvironmentDisplayer s_EnvironmentDisplayer;
  17. static Compositer s_Compositor;
  18. static StageCache s_Stages;
  19. static Context s_CurrentContext;
  20. internal static IDataProvider dataProvider
  21. => RenderPipelineManager.currentPipeline as IDataProvider;
  22. /// <summary>
  23. /// Get all the data used in LookDev currently (views, layout, debug... )
  24. /// </summary>
  25. internal static Context currentContext
  26. {
  27. //Lazy init: load it when needed instead in static even if you do not support lookdev
  28. get
  29. {
  30. if (s_CurrentContext == null || s_CurrentContext.Equals(null))
  31. {
  32. // In case the context is still alive somewhere but the static reference has been lost, we can find the object back with this
  33. s_CurrentContext = TryFindCurrentContext();
  34. if (s_CurrentContext != null)
  35. return s_CurrentContext;
  36. s_CurrentContext = LoadConfigInternal();
  37. if (s_CurrentContext == null)
  38. s_CurrentContext = defaultContext;
  39. ReloadStage(false);
  40. }
  41. return s_CurrentContext;
  42. }
  43. private set => s_CurrentContext = value;
  44. }
  45. static Context TryFindCurrentContext()
  46. {
  47. Context context = s_CurrentContext;
  48. if (context != null)
  49. return context;
  50. return Resources.FindObjectsOfTypeAll<Context>().FirstOrDefault();
  51. }
  52. static Context defaultContext
  53. {
  54. get
  55. {
  56. var context = UnityEngine.ScriptableObject.CreateInstance<Context>();
  57. context.hideFlags = HideFlags.HideAndDontSave;
  58. context.Init();
  59. return context;
  60. }
  61. }
  62. //[TODO: not compatible with multiple displayer. To rework if needed]
  63. internal static IViewDisplayer currentViewDisplayer => s_ViewDisplayer;
  64. internal static IEnvironmentDisplayer currentEnvironmentDisplayer => s_EnvironmentDisplayer;
  65. [MenuItem("Window/Render Pipeline/Look Dev", false, 10200)]
  66. static void OpenLookDev() => Open();
  67. [MenuItem("Window/Render Pipeline/Look Dev", true, 10200)]
  68. static bool LookDevAvailable() => supported;
  69. internal static bool waitingConfigure { get; private set; } = true;
  70. /// <summary>State of the LookDev window</summary>
  71. public static bool open { get; private set; }
  72. /// <summary>
  73. /// Does LookDev is supported with the current render pipeline?
  74. /// </summary>
  75. public static bool supported => dataProvider != null;
  76. /// <summary>
  77. /// Reset all LookDevs datas to the default configuration
  78. /// </summary>
  79. public static void ResetConfig()
  80. => currentContext = defaultContext;
  81. static Context LoadConfigInternal(string path = lastRenderingDataSavePath)
  82. {
  83. var objs = InternalEditorUtility.LoadSerializedFileAndForget(path);
  84. Context context = (objs.Length > 0 ? objs[0] : null) as Context;
  85. if (context != null && !context.Equals(null))
  86. context.Init();
  87. return context;
  88. }
  89. /// <summary>
  90. /// Load a different set of datas
  91. /// </summary>
  92. /// <param name="path">Path where to load</param>
  93. internal static void LoadConfig(string path = lastRenderingDataSavePath)
  94. {
  95. var last = LoadConfigInternal(path);
  96. if (last != null)
  97. currentContext = last;
  98. }
  99. /// <summary>
  100. /// Save the current set of datas
  101. /// </summary>
  102. /// <param name="path">[optional] Path to save. By default, saved in Library folder</param>
  103. internal static void SaveConfig(string path = lastRenderingDataSavePath)
  104. {
  105. if (currentContext != null && !currentContext.Equals(null))
  106. InternalEditorUtility.SaveToSerializedFileAndForget(new[] { currentContext }, path, true);
  107. }
  108. /// <summary>Open the LookDev window</summary>
  109. public static void Open()
  110. {
  111. EditorWindow.GetWindow<DisplayWindow>();
  112. }
  113. /// <summary>Close the LookDev window</summary>
  114. public static void Close()
  115. {
  116. (s_ViewDisplayer as EditorWindow)?.Close();
  117. s_ViewDisplayer = null;
  118. (s_EnvironmentDisplayer as EditorWindow)?.Close();
  119. s_EnvironmentDisplayer = null;
  120. }
  121. internal static void Initialize(DisplayWindow window)
  122. {
  123. s_ViewDisplayer = window;
  124. s_EnvironmentDisplayer = window;
  125. open = true;
  126. // Lookdev Initialize can be called when the window is re-created by the editor layout system.
  127. // In that case, the current context won't be null and there might be objects to reload from the temp ID
  128. ConfigureLookDev(reloadWithTemporaryID: TryFindCurrentContext() != null);
  129. }
  130. [Callbacks.DidReloadScripts]
  131. static void OnEditorReload()
  132. {
  133. var windows = Resources.FindObjectsOfTypeAll<DisplayWindow>();
  134. s_ViewDisplayer = windows.Length > 0 ? windows[0] : null;
  135. s_EnvironmentDisplayer = windows.Length > 0 ? windows[0] : null;
  136. open = s_ViewDisplayer != null;
  137. if (open)
  138. ConfigureLookDev(reloadWithTemporaryID: true);
  139. }
  140. static void ConfigureLookDev(bool reloadWithTemporaryID)
  141. {
  142. open = true;
  143. waitingConfigure = true;
  144. if (s_CurrentContext == null || s_CurrentContext.Equals(null))
  145. LoadConfig();
  146. WaitingSRPReloadForConfiguringRenderer(5, reloadWithTemporaryID: reloadWithTemporaryID);
  147. }
  148. static void WaitingSRPReloadForConfiguringRenderer(int maxAttempt, bool reloadWithTemporaryID, int attemptNumber = 0)
  149. {
  150. if (supported)
  151. {
  152. waitingConfigure = false;
  153. ConfigureRenderer(reloadWithTemporaryID);
  154. LinkViewDisplayer();
  155. LinkEnvironmentDisplayer();
  156. ReloadStage(reloadWithTemporaryID);
  157. }
  158. else if (attemptNumber < maxAttempt)
  159. EditorApplication.delayCall +=
  160. () => WaitingSRPReloadForConfiguringRenderer(maxAttempt, reloadWithTemporaryID, ++attemptNumber);
  161. else
  162. waitingConfigure = false;
  163. }
  164. static void ConfigureRenderer(bool reloadWithTemporaryID)
  165. {
  166. s_Stages?.Dispose(); //clean previous occurrence on reloading
  167. s_Stages = new StageCache(dataProvider);
  168. s_Compositor?.Dispose(); //clean previous occurrence on reloading
  169. s_Compositor = new Compositer(s_ViewDisplayer, dataProvider, s_Stages);
  170. }
  171. static void LinkViewDisplayer()
  172. {
  173. s_ViewDisplayer.OnClosed += () =>
  174. {
  175. s_Compositor?.Dispose();
  176. s_Compositor = null;
  177. s_Stages?.Dispose();
  178. s_Stages = null;
  179. s_ViewDisplayer = null;
  180. //currentContext = null;
  181. SaveConfig();
  182. open = false;
  183. };
  184. s_ViewDisplayer.OnLayoutChanged += (layout, envPanelOpen) =>
  185. {
  186. currentContext.layout.viewLayout = layout;
  187. currentContext.layout.showedSidePanel = envPanelOpen;
  188. SaveConfig();
  189. };
  190. s_ViewDisplayer.OnChangingObjectInView += (go, index, localPos) =>
  191. {
  192. switch (index)
  193. {
  194. case ViewCompositionIndex.First:
  195. case ViewCompositionIndex.Second:
  196. currentContext.GetViewContent((ViewIndex)index).UpdateViewedObject(go);
  197. SaveContextChangeAndApply((ViewIndex)index);
  198. break;
  199. case ViewCompositionIndex.Composite:
  200. ViewIndex viewIndex = s_Compositor.GetViewFromComposition(localPos);
  201. currentContext.GetViewContent(viewIndex).UpdateViewedObject(go);
  202. SaveContextChangeAndApply(viewIndex);
  203. break;
  204. }
  205. };
  206. s_ViewDisplayer.OnChangingEnvironmentInView += (obj, index, localPos) =>
  207. {
  208. switch (index)
  209. {
  210. case ViewCompositionIndex.First:
  211. case ViewCompositionIndex.Second:
  212. currentContext.GetViewContent((ViewIndex)index).UpdateEnvironment(obj);
  213. SaveContextChangeAndApply((ViewIndex)index);
  214. break;
  215. case ViewCompositionIndex.Composite:
  216. ViewIndex viewIndex = s_Compositor.GetViewFromComposition(localPos);
  217. currentContext.GetViewContent(viewIndex).UpdateEnvironment(obj);
  218. SaveContextChangeAndApply(viewIndex);
  219. break;
  220. }
  221. };
  222. }
  223. static void LinkEnvironmentDisplayer()
  224. {
  225. s_EnvironmentDisplayer.OnChangingEnvironmentLibrary += UpdateEnvironmentLibrary;
  226. }
  227. static void UpdateEnvironmentLibrary(EnvironmentLibrary library)
  228. {
  229. LookDev.currentContext.UpdateEnvironmentLibrary(library);
  230. }
  231. static void ReloadStage(bool reloadWithTemporaryID)
  232. {
  233. currentContext.GetViewContent(ViewIndex.First).LoadAll(reloadWithTemporaryID);
  234. ApplyContextChange(ViewIndex.First);
  235. currentContext.GetViewContent(ViewIndex.Second).LoadAll(reloadWithTemporaryID);
  236. ApplyContextChange(ViewIndex.Second);
  237. }
  238. static void ApplyContextChange(ViewIndex index)
  239. {
  240. s_Stages.UpdateSceneObjects(index);
  241. s_Stages.UpdateSceneLighting(index, dataProvider);
  242. s_ViewDisplayer.Repaint();
  243. }
  244. /// <summary>Update the rendered element with element in the context</summary>
  245. /// <param name="index">The index of the stage to update</param>
  246. internal static void SaveContextChangeAndApply(ViewIndex index)
  247. {
  248. SaveConfig();
  249. ApplyContextChange(index);
  250. }
  251. }
  252. }