MaterialSlot.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using UnityEngine;
  6. using UnityEditor.Graphing;
  7. using UnityEditor.ShaderGraph.Internal;
  8. using UnityEngine.UIElements;
  9. namespace UnityEditor.ShaderGraph
  10. {
  11. [Serializable]
  12. abstract class MaterialSlot : ISlot
  13. {
  14. const string k_NotInit = "Not Initilaized";
  15. [SerializeField]
  16. int m_Id;
  17. [SerializeField]
  18. string m_DisplayName = k_NotInit;
  19. [SerializeField]
  20. SlotType m_SlotType = SlotType.Input;
  21. [SerializeField]
  22. int m_Priority = int.MaxValue;
  23. [SerializeField]
  24. bool m_Hidden;
  25. [SerializeField]
  26. string m_ShaderOutputName;
  27. [SerializeField]
  28. ShaderStageCapability m_StageCapability;
  29. bool m_HasError;
  30. protected MaterialSlot() {}
  31. protected MaterialSlot(int slotId, string displayName, string shaderOutputName, SlotType slotType, ShaderStageCapability stageCapability = ShaderStageCapability.All, bool hidden = false)
  32. {
  33. m_Id = slotId;
  34. m_DisplayName = displayName;
  35. m_SlotType = slotType;
  36. m_Hidden = hidden;
  37. m_ShaderOutputName = shaderOutputName;
  38. this.stageCapability = stageCapability;
  39. }
  40. protected MaterialSlot(int slotId, string displayName, string shaderOutputName, SlotType slotType, int priority, ShaderStageCapability stageCapability = ShaderStageCapability.All, bool hidden = false)
  41. {
  42. m_Id = slotId;
  43. m_DisplayName = displayName;
  44. m_SlotType = slotType;
  45. m_Priority = priority;
  46. m_Hidden = hidden;
  47. m_ShaderOutputName = shaderOutputName;
  48. this.stageCapability = stageCapability;
  49. }
  50. public virtual VisualElement InstantiateControl()
  51. {
  52. return null;
  53. }
  54. static string ConcreteSlotValueTypeAsString(ConcreteSlotValueType type)
  55. {
  56. switch (type)
  57. {
  58. case ConcreteSlotValueType.Vector1:
  59. return "(1)";
  60. case ConcreteSlotValueType.Vector2:
  61. return "(2)";
  62. case ConcreteSlotValueType.Vector3:
  63. return "(3)";
  64. case ConcreteSlotValueType.Vector4:
  65. return "(4)";
  66. case ConcreteSlotValueType.Boolean:
  67. return "(B)";
  68. case ConcreteSlotValueType.Matrix2:
  69. return "(2x2)";
  70. case ConcreteSlotValueType.Matrix3:
  71. return "(3x3)";
  72. case ConcreteSlotValueType.Matrix4:
  73. return "(4x4)";
  74. case ConcreteSlotValueType.SamplerState:
  75. return "(SS)";
  76. case ConcreteSlotValueType.Texture2D:
  77. return "(T2)";
  78. case ConcreteSlotValueType.Texture2DArray:
  79. return "(T2A)";
  80. case ConcreteSlotValueType.Texture3D:
  81. return "(T3)";
  82. case ConcreteSlotValueType.Cubemap:
  83. return "(C)";
  84. case ConcreteSlotValueType.Gradient:
  85. return "(G)";
  86. default:
  87. return "(E)";
  88. }
  89. }
  90. public virtual string displayName
  91. {
  92. get { return m_DisplayName + ConcreteSlotValueTypeAsString(concreteValueType); }
  93. set { m_DisplayName = value; }
  94. }
  95. public string RawDisplayName()
  96. {
  97. return m_DisplayName;
  98. }
  99. public static MaterialSlot CreateMaterialSlot(SlotValueType type, int slotId, string displayName, string shaderOutputName, SlotType slotType, Vector4 defaultValue, ShaderStageCapability shaderStageCapability = ShaderStageCapability.All, bool hidden = false)
  100. {
  101. switch (type)
  102. {
  103. case SlotValueType.SamplerState:
  104. return new SamplerStateMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStageCapability, hidden);
  105. case SlotValueType.DynamicMatrix:
  106. return new DynamicMatrixMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStageCapability, hidden);
  107. case SlotValueType.Matrix4:
  108. return new Matrix4MaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStageCapability, hidden);
  109. case SlotValueType.Matrix3:
  110. return new Matrix3MaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStageCapability, hidden);
  111. case SlotValueType.Matrix2:
  112. return new Matrix2MaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStageCapability, hidden);
  113. case SlotValueType.Texture2D:
  114. return slotType == SlotType.Input
  115. ? new Texture2DInputMaterialSlot(slotId, displayName, shaderOutputName, shaderStageCapability, hidden)
  116. : new Texture2DMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStageCapability, hidden);
  117. case SlotValueType.Texture2DArray:
  118. return slotType == SlotType.Input
  119. ? new Texture2DArrayInputMaterialSlot(slotId, displayName, shaderOutputName, shaderStageCapability, hidden)
  120. : new Texture2DArrayMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStageCapability, hidden);
  121. case SlotValueType.Texture3D:
  122. return slotType == SlotType.Input
  123. ? new Texture3DInputMaterialSlot(slotId, displayName, shaderOutputName, shaderStageCapability, hidden)
  124. : new Texture3DMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStageCapability, hidden);
  125. case SlotValueType.Cubemap:
  126. return slotType == SlotType.Input
  127. ? new CubemapInputMaterialSlot(slotId, displayName, shaderOutputName, shaderStageCapability, hidden)
  128. : new CubemapMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStageCapability, hidden);
  129. case SlotValueType.Gradient:
  130. return slotType == SlotType.Input
  131. ? new GradientInputMaterialSlot(slotId, displayName, shaderOutputName, shaderStageCapability, hidden)
  132. : new GradientMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStageCapability, hidden);
  133. case SlotValueType.DynamicVector:
  134. return new DynamicVectorMaterialSlot(slotId, displayName, shaderOutputName, slotType, defaultValue, shaderStageCapability, hidden);
  135. case SlotValueType.Vector4:
  136. return new Vector4MaterialSlot(slotId, displayName, shaderOutputName, slotType, defaultValue, shaderStageCapability, hidden: hidden);
  137. case SlotValueType.Vector3:
  138. return new Vector3MaterialSlot(slotId, displayName, shaderOutputName, slotType, defaultValue, shaderStageCapability, hidden: hidden);
  139. case SlotValueType.Vector2:
  140. return new Vector2MaterialSlot(slotId, displayName, shaderOutputName, slotType, defaultValue, shaderStageCapability, hidden: hidden);
  141. case SlotValueType.Vector1:
  142. return new Vector1MaterialSlot(slotId, displayName, shaderOutputName, slotType, defaultValue.x, shaderStageCapability, hidden: hidden);
  143. case SlotValueType.Dynamic:
  144. return new DynamicValueMaterialSlot(slotId, displayName, shaderOutputName, slotType, new Matrix4x4(defaultValue, Vector4.zero, Vector4.zero, Vector4.zero), shaderStageCapability, hidden);
  145. case SlotValueType.Boolean:
  146. return new BooleanMaterialSlot(slotId, displayName, shaderOutputName, slotType, false, shaderStageCapability, hidden);
  147. }
  148. throw new ArgumentOutOfRangeException("type", type, null);
  149. }
  150. public SlotReference slotReference
  151. {
  152. get { return new SlotReference(owner.guid, m_Id); }
  153. }
  154. public AbstractMaterialNode owner { get; set; }
  155. public bool hidden
  156. {
  157. get { return m_Hidden; }
  158. set { m_Hidden = value; }
  159. }
  160. public int id
  161. {
  162. get { return m_Id; }
  163. }
  164. public int priority
  165. {
  166. get { return m_Priority; }
  167. set { m_Priority = value; }
  168. }
  169. public bool isInputSlot
  170. {
  171. get { return m_SlotType == SlotType.Input; }
  172. }
  173. public bool isOutputSlot
  174. {
  175. get { return m_SlotType == SlotType.Output; }
  176. }
  177. public SlotType slotType
  178. {
  179. get { return m_SlotType; }
  180. }
  181. public bool isConnected
  182. {
  183. get
  184. {
  185. // node and graph respectivly
  186. if (owner == null || owner.owner == null)
  187. return false;
  188. var graph = owner.owner;
  189. var edges = graph.GetEdges(slotReference);
  190. return edges.Any();
  191. }
  192. }
  193. public abstract SlotValueType valueType { get; }
  194. public abstract ConcreteSlotValueType concreteValueType { get; }
  195. public string shaderOutputName
  196. {
  197. get { return m_ShaderOutputName; }
  198. private set { m_ShaderOutputName = value; }
  199. }
  200. public ShaderStageCapability stageCapability
  201. {
  202. get { return m_StageCapability; }
  203. set { m_StageCapability = value; }
  204. }
  205. public bool hasError
  206. {
  207. get { return m_HasError; }
  208. set { m_HasError = value; }
  209. }
  210. public bool IsCompatibleWith(MaterialSlot otherSlot)
  211. {
  212. return otherSlot != null
  213. && otherSlot.owner != owner
  214. && otherSlot.isInputSlot != isInputSlot
  215. && ((isInputSlot
  216. ? SlotValueHelper.AreCompatible(valueType, otherSlot.concreteValueType)
  217. : SlotValueHelper.AreCompatible(otherSlot.valueType, concreteValueType)));
  218. }
  219. public bool IsCompatibleStageWith(MaterialSlot otherSlot)
  220. {
  221. var candidateStage = otherSlot.stageCapability;
  222. return stageCapability == ShaderStageCapability.All || candidateStage == stageCapability;
  223. }
  224. public string GetDefaultValue(GenerationMode generationMode, ConcretePrecision concretePrecision)
  225. {
  226. string defaultValue = GetDefaultValue(generationMode);
  227. return defaultValue.Replace(PrecisionUtil.Token, concretePrecision.ToShaderString());
  228. }
  229. public virtual string GetDefaultValue(GenerationMode generationMode)
  230. {
  231. var matOwner = owner as AbstractMaterialNode;
  232. if (matOwner == null)
  233. throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
  234. if (generationMode.IsPreview())
  235. return matOwner.GetVariableNameForSlot(id);
  236. return ConcreteSlotValueAsVariable();
  237. }
  238. protected virtual string ConcreteSlotValueAsVariable()
  239. {
  240. return "error";
  241. }
  242. public abstract void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode);
  243. public virtual void GetPreviewProperties(List<PreviewProperty> properties, string name)
  244. {
  245. properties.Add(default(PreviewProperty));
  246. }
  247. public abstract void CopyValuesFrom(MaterialSlot foundSlot);
  248. bool Equals(MaterialSlot other)
  249. {
  250. return m_Id == other.m_Id && owner.guid.Equals(other.owner.guid);
  251. }
  252. public bool Equals(ISlot other)
  253. {
  254. return Equals(other as object);
  255. }
  256. public override bool Equals(object obj)
  257. {
  258. if (ReferenceEquals(null, obj)) return false;
  259. if (ReferenceEquals(this, obj)) return true;
  260. if (obj.GetType() != this.GetType()) return false;
  261. return Equals((MaterialSlot)obj);
  262. }
  263. public override int GetHashCode()
  264. {
  265. unchecked
  266. {
  267. return (m_Id * 397) ^ (owner != null ? owner.GetHashCode() : 0);
  268. }
  269. }
  270. }
  271. }