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
|
// Amplify Shader Editor - Visual Shader Editing Tool
// Copyright (c) Amplify Creations, Lda <info@amplify.pt>
// http://kylehalladay.com/blog/tutorial/2014/02/18/Fresnel-Shaders-From-The-Ground-Up.html
// http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter07.html
using System;
using UnityEngine;
using UnityEditor;
namespace AmplifyShaderEditor
{
[Serializable]
[NodeAttributes( "Fresnel", "Surface Data", "Simple Fresnel effect" )]
public sealed class FresnelNode : ParentNode
{
private const string FresnedFinalVar = "fresnelNode";
[SerializeField]
private ViewSpace m_normalSpace = ViewSpace.Tangent;
enum FresnelType
{
Standard = 0,
Schlick,
SchlickIOR,
}
enum NormalType
{
WorldNormal = 0,
TangentNormal,
HalfVector,
}
enum ViewType
{
ViewDir = 0,
LightDir,
}
[SerializeField]
private FresnelType m_fresnelType = FresnelType.Standard;
[SerializeField]
private NormalType m_normalType = NormalType.WorldNormal;
[SerializeField]
private ViewType m_viewType = ViewType.ViewDir;
[SerializeField]
private bool m_normalizeVectors = false;
[SerializeField]
private bool m_safePower = false;
private InputPort m_normalVecPort;
private InputPort m_viewVecPort;
private InputPort m_biasPort;
private InputPort m_scalePort;
private InputPort m_powerPort;
protected override void CommonInit( int uniqueId )
{
base.CommonInit( uniqueId );
AddInputPort( WirePortDataType.FLOAT3, false, "World Normal", -1, MasterNodePortCategory.Fragment, 0 );
AddInputPort( WirePortDataType.FLOAT3, false, "View Dir", -1, MasterNodePortCategory.Fragment, 4 );
AddInputPort( WirePortDataType.FLOAT, false, "Bias", -1, MasterNodePortCategory.Fragment, 1 );
AddInputPort( WirePortDataType.FLOAT, false, "Scale", -1, MasterNodePortCategory.Fragment, 2 );
AddInputPort( WirePortDataType.FLOAT, false, "Power", -1, MasterNodePortCategory.Fragment, 3 );
AddOutputPort( WirePortDataType.FLOAT, "Out" );
m_normalVecPort = m_inputPorts[ 0 ];
m_viewVecPort = m_inputPorts[ 1 ];
m_biasPort = m_inputPorts[ 2 ];
m_scalePort = m_inputPorts[ 3 ];
m_powerPort = m_inputPorts[ 4 ];
m_biasPort.AutoDrawInternalData = true;
m_scalePort.AutoDrawInternalData = true;
m_powerPort.AutoDrawInternalData = true;
m_autoWrapProperties = true;
m_drawPreviewAsSphere = true;
m_normalVecPort.Vector3InternalData = Vector3.forward;
m_scalePort.FloatInternalData = 1;
m_powerPort.FloatInternalData = 5;
m_previewShaderGUID = "240145eb70cf79f428015012559f4e7d";
}
public override void SetPreviewInputs()
{
base.SetPreviewInputs();
//m_mate
PreviewMaterial.SetInt( "_FresnelType", (int)m_fresnelType );
if( m_normalType == NormalType.TangentNormal && m_normalVecPort.IsConnected )
m_previewMaterialPassId = 2;
else if( (m_normalType == NormalType.WorldNormal || m_normalType == NormalType.HalfVector ) && m_normalVecPort.IsConnected && !m_viewVecPort.IsConnected )
m_previewMaterialPassId = 1;
else if( m_normalType == NormalType.HalfVector && !m_normalVecPort.IsConnected && !m_viewVecPort.IsConnected )
m_previewMaterialPassId = 3;
else if( m_normalVecPort.IsConnected && m_viewVecPort.IsConnected )
m_previewMaterialPassId = 4;
else if( !m_normalVecPort.IsConnected && !m_viewVecPort.IsConnected && m_viewType == ViewType.LightDir )
m_previewMaterialPassId = 5;
else if( !m_normalVecPort.IsConnected && m_viewVecPort.IsConnected && m_normalType == NormalType.HalfVector )
m_previewMaterialPassId = 7;
else if( !m_normalVecPort.IsConnected && m_viewVecPort.IsConnected )
m_previewMaterialPassId = 6;
else
m_previewMaterialPassId = 0;
}
public override void DrawProperties()
{
base.DrawProperties();
EditorGUI.BeginChangeCheck();
m_fresnelType = (FresnelType)EditorGUILayoutEnumPopup( "Type", m_fresnelType );
m_normalType = (NormalType)EditorGUILayoutEnumPopup( "Normal Vector", m_normalType );
m_viewType = (ViewType)EditorGUILayoutEnumPopup( "View Vector", m_viewType );
if( EditorGUI.EndChangeCheck() )
{
UpdatePort();
}
if( !m_biasPort.IsConnected && m_biasPort.Visible )
m_biasPort.FloatInternalData = EditorGUILayoutFloatField( m_biasPort.Name, m_biasPort.FloatInternalData );
if( !m_scalePort.IsConnected && m_scalePort.Visible )
m_scalePort.FloatInternalData = EditorGUILayoutFloatField( m_scalePort.Name, m_scalePort.FloatInternalData );
if( !m_powerPort.IsConnected && m_powerPort.Visible )
m_powerPort.FloatInternalData = EditorGUILayoutFloatField( m_powerPort.Name, m_powerPort.FloatInternalData );
m_normalizeVectors = EditorGUILayoutToggle( "Normalize Vectors", m_normalizeVectors );
m_safePower = EditorGUILayoutToggle( PowerNode.SafePowerLabel, m_safePower );
}
private void UpdatePort()
{
switch( m_normalType )
{
default:
case NormalType.WorldNormal:
m_normalVecPort.Name = "World Normal";
break;
case NormalType.TangentNormal:
m_normalVecPort.Name = "Normal";
break;
case NormalType.HalfVector:
m_normalVecPort.Name = "Half Vector";
break;
}
switch( m_viewType )
{
default:
case ViewType.ViewDir:
m_viewVecPort.Name = "View Dir";
break;
case ViewType.LightDir:
m_viewVecPort.Name = "Light Dir";
break;
}
switch( m_fresnelType )
{
default:
case FresnelType.Standard:
m_biasPort.Visible = true;
m_biasPort.Name = "Bias";
m_scalePort.Name = "Scale";
m_scalePort.Visible = true;
m_powerPort.Visible = true;
break;
case FresnelType.Schlick:
m_biasPort.Visible = true;
m_biasPort.Name = "F0";
m_scalePort.Visible = false;
m_powerPort.Visible = false;
break;
case FresnelType.SchlickIOR:
m_biasPort.Visible = false;
m_scalePort.Name = "IOR";
m_scalePort.Visible = true;
m_powerPort.Visible = false;
break;
}
m_sizeIsDirty = true;
}
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( dataCollector.IsFragmentCategory )
dataCollector.AddToInput( UniqueId, SurfaceInputs.WORLD_POS );
string viewdir = string.Empty;
if( m_viewType == ViewType.ViewDir )
{
if( m_viewVecPort.IsConnected )
viewdir = m_viewVecPort.GeneratePortInstructions( ref dataCollector );
else
viewdir = GeneratorUtils.GenerateViewDirection( ref dataCollector, UniqueId, ViewSpace.World );
}
else
{
if( m_viewVecPort.IsConnected )
viewdir = m_viewVecPort.GeneratePortInstructions( ref dataCollector );
else
viewdir = GeneratorUtils.GenerateWorldLightDirection( ref dataCollector, UniqueId, CurrentPrecisionType );
}
string normal = string.Empty;
if( m_normalType == NormalType.WorldNormal || m_normalType == NormalType.TangentNormal )
{
if( m_normalVecPort.IsConnected )
{
normal = m_normalVecPort.GeneratePortInstructions( ref dataCollector );
if( dataCollector.IsFragmentCategory )
{
dataCollector.AddToInput( UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false );
if( m_normalType == NormalType.TangentNormal )
{
if( dataCollector.IsTemplate )
{
normal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal( UniqueId, CurrentPrecisionType, normal, OutputId );
}
else
{
normal = GeneratorUtils.GenerateWorldNormal( ref dataCollector, UniqueId, CurrentPrecisionType, normal, OutputId );
dataCollector.AddToInput( UniqueId, SurfaceInputs.WORLD_NORMAL, CurrentPrecisionType );
dataCollector.ForceNormal = true;
}
}
else
{
if( m_normalizeVectors )
normal = string.Format( "normalize( {0} )", normal );
}
}
else
{
if( m_normalType == NormalType.TangentNormal )
{
string wtMatrix = GeneratorUtils.GenerateWorldToTangentMatrix( ref dataCollector, UniqueId, CurrentPrecisionType );
normal = "mul( " + normal + "," + wtMatrix + " )";
}
}
}
else
{
if( dataCollector.IsFragmentCategory )
{
dataCollector.AddToInput( UniqueId, SurfaceInputs.WORLD_NORMAL, CurrentPrecisionType );
if( dataCollector.DirtyNormal )
dataCollector.AddToInput( UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false );
}
if( dataCollector.IsTemplate )
normal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal( CurrentPrecisionType, normalize: ( dataCollector.DirtyNormal && m_normalizeVectors ) );
else
normal = GeneratorUtils.GenerateWorldNormal( ref dataCollector, UniqueId, ( dataCollector.DirtyNormal && m_normalizeVectors ) );
if( dataCollector.DirtyNormal )
{
dataCollector.ForceNormal = true;
}
}
}
else
{
// generate HV
if( !m_normalVecPort.IsConnected )
{
string halfView = GeneratorUtils.GenerateViewDirection( ref dataCollector, UniqueId, ViewSpace.World );
string halfLight = GeneratorUtils.GenerateWorldLightDirection( ref dataCollector, UniqueId, CurrentPrecisionType );
normal = "halfVector" + OutputId;
dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT3, normal, "normalize( " + halfView + " + " + halfLight + " )" );
}
else
{
normal = m_normalVecPort.GeneratePortInstructions( ref dataCollector );
if( m_normalizeVectors )
normal = string.Format( "normalize( {0} )", normal );
}
}
string bias = m_biasPort.GeneratePortInstructions( ref dataCollector );
string scale = m_scalePort.GeneratePortInstructions( ref dataCollector );
string power = m_powerPort.GeneratePortInstructions( ref dataCollector );
string fresnelNDotVLocalValue = "dot( " + normal + ", " + viewdir + " )";
string fresnelNDotVLocalVar = "fresnelNdotV" + OutputId;
dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT, fresnelNDotVLocalVar, fresnelNDotVLocalValue );
string fresnelFinalVar = FresnedFinalVar + OutputId;
string result = string.Empty;
switch( m_fresnelType )
{
default:
case FresnelType.Standard:
{
string powOp = m_safePower? string.Format( "pow( max( 1.0 - {0} , 0.0001 ), {1} )", fresnelNDotVLocalVar, power ):
string.Format( "pow( 1.0 - {0}, {1} )", fresnelNDotVLocalVar, power );
result = string.Format( "( {0} + {1} * {2} )", bias, scale, powOp );
}
break;
case FresnelType.Schlick:
{
string f0VarName = "f0" + OutputId;
dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT, f0VarName, bias );
string powOp = m_safePower? string.Format( "pow( max( 1.0 - {0} , 0.0001 ), 5 )", fresnelNDotVLocalVar ) :
string.Format( "pow( 1.0 - {0}, 5 )", fresnelNDotVLocalVar );
result = string.Format( "( {0} + ( 1.0 - {0} ) * {1} )", f0VarName, powOp );
}
break;
case FresnelType.SchlickIOR:
{
string iorVarName = "ior" + OutputId;
dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT, iorVarName, scale );
string iorPowOp = m_safePower? string.Format( "pow( max( ( 1 - {0} ) / ( 1 + {0} ) , 0.0001 ), 2 )", iorVarName ):
string.Format( "pow( ( 1 - {0} ) / ( 1 + {0} ), 2 )", iorVarName );
dataCollector.AddLocalVariable( UniqueId, iorVarName +" = "+ iorPowOp + ";");
string fresnelPowOp = m_safePower? string.Format( "pow( max( 1.0 - {0} , 0.0001 ), 5 )", fresnelNDotVLocalVar ):
string.Format( "pow( 1.0 - {0}, 5 )", fresnelNDotVLocalVar );
result = string.Format( "( {0} + ( 1.0 - {0} ) * {1} )", iorVarName, fresnelPowOp );
}
break;
}
RegisterLocalVariable( 0, result, ref dataCollector, fresnelFinalVar );
return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
}
public override void PropagateNodeData( NodeData nodeData, ref MasterNodeDataCollector dataCollector )
{
base.PropagateNodeData( nodeData, ref dataCollector );
if( m_normalType == NormalType.TangentNormal && m_normalVecPort.IsConnected )
dataCollector.DirtyNormal = true;
}
public override void ReadFromString( ref string[] nodeParams )
{
base.ReadFromString( ref nodeParams );
if( UIUtils.CurrentShaderVersion() > 15305 )
{
m_fresnelType = (FresnelType)Enum.Parse( typeof( FresnelType ), GetCurrentParam( ref nodeParams ) );
m_normalType = (NormalType)Enum.Parse( typeof( NormalType ), GetCurrentParam( ref nodeParams ) );
m_viewType = (ViewType)Enum.Parse( typeof( ViewType ), GetCurrentParam( ref nodeParams ) );
m_normalizeVectors = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
if( UIUtils.CurrentShaderVersion() > 17502 )
m_safePower = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
}
else
{
if( UIUtils.CurrentShaderVersion() >= 13202 )
{
m_normalSpace = (ViewSpace)Enum.Parse( typeof( ViewSpace ), GetCurrentParam( ref nodeParams ) );
}
else
{
m_normalSpace = ViewSpace.World;
}
if( m_normalSpace == ViewSpace.World )
m_normalType = NormalType.WorldNormal;
else
m_normalType = NormalType.TangentNormal;
}
UpdatePort();
}
public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
{
base.WriteToString( ref nodeInfo, ref connectionsInfo );
IOUtils.AddFieldValueToString( ref nodeInfo, m_fresnelType );
IOUtils.AddFieldValueToString( ref nodeInfo, m_normalType );
IOUtils.AddFieldValueToString( ref nodeInfo, m_viewType );
IOUtils.AddFieldValueToString( ref nodeInfo, m_normalizeVectors );
IOUtils.AddFieldValueToString( ref nodeInfo, m_safePower );
}
}
}
|