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
|
using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;
namespace AmplifyShaderEditor
{
[Serializable]
[NodeAttributes( "Texture Transform", "Textures", "Gives access to texture tiling and offset as set on the material inspector" )]
public sealed class TextureTransformNode : ParentNode
{
private readonly string[] Dummy = { string.Empty };
private const string InstancedLabelStr = "Instanced";
[SerializeField]
private bool m_instanced = false;
[SerializeField]
private int m_referenceSamplerId = -1;
[SerializeField]
private int m_referenceNodeId = -1;
[SerializeField]
private TexturePropertyNode m_inputReferenceNode = null;
private TexturePropertyNode m_referenceNode = null;
private Vector4Node m_texCoordsHelper;
private UpperLeftWidgetHelper m_upperLeftWidget = new UpperLeftWidgetHelper();
private int m_cachedSamplerId = -1;
private int m_cachedSamplerIdArray = -1;
private int m_cachedSamplerIdCube = -1;
private int m_cachedSamplerId3D = -1;
protected override void CommonInit( int uniqueId )
{
base.CommonInit( uniqueId );
AddInputPort( WirePortDataType.SAMPLER2D, false, "Tex" );
m_inputPorts[ 0 ].CreatePortRestrictions( WirePortDataType.SAMPLER1D, WirePortDataType.SAMPLER2D, WirePortDataType.SAMPLER3D, WirePortDataType.SAMPLERCUBE, WirePortDataType.OBJECT );
AddOutputPort( WirePortDataType.FLOAT2, "Tiling" );
AddOutputPort( WirePortDataType.FLOAT2, "Offset" );
m_textLabelWidth = 80;
m_autoWrapProperties = true;
m_hasLeftDropdown = true;
m_previewShaderGUID = "25ba2903568b00343ae06788994cab54";
}
public override void AfterCommonInit()
{
base.AfterCommonInit();
if( PaddingTitleLeft == 0 )
{
PaddingTitleLeft = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
if( PaddingTitleRight == 0 )
PaddingTitleRight = Constants.PropertyPickerWidth + Constants.IconsLeftRightMargin;
}
}
public override void RenderNodePreview()
{
//Runs at least one time
if( !m_initialized )
{
// nodes with no preview don't update at all
PreviewIsDirty = false;
return;
}
if( !PreviewIsDirty )
return;
SetPreviewInputs();
RenderTexture temp = RenderTexture.active;
RenderTexture.active = m_outputPorts[ 0 ].OutputPreviewTexture;
PreviewMaterial.SetInt( "_PreviewID", 0 );
Graphics.Blit( null, m_outputPorts[ 0 ].OutputPreviewTexture, PreviewMaterial, m_previewMaterialPassId );
RenderTexture.active = m_outputPorts[ 1 ].OutputPreviewTexture;
PreviewMaterial.SetInt( "_PreviewID", 1 );
Graphics.Blit( null, m_outputPorts[ 1 ].OutputPreviewTexture, PreviewMaterial, m_previewMaterialPassId );
RenderTexture.active = temp;
PreviewIsDirty = m_continuousPreviewRefresh;
FinishPreviewRender = true;
}
void SetPreviewTexture( Texture newValue )
{
if( newValue is Cubemap )
{
m_previewMaterialPassId = 3;
if( m_cachedSamplerIdCube == -1 )
m_cachedSamplerIdCube = Shader.PropertyToID( "_Cube" );
PreviewMaterial.SetTexture( m_cachedSamplerIdCube, newValue as Cubemap );
}
else if( newValue is Texture2DArray )
{
m_previewMaterialPassId = 2;
if( m_cachedSamplerIdArray == -1 )
m_cachedSamplerIdArray = Shader.PropertyToID( "_Array" );
PreviewMaterial.SetTexture( m_cachedSamplerIdArray, newValue as Texture2DArray );
}
else if( newValue is Texture3D )
{
m_previewMaterialPassId = 1;
if( m_cachedSamplerId3D == -1 )
m_cachedSamplerId3D = Shader.PropertyToID( "_Sampler3D" );
PreviewMaterial.SetTexture( m_cachedSamplerId3D, newValue as Texture3D );
}
else
{
m_previewMaterialPassId = 0;
if( m_cachedSamplerId == -1 )
m_cachedSamplerId = Shader.PropertyToID( "_Sampler" );
PreviewMaterial.SetTexture( m_cachedSamplerId, newValue );
}
}
public override void SetPreviewInputs()
{
base.SetPreviewInputs();
if( m_inputPorts[ 0 ].IsConnected )
{
SetPreviewTexture( m_inputPorts[ 0 ].InputPreviewTexture( ContainerGraph ) );
}
else if( m_referenceNode != null )
{
if( m_referenceNode.Value != null )
{
SetPreviewTexture( m_referenceNode.Value );
}
else
{
SetPreviewTexture( m_referenceNode.PreviewTexture );
}
}
}
public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
{
base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
m_inputReferenceNode = m_inputPorts[ 0 ].GetOutputNodeWhichIsNotRelay() as TexturePropertyNode;
UpdateTitle();
}
public override void OnInputPortDisconnected( int portId )
{
base.OnInputPortDisconnected( portId );
m_inputReferenceNode = null;
UpdateTitle();
}
void UpdateTitle()
{
if( m_inputReferenceNode != null )
{
m_additionalContent.text = string.Format( Constants.PropertyValueLabel, m_inputReferenceNode.PropertyInspectorName );
}
else if( m_referenceSamplerId > -1 && m_referenceNode != null )
{
m_additionalContent.text = string.Format( Constants.PropertyValueLabel, m_referenceNode.PropertyInspectorName );
}
else
{
m_additionalContent.text = string.Empty;
}
m_sizeIsDirty = true;
}
public override void DrawProperties()
{
base.DrawProperties();
bool guiEnabledBuffer = GUI.enabled;
EditorGUI.BeginChangeCheck();
List<string> arr = new List<string>( UIUtils.TexturePropertyNodeArr() );
if( arr != null && arr.Count > 0 )
{
arr.Insert( 0, "None" );
GUI.enabled = true && ( !m_inputPorts[ 0 ].IsConnected );
m_referenceSamplerId = EditorGUILayoutPopup( Constants.AvailableReferenceStr, m_referenceSamplerId + 1, arr.ToArray() ) - 1;
}
else
{
m_referenceSamplerId = -1;
GUI.enabled = false;
EditorGUILayoutPopup( Constants.AvailableReferenceStr, m_referenceSamplerId, Dummy );
}
GUI.enabled = guiEnabledBuffer;
if( EditorGUI.EndChangeCheck() )
{
m_referenceNode = UIUtils.GetTexturePropertyNode( m_referenceSamplerId );
if( m_referenceNode != null )
{
m_referenceNodeId = m_referenceNode.UniqueId;
}
else
{
m_referenceNodeId = -1;
m_referenceSamplerId = -1;
}
UpdateTitle();
}
m_instanced = EditorGUILayoutToggle( InstancedLabelStr, m_instanced );
}
public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
{
if( !m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
{
base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
string texTransform = string.Empty;
if( m_inputPorts[ 0 ].IsConnected )
{
texTransform = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector ) + "_ST";
}
else if( m_referenceNode != null )
{
m_referenceNode.BaseGenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
texTransform = m_referenceNode.PropertyName + "_ST";
}
else
{
texTransform = "_ST";
UIUtils.ShowMessage( UniqueId, "Please specify a texture sample on the Texture Transform Size node", MessageSeverity.Warning );
}
//bool excludeUniformKeyword = UIUtils.CurrentWindow.OutsideGraph.IsInstancedShader || UIUtils.CurrentWindow.OutsideGraph.IsSRP;
//string uniformRegister = UIUtils.GenerateUniformName( excludeUniformKeyword, WirePortDataType.FLOAT4, texTransform );
//dataCollector.AddToUniforms( UniqueId, uniformRegister, true );
if( m_texCoordsHelper == null )
{
m_texCoordsHelper = CreateInstance<Vector4Node>();
m_texCoordsHelper.ContainerGraph = ContainerGraph;
m_texCoordsHelper.SetBaseUniqueId( UniqueId, true );
m_texCoordsHelper.RegisterPropertyOnInstancing = false;
m_texCoordsHelper.AddGlobalToSRPBatcher = true;
}
if( m_instanced )
{
m_texCoordsHelper.CurrentParameterType = PropertyType.InstancedProperty;
}
else
{
m_texCoordsHelper.CurrentParameterType = PropertyType.Global;
}
m_texCoordsHelper.ResetOutputLocals();
m_texCoordsHelper.SetRawPropertyName( texTransform );
texTransform = m_texCoordsHelper.GenerateShaderForOutput( 0, ref dataCollector, false );
m_outputPorts[ 0 ].SetLocalValue( texTransform+ ".xy", dataCollector.PortCategory );
m_outputPorts[ 1 ].SetLocalValue( texTransform + ".zw", dataCollector.PortCategory );
}
return m_outputPorts[ outputId ].LocalValue( dataCollector.PortCategory );
}
public override void Draw( DrawInfo drawInfo )
{
base.Draw( drawInfo );
EditorGUI.BeginChangeCheck();
{
List<string> arr = new List<string>( UIUtils.TexturePropertyNodeArr() );
bool guiEnabledBuffer = GUI.enabled;
if( arr != null && arr.Count > 0 )
{
arr.Insert( 0, "None" );
GUI.enabled = true && ( !m_inputPorts[ 0 ].IsConnected );
m_referenceSamplerId = m_upperLeftWidget.DrawWidget( this, m_referenceSamplerId + 1, arr.ToArray() ) - 1;
}
else
{
m_referenceSamplerId = -1;
GUI.enabled = false;
m_upperLeftWidget.DrawWidget( this, m_referenceSamplerId, Dummy );
}
GUI.enabled = guiEnabledBuffer;
}
if( EditorGUI.EndChangeCheck() )
{
m_referenceNode = UIUtils.GetTexturePropertyNode( m_referenceSamplerId );
if( m_referenceNode != null )
{
m_referenceNodeId = m_referenceNode.UniqueId;
}
else
{
m_referenceNodeId = -1;
m_referenceSamplerId = -1;
}
UpdateTitle();
}
}
public override void RefreshExternalReferences()
{
base.RefreshExternalReferences();
m_referenceNode = UIUtils.GetNode( m_referenceNodeId ) as TexturePropertyNode;
m_referenceSamplerId = UIUtils.GetTexturePropertyNodeRegisterId( m_referenceNodeId );
UpdateTitle();
}
public override void ReadFromString( ref string[] nodeParams )
{
base.ReadFromString( ref nodeParams );
m_referenceNodeId = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
if( UIUtils.CurrentShaderVersion() > 17200 )
{
m_instanced = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
}
}
public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
{
base.WriteToString( ref nodeInfo, ref connectionsInfo );
IOUtils.AddFieldValueToString( ref nodeInfo, m_referenceNodeId );
IOUtils.AddFieldValueToString( ref nodeInfo, m_instanced );
}
public override void Destroy()
{
base.Destroy();
m_referenceNode = null;
m_inputReferenceNode = null;
m_upperLeftWidget = null;
if( m_texCoordsHelper != null )
{
//Not calling m_texCoordsHelper.Destroy() on purpose so UIUtils does not incorrectly unregister stuff
DestroyImmediate( m_texCoordsHelper );
m_texCoordsHelper = null;
}
}
}
}
|