GradientNode.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.ShaderGraph.Drawing.Controls;
  5. using UnityEngine;
  6. using UnityEditor.Graphing;
  7. using UnityEditor.ShaderGraph.Internal;
  8. namespace UnityEditor.ShaderGraph
  9. {
  10. [Title("Input", "Gradient", "Gradient")]
  11. class GradientNode : AbstractMaterialNode, IGeneratesBodyCode, IPropertyFromNode
  12. {
  13. [SerializeField]
  14. private float m_Value;
  15. public const int OutputSlotId = 0;
  16. private const string kOutputSlotName = "Out";
  17. public GradientNode()
  18. {
  19. name = "Gradient";
  20. UpdateNodeAfterDeserialization();
  21. }
  22. string GetFunctionName()
  23. {
  24. return string.Format("Unity_{0}", GetVariableNameForNode());
  25. }
  26. Gradient m_Gradient = new Gradient();
  27. [SerializeField]
  28. Vector4[] m_SerializableColorKeys = { new Vector4(1f, 1f, 1f, 0f), new Vector4(0f, 0f, 0f, 1f), };
  29. [SerializeField]
  30. Vector2[] m_SerializableAlphaKeys = { new Vector2(1f, 0f), new Vector2(1f, 1f) };
  31. [SerializeField]
  32. int m_SerializableMode = 0;
  33. [GradientControl("")]
  34. public Gradient gradient
  35. {
  36. get
  37. {
  38. if (m_SerializableAlphaKeys != null && m_SerializableColorKeys != null)
  39. {
  40. m_Gradient = new Gradient();
  41. var colorKeys = m_SerializableColorKeys.Select(k => new GradientColorKey(new Color(k.x, k.y, k.z, 1f), k.w)).ToArray();
  42. var alphaKeys = m_SerializableAlphaKeys.Select(k => new GradientAlphaKey(k.x, k.y)).ToArray();
  43. m_SerializableAlphaKeys = null;
  44. m_SerializableColorKeys = null;
  45. m_Gradient.SetKeys(colorKeys, alphaKeys);
  46. m_Gradient.mode = (GradientMode)m_SerializableMode;
  47. }
  48. return m_Gradient;
  49. }
  50. set
  51. {
  52. var scope = ModificationScope.Nothing;
  53. if (!GradientUtil.CheckEquivalency(gradient, value))
  54. scope = scope < ModificationScope.Graph ? ModificationScope.Graph : scope;
  55. if (scope > ModificationScope.Nothing)
  56. {
  57. var newColorKeys = value.colorKeys;
  58. var newAlphaKeys = value.alphaKeys;
  59. m_Gradient.SetKeys(newColorKeys, newAlphaKeys);
  60. m_Gradient.mode = value.mode;
  61. Dirty(ModificationScope.Node);
  62. }
  63. }
  64. }
  65. public override void OnBeforeSerialize()
  66. {
  67. base.OnBeforeSerialize();
  68. if (m_Gradient != null)
  69. {
  70. m_SerializableColorKeys = m_Gradient.colorKeys.Select(k => new Vector4(k.color.r, k.color.g, k.color.b, k.time)).ToArray();
  71. m_SerializableAlphaKeys = m_Gradient.alphaKeys.Select(k => new Vector2(k.alpha, k.time)).ToArray();
  72. m_SerializableMode = (int)m_Gradient.mode;
  73. }
  74. }
  75. public override bool hasPreview { get { return false; } }
  76. public sealed override void UpdateNodeAfterDeserialization()
  77. {
  78. AddSlot(new GradientMaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output));
  79. RemoveSlotsNameNotMatching(new[] { OutputSlotId });
  80. }
  81. public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
  82. {
  83. if (generationMode.IsPreview())
  84. {
  85. sb.AppendLine("Gradient {0} = {1};", GetVariableNameForSlot(outputSlotId), GradientUtil.GetGradientForPreview(GetVariableNameForNode()));
  86. }
  87. else
  88. {
  89. sb.AppendLine("Gradient {0} = {1}", GetVariableNameForSlot(outputSlotId), GradientUtil.GetGradientValue(gradient, ";"));
  90. }
  91. }
  92. public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
  93. {
  94. base.CollectPreviewMaterialProperties(properties);
  95. properties.Add(new PreviewProperty(PropertyType.Gradient)
  96. {
  97. name = GetVariableNameForNode(),
  98. gradientValue = gradient
  99. });
  100. }
  101. public override void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
  102. {
  103. if(!generationMode.IsPreview())
  104. return;
  105. base.CollectShaderProperties(properties, generationMode);
  106. GradientUtil.GetGradientPropertiesForPreview(properties, GetVariableNameForNode(), gradient);
  107. }
  108. public AbstractShaderProperty AsShaderProperty()
  109. {
  110. return new GradientShaderProperty { value = gradient };
  111. }
  112. public int outputSlotId { get { return OutputSlotId; } }
  113. }
  114. }