123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- // Amplify Shader Editor - Visual Shader Editing Tool
- // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
- //
- // Custom Node Swizzle
- // Donated by Tobias Pott - @ Tobias Pott
- // www.tobiaspott.de
- using System;
- using UnityEditor;
- using UnityEngine;
- namespace AmplifyShaderEditor
- {
- [Serializable]
- [NodeAttributes( "Swizzle", "Vector Operators", "Swizzle components of vector types", null, KeyCode.Z, true, false, null, null, "Tobias Pott - @TobiasPott" )]
- public sealed class SwizzleNode : SingleInputOp
- {
- private const string OutputTypeStr = "Output type";
- [SerializeField]
- private WirePortDataType m_selectedOutputType = WirePortDataType.FLOAT4;
- [SerializeField]
- private int m_selectedOutputTypeInt = 3;
- [SerializeField]
- private int[] m_selectedOutputSwizzleTypes = new int[] { 0, 1, 2, 3 };
- [SerializeField]
- private int m_maskId;
- [SerializeField]
- private Vector4 m_maskValue = Vector4.one;
- private readonly string[] SwizzleVectorChannels = { "x", "y", "z", "w" };
- private readonly string[] SwizzleColorChannels = { "r", "g", "b", "a" };
- private readonly string[] SwizzleChannelLabels = { "Channel 0", "Channel 1", "Channel 2", "Channel 3" };
- private readonly string[] m_outputValueTypes ={ "Float",
- "Vector 2",
- "Vector 3",
- "Vector 4"};
- protected override void CommonInit( int uniqueId )
- {
- base.CommonInit( uniqueId );
- m_inputPorts[ 0 ].CreatePortRestrictions( WirePortDataType.FLOAT,
- WirePortDataType.FLOAT2,
- WirePortDataType.FLOAT3,
- WirePortDataType.FLOAT4,
- WirePortDataType.COLOR,
- WirePortDataType.INT );
- m_inputPorts[ 0 ].DataType = WirePortDataType.FLOAT4;
- m_outputPorts[ 0 ].DataType = m_selectedOutputType;
- m_textLabelWidth = 90;
- m_autoWrapProperties = true;
- m_autoUpdateOutputPort = false;
- m_hasLeftDropdown = true;
- m_previewShaderGUID = "d20531704ce28b14bafb296f291f6608";
- m_previewMaterialPassId = 0;
- SetAdditonalTitleText( "Value( XYZW )" );
- CalculatePreviewData();
- }
- public override void OnEnable()
- {
- base.OnEnable();
- m_maskId = Shader.PropertyToID( "_Mask" );
- }
- public override void SetPreviewInputs()
- {
- base.SetPreviewInputs();
- PreviewMaterial.SetVector( m_maskId, m_maskValue );
- }
- void CalculatePreviewData()
- {
- int inputMaxChannelId = 0;
- switch ( m_inputPorts[ 0 ].DataType )
- {
- case WirePortDataType.FLOAT4:
- case WirePortDataType.COLOR:
- inputMaxChannelId = 3;
- break;
- case WirePortDataType.FLOAT3:
- inputMaxChannelId = 2;
- break;
- case WirePortDataType.FLOAT2:
- inputMaxChannelId = 1;
- break;
- case WirePortDataType.INT:
- case WirePortDataType.FLOAT:
- inputMaxChannelId = 0;
- break;
- case WirePortDataType.OBJECT:
- case WirePortDataType.FLOAT3x3:
- case WirePortDataType.FLOAT4x4:
- break;
- }
- for ( int i = 0; i < 4; i++ )
- {
- int channel = m_selectedOutputSwizzleTypes[ Mathf.Min( m_selectedOutputTypeInt, i ) ];
- m_maskValue[ i ] = Mathf.Min( inputMaxChannelId, channel );
- }
- }
- public override void AfterCommonInit()
- {
- base.AfterCommonInit();
- if ( PaddingTitleLeft == 0 )
- {
- PaddingTitleLeft = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
- if ( PaddingTitleRight == 0 )
- PaddingTitleRight = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
- }
- }
- public override void OnConnectedOutputNodeChanges( int outputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
- {
- base.OnConnectedOutputNodeChanges( outputPortId, otherNodeId, otherPortId, name, type );
- UpdatePorts();
- }
- public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
- {
- base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
- UpdatePorts();
- }
- public override void OnInputPortDisconnected( int portId )
- {
- base.OnInputPortDisconnected( portId );
- UpdatePorts();
- }
- public override void Draw( DrawInfo drawInfo )
- {
- base.Draw( drawInfo );
- if ( m_dropdownEditing )
- {
- EditorGUI.BeginChangeCheck();
- m_selectedOutputTypeInt = EditorGUIPopup( m_dropdownRect, m_selectedOutputTypeInt, m_outputValueTypes, UIUtils.PropertyPopUp );
- if ( EditorGUI.EndChangeCheck() )
- {
- switch ( m_selectedOutputTypeInt )
- {
- case 0: m_selectedOutputType = WirePortDataType.FLOAT; break;
- case 1: m_selectedOutputType = WirePortDataType.FLOAT2; break;
- case 2: m_selectedOutputType = WirePortDataType.FLOAT3; break;
- case 3: m_selectedOutputType = WirePortDataType.FLOAT4; break;
- }
- UpdatePorts();
- DropdownEditing = false;
- }
- }
- }
- public override void DrawProperties()
- {
- EditorGUILayout.BeginVertical();
- EditorGUI.BeginChangeCheck();
- m_selectedOutputTypeInt = EditorGUILayoutPopup( OutputTypeStr, m_selectedOutputTypeInt, m_outputValueTypes );
- if ( EditorGUI.EndChangeCheck() )
- {
- switch ( m_selectedOutputTypeInt )
- {
- case 0: m_selectedOutputType = WirePortDataType.FLOAT; break;
- case 1: m_selectedOutputType = WirePortDataType.FLOAT2; break;
- case 2: m_selectedOutputType = WirePortDataType.FLOAT3; break;
- case 3: m_selectedOutputType = WirePortDataType.FLOAT4; break;
- }
- UpdatePorts();
- }
- EditorGUILayout.EndVertical();
- // Draw base properties
- base.DrawProperties();
- EditorGUILayout.BeginVertical();
- int count = 0;
- switch ( m_selectedOutputType )
- {
- case WirePortDataType.FLOAT4:
- case WirePortDataType.COLOR:
- count = 4;
- break;
- case WirePortDataType.FLOAT3:
- count = 3;
- break;
- case WirePortDataType.FLOAT2:
- count = 2;
- break;
- case WirePortDataType.INT:
- case WirePortDataType.FLOAT:
- count = 1;
- break;
- case WirePortDataType.OBJECT:
- case WirePortDataType.FLOAT3x3:
- case WirePortDataType.FLOAT4x4:
- break;
- }
- EditorGUI.BeginChangeCheck();
- if ( m_inputPorts[ 0 ].DataType == WirePortDataType.COLOR )
- {
- for ( int i = 0; i < count; i++ )
- {
- m_selectedOutputSwizzleTypes[ i ] = EditorGUILayoutPopup( SwizzleChannelLabels[ i ], m_selectedOutputSwizzleTypes[ i ], SwizzleColorChannels );
- }
- }
- else
- {
- for ( int i = 0; i < count; i++ )
- {
- m_selectedOutputSwizzleTypes[ i ] = EditorGUILayoutPopup( SwizzleChannelLabels[ i ], m_selectedOutputSwizzleTypes[ i ], SwizzleVectorChannels );
- }
- }
- if ( EditorGUI.EndChangeCheck() )
- {
- UpdatePorts();
- }
- EditorGUILayout.EndVertical();
- }
- void UpdatePorts()
- {
- ChangeOutputType( m_selectedOutputType, false );
- int count = 0;
- switch ( m_selectedOutputType )
- {
- case WirePortDataType.FLOAT4:
- case WirePortDataType.COLOR:
- count = 4;
- break;
- case WirePortDataType.FLOAT3:
- count = 3;
- break;
- case WirePortDataType.FLOAT2:
- count = 2;
- break;
- case WirePortDataType.INT:
- case WirePortDataType.FLOAT:
- count = 1;
- break;
- case WirePortDataType.OBJECT:
- case WirePortDataType.FLOAT3x3:
- case WirePortDataType.FLOAT4x4:
- break;
- }
- int inputMaxChannelId = 0;
- switch ( m_inputPorts[ 0 ].DataType )
- {
- case WirePortDataType.FLOAT4:
- case WirePortDataType.COLOR:
- inputMaxChannelId = 3;
- break;
- case WirePortDataType.FLOAT3:
- inputMaxChannelId = 2;
- break;
- case WirePortDataType.FLOAT2:
- inputMaxChannelId = 1;
- break;
- case WirePortDataType.INT:
- case WirePortDataType.FLOAT:
- inputMaxChannelId = 0;
- break;
- case WirePortDataType.OBJECT:
- case WirePortDataType.FLOAT3x3:
- case WirePortDataType.FLOAT4x4:
- break;
- }
- //for ( int i = 0; i < count; i++ )
- //{
- //m_selectedOutputSwizzleTypes[ i ] = Mathf.Clamp( m_selectedOutputSwizzleTypes[ i ], 0, inputMaxChannelId );
- //}
- // Update Title
- string additionalText = string.Empty;
- for ( int i = 0; i < count; i++ )
- {
- int currentSwizzle = Mathf.Min( inputMaxChannelId, m_selectedOutputSwizzleTypes[ i ] );
- additionalText += GetSwizzleComponentForChannel( currentSwizzle ).ToUpper();
- }
- if ( additionalText.Length > 0 )
- SetAdditonalTitleText( "Value( " + additionalText + " )" );
- else
- SetAdditonalTitleText( string.Empty );
- CalculatePreviewData();
- m_sizeIsDirty = true;
- }
- public string GetSwizzleComponentForChannel( int channel )
- {
- if ( m_inputPorts[ 0 ].DataType == WirePortDataType.COLOR )
- {
- return SwizzleColorChannels[ channel ];
- }
- else
- {
- return SwizzleVectorChannels[ channel ];
- }
- }
- public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
- {
- if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
- return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
-
- string value = string.Format( "({0}).", m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector ) );
- int inputMaxChannelId = 0;
- switch( m_inputPorts[ 0 ].DataType )
- {
- case WirePortDataType.FLOAT4:
- case WirePortDataType.COLOR:
- inputMaxChannelId = 3;
- break;
- case WirePortDataType.FLOAT3:
- inputMaxChannelId = 2;
- break;
- case WirePortDataType.FLOAT2:
- inputMaxChannelId = 1;
- break;
- case WirePortDataType.INT:
- case WirePortDataType.FLOAT:
- inputMaxChannelId = 0;
- break;
- case WirePortDataType.OBJECT:
- case WirePortDataType.FLOAT3x3:
- case WirePortDataType.FLOAT4x4:
- break;
- }
- int count = 0;
- switch ( m_selectedOutputType )
- {
- case WirePortDataType.FLOAT4:
- case WirePortDataType.COLOR:
- count = 4;
- break;
- case WirePortDataType.FLOAT3:
- count = 3;
- break;
- case WirePortDataType.FLOAT2:
- count = 2;
- break;
- case WirePortDataType.INT:
- case WirePortDataType.FLOAT:
- count = 1;
- break;
- case WirePortDataType.OBJECT:
- case WirePortDataType.FLOAT3x3:
- case WirePortDataType.FLOAT4x4:
- break;
- }
- for ( int i = 0; i < count; i++ )
- {
- int currentSwizzle = Mathf.Min( inputMaxChannelId, m_selectedOutputSwizzleTypes[ i ] );
- value += GetSwizzleComponentForChannel( currentSwizzle );
- }
- return CreateOutputLocalVariable( 0, value, ref dataCollector );
- }
- public override void ReadFromString( ref string[] nodeParams )
- {
- base.ReadFromString( ref nodeParams );
- m_selectedOutputType = ( WirePortDataType ) Enum.Parse( typeof( WirePortDataType ), GetCurrentParam( ref nodeParams ) );
- switch ( m_selectedOutputType )
- {
- case WirePortDataType.FLOAT: m_selectedOutputTypeInt = 0; break;
- case WirePortDataType.FLOAT2: m_selectedOutputTypeInt = 1; break;
- case WirePortDataType.FLOAT3: m_selectedOutputTypeInt = 2; break;
- case WirePortDataType.COLOR:
- case WirePortDataType.FLOAT4: m_selectedOutputTypeInt = 3; break;
- }
- for ( int i = 0; i < m_selectedOutputSwizzleTypes.Length; i++ )
- {
- m_selectedOutputSwizzleTypes[ i ] = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
- }
- UpdatePorts();
- }
- public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
- {
- base.WriteToString( ref nodeInfo, ref connectionsInfo );
- IOUtils.AddFieldValueToString( ref nodeInfo, m_selectedOutputType );
- for ( int i = 0; i < m_selectedOutputSwizzleTypes.Length; i++ )
- {
- IOUtils.AddFieldValueToString( ref nodeInfo, m_selectedOutputSwizzleTypes[ i ] );
- }
- }
- }
- }
|