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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
using System;
using UnityEngine;
using UnityEditor;
namespace AmplifyShaderEditor
{
public enum eVectorFromMatrixMode
{
Row,
Column
}
[Serializable]
[NodeAttributes( "Vector From Matrix", "Matrix Operators", "Retrieve vector data from a matrix" )]
public sealed class VectorFromMatrixNode : ParentNode
{
private const string IndexStr = "Index";
private const string ModeStr = "Mode";
[SerializeField]
private eVectorFromMatrixMode m_mode = eVectorFromMatrixMode.Row;
[SerializeField]
private int m_index = 0;
[SerializeField]
private int m_maxIndex = 3;
protected override void CommonInit( int uniqueId )
{
base.CommonInit( uniqueId );
AddInputPort( WirePortDataType.FLOAT4x4, false, Constants.EmptyPortValue );
m_inputPorts[ 0 ].CreatePortRestrictions( WirePortDataType.FLOAT3x3, WirePortDataType.FLOAT4x4 );
AddOutputVectorPorts( WirePortDataType.FLOAT4, "XYZW" );
m_useInternalPortData = true;
m_autoWrapProperties = true;
}
public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
{
base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
UpdatePorts();
}
public override void OnConnectedOutputNodeChanges( int inputPortId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
{
base.OnConnectedOutputNodeChanges( inputPortId, otherNodeId, otherPortId, name, type );
UpdatePorts();
}
void UpdatePorts()
{
m_inputPorts[ 0 ].MatchPortToConnection();
if ( m_inputPorts[ 0 ].DataType == WirePortDataType.FLOAT3x3 )
{
m_outputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT3, false );
m_outputPorts[ 0 ].Name = "XYZ";
m_maxIndex = 2;
m_outputPorts[ 4 ].Visible = false;
}
else
{
m_outputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT4, false );
m_outputPorts[ 0 ].Name = "XYZW";
m_maxIndex = 3;
m_outputPorts[ 4 ].Visible = true;
}
m_sizeIsDirty = true;
}
public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
{
string value = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
if ( m_inputPorts[ 0 ].DataType != WirePortDataType.FLOAT4x4 &&
m_inputPorts[ 0 ].DataType != WirePortDataType.FLOAT3x3 )
{
value = UIUtils.CastPortType( ref dataCollector, CurrentPrecisionType, new NodeCastInfo( UniqueId, outputId ), value, m_inputPorts[ 0 ].DataType, WirePortDataType.FLOAT4x4, value );
}
if ( m_mode == eVectorFromMatrixMode.Row )
{
value += "[" + m_index + "]";
}
else
{
string formatStr = value + "[{0}]" + "[" + m_index + "]";
int count = 4;
if ( m_inputPorts[ 0 ].DataType != WirePortDataType.FLOAT3x3 )
{
value = "float4( ";
}
else
{
count = 3;
value = "float3( ";
}
for ( int i = 0; i < count; i++ )
{
value += string.Format( formatStr, i );
if ( i != ( count - 1 ) )
{
value += ",";
}
}
value += " )";
}
return GetOutputVectorItem( 0, outputId, value );
}
public override void DrawProperties()
{
m_mode = (eVectorFromMatrixMode)EditorGUILayoutEnumPopup( ModeStr, m_mode );
m_index = EditorGUILayoutIntSlider( IndexStr, m_index, 0, m_maxIndex );
base.DrawProperties();
}
public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
{
base.WriteToString( ref nodeInfo, ref connectionsInfo );
IOUtils.AddFieldValueToString( ref nodeInfo, m_mode );
IOUtils.AddFieldValueToString( ref nodeInfo, m_index );
}
public override void ReadFromString( ref string[] nodeParams )
{
base.ReadFromString( ref nodeParams );
m_mode = ( eVectorFromMatrixMode ) Enum.Parse( typeof( eVectorFromMatrixMode ), GetCurrentParam( ref nodeParams ) );
m_index = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
}
}
}
|