A game about forced loneliness, made by TACStudios
at master 4.4 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 Vector3MaterialSlot : MaterialSlot, IMaterialSlotHasValue<Vector3> 13 { 14 [SerializeField] 15 private Vector3 m_Value; 16 17 [SerializeField] 18 private Vector3 m_DefaultValue = Vector3.zero; 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", "Y", "Z" }; 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 Vector3MaterialSlot() 35 { 36 } 37 38 public Vector3MaterialSlot( 39 int slotId, 40 string displayName, 41 string shaderOutputName, 42 SlotType slotType, 43 Vector3 value, 44 ShaderStageCapability stageCapability = ShaderStageCapability.All, 45 string label1 = null, 46 string label2 = null, 47 string label3 = null, 48 bool hidden = false) 49 : base(slotId, displayName, shaderOutputName, slotType, stageCapability, hidden) 50 { 51 m_Value = value; 52 m_DefaultValue = value; 53 if ((label1 != null) || (label2 != null) || (label3 != null)) 54 { 55 m_Labels = new[] 56 { 57 label1 ?? k_LabelDefaults[0], 58 label2 ?? k_LabelDefaults[1], 59 label3 ?? k_LabelDefaults[2] 60 }; 61 } 62 } 63 64 public Vector3 defaultValue { get { return m_DefaultValue; } } 65 66 public Vector3 value 67 { 68 get { return m_Value; } 69 set { m_Value = value; } 70 } 71 72 public override bool isDefaultValue => value.Equals(defaultValue); 73 74 public override VisualElement InstantiateControl() 75 { 76 return new MultiFloatSlotControlView(owner, labels, () => value, (newValue) => value = newValue); 77 } 78 79 protected override string ConcreteSlotValueAsVariable() 80 { 81 return string.Format("$precision3 ({0}, {1}, {2})" 82 , NodeUtils.FloatToShaderValue(value.x) 83 , NodeUtils.FloatToShaderValue(value.y) 84 , NodeUtils.FloatToShaderValue(value.z)); 85 } 86 87 public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode) 88 { 89 if (!generationMode.IsPreview()) 90 return; 91 92 var matOwner = owner as AbstractMaterialNode; 93 if (matOwner == null) 94 throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode))); 95 96 var property = new Vector3ShaderProperty() 97 { 98 overrideReferenceName = matOwner.GetVariableNameForSlot(id), 99 generatePropertyBlock = false, 100 value = value 101 }; 102 properties.AddShaderProperty(property); 103 } 104 105 public override void GetPreviewProperties(List<PreviewProperty> properties, string name) 106 { 107 var pp = new PreviewProperty(PropertyType.Vector3) 108 { 109 name = name, 110 vector4Value = new Vector4(value.x, value.y, value.z, 0) 111 }; 112 properties.Add(pp); 113 } 114 115 public override SlotValueType valueType { get { return SlotValueType.Vector3; } } 116 public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector3; } } 117 118 public override void CopyValuesFrom(MaterialSlot foundSlot) 119 { 120 var slot = foundSlot as Vector3MaterialSlot; 121 if (slot != null) 122 value = slot.value; 123 } 124 125 public override void CopyDefaultValue(MaterialSlot other) 126 { 127 base.CopyDefaultValue(other); 128 if (other is IMaterialSlotHasValue<Vector3> ms) 129 { 130 m_DefaultValue = ms.defaultValue; 131 } 132 } 133 } 134}