説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ShaderGraphPropertyDrawers.cs 12KB

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