OutputPort.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace AmplifyShaderEditor
  6. {
  7. [System.Serializable]
  8. public sealed class OutputPort : WirePort
  9. {
  10. public delegate void OnNewPreviewRTCreated();
  11. public OnNewPreviewRTCreated OnNewPreviewRTCreatedEvent;
  12. [SerializeField]
  13. private bool m_connectedToMasterNode;
  14. [SerializeField]
  15. private bool[] m_isLocalValue = { false, false};
  16. [SerializeField]
  17. private string[] m_localOutputValue = { string.Empty,string.Empty};
  18. //[SerializeField]
  19. //private int m_isLocalWithPortType = 0;
  20. private RenderTexture m_outputPreview = null;
  21. private Material m_outputMaskMaterial = null;
  22. private int m_indexPreviewOffset = 0;
  23. public OutputPort( ParentNode owner, int nodeId, int portId, WirePortDataType dataType, string name ) : base( nodeId, portId, dataType, name )
  24. {
  25. LabelSize = Vector2.zero;
  26. OnNewPreviewRTCreatedEvent += owner.SetPreviewDirtyFromOutputs;
  27. }
  28. public string ErrorValue
  29. {
  30. get
  31. {
  32. string value = string.Empty;
  33. switch( m_dataType )
  34. {
  35. default:
  36. case WirePortDataType.OBJECT:
  37. case WirePortDataType.INT:
  38. case WirePortDataType.FLOAT: value = "(0)"; break;
  39. case WirePortDataType.FLOAT2: value = "half2(0,0)"; break;
  40. case WirePortDataType.FLOAT3: value = "half3(0,0,0)"; break;
  41. case WirePortDataType.COLOR:
  42. case WirePortDataType.FLOAT4: value = "half4(0,0,0,0)"; break;
  43. case WirePortDataType.FLOAT3x3: value = "half3x3(0,0,0,0,0,0,0,0,0)"; break;
  44. case WirePortDataType.FLOAT4x4: value = "half4x4(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)"; break;
  45. }
  46. return value;
  47. }
  48. }
  49. public bool ConnectedToMasterNode
  50. {
  51. get { return m_connectedToMasterNode; }
  52. set { m_connectedToMasterNode = value; }
  53. }
  54. public override void FullDeleteConnections()
  55. {
  56. UIUtils.DeleteConnection( false, m_nodeId, m_portId, true, true );
  57. }
  58. public bool HasConnectedNode
  59. {
  60. get
  61. {
  62. int count = m_externalReferences.Count;
  63. for( int i = 0; i < count; i++ )
  64. {
  65. if( UIUtils.GetNode( m_externalReferences[ i ].NodeId ).IsConnected )
  66. return true;
  67. }
  68. return false;
  69. }
  70. }
  71. public InputPort GetInputConnection( int connID = 0 )
  72. {
  73. if( connID < m_externalReferences.Count )
  74. {
  75. return UIUtils.GetNode( m_externalReferences[ connID ].NodeId ).GetInputPortByUniqueId( m_externalReferences[ connID ].PortId );
  76. }
  77. return null;
  78. }
  79. public ParentNode GetInputNode( int connID = 0 )
  80. {
  81. if( connID < m_externalReferences.Count )
  82. {
  83. return UIUtils.GetNode( m_externalReferences[ connID ].NodeId );
  84. }
  85. return null;
  86. }
  87. public override void NotifyExternalRefencesOnChange()
  88. {
  89. for( int i = 0; i < m_externalReferences.Count; i++ )
  90. {
  91. ParentNode node = UIUtils.GetNode( m_externalReferences[ i ].NodeId );
  92. if( node )
  93. {
  94. node.CheckSpherePreview();
  95. InputPort port = node.GetInputPortByUniqueId( m_externalReferences[ i ].PortId );
  96. port.UpdateInfoOnExternalConn( m_nodeId, m_portId, m_dataType );
  97. node.OnConnectedOutputNodeChanges( m_externalReferences[ i ].PortId, m_nodeId, m_portId, m_name, m_dataType );
  98. }
  99. }
  100. }
  101. public void ChangeTypeWithRestrictions( WirePortDataType newType, int restrictions )
  102. {
  103. if( m_dataType != newType )
  104. {
  105. DataType = newType;
  106. for( int i = 0; i < m_externalReferences.Count; i++ )
  107. {
  108. ParentNode inNode = UIUtils.GetNode( m_externalReferences[ i ].NodeId );
  109. InputPort inputPort = inNode.GetInputPortByUniqueId( m_externalReferences[ i ].PortId );
  110. bool valid = false;
  111. if( restrictions == 0 )
  112. {
  113. valid = true;
  114. }
  115. else
  116. {
  117. valid = ( restrictions & (int)inputPort.DataType ) != 0;
  118. }
  119. if( valid )
  120. {
  121. inNode.CheckSpherePreview();
  122. inputPort.UpdateInfoOnExternalConn( m_nodeId, m_portId, m_dataType );
  123. inNode.OnConnectedOutputNodeChanges( m_externalReferences[ i ].PortId, m_nodeId, m_portId, m_name, m_dataType );
  124. }
  125. else
  126. {
  127. InvalidateConnection( m_externalReferences[ i ].NodeId, m_externalReferences[ i ].PortId );
  128. inputPort.InvalidateConnection( NodeId, PortId );
  129. i--;
  130. }
  131. }
  132. }
  133. }
  134. public override void ChangePortId( int newPortId )
  135. {
  136. if( IsConnected )
  137. {
  138. int count = ExternalReferences.Count;
  139. for( int connIdx = 0; connIdx < count; connIdx++ )
  140. {
  141. int nodeId = ExternalReferences[ connIdx ].NodeId;
  142. int portId = ExternalReferences[ connIdx ].PortId;
  143. ParentNode node = UIUtils.GetNode( nodeId );
  144. if( node != null )
  145. {
  146. InputPort inputPort = node.GetInputPortByUniqueId( portId );
  147. int inputCount = inputPort.ExternalReferences.Count;
  148. for( int j = 0; j < inputCount; j++ )
  149. {
  150. if( inputPort.ExternalReferences[ j ].NodeId == NodeId &&
  151. inputPort.ExternalReferences[ j ].PortId == PortId )
  152. {
  153. inputPort.ExternalReferences[ j ].PortId = newPortId;
  154. }
  155. }
  156. }
  157. }
  158. }
  159. PortId = newPortId;
  160. }
  161. public string ConfigOutputLocalValue( PrecisionType precisionType, string value, string customName, MasterNodePortCategory category )
  162. {
  163. int idx = UIUtils.PortCategorytoAttayIdx( category );
  164. ParentGraph currentGraph = UIUtils.GetNode( NodeId ).ContainerGraph;
  165. string autoGraphId = currentGraph.GraphId > 0 ? "_g" + currentGraph.GraphId : string.Empty;
  166. m_localOutputValue[idx] = string.IsNullOrEmpty( customName ) ? ( "temp_output_" + m_nodeId + "_" + PortId + autoGraphId ) : customName;
  167. m_isLocalValue[idx] = true;
  168. //m_isLocalWithPortType |= (int)category;
  169. return string.Format( Constants.LocalValueDecWithoutIdent, UIUtils.PrecisionWirePortToCgType( precisionType, DataType ), m_localOutputValue[idx], value );
  170. }
  171. public void SetLocalValue( string value, MasterNodePortCategory category )
  172. {
  173. int idx = UIUtils.PortCategorytoAttayIdx( category );
  174. m_isLocalValue[idx] = true;
  175. m_localOutputValue[ idx ] = value;
  176. //m_isLocalWithPortType |= (int)category;
  177. }
  178. public void ResetLocalValue()
  179. {
  180. for( int i = 0; i < m_localOutputValue.Length; i++ )
  181. {
  182. m_localOutputValue[ i ] = string.Empty;
  183. m_isLocalValue[i] = false;
  184. }
  185. //m_isLocalWithPortType = 0;
  186. }
  187. public void ResetLocalValueIfNot( MasterNodePortCategory category )
  188. {
  189. int idx = UIUtils.PortCategorytoAttayIdx( category );
  190. for( int i = 0; i < m_localOutputValue.Length; i++ )
  191. {
  192. if( i != idx )
  193. {
  194. m_localOutputValue[ i ] = string.Empty;
  195. m_isLocalValue[ i ] = false;
  196. }
  197. }
  198. }
  199. public void ResetLocalValueOnCategory( MasterNodePortCategory category )
  200. {
  201. int idx = UIUtils.PortCategorytoAttayIdx( category );
  202. m_localOutputValue[ idx ] = string.Empty;
  203. m_isLocalValue[ idx ] = false;
  204. }
  205. public bool IsLocalOnCategory( MasterNodePortCategory category )
  206. {
  207. int idx = UIUtils.PortCategorytoAttayIdx( category );
  208. return m_isLocalValue[ idx ];
  209. //return ( m_isLocalWithPortType & (int)category ) != 0; ;
  210. }
  211. public override void ForceClearConnection()
  212. {
  213. UIUtils.DeleteConnection( false, m_nodeId, m_portId, false, true );
  214. }
  215. public bool IsLocalValue( MasterNodePortCategory category )
  216. {
  217. int idx = UIUtils.PortCategorytoAttayIdx( category );
  218. return m_isLocalValue[ idx ];
  219. }
  220. public string LocalValue(MasterNodePortCategory category)
  221. {
  222. int idx = UIUtils.PortCategorytoAttayIdx( category );
  223. return m_localOutputValue[idx];
  224. }
  225. public RenderTexture OutputPreviewTexture
  226. {
  227. get
  228. {
  229. if( Preferences.User.DisablePreviews )
  230. {
  231. return UIUtils.DummyRT;
  232. }
  233. if( m_outputPreview == null )
  234. {
  235. m_outputPreview = new RenderTexture( Constants.PreviewSize , Constants.PreviewSize , 0 , Constants.PreviewFormat , RenderTextureReadWrite.Linear );
  236. m_outputPreview.wrapMode = TextureWrapMode.Repeat;
  237. if( OnNewPreviewRTCreatedEvent != null )
  238. OnNewPreviewRTCreatedEvent();
  239. }
  240. return m_outputPreview;
  241. }
  242. set { m_outputPreview = value; }
  243. }
  244. public int IndexPreviewOffset
  245. {
  246. get { return m_indexPreviewOffset; }
  247. set { m_indexPreviewOffset = value; }
  248. }
  249. public override void Destroy()
  250. {
  251. base.Destroy();
  252. if( m_outputPreview != null )
  253. {
  254. m_outputPreview.Release();
  255. UnityEngine.ScriptableObject.DestroyImmediate( m_outputPreview );
  256. }
  257. m_outputPreview = null;
  258. if( m_outputMaskMaterial != null )
  259. UnityEngine.ScriptableObject.DestroyImmediate( m_outputMaskMaterial );
  260. m_outputMaskMaterial = null;
  261. OnNewPreviewRTCreatedEvent = null;
  262. }
  263. public Material MaskingMaterial
  264. {
  265. get
  266. {
  267. if( m_outputMaskMaterial == null )
  268. {
  269. //m_outputMaskMaterial = new Material( AssetDatabase.LoadAssetAtPath<Shader>( AssetDatabase.GUIDToAssetPath( "9c34f18ebe2be3e48b201b748c73dec0" ) ) );
  270. m_outputMaskMaterial = new Material( UIUtils.MaskingShader );
  271. }
  272. return m_outputMaskMaterial;
  273. }
  274. //set { m_outputMaskMaterial = value; }
  275. }
  276. }
  277. }