Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UniversalRenderPipelineMaterialUpgrader.cs 37KB

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