UniversalRenderPipelineMaterialUpgrader.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. upgraders.Add(new SpeedTreeUpgrader("Nature/SpeedTree"));
  130. upgraders.Add(new SpeedTreeBillboardUpgrader("Nature/SpeedTree Billboard"));
  131. upgraders.Add(new SpeedTree8Upgrader("Nature/SpeedTree8"));
  132. ////////////////////////////////////
  133. // Particle Upgraders //
  134. ////////////////////////////////////
  135. upgraders.Add(new ParticleUpgrader("Particles/Standard Surface"));
  136. upgraders.Add(new ParticleUpgrader("Particles/Standard Unlit"));
  137. upgraders.Add(new ParticleUpgrader("Particles/VertexLit Blended"));
  138. ////////////////////////////////////
  139. // Autodesk Interactive //
  140. ////////////////////////////////////
  141. upgraders.Add(new AutodeskInteractiveUpgrader("Autodesk Interactive"));
  142. }
  143. }
  144. [MovedFrom("UnityEditor.Rendering.LWRP")] public static class SupportedUpgradeParams
  145. {
  146. static public UpgradeParams diffuseOpaque = new UpgradeParams()
  147. {
  148. surfaceType = UpgradeSurfaceType.Opaque,
  149. blendMode = UpgradeBlendMode.Alpha,
  150. alphaClip = false,
  151. specularSource = SpecularSource.NoSpecular,
  152. smoothnessSource = SmoothnessSource.BaseAlpha,
  153. };
  154. static public UpgradeParams specularOpaque = new UpgradeParams()
  155. {
  156. surfaceType = UpgradeSurfaceType.Opaque,
  157. blendMode = UpgradeBlendMode.Alpha,
  158. alphaClip = false,
  159. specularSource = SpecularSource.SpecularTextureAndColor,
  160. smoothnessSource = SmoothnessSource.BaseAlpha,
  161. };
  162. static public UpgradeParams diffuseAlpha = new UpgradeParams()
  163. {
  164. surfaceType = UpgradeSurfaceType.Transparent,
  165. blendMode = UpgradeBlendMode.Alpha,
  166. alphaClip = false,
  167. specularSource = SpecularSource.NoSpecular,
  168. smoothnessSource = SmoothnessSource.SpecularAlpha,
  169. };
  170. static public UpgradeParams specularAlpha = new UpgradeParams()
  171. {
  172. surfaceType = UpgradeSurfaceType.Transparent,
  173. blendMode = UpgradeBlendMode.Alpha,
  174. alphaClip = false,
  175. specularSource = SpecularSource.SpecularTextureAndColor,
  176. smoothnessSource = SmoothnessSource.SpecularAlpha,
  177. };
  178. static public UpgradeParams diffuseAlphaCutout = new UpgradeParams()
  179. {
  180. surfaceType = UpgradeSurfaceType.Opaque,
  181. blendMode = UpgradeBlendMode.Alpha,
  182. alphaClip = true,
  183. specularSource = SpecularSource.NoSpecular,
  184. smoothnessSource = SmoothnessSource.SpecularAlpha,
  185. };
  186. static public UpgradeParams specularAlphaCutout = new UpgradeParams()
  187. {
  188. surfaceType = UpgradeSurfaceType.Opaque,
  189. blendMode = UpgradeBlendMode.Alpha,
  190. alphaClip = true,
  191. specularSource = SpecularSource.SpecularTextureAndColor,
  192. smoothnessSource = SmoothnessSource.SpecularAlpha,
  193. };
  194. static public UpgradeParams diffuseCubemap = new UpgradeParams()
  195. {
  196. surfaceType = UpgradeSurfaceType.Opaque,
  197. blendMode = UpgradeBlendMode.Alpha,
  198. alphaClip = false,
  199. specularSource = SpecularSource.NoSpecular,
  200. smoothnessSource = SmoothnessSource.BaseAlpha,
  201. };
  202. static public UpgradeParams specularCubemap = new UpgradeParams()
  203. {
  204. surfaceType = UpgradeSurfaceType.Opaque,
  205. blendMode = UpgradeBlendMode.Alpha,
  206. alphaClip = false,
  207. specularSource = SpecularSource.SpecularTextureAndColor,
  208. smoothnessSource = SmoothnessSource.BaseAlpha,
  209. };
  210. static public UpgradeParams diffuseCubemapAlpha = new UpgradeParams()
  211. {
  212. surfaceType = UpgradeSurfaceType.Transparent,
  213. blendMode = UpgradeBlendMode.Alpha,
  214. alphaClip = false,
  215. specularSource = SpecularSource.NoSpecular,
  216. smoothnessSource = SmoothnessSource.BaseAlpha,
  217. };
  218. static public UpgradeParams specularCubemapAlpha = new UpgradeParams()
  219. {
  220. surfaceType = UpgradeSurfaceType.Transparent,
  221. blendMode = UpgradeBlendMode.Alpha,
  222. alphaClip = false,
  223. specularSource = SpecularSource.SpecularTextureAndColor,
  224. smoothnessSource = SmoothnessSource.BaseAlpha,
  225. };
  226. }
  227. [MovedFrom("UnityEditor.Rendering.LWRP")] public class StandardUpgrader : MaterialUpgrader
  228. {
  229. enum LegacyRenderingMode
  230. {
  231. Opaque,
  232. Cutout,
  233. Fade, // Old school alpha-blending mode, fresnel does not affect amount of transparency
  234. Transparent // Physically plausible transparency mode, implemented as alpha pre-multiply
  235. }
  236. public static void UpdateStandardMaterialKeywords(Material material)
  237. {
  238. if (material == null)
  239. throw new ArgumentNullException("material");
  240. if(material.GetTexture("_MetallicGlossMap"))
  241. material.SetFloat("_Smoothness", material.GetFloat("_GlossMapScale"));
  242. else
  243. material.SetFloat("_Smoothness", material.GetFloat("_Glossiness"));
  244. if (material.IsKeywordEnabled("_ALPHATEST_ON"))
  245. {
  246. material.SetFloat("_AlphaClip", 1.0f);
  247. }
  248. material.SetFloat("_WorkflowMode", 1.0f);
  249. CoreUtils.SetKeyword(material, "_OCCLUSIONMAP", material.GetTexture("_OcclusionMap"));
  250. CoreUtils.SetKeyword(material, "_METALLICSPECGLOSSMAP", material.GetTexture("_MetallicGlossMap"));
  251. UpdateSurfaceTypeAndBlendMode(material);
  252. BaseShaderGUI.SetupMaterialBlendMode(material);
  253. }
  254. public static void UpdateStandardSpecularMaterialKeywords(Material material)
  255. {
  256. if (material == null)
  257. throw new ArgumentNullException("material");
  258. if(material.GetTexture("_SpecGlossMap"))
  259. material.SetFloat("_Smoothness", material.GetFloat("_GlossMapScale"));
  260. else
  261. material.SetFloat("_Smoothness", material.GetFloat("_Glossiness"));
  262. material.SetFloat("_WorkflowMode", 0.0f);
  263. CoreUtils.SetKeyword(material, "_OCCLUSIONMAP", material.GetTexture("_OcclusionMap"));
  264. CoreUtils.SetKeyword(material, "_METALLICSPECGLOSSMAP", material.GetTexture("_SpecGlossMap"));
  265. CoreUtils.SetKeyword(material, "_SPECULAR_SETUP", true);
  266. UpdateSurfaceTypeAndBlendMode(material);
  267. BaseShaderGUI.SetupMaterialBlendMode(material);
  268. }
  269. // Converts from legacy RenderingMode to new SurfaceType and BlendMode
  270. static void UpdateSurfaceTypeAndBlendMode(Material material)
  271. {
  272. // Property _Mode is already renamed to _Surface at this point
  273. var legacyRenderingMode = (LegacyRenderingMode)material.GetFloat("_Surface");
  274. if (legacyRenderingMode == LegacyRenderingMode.Transparent)
  275. {
  276. material.SetInt("_Surface", (int)BaseShaderGUI.SurfaceType.Transparent);
  277. material.SetInt("_Blend", (int)BaseShaderGUI.BlendMode.Premultiply);
  278. }
  279. else if (legacyRenderingMode == LegacyRenderingMode.Fade)
  280. {
  281. material.SetInt("_Surface", (int)BaseShaderGUI.SurfaceType.Transparent);
  282. material.SetInt("_Blend", (int)BaseShaderGUI.BlendMode.Alpha);
  283. }
  284. else
  285. {
  286. material.SetInt("_Surface", (int)BaseShaderGUI.SurfaceType.Opaque);
  287. }
  288. }
  289. public StandardUpgrader(string oldShaderName)
  290. {
  291. if (oldShaderName == null)
  292. throw new ArgumentNullException("oldShaderName");
  293. string standardShaderPath = ShaderUtils.GetShaderPath(ShaderPathID.Lit);
  294. if (oldShaderName.Contains("Specular"))
  295. {
  296. RenameShader(oldShaderName, standardShaderPath, UpdateStandardSpecularMaterialKeywords);
  297. }
  298. else
  299. {
  300. RenameShader(oldShaderName, standardShaderPath, UpdateStandardMaterialKeywords);
  301. }
  302. RenameFloat("_Mode", "_Surface");
  303. RenameTexture("_MainTex", "_BaseMap");
  304. RenameColor("_Color", "_BaseColor");
  305. RenameFloat("_GlossyReflections", "_EnvironmentReflections");
  306. }
  307. }
  308. internal class StandardSimpleLightingUpgrader : MaterialUpgrader
  309. {
  310. public StandardSimpleLightingUpgrader(string oldShaderName, UpgradeParams upgradeParams)
  311. {
  312. if (oldShaderName == null)
  313. throw new ArgumentNullException("oldShaderName");
  314. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.SimpleLit), UpdateMaterialKeywords);
  315. SetFloat("_Surface", (float)upgradeParams.surfaceType);
  316. SetFloat("_Blend", (float)upgradeParams.blendMode);
  317. SetFloat("_AlphaClip", upgradeParams.alphaClip ? 1 : 0);
  318. SetFloat("_SpecularHighlights", (float)upgradeParams.specularSource);
  319. SetFloat("_SmoothnessSource", (float)upgradeParams.smoothnessSource);
  320. RenameTexture("_MainTex", "_BaseMap");
  321. RenameColor("_Color", "_BaseColor");
  322. RenameFloat("_Shininess", "_Smoothness");
  323. if (oldShaderName.Contains("Legacy Shaders/Self-Illumin"))
  324. {
  325. RenameTexture("_Illum", "_EmissionMap");
  326. RemoveTexture("_Illum");
  327. SetColor("_EmissionColor", Color.white);
  328. }
  329. }
  330. public static void UpdateMaterialKeywords(Material material)
  331. {
  332. if (material == null)
  333. throw new ArgumentNullException("material");
  334. material.shaderKeywords = null;
  335. BaseShaderGUI.SetupMaterialBlendMode(material);
  336. UpdateMaterialSpecularSource(material);
  337. CoreUtils.SetKeyword(material, "_NORMALMAP", material.GetTexture("_BumpMap"));
  338. // A material's GI flag internally keeps track of whether emission is enabled at all, it's enabled but has no effect
  339. // or is enabled and may be modified at runtime. This state depends on the values of the current flag and emissive color.
  340. // The fixup routine makes sure that the material is in the correct state if/when changes are made to the mode or color.
  341. MaterialEditor.FixupEmissiveFlag(material);
  342. bool shouldEmissionBeEnabled = (material.globalIlluminationFlags & MaterialGlobalIlluminationFlags.EmissiveIsBlack) == 0;
  343. CoreUtils.SetKeyword(material, "_EMISSION", shouldEmissionBeEnabled);
  344. }
  345. private static void UpdateMaterialSpecularSource(Material material)
  346. {
  347. SpecularSource specSource = (SpecularSource)material.GetFloat("_SpecSource");
  348. if (specSource == SpecularSource.NoSpecular)
  349. {
  350. CoreUtils.SetKeyword(material, "_SPECGLOSSMAP", false);
  351. CoreUtils.SetKeyword(material, "_SPECULAR_COLOR", false);
  352. CoreUtils.SetKeyword(material, "_GLOSSINESS_FROM_BASE_ALPHA", false);
  353. }
  354. else
  355. {
  356. SmoothnessSource glossSource = (SmoothnessSource)material.GetFloat("_SmoothnessSource");
  357. bool hasGlossMap = material.GetTexture("_SpecGlossMap");
  358. CoreUtils.SetKeyword(material, "_SPECGLOSSMAP", hasGlossMap);
  359. CoreUtils.SetKeyword(material, "_SPECULAR_COLOR", !hasGlossMap);
  360. CoreUtils.SetKeyword(material, "_GLOSSINESS_FROM_BASE_ALPHA", glossSource == SmoothnessSource.BaseAlpha);
  361. }
  362. }
  363. }
  364. [MovedFrom("UnityEditor.Rendering.LWRP")] public class TerrainUpgrader : MaterialUpgrader
  365. {
  366. public TerrainUpgrader(string oldShaderName)
  367. {
  368. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.TerrainLit));
  369. }
  370. }
  371. internal class SpeedTreeUpgrader : MaterialUpgrader
  372. {
  373. internal SpeedTreeUpgrader(string oldShaderName)
  374. {
  375. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.SpeedTree7));
  376. }
  377. }
  378. internal class SpeedTreeBillboardUpgrader : MaterialUpgrader
  379. {
  380. internal SpeedTreeBillboardUpgrader(string oldShaderName)
  381. {
  382. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.SpeedTree7Billboard));
  383. }
  384. }
  385. internal class SpeedTree8Upgrader : MaterialUpgrader
  386. {
  387. internal SpeedTree8Upgrader(string oldShaderName)
  388. {
  389. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.SpeedTree8));
  390. }
  391. }
  392. [MovedFrom("UnityEditor.Rendering.LWRP")] public class ParticleUpgrader : MaterialUpgrader
  393. {
  394. public ParticleUpgrader(string oldShaderName)
  395. {
  396. if (oldShaderName == null)
  397. throw new ArgumentNullException("oldShaderName");
  398. RenameFloat("_Mode", "_Surface");
  399. if (oldShaderName.Contains("Unlit"))
  400. {
  401. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.ParticlesUnlit), UpdateUnlit);
  402. }
  403. else
  404. {
  405. RenameShader(oldShaderName, ShaderUtils.GetShaderPath(ShaderPathID.ParticlesLit),
  406. UpdateStandardSurface);
  407. RenameFloat("_Glossiness", "_Smoothness");
  408. }
  409. RenameTexture("_MainTex", "_BaseMap");
  410. RenameColor("_Color", "_BaseColor");
  411. RenameFloat("_FlipbookMode", "_FlipbookBlending");
  412. }
  413. public static void UpdateStandardSurface(Material material)
  414. {
  415. UpdateSurfaceBlendModes(material);
  416. }
  417. public static void UpdateUnlit(Material material)
  418. {
  419. UpdateSurfaceBlendModes(material);
  420. }
  421. public static void UpdateSurfaceBlendModes(Material material)
  422. {
  423. switch (material.GetFloat("_Mode"))
  424. {
  425. case 0: // opaque
  426. material.SetFloat("_Surface", (int)UpgradeSurfaceType.Opaque);
  427. break;
  428. case 1: // cutout > alphatest
  429. material.SetFloat("_Surface", (int)UpgradeSurfaceType.Opaque);
  430. material.SetFloat("_AlphaClip", 1);
  431. break;
  432. case 2: // fade > alpha
  433. material.SetFloat("_Surface", (int)UpgradeSurfaceType.Transparent);
  434. material.SetFloat("_Blend", (int)UpgradeBlendMode.Alpha);
  435. break;
  436. case 3: // transparent > premul
  437. material.SetFloat("_Surface", (int)UpgradeSurfaceType.Transparent);
  438. material.SetFloat("_Blend", (int)UpgradeBlendMode.Premultiply);
  439. break;
  440. case 4: // add
  441. material.SetFloat("_Surface", (int)UpgradeSurfaceType.Transparent);
  442. material.SetFloat("_Blend", (int)UpgradeBlendMode.Additive);
  443. break;
  444. case 5: // sub > none
  445. break;
  446. case 6: // mod > multiply
  447. material.SetFloat("_Surface", (int)UpgradeSurfaceType.Transparent);
  448. material.SetFloat("_Blend", (int)UpgradeBlendMode.Multiply);
  449. break;
  450. }
  451. }
  452. }
  453. [MovedFrom("UnityEditor.Rendering.LWRP")] public class AutodeskInteractiveUpgrader : MaterialUpgrader
  454. {
  455. public AutodeskInteractiveUpgrader(string oldShaderName)
  456. {
  457. RenameShader(oldShaderName, "Universal Render Pipeline/Autodesk Interactive/Autodesk Interactive");
  458. }
  459. public override void Convert(Material srcMaterial, Material dstMaterial)
  460. {
  461. base.Convert(srcMaterial, dstMaterial);
  462. dstMaterial.SetFloat("_UseColorMap", srcMaterial.GetTexture("_MainTex") ? 1.0f : .0f);
  463. dstMaterial.SetFloat("_UseMetallicMap", srcMaterial.GetTexture("_MetallicGlossMap") ? 1.0f : .0f);
  464. dstMaterial.SetFloat("_UseNormalMap", srcMaterial.GetTexture("_BumpMap") ? 1.0f : .0f);
  465. dstMaterial.SetFloat("_UseRoughnessMap", srcMaterial.GetTexture("_SpecGlossMap") ? 1.0f : .0f);
  466. dstMaterial.SetFloat("_UseEmissiveMap", srcMaterial.GetTexture("_EmissionMap") ? 1.0f : .0f);
  467. dstMaterial.SetFloat("_UseAoMap", srcMaterial.GetTexture("_OcclusionMap") ? 1.0f : .0f);
  468. dstMaterial.SetVector("_UvOffset", srcMaterial.GetTextureOffset("_MainTex"));
  469. dstMaterial.SetVector("_UvTiling", srcMaterial.GetTextureScale("_MainTex"));
  470. }
  471. }
  472. }