ShaderUtils.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. public static class ShaderUtils
  11. {
  12. internal enum ShaderID
  13. {
  14. Unknown = -1,
  15. Lit = ShaderPathID.Lit,
  16. SimpleLit = ShaderPathID.SimpleLit,
  17. Unlit = ShaderPathID.Unlit,
  18. TerrainLit = ShaderPathID.TerrainLit,
  19. ParticlesLit = ShaderPathID.ParticlesLit,
  20. ParticlesSimpleLit = ShaderPathID.ParticlesSimpleLit,
  21. ParticlesUnlit = ShaderPathID.ParticlesUnlit,
  22. BakedLit = ShaderPathID.BakedLit,
  23. SpeedTree7 = ShaderPathID.SpeedTree7,
  24. SpeedTree7Billboard = ShaderPathID.SpeedTree7Billboard,
  25. SpeedTree8 = ShaderPathID.SpeedTree8,
  26. // ShaderGraph IDs start at 1000, correspond to subtargets
  27. SG_Unlit = 1000, // UniversalUnlitSubTarget
  28. SG_Lit, // UniversalLitSubTarget
  29. }
  30. internal static bool IsShaderGraph(this ShaderID id)
  31. {
  32. return ((int)id >= 1000);
  33. }
  34. // NOTE: this won't work for non-Asset shaders... (i.e. shadergraph preview shaders)
  35. internal static ShaderID GetShaderID(Shader shader)
  36. {
  37. if (shader.IsShaderGraphAsset())
  38. {
  39. UniversalMetadata meta;
  40. if (!shader.TryGetMetadataOfType<UniversalMetadata>(out meta))
  41. return ShaderID.Unknown;
  42. return meta.shaderID;
  43. }
  44. else
  45. {
  46. ShaderPathID pathID = UnityEngine.Rendering.Universal.ShaderUtils.GetEnumFromPath(shader.name);
  47. return (ShaderID)pathID;
  48. }
  49. }
  50. internal enum MaterialUpdateType
  51. {
  52. CreatedNewMaterial,
  53. ChangedAssignedShader,
  54. ModifiedShader,
  55. ModifiedMaterial
  56. }
  57. // this is used to update a material's keywords, applying any shader-associated logic to update dependent properties and keywords
  58. // this is also invoked when a material is created, modified, or the material's shader is modified or reassigned
  59. internal static void UpdateMaterial(Material material, MaterialUpdateType updateType, ShaderID shaderID = ShaderID.Unknown)
  60. {
  61. // if unknown, look it up from the material's shader
  62. // NOTE: this will only work for asset-based shaders..
  63. if (shaderID == ShaderID.Unknown)
  64. shaderID = GetShaderID(material.shader);
  65. switch (shaderID)
  66. {
  67. case ShaderID.Lit:
  68. LitShader.SetMaterialKeywords(material, LitGUI.SetMaterialKeywords);
  69. break;
  70. case ShaderID.SimpleLit:
  71. SimpleLitShader.SetMaterialKeywords(material, SimpleLitGUI.SetMaterialKeywords);
  72. break;
  73. case ShaderID.Unlit:
  74. UnlitShader.SetMaterialKeywords(material);
  75. break;
  76. case ShaderID.ParticlesLit:
  77. ParticlesLitShader.SetMaterialKeywords(material, LitGUI.SetMaterialKeywords, ParticleGUI.SetMaterialKeywords);
  78. break;
  79. case ShaderID.ParticlesSimpleLit:
  80. ParticlesSimpleLitShader.SetMaterialKeywords(material, SimpleLitGUI.SetMaterialKeywords, ParticleGUI.SetMaterialKeywords);
  81. break;
  82. case ShaderID.ParticlesUnlit:
  83. ParticlesUnlitShader.SetMaterialKeywords(material, null, ParticleGUI.SetMaterialKeywords);
  84. break;
  85. case ShaderID.SG_Lit:
  86. ShaderGraphLitGUI.UpdateMaterial(material, updateType);
  87. break;
  88. case ShaderID.SG_Unlit:
  89. ShaderGraphUnlitGUI.UpdateMaterial(material, updateType);
  90. break;
  91. default:
  92. break;
  93. }
  94. }
  95. }
  96. }