No Description
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.

ShaderUtils.cs 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using ShaderPathID = UnityEngine.Rendering.Universal.ShaderPathID;
  5. using UnityEditor.ShaderGraph;
  6. using UnityEditor.Rendering.Universal.ShaderGraph;
  7. using UnityEditor.Rendering.Universal.ShaderGUI;
  8. namespace Unity.Rendering.Universal
  9. {
  10. /// <summary>
  11. /// Various utility functions for shaders in URP.
  12. /// </summary>
  13. public static class ShaderUtils
  14. {
  15. internal enum ShaderID
  16. {
  17. Unknown = -1,
  18. Lit = ShaderPathID.Lit,
  19. SimpleLit = ShaderPathID.SimpleLit,
  20. Unlit = ShaderPathID.Unlit,
  21. TerrainLit = ShaderPathID.TerrainLit,
  22. ParticlesLit = ShaderPathID.ParticlesLit,
  23. ParticlesSimpleLit = ShaderPathID.ParticlesSimpleLit,
  24. ParticlesUnlit = ShaderPathID.ParticlesUnlit,
  25. BakedLit = ShaderPathID.BakedLit,
  26. SpeedTree7 = ShaderPathID.SpeedTree7,
  27. SpeedTree7Billboard = ShaderPathID.SpeedTree7Billboard,
  28. SpeedTree8 = ShaderPathID.SpeedTree8,
  29. SpeedTree9 = ShaderPathID.SpeedTree9,
  30. ComplexLit = ShaderPathID.ComplexLit,
  31. // ShaderGraph IDs start at 1000, correspond to subtargets
  32. SG_Unlit = 1000, // UniversalUnlitSubTarget
  33. SG_Lit, // UniversalLitSubTarget
  34. SG_Decal, // UniversalDecalSubTarget
  35. SG_SpriteUnlit, // UniversalSpriteUnlitSubTarget
  36. SG_SpriteLit, // UniversalSpriteLitSubTarget
  37. SG_SpriteCustomLit, // UniversalSpriteCustomLitSubTarget
  38. SG_SixWaySmokeLit // UniversalSixWaySubTarget
  39. }
  40. internal static bool IsShaderGraph(this ShaderID id)
  41. {
  42. return ((int)id >= 1000);
  43. }
  44. // NOTE: this won't work for non-Asset shaders... (i.e. shadergraph preview shaders)
  45. internal static ShaderID GetShaderID(Shader shader)
  46. {
  47. if (shader.IsShaderGraphAsset())
  48. {
  49. UniversalMetadata meta;
  50. if (!shader.TryGetMetadataOfType<UniversalMetadata>(out meta))
  51. return ShaderID.Unknown;
  52. return meta.shaderID;
  53. }
  54. else
  55. {
  56. ShaderPathID pathID = UnityEngine.Rendering.Universal.ShaderUtils.GetEnumFromPath(shader.name);
  57. return (ShaderID)pathID;
  58. }
  59. }
  60. internal static bool HasMotionVectorLightModeTag(ShaderID id)
  61. {
  62. // Currently only these ShaderIDs have a pass with a { "LightMode" = "MotionVectors" } tag in URP
  63. // (this is a more efficient check than looping over all sub-shaders and their passes and checking the
  64. // "LightMode" tag value with FindPassTagValue)
  65. switch(id)
  66. {
  67. case ShaderID.Lit:
  68. case ShaderID.Unlit:
  69. case ShaderID.SimpleLit:
  70. case ShaderID.ComplexLit:
  71. case ShaderID.BakedLit:
  72. case ShaderID.SG_Unlit:
  73. case ShaderID.SG_Lit:
  74. return true;
  75. }
  76. return false;
  77. }
  78. internal enum MaterialUpdateType
  79. {
  80. CreatedNewMaterial,
  81. ChangedAssignedShader,
  82. ModifiedShader,
  83. ModifiedMaterial
  84. }
  85. //Helper used by VFX, allow retrieval of ShaderID on another object than material.shader
  86. //In case of ShaderGraph integration, the material.shader is actually pointing to VisualEffectAsset
  87. internal static void UpdateMaterial(Material material, MaterialUpdateType updateType, UnityEngine.Object assetWithURPMetaData)
  88. {
  89. var currentShaderId = ShaderUtils.ShaderID.Unknown;
  90. if (assetWithURPMetaData != null)
  91. {
  92. var path = AssetDatabase.GetAssetPath(assetWithURPMetaData);
  93. foreach (var asset in AssetDatabase.LoadAllAssetsAtPath(path))
  94. {
  95. if (asset is UniversalMetadata metadataAsset)
  96. {
  97. currentShaderId = metadataAsset.shaderID;
  98. break;
  99. }
  100. }
  101. }
  102. UpdateMaterial(material, updateType, currentShaderId);
  103. }
  104. // this is used to update a material's keywords, applying any shader-associated logic to update dependent properties and keywords
  105. // this is also invoked when a material is created, modified, or the material's shader is modified or reassigned
  106. internal static void UpdateMaterial(Material material, MaterialUpdateType updateType, ShaderID shaderID = ShaderID.Unknown)
  107. {
  108. // if unknown, look it up from the material's shader
  109. // NOTE: this will only work for asset-based shaders..
  110. if (shaderID == ShaderID.Unknown)
  111. shaderID = GetShaderID(material.shader);
  112. switch (shaderID)
  113. {
  114. case ShaderID.Lit:
  115. LitShader.SetMaterialKeywords(material, LitGUI.SetMaterialKeywords);
  116. break;
  117. case ShaderID.SimpleLit:
  118. SimpleLitShader.SetMaterialKeywords(material, SimpleLitGUI.SetMaterialKeywords);
  119. break;
  120. case ShaderID.Unlit:
  121. UnlitShader.SetMaterialKeywords(material);
  122. break;
  123. case ShaderID.ParticlesLit:
  124. ParticlesLitShader.SetMaterialKeywords(material, LitGUI.SetMaterialKeywords, ParticleGUI.SetMaterialKeywords);
  125. break;
  126. case ShaderID.ParticlesSimpleLit:
  127. ParticlesSimpleLitShader.SetMaterialKeywords(material, SimpleLitGUI.SetMaterialKeywords, ParticleGUI.SetMaterialKeywords);
  128. break;
  129. case ShaderID.ParticlesUnlit:
  130. ParticlesUnlitShader.SetMaterialKeywords(material, null, ParticleGUI.SetMaterialKeywords);
  131. break;
  132. case ShaderID.SpeedTree8:
  133. ShaderGraphLitGUI.UpdateMaterial(material, updateType);
  134. break;
  135. case ShaderID.SpeedTree9:
  136. ShaderGraphLitGUI.UpdateMaterial(material, updateType);
  137. break;
  138. case ShaderID.SG_Lit:
  139. ShaderGraphLitGUI.UpdateMaterial(material, updateType);
  140. break;
  141. case ShaderID.SG_Unlit:
  142. ShaderGraphUnlitGUI.UpdateMaterial(material, updateType);
  143. break;
  144. case ShaderID.SG_Decal:
  145. break;
  146. case ShaderID.SG_SpriteUnlit:
  147. break;
  148. case ShaderID.SG_SpriteLit:
  149. break;
  150. case ShaderID.SG_SpriteCustomLit:
  151. break;
  152. case ShaderID.SG_SixWaySmokeLit:
  153. ShaderGraphLitGUI.UpdateMaterial(material, updateType);
  154. SixWayGUI.UpdateSixWayKeywords(material);
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160. }
  161. }