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