BakedLitShader.cs 3.6 KB

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