123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- // Amplify Shader Editor - Visual Shader Editing Tool
- // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
- using UnityEngine;
- using UnityEditor;
- using System;
- using System.Collections.Generic;
- namespace AmplifyShaderEditor
- {
- [Serializable]
- [NodeAttributes( "Template Fragment Data" , "Surface Data" , "Select and use available interpolated fragment data from the template" )]
- public class TemplateFragmentDataNode : TemplateNodeParent
- {
- private List<TemplateVertexData> m_interpolatorData = null;
- [SerializeField]
- private int m_currentDataIdx = -1;
- [SerializeField]
- private string m_dataName = string.Empty;
- [SerializeField]
- private string m_inVarName = string.Empty;
- private string[] m_dataLabels = null;
- private bool m_fetchDataId = false;
- private UpperLeftWidgetHelper m_upperLeftWidgetHelper = new UpperLeftWidgetHelper();
- protected override void CommonInit( int uniqueId )
- {
- base.CommonInit( uniqueId );
- m_autoWrapProperties = true;
- }
- void FetchDataId()
- {
- if( m_interpolatorData != null )
- {
- m_currentDataIdx = 0;
- int count = m_interpolatorData.Count;
- m_dataLabels = new string[ count ];
- for( int i = 0 ; i < count ; i++ )
- {
- m_dataLabels[ i ] = m_interpolatorData[ i ].VarName;
- if( m_interpolatorData[ i ].VarName.Equals( m_dataName ) )
- {
- m_currentDataIdx = i;
- }
- }
- UpdateFromId();
- }
- else
- {
- m_currentDataIdx = -1;
- }
- }
- void UpdateFromId()
- {
- if( m_interpolatorData != null )
- {
- if( m_interpolatorData.Count == 0 )
- {
- for( int i = 0 ; i < 4 ; i++ )
- m_containerGraph.DeleteConnection( false , UniqueId , i , false , true );
- m_headerColor = UIUtils.GetColorFromCategory( "Default" );
- SetAdditonalTitleText( "<None>" );
- m_additionalContent.text = string.Empty;
- m_outputPorts[ 0 ].ChangeProperties( "None" , WirePortDataType.OBJECT , false );
- ConfigurePorts();
- return;
- }
- bool areCompatible = TemplateHelperFunctions.CheckIfCompatibles( m_outputPorts[ 0 ].DataType , m_interpolatorData[ m_currentDataIdx ].DataType );
- switch( m_interpolatorData[ m_currentDataIdx ].DataType )
- {
- default:
- case WirePortDataType.INT:
- case WirePortDataType.FLOAT:
- m_outputPorts[ 0 ].ChangeProperties( Constants.EmptyPortValue , m_interpolatorData[ m_currentDataIdx ].DataType , false );
- break;
- case WirePortDataType.FLOAT2:
- m_outputPorts[ 0 ].ChangeProperties( "XY" , m_interpolatorData[ m_currentDataIdx ].DataType , false );
- break;
- case WirePortDataType.FLOAT3:
- m_outputPorts[ 0 ].ChangeProperties( "XYZ" , m_interpolatorData[ m_currentDataIdx ].DataType , false );
- break;
- case WirePortDataType.FLOAT4:
- m_outputPorts[ 0 ].ChangeProperties( "XYZW" , m_interpolatorData[ m_currentDataIdx ].DataType , false );
- break;
- case WirePortDataType.COLOR:
- m_outputPorts[ 0 ].ChangeProperties( "RGBA" , m_interpolatorData[ m_currentDataIdx ].DataType , false );
- break;
- }
- ConfigurePorts();
- if( !areCompatible )
- {
- m_containerGraph.DeleteConnection( false , UniqueId , 0 , false , true );
- }
- m_dataName = m_interpolatorData[ m_currentDataIdx ].VarName;
- SetAdditonalTitleText( m_dataName );
- m_sizeIsDirty = true;
- CheckWarningState();
- }
- }
- public override void DrawProperties()
- {
- base.DrawProperties();
- if( m_containerGraph.CurrentCanvasMode != NodeAvailability.TemplateShader )
- {
- return;
- }
- if( m_multiPassMode )
- {
- DrawMultipassProperties();
- }
- if( m_currentDataIdx > -1 )
- {
- EditorGUI.BeginChangeCheck();
- m_currentDataIdx = EditorGUILayoutPopup( DataLabelStr , m_currentDataIdx , m_dataLabels );
- if( EditorGUI.EndChangeCheck() )
- {
- UpdateFromId();
- }
- }
- }
- protected override void OnSubShaderChange()
- {
- base.OnSubShaderChange();
- FetchInterpolator();
- FetchDataId();
- }
- protected override void OnPassChange()
- {
- FetchInterpolator();
- FetchDataId();
- }
- void DrawMultipassProperties()
- {
- DrawSubShaderUI();
- DrawPassUI();
- }
- public override void Draw( DrawInfo drawInfo )
- {
- base.Draw( drawInfo );
- if( m_containerGraph.CurrentCanvasMode != NodeAvailability.TemplateShader )
- return;
- if( m_interpolatorData == null || m_interpolatorData.Count == 0 )
- {
- MasterNode masterNode = m_containerGraph.CurrentMasterNode;
- FetchInterpolator( masterNode );
- }
- if( m_fetchDataId )
- {
- m_fetchDataId = false;
- FetchDataId();
- }
- if( m_currentDataIdx > -1 )
- {
- EditorGUI.BeginChangeCheck();
- m_currentDataIdx = m_upperLeftWidgetHelper.DrawWidget( this , m_currentDataIdx , m_dataLabels );
- if( EditorGUI.EndChangeCheck() )
- {
- UpdateFromId();
- }
- }
- }
- public override string GenerateShaderForOutput( int outputId , ref MasterNodeDataCollector dataCollector , bool ignoreLocalvar )
- {
- if( dataCollector.MasterNodeCategory != AvailableShaderTypes.Template )
- {
- UIUtils.ShowMessage( UniqueId , "Template Fragmment Data node is only intended for templates use only" , MessageSeverity.Error );
- return m_outputPorts[ 0 ].ErrorValue;
- }
- if( !dataCollector.IsFragmentCategory )
- {
- UIUtils.ShowMessage( UniqueId , "Template Fragment Data node node is only intended for fragment use use only" , MessageSeverity.Error );
- return m_outputPorts[ 0 ].ErrorValue;
- }
- if( m_multiPassMode )
- {
- if( dataCollector.TemplateDataCollectorInstance.MultipassSubshaderIdx != SubShaderIdx ||
- dataCollector.TemplateDataCollectorInstance.MultipassPassIdx != PassIdx
- )
- {
- UIUtils.ShowMessage( UniqueId , string.Format( "{0} is only intended for subshader {1} and pass {2}" , m_dataLabels[ m_currentDataIdx ] , SubShaderIdx , PassIdx ) );
- return m_outputPorts[ outputId ].ErrorValue;
- }
- }
- return GetOutputVectorItem( 0 , outputId , m_inVarName + m_dataName );
- }
- public override void ReadFromString( ref string[] nodeParams )
- {
- base.ReadFromString( ref nodeParams );
- m_dataName = GetCurrentParam( ref nodeParams );
- m_fetchDataId = true;
- }
- public override void WriteToString( ref string nodeInfo , ref string connectionsInfo )
- {
- base.WriteToString( ref nodeInfo , ref connectionsInfo );
- IOUtils.AddFieldValueToString( ref nodeInfo , m_dataName );
- }
- public override void OnMasterNodeReplaced( MasterNode newMasterNode )
- {
- base.OnMasterNodeReplaced( newMasterNode );
- if( newMasterNode.CurrentMasterNodeCategory == AvailableShaderTypes.Template )
- {
- FetchInterpolator( newMasterNode );
- }
- else
- {
- m_interpolatorData = null;
- m_currentDataIdx = -1;
- }
- }
- protected override bool ValidatePass( int passIdx )
- {
- return ( m_templateMPData.SubShaders[ SubShaderIdx ].Passes[ passIdx ].FragmentFunctionData != null &&
- m_templateMPData.SubShaders[ SubShaderIdx ].Passes[ passIdx ].InterpolatorDataContainer != null );
- }
- void FetchInterpolator( MasterNode masterNode = null )
- {
- FetchMultiPassTemplate( masterNode );
- if( m_multiPassMode )
- {
- if( m_templateMPData != null )
- {
- m_inVarName = m_templateMPData.SubShaders[ SubShaderIdx ].Passes[ PassIdx ].FragmentFunctionData.InVarName + ".";
- m_interpolatorData = m_templateMPData.SubShaders[ SubShaderIdx ].Passes[ PassIdx ].InterpolatorDataContainer.RawInterpolators;
- m_fetchDataId = true;
- }
- }
- else
- {
- if( masterNode == null )
- masterNode = m_containerGraph.CurrentMasterNode;
- TemplateData currentTemplate = ( masterNode as TemplateMasterNode ).CurrentTemplate;
- if( currentTemplate != null )
- {
- m_inVarName = currentTemplate.FragmentFunctionData.InVarName + ".";
- m_interpolatorData = currentTemplate.InterpolatorData.RawInterpolators;
- FetchDataId();
- }
- else
- {
- m_interpolatorData = null;
- m_currentDataIdx = -1;
- }
- }
- }
- public override void Destroy()
- {
- base.Destroy();
- m_dataLabels = null;
- m_interpolatorData = null;
- m_upperLeftWidgetHelper = null;
- }
- }
- }
|