A game about forced loneliness, made by TACStudios
at master 3.9 kB view raw
1using System; 2using System.Collections.Generic; 3using UnityEditor.Graphing; 4using UnityEditor.ShaderGraph.Drawing.Slots; 5using UnityEditor.ShaderGraph.Internal; 6using UnityEngine; 7using UnityEngine.UIElements; 8 9namespace UnityEditor.ShaderGraph 10{ 11 [Serializable] 12 class Vector1MaterialSlot : MaterialSlot, IMaterialSlotHasValue<float> 13 { 14 [SerializeField] 15 float m_Value; 16 17 [SerializeField] 18 float m_DefaultValue; 19 20 [SerializeField] 21 string[] m_Labels; // this can be null, which means fallback to k_LabelDefaults 22 23 static readonly string[] k_LabelDefaults = { "X" }; 24 string[] labels 25 { 26 get 27 { 28 if ((m_Labels == null) || (m_Labels.Length != k_LabelDefaults.Length)) 29 return k_LabelDefaults; 30 return m_Labels; 31 } 32 } 33 34 public Vector1MaterialSlot() 35 { 36 } 37 38 public Vector1MaterialSlot( 39 int slotId, 40 string displayName, 41 string shaderOutputName, 42 SlotType slotType, 43 float value, 44 ShaderStageCapability stageCapability = ShaderStageCapability.All, 45 string label1 = null, 46 bool hidden = false) 47 : base(slotId, displayName, shaderOutputName, slotType, stageCapability, hidden) 48 { 49 m_DefaultValue = value; 50 m_Value = value; 51 if (label1 != null) 52 m_Labels = new[] { label1 }; 53 } 54 55 public float defaultValue { get { return m_DefaultValue; } } 56 57 public float value 58 { 59 get { return m_Value; } 60 set { m_Value = value; } 61 } 62 63 public override bool isDefaultValue => value.Equals(defaultValue); 64 65 public override VisualElement InstantiateControl() 66 { 67 return new MultiFloatSlotControlView(owner, labels, () => new Vector4(value, 0f, 0f, 0f), (newValue) => value = newValue.x); 68 } 69 70 protected override string ConcreteSlotValueAsVariable() 71 { 72 return string.Format("$precision({0})", NodeUtils.FloatToShaderValue(value)); 73 } 74 75 public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode) 76 { 77 if (!generationMode.IsPreview()) 78 return; 79 80 var matOwner = owner as AbstractMaterialNode; 81 if (matOwner == null) 82 throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode))); 83 84 var property = new Vector1ShaderProperty() 85 { 86 overrideReferenceName = matOwner.GetVariableNameForSlot(id), 87 generatePropertyBlock = false, 88 value = value 89 }; 90 properties.AddShaderProperty(property); 91 } 92 93 public override SlotValueType valueType { get { return SlotValueType.Vector1; } } 94 public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector1; } } 95 96 public override void GetPreviewProperties(List<PreviewProperty> properties, string name) 97 { 98 var pp = new PreviewProperty(PropertyType.Float) 99 { 100 name = name, 101 floatValue = value, 102 }; 103 properties.Add(pp); 104 } 105 106 public override void CopyValuesFrom(MaterialSlot foundSlot) 107 { 108 var slot = foundSlot as Vector1MaterialSlot; 109 if (slot != null) 110 value = slot.value; 111 } 112 113 public override void CopyDefaultValue(MaterialSlot other) 114 { 115 base.CopyDefaultValue(other); 116 if (other is IMaterialSlotHasValue<float> ms) 117 { 118 m_DefaultValue = ms.defaultValue; 119 } 120 } 121 } 122}