A game about forced loneliness, made by TACStudios
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Threading;
5using UnityEditor;
6using UnityEditor.ShaderGraph.Internal;
7using UnityEngine;
8using UnityEngine.Rendering;
9using Object = UnityEngine.Object;
10
11namespace UnityEditor.ShaderGraph.Drawing
12{
13 internal static class ShaderGraphPropertyDrawers
14 {
15 static Dictionary<GraphInputData, bool> s_CompoundPropertyFoldoutStates = new();
16
17 public static void DrawShaderGraphGUI(MaterialEditor materialEditor, IEnumerable<MaterialProperty> properties)
18 {
19 Material m = materialEditor.target as Material;
20 Shader s = m.shader;
21 string path = AssetDatabase.GetAssetPath(s);
22 ShaderGraphMetadata metadata = null;
23 foreach (var obj in AssetDatabase.LoadAllAssetsAtPath(path))
24 {
25 if (obj is ShaderGraphMetadata meta)
26 {
27 metadata = meta;
28 break;
29 }
30 }
31
32 if (metadata != null)
33 DrawShaderGraphGUI(materialEditor, properties, metadata.categoryDatas);
34 else
35 PropertiesDefaultGUI(materialEditor, properties);
36 }
37
38 static void PropertiesDefaultGUI(MaterialEditor materialEditor, IEnumerable<MaterialProperty> properties)
39 {
40 foreach (var property in properties)
41 {
42 if ((property.flags & (MaterialProperty.PropFlags.HideInInspector | MaterialProperty.PropFlags.PerRendererData)) != 0)
43 continue;
44
45 float h = materialEditor.GetPropertyHeight(property, property.displayName);
46 Rect r = EditorGUILayout.GetControlRect(true, h, EditorStyles.layerMaskField);
47
48 materialEditor.ShaderProperty(r, property, property.displayName);
49 }
50 }
51
52 static Rect GetRect(MaterialProperty prop)
53 {
54 return EditorGUILayout.GetControlRect(true, MaterialEditor.GetDefaultPropertyHeight(prop));
55 }
56
57 static MaterialProperty FindProperty(string propertyName, IEnumerable<MaterialProperty> properties)
58 {
59 foreach (var prop in properties)
60 {
61 if (prop.name == propertyName)
62 {
63 return prop;
64 }
65 }
66
67 return null;
68 }
69
70 public static void DrawShaderGraphGUI(MaterialEditor materialEditor, IEnumerable<MaterialProperty> properties, IEnumerable<MinimalCategoryData> categoryDatas)
71 {
72 foreach (MinimalCategoryData mcd in categoryDatas)
73 {
74 DrawCategory(materialEditor, properties, mcd);
75 }
76 }
77
78 static void DrawCategory(MaterialEditor materialEditor, IEnumerable<MaterialProperty> properties, MinimalCategoryData minimalCategoryData)
79 {
80 if (minimalCategoryData.categoryName.Length > 0)
81 {
82 minimalCategoryData.expanded = EditorGUILayout.BeginFoldoutHeaderGroup(minimalCategoryData.expanded, minimalCategoryData.categoryName);
83 }
84 else
85 {
86 // force draw if no category name to do foldout on
87 minimalCategoryData.expanded = true;
88 }
89
90 if (minimalCategoryData.expanded)
91 {
92 foreach (var propData in minimalCategoryData.propertyDatas)
93 {
94 if (propData.isCompoundProperty == false)
95 {
96 MaterialProperty prop = FindProperty(propData.referenceName, properties);
97 if (prop == null) continue;
98 DrawMaterialProperty(materialEditor, prop, propData.propertyType, propData.isKeyword, propData.keywordType);
99 }
100 else
101 {
102 DrawCompoundProperty(materialEditor, properties, propData);
103 }
104 }
105 }
106
107 EditorGUILayout.EndFoldoutHeaderGroup();
108 }
109
110 static void DrawCompoundProperty(MaterialEditor materialEditor, IEnumerable<MaterialProperty> properties, GraphInputData compoundPropertyData)
111 {
112 EditorGUI.indentLevel++;
113
114 bool foldoutState = true;
115 var exists = s_CompoundPropertyFoldoutStates.ContainsKey(compoundPropertyData);
116 if (!exists)
117 s_CompoundPropertyFoldoutStates.Add(compoundPropertyData, true);
118 else
119 foldoutState = s_CompoundPropertyFoldoutStates[compoundPropertyData];
120
121 foldoutState = EditorGUILayout.Foldout(foldoutState, compoundPropertyData.referenceName);
122 if (foldoutState)
123 {
124 EditorGUI.indentLevel++;
125 foreach (var subProperty in compoundPropertyData.subProperties)
126 {
127 var property = FindProperty(subProperty.referenceName, properties);
128 if (property == null) continue;
129 DrawMaterialProperty(materialEditor, property, subProperty.propertyType);
130 }
131 EditorGUI.indentLevel--;
132 }
133
134 if (exists)
135 s_CompoundPropertyFoldoutStates[compoundPropertyData] = foldoutState;
136 EditorGUI.indentLevel--;
137 }
138
139 static void DrawMaterialProperty(MaterialEditor materialEditor, MaterialProperty property, PropertyType propertyType, bool isKeyword = false, KeywordType keywordType = KeywordType.Boolean)
140 {
141 if (isKeyword)
142 {
143 switch (keywordType)
144 {
145 case KeywordType.Boolean:
146 DrawBooleanKeyword(materialEditor, property);
147 break;
148 case KeywordType.Enum:
149 DrawEnumKeyword(materialEditor, property);
150 break;
151 }
152 }
153 else
154 {
155 switch (propertyType)
156 {
157 case PropertyType.SamplerState:
158 DrawSamplerStateProperty(materialEditor, property);
159 break;
160 case PropertyType.Matrix4:
161 DrawMatrix4Property(materialEditor, property);
162 break;
163 case PropertyType.Matrix3:
164 DrawMatrix3Property(materialEditor, property);
165 break;
166 case PropertyType.Matrix2:
167 DrawMatrix2Property(materialEditor, property);
168 break;
169 case PropertyType.Texture2D:
170 DrawTexture2DProperty(materialEditor, property);
171 break;
172 case PropertyType.Texture2DArray:
173 DrawTexture2DArrayProperty(materialEditor, property);
174 break;
175 case PropertyType.Texture3D:
176 DrawTexture3DProperty(materialEditor, property);
177 break;
178 case PropertyType.Cubemap:
179 DrawCubemapProperty(materialEditor, property);
180 break;
181 case PropertyType.Gradient:
182 break;
183 case PropertyType.Vector4:
184 DrawVector4Property(materialEditor, property);
185 break;
186 case PropertyType.Vector3:
187 DrawVector3Property(materialEditor, property);
188 break;
189 case PropertyType.Vector2:
190 DrawVector2Property(materialEditor, property);
191 break;
192 case PropertyType.Float:
193 DrawFloatProperty(materialEditor, property);
194 break;
195 case PropertyType.Boolean:
196 DrawBooleanProperty(materialEditor, property);
197 break;
198 case PropertyType.VirtualTexture:
199 DrawVirtualTextureProperty(materialEditor, property);
200 break;
201 case PropertyType.Color:
202 DrawColorProperty(materialEditor, property);
203 break;
204 }
205 }
206 }
207
208 static void DrawColorProperty(MaterialEditor materialEditor, MaterialProperty property)
209 {
210 materialEditor.ShaderProperty(property, property.displayName);
211 }
212
213 static void DrawEnumKeyword(MaterialEditor materialEditor, MaterialProperty property)
214 {
215 materialEditor.ShaderProperty(property, property.displayName);
216 }
217
218 static void DrawBooleanKeyword(MaterialEditor materialEditor, MaterialProperty property)
219 {
220 materialEditor.ShaderProperty(property, property.displayName);
221 }
222
223 static void DrawVirtualTextureProperty(MaterialEditor materialEditor, MaterialProperty property)
224 {
225 }
226
227 static void DrawBooleanProperty(MaterialEditor materialEditor, MaterialProperty property)
228 {
229 materialEditor.ShaderProperty(property, property.displayName);
230 }
231
232 static void DrawFloatProperty(MaterialEditor materialEditor, MaterialProperty property)
233 {
234 materialEditor.ShaderProperty(property, property.displayName);
235 }
236
237 static void DrawVector2Property(MaterialEditor materialEditor, MaterialProperty property)
238 {
239 EditorGUI.BeginChangeCheck();
240 EditorGUI.showMixedValue = property.hasMixedValue;
241 Vector2 newValue = EditorGUI.Vector2Field(GetRect(property), property.displayName, new Vector2(property.vectorValue.x, property.vectorValue.y));
242 EditorGUI.showMixedValue = false;
243 if (EditorGUI.EndChangeCheck())
244 {
245 property.vectorValue = newValue;
246 }
247 }
248
249 static void DrawVector3Property(MaterialEditor materialEditor, MaterialProperty property)
250 {
251 EditorGUI.BeginChangeCheck();
252 EditorGUI.showMixedValue = property.hasMixedValue;
253 Vector3 newValue = EditorGUI.Vector3Field(GetRect(property), property.displayName, new Vector3(property.vectorValue.x, property.vectorValue.y, property.vectorValue.z));
254 EditorGUI.showMixedValue = false;
255 if (EditorGUI.EndChangeCheck())
256 {
257 property.vectorValue = newValue;
258 }
259 }
260
261 static void DrawVector4Property(MaterialEditor materialEditor, MaterialProperty property)
262 {
263 materialEditor.ShaderProperty(property, property.displayName);
264 }
265
266 static void DrawCubemapProperty(MaterialEditor materialEditor, MaterialProperty property)
267 {
268 materialEditor.ShaderProperty(property, property.displayName);
269 }
270
271 static void DrawTexture3DProperty(MaterialEditor materialEditor, MaterialProperty property)
272 {
273 materialEditor.ShaderProperty(property, property.displayName);
274 }
275
276 static void DrawTexture2DArrayProperty(MaterialEditor materialEditor, MaterialProperty property)
277 {
278 materialEditor.ShaderProperty(property, property.displayName);
279 }
280
281 static void DrawTexture2DProperty(MaterialEditor materialEditor, MaterialProperty property)
282 {
283 materialEditor.ShaderProperty(property, property.displayName);
284 }
285
286 static void DrawMatrix2Property(MaterialEditor materialEditor, MaterialProperty property)
287 {
288 //we dont expose
289 }
290
291 static void DrawMatrix3Property(MaterialEditor materialEditor, MaterialProperty property)
292 {
293 //we dont expose
294 }
295
296 static void DrawMatrix4Property(MaterialEditor materialEditor, MaterialProperty property)
297 {
298 //we dont expose
299 }
300
301 static void DrawSamplerStateProperty(MaterialEditor materialEditor, MaterialProperty property)
302 {
303 //we dont expose
304 }
305 }
306}