MaterialPostprocessor.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.Rendering.Universal.ShaderGUI;
  5. using UnityEngine;
  6. using UnityEngine.Rendering.Universal;
  7. namespace UnityEditor.Rendering.Universal
  8. {
  9. class MaterialModificationProcessor : AssetModificationProcessor
  10. {
  11. static void OnWillCreateAsset(string asset)
  12. {
  13. if (!asset.ToLowerInvariant().EndsWith(".mat"))
  14. {
  15. return;
  16. }
  17. MaterialPostprocessor.s_CreatedAssets.Add(asset);
  18. }
  19. }
  20. class MaterialReimporter : Editor
  21. {
  22. static bool s_NeedToCheckProjSettingExistence = true;
  23. static void ReimportAllMaterials()
  24. {
  25. string[] guids = AssetDatabase.FindAssets("t:material", null);
  26. // There can be several materials subAssets per guid ( ie : FBX files ), remove duplicate guids.
  27. var distinctGuids = guids.Distinct();
  28. int materialIdx = 0;
  29. int totalMaterials = distinctGuids.Count();
  30. try
  31. {
  32. AssetDatabase.StartAssetEditing();
  33. foreach (var asset in distinctGuids)
  34. {
  35. materialIdx++;
  36. var path = AssetDatabase.GUIDToAssetPath(asset);
  37. EditorUtility.DisplayProgressBar("Material Upgrader re-import", string.Format("({0} of {1}) {2}", materialIdx, totalMaterials, path), (float)materialIdx / (float)totalMaterials);
  38. AssetDatabase.ImportAsset(path);
  39. }
  40. }
  41. finally
  42. {
  43. // Ensure the AssetDatabase knows we're finished editing
  44. AssetDatabase.StopAssetEditing();
  45. }
  46. EditorUtility.ClearProgressBar();
  47. MaterialPostprocessor.s_NeedsSavingAssets = true;
  48. }
  49. [InitializeOnLoadMethod]
  50. static void RegisterUpgraderReimport()
  51. {
  52. EditorApplication.update += () =>
  53. {
  54. if (Time.renderedFrameCount > 0)
  55. {
  56. bool fileExist = true;
  57. // We check the file existence only once to avoid IO operations every frame.
  58. if (s_NeedToCheckProjSettingExistence)
  59. {
  60. fileExist = System.IO.File.Exists(UniversalProjectSettings.filePath);
  61. s_NeedToCheckProjSettingExistence = false;
  62. }
  63. //This method is called at opening and when URP package change (update of manifest.json)
  64. var curUpgradeVersion = UniversalProjectSettings.materialVersionForUpgrade;
  65. if (curUpgradeVersion != MaterialPostprocessor.k_Upgraders.Length)
  66. {
  67. string commandLineOptions = Environment.CommandLine;
  68. bool inTestSuite = commandLineOptions.Contains("-testResults");
  69. if (!inTestSuite && fileExist)
  70. {
  71. EditorUtility.DisplayDialog("URP Material upgrade", "The Materials in your Project were created using an older version of the Universal Render Pipeline (URP)." +
  72. " Unity must upgrade them to be compatible with your current version of URP. \n" +
  73. " Unity will re-import all of the Materials in your project, save the upgraded Materials to disk, and check them out in source control if needed.\n" +
  74. " Please see the Material upgrade guide in the URP documentation for more information.", "Ok");
  75. }
  76. ReimportAllMaterials();
  77. }
  78. if (MaterialPostprocessor.s_NeedsSavingAssets)
  79. MaterialPostprocessor.SaveAssetsToDisk();
  80. }
  81. };
  82. }
  83. }
  84. class MaterialPostprocessor : AssetPostprocessor
  85. {
  86. public static List<string> s_CreatedAssets = new List<string>();
  87. internal static List<string> s_ImportedAssetThatNeedSaving = new List<string>();
  88. internal static bool s_NeedsSavingAssets = false;
  89. internal static readonly Action<Material, ShaderPathID>[] k_Upgraders = { UpgradeV1, UpgradeV2, UpgradeV3, UpgradeV4 };
  90. static internal void SaveAssetsToDisk()
  91. {
  92. string commandLineOptions = System.Environment.CommandLine;
  93. bool inTestSuite = commandLineOptions.Contains("-testResults");
  94. if (inTestSuite)
  95. {
  96. // Need to update material version to prevent infinite loop in the upgrader
  97. // when running tests.
  98. UniversalProjectSettings.materialVersionForUpgrade = k_Upgraders.Length;
  99. return;
  100. }
  101. foreach (var asset in s_ImportedAssetThatNeedSaving)
  102. {
  103. AssetDatabase.MakeEditable(asset);
  104. }
  105. AssetDatabase.SaveAssets();
  106. //to prevent data loss, only update the saved version if user applied change and assets are written to
  107. UniversalProjectSettings.materialVersionForUpgrade = k_Upgraders.Length;
  108. UniversalProjectSettings.Save();
  109. s_ImportedAssetThatNeedSaving.Clear();
  110. s_NeedsSavingAssets = false;
  111. }
  112. static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
  113. {
  114. var upgradeLog = "UniversalRP Material log:";
  115. var upgradeCount = 0;
  116. foreach (var asset in importedAssets)
  117. {
  118. if (!asset.EndsWith(".mat", StringComparison.InvariantCultureIgnoreCase))
  119. continue;
  120. var material = (Material)AssetDatabase.LoadAssetAtPath(asset, typeof(Material));
  121. if (!ShaderUtils.IsLWShader(material.shader))
  122. continue;
  123. ShaderPathID id = ShaderUtils.GetEnumFromPath(material.shader.name);
  124. var wasUpgraded = false;
  125. var debug = "\n" + material.name;
  126. AssetVersion assetVersion = null;
  127. var allAssets = AssetDatabase.LoadAllAssetsAtPath(asset);
  128. foreach (var subAsset in allAssets)
  129. {
  130. if (subAsset is AssetVersion sub)
  131. {
  132. assetVersion = sub;
  133. }
  134. }
  135. if (!assetVersion)
  136. {
  137. wasUpgraded = true;
  138. assetVersion = ScriptableObject.CreateInstance<AssetVersion>();
  139. if (s_CreatedAssets.Contains(asset))
  140. {
  141. assetVersion.version = k_Upgraders.Length;
  142. s_CreatedAssets.Remove(asset);
  143. InitializeLatest(material, id);
  144. debug += " initialized.";
  145. }
  146. else
  147. {
  148. assetVersion.version = UniversalProjectSettings.materialVersionForUpgrade;
  149. debug += $" assumed to be version {UniversalProjectSettings.materialVersionForUpgrade} due to missing version.";
  150. }
  151. assetVersion.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector | HideFlags.NotEditable;
  152. AssetDatabase.AddObjectToAsset(assetVersion, asset);
  153. }
  154. while (assetVersion.version < k_Upgraders.Length)
  155. {
  156. k_Upgraders[assetVersion.version](material, id);
  157. debug += $" upgrading:v{assetVersion.version} to v{assetVersion.version + 1}";
  158. assetVersion.version++;
  159. wasUpgraded = true;
  160. }
  161. if (wasUpgraded)
  162. {
  163. upgradeLog += debug;
  164. upgradeCount++;
  165. EditorUtility.SetDirty(assetVersion);
  166. s_ImportedAssetThatNeedSaving.Add(asset);
  167. s_NeedsSavingAssets = true;
  168. }
  169. }
  170. }
  171. static void InitializeLatest(Material material, ShaderPathID id)
  172. {
  173. }
  174. static void UpgradeV1(Material material, ShaderPathID shaderID)
  175. {
  176. var shaderPath = ShaderUtils.GetShaderPath(shaderID);
  177. var upgradeFlag = MaterialUpgrader.UpgradeFlags.LogMessageWhenNoUpgraderFound;
  178. switch (shaderID)
  179. {
  180. case ShaderPathID.Unlit:
  181. MaterialUpgrader.Upgrade(material, new UnlitUpdaterV1(shaderPath), upgradeFlag);
  182. UnlitShader.SetMaterialKeywords(material);
  183. break;
  184. case ShaderPathID.SimpleLit:
  185. MaterialUpgrader.Upgrade(material, new SimpleLitUpdaterV1(shaderPath), upgradeFlag);
  186. SimpleLitShader.SetMaterialKeywords(material, SimpleLitGUI.SetMaterialKeywords);
  187. break;
  188. case ShaderPathID.Lit:
  189. MaterialUpgrader.Upgrade(material, new LitUpdaterV1(shaderPath), upgradeFlag);
  190. LitShader.SetMaterialKeywords(material, LitGUI.SetMaterialKeywords);
  191. break;
  192. case ShaderPathID.ParticlesLit:
  193. MaterialUpgrader.Upgrade(material, new ParticleUpdaterV1(shaderPath), upgradeFlag);
  194. ParticlesLitShader.SetMaterialKeywords(material, LitGUI.SetMaterialKeywords, ParticleGUI.SetMaterialKeywords);
  195. break;
  196. case ShaderPathID.ParticlesSimpleLit:
  197. MaterialUpgrader.Upgrade(material, new ParticleUpdaterV1(shaderPath), upgradeFlag);
  198. ParticlesSimpleLitShader.SetMaterialKeywords(material, SimpleLitGUI.SetMaterialKeywords, ParticleGUI.SetMaterialKeywords);
  199. break;
  200. case ShaderPathID.ParticlesUnlit:
  201. MaterialUpgrader.Upgrade(material, new ParticleUpdaterV1(shaderPath), upgradeFlag);
  202. ParticlesUnlitShader.SetMaterialKeywords(material, null, ParticleGUI.SetMaterialKeywords);
  203. break;
  204. }
  205. }
  206. static void UpgradeV2(Material material, ShaderPathID shaderID)
  207. {
  208. // fix 50 offset on shaders
  209. if(material.HasProperty("_QueueOffset"))
  210. BaseShaderGUI.SetupMaterialBlendMode(material);
  211. }
  212. static void UpgradeV3(Material material, ShaderPathID shaderID)
  213. {
  214. switch (shaderID)
  215. {
  216. case ShaderPathID.Lit:
  217. case ShaderPathID.SimpleLit:
  218. case ShaderPathID.ParticlesLit:
  219. case ShaderPathID.ParticlesSimpleLit:
  220. case ShaderPathID.ParticlesUnlit:
  221. var propertyID = Shader.PropertyToID("_EmissionColor");
  222. if (material.HasProperty(propertyID))
  223. {
  224. // In older version there was a bug that these shaders did not had HDR attribute on emission property.
  225. // This caused emission color to be converted from gamma to linear space.
  226. // In order to avoid visual regression on older projects we will do gamma to linear conversion here.
  227. var emissionGamma = material.GetColor(propertyID);
  228. var emissionLinear = emissionGamma.linear;
  229. material.SetColor(propertyID, emissionLinear);
  230. }
  231. break;
  232. }
  233. }
  234. static void UpgradeV4(Material material, ShaderPathID shaderID)
  235. {}
  236. }
  237. // Upgraders v1
  238. #region UpgradersV1
  239. internal class LitUpdaterV1 : MaterialUpgrader
  240. {
  241. public static void UpdateLitDetails(Material material)
  242. {
  243. if (material == null)
  244. throw new ArgumentNullException("material");
  245. if(material.GetTexture("_MetallicGlossMap") || material.GetTexture("_SpecGlossMap") || material.GetFloat("_SmoothnessTextureChannel") >= 0.5f)
  246. material.SetFloat("_Smoothness", material.GetFloat("_GlossMapScale"));
  247. else
  248. material.SetFloat("_Smoothness", material.GetFloat("_Glossiness"));
  249. }
  250. public LitUpdaterV1(string oldShaderName)
  251. {
  252. if (oldShaderName == null)
  253. throw new ArgumentNullException("oldShaderName");
  254. string standardShaderPath = ShaderUtils.GetShaderPath(ShaderPathID.Lit);
  255. RenameShader(oldShaderName, standardShaderPath, UpdateLitDetails);
  256. RenameTexture("_MainTex", "_BaseMap");
  257. RenameColor("_Color", "_BaseColor");
  258. RenameFloat("_GlossyReflections", "_EnvironmentReflections");
  259. }
  260. }
  261. internal class UnlitUpdaterV1 : MaterialUpgrader
  262. {
  263. static Shader bakedLit = Shader.Find(ShaderUtils.GetShaderPath(ShaderPathID.BakedLit));
  264. public static void UpgradeToUnlit(Material material)
  265. {
  266. if (material == null)
  267. throw new ArgumentNullException("material");
  268. if (material.GetFloat("_SampleGI") != 0)
  269. {
  270. material.shader = bakedLit;
  271. material.EnableKeyword("_NORMALMAP");
  272. }
  273. }
  274. public UnlitUpdaterV1(string oldShaderName)
  275. {
  276. if (oldShaderName == null)
  277. throw new ArgumentNullException("oldShaderName");
  278. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.Unlit), UpgradeToUnlit);
  279. RenameTexture("_MainTex", "_BaseMap");
  280. RenameColor("_Color", "_BaseColor");
  281. }
  282. }
  283. internal class SimpleLitUpdaterV1 : MaterialUpgrader
  284. {
  285. public SimpleLitUpdaterV1(string oldShaderName)
  286. {
  287. if (oldShaderName == null)
  288. throw new ArgumentNullException("oldShaderName");
  289. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.SimpleLit), UpgradeToSimpleLit);
  290. RenameTexture("_MainTex", "_BaseMap");
  291. RenameColor("_Color", "_BaseColor");
  292. RenameFloat("_SpecSource", "_SpecularHighlights");
  293. RenameFloat("_Shininess", "_Smoothness");
  294. }
  295. public static void UpgradeToSimpleLit(Material material)
  296. {
  297. if (material == null)
  298. throw new ArgumentNullException("material");
  299. var smoothnessSource = 1 - (int)material.GetFloat("_GlossinessSource");
  300. material.SetFloat("_SmoothnessSource" ,smoothnessSource);
  301. if (material.GetTexture("_SpecGlossMap") == null)
  302. {
  303. var col = material.GetColor("_SpecColor");
  304. var colBase = material.GetColor("_Color");
  305. var smoothness = material.GetFloat("_Shininess");
  306. if (material.GetFloat("_Surface") == 0)
  307. {
  308. if (smoothnessSource == 1)
  309. colBase.a = smoothness;
  310. else
  311. col.a = smoothness;
  312. material.SetColor("_BaseColor", colBase);
  313. }
  314. material.SetColor("_BaseColor", colBase);
  315. material.SetColor("_SpecColor", col);
  316. }
  317. }
  318. }
  319. internal class ParticleUpdaterV1 : MaterialUpgrader
  320. {
  321. public ParticleUpdaterV1(string shaderName)
  322. {
  323. if (shaderName == null)
  324. throw new ArgumentNullException("oldShaderName");
  325. RenameShader(shaderName, shaderName, ParticleUpgrader.UpdateSurfaceBlendModes);
  326. RenameTexture("_MainTex", "_BaseMap");
  327. RenameColor("_Color", "_BaseColor");
  328. RenameFloat("_FlipbookMode", "_FlipbookBlending");
  329. switch (ShaderUtils.GetEnumFromPath(shaderName))
  330. {
  331. case ShaderPathID.ParticlesLit:
  332. RenameFloat("_Glossiness", "_Smoothness");
  333. break;
  334. case ShaderPathID.ParticlesSimpleLit:
  335. RenameFloat("_Glossiness", "_Smoothness");
  336. break;
  337. case ShaderPathID.ParticlesUnlit:
  338. break;
  339. }
  340. }
  341. }
  342. #endregion
  343. }