using System; using UnityEditor.Graphing; using UnityEditor.ShaderGraph.Drawing.Slots; using UnityEngine; using UnityEngine.Experimental.UIElements; namespace UnityEditor.ShaderGraph { [Serializable] public class Vector2MaterialSlot : MaterialSlot, IMaterialSlotHasVaule { [SerializeField] private Vector2 m_Value; [SerializeField] private Vector2 m_DefaultValue; public Vector2MaterialSlot() {} public Vector2MaterialSlot( int slotId, string displayName, string shaderOutputName, SlotType slotType, Vector2 value, ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false) : base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden) { m_Value = value; } public Vector2 defaultValue { get { return m_DefaultValue; } } public Vector2 value { get { return m_Value; } set { m_Value = value; } } public override VisualElement InstantiateControl() { return new MultiFloatSlotControlView(owner, 2, () => value, (newValue) => value = newValue); } protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision) { return precision + "2 (" + value.x + "," + value.y + ")"; } public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode) { if (!generationMode.IsPreview()) return; var matOwner = owner as AbstractMaterialNode; if (matOwner == null) throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode))); var property = new Vector2ShaderProperty { overrideReferenceName = matOwner.GetVariableNameForSlot(id), generatePropertyBlock = false, value = value }; properties.AddShaderProperty(property); } public override PreviewProperty GetPreviewProperty(string name) { var pp = new PreviewProperty { name = name, propType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType), vector4Value = new Vector4(value.x, value.y, 0, 0), floatValue = value.x, colorValue = new Vector4(value.x, value.x, 0, 0), }; return pp; } public override SlotValueType valueType { get { return SlotValueType.Vector2; } } public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector2; } } public override void CopyValuesFrom(MaterialSlot foundSlot) { var slot = foundSlot as Vector2MaterialSlot; if (slot != null) value = slot.value; } } }