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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
// 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();
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" );
m_content.text = "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;
m_content.text = m_dataName;
m_sizeIsDirty = true;
CheckWarningState();
}
}
public override void DrawProperties()
{
base.DrawProperties();
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" );
return m_outputPorts[ 0 ].ErrorValue;
}
if( !dataCollector.IsFragmentCategory )
{
UIUtils.ShowMessage( UniqueId, "Template Fragment Data node node is only intended for fragment use use only" );
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;
}
}
}
|