UnlitShader.cs 2.8 KB

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