Context.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. using System;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. using System.Collections;
  5. namespace UnityEditor.Rendering.LookDev
  6. {
  7. /// <summary>
  8. /// Different working views in LookDev
  9. /// </summary>
  10. public enum ViewIndex
  11. {
  12. /// <summary>First view</summary>
  13. First,
  14. /// <summary>Second view</summary>
  15. Second
  16. };
  17. /// <summary>
  18. /// Same as <see cref="ViewIndex"/> plus a compound value
  19. /// </summary>
  20. public enum ViewCompositionIndex
  21. {
  22. /// <summary>First view</summary>
  23. First = ViewIndex.First,
  24. /// <summary>Second view</summary>
  25. Second = ViewIndex.Second,
  26. /// <summary>Composite view (Several view on screen)</summary>
  27. Composite
  28. };
  29. // /!\ WARNING: these value name are used as uss file too.
  30. // if your rename here, rename in the uss too.
  31. /// <summary>
  32. /// Different layout supported in LookDev
  33. /// </summary>
  34. public enum Layout
  35. {
  36. /// <summary>First view display fully</summary>
  37. FullFirstView,
  38. /// <summary>Second view display fully</summary>
  39. FullSecondView,
  40. /// <summary>First and second views displayed splitted horizontally</summary>
  41. HorizontalSplit,
  42. /// <summary>First and second views displayed splitted vertically</summary>
  43. VerticalSplit,
  44. /// <summary>First and second views displayed with stacking and orientation customizable split</summary>
  45. CustomSplit
  46. }
  47. /// <summary>
  48. /// Status of the side panel of the LookDev window
  49. /// </summary>
  50. public enum SidePanel
  51. {
  52. /// <summary>No side panel</summary>
  53. None = -1,
  54. /// <summary>Environment side panel</summary>
  55. Environment,
  56. /// <summary>Debug side panel</summary>
  57. Debug,
  58. }
  59. /// <summary>
  60. /// The target views of the debug panel
  61. /// </summary>
  62. public enum TargetDebugView
  63. {
  64. /// <summary>First Debug view</summary>
  65. First,
  66. /// <summary>Both Debug view</summary>
  67. Both,
  68. /// <summary>Second Debug view</summary>
  69. Second
  70. };
  71. /// <summary>
  72. /// Class containing all data used by the LookDev Window to render
  73. /// </summary>
  74. [System.Serializable]
  75. public class Context : ScriptableObject, IDisposable
  76. {
  77. [SerializeField]
  78. string m_EnvironmentLibraryGUID = ""; //Empty GUID
  79. [SerializeField]
  80. bool m_CameraSynced = true;
  81. EnvironmentLibrary m_EnvironmentLibrary;
  82. /// <summary>The currently used Environment</summary>
  83. public EnvironmentLibrary environmentLibrary
  84. {
  85. get
  86. {
  87. //check if asset deleted by user
  88. if (m_EnvironmentLibrary != null && AssetDatabase.Contains(m_EnvironmentLibrary))
  89. return m_EnvironmentLibrary;
  90. if (!String.IsNullOrEmpty(m_EnvironmentLibraryGUID))
  91. {
  92. //user deleted the EnvironmentLibrary asset
  93. m_EnvironmentLibraryGUID = ""; //Empty GUID
  94. LookDev.currentEnvironmentDisplayer.Repaint();
  95. }
  96. return null;
  97. }
  98. private set => m_EnvironmentLibrary = value;
  99. }
  100. /// <summary>The currently used layout</summary>
  101. [field: SerializeField]
  102. public LayoutContext layout { get; private set; } = new LayoutContext();
  103. /// <summary>
  104. /// State if both views camera movement are synced or not
  105. /// </summary>
  106. public bool cameraSynced
  107. {
  108. get => m_CameraSynced;
  109. set
  110. {
  111. if (m_CameraSynced ^ value)
  112. {
  113. if (value)
  114. EditorApplication.update += SynchronizeCameraStates;
  115. else
  116. EditorApplication.update -= SynchronizeCameraStates;
  117. m_CameraSynced = value;
  118. }
  119. }
  120. }
  121. [SerializeField]
  122. ViewContext[] m_Views = new ViewContext[2]
  123. {
  124. new ViewContext(),
  125. new ViewContext()
  126. };
  127. /// <summary>
  128. /// Helper class to iterate on views
  129. /// </summary>
  130. public struct ViewIterator : IEnumerable<ViewContext>
  131. {
  132. ViewContext[] m_Views;
  133. internal ViewIterator(ViewContext[] views)
  134. => m_Views = views;
  135. /// <summary>
  136. /// Helper function to enumerates on ViewContexts
  137. /// </summary>
  138. /// <returns>Enumerator on ViewContext</returns>
  139. IEnumerator IEnumerable.GetEnumerator()
  140. => m_Views.GetEnumerator();
  141. /// <summary>
  142. /// Helper function to enumerates on ViewContexts
  143. /// </summary>
  144. /// <returns>Enumerator on ViewContext</returns>
  145. IEnumerator<ViewContext> IEnumerable<ViewContext>.GetEnumerator()
  146. => ((IEnumerable<ViewContext>)m_Views).GetEnumerator();
  147. }
  148. /// <summary>
  149. /// Helper function to get ViewIterator on ViewContexts
  150. /// </summary>
  151. public ViewIterator viewContexts
  152. => new ViewIterator(m_Views);
  153. /// <summary>
  154. /// Get datas relative to a view
  155. /// </summary>
  156. /// <param name="index">The view index to look at</param>
  157. /// <returns>Datas for the selected view</returns>
  158. public ViewContext GetViewContent(ViewIndex index)
  159. => m_Views[(int)index];
  160. internal void Init()
  161. {
  162. LoadEnvironmentLibraryFromGUID();
  163. //recompute non serialized computes states
  164. layout.gizmoState.Init();
  165. if (cameraSynced)
  166. EditorApplication.update += SynchronizeCameraStates;
  167. }
  168. /// <summary>Update the environment library used.</summary>
  169. /// <param name="library">The new EnvironmentLibrary</param>
  170. public void UpdateEnvironmentLibrary(EnvironmentLibrary library)
  171. {
  172. m_EnvironmentLibraryGUID = "";
  173. environmentLibrary = null;
  174. if (library == null || library.Equals(null))
  175. return;
  176. m_EnvironmentLibraryGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(library));
  177. environmentLibrary = library;
  178. }
  179. void LoadEnvironmentLibraryFromGUID()
  180. {
  181. environmentLibrary = null;
  182. GUID storedGUID;
  183. GUID.TryParse(m_EnvironmentLibraryGUID, out storedGUID);
  184. if (storedGUID.Empty())
  185. return;
  186. string path = AssetDatabase.GUIDToAssetPath(m_EnvironmentLibraryGUID);
  187. environmentLibrary = AssetDatabase.LoadAssetAtPath<EnvironmentLibrary>(path);
  188. }
  189. /// <summary>
  190. /// Synchronize cameras from both view using data from the baseCameraState
  191. /// </summary>
  192. /// <param name="baseCameraState">The <see cref="ViewIndex"/> to be used as reference</param>
  193. public void SynchronizeCameraStates(ViewIndex baseCameraState)
  194. {
  195. switch (baseCameraState)
  196. {
  197. case ViewIndex.First:
  198. m_Views[1].camera.SynchronizeFrom(m_Views[0].camera);
  199. break;
  200. case ViewIndex.Second:
  201. m_Views[0].camera.SynchronizeFrom(m_Views[1].camera);
  202. break;
  203. default:
  204. throw new System.ArgumentException("Unknow ViewIndex given in parameter.");
  205. }
  206. }
  207. void SynchronizeCameraStates()
  208. => SynchronizeCameraStates(layout.lastFocusedView);
  209. /// <summary>
  210. /// Change focused view.
  211. /// Focused view is the base view to copy data when syncing views' cameras
  212. /// </summary>
  213. /// <param name="index">The index of the view</param>
  214. public void SetFocusedCamera(ViewIndex index)
  215. => layout.lastFocusedView = index;
  216. private bool disposedValue = false; // To detect redundant calls
  217. /// <summary>Disposable behaviour</summary>
  218. void IDisposable.Dispose()
  219. {
  220. if (!disposedValue)
  221. {
  222. if (cameraSynced)
  223. EditorApplication.update -= SynchronizeCameraStates;
  224. disposedValue = true;
  225. }
  226. }
  227. internal bool HasLibraryAssetChanged(EnvironmentLibrary environmentLibrary)
  228. {
  229. if (environmentLibrary == null)
  230. return !String.IsNullOrEmpty(m_EnvironmentLibraryGUID);
  231. return m_EnvironmentLibraryGUID != AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(environmentLibrary));
  232. }
  233. }
  234. /// <summary>
  235. /// Data regarding the layout currently used in LookDev
  236. /// </summary>
  237. [System.Serializable]
  238. public class LayoutContext
  239. {
  240. /// <summary>The layout used</summary>
  241. public Layout viewLayout;
  242. /// <summary>The last focused view</summary>
  243. public ViewIndex lastFocusedView = ViewIndex.First;
  244. /// <summary>The state of the side panel</summary>
  245. public SidePanel showedSidePanel;
  246. /// <summary>The view to change when manipulating the Debug side panel</summary>
  247. [NonSerialized]
  248. public TargetDebugView debugPanelSource = TargetDebugView.Both;
  249. [SerializeField]
  250. internal ComparisonGizmoState gizmoState = new ComparisonGizmoState();
  251. internal bool isSimpleView => viewLayout == Layout.FullFirstView || viewLayout == Layout.FullSecondView;
  252. internal bool isMultiView => viewLayout == Layout.HorizontalSplit || viewLayout == Layout.VerticalSplit;
  253. internal bool isCombinedView => viewLayout == Layout.CustomSplit;
  254. }
  255. /// <summary>
  256. /// Data container containing content of a view
  257. /// </summary>
  258. [System.Serializable]
  259. public class ViewContext
  260. {
  261. /// <summary>The position and rotation of the camera</summary>
  262. [field: SerializeField]
  263. public CameraState camera { get; private set; } = new CameraState();
  264. /// <summary>The currently viewed debugState</summary>
  265. public DebugContext debug { get; private set; } = new DebugContext();
  266. //Environment asset, sub-asset (under a library) or cubemap
  267. [SerializeField]
  268. string m_EnvironmentGUID = ""; //Empty GUID
  269. /// <summary>
  270. /// Check if an Environment is registered for this view.
  271. /// The result will be accurate even if the Environment have not been reloaded yet.
  272. /// </summary>
  273. public bool hasEnvironment => !String.IsNullOrEmpty(m_EnvironmentGUID);
  274. /// <summary>The currently used Environment</summary>
  275. public Environment environment { get; private set; }
  276. [SerializeField]
  277. string viewedObjectAssetGUID = ""; //Empty GUID
  278. // Careful here: we want to keep it while reloading script.
  279. // But from one unity editor to an other, ID are not kept.
  280. // So, only use it when reloading from script update.
  281. [SerializeField]
  282. int viewedObjecHierarchytInstanceID;
  283. /// <summary>
  284. /// Check if an Environment is registered for this view.
  285. /// The result will be accurate even if the object have not been reloaded yet.
  286. /// </summary>
  287. public bool hasViewedObject =>
  288. !String.IsNullOrEmpty(viewedObjectAssetGUID)
  289. || viewedObjecHierarchytInstanceID != 0;
  290. /// <summary>Reference to the object given for instantiation.</summary>
  291. public GameObject viewedObjectReference { get; private set; }
  292. /// <summary>
  293. /// The currently displayed instance of <see cref="viewedObjectReference"/>.
  294. /// It will be instantiated when pushing changes to renderer.
  295. /// See <see cref="LookDev.SaveContextChangeAndApply(ViewIndex)"/>
  296. /// </summary>
  297. public GameObject viewedInstanceInPreview { get; internal set; }
  298. /// <summary>Update the environment used.</summary>
  299. /// <param name="environmentOrCubemapAsset">
  300. /// The new <see cref="Environment"/> to use.
  301. /// Or the <see cref="Cubemap"/> to use to build a new one.
  302. /// Other types will raise an ArgumentException.
  303. /// </param>
  304. public void UpdateEnvironment(UnityEngine.Object environmentOrCubemapAsset)
  305. {
  306. m_EnvironmentGUID = "";
  307. environment = null;
  308. if (environmentOrCubemapAsset == null || environmentOrCubemapAsset.Equals(null))
  309. return;
  310. if (!(environmentOrCubemapAsset is Environment)
  311. && !(environmentOrCubemapAsset is Cubemap))
  312. throw new System.ArgumentException("Only Environment or Cubemap accepted for environmentOrCubemapAsset parameter");
  313. string GUID;
  314. long localIDInFile;
  315. AssetDatabase.TryGetGUIDAndLocalFileIdentifier(environmentOrCubemapAsset, out GUID, out localIDInFile);
  316. m_EnvironmentGUID = $"{GUID},{localIDInFile}";
  317. if (environmentOrCubemapAsset is Environment)
  318. environment = environmentOrCubemapAsset as Environment;
  319. else //Cubemap
  320. environment = Environment.GetTemporaryEnvironmentForCubemap(environmentOrCubemapAsset as Cubemap);
  321. }
  322. void LoadEnvironmentFromGUID()
  323. {
  324. environment = null;
  325. GUID storedGUID;
  326. string[] GUIDAndLocalIDInFile = m_EnvironmentGUID.Split(new[] { ',' });
  327. GUID.TryParse(GUIDAndLocalIDInFile[0], out storedGUID);
  328. if (storedGUID.Empty())
  329. return;
  330. long localIDInFile = GUIDAndLocalIDInFile.Length < 2 ? 0L : long.Parse(GUIDAndLocalIDInFile[1]);
  331. string path = AssetDatabase.GUIDToAssetPath(GUIDAndLocalIDInFile[0]);
  332. Type savedType = AssetDatabase.GetMainAssetTypeAtPath(path);
  333. if (savedType == typeof(EnvironmentLibrary))
  334. {
  335. object[] loaded = AssetDatabase.LoadAllAssetsAtPath(path);
  336. for (int i = 0; i < loaded.Length; ++i)
  337. {
  338. string garbage;
  339. long testedLocalIndex;
  340. if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier((UnityEngine.Object)loaded[i], out garbage, out testedLocalIndex)
  341. && testedLocalIndex == localIDInFile)
  342. {
  343. environment = loaded[i] as Environment;
  344. break;
  345. }
  346. }
  347. }
  348. else if (savedType == typeof(Environment))
  349. environment = AssetDatabase.LoadAssetAtPath<Environment>(path);
  350. else if (savedType == typeof(Cubemap))
  351. {
  352. Cubemap cubemap = AssetDatabase.LoadAssetAtPath<Cubemap>(path);
  353. environment = Environment.GetTemporaryEnvironmentForCubemap(cubemap);
  354. }
  355. }
  356. /// <summary>Update the object reference used for instantiation.</summary>
  357. /// <param name="viewedObject">The new reference.</param>
  358. public void UpdateViewedObject(GameObject viewedObject)
  359. {
  360. viewedObjectAssetGUID = "";
  361. viewedObjecHierarchytInstanceID = 0;
  362. viewedObjectReference = null;
  363. if (viewedObject == null || viewedObject.Equals(null))
  364. return;
  365. bool fromHierarchy = viewedObject.scene.IsValid();
  366. if (fromHierarchy)
  367. viewedObjecHierarchytInstanceID = viewedObject.GetInstanceID();
  368. else
  369. viewedObjectAssetGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(viewedObject));
  370. viewedObjectReference = viewedObject;
  371. }
  372. //WARNING: only for script reloading
  373. void LoadViewedObject()
  374. {
  375. viewedObjectReference = null;
  376. GUID storedGUID;
  377. GUID.TryParse(viewedObjectAssetGUID, out storedGUID);
  378. if (!storedGUID.Empty())
  379. {
  380. string path = AssetDatabase.GUIDToAssetPath(viewedObjectAssetGUID);
  381. viewedObjectReference = AssetDatabase.LoadAssetAtPath<GameObject>(path);
  382. }
  383. else if (viewedObjecHierarchytInstanceID != 0)
  384. {
  385. viewedObjectReference = EditorUtility.InstanceIDToObject(viewedObjecHierarchytInstanceID) as GameObject;
  386. }
  387. }
  388. internal void LoadAll(bool reloadWithTemporaryID)
  389. {
  390. if (!reloadWithTemporaryID)
  391. CleanTemporaryObjectIndexes();
  392. LoadEnvironmentFromGUID();
  393. LoadViewedObject();
  394. }
  395. internal void CleanTemporaryObjectIndexes()
  396. => viewedObjecHierarchytInstanceID = 0;
  397. /// <summary>Reset the camera state to default values</summary>
  398. public void ResetCameraState()
  399. => camera.Reset();
  400. }
  401. /// <summary>
  402. /// Class that will contain debug value used.
  403. /// </summary>
  404. public class DebugContext
  405. {
  406. /// <summary>Display shadows in view.</summary>
  407. public bool shadow = true;
  408. /// <summary>Debug mode displayed. -1 means none.</summary>
  409. public int viewMode = -1;
  410. ///// <summary>Display the debug grey balls</summary>
  411. //public bool greyBalls;
  412. //[SerializeField]
  413. //string colorChartGUID = ""; //Empty GUID
  414. ///// <summary>The currently used color chart</summary>
  415. //public Texture2D colorChart { get; private set; }
  416. }
  417. }