UniversalRenderPipelineMaterialUpgrader.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor.Rendering.Universal.Converters;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. using UnityEngine.Rendering.Universal;
  7. namespace UnityEditor.Rendering.Universal
  8. {
  9. internal sealed class UniversalRenderPipelineMaterialUpgrader : RenderPipelineConverter
  10. {
  11. public override string name => "Material Upgrade";
  12. public override string info => "This will upgrade your materials.";
  13. public override int priority => -1000;
  14. public override Type container => typeof(BuiltInToURPConverterContainer);
  15. List<string> m_AssetsToConvert = new List<string>();
  16. private List<GUID> m_MaterialGUIDs = new();
  17. static List<MaterialUpgrader> m_Upgraders;
  18. private static HashSet<string> m_ShaderNamesToIgnore;
  19. public IReadOnlyList<MaterialUpgrader> upgraders => m_Upgraders;
  20. static UniversalRenderPipelineMaterialUpgrader()
  21. {
  22. m_Upgraders = new List<MaterialUpgrader>();
  23. GetUpgraders(ref m_Upgraders);
  24. m_ShaderNamesToIgnore = new HashSet<string>();
  25. GetShaderNamesToIgnore(ref m_ShaderNamesToIgnore);
  26. }
  27. private static void UpgradeProjectMaterials()
  28. {
  29. m_Upgraders = new List<MaterialUpgrader>();
  30. GetUpgraders(ref m_Upgraders);
  31. m_ShaderNamesToIgnore = new HashSet<string>();
  32. GetShaderNamesToIgnore(ref m_ShaderNamesToIgnore);
  33. MaterialUpgrader.UpgradeProjectFolder(m_Upgraders, m_ShaderNamesToIgnore, "Upgrade to URP Materials", MaterialUpgrader.UpgradeFlags.LogMessageWhenNoUpgraderFound);
  34. // TODO: return upgrade paths and pass to AnimationClipUpgrader
  35. AnimationClipUpgrader.DoUpgradeAllClipsMenuItem(m_Upgraders, "Upgrade Animation Clips to URP Materials");
  36. }
  37. [MenuItem("Edit/Rendering/Materials/Convert Selected Built-in Materials to URP", true)]
  38. static bool MaterialValidate(MenuCommand command)
  39. {
  40. foreach (var obj in Selection.objects)
  41. {
  42. if (obj is not Material) return false;
  43. }
  44. return true;
  45. }
  46. [MenuItem("Edit/Rendering/Materials/Convert Selected Built-in Materials to URP", priority = CoreUtils.Sections.section1 + CoreUtils.Priorities.editMenuPriority + 1)]
  47. private static void UpgradeSelectedMaterialsMenuItem()
  48. {
  49. UpgradeSelectedMaterials(false);
  50. }
  51. // Added bool variable in case this method was used by anyone.
  52. // Doing this, since the menuitem should behave as it did before,
  53. // and then we didn't have the Animation clips upgrader
  54. private static void UpgradeSelectedMaterials(bool upgradeAnimationClips = true)
  55. {
  56. List<MaterialUpgrader> upgraders = new List<MaterialUpgrader>();
  57. GetUpgraders(ref upgraders);
  58. HashSet<string> shaderNamesToIgnore = new HashSet<string>();
  59. GetShaderNamesToIgnore(ref shaderNamesToIgnore);
  60. MaterialUpgrader.UpgradeSelection(upgraders, shaderNamesToIgnore, "Upgrade to URP Materials", MaterialUpgrader.UpgradeFlags.LogMessageWhenNoUpgraderFound);
  61. // TODO: return upgrade paths and pass to AnimationClipUpgrader
  62. if (upgradeAnimationClips)
  63. {
  64. // TODO: return upgrade paths and pass to AnimationClipUpgrader
  65. AnimationClipUpgrader.DoUpgradeAllClipsMenuItem(upgraders, "Upgrade Animation Clips to URP Materials");
  66. }
  67. }
  68. private static void GetShaderNamesToIgnore(ref HashSet<string> shadersToIgnore)
  69. {
  70. shadersToIgnore.Add("Universal Render Pipeline/Baked Lit");
  71. shadersToIgnore.Add("Universal Render Pipeline/Lit");
  72. shadersToIgnore.Add("Universal Render Pipeline/Particles/Lit");
  73. shadersToIgnore.Add("Universal Render Pipeline/Particles/Simple Lit");
  74. shadersToIgnore.Add("Universal Render Pipeline/Particles/Unlit");
  75. shadersToIgnore.Add("Universal Render Pipeline/Simple Lit");
  76. shadersToIgnore.Add("Universal Render Pipeline/Nature/SpeedTree7");
  77. shadersToIgnore.Add("Universal Render Pipeline/Nature/SpeedTree7 Billboard");
  78. shadersToIgnore.Add("Universal Render Pipeline/Nature/SpeedTree8");
  79. shadersToIgnore.Add("Universal Render Pipeline/2D/Sprite-Lit-Default");
  80. shadersToIgnore.Add("Universal Render Pipeline/Terrain/Lit");
  81. shadersToIgnore.Add("Universal Render Pipeline/Unlit");
  82. shadersToIgnore.Add("Sprites/Default");
  83. }
  84. private static void GetUpgraders(ref List<MaterialUpgrader> upgraders)
  85. {
  86. /////////////////////////////////////
  87. // Unity Standard Upgraders //
  88. /////////////////////////////////////
  89. upgraders.Add(new StandardUpgrader("Standard"));
  90. upgraders.Add(new StandardUpgrader("Standard (Specular setup)"));
  91. /////////////////////////////////////
  92. // Legacy Shaders upgraders /
  93. /////////////////////////////////////
  94. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Diffuse", SupportedUpgradeParams.diffuseOpaque));
  95. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Diffuse Detail", SupportedUpgradeParams.diffuseOpaque));
  96. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Diffuse Fast", SupportedUpgradeParams.diffuseOpaque));
  97. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Specular", SupportedUpgradeParams.specularOpaque));
  98. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Bumped Diffuse", SupportedUpgradeParams.diffuseOpaque));
  99. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Bumped Specular", SupportedUpgradeParams.specularOpaque));
  100. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Parallax Diffuse", SupportedUpgradeParams.diffuseOpaque));
  101. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Parallax Specular", SupportedUpgradeParams.specularOpaque));
  102. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/VertexLit", SupportedUpgradeParams.specularOpaque));
  103. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Transparent/Cutout/VertexLit", SupportedUpgradeParams.specularAlphaCutout));
  104. // Reflective
  105. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Reflective/Bumped Diffuse", SupportedUpgradeParams.diffuseCubemap));
  106. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Reflective/Bumped Specular", SupportedUpgradeParams.specularCubemap));
  107. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Reflective/Bumped Unlit", SupportedUpgradeParams.diffuseCubemap));
  108. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Reflective/Bumped VertexLit", SupportedUpgradeParams.diffuseCubemap));
  109. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Reflective/Diffuse", SupportedUpgradeParams.diffuseCubemap));
  110. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Reflective/Specular", SupportedUpgradeParams.specularCubemap));
  111. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Reflective/VertexLit", SupportedUpgradeParams.diffuseCubemap));
  112. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Reflective/Parallax Diffuse", SupportedUpgradeParams.diffuseCubemap));
  113. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Reflective/Parallax Specular", SupportedUpgradeParams.specularCubemap));
  114. // Self-Illum upgrader
  115. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Self-Illumin/Diffuse", SupportedUpgradeParams.diffuseOpaque));
  116. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Self-Illumin/Bumped Diffuse", SupportedUpgradeParams.diffuseOpaque));
  117. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Self-Illumin/Parallax Diffuse", SupportedUpgradeParams.diffuseOpaque));
  118. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Self-Illumin/Specular", SupportedUpgradeParams.specularOpaque));
  119. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Self-Illumin/Bumped Specular", SupportedUpgradeParams.specularOpaque));
  120. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Self-Illumin/Parallax Specular", SupportedUpgradeParams.specularOpaque));
  121. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Self-Illumin/VertexLit", SupportedUpgradeParams.specularOpaque));
  122. // Alpha Blended
  123. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Transparent/Diffuse", SupportedUpgradeParams.diffuseAlpha));
  124. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Transparent/Specular", SupportedUpgradeParams.specularAlpha));
  125. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Transparent/Bumped Diffuse", SupportedUpgradeParams.diffuseAlpha));
  126. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Transparent/Bumped Specular", SupportedUpgradeParams.specularAlpha));
  127. // Cutout
  128. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Transparent/Cutout/Diffuse", SupportedUpgradeParams.diffuseAlphaCutout));
  129. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Transparent/Cutout/Specular", SupportedUpgradeParams.specularAlphaCutout));
  130. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Transparent/Cutout/Bumped Diffuse", SupportedUpgradeParams.diffuseAlphaCutout));
  131. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Transparent/Cutout/Bumped Specular", SupportedUpgradeParams.specularAlphaCutout));
  132. // Lightmapped
  133. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Lightmapped/Diffuse", SupportedUpgradeParams.diffuseOpaque));
  134. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Lightmapped/Specular", SupportedUpgradeParams.specularOpaque));
  135. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Lightmapped/VertexLit", SupportedUpgradeParams.specularOpaque));
  136. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Lightmapped/Bumped Diffuse", SupportedUpgradeParams.diffuseOpaque));
  137. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Lightmapped/Bumped Specular", SupportedUpgradeParams.specularOpaque));
  138. /////////////////////////////////////
  139. // Sprites Upgraders
  140. /////////////////////////////////////
  141. upgraders.Add(new StandardSimpleLightingUpgrader("Sprites/Diffuse", SupportedUpgradeParams.diffuseAlpha));
  142. /////////////////////////////////////
  143. // UI Upgraders
  144. /////////////////////////////////////
  145. upgraders.Add(new StandardSimpleLightingUpgrader("UI/Lit/Bumped", SupportedUpgradeParams.diffuseAlphaCutout));
  146. upgraders.Add(new StandardSimpleLightingUpgrader("UI/Lit/Detail", SupportedUpgradeParams.diffuseAlphaCutout));
  147. upgraders.Add(new StandardSimpleLightingUpgrader("UI/Lit/Refraction", SupportedUpgradeParams.diffuseAlphaCutout));
  148. upgraders.Add(new StandardSimpleLightingUpgrader("UI/Lit/Refraction Detail", SupportedUpgradeParams.diffuseAlphaCutout));
  149. upgraders.Add(new StandardSimpleLightingUpgrader("UI/Lit/Transparent", SupportedUpgradeParams.diffuseAlpha));
  150. /////////////////////////////////////
  151. // Mobile Upgraders /
  152. /////////////////////////////////////
  153. upgraders.Add(new StandardSimpleLightingUpgrader("Mobile/Diffuse", SupportedUpgradeParams.diffuseOpaque));
  154. upgraders.Add(new StandardSimpleLightingUpgrader("Mobile/Bumped Specular", SupportedUpgradeParams.specularOpaque));
  155. upgraders.Add(new StandardSimpleLightingUpgrader("Mobile/Bumped Specular (1 Directional Light)", SupportedUpgradeParams.specularOpaque));
  156. upgraders.Add(new StandardSimpleLightingUpgrader("Mobile/Bumped Diffuse", SupportedUpgradeParams.diffuseOpaque));
  157. upgraders.Add(new StandardSimpleLightingUpgrader("Mobile/Unlit (Supports Lightmap)", SupportedUpgradeParams.diffuseOpaque));
  158. upgraders.Add(new StandardSimpleLightingUpgrader("Mobile/VertexLit", SupportedUpgradeParams.specularOpaque));
  159. upgraders.Add(new StandardSimpleLightingUpgrader("Mobile/VertexLit (Only Directional Lights)", SupportedUpgradeParams.specularOpaque));
  160. upgraders.Add(new StandardSimpleLightingUpgrader("Mobile/Particles/VertexLit Blended", SupportedUpgradeParams.specularOpaque));
  161. ////////////////////////////////////
  162. // Terrain Upgraders //
  163. ////////////////////////////////////
  164. upgraders.Add(new TerrainUpgrader("Nature/Terrain/Standard"));
  165. upgraders.Add(new SpeedTreeUpgrader("Nature/SpeedTree"));
  166. upgraders.Add(new SpeedTreeBillboardUpgrader("Nature/SpeedTree Billboard"));
  167. upgraders.Add(new SpeedTree8Upgrader("Nature/SpeedTree8"));
  168. ////////////////////////////////////
  169. // Particle Upgraders //
  170. ////////////////////////////////////
  171. upgraders.Add(new ParticleUpgrader("Particles/Standard Surface"));
  172. upgraders.Add(new ParticleUpgrader("Particles/Standard Unlit"));
  173. upgraders.Add(new ParticleUpgrader("Particles/VertexLit Blended"));
  174. ////////////////////////////////////
  175. // Autodesk Interactive //
  176. ////////////////////////////////////
  177. upgraders.Add(new AutodeskInteractiveUpgrader("Autodesk Interactive"));
  178. }
  179. bool IsMaterialPath(string path)
  180. {
  181. if (string.IsNullOrEmpty(path))
  182. {
  183. throw new ArgumentNullException(nameof(path));
  184. }
  185. return path.EndsWith(".mat", StringComparison.OrdinalIgnoreCase);
  186. }
  187. bool ShouldUpgradeShader(Material material, HashSet<string> shaderNamesToIgnore)
  188. {
  189. if (material == null)
  190. return false;
  191. if (material.shader == null)
  192. return false;
  193. return !shaderNamesToIgnore.Contains(material.shader.name);
  194. }
  195. public override void OnInitialize(InitializeConverterContext context, Action callback)
  196. {
  197. foreach (string path in AssetDatabase.GetAllAssetPaths())
  198. {
  199. if (IsMaterialPath(path))
  200. {
  201. Material m = AssetDatabase.LoadMainAssetAtPath(path) as Material;
  202. if (!ShouldUpgradeShader(m, m_ShaderNamesToIgnore))
  203. continue;
  204. GUID guid = AssetDatabase.GUIDFromAssetPath(path);
  205. m_MaterialGUIDs.Add(guid);
  206. m_AssetsToConvert.Add(path);
  207. ConverterItemDescriptor desc = new ConverterItemDescriptor()
  208. {
  209. name = m.name,
  210. info = path,
  211. warningMessage = String.Empty,
  212. helpLink = String.Empty,
  213. };
  214. // Each converter needs to add this info using this API.
  215. context.AddAssetToConvert(desc);
  216. }
  217. }
  218. callback.Invoke();
  219. }
  220. public override void OnRun(ref RunItemContext context)
  221. {
  222. var mat = AssetDatabase.LoadAssetAtPath<Material>(context.item.descriptor.info);
  223. string message = String.Empty;
  224. if (!MaterialUpgrader.Upgrade(mat, m_Upgraders, MaterialUpgrader.UpgradeFlags.LogMessageWhenNoUpgraderFound, ref message))
  225. {
  226. context.didFail = true;
  227. context.info = message;
  228. }
  229. }
  230. public override void OnClicked(int index)
  231. {
  232. EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath<Material>(m_AssetsToConvert[index]));
  233. }
  234. }
  235. public static class SupportedUpgradeParams
  236. {
  237. static public UpgradeParams diffuseOpaque = new UpgradeParams()
  238. {
  239. surfaceType = UpgradeSurfaceType.Opaque,
  240. blendMode = UpgradeBlendMode.Alpha,
  241. alphaClip = false,
  242. specularSource = SpecularSource.NoSpecular,
  243. smoothnessSource = SmoothnessSource.BaseAlpha,
  244. };
  245. static public UpgradeParams specularOpaque = new UpgradeParams()
  246. {
  247. surfaceType = UpgradeSurfaceType.Opaque,
  248. blendMode = UpgradeBlendMode.Alpha,
  249. alphaClip = false,
  250. specularSource = SpecularSource.SpecularTextureAndColor,
  251. smoothnessSource = SmoothnessSource.BaseAlpha,
  252. };
  253. static public UpgradeParams diffuseAlpha = new UpgradeParams()
  254. {
  255. surfaceType = UpgradeSurfaceType.Transparent,
  256. blendMode = UpgradeBlendMode.Alpha,
  257. alphaClip = false,
  258. specularSource = SpecularSource.NoSpecular,
  259. smoothnessSource = SmoothnessSource.SpecularAlpha,
  260. };
  261. static public UpgradeParams specularAlpha = new UpgradeParams()
  262. {
  263. surfaceType = UpgradeSurfaceType.Transparent,
  264. blendMode = UpgradeBlendMode.Alpha,
  265. alphaClip = false,
  266. specularSource = SpecularSource.SpecularTextureAndColor,
  267. smoothnessSource = SmoothnessSource.SpecularAlpha,
  268. };
  269. static public UpgradeParams diffuseAlphaCutout = new UpgradeParams()
  270. {
  271. surfaceType = UpgradeSurfaceType.Opaque,
  272. blendMode = UpgradeBlendMode.Alpha,
  273. alphaClip = true,
  274. specularSource = SpecularSource.NoSpecular,
  275. smoothnessSource = SmoothnessSource.SpecularAlpha,
  276. };
  277. static public UpgradeParams specularAlphaCutout = new UpgradeParams()
  278. {
  279. surfaceType = UpgradeSurfaceType.Opaque,
  280. blendMode = UpgradeBlendMode.Alpha,
  281. alphaClip = true,
  282. specularSource = SpecularSource.SpecularTextureAndColor,
  283. smoothnessSource = SmoothnessSource.SpecularAlpha,
  284. };
  285. static public UpgradeParams diffuseCubemap = new UpgradeParams()
  286. {
  287. surfaceType = UpgradeSurfaceType.Opaque,
  288. blendMode = UpgradeBlendMode.Alpha,
  289. alphaClip = false,
  290. specularSource = SpecularSource.NoSpecular,
  291. smoothnessSource = SmoothnessSource.BaseAlpha,
  292. };
  293. static public UpgradeParams specularCubemap = new UpgradeParams()
  294. {
  295. surfaceType = UpgradeSurfaceType.Opaque,
  296. blendMode = UpgradeBlendMode.Alpha,
  297. alphaClip = false,
  298. specularSource = SpecularSource.SpecularTextureAndColor,
  299. smoothnessSource = SmoothnessSource.BaseAlpha,
  300. };
  301. static public UpgradeParams diffuseCubemapAlpha = new UpgradeParams()
  302. {
  303. surfaceType = UpgradeSurfaceType.Transparent,
  304. blendMode = UpgradeBlendMode.Alpha,
  305. alphaClip = false,
  306. specularSource = SpecularSource.NoSpecular,
  307. smoothnessSource = SmoothnessSource.BaseAlpha,
  308. };
  309. static public UpgradeParams specularCubemapAlpha = new UpgradeParams()
  310. {
  311. surfaceType = UpgradeSurfaceType.Transparent,
  312. blendMode = UpgradeBlendMode.Alpha,
  313. alphaClip = false,
  314. specularSource = SpecularSource.SpecularTextureAndColor,
  315. smoothnessSource = SmoothnessSource.BaseAlpha,
  316. };
  317. }
  318. public class StandardUpgrader : MaterialUpgrader
  319. {
  320. enum LegacyRenderingMode
  321. {
  322. Opaque,
  323. Cutout,
  324. Fade, // Old school alpha-blending mode, fresnel does not affect amount of transparency
  325. Transparent // Physically plausible transparency mode, implemented as alpha pre-multiply
  326. }
  327. public static void UpdateStandardMaterialKeywords(Material material)
  328. {
  329. if (material == null)
  330. throw new ArgumentNullException("material");
  331. if (material.GetTexture("_MetallicGlossMap"))
  332. material.SetFloat("_Smoothness", material.GetFloat("_GlossMapScale"));
  333. else
  334. material.SetFloat("_Smoothness", material.GetFloat("_Glossiness"));
  335. if (material.IsKeywordEnabled("_ALPHATEST_ON"))
  336. {
  337. material.SetFloat("_AlphaClip", 1.0f);
  338. }
  339. material.SetFloat("_WorkflowMode", 1.0f);
  340. CoreUtils.SetKeyword(material, "_OCCLUSIONMAP", material.GetTexture("_OcclusionMap"));
  341. CoreUtils.SetKeyword(material, "_METALLICSPECGLOSSMAP", material.GetTexture("_MetallicGlossMap"));
  342. UpdateSurfaceTypeAndBlendMode(material);
  343. BaseShaderGUI.SetupMaterialBlendMode(material);
  344. }
  345. public static void UpdateStandardSpecularMaterialKeywords(Material material)
  346. {
  347. if (material == null)
  348. throw new ArgumentNullException("material");
  349. if (material.GetTexture("_SpecGlossMap"))
  350. material.SetFloat("_Smoothness", material.GetFloat("_GlossMapScale"));
  351. else
  352. material.SetFloat("_Smoothness", material.GetFloat("_Glossiness"));
  353. material.SetFloat("_WorkflowMode", 0.0f);
  354. CoreUtils.SetKeyword(material, "_OCCLUSIONMAP", material.GetTexture("_OcclusionMap"));
  355. CoreUtils.SetKeyword(material, "_METALLICSPECGLOSSMAP", material.GetTexture("_SpecGlossMap"));
  356. CoreUtils.SetKeyword(material, "_SPECULAR_SETUP", true);
  357. UpdateSurfaceTypeAndBlendMode(material);
  358. BaseShaderGUI.SetupMaterialBlendMode(material);
  359. }
  360. // Converts from legacy RenderingMode to new SurfaceType and BlendMode
  361. static void UpdateSurfaceTypeAndBlendMode(Material material)
  362. {
  363. // Property _Mode is already renamed to _Surface at this point
  364. var legacyRenderingMode = (LegacyRenderingMode)material.GetFloat("_Surface");
  365. if (legacyRenderingMode == LegacyRenderingMode.Transparent)
  366. {
  367. material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
  368. material.SetFloat("_Surface", (float)BaseShaderGUI.SurfaceType.Transparent);
  369. material.SetFloat("_Blend", (float)BaseShaderGUI.BlendMode.Premultiply);
  370. }
  371. else if (legacyRenderingMode == LegacyRenderingMode.Fade)
  372. {
  373. material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
  374. material.SetFloat("_Surface", (float)BaseShaderGUI.SurfaceType.Transparent);
  375. material.SetFloat("_Blend", (float)BaseShaderGUI.BlendMode.Alpha);
  376. }
  377. else
  378. {
  379. material.DisableKeyword("_SURFACE_TYPE_TRANSPARENT");
  380. material.SetFloat("_Surface", (float)BaseShaderGUI.SurfaceType.Opaque);
  381. }
  382. }
  383. public StandardUpgrader(string oldShaderName)
  384. {
  385. if (oldShaderName == null)
  386. throw new ArgumentNullException("oldShaderName");
  387. string standardShaderPath = ShaderUtils.GetShaderPath(ShaderPathID.Lit);
  388. if (oldShaderName.Contains("Specular"))
  389. {
  390. RenameShader(oldShaderName, standardShaderPath, UpdateStandardSpecularMaterialKeywords);
  391. }
  392. else
  393. {
  394. RenameShader(oldShaderName, standardShaderPath, UpdateStandardMaterialKeywords);
  395. }
  396. RenameFloat("_Mode", "_Surface");
  397. RenameTexture("_MainTex", "_BaseMap");
  398. RenameColor("_Color", "_BaseColor");
  399. RenameFloat("_GlossyReflections", "_EnvironmentReflections");
  400. }
  401. }
  402. internal class StandardSimpleLightingUpgrader : MaterialUpgrader
  403. {
  404. public StandardSimpleLightingUpgrader(string oldShaderName, UpgradeParams upgradeParams)
  405. {
  406. if (oldShaderName == null)
  407. throw new ArgumentNullException("oldShaderName");
  408. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.SimpleLit), UpdateMaterialKeywords);
  409. SetFloat("_Surface", (float)upgradeParams.surfaceType);
  410. SetFloat("_Blend", (float)upgradeParams.blendMode);
  411. SetFloat("_AlphaClip", upgradeParams.alphaClip ? 1 : 0);
  412. SetFloat("_SpecularHighlights", (float)upgradeParams.specularSource);
  413. SetFloat("_SmoothnessSource", (float)upgradeParams.smoothnessSource);
  414. RenameTexture("_MainTex", "_BaseMap");
  415. RenameColor("_Color", "_BaseColor");
  416. RenameFloat("_Shininess", "_Smoothness");
  417. if (oldShaderName.Contains("Legacy Shaders/Self-Illumin"))
  418. {
  419. RenameTexture("_Illum", "_EmissionMap");
  420. RemoveTexture("_Illum");
  421. SetColor("_EmissionColor", Color.white);
  422. }
  423. }
  424. public static void UpdateMaterialKeywords(Material material)
  425. {
  426. if (material == null)
  427. throw new ArgumentNullException("material");
  428. material.shaderKeywords = null;
  429. BaseShaderGUI.SetupMaterialBlendMode(material);
  430. UpdateMaterialSpecularSource(material);
  431. CoreUtils.SetKeyword(material, "_NORMALMAP", material.GetTexture("_BumpMap"));
  432. // A material's GI flag internally keeps track of whether emission is enabled at all, it's enabled but has no effect
  433. // or is enabled and may be modified at runtime. This state depends on the values of the current flag and emissive color.
  434. // The fixup routine makes sure that the material is in the correct state if/when changes are made to the mode or color.
  435. MaterialEditor.FixupEmissiveFlag(material);
  436. bool shouldEmissionBeEnabled = (material.globalIlluminationFlags & MaterialGlobalIlluminationFlags.EmissiveIsBlack) == 0;
  437. CoreUtils.SetKeyword(material, "_EMISSION", shouldEmissionBeEnabled);
  438. }
  439. private static void UpdateMaterialSpecularSource(Material material)
  440. {
  441. SpecularSource specSource = (SpecularSource)material.GetFloat("_SpecSource");
  442. if (specSource == SpecularSource.NoSpecular)
  443. {
  444. CoreUtils.SetKeyword(material, "_SPECGLOSSMAP", false);
  445. CoreUtils.SetKeyword(material, "_SPECULAR_COLOR", false);
  446. CoreUtils.SetKeyword(material, "_GLOSSINESS_FROM_BASE_ALPHA", false);
  447. }
  448. else
  449. {
  450. SmoothnessSource glossSource = (SmoothnessSource)material.GetFloat("_SmoothnessSource");
  451. bool hasGlossMap = material.GetTexture("_SpecGlossMap");
  452. CoreUtils.SetKeyword(material, "_SPECGLOSSMAP", hasGlossMap);
  453. CoreUtils.SetKeyword(material, "_SPECULAR_COLOR", !hasGlossMap);
  454. CoreUtils.SetKeyword(material, "_GLOSSINESS_FROM_BASE_ALPHA", glossSource == SmoothnessSource.BaseAlpha);
  455. }
  456. }
  457. }
  458. public class TerrainUpgrader : MaterialUpgrader
  459. {
  460. public TerrainUpgrader(string oldShaderName)
  461. {
  462. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.TerrainLit));
  463. }
  464. }
  465. internal class SpeedTreeUpgrader : MaterialUpgrader
  466. {
  467. internal SpeedTreeUpgrader(string oldShaderName)
  468. {
  469. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.SpeedTree7));
  470. }
  471. }
  472. internal class SpeedTreeBillboardUpgrader : MaterialUpgrader
  473. {
  474. internal SpeedTreeBillboardUpgrader(string oldShaderName)
  475. {
  476. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.SpeedTree7Billboard));
  477. }
  478. }
  479. internal class SpeedTree8Upgrader : MaterialUpgrader
  480. {
  481. internal SpeedTree8Upgrader(string oldShaderName)
  482. {
  483. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.SpeedTree8));
  484. }
  485. }
  486. public class ParticleUpgrader : MaterialUpgrader
  487. {
  488. public ParticleUpgrader(string oldShaderName)
  489. {
  490. if (oldShaderName == null)
  491. throw new ArgumentNullException("oldShaderName");
  492. RenameFloat("_Mode", "_Surface");
  493. if (oldShaderName.Contains("Unlit"))
  494. {
  495. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.ParticlesUnlit), UpdateUnlit);
  496. }
  497. else
  498. {
  499. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.ParticlesLit),
  500. UpdateStandardSurface);
  501. RenameFloat("_Glossiness", "_Smoothness");
  502. }
  503. RenameTexture("_MainTex", "_BaseMap");
  504. RenameColor("_Color", "_BaseColor");
  505. RenameFloat("_FlipbookMode", "_FlipbookBlending");
  506. }
  507. public static void UpdateStandardSurface(Material material)
  508. {
  509. UpdateSurfaceBlendModes(material);
  510. }
  511. public static void UpdateUnlit(Material material)
  512. {
  513. UpdateSurfaceBlendModes(material);
  514. }
  515. public static void UpdateSurfaceBlendModes(Material material)
  516. {
  517. switch (material.GetFloat("_Mode"))
  518. {
  519. case 0: // opaque
  520. material.DisableKeyword("_SURFACE_TYPE_TRANSPARENT");
  521. material.SetFloat("_Surface", (int)UpgradeSurfaceType.Opaque);
  522. break;
  523. case 1: // cutout > alphatest
  524. material.DisableKeyword("_SURFACE_TYPE_TRANSPARENT");
  525. material.SetFloat("_Surface", (int)UpgradeSurfaceType.Opaque);
  526. material.SetFloat("_AlphaClip", 1);
  527. break;
  528. case 2: // fade > alpha
  529. material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
  530. material.SetFloat("_Surface", (int)UpgradeSurfaceType.Transparent);
  531. material.SetFloat("_Blend", (int)UpgradeBlendMode.Alpha);
  532. break;
  533. case 3: // transparent > premul
  534. material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
  535. material.SetFloat("_Surface", (int)UpgradeSurfaceType.Transparent);
  536. material.SetFloat("_Blend", (int)UpgradeBlendMode.Premultiply);
  537. break;
  538. case 4: // add
  539. material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
  540. material.SetFloat("_Surface", (int)UpgradeSurfaceType.Transparent);
  541. material.SetFloat("_Blend", (int)UpgradeBlendMode.Additive);
  542. break;
  543. case 5: // sub > none
  544. break;
  545. case 6: // mod > multiply
  546. material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
  547. material.SetFloat("_Surface", (int)UpgradeSurfaceType.Transparent);
  548. material.SetFloat("_Blend", (int)UpgradeBlendMode.Multiply);
  549. break;
  550. }
  551. }
  552. }
  553. public class AutodeskInteractiveUpgrader : MaterialUpgrader
  554. {
  555. public AutodeskInteractiveUpgrader(string oldShaderName)
  556. {
  557. RenameShader(oldShaderName, "Universal Render Pipeline/Autodesk Interactive/Autodesk Interactive");
  558. }
  559. public override void Convert(Material srcMaterial, Material dstMaterial)
  560. {
  561. base.Convert(srcMaterial, dstMaterial);
  562. dstMaterial.SetFloat("_UseColorMap", srcMaterial.GetTexture("_MainTex") ? 1.0f : .0f);
  563. dstMaterial.SetFloat("_UseMetallicMap", srcMaterial.GetTexture("_MetallicGlossMap") ? 1.0f : .0f);
  564. dstMaterial.SetFloat("_UseNormalMap", srcMaterial.GetTexture("_BumpMap") ? 1.0f : .0f);
  565. dstMaterial.SetFloat("_UseRoughnessMap", srcMaterial.GetTexture("_SpecGlossMap") ? 1.0f : .0f);
  566. dstMaterial.SetFloat("_UseEmissiveMap", srcMaterial.GetTexture("_EmissionMap") ? 1.0f : .0f);
  567. dstMaterial.SetFloat("_UseAoMap", srcMaterial.GetTexture("_OcclusionMap") ? 1.0f : .0f);
  568. dstMaterial.SetVector("_UvOffset", srcMaterial.GetTextureOffset("_MainTex"));
  569. dstMaterial.SetVector("_UvTiling", srcMaterial.GetTextureScale("_MainTex"));
  570. }
  571. }
  572. }