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 2x2")]
10 class Matrix2Node : AbstractMaterialNode, IGeneratesBodyCode, IPropertyFromNode
11 {
12 public const int OutputSlotId = 0;
13 const string kOutputSlotName = "Out";
14
15 [SerializeField]
16 Vector2 m_Row0;
17
18 [SerializeField]
19 Vector2 m_Row1;
20
21 [MultiFloatControl("", " ", " ", " ", " ")]
22 public Vector2 row0
23 {
24 get { return m_Row0; }
25 set { SetRow(ref m_Row0, value); }
26 }
27
28 [MultiFloatControl("", " ", " ", " ", " ")]
29 public Vector2 row1
30 {
31 get { return m_Row1; }
32 set { SetRow(ref m_Row1, value); }
33 }
34
35 void SetRow(ref Vector2 row, Vector2 value)
36 {
37 if (value == row)
38 return;
39 row = value;
40 Dirty(ModificationScope.Node);
41 }
42
43 public Matrix2Node()
44 {
45 name = "Matrix 2x2";
46 UpdateNodeAfterDeserialization();
47 }
48
49 public sealed override void UpdateNodeAfterDeserialization()
50 {
51 AddSlot(new Matrix2MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output));
52 RemoveSlotsNameNotMatching(new[] { OutputSlotId });
53 }
54
55 public override void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
56 {
57 if (!generationMode.IsPreview())
58 return;
59
60 properties.AddShaderProperty(new Vector2ShaderProperty()
61 {
62 overrideReferenceName = string.Format("_{0}_m0", GetVariableNameForNode()),
63 generatePropertyBlock = false,
64 value = m_Row0
65 });
66
67 properties.AddShaderProperty(new Vector2ShaderProperty()
68 {
69 overrideReferenceName = string.Format("_{0}_m1", GetVariableNameForNode()),
70 generatePropertyBlock = false,
71 value = m_Row1
72 });
73 }
74
75 public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
76 {
77 if (!generationMode.IsPreview())
78 {
79 sb.AppendLine("$precision2 _{0}_m0 = $precision2 ({1}, {2});", GetVariableNameForNode(),
80 NodeUtils.FloatToShaderValue(m_Row0.x),
81 NodeUtils.FloatToShaderValue(m_Row0.y));
82 sb.AppendLine("$precision2 _{0}_m1 = $precision2 ({1}, {2});", GetVariableNameForNode(),
83 NodeUtils.FloatToShaderValue(m_Row1.x),
84 NodeUtils.FloatToShaderValue(m_Row1.y));
85 }
86 sb.AppendLine("$precision2x2 {0} = $precision2x2 (_{0}_m0.x, _{0}_m0.y, _{0}_m1.x, _{0}_m1.y);", GetVariableNameForNode());
87 }
88
89 public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
90 {
91 properties.Add(new PreviewProperty(PropertyType.Vector2)
92 {
93 name = string.Format("_{0}_m0", GetVariableNameForNode()),
94 vector4Value = m_Row0
95 });
96
97 properties.Add(new PreviewProperty(PropertyType.Vector2)
98 {
99 name = string.Format("_{0}_m1", GetVariableNameForNode()),
100 vector4Value = m_Row1
101 });
102 }
103
104 public override string GetVariableNameForSlot(int slotId)
105 {
106 return GetVariableNameForNode();
107 }
108
109 public AbstractShaderProperty AsShaderProperty()
110 {
111 return new Matrix2ShaderProperty
112 {
113 value = new Matrix4x4()
114 {
115 m00 = row0.x,
116 m01 = row0.y,
117 m02 = 0,
118 m03 = 0,
119 m10 = row1.x,
120 m11 = row1.y,
121 m12 = 0,
122 m13 = 0,
123 m20 = 0,
124 m21 = 0,
125 m22 = 0,
126 m23 = 0,
127 m30 = 0,
128 m31 = 0,
129 m32 = 0,
130 m33 = 0,
131 }
132 };
133 }
134
135 public int outputSlotId { get { return OutputSlotId; } }
136 }
137}