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