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
|
// 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 TemplateInterpElement
{
public TemplateSemantics Semantic;
public bool[] AvailableChannels = { true, true, true, true };
public bool IsFull = false;
public int Usage = 0;
public string Name;
public TemplateInterpElement( TemplateInterpElement other )
{
Semantic = other.Semantic;
for ( int i = 0; i < AvailableChannels.Length; i++ )
{
AvailableChannels[ i ] = other.AvailableChannels[ i ];
}
IsFull = other.IsFull;
Usage = other.Usage;
Name = other.Name;
}
public TemplateInterpElement( TemplateSemantics semantic )
{
Semantic = semantic;
int semanticId = TemplateHelperFunctions.SemanticToInt[ Semantic ];
Name = ( semanticId == 0 ) ? TemplateHelperFunctions.BaseInterpolatorName : TemplateHelperFunctions.BaseInterpolatorName + semanticId.ToString();
}
public void SetAvailableChannelsFromString( string channels )
{
for ( int i = 0; i < AvailableChannels.Length; i++ )
{
AvailableChannels[ i ] = false;
}
Usage = AvailableChannels.Length;
for ( int i = 0; i < channels.Length; i++ )
{
switch ( channels[ i ] )
{
case 'x': if ( !AvailableChannels[ 0 ] ) { AvailableChannels[ 0 ] = true; Usage--; } break;
case 'y': if ( !AvailableChannels[ 1 ] ) { AvailableChannels[ 1 ] = true; Usage--; } break;
case 'z': if ( !AvailableChannels[ 2 ] ) { AvailableChannels[ 2 ] = true; Usage--; } break;
case 'w': if ( !AvailableChannels[ 3 ] ) { AvailableChannels[ 3 ] = true; Usage--; } break;
}
}
}
public TemplateVertexData RequestChannels( WirePortDataType type, bool isColor, string customName = null )
{
if ( IsFull )
return null;
int channelsRequired = TemplateHelperFunctions.DataTypeChannelUsage[ type ];
if ( channelsRequired == 0 )
return null;
int firstChannel = -1;
for ( int i = 0; i < AvailableChannels.Length; i++ )
{
if ( AvailableChannels[ i ] )
{
if ( firstChannel < 0 )
{
firstChannel = i;
}
channelsRequired -= 1;
if ( channelsRequired == 0 )
break;
}
}
//did not found enough channels to fill request
if ( channelsRequired > 0 )
return null;
if( Usage == 0 && customName != null )
{
Name = customName;
}
Usage += 1;
TemplateVertexData data = null;
if ( type == WirePortDataType.COLOR || type == WirePortDataType.FLOAT4 )
{
// Automatically lock all channels
for ( int i = firstChannel; i < ( firstChannel + channelsRequired ); i++ )
{
AvailableChannels[ i ] = false;
}
IsFull = true;
data = new TemplateVertexData( Semantic, type, Name );
}
else
{
string[] swizzleArray = ( isColor ) ? TemplateHelperFunctions.ColorSwizzle : TemplateHelperFunctions.VectorSwizzle;
string channels = ".";
int count = firstChannel + TemplateHelperFunctions.DataTypeChannelUsage[ type ];
for ( int i = firstChannel; i < count; i++ )
{
AvailableChannels[ i ] = false;
channels += swizzleArray[ i ];
if ( i == ( AvailableChannels.Length - 1 ) )
{
IsFull = true;
}
}
data = new TemplateVertexData( Semantic, type, Name, channels );
}
return data;
}
}
[Serializable]
public class TemplateInterpData
{
[SerializeField]
private string m_interpDataId = string.Empty;
[SerializeField]
private int m_interpDataStartIdx = -1;
[SerializeField]
private bool m_dynamicMax = false;
public List<TemplateInterpElement> AvailableInterpolators = new List<TemplateInterpElement>();
public List<TemplateVertexData> Interpolators = new List<TemplateVertexData>();
public List<TemplateVertexData> RawInterpolators = new List<TemplateVertexData>();
public TemplateInterpData() { }
public bool HasRawInterpolatorOfName( string name )
{
return RawInterpolators.Exists( ( x ) => x.VarName.Equals( name ));
}
public TemplateInterpData( TemplateInterpData other )
{
m_dynamicMax = other.DynamicMax;
foreach ( TemplateInterpElement data in other.AvailableInterpolators )
{
AvailableInterpolators.Add( new TemplateInterpElement( data ) );
}
for ( int i = 0; i < other.Interpolators.Count; i++ )
{
Interpolators.Add( new TemplateVertexData( other.Interpolators[ i ] ) );
}
for( int i = 0; i < other.RawInterpolators.Count; i++ )
{
RawInterpolators.Add( new TemplateVertexData( other.RawInterpolators[ i ] ) );
}
}
public void RecalculateAvailableInterpolators( int newMax )
{
if( m_dynamicMax )
{
if( !TemplateHelperFunctions.IntToSemantic.ContainsKey( ( newMax - 1 ) ) )
{
Debug.LogWarning( "Attempting to add inexisting available interpolators" );
return;
}
if( AvailableInterpolators.Count > 0 )
{
int currMax = 1 + TemplateHelperFunctions.SemanticToInt[ AvailableInterpolators[ AvailableInterpolators.Count - 1 ].Semantic ];
if( newMax > currMax )
{
int count = newMax - currMax;
for( int i = 0; i < count; i++ )
{
AvailableInterpolators.Add( new TemplateInterpElement( TemplateHelperFunctions.IntToSemantic[ currMax + i ] ));
}
}
else if( newMax < currMax )
{
int min = TemplateHelperFunctions.SemanticToInt[ AvailableInterpolators[ 0 ].Semantic ];
if( newMax > min )
{
int count = currMax - newMax;
for( int i = 0; i < count; i++ )
{
AvailableInterpolators.RemoveAt( AvailableInterpolators.Count - 1 );
}
}
}
}
}
}
public void ReplaceNameOnInterpolator( TemplateSemantics semantic, string newName )
{
for ( int i = 0; i < AvailableInterpolators.Count; i++ )
{
if ( AvailableInterpolators[ i ].Semantic == semantic )
{
AvailableInterpolators[ i ].Name = newName;
break;
}
}
}
public void Destroy()
{
AvailableInterpolators.Clear();
AvailableInterpolators = null;
Interpolators.Clear();
Interpolators = null;
RawInterpolators.Clear();
RawInterpolators = null;
}
public string InterpDataId { get { return m_interpDataId; } set { m_interpDataId = value; } }
public int InterpDataStartIdx { get { return m_interpDataStartIdx; } set { m_interpDataStartIdx = value; } }
public bool DynamicMax { get { return m_dynamicMax; } set { m_dynamicMax = value; } }
}
}
|