-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathVFXShaderGraphParticleOutput.cs
More file actions
362 lines (312 loc) · 13.3 KB
/
Copy pathVFXShaderGraphParticleOutput.cs
File metadata and controls
362 lines (312 loc) · 13.3 KB
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
using System.Collections.Generic;
using System.Linq;
using UnityEditor.ShaderGraph.Internal;
using UnityEngine;
namespace UnityEditor.VFX
{
class VFXShaderGraphParticleOutput : VFXAbstractParticleOutput, IVFXShaderGraphOutput
{
//"protected" is only to be listed by VFXModel.GetSettings, we should always use GetOrRefreshShaderGraphObject
[SerializeField, VFXSetting]
protected ShaderGraphVfxAsset shaderGraph;
[SerializeField, VFXSetting]
internal VFXMaterialSerializedSettings materialSettings = new VFXMaterialSerializedSettings();
private bool m_IsShaderGraphMissing;
protected override IEnumerable<string> untransferableSettings
{
get
{
//In case of convert from VFXComposedParticleOutput, we won't retrieve shaderGraph anymore, this settings is now hidden
yield return nameof(shaderGraph);
}
}
public ShaderGraphVfxAsset GetOrRefreshShaderGraphObject(bool refreshErrors = true)
{
var wasShaderGraphMissing = m_IsShaderGraphMissing;
//This is the only place where shaderGraph property is updated or read
if (shaderGraph == null && !object.ReferenceEquals(shaderGraph, null))
{
var assetPath = AssetDatabase.GetAssetPath(shaderGraph.GetInstanceID());
var newShaderGraph = AssetDatabase.LoadAssetAtPath<ShaderGraphVfxAsset>(assetPath);
m_IsShaderGraphMissing = newShaderGraph == null;
if (!m_IsShaderGraphMissing)
{
shaderGraph = newShaderGraph;
}
}
else
{
m_IsShaderGraphMissing = false;
}
if (refreshErrors && wasShaderGraphMissing != m_IsShaderGraphMissing)
{
RefreshErrors();
}
return shaderGraph;
}
public override void Sanitize(int version)
{
base.Sanitize(version);
if (version < 14)
{
var shaderGraph = GetOrRefreshShaderGraphObject();
if (shaderGraph && shaderGraph.generatesWithShaderGraph)
{
var path = AssetDatabase.GetAssetPath(shaderGraph);
var referenceMaterial = AssetDatabase.LoadAssetAtPath<Material>(path);
materialSettings.UpgradeToMaterialWorkflowVersion(referenceMaterial);
}
}
if (version < 15)
{
SanitizeHelper.MigrateSGOutputToComposed(this);
}
}
public override bool CanBeCompiled()
{
if (m_IsShaderGraphMissing)
return false;
var sg = GetOrRefreshShaderGraphObject();
if (sg != null && sg.generatesWithShaderGraph)
return false;
return base.CanBeCompiled();
}
public override void OnSettingModified(VFXSetting setting)
{
if (setting.name == nameof(shaderGraph))
{
VFXAnalytics.GetInstance().OnSpecificSettingChanged($"{GetType().Name}.{setting.name}");
}
}
public override void GetImportDependentAssets(HashSet<int> dependencies)
{
base.GetImportDependentAssets(dependencies);
if (!object.ReferenceEquals(shaderGraph, null))
{
dependencies.Add(shaderGraph.GetInstanceID());
}
}
protected VFXShaderGraphParticleOutput(bool strip = false) : base(strip) { }
// Here we maintain a list of settings that we do not need if we are using the ShaderGraph generation path (it will be in the material inspector).
static IEnumerable<string> FilterOutBuiltinSettings()
{
yield return "blendMode";
yield return "cullMode";
yield return "zWriteMode";
yield return "zTestMode";
yield return "excludeFromTUAndAA";
yield return "preserveSpecularLighting";
yield return "doubleSided";
yield return "onlyAmbientLighting";
yield return "useExposureWeight";
yield return "alphaThreshold";
yield return "normalBending";
}
protected override IEnumerable<string> filteredOutSettings
{
get
{
foreach (var setting in base.filteredOutSettings)
yield return setting;
var sg = GetOrRefreshShaderGraphObject();
if (sg != null || m_IsShaderGraphMissing)
{
yield return "colorMapping";
yield return "useAlphaClipping";
if (m_IsShaderGraphMissing)
{
yield return "useSoftParticle";
yield return "uvMode";
foreach (var builtinSetting in FilterOutBuiltinSettings())
yield return builtinSetting;
}
}
else
{
yield return nameof(shaderGraph);
}
yield return nameof(materialSettings);
}
}
public override bool supportsUV => base.supportsUV && GetOrRefreshShaderGraphObject() == null;
public override bool exposeAlphaThreshold
{
get
{
var shaderGraph = GetOrRefreshShaderGraphObject();
if (shaderGraph == null)
{
if (base.exposeAlphaThreshold)
return true;
}
else
{
if (!shaderGraph.alphaClipping)
{
//alpha clipping isn't enabled in shaderGraph, we implicitly still allows clipping for shadow & motion vector passes.
if (!isBlendModeOpaque && (hasMotionVector || hasShadowCasting))
return true;
}
}
return false;
}
}
public override bool supportSoftParticles => base.supportSoftParticles && GetOrRefreshShaderGraphObject() == null;
public override bool hasAlphaClipping
{
get
{
var shaderGraph = GetOrRefreshShaderGraphObject();
bool noShaderGraphAlphaThreshold = shaderGraph == null && useAlphaClipping;
bool ShaderGraphAlphaThreshold = shaderGraph != null && shaderGraph.alphaClipping;
return noShaderGraphAlphaThreshold || ShaderGraphAlphaThreshold;
}
}
// Do not resync slots when shader graph is missing to keep potential links to the shader properties
public override bool ResyncSlots(bool notify) => !m_IsShaderGraphMissing && base.ResyncSlots(notify);
public override void CheckGraphBeforeImport()
{
base.CheckGraphBeforeImport();
var currentShaderGraph = GetOrRefreshShaderGraphObject();
// If the graph is reimported it can be because one of its dependency such as the shadergraphs, has been changed.
if (!VFXGraph.explicitCompile)
{
ResyncSlots(true);
// Ensure that the output context name is in sync with the shader graph shader enum name.
if (currentShaderGraph != null && currentShaderGraph.generatesWithShaderGraph)
Invalidate(InvalidationCause.kUIChangedTransient);
else if (m_IsShaderGraphMissing)
{
var vfxName = GetGraph().visualEffectResource.name;
Debug.LogError($"The VFX Graph '{vfxName}'" + VFXShaderGraphHelpers.GetMissingShaderGraphErrorMessage(currentShaderGraph));
}
}
}
internal override void GenerateErrors(VFXInvalidateErrorReporter manager)
{
base.GenerateErrors(manager);
var currentShaderGraph = GetOrRefreshShaderGraphObject(false);
if (m_IsShaderGraphMissing)
{
var message = VFXShaderGraphHelpers.GetMissingShaderGraphErrorMessage(currentShaderGraph);
manager.RegisterError("ErrorMissingShaderGraph", VFXErrorType.Error, "The VFX Graph" + message);
}
if (currentShaderGraph != null)
{
if (!currentShaderGraph.generatesWithShaderGraph)
{
manager.RegisterError("DeprecatedOldShaderGraph", VFXErrorType.Error, ParticleShadingShaderGraph.kErrorOldSG);
}
else
{
//There isn't automatic sanitize if the SG change its status from old to new SG integration
manager.RegisterError("WrongOutputShaderGraph", VFXErrorType.Error, "Please convert this context to dedicated ShaderGraph Output.");
}
}
}
protected override IEnumerable<VFXPropertyWithValue> inputProperties
{
get
{
IEnumerable<VFXPropertyWithValue> properties = base.inputProperties;
var sg = GetOrRefreshShaderGraphObject();
if (sg != null)
{
//This path is only used with old shader graph integration. It doesn't support keyword.
var shaderGraphProperties = VFXShaderGraphHelpers.GetProperties(sg).Where(o => o.keywordsMapping == null).Select(o => o.property);
properties = properties.Concat(shaderGraphProperties);
}
return properties;
}
}
protected override IEnumerable<VFXNamedExpression> CollectGPUExpressions(IEnumerable<VFXNamedExpression> slotExpressions)
{
foreach (var exp in base.CollectGPUExpressions(slotExpressions))
yield return exp;
var shaderGraph = GetOrRefreshShaderGraphObject();
if (shaderGraph != null)
{
foreach (var sgProperty in shaderGraph.properties)
{
if (inputSlots.Any(t => t.property.name == sgProperty.referenceName))
yield return slotExpressions.First(o => o.name == sgProperty.referenceName);
}
}
}
protected virtual VFXOldShaderGraphHelpers.RPInfo currentRP
{
get { return VFXOldShaderGraphHelpers.hdrpInfo; }
}
public override IEnumerable<string> additionalDefines
{
get
{
foreach (var def in base.additionalDefines)
yield return def;
var shaderGraph = GetOrRefreshShaderGraphObject();
if (shaderGraph != null && !shaderGraph.generatesWithShaderGraph)
{
foreach (var def in VFXOldShaderGraphHelpers.GetAdditionalDefinesGetAdditionalReplacement(shaderGraph, currentRP, graphCodes))
yield return def;
}
}
}
public override VFXExpressionMapper GetExpressionMapper(VFXDeviceTarget target)
{
var mapper = base.GetExpressionMapper(target);
var shaderGraph = GetOrRefreshShaderGraphObject();
switch (target)
{
case VFXDeviceTarget.CPU:
{
}
break;
case VFXDeviceTarget.GPU:
if (shaderGraph != null)
{
foreach (var texture in VFXShaderGraphHelpers.GetTextureConstant(shaderGraph))
mapper.AddExpression(texture, -1);
}
break;
}
return mapper;
}
public virtual bool isLitShader { get => false; }
Dictionary<string, GraphCode> graphCodes;
public override bool SetupCompilation()
{
if (!base.SetupCompilation())
return false;
var shaderGraph = GetOrRefreshShaderGraphObject();
if (shaderGraph != null && !shaderGraph.generatesWithShaderGraph)
{
graphCodes = VFXOldShaderGraphHelpers.BuildGraphCode(shaderGraph, currentRP, isLitShader);
return graphCodes != null;
}
return true;
}
public override void EndCompilation()
{
graphCodes = null;
}
public override IEnumerable<KeyValuePair<string, VFXShaderWriter>> additionalReplacements
{
get
{
foreach (var rep in base.additionalReplacements)
yield return rep;
var shaderGraph = GetOrRefreshShaderGraphObject();
if (shaderGraph != null && !shaderGraph.generatesWithShaderGraph)
{
foreach (var def in VFXOldShaderGraphHelpers.GetAdditionalReplacement(shaderGraph, currentRP, graphCodes, taskType == VFXTaskType.ParticleMeshOutput))
yield return def;
}
}
}
public ShaderGraphVfxAsset GetShaderGraph()
{
var shaderGraph = GetOrRefreshShaderGraphObject();
return shaderGraph;
}
}
}