123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- // Amplify Shader Editor - Visual Shader Editing Tool
- // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
- using UnityEditor;
- using UnityEngine;
- namespace AmplifyShaderEditor
- {
- [System.Serializable]
- public sealed class OutputPort : WirePort
- {
- public delegate void OnNewPreviewRTCreated();
- public OnNewPreviewRTCreated OnNewPreviewRTCreatedEvent;
- [SerializeField]
- private bool m_connectedToMasterNode;
- [SerializeField]
- private bool[] m_isLocalValue = { false, false};
- [SerializeField]
- private string[] m_localOutputValue = { string.Empty,string.Empty};
- //[SerializeField]
- //private int m_isLocalWithPortType = 0;
- private RenderTexture m_outputPreview = null;
- private Material m_outputMaskMaterial = null;
- private int m_indexPreviewOffset = 0;
- public OutputPort( ParentNode owner, int nodeId, int portId, WirePortDataType dataType, string name ) : base( nodeId, portId, dataType, name )
- {
- LabelSize = Vector2.zero;
- OnNewPreviewRTCreatedEvent += owner.SetPreviewDirtyFromOutputs;
- }
- public string ErrorValue
- {
- get
- {
- string value = string.Empty;
- switch( m_dataType )
- {
- default:
- case WirePortDataType.OBJECT:
- case WirePortDataType.INT:
- case WirePortDataType.FLOAT: value = "(0)"; break;
- case WirePortDataType.FLOAT2: value = "half2(0,0)"; break;
- case WirePortDataType.FLOAT3: value = "half3(0,0,0)"; break;
- case WirePortDataType.COLOR:
- case WirePortDataType.FLOAT4: value = "half4(0,0,0,0)"; break;
- case WirePortDataType.FLOAT3x3: value = "half3x3(0,0,0,0,0,0,0,0,0)"; break;
- case WirePortDataType.FLOAT4x4: value = "half4x4(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)"; break;
- }
- return value;
- }
- }
- public bool ConnectedToMasterNode
- {
- get { return m_connectedToMasterNode; }
- set { m_connectedToMasterNode = value; }
- }
- public override void FullDeleteConnections()
- {
- UIUtils.DeleteConnection( false, m_nodeId, m_portId, true, true );
- }
- public bool HasConnectedNode
- {
- get
- {
- int count = m_externalReferences.Count;
- for( int i = 0; i < count; i++ )
- {
- if( UIUtils.GetNode( m_externalReferences[ i ].NodeId ).IsConnected )
- return true;
- }
- return false;
- }
- }
- public InputPort GetInputConnection( int connID = 0 )
- {
- if( connID < m_externalReferences.Count )
- {
- return UIUtils.GetNode( m_externalReferences[ connID ].NodeId ).GetInputPortByUniqueId( m_externalReferences[ connID ].PortId );
- }
- return null;
- }
- public ParentNode GetInputNode( int connID = 0 )
- {
- if( connID < m_externalReferences.Count )
- {
- return UIUtils.GetNode( m_externalReferences[ connID ].NodeId );
- }
- return null;
- }
- public override void NotifyExternalRefencesOnChange()
- {
- for( int i = 0; i < m_externalReferences.Count; i++ )
- {
- ParentNode node = UIUtils.GetNode( m_externalReferences[ i ].NodeId );
- if( node )
- {
- node.CheckSpherePreview();
- InputPort port = node.GetInputPortByUniqueId( m_externalReferences[ i ].PortId );
- port.UpdateInfoOnExternalConn( m_nodeId, m_portId, m_dataType );
- node.OnConnectedOutputNodeChanges( m_externalReferences[ i ].PortId, m_nodeId, m_portId, m_name, m_dataType );
- }
- }
- }
- public void ChangeTypeWithRestrictions( WirePortDataType newType, int restrictions )
- {
- if( m_dataType != newType )
- {
- DataType = newType;
- for( int i = 0; i < m_externalReferences.Count; i++ )
- {
- ParentNode inNode = UIUtils.GetNode( m_externalReferences[ i ].NodeId );
- InputPort inputPort = inNode.GetInputPortByUniqueId( m_externalReferences[ i ].PortId );
- bool valid = false;
- if( restrictions == 0 )
- {
- valid = true;
- }
- else
- {
- valid = ( restrictions & (int)inputPort.DataType ) != 0;
- }
- if( valid )
- {
- inNode.CheckSpherePreview();
- inputPort.UpdateInfoOnExternalConn( m_nodeId, m_portId, m_dataType );
- inNode.OnConnectedOutputNodeChanges( m_externalReferences[ i ].PortId, m_nodeId, m_portId, m_name, m_dataType );
- }
- else
- {
- InvalidateConnection( m_externalReferences[ i ].NodeId, m_externalReferences[ i ].PortId );
- inputPort.InvalidateConnection( NodeId, PortId );
- i--;
- }
- }
- }
- }
- public override void ChangePortId( int newPortId )
- {
- if( IsConnected )
- {
- int count = ExternalReferences.Count;
- for( int connIdx = 0; connIdx < count; connIdx++ )
- {
- int nodeId = ExternalReferences[ connIdx ].NodeId;
- int portId = ExternalReferences[ connIdx ].PortId;
- ParentNode node = UIUtils.GetNode( nodeId );
- if( node != null )
- {
- InputPort inputPort = node.GetInputPortByUniqueId( portId );
- int inputCount = inputPort.ExternalReferences.Count;
- for( int j = 0; j < inputCount; j++ )
- {
- if( inputPort.ExternalReferences[ j ].NodeId == NodeId &&
- inputPort.ExternalReferences[ j ].PortId == PortId )
- {
- inputPort.ExternalReferences[ j ].PortId = newPortId;
- }
- }
- }
- }
- }
- PortId = newPortId;
- }
- public string ConfigOutputLocalValue( PrecisionType precisionType, string value, string customName, MasterNodePortCategory category )
- {
- int idx = UIUtils.PortCategorytoAttayIdx( category );
- ParentGraph currentGraph = UIUtils.GetNode( NodeId ).ContainerGraph;
- string autoGraphId = currentGraph.GraphId > 0 ? "_g" + currentGraph.GraphId : string.Empty;
- m_localOutputValue[idx] = string.IsNullOrEmpty( customName ) ? ( "temp_output_" + m_nodeId + "_" + PortId + autoGraphId ) : customName;
- m_isLocalValue[idx] = true;
- //m_isLocalWithPortType |= (int)category;
- return string.Format( Constants.LocalValueDecWithoutIdent, UIUtils.PrecisionWirePortToCgType( precisionType, DataType ), m_localOutputValue[idx], value );
- }
- public void SetLocalValue( string value, MasterNodePortCategory category )
- {
- int idx = UIUtils.PortCategorytoAttayIdx( category );
- m_isLocalValue[idx] = true;
- m_localOutputValue[ idx ] = value;
- //m_isLocalWithPortType |= (int)category;
- }
- public void ResetLocalValue()
- {
- for( int i = 0; i < m_localOutputValue.Length; i++ )
- {
- m_localOutputValue[ i ] = string.Empty;
- m_isLocalValue[i] = false;
- }
- //m_isLocalWithPortType = 0;
- }
- public void ResetLocalValueIfNot( MasterNodePortCategory category )
- {
- int idx = UIUtils.PortCategorytoAttayIdx( category );
- for( int i = 0; i < m_localOutputValue.Length; i++ )
- {
- if( i != idx )
- {
- m_localOutputValue[ i ] = string.Empty;
- m_isLocalValue[ i ] = false;
- }
- }
- }
- public void ResetLocalValueOnCategory( MasterNodePortCategory category )
- {
- int idx = UIUtils.PortCategorytoAttayIdx( category );
- m_localOutputValue[ idx ] = string.Empty;
- m_isLocalValue[ idx ] = false;
- }
- public bool IsLocalOnCategory( MasterNodePortCategory category )
- {
- int idx = UIUtils.PortCategorytoAttayIdx( category );
- return m_isLocalValue[ idx ];
- //return ( m_isLocalWithPortType & (int)category ) != 0; ;
- }
- public override void ForceClearConnection()
- {
- UIUtils.DeleteConnection( false, m_nodeId, m_portId, false, true );
- }
- public bool IsLocalValue( MasterNodePortCategory category )
- {
- int idx = UIUtils.PortCategorytoAttayIdx( category );
- return m_isLocalValue[ idx ];
- }
- public string LocalValue(MasterNodePortCategory category)
- {
- int idx = UIUtils.PortCategorytoAttayIdx( category );
- return m_localOutputValue[idx];
- }
- public RenderTexture OutputPreviewTexture
- {
- get
- {
- if( Preferences.User.DisablePreviews )
- {
- return UIUtils.DummyRT;
- }
- if( m_outputPreview == null )
- {
- m_outputPreview = new RenderTexture( Constants.PreviewSize , Constants.PreviewSize , 0 , Constants.PreviewFormat , RenderTextureReadWrite.Linear );
- m_outputPreview.wrapMode = TextureWrapMode.Repeat;
- if( OnNewPreviewRTCreatedEvent != null )
- OnNewPreviewRTCreatedEvent();
- }
- return m_outputPreview;
- }
- set { m_outputPreview = value; }
- }
- public int IndexPreviewOffset
- {
- get { return m_indexPreviewOffset; }
- set { m_indexPreviewOffset = value; }
- }
- public override void Destroy()
- {
- base.Destroy();
- if( m_outputPreview != null )
- {
- m_outputPreview.Release();
- UnityEngine.ScriptableObject.DestroyImmediate( m_outputPreview );
- }
- m_outputPreview = null;
- if( m_outputMaskMaterial != null )
- UnityEngine.ScriptableObject.DestroyImmediate( m_outputMaskMaterial );
- m_outputMaskMaterial = null;
- OnNewPreviewRTCreatedEvent = null;
- }
- public Material MaskingMaterial
- {
- get
- {
- if( m_outputMaskMaterial == null )
- {
- //m_outputMaskMaterial = new Material( AssetDatabase.LoadAssetAtPath<Shader>( AssetDatabase.GUIDToAssetPath( "9c34f18ebe2be3e48b201b748c73dec0" ) ) );
- m_outputMaskMaterial = new Material( UIUtils.MaskingShader );
- }
- return m_outputMaskMaterial;
- }
- //set { m_outputMaskMaterial = value; }
- }
- }
- }
|