LitShader.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Rendering;
  4. using UnityEditor.Rendering.Universal;
  5. namespace UnityEditor.Rendering.Universal.ShaderGUI
  6. {
  7. internal class LitShader : BaseShaderGUI
  8. {
  9. // Properties
  10. private LitGUI.LitProperties litProperties;
  11. // collect properties from the material properties
  12. public override void FindProperties(MaterialProperty[] properties)
  13. {
  14. base.FindProperties(properties);
  15. litProperties = new LitGUI.LitProperties(properties);
  16. }
  17. // material changed check
  18. public override void MaterialChanged(Material material)
  19. {
  20. if (material == null)
  21. throw new ArgumentNullException("material");
  22. SetMaterialKeywords(material, LitGUI.SetMaterialKeywords);
  23. }
  24. // material main surface options
  25. public override void DrawSurfaceOptions(Material material)
  26. {
  27. if (material == null)
  28. throw new ArgumentNullException("material");
  29. // Use default labelWidth
  30. EditorGUIUtility.labelWidth = 0f;
  31. // Detect any changes to the material
  32. EditorGUI.BeginChangeCheck();
  33. if (litProperties.workflowMode != null)
  34. {
  35. DoPopup(LitGUI.Styles.workflowModeText, litProperties.workflowMode, Enum.GetNames(typeof(LitGUI.WorkflowMode)));
  36. }
  37. if (EditorGUI.EndChangeCheck())
  38. {
  39. foreach (var obj in blendModeProp.targets)
  40. MaterialChanged((Material)obj);
  41. }
  42. base.DrawSurfaceOptions(material);
  43. }
  44. // material main surface inputs
  45. public override void DrawSurfaceInputs(Material material)
  46. {
  47. base.DrawSurfaceInputs(material);
  48. LitGUI.Inputs(litProperties, materialEditor, material);
  49. DrawEmissionProperties(material, true);
  50. DrawTileOffset(materialEditor, baseMapProp);
  51. }
  52. // material main advanced options
  53. public override void DrawAdvancedOptions(Material material)
  54. {
  55. if (litProperties.reflections != null && litProperties.highlights != null)
  56. {
  57. EditorGUI.BeginChangeCheck();
  58. materialEditor.ShaderProperty(litProperties.highlights, LitGUI.Styles.highlightsText);
  59. materialEditor.ShaderProperty(litProperties.reflections, LitGUI.Styles.reflectionsText);
  60. if(EditorGUI.EndChangeCheck())
  61. {
  62. MaterialChanged(material);
  63. }
  64. }
  65. base.DrawAdvancedOptions(material);
  66. }
  67. public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader)
  68. {
  69. if (material == null)
  70. throw new ArgumentNullException("material");
  71. // _Emission property is lost after assigning Standard shader to the material
  72. // thus transfer it before assigning the new shader
  73. if (material.HasProperty("_Emission"))
  74. {
  75. material.SetColor("_EmissionColor", material.GetColor("_Emission"));
  76. }
  77. base.AssignNewShaderToMaterial(material, oldShader, newShader);
  78. if (oldShader == null || !oldShader.name.Contains("Legacy Shaders/"))
  79. {
  80. SetupMaterialBlendMode(material);
  81. return;
  82. }
  83. SurfaceType surfaceType = SurfaceType.Opaque;
  84. BlendMode blendMode = BlendMode.Alpha;
  85. if (oldShader.name.Contains("/Transparent/Cutout/"))
  86. {
  87. surfaceType = SurfaceType.Opaque;
  88. material.SetFloat("_AlphaClip", 1);
  89. }
  90. else if (oldShader.name.Contains("/Transparent/"))
  91. {
  92. // NOTE: legacy shaders did not provide physically based transparency
  93. // therefore Fade mode
  94. surfaceType = SurfaceType.Transparent;
  95. blendMode = BlendMode.Alpha;
  96. }
  97. material.SetFloat("_Surface", (float)surfaceType);
  98. material.SetFloat("_Blend", (float)blendMode);
  99. if (oldShader.name.Equals("Standard (Specular setup)"))
  100. {
  101. material.SetFloat("_WorkflowMode", (float)LitGUI.WorkflowMode.Specular);
  102. Texture texture = material.GetTexture("_SpecGlossMap");
  103. if (texture != null)
  104. material.SetTexture("_MetallicSpecGlossMap", texture);
  105. }
  106. else
  107. {
  108. material.SetFloat("_WorkflowMode", (float)LitGUI.WorkflowMode.Metallic);
  109. Texture texture = material.GetTexture("_MetallicGlossMap");
  110. if (texture != null)
  111. material.SetTexture("_MetallicSpecGlossMap", texture);
  112. }
  113. MaterialChanged(material);
  114. }
  115. }
  116. }