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