You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

295 lines
8.7 KiB

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