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
|
// Amplify Shader Editor - Visual Shader Editing Tool
// Copyright (c) Amplify Creations, Lda <info@amplify.pt>
using System;
using System.Collections.Generic;
using UnityEngine;
namespace AmplifyShaderEditor
{
[Serializable]
public class CustomNodeOutputData
{
public string expression;
public string name;
public List<CustomNodeInputData> inputData;
public CustomNodeOutputData( string newName, string newExpression )
{
name = newName;
expression = newExpression;
inputData = new List<CustomNodeInputData>();
}
public void Destroy()
{
inputData.Clear();
inputData = null;
}
public override string ToString()
{
string result = "name: " + name + " outputExpression: " + expression + '\n';
for ( int i = 0; i < inputData.Count; i++ )
{
result += inputData[ i ].ToString() + '\n';
}
return result;
}
}
[Serializable]
public class CustomNodeInputData
{
public int index;
public int length;
public string name;
public CustomNodeInputData( int newIndex, int newLength, string newName )
{
index = newIndex;
length = newLength;
name = newName;
}
public override string ToString()
{
return "index: " + index + " length: " + length + " name: " + name;
}
}
[Serializable]
public class CustomNode : ParentNode
{
[SerializeField]
private List<string> m_includes;
[SerializeField]
private List<CustomNodeOutputData> m_outputData;
protected override void CommonInit( int uniqueId )
{
base.CommonInit( uniqueId );
m_outputData = new List<CustomNodeOutputData>();
}
public void AddIncludes( string newInclude )
{
m_includes.Add( newInclude );
}
protected void AddOutputsFromString( string outputName, string output )
{
AddOutputPort( WirePortDataType.OBJECT, outputName );
CustomNodeOutputData currOutputData = new CustomNodeOutputData( outputName, output );
// Get existing input nodes so we can test for duplicates
Dictionary<string, InputPort> existingPorts = InputPortsDict;
// Create dictionary to prevent duplicates when dealing with expresssions with multiple occurences of an input
Dictionary<string, string> inputDuplicatePrevention = new Dictionary<string, string>();
// Get all inputs on the expression and save their info
int[] indexes = output.AllIndexesOf( Constants.CNIP );
for ( int i = 0; i < indexes.Length; i++ )
{
string name = output.Substring( indexes[ i ], Constants.CNIP.Length + 1 );
currOutputData.inputData.Add( new CustomNodeInputData( indexes[ i ], Constants.CNIP.Length + 1, name ) );
if ( !inputDuplicatePrevention.ContainsKey( name ) && !existingPorts.ContainsKey( name ) )
{
inputDuplicatePrevention.Add( name, name );
AddInputPort( WirePortDataType.OBJECT, false, name );
}
}
inputDuplicatePrevention.Clear();
inputDuplicatePrevention = null;
existingPorts.Clear();
existingPorts = null;
m_outputData.Add( currOutputData );
}
public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
{
base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
if ( outputId < m_outputData.Count )
{
Dictionary<string, InputPort> inputs = InputPortsDict;
string value = m_outputData[ outputId ].expression;
for ( int i = 0; i < m_outputData[ outputId ].inputData.Count; i++ )
{
if ( inputs.ContainsKey( m_outputData[ outputId ].inputData[ i ].name ) )
{
InputPort inputPort = inputs[ m_outputData[ outputId ].inputData[ i ].name ];
if ( inputPort != null )
{
string inputValue = inputPort.GenerateShaderForOutput( ref dataCollector, WirePortDataType.OBJECT, ignoreLocalvar );
value = value.Replace( m_outputData[ outputId ].inputData[ i ].name, inputValue );
}
else
{
UIUtils.ShowMessage( UniqueId, m_outputData[ outputId ].inputData[ i ].name + " invalid on the inputs list", MessageSeverity.Error );
return string.Empty;
}
}
else
{
UIUtils.ShowMessage( UniqueId, m_outputData[ outputId ].inputData[ i ].name + " Not found on the inputs list", MessageSeverity.Error );
return string.Empty;
}
}
return value;
}
return string.Empty;
}
public void DumpOutputData()
{
for ( int i = 0; i < m_outputData.Count; i++ )
{
Debug.Log( m_outputData[ i ] );
}
}
public override void Destroy()
{
base.Destroy();
if ( m_outputData != null )
{
for ( int i = 0; i < m_outputData.Count; i++ )
{
m_outputData[ i ].Destroy();
}
m_outputData.Clear();
m_outputData = null;
}
if ( m_includes != null )
{
m_includes.Clear();
m_includes = null;
}
}
}
}
|