summaryrefslogtreecommitdiff
path: root/Assets/AmplifyShaderEditor/Plugins/Editor/Nodes/Operators/LerpOp.cs
blob: 1ec7199d46d850cedfe4600c6825963a0d6a788b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Amplify Shader Editor - Visual Shader Editing Tool
// Copyright (c) Amplify Creations, Lda <info@amplify.pt>

using UnityEngine;
using System;

namespace AmplifyShaderEditor
{
	[Serializable]
	[NodeAttributes( "Lerp", "Math Operators", "Linear interpolation of two scalars or vectors based on a weight", null, KeyCode.L )]
	public sealed class LerpOp : ParentNode
	{
		private const string LertOpFormat = "lerp( {0} , {1} , {2})";

		[UnityEngine.SerializeField]
		private WirePortDataType m_mainDataType = WirePortDataType.FLOAT;

		protected override void CommonInit( int uniqueId )
		{
			base.CommonInit( uniqueId );
			m_textLabelWidth = 55;
			AddInputPort( WirePortDataType.FLOAT, false, "A" );
			AddInputPort( WirePortDataType.FLOAT, false, "B" );
			AddInputPort( WirePortDataType.FLOAT, false, "Alpha" );
			AddOutputPort( WirePortDataType.FLOAT,Constants.EmptyPortValue);
			m_useInternalPortData = true;
			m_previewShaderGUID = "34d9c4cdcf1fadb49af2de3f90bbc57d";
		}

		public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
		{
			base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
			UpdateConnection( portId );
		}

		public override void OnConnectedOutputNodeChanges( int inputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
		{
			base.OnConnectedOutputNodeChanges( inputPortId, otherNodeId, otherPortId, name, type );
			UpdateConnection( inputPortId );
		}

		public override void OnInputPortDisconnected( int portId )
		{
			base.OnInputPortDisconnected( portId );
			UpdateConnection( portId );
		}
		
		void UpdateConnection( int portId )
		{
			WirePortDataType type1 = WirePortDataType.FLOAT;
			if( m_inputPorts[ 0 ].IsConnected )
				type1 = m_inputPorts[ 0 ].GetOutputConnection( 0 ).DataType;

			WirePortDataType type2 = WirePortDataType.FLOAT;
			if( m_inputPorts[ 1 ].IsConnected )
				type2 = m_inputPorts[ 1 ].GetOutputConnection( 0 ).DataType;

			WirePortDataType typealpha = WirePortDataType.FLOAT;
			if( m_inputPorts[ 2 ].IsConnected )
				typealpha = m_inputPorts[ 2 ].GetOutputConnection( 0 ).DataType;

			m_mainDataType = UIUtils.GetPriority( type1 ) > UIUtils.GetPriority( type2 ) ? type1 : type2;

			if( !m_inputPorts[ 0 ].IsConnected && !m_inputPorts[ 1 ].IsConnected && m_inputPorts[ 2 ].IsConnected )
				m_mainDataType = m_inputPorts[ 2 ].GetOutputConnection( 0 ).DataType;

			m_inputPorts[ 0 ].ChangeType( m_mainDataType, false );

			m_inputPorts[ 1 ].ChangeType( m_mainDataType, false );
			if( m_inputPorts[ 2 ].IsConnected && ( typealpha == WirePortDataType.FLOAT || typealpha == WirePortDataType.INT ) )
				m_inputPorts[ 2 ].ChangeType( WirePortDataType.FLOAT, false );
			else
				m_inputPorts[ 2 ].ChangeType( m_mainDataType, false );

			m_outputPorts[ 0 ].ChangeType( m_mainDataType, false );
		}

		//void UpdateDisconnection( int portId )
		//{
		//	int otherPortId = ( portId + 1 ) % 2;
		//	if ( m_inputPorts[ otherPortId ].IsConnected )
		//	{
		//		m_mainDataType = m_inputPorts[ otherPortId ].DataType;
		//		m_inputPorts[ portId ].ChangeType( m_mainDataType, false );
		//		m_outputPorts[ 0 ].ChangeType( m_mainDataType, false );
		//	}
		//}

		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 aValue = m_inputPorts[ 0 ].GenerateShaderForOutput( ref dataCollector, m_mainDataType, ignoreLocalVar, true );
			string bValue = m_inputPorts[ 1 ].GenerateShaderForOutput( ref dataCollector, m_mainDataType, ignoreLocalVar, true );
			string interp = string.Empty;
			if( m_inputPorts[ 2 ].DataType == WirePortDataType.FLOAT )
				interp = m_inputPorts[ 2 ].GeneratePortInstructions( ref dataCollector );
			else
				interp = m_inputPorts[ 2 ].GenerateShaderForOutput( ref dataCollector, m_mainDataType, ignoreLocalVar,true );
			string result = string.Format( LertOpFormat, aValue,bValue,interp);

			RegisterLocalVariable( 0, result, ref dataCollector, "lerpResult"+OutputId );
			
			return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
		}

		//public override void RefreshExternalReferences()
		//{
		//	if ( m_inputPorts[ 2 ].DataType != WirePortDataType.FLOAT )
		//	{
		//		m_inputPorts[ 2 ].ChangeType( WirePortDataType.FLOAT, false );
		//	}
		//}
	}
}