A game about forced loneliness, made by TACStudios
1using UnityEditor.ShaderGraph.Drawing.Controls;
2using UnityEngine;
3using UnityEditor.Graphing;
4using System.Collections.Generic;
5using UnityEditor.ShaderGraph.Internal;
6
7namespace UnityEditor.ShaderGraph
8{
9 [Title("Input", "Matrix", "Matrix 3x3")]
10 class Matrix3Node : AbstractMaterialNode, IGeneratesBodyCode, IPropertyFromNode
11 {
12 public const int OutputSlotId = 0;
13 const string kOutputSlotName = "Out";
14
15 [SerializeField]
16 Vector3 m_Row0;
17
18 [SerializeField]
19 Vector3 m_Row1;
20
21 [SerializeField]
22 Vector3 m_Row2;
23
24 [MultiFloatControl("", " ", " ", " ", " ")]
25 public Vector3 row0
26 {
27 get { return m_Row0; }
28 set { SetRow(ref m_Row0, value); }
29 }
30
31 [MultiFloatControl("", " ", " ", " ", " ")]
32 public Vector3 row1
33 {
34 get { return m_Row1; }
35 set { SetRow(ref m_Row1, value); }
36 }
37
38 [MultiFloatControl("", " ", " ", " ", " ")]
39 public Vector3 row2
40 {
41 get { return m_Row2; }
42 set { SetRow(ref m_Row2, value); }
43 }
44
45 void SetRow(ref Vector3 row, Vector3 value)
46 {
47 if (value == row)
48 return;
49 row = value;
50 Dirty(ModificationScope.Node);
51 }
52
53 public Matrix3Node()
54 {
55 name = "Matrix 3x3";
56 UpdateNodeAfterDeserialization();
57 }
58
59 public sealed override void UpdateNodeAfterDeserialization()
60 {
61 AddSlot(new Matrix3MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output));
62 RemoveSlotsNameNotMatching(new[] { OutputSlotId });
63 }
64
65 public override void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
66 {
67 if (!generationMode.IsPreview())
68 return;
69
70 properties.AddShaderProperty(new Vector3ShaderProperty()
71 {
72 overrideReferenceName = string.Format("_{0}_m0", GetVariableNameForNode()),
73 generatePropertyBlock = false,
74 value = m_Row0
75 });
76
77 properties.AddShaderProperty(new Vector3ShaderProperty()
78 {
79 overrideReferenceName = string.Format("_{0}_m1", GetVariableNameForNode()),
80 generatePropertyBlock = false,
81 value = m_Row1
82 });
83
84 properties.AddShaderProperty(new Vector3ShaderProperty()
85 {
86 overrideReferenceName = string.Format("_{0}_m2", GetVariableNameForNode()),
87 generatePropertyBlock = false,
88 value = m_Row2
89 });
90 }
91
92 public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
93 {
94 if (!generationMode.IsPreview())
95 {
96 sb.AppendLine("$precision3 _{0}_m0 = $precision3 ({1}, {2}, {3});", GetVariableNameForNode(),
97 NodeUtils.FloatToShaderValue(m_Row0.x),
98 NodeUtils.FloatToShaderValue(m_Row0.y),
99 NodeUtils.FloatToShaderValue(m_Row0.z));
100 sb.AppendLine("$precision3 _{0}_m1 = $precision3 ({1}, {2}, {3});", GetVariableNameForNode(),
101 NodeUtils.FloatToShaderValue(m_Row1.x),
102 NodeUtils.FloatToShaderValue(m_Row1.y),
103 NodeUtils.FloatToShaderValue(m_Row1.z));
104 sb.AppendLine("$precision3 _{0}_m2 = $precision3 ({1}, {2}, {3});", GetVariableNameForNode(),
105 NodeUtils.FloatToShaderValue(m_Row2.x),
106 NodeUtils.FloatToShaderValue(m_Row2.y),
107 NodeUtils.FloatToShaderValue(m_Row2.z));
108 }
109 sb.AppendLine("$precision3x3 {0} = $precision3x3 (_{0}_m0.x, _{0}_m0.y, _{0}_m0.z, _{0}_m1.x, _{0}_m1.y, _{0}_m1.z, _{0}_m2.x, _{0}_m2.y, _{0}_m2.z);", GetVariableNameForNode());
110 }
111
112 public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
113 {
114 properties.Add(new PreviewProperty(PropertyType.Vector3)
115 {
116 name = string.Format("_{0}_m0", GetVariableNameForNode()),
117 vector4Value = m_Row0
118 });
119
120 properties.Add(new PreviewProperty(PropertyType.Vector3)
121 {
122 name = string.Format("_{0}_m1", GetVariableNameForNode()),
123 vector4Value = m_Row1
124 });
125
126 properties.Add(new PreviewProperty(PropertyType.Vector3)
127 {
128 name = string.Format("_{0}_m2", GetVariableNameForNode()),
129 vector4Value = m_Row2
130 });
131 }
132
133 public override string GetVariableNameForSlot(int slotId)
134 {
135 return GetVariableNameForNode();
136 }
137
138 public AbstractShaderProperty AsShaderProperty()
139 {
140 return new Matrix3ShaderProperty
141 {
142 value = new Matrix4x4()
143 {
144 m00 = row0.x,
145 m01 = row0.y,
146 m02 = row0.z,
147 m03 = 0,
148 m10 = row1.x,
149 m11 = row1.y,
150 m12 = row1.z,
151 m13 = 0,
152 m20 = row2.x,
153 m21 = row2.y,
154 m22 = row2.z,
155 m23 = 0,
156 m30 = 0,
157 m31 = 0,
158 m32 = 0,
159 m33 = 0,
160 }
161 };
162 }
163
164 public int outputSlotId { get { return OutputSlotId; } }
165 }
166}