UniversalRenderPipelineMaterialUpgrader.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. using UnityEngine.Rendering.Universal;
  6. using UnityEngine.Scripting.APIUpdating;
  7. namespace UnityEditor.Rendering.Universal
  8. {
  9. internal sealed class UniversalRenderPipelineMaterialUpgrader
  10. {
  11. private UniversalRenderPipelineMaterialUpgrader()
  12. {
  13. }
  14. [MenuItem("Edit/Render Pipeline/Universal Render Pipeline/Upgrade Project Materials to UniversalRP Materials", priority = CoreUtils.editMenuPriority2)]
  15. private static void UpgradeProjectMaterials()
  16. {
  17. List<MaterialUpgrader> upgraders = new List<MaterialUpgrader>();
  18. GetUpgraders(ref upgraders);
  19. HashSet<string> shaderNamesToIgnore = new HashSet<string>();
  20. GetShaderNamesToIgnore(ref shaderNamesToIgnore);
  21. MaterialUpgrader.UpgradeProjectFolder(upgraders, shaderNamesToIgnore, "Upgrade to UniversalRP Materials", MaterialUpgrader.UpgradeFlags.LogMessageWhenNoUpgraderFound);
  22. }
  23. [MenuItem("Edit/Render Pipeline/Universal Render Pipeline/Upgrade Selected Materials to UniversalRP Materials", priority = CoreUtils.editMenuPriority2)]
  24. private static void UpgradeSelectedMaterials()
  25. {
  26. List<MaterialUpgrader> upgraders = new List<MaterialUpgrader>();
  27. GetUpgraders(ref upgraders);
  28. HashSet<string> shaderNamesToIgnore = new HashSet<string>();
  29. GetShaderNamesToIgnore(ref shaderNamesToIgnore);
  30. MaterialUpgrader.UpgradeSelection(upgraders, shaderNamesToIgnore, "Upgrade to UniversalRP Materials", MaterialUpgrader.UpgradeFlags.LogMessageWhenNoUpgraderFound);
  31. }
  32. private static void GetShaderNamesToIgnore(ref HashSet<string> shadersToIgnore)
  33. {
  34. shadersToIgnore.Add("Universal Render Pipeline/Baked Lit");
  35. shadersToIgnore.Add("Universal Render Pipeline/Lit");
  36. shadersToIgnore.Add("Universal Render Pipeline/Particles/Lit");
  37. shadersToIgnore.Add("Universal Render Pipeline/Particles/Simple Lit");
  38. shadersToIgnore.Add("Universal Render Pipeline/Particles/Unlit");
  39. shadersToIgnore.Add("Universal Render Pipeline/Simple Lit");
  40. shadersToIgnore.Add("Universal Render Pipeline/Nature/SpeedTree7");
  41. shadersToIgnore.Add("Universal Render Pipeline/Nature/SpeedTree7 Billboard");
  42. shadersToIgnore.Add("Universal Render Pipeline/Nature/SpeedTree8");
  43. shadersToIgnore.Add("Universal Render Pipeline/2D/Sprite-Lit-Default");
  44. shadersToIgnore.Add("Universal Render Pipeline/Terrain/Lit");
  45. shadersToIgnore.Add("Universal Render Pipeline/Unlit");
  46. shadersToIgnore.Add("Sprites/Default");
  47. }
  48. private static void GetUpgraders(ref List<MaterialUpgrader> upgraders)
  49. {
  50. /////////////////////////////////////
  51. // Unity Standard Upgraders //
  52. /////////////////////////////////////
  53. upgraders.Add(new StandardUpgrader("Standard"));
  54. upgraders.Add(new StandardUpgrader("Standard (Specular setup)"));
  55. /////////////////////////////////////
  56. // Legacy Shaders upgraders /
  57. /////////////////////////////////////
  58. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Diffuse", SupportedUpgradeParams.diffuseOpaque));
  59. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Diffuse Detail", SupportedUpgradeParams.diffuseOpaque));
  60. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Diffuse Fast", SupportedUpgradeParams.diffuseOpaque));
  61. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Specular", SupportedUpgradeParams.specularOpaque));
  62. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Bumped Diffuse", SupportedUpgradeParams.diffuseOpaque));
  63. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Bumped Specular", SupportedUpgradeParams.specularOpaque));
  64. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Parallax Diffuse", SupportedUpgradeParams.diffuseOpaque));
  65. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Parallax Specular", SupportedUpgradeParams.specularOpaque));
  66. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/VertexLit", SupportedUpgradeParams.specularOpaque));
  67. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Transparent/Cutout/VertexLit", SupportedUpgradeParams.specularAlphaCutout));
  68. // Reflective
  69. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Reflective/Bumped Diffuse", SupportedUpgradeParams.diffuseCubemap));
  70. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Reflective/Bumped Specular", SupportedUpgradeParams.specularCubemap));
  71. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Reflective/Bumped Unlit", SupportedUpgradeParams.diffuseCubemap));
  72. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Reflective/Bumped VertexLit", SupportedUpgradeParams.diffuseCubemap));
  73. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Reflective/Diffuse", SupportedUpgradeParams.diffuseCubemap));
  74. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Reflective/Specular", SupportedUpgradeParams.specularCubemap));
  75. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Reflective/VertexLit", SupportedUpgradeParams.diffuseCubemap));
  76. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Reflective/Parallax Diffuse", SupportedUpgradeParams.diffuseCubemap));
  77. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Reflective/Parallax Specular", SupportedUpgradeParams.specularCubemap));
  78. // Self-Illum upgrader
  79. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Self-Illumin/Diffuse", SupportedUpgradeParams.diffuseOpaque));
  80. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Self-Illumin/Bumped Diffuse", SupportedUpgradeParams.diffuseOpaque));
  81. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Self-Illumin/Parallax Diffuse", SupportedUpgradeParams.diffuseOpaque));
  82. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Self-Illumin/Specular", SupportedUpgradeParams.specularOpaque));
  83. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Self-Illumin/Bumped Specular", SupportedUpgradeParams.specularOpaque));
  84. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Self-Illumin/Parallax Specular", SupportedUpgradeParams.specularOpaque));
  85. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Self-Illumin/VertexLit", SupportedUpgradeParams.specularOpaque));
  86. // Alpha Blended
  87. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Transparent/Diffuse", SupportedUpgradeParams.diffuseAlpha));
  88. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Transparent/Specular", SupportedUpgradeParams.specularAlpha));
  89. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Transparent/Bumped Diffuse", SupportedUpgradeParams.diffuseAlpha));
  90. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Transparent/Bumped Specular", SupportedUpgradeParams.specularAlpha));
  91. // Cutout
  92. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Transparent/Cutout/Diffuse", SupportedUpgradeParams.diffuseAlphaCutout));
  93. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Transparent/Cutout/Specular", SupportedUpgradeParams.specularAlphaCutout));
  94. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Transparent/Cutout/Bumped Diffuse", SupportedUpgradeParams.diffuseAlphaCutout));
  95. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Transparent/Cutout/Bumped Specular", SupportedUpgradeParams.specularAlphaCutout));
  96. // Lightmapped
  97. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Lightmapped/Diffuse", SupportedUpgradeParams.diffuseOpaque));
  98. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Lightmapped/Specular", SupportedUpgradeParams.specularOpaque));
  99. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Lightmapped/VertexLit", SupportedUpgradeParams.specularOpaque));
  100. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Lightmapped/Bumped Diffuse", SupportedUpgradeParams.diffuseOpaque));
  101. upgraders.Add(new StandardSimpleLightingUpgrader("Legacy Shaders/Lightmapped/Bumped Specular", SupportedUpgradeParams.specularOpaque));
  102. /////////////////////////////////////
  103. // Sprites Upgraders
  104. /////////////////////////////////////
  105. upgraders.Add(new StandardSimpleLightingUpgrader("Sprites/Diffuse", SupportedUpgradeParams.diffuseAlpha));
  106. /////////////////////////////////////
  107. // UI Upgraders
  108. /////////////////////////////////////
  109. upgraders.Add(new StandardSimpleLightingUpgrader("UI/Lit/Bumped", SupportedUpgradeParams.diffuseAlphaCutout));
  110. upgraders.Add(new StandardSimpleLightingUpgrader("UI/Lit/Detail", SupportedUpgradeParams.diffuseAlphaCutout));
  111. upgraders.Add(new StandardSimpleLightingUpgrader("UI/Lit/Refraction", SupportedUpgradeParams.diffuseAlphaCutout));
  112. upgraders.Add(new StandardSimpleLightingUpgrader("UI/Lit/Refraction Detail", SupportedUpgradeParams.diffuseAlphaCutout));
  113. upgraders.Add(new StandardSimpleLightingUpgrader("UI/Lit/Transparent", SupportedUpgradeParams.diffuseAlpha));
  114. /////////////////////////////////////
  115. // Mobile Upgraders /
  116. /////////////////////////////////////
  117. upgraders.Add(new StandardSimpleLightingUpgrader("Mobile/Diffuse", SupportedUpgradeParams.diffuseOpaque));
  118. upgraders.Add(new StandardSimpleLightingUpgrader("Mobile/Bumped Specular", SupportedUpgradeParams.specularOpaque));
  119. upgraders.Add(new StandardSimpleLightingUpgrader("Mobile/Bumped Specular (1 Directional Light)", SupportedUpgradeParams.specularOpaque));
  120. upgraders.Add(new StandardSimpleLightingUpgrader("Mobile/Bumped Diffuse", SupportedUpgradeParams.diffuseOpaque));
  121. upgraders.Add(new StandardSimpleLightingUpgrader("Mobile/Unlit (Supports Lightmap)", SupportedUpgradeParams.diffuseOpaque));
  122. upgraders.Add(new StandardSimpleLightingUpgrader("Mobile/VertexLit", SupportedUpgradeParams.specularOpaque));
  123. upgraders.Add(new StandardSimpleLightingUpgrader("Mobile/VertexLit (Only Directional Lights)", SupportedUpgradeParams.specularOpaque));
  124. upgraders.Add(new StandardSimpleLightingUpgrader("Mobile/Particles/VertexLit Blended", SupportedUpgradeParams.specularOpaque));
  125. ////////////////////////////////////
  126. // Terrain Upgraders //
  127. ////////////////////////////////////
  128. upgraders.Add(new TerrainUpgrader("Nature/Terrain/Standard"));
  129. ////////////////////////////////////
  130. // Particle Upgraders //
  131. ////////////////////////////////////
  132. upgraders.Add(new ParticleUpgrader("Particles/Standard Surface"));
  133. upgraders.Add(new ParticleUpgrader("Particles/Standard Unlit"));
  134. upgraders.Add(new ParticleUpgrader("Particles/VertexLit Blended"));
  135. ////////////////////////////////////
  136. // Autodesk Interactive //
  137. ////////////////////////////////////
  138. upgraders.Add(new AutodeskInteractiveUpgrader("Autodesk Interactive"));
  139. }
  140. }
  141. [MovedFrom("UnityEditor.Rendering.LWRP")] public static class SupportedUpgradeParams
  142. {
  143. static public UpgradeParams diffuseOpaque = new UpgradeParams()
  144. {
  145. surfaceType = UpgradeSurfaceType.Opaque,
  146. blendMode = UpgradeBlendMode.Alpha,
  147. alphaClip = false,
  148. specularSource = SpecularSource.NoSpecular,
  149. smoothnessSource = SmoothnessSource.BaseAlpha,
  150. };
  151. static public UpgradeParams specularOpaque = new UpgradeParams()
  152. {
  153. surfaceType = UpgradeSurfaceType.Opaque,
  154. blendMode = UpgradeBlendMode.Alpha,
  155. alphaClip = false,
  156. specularSource = SpecularSource.SpecularTextureAndColor,
  157. smoothnessSource = SmoothnessSource.BaseAlpha,
  158. };
  159. static public UpgradeParams diffuseAlpha = new UpgradeParams()
  160. {
  161. surfaceType = UpgradeSurfaceType.Transparent,
  162. blendMode = UpgradeBlendMode.Alpha,
  163. alphaClip = false,
  164. specularSource = SpecularSource.NoSpecular,
  165. smoothnessSource = SmoothnessSource.SpecularAlpha,
  166. };
  167. static public UpgradeParams specularAlpha = new UpgradeParams()
  168. {
  169. surfaceType = UpgradeSurfaceType.Transparent,
  170. blendMode = UpgradeBlendMode.Alpha,
  171. alphaClip = false,
  172. specularSource = SpecularSource.SpecularTextureAndColor,
  173. smoothnessSource = SmoothnessSource.SpecularAlpha,
  174. };
  175. static public UpgradeParams diffuseAlphaCutout = new UpgradeParams()
  176. {
  177. surfaceType = UpgradeSurfaceType.Opaque,
  178. blendMode = UpgradeBlendMode.Alpha,
  179. alphaClip = true,
  180. specularSource = SpecularSource.NoSpecular,
  181. smoothnessSource = SmoothnessSource.SpecularAlpha,
  182. };
  183. static public UpgradeParams specularAlphaCutout = new UpgradeParams()
  184. {
  185. surfaceType = UpgradeSurfaceType.Opaque,
  186. blendMode = UpgradeBlendMode.Alpha,
  187. alphaClip = true,
  188. specularSource = SpecularSource.SpecularTextureAndColor,
  189. smoothnessSource = SmoothnessSource.SpecularAlpha,
  190. };
  191. static public UpgradeParams diffuseCubemap = new UpgradeParams()
  192. {
  193. surfaceType = UpgradeSurfaceType.Opaque,
  194. blendMode = UpgradeBlendMode.Alpha,
  195. alphaClip = false,
  196. specularSource = SpecularSource.NoSpecular,
  197. smoothnessSource = SmoothnessSource.BaseAlpha,
  198. };
  199. static public UpgradeParams specularCubemap = new UpgradeParams()
  200. {
  201. surfaceType = UpgradeSurfaceType.Opaque,
  202. blendMode = UpgradeBlendMode.Alpha,
  203. alphaClip = false,
  204. specularSource = SpecularSource.SpecularTextureAndColor,
  205. smoothnessSource = SmoothnessSource.BaseAlpha,
  206. };
  207. static public UpgradeParams diffuseCubemapAlpha = new UpgradeParams()
  208. {
  209. surfaceType = UpgradeSurfaceType.Transparent,
  210. blendMode = UpgradeBlendMode.Alpha,
  211. alphaClip = false,
  212. specularSource = SpecularSource.NoSpecular,
  213. smoothnessSource = SmoothnessSource.BaseAlpha,
  214. };
  215. static public UpgradeParams specularCubemapAlpha = new UpgradeParams()
  216. {
  217. surfaceType = UpgradeSurfaceType.Transparent,
  218. blendMode = UpgradeBlendMode.Alpha,
  219. alphaClip = false,
  220. specularSource = SpecularSource.SpecularTextureAndColor,
  221. smoothnessSource = SmoothnessSource.BaseAlpha,
  222. };
  223. }
  224. [MovedFrom("UnityEditor.Rendering.LWRP")] public class StandardUpgrader : MaterialUpgrader
  225. {
  226. enum LegacyRenderingMode
  227. {
  228. Opaque,
  229. Cutout,
  230. Fade, // Old school alpha-blending mode, fresnel does not affect amount of transparency
  231. Transparent // Physically plausible transparency mode, implemented as alpha pre-multiply
  232. }
  233. public static void UpdateStandardMaterialKeywords(Material material)
  234. {
  235. if (material == null)
  236. throw new ArgumentNullException("material");
  237. if (material.GetTexture("_MetallicGlossMap"))
  238. material.SetFloat("_Smoothness", material.GetFloat("_GlossMapScale"));
  239. else
  240. material.SetFloat("_Smoothness", material.GetFloat("_Glossiness"));
  241. material.SetFloat("_WorkflowMode", 1.0f);
  242. CoreUtils.SetKeyword(material, "_OCCLUSIONMAP", material.GetTexture("_OcclusionMap"));
  243. CoreUtils.SetKeyword(material, "_METALLICSPECGLOSSMAP", material.GetTexture("_MetallicGlossMap"));
  244. UpdateSurfaceTypeAndBlendMode(material);
  245. BaseShaderGUI.SetupMaterialBlendMode(material);
  246. }
  247. public static void UpdateStandardSpecularMaterialKeywords(Material material)
  248. {
  249. if (material == null)
  250. throw new ArgumentNullException("material");
  251. if (material.GetTexture("_SpecGlossMap"))
  252. material.SetFloat("_Smoothness", material.GetFloat("_GlossMapScale"));
  253. else
  254. material.SetFloat("_Smoothness", material.GetFloat("_Glossiness"));
  255. material.SetFloat("_WorkflowMode", 0.0f);
  256. CoreUtils.SetKeyword(material, "_OCCLUSIONMAP", material.GetTexture("_OcclusionMap"));
  257. CoreUtils.SetKeyword(material, "_METALLICSPECGLOSSMAP", material.GetTexture("_SpecGlossMap"));
  258. CoreUtils.SetKeyword(material, "_SPECULAR_SETUP", true);
  259. UpdateSurfaceTypeAndBlendMode(material);
  260. BaseShaderGUI.SetupMaterialBlendMode(material);
  261. }
  262. // Converts from legacy RenderingMode to new SurfaceType and BlendMode
  263. static void UpdateSurfaceTypeAndBlendMode(Material material)
  264. {
  265. // Property _Mode is already renamed to _Surface at this point
  266. var legacyRenderingMode = (LegacyRenderingMode)material.GetFloat("_Surface");
  267. if (legacyRenderingMode == LegacyRenderingMode.Transparent)
  268. {
  269. material.SetInt("_Surface", (int)BaseShaderGUI.SurfaceType.Transparent);
  270. material.SetInt("_Blend", (int)BaseShaderGUI.BlendMode.Premultiply);
  271. }
  272. else if (legacyRenderingMode == LegacyRenderingMode.Fade)
  273. {
  274. material.SetInt("_Surface", (int)BaseShaderGUI.SurfaceType.Transparent);
  275. material.SetInt("_Blend", (int)BaseShaderGUI.BlendMode.Alpha);
  276. }
  277. else
  278. {
  279. material.SetInt("_Surface", (int)BaseShaderGUI.SurfaceType.Opaque);
  280. }
  281. }
  282. public StandardUpgrader(string oldShaderName)
  283. {
  284. if (oldShaderName == null)
  285. throw new ArgumentNullException("oldShaderName");
  286. string standardShaderPath = ShaderUtils.GetShaderPath(ShaderPathID.Lit);
  287. if (oldShaderName.Contains("Specular"))
  288. {
  289. RenameShader(oldShaderName, standardShaderPath, UpdateStandardSpecularMaterialKeywords);
  290. }
  291. else
  292. {
  293. RenameShader(oldShaderName, standardShaderPath, UpdateStandardMaterialKeywords);
  294. }
  295. RenameFloat("_Mode", "_Surface");
  296. RenameTexture("_MainTex", "_BaseMap");
  297. RenameColor("_Color", "_BaseColor");
  298. RenameFloat("_GlossyReflections", "_EnvironmentReflections");
  299. }
  300. }
  301. internal class StandardSimpleLightingUpgrader : MaterialUpgrader
  302. {
  303. public StandardSimpleLightingUpgrader(string oldShaderName, UpgradeParams upgradeParams)
  304. {
  305. if (oldShaderName == null)
  306. throw new ArgumentNullException("oldShaderName");
  307. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.SimpleLit), UpdateMaterialKeywords);
  308. SetFloat("_Surface", (float)upgradeParams.surfaceType);
  309. SetFloat("_Blend", (float)upgradeParams.blendMode);
  310. SetFloat("_AlphaClip", upgradeParams.alphaClip ? 1 : 0);
  311. SetFloat("_SpecularHighlights", (float)upgradeParams.specularSource);
  312. SetFloat("_SmoothnessSource", (float)upgradeParams.smoothnessSource);
  313. RenameTexture("_MainTex", "_BaseMap");
  314. RenameColor("_Color", "_BaseColor");
  315. RenameFloat("_Shininess", "_Smoothness");
  316. if (oldShaderName.Contains("Legacy Shaders/Self-Illumin"))
  317. {
  318. RenameTexture("_Illum", "_EmissionMap");
  319. RemoveTexture("_Illum");
  320. SetColor("_EmissionColor", Color.white);
  321. }
  322. }
  323. public static void UpdateMaterialKeywords(Material material)
  324. {
  325. if (material == null)
  326. throw new ArgumentNullException("material");
  327. material.shaderKeywords = null;
  328. BaseShaderGUI.SetupMaterialBlendMode(material);
  329. UpdateMaterialSpecularSource(material);
  330. CoreUtils.SetKeyword(material, "_NORMALMAP", material.GetTexture("_BumpMap"));
  331. // A material's GI flag internally keeps track of whether emission is enabled at all, it's enabled but has no effect
  332. // or is enabled and may be modified at runtime. This state depends on the values of the current flag and emissive color.
  333. // The fixup routine makes sure that the material is in the correct state if/when changes are made to the mode or color.
  334. MaterialEditor.FixupEmissiveFlag(material);
  335. bool shouldEmissionBeEnabled = (material.globalIlluminationFlags & MaterialGlobalIlluminationFlags.EmissiveIsBlack) == 0;
  336. CoreUtils.SetKeyword(material, "_EMISSION", shouldEmissionBeEnabled);
  337. }
  338. private static void UpdateMaterialSpecularSource(Material material)
  339. {
  340. SpecularSource specSource = (SpecularSource)material.GetFloat("_SpecSource");
  341. if (specSource == SpecularSource.NoSpecular)
  342. {
  343. CoreUtils.SetKeyword(material, "_SPECGLOSSMAP", false);
  344. CoreUtils.SetKeyword(material, "_SPECULAR_COLOR", false);
  345. CoreUtils.SetKeyword(material, "_GLOSSINESS_FROM_BASE_ALPHA", false);
  346. }
  347. else
  348. {
  349. SmoothnessSource glossSource = (SmoothnessSource)material.GetFloat("_SmoothnessSource");
  350. bool hasGlossMap = material.GetTexture("_SpecGlossMap");
  351. CoreUtils.SetKeyword(material, "_SPECGLOSSMAP", hasGlossMap);
  352. CoreUtils.SetKeyword(material, "_SPECULAR_COLOR", !hasGlossMap);
  353. CoreUtils.SetKeyword(material, "_GLOSSINESS_FROM_BASE_ALPHA", glossSource == SmoothnessSource.BaseAlpha);
  354. }
  355. }
  356. }
  357. [MovedFrom("UnityEditor.Rendering.LWRP")] public class TerrainUpgrader : MaterialUpgrader
  358. {
  359. public TerrainUpgrader(string oldShaderName)
  360. {
  361. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.TerrainLit));
  362. }
  363. }
  364. [MovedFrom("UnityEditor.Rendering.LWRP")] public class ParticleUpgrader : MaterialUpgrader
  365. {
  366. public ParticleUpgrader(string oldShaderName)
  367. {
  368. if (oldShaderName == null)
  369. throw new ArgumentNullException("oldShaderName");
  370. RenameFloat("_Mode", "_Surface");
  371. if (oldShaderName.Contains("Unlit"))
  372. {
  373. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.ParticlesUnlit), UpdateUnlit);
  374. }
  375. else
  376. {
  377. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.ParticlesLit),
  378. UpdateStandardSurface);
  379. RenameFloat("_Glossiness", "_Smoothness");
  380. }
  381. RenameTexture("_MainTex", "_BaseMap");
  382. RenameColor("_Color", "_BaseColor");
  383. RenameFloat("_FlipbookMode", "_FlipbookBlending");
  384. }
  385. public static void UpdateStandardSurface(Material material)
  386. {
  387. UpdateSurfaceBlendModes(material);
  388. }
  389. public static void UpdateUnlit(Material material)
  390. {
  391. UpdateSurfaceBlendModes(material);
  392. }
  393. public static void UpdateSurfaceBlendModes(Material material)
  394. {
  395. switch (material.GetFloat("_Mode"))
  396. {
  397. case 0: // opaque
  398. material.SetFloat("_Surface", (int)UpgradeSurfaceType.Opaque);
  399. break;
  400. case 1: // cutout > alphatest
  401. material.SetFloat("_Surface", (int)UpgradeSurfaceType.Opaque);
  402. material.SetFloat("_AlphaClip", 1);
  403. break;
  404. case 2: // fade > alpha
  405. material.SetFloat("_Surface", (int)UpgradeSurfaceType.Transparent);
  406. material.SetFloat("_Blend", (int)UpgradeBlendMode.Alpha);
  407. break;
  408. case 3: // transparent > premul
  409. material.SetFloat("_Surface", (int)UpgradeSurfaceType.Transparent);
  410. material.SetFloat("_Blend", (int)UpgradeBlendMode.Premultiply);
  411. break;
  412. case 4: // add
  413. material.SetFloat("_Surface", (int)UpgradeSurfaceType.Transparent);
  414. material.SetFloat("_Blend", (int)UpgradeBlendMode.Additive);
  415. break;
  416. case 5: // sub > none
  417. break;
  418. case 6: // mod > multiply
  419. material.SetFloat("_Surface", (int)UpgradeSurfaceType.Transparent);
  420. material.SetFloat("_Blend", (int)UpgradeBlendMode.Multiply);
  421. break;
  422. }
  423. }
  424. }
  425. [MovedFrom("UnityEditor.Rendering.LWRP")] public class AutodeskInteractiveUpgrader : MaterialUpgrader
  426. {
  427. public AutodeskInteractiveUpgrader(string oldShaderName)
  428. {
  429. RenameShader(oldShaderName, "Universal Render Pipeline/Autodesk Interactive/Autodesk Interactive");
  430. }
  431. public override void Convert(Material srcMaterial, Material dstMaterial)
  432. {
  433. base.Convert(srcMaterial, dstMaterial);
  434. dstMaterial.SetFloat("_UseColorMap", srcMaterial.GetTexture("_MainTex") ? 1.0f : .0f);
  435. dstMaterial.SetFloat("_UseMetallicMap", srcMaterial.GetTexture("_MetallicGlossMap") ? 1.0f : .0f);
  436. dstMaterial.SetFloat("_UseNormalMap", srcMaterial.GetTexture("_BumpMap") ? 1.0f : .0f);
  437. dstMaterial.SetFloat("_UseRoughnessMap", srcMaterial.GetTexture("_SpecGlossMap") ? 1.0f : .0f);
  438. dstMaterial.SetFloat("_UseEmissiveMap", srcMaterial.GetTexture("_EmissionMap") ? 1.0f : .0f);
  439. dstMaterial.SetFloat("_UseAoMap", srcMaterial.GetTexture("_OcclusionMap") ? 1.0f : .0f);
  440. dstMaterial.SetVector("_UvOffset", srcMaterial.GetTextureOffset("_MainTex"));
  441. dstMaterial.SetVector("_UvTiling", srcMaterial.GetTextureScale("_MainTex"));
  442. }
  443. }
  444. }