summaryrefslogtreecommitdiff
path: root/Assets/ThirdParty/AmplifyShaderEditor/Plugins/Editor/Nodes/Constants/GlobalArrayNode.cs
blob: d224465f69a61c5f070b2e3b901d736a509af75e (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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// Amplify Shader Editor - Visual Shader Editing Tool
// Copyright (c) Amplify Creations, Lda <info@amplify.pt>
//
// Custom Node Global Array
// Donated by Johann van Berkel

using System;
using UnityEngine;
using UnityEditor;

namespace AmplifyShaderEditor
{
	[Serializable]
	[NodeAttributes( "Global Array", "Constants And Properties", "The node returns a value from a global array, which you can configure by entering the name of the array in the node's settings.", null, KeyCode.None, true, false, null, null, "Johann van Berkel" )]
	public sealed class GlobalArrayNode : ParentNode
	{
		private const string DefaultArrayName = "MyGlobalArray";
		private const string TypeStr = "Type";
		private const string AutoRangeCheckStr = "Range Check";
		private const string ArrayFormatStr = "{0}[{1}]";
		private const string JaggedArrayFormatStr = "{0}[{1}][{2}]";
		private const string IsJaggedStr = "Is Jagged";
		private const string AutoRegisterStr = "Auto-Register";

		private readonly string[] AvailableTypesLabel = { "Float", "Color", "Vector4", "Matrix4" };
		private readonly WirePortDataType[] AvailableTypesValues = { WirePortDataType.FLOAT, WirePortDataType.COLOR, WirePortDataType.FLOAT4, WirePortDataType.FLOAT4x4 };

		[SerializeField]
		private string m_name = DefaultArrayName;

		[SerializeField]
		private int m_indexX = 0;

		[SerializeField]
		private int m_indexY = 0;

		[SerializeField]
		private int m_arrayLengthX = 1;

		[SerializeField]
		private int m_arrayLengthY = 1;

		[SerializeField]
		private int m_type = 0;

		[SerializeField]
		private bool m_autoRangeCheck = false;

		[SerializeField]
		private bool m_isJagged = false;

		[SerializeField]
		private bool m_autoRegister = false;

		//////////////////////////////////////////////////////////////////
		private readonly Color ReferenceHeaderColor = new Color( 0.6f, 3.0f, 1.25f, 1.0f );

		[SerializeField]
		private TexReferenceType m_referenceType = TexReferenceType.Object;

		[SerializeField]
		private int m_referenceArrayId = -1;

		[SerializeField]
		private int m_referenceNodeId = -1;

		private GlobalArrayNode m_referenceNode = null;

		private bool m_updated = false;

		protected override void CommonInit( int uniqueId )
		{
			base.CommonInit( uniqueId );

			AddInputPort( WirePortDataType.INT, false, "Index", -1, MasterNodePortCategory.Fragment, 0 );
			AddInputPort( WirePortDataType.INT, false, "Index Y", -1, MasterNodePortCategory.Fragment, 2 );
			AddInputPort( WirePortDataType.INT, false, "Array Length", -1, MasterNodePortCategory.Fragment, 1 );
			AddInputPort( WirePortDataType.INT, false, "Array Length Y", -1, MasterNodePortCategory.Fragment, 3 );

			AddOutputPort( WirePortDataType.FLOAT, "Out" );

			m_textLabelWidth = 95;
			SetAdditonalTitleText( string.Format( Constants.SubTitleValueFormatStr, m_name ) );
			UpdatePorts();
		}

		protected override void OnUniqueIDAssigned()
		{
			base.OnUniqueIDAssigned();
			UIUtils.CurrentWindow.OutsideGraph.GlobalArrayNodes.AddNode( this );
		}

		public override void Destroy()
		{
			base.Destroy();
			UIUtils.CurrentWindow.OutsideGraph.GlobalArrayNodes.RemoveNode( this );
		}

		void UpdatePorts()
		{
			InputPort indexXPort = GetInputPortByUniqueId( 0 );
			InputPort arrayLengthPortX = GetInputPortByUniqueId( 1 );
			InputPort indexYPort = GetInputPortByUniqueId( 2 );
			InputPort arrayLengthPortY = GetInputPortByUniqueId( 3 );
			if( m_referenceType == TexReferenceType.Object )
			{
				m_headerColorModifier =  Color.white;
				SetAdditonalTitleText( string.Format( Constants.SubTitleValueFormatStr, m_name ) );
				arrayLengthPortX.Visible = true;
				if( m_isJagged )
				{
					indexXPort.Name = "Index X";
					arrayLengthPortX.Name = "Array Length X";
					indexYPort.Visible = true;
					arrayLengthPortY.Visible = true;
				}
				else
				{
					indexXPort.Name = "Index";
					arrayLengthPortX.Name = "Array Length";
					indexYPort.Visible = false;
					arrayLengthPortY.Visible = false;
				}
			}
			else if( m_referenceNodeId > -1 )
			{
				m_headerColorModifier = ReferenceHeaderColor;
				if( m_referenceNode == null )
					m_referenceNode = UIUtils.GetNode( m_referenceNodeId ) as GlobalArrayNode;

				if( m_referenceNode != null )
				{
					SetAdditonalTitleText( string.Format( Constants.SubTitleValueFormatStr, m_referenceNode.DataToArray ) );
					arrayLengthPortX.Visible = false;
					arrayLengthPortY.Visible = false;
					if( m_referenceNode.IsJagged )
					{
						indexXPort.Name = "Index X";
						indexYPort.Visible = true;
					}
					else
					{
						indexXPort.Name = "Index";
						indexYPort.Visible = false;
					}
				}
			}
			m_sizeIsDirty = true;
		}

		void DrawObjectProperties()
		{
			EditorGUI.BeginChangeCheck();
			m_name = EditorGUILayoutStringField( "Name", m_name );
			if( EditorGUI.EndChangeCheck() )
			{
				m_updated = true;
				m_name = UIUtils.RemoveInvalidCharacters( m_name );
				if( string.IsNullOrEmpty( m_name ) )
					m_name = DefaultArrayName;
				UIUtils.UpdateGlobalArrayDataNode( UniqueId, m_name );
				SetAdditonalTitleText( string.Format( Constants.SubTitleValueFormatStr, m_name ) );
			}


			m_autoRegister = EditorGUILayoutToggle( AutoRegisterStr, m_autoRegister );

			EditorGUI.BeginChangeCheck();
			m_isJagged = EditorGUILayoutToggle( IsJaggedStr, m_isJagged );
			if( EditorGUI.EndChangeCheck() )
			{
				m_updated = true;
				UpdatePorts();
			}

			InputPort indexXPort = GetInputPortByUniqueId( 0 );
			if( !indexXPort.IsConnected )
			{
				EditorGUI.BeginChangeCheck();
				m_indexX = EditorGUILayoutIntField( indexXPort.Name, m_indexX );
				if( EditorGUI.EndChangeCheck() )
				{
					m_indexX = Mathf.Clamp( m_indexX, 0, ( m_arrayLengthX - 1 ) );
				}
			}

			if( m_isJagged )
			{
				InputPort indexYPort = GetInputPortByUniqueId( 2 );
				if( !indexYPort.IsConnected )
				{
					EditorGUI.BeginChangeCheck();
					m_indexY = EditorGUILayoutIntField( indexYPort.Name, m_indexY );
					if( EditorGUI.EndChangeCheck() )
					{
						m_indexY = Mathf.Clamp( m_indexY, 0, ( m_arrayLengthY - 1 ) );
					}
				}
			}

			InputPort arrayLengthXPort = GetInputPortByUniqueId( 1 );
			if( !arrayLengthXPort.IsConnected )
			{
				EditorGUI.BeginChangeCheck();
				m_arrayLengthX = EditorGUILayoutIntField( arrayLengthXPort.Name, m_arrayLengthX );
				if( EditorGUI.EndChangeCheck() )
				{
					m_arrayLengthX = Mathf.Max( 1, m_arrayLengthX );
				}
			}

			if( m_isJagged )
			{
				InputPort arrayLengthYPort = GetInputPortByUniqueId( 3 );
				if( !arrayLengthYPort.IsConnected )
				{
					EditorGUI.BeginChangeCheck();
					m_arrayLengthY = EditorGUILayoutIntField( arrayLengthYPort.Name, m_arrayLengthY );
					if( EditorGUI.EndChangeCheck() )
					{
						m_arrayLengthY = Mathf.Max( 1, m_arrayLengthY );
					}
				}
			}

			EditorGUI.BeginChangeCheck();
			m_type = EditorGUILayoutPopup( TypeStr, m_type, AvailableTypesLabel );
			if( EditorGUI.EndChangeCheck() )
			{
				m_outputPorts[ 0 ].ChangeType( (WirePortDataType)AvailableTypesValues[ m_type ], false );
			}

			m_autoRangeCheck = EditorGUILayoutToggle( AutoRangeCheckStr, m_autoRangeCheck );
		}

		public override void OnNodeLayout( DrawInfo drawInfo )
		{
			base.OnNodeLayout( drawInfo );
			m_updated = false;
			if( m_referenceType == TexReferenceType.Instance )
			{
				if( m_referenceNodeId > -1 && m_referenceNode == null )
				{
					m_referenceNode = UIUtils.GetNode( m_referenceNodeId ) as GlobalArrayNode;
					if( m_referenceNode == null )
					{
						m_referenceNodeId = -1;
					}
				}
				if( m_referenceNode != null && m_referenceNode.Updated)
				{
					UpdatePorts();
				}
			}
		}

		void DrawInstancedProperties()
		{
			string[] arr = UIUtils.GlobalArrayNodeArr();
			bool guiEnabledBuffer = GUI.enabled;
			if( arr != null && arr.Length > 0 )
			{
				GUI.enabled = true;
			}
			else
			{
				m_referenceArrayId = -1;
				m_referenceNodeId = -1;
				m_referenceNode = null;
				GUI.enabled = false;
			}
			EditorGUI.BeginChangeCheck();
			m_referenceArrayId = EditorGUILayoutPopup( Constants.AvailableReferenceStr, m_referenceArrayId, arr );
			if( EditorGUI.EndChangeCheck() )
			{
				m_referenceNode = UIUtils.GetGlobalArrayNode( m_referenceArrayId );
				if( m_referenceNode != null )
				{
					m_referenceNodeId = m_referenceNode.UniqueId;
				}
				UpdatePorts();
			}

			GUI.enabled = guiEnabledBuffer;

			InputPort indexXPort = GetInputPortByUniqueId( 0 );
			if( !indexXPort.IsConnected )
			{
				EditorGUI.BeginChangeCheck();
				m_indexX = EditorGUILayoutIntField( indexXPort.Name, m_indexX );
				if( EditorGUI.EndChangeCheck() )
				{
					m_indexX = Mathf.Clamp( m_indexX, 0, ( m_arrayLengthX - 1 ) );
				}
			}

			if( m_isJagged )
			{
				InputPort indexYPort = GetInputPortByUniqueId( 2 );
				if( !indexYPort.IsConnected )
				{
					EditorGUI.BeginChangeCheck();
					m_indexY = EditorGUILayoutIntField( indexYPort.Name, m_indexY );
					if( EditorGUI.EndChangeCheck() )
					{
						m_indexY = Mathf.Clamp( m_indexY, 0, ( m_arrayLengthY - 1 ) );
					}
				}
			}
		}

		public override void DrawProperties()
		{
			EditorGUI.BeginChangeCheck();
			m_referenceType = (TexReferenceType)EditorGUILayoutPopup( Constants.ReferenceTypeStr, (int)m_referenceType, Constants.ReferenceArrayLabels );
			if( EditorGUI.EndChangeCheck() )
			{
				UpdatePorts();
			}

			if( m_referenceType == TexReferenceType.Object )
				DrawObjectProperties();
			else
				DrawInstancedProperties();
		}

		public string GetArrayValue( string indexX, string indexY = null )
		{
			if( m_isJagged )
				return string.Format( JaggedArrayFormatStr, m_name, indexX, indexY );

			return string.Format( ArrayFormatStr, m_name, indexX );
		}

		public string GenerateInstancedShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
		{
			string result = string.Empty;
			if( m_referenceNode != null )
			{
				InputPort indexXPort = GetInputPortByUniqueId( 0 );
				if( m_referenceNode.IsJagged )
				{
					InputPort indexYPort = GetInputPortByUniqueId( 2 );
					string arrayIndexX = indexXPort.IsConnected ? indexXPort.GeneratePortInstructions( ref dataCollector ) : m_indexX.ToString();
					string arrayIndexY = indexYPort.IsConnected ? indexYPort.GeneratePortInstructions( ref dataCollector ) : m_indexY.ToString();
					result = m_referenceNode.GetArrayValue( arrayIndexX, arrayIndexY );
				}
				else
				{
					string arrayIndexX = indexXPort.IsConnected ? indexXPort.GeneratePortInstructions( ref dataCollector ) : m_indexX.ToString();
					result = m_referenceNode.GetArrayValue( arrayIndexX );
				}
			}
			m_outputPorts[ 0 ].SetLocalValue( result, dataCollector.PortCategory );
			return result;
		}

		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 );

			if( m_referenceType == TexReferenceType.Instance )
				return GenerateInstancedShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );

			string dataType = UIUtils.PrecisionWirePortToCgType( CurrentPrecisionType, AvailableTypesValues[ m_type ] );

			InputPort indexXPort = GetInputPortByUniqueId( 0 );
			InputPort arrayLengthXPort = GetInputPortByUniqueId( 1 );
			string result = string.Empty;

			if( m_isJagged )
			{
				InputPort indexYPort = GetInputPortByUniqueId( 2 );
				InputPort arrayLengthYPort = GetInputPortByUniqueId( 3 );

				string arrayIndexX = indexXPort.IsConnected ? indexXPort.GeneratePortInstructions( ref dataCollector ) : m_indexX.ToString();
				string arrayLengthX = arrayLengthXPort.IsConnected ? arrayLengthXPort.GeneratePortInstructions( ref dataCollector ) : m_arrayLengthX.ToString();

				string arrayIndexY = indexYPort.IsConnected ? indexYPort.GeneratePortInstructions( ref dataCollector ) : m_indexY.ToString();
				string arrayLengthY = arrayLengthYPort.IsConnected ? arrayLengthYPort.GeneratePortInstructions( ref dataCollector ) : m_arrayLengthY.ToString();

				dataCollector.AddToUniforms( UniqueId, dataType, string.Format( JaggedArrayFormatStr, m_name, arrayLengthX, arrayLengthY ) );
				if( m_autoRangeCheck )
				{
					arrayIndexX = string.Format( "clamp({0},0,({1} - 1))", arrayIndexX, arrayLengthX );
					arrayIndexY = string.Format( "clamp({0},0,({1} - 1))", arrayIndexY, arrayLengthY );
				}
				result = string.Format( JaggedArrayFormatStr, m_name, arrayIndexX, arrayIndexY );
			}
			else
			{

				string arrayIndex = indexXPort.IsConnected ? indexXPort.GeneratePortInstructions( ref dataCollector ) : m_indexX.ToString();
				string arrayLength = arrayLengthXPort.IsConnected ? arrayLengthXPort.GeneratePortInstructions( ref dataCollector ) : m_arrayLengthX.ToString();


				dataCollector.AddToUniforms( UniqueId, dataType, string.Format( ArrayFormatStr, m_name, arrayLength ) );

				if( m_autoRangeCheck )
					arrayIndex = string.Format( "clamp({0},0,({1} - 1))", arrayIndex, arrayLength );

				result = string.Format( ArrayFormatStr, m_name, arrayIndex );
			}

			m_outputPorts[ 0 ].SetLocalValue( result, dataCollector.PortCategory );
			return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
		}

		public void CheckIfAutoRegister( ref MasterNodeDataCollector dataCollector )
		{
			if( m_referenceType == TexReferenceType.Object && m_autoRegister && m_connStatus != NodeConnectionStatus.Connected )
			{
				string dataType = UIUtils.PrecisionWirePortToCgType( CurrentPrecisionType, AvailableTypesValues[ m_type ] );
				if( m_isJagged )
				{
					dataCollector.AddToUniforms( UniqueId, dataType, string.Format( JaggedArrayFormatStr, m_name, m_arrayLengthX, m_arrayLengthY ) );
				}
				else
				{
					dataCollector.AddToUniforms( UniqueId, dataType, string.Format( ArrayFormatStr, m_name, m_arrayLengthX ) );
				}
			}
		}

		public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
		{
			base.WriteToString( ref nodeInfo, ref connectionsInfo );
			IOUtils.AddFieldValueToString( ref nodeInfo, m_name );
			IOUtils.AddFieldValueToString( ref nodeInfo, m_indexX );
			IOUtils.AddFieldValueToString( ref nodeInfo, m_arrayLengthX );
			IOUtils.AddFieldValueToString( ref nodeInfo, m_type );
			IOUtils.AddFieldValueToString( ref nodeInfo, m_autoRangeCheck );
			IOUtils.AddFieldValueToString( ref nodeInfo, m_isJagged );
			IOUtils.AddFieldValueToString( ref nodeInfo, m_indexY );
			IOUtils.AddFieldValueToString( ref nodeInfo, m_arrayLengthY );
			IOUtils.AddFieldValueToString( ref nodeInfo, m_autoRegister );
			IOUtils.AddFieldValueToString( ref nodeInfo, m_referenceType );
			IOUtils.AddFieldValueToString( ref nodeInfo, m_referenceNodeId );
		}

		public override void ReadFromString( ref string[] nodeParams )
		{
			base.ReadFromString( ref nodeParams );
			m_name = GetCurrentParam( ref nodeParams );
			m_indexX = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
			m_arrayLengthX = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
			m_type = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
			m_autoRangeCheck = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
			if( UIUtils.CurrentShaderVersion() > 15801 )
			{
				m_isJagged = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
				m_indexY = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
				m_arrayLengthY = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
				m_autoRegister = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
				m_referenceType = (TexReferenceType)Enum.Parse( typeof( TexReferenceType ), GetCurrentParam( ref nodeParams ) );
				m_referenceNodeId = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
			}
			SetAdditonalTitleText( string.Format( Constants.SubTitleValueFormatStr, m_name ) );
			UpdatePorts();
		}

		public override void RefreshExternalReferences()
		{
			base.RefreshExternalReferences();
			if( m_referenceType == TexReferenceType.Instance && m_referenceNodeId > -1 )
			{
				m_referenceNode = UIUtils.GetNode( m_referenceNodeId ) as GlobalArrayNode;
				if( m_referenceNode != null )
				{
					m_referenceArrayId = UIUtils.GetGlobalArrayNodeRegisterId( m_referenceNodeId );
					UpdatePorts();
				}
				else
				{
					m_referenceNodeId = -1;
				}
			}
		}

		public bool AutoRegister { get { return m_autoRegister; } }
		public bool IsJagged { get { return m_isJagged; } }
		public bool Updated { get { return m_updated; } }
		public override string DataToArray { get { return m_name; } }
	}
}