SimpleLitShader.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.Rendering.Universal.ShaderGUI
  4. {
  5. internal class SimpleLitShader : BaseShaderGUI
  6. {
  7. // Properties
  8. private SimpleLitGUI.SimpleLitProperties shadingModelProperties;
  9. // collect properties from the material properties
  10. public override void FindProperties(MaterialProperty[] properties)
  11. {
  12. base.FindProperties(properties);
  13. shadingModelProperties = new SimpleLitGUI.SimpleLitProperties(properties);
  14. }
  15. // material changed check
  16. public override void MaterialChanged(Material material)
  17. {
  18. if (material == null)
  19. throw new ArgumentNullException("material");
  20. SetMaterialKeywords(material, SimpleLitGUI.SetMaterialKeywords);
  21. }
  22. // material main surface options
  23. public override void DrawSurfaceOptions(Material material)
  24. {
  25. if (material == null)
  26. throw new ArgumentNullException("material");
  27. // Use default labelWidth
  28. EditorGUIUtility.labelWidth = 0f;
  29. // Detect any changes to the material
  30. EditorGUI.BeginChangeCheck();
  31. {
  32. base.DrawSurfaceOptions(material);
  33. }
  34. if (EditorGUI.EndChangeCheck())
  35. {
  36. foreach (var obj in blendModeProp.targets)
  37. MaterialChanged((Material)obj);
  38. }
  39. }
  40. // material main surface inputs
  41. public override void DrawSurfaceInputs(Material material)
  42. {
  43. base.DrawSurfaceInputs(material);
  44. SimpleLitGUI.Inputs(shadingModelProperties, materialEditor, material);
  45. DrawEmissionProperties(material, true);
  46. DrawTileOffset(materialEditor, baseMapProp);
  47. }
  48. public override void DrawAdvancedOptions(Material material)
  49. {
  50. SimpleLitGUI.Advanced(shadingModelProperties);
  51. base.DrawAdvancedOptions(material);
  52. }
  53. public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader)
  54. {
  55. if (material == null)
  56. throw new ArgumentNullException("material");
  57. // _Emission property is lost after assigning Standard shader to the material
  58. // thus transfer it before assigning the new shader
  59. if (material.HasProperty("_Emission"))
  60. {
  61. material.SetColor("_EmissionColor", material.GetColor("_Emission"));
  62. }
  63. base.AssignNewShaderToMaterial(material, oldShader, newShader);
  64. if (oldShader == null || !oldShader.name.Contains("Legacy Shaders/"))
  65. {
  66. SetupMaterialBlendMode(material);
  67. return;
  68. }
  69. SurfaceType surfaceType = SurfaceType.Opaque;
  70. BlendMode blendMode = BlendMode.Alpha;
  71. if (oldShader.name.Contains("/Transparent/Cutout/"))
  72. {
  73. surfaceType = SurfaceType.Opaque;
  74. material.SetFloat("_AlphaClip", 1);
  75. }
  76. else if (oldShader.name.Contains("/Transparent/"))
  77. {
  78. // NOTE: legacy shaders did not provide physically based transparency
  79. // therefore Fade mode
  80. surfaceType = SurfaceType.Transparent;
  81. blendMode = BlendMode.Alpha;
  82. }
  83. material.SetFloat("_Surface", (float)surfaceType);
  84. material.SetFloat("_Blend", (float)blendMode);
  85. MaterialChanged(material);
  86. }
  87. }
  88. }