暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ShaderInputViewController.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEditor.Graphing;
  5. using UnityEditor.ShaderGraph.Drawing.Controls;
  6. using UnityEditor.ShaderGraph.Internal;
  7. using UnityEngine.Assertions;
  8. using UnityEngine.UIElements;
  9. using GraphDataStore = UnityEditor.ShaderGraph.DataStore<UnityEditor.ShaderGraph.GraphData>;
  10. namespace UnityEditor.ShaderGraph.Drawing
  11. {
  12. class ChangeExposedFlagAction : IGraphDataAction
  13. {
  14. internal ChangeExposedFlagAction(ShaderInput shaderInput, bool newIsExposed)
  15. {
  16. this.shaderInputReference = shaderInput;
  17. this.newIsExposedValue = newIsExposed;
  18. this.oldIsExposedValue = shaderInput.generatePropertyBlock;
  19. }
  20. void ChangeExposedFlag(GraphData graphData)
  21. {
  22. AssertHelpers.IsNotNull(graphData, "GraphData is null while carrying out ChangeExposedFlagAction");
  23. AssertHelpers.IsNotNull(shaderInputReference, "ShaderInputReference is null while carrying out ChangeExposedFlagAction");
  24. // The Undos are currently handled in ShaderInputPropertyDrawer but we want to move that out from there and handle here
  25. //graphData.owner.RegisterCompleteObjectUndo("Change Exposed Toggle");
  26. shaderInputReference.generatePropertyBlock = newIsExposedValue;
  27. }
  28. public Action<GraphData> modifyGraphDataAction => ChangeExposedFlag;
  29. // Reference to the shader input being modified
  30. internal ShaderInput shaderInputReference { get; private set; }
  31. // New value of whether the shader input should be exposed to the material inspector
  32. internal bool newIsExposedValue { get; private set; }
  33. internal bool oldIsExposedValue { get; private set; }
  34. }
  35. class ChangePropertyValueAction : IGraphDataAction
  36. {
  37. void ChangePropertyValue(GraphData graphData)
  38. {
  39. AssertHelpers.IsNotNull(graphData, "GraphData is null while carrying out ChangePropertyValueAction");
  40. AssertHelpers.IsNotNull(shaderInputReference, "ShaderPropertyReference is null while carrying out ChangePropertyValueAction");
  41. // The Undos are currently handled in ShaderInputPropertyDrawer but we want to move that out from there and handle here
  42. //graphData.owner.RegisterCompleteObjectUndo("Change Property Value");
  43. var material = graphData.owner.materialArtifact;
  44. switch (shaderInputReference)
  45. {
  46. case BooleanShaderProperty booleanProperty:
  47. booleanProperty.value = ((ToggleData)newShaderInputValue).isOn;
  48. if (material) material.SetFloat(shaderInputReference.referenceName, booleanProperty.value ? 1.0f : 0.0f);
  49. break;
  50. case Vector1ShaderProperty vector1Property:
  51. vector1Property.value = (float)newShaderInputValue;
  52. if (material) material.SetFloat(shaderInputReference.referenceName, vector1Property.value);
  53. break;
  54. case Vector2ShaderProperty vector2Property:
  55. vector2Property.value = (Vector2)newShaderInputValue;
  56. if (material) material.SetVector(shaderInputReference.referenceName, vector2Property.value);
  57. break;
  58. case Vector3ShaderProperty vector3Property:
  59. vector3Property.value = (Vector3)newShaderInputValue;
  60. if (material) material.SetVector(shaderInputReference.referenceName, vector3Property.value);
  61. break;
  62. case Vector4ShaderProperty vector4Property:
  63. vector4Property.value = (Vector4)newShaderInputValue;
  64. if (material) material.SetVector(shaderInputReference.referenceName, vector4Property.value);
  65. break;
  66. case ColorShaderProperty colorProperty:
  67. colorProperty.value = (Color)newShaderInputValue;
  68. if (material) material.SetColor(shaderInputReference.referenceName, colorProperty.value);
  69. break;
  70. case Texture2DShaderProperty texture2DProperty:
  71. texture2DProperty.value.texture = (Texture)newShaderInputValue;
  72. if (material) material.SetTexture(shaderInputReference.referenceName, texture2DProperty.value.texture);
  73. break;
  74. case Texture2DArrayShaderProperty texture2DArrayProperty:
  75. texture2DArrayProperty.value.textureArray = (Texture2DArray)newShaderInputValue;
  76. if (material) material.SetTexture(shaderInputReference.referenceName, texture2DArrayProperty.value.textureArray);
  77. break;
  78. case Texture3DShaderProperty texture3DProperty:
  79. texture3DProperty.value.texture = (Texture3D)newShaderInputValue;
  80. if (material) material.SetTexture(shaderInputReference.referenceName, texture3DProperty.value.texture);
  81. break;
  82. case CubemapShaderProperty cubemapProperty:
  83. cubemapProperty.value.cubemap = (Cubemap)newShaderInputValue;
  84. if (material) material.SetTexture(shaderInputReference.referenceName, cubemapProperty.value.cubemap);
  85. break;
  86. case Matrix2ShaderProperty matrix2Property:
  87. matrix2Property.value = (Matrix4x4)newShaderInputValue;
  88. break;
  89. case Matrix3ShaderProperty matrix3Property:
  90. matrix3Property.value = (Matrix4x4)newShaderInputValue;
  91. break;
  92. case Matrix4ShaderProperty matrix4Property:
  93. matrix4Property.value = (Matrix4x4)newShaderInputValue;
  94. break;
  95. case SamplerStateShaderProperty samplerStateProperty:
  96. samplerStateProperty.value = (TextureSamplerState)newShaderInputValue;
  97. break;
  98. case GradientShaderProperty gradientProperty:
  99. gradientProperty.value = (Gradient)newShaderInputValue;
  100. break;
  101. default:
  102. throw new ArgumentOutOfRangeException();
  103. }
  104. }
  105. public Action<GraphData> modifyGraphDataAction => ChangePropertyValue;
  106. // Reference to the shader input being modified
  107. internal ShaderInput shaderInputReference { get; set; }
  108. // New value of the shader property
  109. internal object newShaderInputValue { get; set; }
  110. }
  111. class ChangeDisplayNameAction : IGraphDataAction
  112. {
  113. void ChangeDisplayName(GraphData graphData)
  114. {
  115. AssertHelpers.IsNotNull(graphData, "GraphData is null while carrying out ChangeDisplayNameAction");
  116. AssertHelpers.IsNotNull(shaderInputReference, "ShaderInputReference is null while carrying out ChangeDisplayNameAction");
  117. graphData.owner.RegisterCompleteObjectUndo("Change Display Name");
  118. if (newDisplayNameValue != shaderInputReference.displayName)
  119. {
  120. shaderInputReference.SetDisplayNameAndSanitizeForGraph(graphData, newDisplayNameValue);
  121. }
  122. }
  123. public Action<GraphData> modifyGraphDataAction => ChangeDisplayName;
  124. // Reference to the shader input being modified
  125. internal ShaderInput shaderInputReference { get; set; }
  126. internal string newDisplayNameValue { get; set; }
  127. }
  128. class ChangeReferenceNameAction : IGraphDataAction
  129. {
  130. void ChangeReferenceName(GraphData graphData)
  131. {
  132. AssertHelpers.IsNotNull(graphData, "GraphData is null while carrying out ChangeReferenceNameAction");
  133. AssertHelpers.IsNotNull(shaderInputReference, "ShaderInputReference is null while carrying out ChangeReferenceNameAction");
  134. // The Undos are currently handled in ShaderInputPropertyDrawer but we want to move that out from there and handle here
  135. //graphData.owner.RegisterCompleteObjectUndo("Change Reference Name");
  136. if (newReferenceNameValue != shaderInputReference.overrideReferenceName)
  137. {
  138. graphData.SanitizeGraphInputReferenceName(shaderInputReference, newReferenceNameValue);
  139. }
  140. }
  141. public Action<GraphData> modifyGraphDataAction => ChangeReferenceName;
  142. // Reference to the shader input being modified
  143. internal ShaderInput shaderInputReference { get; set; }
  144. internal string newReferenceNameValue { get; set; }
  145. }
  146. class ResetReferenceNameAction : IGraphDataAction
  147. {
  148. void ResetReferenceName(GraphData graphData)
  149. {
  150. AssertHelpers.IsNotNull(graphData, "GraphData is null while carrying out ResetReferenceNameAction");
  151. AssertHelpers.IsNotNull(shaderInputReference, "ShaderInputReference is null while carrying out ResetReferenceNameAction");
  152. graphData.owner.RegisterCompleteObjectUndo("Reset Reference Name");
  153. shaderInputReference.overrideReferenceName = null;
  154. }
  155. public Action<GraphData> modifyGraphDataAction => ResetReferenceName;
  156. // Reference to the shader input being modified
  157. internal ShaderInput shaderInputReference { get; set; }
  158. }
  159. class DeleteShaderInputAction : IGraphDataAction
  160. {
  161. void DeleteShaderInput(GraphData graphData)
  162. {
  163. AssertHelpers.IsNotNull(graphData, "GraphData is null while carrying out DeleteShaderInputAction");
  164. AssertHelpers.IsNotNull(shaderInputsToDelete, "ShaderInputsToDelete is null while carrying out DeleteShaderInputAction");
  165. // This is called by MaterialGraphView currently, no need to repeat it here, though ideally it would live here
  166. //graphData.owner.RegisterCompleteObjectUndo("Delete Graph Input(s)");
  167. foreach (var shaderInput in shaderInputsToDelete)
  168. {
  169. graphData.RemoveGraphInput(shaderInput);
  170. }
  171. }
  172. public Action<GraphData> modifyGraphDataAction => DeleteShaderInput;
  173. // Reference to the shader input(s) being deleted
  174. internal IList<ShaderInput> shaderInputsToDelete { get; set; } = new List<ShaderInput>();
  175. }
  176. class ShaderInputViewController : SGViewController<ShaderInput, ShaderInputViewModel>
  177. {
  178. // Exposed for PropertyView
  179. internal GraphData graphData => DataStore.State;
  180. internal ShaderInputViewController(ShaderInput shaderInput, ShaderInputViewModel inViewModel, GraphDataStore graphDataStore)
  181. : base(shaderInput, inViewModel, graphDataStore)
  182. {
  183. InitializeViewModel();
  184. m_SgBlackboardField = new SGBlackboardField(ViewModel);
  185. m_SgBlackboardField.controller = this;
  186. m_BlackboardRowView = new SGBlackboardRow(m_SgBlackboardField, null);
  187. m_BlackboardRowView.expanded = SessionState.GetBool($"Unity.ShaderGraph.Input.{shaderInput.objectId}.isExpanded", false);
  188. }
  189. void InitializeViewModel()
  190. {
  191. if (Model == null)
  192. {
  193. AssertHelpers.Fail("Could not initialize shader input view model as shader input was null.");
  194. return;
  195. }
  196. ViewModel.model = Model;
  197. ViewModel.isSubGraph = DataStore.State.isSubGraph;
  198. ViewModel.isInputExposed = (DataStore.State.isSubGraph || Model.isExposed);
  199. ViewModel.inputName = Model.displayName;
  200. switch (Model)
  201. {
  202. case AbstractShaderProperty shaderProperty:
  203. ViewModel.inputTypeName = shaderProperty.GetPropertyTypeString();
  204. // Handles upgrade fix for deprecated old Color property
  205. shaderProperty.onBeforeVersionChange += (_) => graphData.owner.RegisterCompleteObjectUndo($"Change {shaderProperty.displayName} Version");
  206. break;
  207. case ShaderKeyword shaderKeyword:
  208. ViewModel.inputTypeName = shaderKeyword.keywordType + " Keyword";
  209. ViewModel.inputTypeName = shaderKeyword.isBuiltIn ? "Built-in " + ViewModel.inputTypeName : ViewModel.inputTypeName;
  210. break;
  211. case ShaderDropdown shaderDropdown:
  212. ViewModel.inputTypeName = "Dropdown";
  213. break;
  214. }
  215. ViewModel.requestModelChangeAction = this.RequestModelChange;
  216. }
  217. SGBlackboardRow m_BlackboardRowView;
  218. SGBlackboardField m_SgBlackboardField;
  219. internal SGBlackboardRow BlackboardItemView => m_BlackboardRowView;
  220. protected override void RequestModelChange(IGraphDataAction changeAction)
  221. {
  222. DataStore.Dispatch(changeAction);
  223. }
  224. // Called by GraphDataStore.Subscribe after the model has been changed
  225. protected override void ModelChanged(GraphData graphData, IGraphDataAction changeAction)
  226. {
  227. switch (changeAction)
  228. {
  229. case ChangeExposedFlagAction changeExposedFlagAction:
  230. // ModelChanged is called overzealously on everything
  231. // but we only care if the action pertains to our Model
  232. if (changeExposedFlagAction.shaderInputReference == Model)
  233. {
  234. ViewModel.isInputExposed = Model.generatePropertyBlock;
  235. if (changeExposedFlagAction.oldIsExposedValue != changeExposedFlagAction.newIsExposedValue)
  236. DirtyNodes(ModificationScope.Graph);
  237. m_SgBlackboardField.UpdateFromViewModel();
  238. }
  239. break;
  240. case ChangePropertyValueAction changePropertyValueAction:
  241. if (changePropertyValueAction.shaderInputReference == Model)
  242. {
  243. DirtyNodes(ModificationScope.Graph);
  244. m_SgBlackboardField.MarkDirtyRepaint();
  245. }
  246. break;
  247. case ResetReferenceNameAction resetReferenceNameAction:
  248. if (resetReferenceNameAction.shaderInputReference == Model)
  249. {
  250. DirtyNodes(ModificationScope.Graph);
  251. }
  252. break;
  253. case ChangeReferenceNameAction changeReferenceNameAction:
  254. if (changeReferenceNameAction.shaderInputReference == Model)
  255. {
  256. DirtyNodes(ModificationScope.Graph);
  257. }
  258. break;
  259. case ChangeDisplayNameAction changeDisplayNameAction:
  260. if (changeDisplayNameAction.shaderInputReference == Model)
  261. {
  262. ViewModel.inputName = Model.displayName;
  263. DirtyNodes(ModificationScope.Layout);
  264. m_SgBlackboardField.UpdateFromViewModel();
  265. }
  266. break;
  267. }
  268. }
  269. // TODO: This should communicate to node controllers instead of searching for the nodes themselves everytime, but that's going to take a while...
  270. internal void DirtyNodes(ModificationScope modificationScope = ModificationScope.Node)
  271. {
  272. foreach (var observer in Model.InputObservers)
  273. {
  274. observer.OnShaderInputUpdated(modificationScope);
  275. }
  276. switch (Model)
  277. {
  278. case AbstractShaderProperty property:
  279. var graphEditorView = m_BlackboardRowView.GetFirstAncestorOfType<GraphEditorView>();
  280. if (graphEditorView == null)
  281. return;
  282. var colorManager = graphEditorView.colorManager;
  283. var nodes = graphEditorView.graphView.Query<MaterialNodeView>().ToList();
  284. colorManager.SetNodesDirty(nodes);
  285. colorManager.UpdateNodeViews(nodes);
  286. break;
  287. case ShaderKeyword keyword:
  288. // Cant determine if Sub Graphs contain the keyword so just update them
  289. foreach (var node in DataStore.State.GetNodes<SubGraphNode>())
  290. {
  291. node.Dirty(modificationScope);
  292. }
  293. break;
  294. case ShaderDropdown dropdown:
  295. // Cant determine if Sub Graphs contain the dropdown so just update them
  296. foreach (var node in DataStore.State.GetNodes<SubGraphNode>())
  297. {
  298. node.Dirty(modificationScope);
  299. }
  300. break;
  301. default:
  302. throw new ArgumentOutOfRangeException();
  303. }
  304. }
  305. public override void Dispose()
  306. {
  307. if (m_SgBlackboardField == null)
  308. return;
  309. Model?.ClearObservers();
  310. base.Dispose();
  311. Cleanup();
  312. BlackboardItemView.Dispose();
  313. m_SgBlackboardField.Dispose();
  314. m_BlackboardRowView = null;
  315. m_SgBlackboardField = null;
  316. }
  317. }
  318. }