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
|
using System;
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
namespace AmplifyShaderEditor
{
[Serializable]
public class StencilBufferOpHelper
{
public static readonly string[] StencilComparisonValues =
{
"<Default>",
"Greater" ,
"GEqual" ,
"Less" ,
"LEqual" ,
"Equal" ,
"NotEqual" ,
"Always" ,
"Never"
};
public static readonly Dictionary<string,int> StencilComparisonValuesDict = new Dictionary<string, int>()
{
{"Greater" , 1},
{"GEqual" , 2},
{"Less" , 3},
{"LEqual" , 4},
{"Equal" , 5},
{"NotEqual", 6},
{"Always" , 7},
{"Never" , 8},
};
public static readonly string[] StencilComparisonLabels =
{
"<Default>",
"Greater" ,
"Greater or Equal" ,
"Less" ,
"Less or Equal" ,
"Equal" ,
"Not Equal" ,
"Always" ,
"Never"
};
public static readonly string[] StencilOpsValues =
{
"<Default>",
"Keep",
"Zero",
"Replace",
"IncrSat",
"DecrSat",
"Invert",
"IncrWrap",
"DecrWrap"
};
public static readonly Dictionary<string,int> StencilOpsValuesDict = new Dictionary<string, int>()
{
{"Keep", 1},
{"Zero", 2},
{"Replace", 3},
{"IncrSat", 4},
{"DecrSat", 5},
{"Invert", 6},
{"IncrWrap",7},
{"DecrWrap",8},
};
public static readonly string[] StencilOpsLabels =
{
"<Default>",
"Keep",
"Zero",
"Replace",
"IncrSat",
"DecrSat",
"Invert",
"IncrWrap",
"DecrWrap"
};
private const string FoldoutLabelStr = " Stencil Buffer";
private GUIContent ReferenceValueContent = new GUIContent( "Reference", "The value to be compared against (if Comparison is anything else than always) and/or the value to be written to the buffer (if either Pass, Fail or ZFail is set to replace)" );
private GUIContent ReadMaskContent = new GUIContent( "Read Mask", "An 8 bit mask as an 0-255 integer, used when comparing the reference value with the contents of the buffer (referenceValue & readMask) comparisonFunction (stencilBufferValue & readMask)" );
private GUIContent WriteMaskContent = new GUIContent( "Write Mask", "An 8 bit mask as an 0-255 integer, used when writing to the buffer" );
private const string ComparisonStr = "Comparison";
private const string PassStr = "Pass";
private const string FailStr = "Fail";
private const string ZFailStr = "ZFail";
private const string ComparisonFrontStr = "Comp. Front";
private const string PassFrontStr = "Pass Front";
private const string FailFrontStr = "Fail Front";
private const string ZFailFrontStr = "ZFail Front";
private const string ComparisonBackStr = "Comp. Back";
private const string PassBackStr = "Pass Back";
private const string FailBackStr = "Fail Back";
private const string ZFailBackStr = "ZFail Back";
private const int ReadMaskDefaultValue = 255;
private const int WriteMaskDefaultValue = 255;
private const int ComparisonDefaultValue = 0;
private const int PassStencilOpDefaultValue = 0;
private const int FailStencilOpDefaultValue = 0;
private const int ZFailStencilOpDefaultValue = 0;
[SerializeField]
private bool m_active;
[SerializeField]
private InlineProperty m_refValue = new InlineProperty();
[SerializeField]
private InlineProperty m_readMask = new InlineProperty( ReadMaskDefaultValue );
[SerializeField]
private InlineProperty m_writeMask = new InlineProperty( WriteMaskDefaultValue );
//Comparison Function
[SerializeField]
private InlineProperty m_comparisonFunctionIdx = new InlineProperty( ComparisonDefaultValue );
[SerializeField]
private InlineProperty m_comparisonFunctionBackIdx = new InlineProperty( ComparisonDefaultValue );
//Pass Stencil Op
[SerializeField]
private InlineProperty m_passStencilOpIdx = new InlineProperty( PassStencilOpDefaultValue );
[SerializeField]
private InlineProperty m_passStencilOpBackIdx = new InlineProperty( PassStencilOpDefaultValue );
//Fail Stencil Op
[SerializeField]
private InlineProperty m_failStencilOpIdx = new InlineProperty( FailStencilOpDefaultValue );
[SerializeField]
private InlineProperty m_failStencilOpBackIdx = new InlineProperty( FailStencilOpDefaultValue );
//ZFail Stencil Op
[SerializeField]
private InlineProperty m_zFailStencilOpIdx = new InlineProperty( ZFailStencilOpDefaultValue );
[SerializeField]
private InlineProperty m_zFailStencilOpBackIdx = new InlineProperty( ZFailStencilOpDefaultValue );
public string CreateStencilOp( UndoParentNode owner )
{
string result = "\t\tStencil\n\t\t{\n";
result += string.Format( "\t\t\tRef {0}\n", m_refValue.GetValueOrProperty() );
if( m_readMask.Active || m_readMask.IntValue != ReadMaskDefaultValue )
{
result += string.Format( "\t\t\tReadMask {0}\n", m_readMask.GetValueOrProperty() );
}
if( m_writeMask.Active || m_writeMask.IntValue != WriteMaskDefaultValue )
{
result += string.Format( "\t\t\tWriteMask {0}\n", m_writeMask.GetValueOrProperty() );
}
if( ( owner as StandardSurfaceOutputNode ).CurrentCullMode == CullMode.Off )
{
if( m_comparisonFunctionIdx.IntValue != ComparisonDefaultValue || m_comparisonFunctionIdx.Active )
result += string.Format( "\t\t\tCompFront {0}\n", m_comparisonFunctionIdx.GetValueOrProperty( StencilComparisonValues[ m_comparisonFunctionIdx.IntValue ] ) );
if( m_passStencilOpIdx.IntValue != PassStencilOpDefaultValue || m_passStencilOpIdx.Active )
result += string.Format( "\t\t\tPassFront {0}\n", m_passStencilOpIdx.GetValueOrProperty( StencilOpsValues[ m_passStencilOpIdx.IntValue ] ) );
if( m_failStencilOpIdx.IntValue != FailStencilOpDefaultValue || m_failStencilOpIdx.Active )
result += string.Format( "\t\t\tFailFront {0}\n", m_failStencilOpIdx.GetValueOrProperty( StencilOpsValues[ m_failStencilOpIdx.IntValue ] ) );
if( m_zFailStencilOpIdx.IntValue != ZFailStencilOpDefaultValue || m_zFailStencilOpIdx.Active )
result += string.Format( "\t\t\tZFailFront {0}\n", m_zFailStencilOpIdx.GetValueOrProperty( StencilOpsValues[ m_zFailStencilOpIdx.IntValue ] ) );
if( m_comparisonFunctionBackIdx.IntValue != ComparisonDefaultValue || m_comparisonFunctionBackIdx.Active )
result += string.Format( "\t\t\tCompBack {0}\n", m_comparisonFunctionBackIdx.GetValueOrProperty( StencilComparisonValues[ m_comparisonFunctionBackIdx.IntValue ] ) );
if( m_passStencilOpBackIdx.IntValue != PassStencilOpDefaultValue || m_passStencilOpBackIdx.Active )
result += string.Format( "\t\t\tPassBack {0}\n", m_passStencilOpBackIdx.GetValueOrProperty( StencilOpsValues[ m_passStencilOpBackIdx.IntValue ] ) );
if( m_failStencilOpBackIdx.IntValue != FailStencilOpDefaultValue || m_failStencilOpBackIdx.Active )
result += string.Format( "\t\t\tFailBack {0}\n", m_failStencilOpBackIdx.GetValueOrProperty( StencilOpsValues[ m_failStencilOpBackIdx.IntValue ] ) );
if( m_zFailStencilOpBackIdx.IntValue != ZFailStencilOpDefaultValue || m_zFailStencilOpBackIdx.Active )
result += string.Format( "\t\t\tZFailBack {0}\n", m_zFailStencilOpBackIdx.GetValueOrProperty( StencilOpsValues[ m_zFailStencilOpBackIdx.IntValue ] ) );
}
else
{
if( m_comparisonFunctionIdx.IntValue != ComparisonDefaultValue || m_comparisonFunctionIdx.Active )
result += string.Format( "\t\t\tComp {0}\n", m_comparisonFunctionIdx.GetValueOrProperty( StencilComparisonValues[ m_comparisonFunctionIdx.IntValue ] ) );
if( m_passStencilOpIdx.IntValue != PassStencilOpDefaultValue || m_passStencilOpIdx.Active )
result += string.Format( "\t\t\tPass {0}\n", m_passStencilOpIdx.GetValueOrProperty( StencilOpsValues[ m_passStencilOpIdx.IntValue ] ) );
if( m_failStencilOpIdx.IntValue != FailStencilOpDefaultValue || m_failStencilOpIdx.Active )
result += string.Format( "\t\t\tFail {0}\n", m_failStencilOpIdx.GetValueOrProperty( StencilOpsValues[ m_failStencilOpIdx.IntValue ] ) );
if( m_zFailStencilOpIdx.IntValue != ZFailStencilOpDefaultValue || m_zFailStencilOpIdx.Active )
result += string.Format( "\t\t\tZFail {0}\n", m_zFailStencilOpIdx.GetValueOrProperty( StencilOpsValues[ m_zFailStencilOpIdx.IntValue ] ) );
}
result += "\t\t}\n";
return result;
}
public void Draw( UndoParentNode owner )
{
bool foldoutValue = owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedStencilOptions;
NodeUtils.DrawPropertyGroup( owner, ref foldoutValue, ref m_active, FoldoutLabelStr, () =>
{
float cache = EditorGUIUtility.labelWidth;
float cache2 = EditorGUIUtility.fieldWidth;
EditorGUIUtility.labelWidth = 110;
EditorGUIUtility.fieldWidth = 30;
m_refValue.IntSlider( ref owner, ReferenceValueContent, 0, 255 );
m_readMask.IntSlider( ref owner, ReadMaskContent, 0, 255 );
m_writeMask.IntSlider( ref owner, WriteMaskContent, 0, 255 );
//EditorGUIUtility.labelWidth = cache;
EditorGUIUtility.fieldWidth = cache2;
if( ( owner as StandardSurfaceOutputNode ).CurrentCullMode == CullMode.Off )
{
m_comparisonFunctionIdx.EnumTypePopup( ref owner, ComparisonFrontStr, StencilComparisonLabels );
m_passStencilOpIdx.EnumTypePopup( ref owner, PassFrontStr, StencilOpsLabels );
m_failStencilOpIdx.EnumTypePopup( ref owner, FailFrontStr, StencilOpsLabels );
m_zFailStencilOpIdx.EnumTypePopup( ref owner, ZFailFrontStr, StencilOpsLabels );
EditorGUILayout.Separator();
m_comparisonFunctionBackIdx.EnumTypePopup( ref owner, ComparisonBackStr, StencilComparisonLabels );
m_passStencilOpBackIdx.EnumTypePopup( ref owner, PassBackStr, StencilOpsLabels );
m_failStencilOpBackIdx.EnumTypePopup( ref owner, FailBackStr, StencilOpsLabels );
m_zFailStencilOpBackIdx.EnumTypePopup( ref owner, ZFailBackStr, StencilOpsLabels );
}
else
{
m_comparisonFunctionIdx.EnumTypePopup( ref owner, ComparisonStr, StencilComparisonLabels );
m_passStencilOpIdx.EnumTypePopup( ref owner, PassFrontStr, StencilOpsLabels );
m_failStencilOpIdx.EnumTypePopup( ref owner, FailFrontStr, StencilOpsLabels );
m_zFailStencilOpIdx.EnumTypePopup( ref owner, ZFailFrontStr, StencilOpsLabels );
}
EditorGUIUtility.labelWidth = cache;
} );
owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedStencilOptions = foldoutValue;
}
public void ReadFromString( ref uint index, ref string[] nodeParams )
{
m_active = Convert.ToBoolean( nodeParams[ index++ ] );
if( UIUtils.CurrentShaderVersion() > 14501 )
{
m_refValue.ReadFromString( ref index, ref nodeParams );
m_readMask.ReadFromString( ref index, ref nodeParams );
m_writeMask.ReadFromString( ref index, ref nodeParams );
m_comparisonFunctionIdx.ReadFromString( ref index, ref nodeParams );
m_passStencilOpIdx.ReadFromString( ref index, ref nodeParams );
m_failStencilOpIdx.ReadFromString( ref index, ref nodeParams );
m_zFailStencilOpIdx.ReadFromString( ref index, ref nodeParams );
}
else
{
m_refValue.IntValue = Convert.ToInt32( nodeParams[ index++ ] );
m_readMask.IntValue = Convert.ToInt32( nodeParams[ index++ ] );
m_writeMask.IntValue = Convert.ToInt32( nodeParams[ index++ ] );
m_comparisonFunctionIdx.IntValue = Convert.ToInt32( nodeParams[ index++ ] );
m_passStencilOpIdx.IntValue = Convert.ToInt32( nodeParams[ index++ ] );
m_failStencilOpIdx.IntValue = Convert.ToInt32( nodeParams[ index++ ] );
m_zFailStencilOpIdx.IntValue = Convert.ToInt32( nodeParams[ index++ ] );
}
if( UIUtils.CurrentShaderVersion() > 13203 )
{
if( UIUtils.CurrentShaderVersion() > 14501 )
{
m_comparisonFunctionBackIdx.ReadFromString( ref index, ref nodeParams );
m_passStencilOpBackIdx.ReadFromString( ref index, ref nodeParams );
m_failStencilOpBackIdx.ReadFromString( ref index, ref nodeParams );
m_zFailStencilOpBackIdx.ReadFromString( ref index, ref nodeParams );
}
else
{
m_comparisonFunctionBackIdx.IntValue = Convert.ToInt32( nodeParams[ index++ ] );
m_passStencilOpBackIdx.IntValue = Convert.ToInt32( nodeParams[ index++ ] );
m_failStencilOpBackIdx.IntValue = Convert.ToInt32( nodeParams[ index++ ] );
m_zFailStencilOpBackIdx.IntValue = Convert.ToInt32( nodeParams[ index++ ] );
}
}
}
public void WriteToString( ref string nodeInfo )
{
IOUtils.AddFieldValueToString( ref nodeInfo, m_active );
m_refValue.WriteToString( ref nodeInfo );
m_readMask.WriteToString( ref nodeInfo );
m_writeMask.WriteToString( ref nodeInfo );
m_comparisonFunctionIdx.WriteToString( ref nodeInfo );
m_passStencilOpIdx.WriteToString( ref nodeInfo );
m_failStencilOpIdx.WriteToString( ref nodeInfo );
m_zFailStencilOpIdx.WriteToString( ref nodeInfo );
m_comparisonFunctionBackIdx.WriteToString( ref nodeInfo );
m_passStencilOpBackIdx.WriteToString( ref nodeInfo );
m_failStencilOpBackIdx.WriteToString( ref nodeInfo );
m_zFailStencilOpBackIdx.WriteToString( ref nodeInfo );
}
public bool Active
{
get { return m_active; }
set { m_active = value; }
}
}
}
|