/
MixtureCallbacks.cs
375 lines (315 loc) · 13.9 KB
/
MixtureCallbacks.cs
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
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System.Linq;
using UnityEditor.ProjectWindowCallback;
using System.IO;
using System.Reflection;
using UnityEngine.Experimental.Rendering;
#if MIXTURE_SHADERGRAPH
using UnityEditor.ShaderGraph;
#endif
namespace Mixture
{
public class MixtureAssetCallbacks
{
public static readonly string Extension = "asset";
public static readonly string customTextureShaderTemplate = "Templates/CustomTextureShaderTemplate";
public static readonly string shaderNodeCSharpTemplate = "Templates/FixedShaderNodeTemplate";
public static readonly string shaderNodeCGTemplate = "Templates/FixedShaderTemplate";
public static readonly string shaderNodeDefaultName = "MixtureShaderNode.cs";
public static readonly string shaderName = "MixtureShader.shader";
public static readonly string csharpComputeShaderNodeTemplate = "Templates/CsharpComputeShaderNodeTemplate";
public static readonly string computeShaderTemplate = "Templates/ComputeShaderTemplate";
public static readonly string computeShaderDefaultName = "MixtureCompute.compute";
public static readonly string computeShaderNodeDefaultName = "MixtureCompute.cs";
public static readonly string cSharpMixtureNodeTemplate = "Templates/CSharpMixtureNodeTemplate";
public static readonly string cSharpMixtureNodeName = "New Mixture Node.cs";
public static readonly string cSharpMixtureNodeViewTemplate = "Templates/CSharpMixtureNodeViewTemplate";
public static readonly string cSharpMixtureNodeViewName = "New Mixture Node View.cs";
public static readonly string customMipMapShaderTemplate = "Templates/CustomMipMapTemplate";
[MenuItem("Assets/Create/🎨 Static Mixture Graph", false, 83)]
public static void CreateStaticMixtureGraph()
{
var graphItem = ScriptableObject.CreateInstance< StaticMixtureGraphAction >();
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, graphItem,
$"New Static Mixture Graph.{Extension}", MixtureUtils.icon, null);
}
[MenuItem("Assets/Create/🌡️ Realtime Mixture Graph", false, 83)]
public static void CreateRealtimeMixtureGraph()
{
var graphItem = ScriptableObject.CreateInstance< RealtimeMixtureGraphAction >();
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, graphItem,
$"New Realtime Mixture Graph.{Extension}", MixtureUtils.realtimeIcon, null);
}
[MenuItem("Assets/Create/Mixture/Variant", false, 100)]
public static void CreateMixtureVariant()
{
var selectedMixtures = Selection.GetFiltered<Texture>(SelectionMode.Assets);
foreach (var mixtureTexture in selectedMixtures)
{
var graph = MixtureDatabase.GetGraphFromTexture(mixtureTexture);
var variant = MixtureEditorUtils.GetVariantAtPath(AssetDatabase.GetAssetPath(mixtureTexture));
if (graph != null)
CreateMixtureVariant(graph, null);
else if (variant != null)
CreateMixtureVariant(null, variant);
}
}
[MenuItem("Assets/Create/Mixture/Variant", true)]
public static bool CreateMixtureVariantEnabled()
{
var selectedMixtures = Selection.GetFiltered<Texture>(SelectionMode.Assets);
int graphOrVariantCount = selectedMixtures.Count(m => MixtureDatabase.GetGraphFromTexture(m) != null || MixtureEditorUtils.GetVariantAtPath(AssetDatabase.GetAssetPath(m)));
return (graphOrVariantCount) > 0;
}
public static void CreateMixtureVariant(MixtureGraph targetGraph, MixtureVariant parentVariant)
{
string path;
if (targetGraph != null)
path = AssetDatabase.GetAssetPath(targetGraph);
else
{
targetGraph = parentVariant.parentGraph;
path = AssetDatabase.GetAssetPath(parentVariant);
}
// Patch path name to add Variant
string fileName = Path.GetFileNameWithoutExtension(path) + " Variant";
path = Path.Combine(Path.GetDirectoryName(path), fileName + Path.GetExtension(path));
path = AssetDatabase.GenerateUniqueAssetPath(path);
var action = ScriptableObject.CreateInstance< MixtureVariantAction >();
action.targetGraph = targetGraph;
action.parentVariant = parentVariant;
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, action,
Path.GetFileName(path), targetGraph.type == MixtureGraphType.Realtime ? MixtureUtils.realtimeVariantIcon : MixtureUtils.iconVariant, null);
}
#if MIXTURE_SHADERGRAPH
[MenuItem("Assets/Create/Mixture/Fixed Shader Graph", false, 202)]
[MenuItem("Assets/Create/Shader/Custom Texture Graph", false, 200)]
public static void CreateCustomTextureShaderGraph()
{
var graphItem = ScriptableObject.CreateInstance< CustomtextureShaderGraphAction >();
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, graphItem,
$"New Custom Texture Graph.{ShaderGraphImporter.Extension}", Resources.Load<Texture2D>("sg_graph_icon@64"), null);
}
[MenuItem("Assets/Create/Mixture/Custom Mip Map Shader", false, 903)]
public static void CreateCustomMipMapShaderGraph()
{
var template = Resources.Load< TextAsset >(customMipMapShaderTemplate);
ProjectWindowUtil.CreateScriptAssetFromTemplateFile(AssetDatabase.GetAssetPath(template), "Custom MipMap");
}
#endif
[MenuItem("Assets/Create/Mixture/C# Fixed Shader Node", false, 200)]
public static void CreateCSharpFixedShaderNode()
{
var template = Resources.Load< TextAsset >(shaderNodeCSharpTemplate);
ProjectWindowUtil.CreateScriptAssetFromTemplateFile(AssetDatabase.GetAssetPath(template), shaderNodeDefaultName);
}
[MenuItem("Assets/Create/Mixture/Fixed Shader", false, 201)]
public static void CreateCGFixedShaderNode()
{
var template = Resources.Load< TextAsset >(shaderNodeCGTemplate);
ProjectWindowUtil.CreateScriptAssetFromTemplateFile(AssetDatabase.GetAssetPath(template), shaderName);
}
[MenuItem("Assets/Create/Shader/Custom Texture", false, 100)]
public static void CreateCustomTextureShader()
{
var shaderAction = ScriptableObject.CreateInstance< CustomTextureShaderAction >();
var shaderTemplate = Resources.Load(customTextureShaderTemplate, typeof(Shader));
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, shaderAction,
$"New Custom Texture Shader.shader",
EditorGUIUtility.ObjectContent(null, typeof(Shader)).image as Texture2D,
AssetDatabase.GetAssetPath(shaderTemplate)
);
}
[MenuItem("Assets/Create/Mixture/C# Compute Shader Node", false, 300)]
public static void CreateCSharpComuteShaderNode()
{
var template = Resources.Load< TextAsset >(csharpComputeShaderNodeTemplate);
ProjectWindowUtil.CreateScriptAssetFromTemplateFile(AssetDatabase.GetAssetPath(template), computeShaderNodeDefaultName);
}
[MenuItem("Assets/Create/Mixture/Compute Shader", false, 301)]
public static void CreateComuteShaderFile()
{
var template = Resources.Load< TextAsset >(computeShaderTemplate);
ProjectWindowUtil.CreateScriptAssetFromTemplateFile(AssetDatabase.GetAssetPath(template), computeShaderDefaultName);
}
[MenuItem("Assets/Create/Mixture/C# Mixture Node", false, 401)]
public static void CreateCSharpMixtureNodeFile()
{
var template = Resources.Load< TextAsset >(cSharpMixtureNodeTemplate);
ProjectWindowUtil.CreateScriptAssetFromTemplateFile(AssetDatabase.GetAssetPath(template), cSharpMixtureNodeName);
}
[MenuItem("Assets/Create/Mixture/C# Mixture Node View", false, 402)]
public static void CreateCSharpMixtureViewNodeFile()
{
var template = Resources.Load< TextAsset >(cSharpMixtureNodeViewTemplate);
ProjectWindowUtil.CreateScriptAssetFromTemplateFile(AssetDatabase.GetAssetPath(template), cSharpMixtureNodeViewName);
}
[OnOpenAsset(0)]
public static bool OnBaseGraphOpened(int instanceID, int line)
{
var asset = EditorUtility.InstanceIDToObject(instanceID);
if (asset is Texture)
{
// Check if the CustomRenderTexture we're opening is a Mixture graph
var path = AssetDatabase.GetAssetPath(EditorUtility.InstanceIDToObject(instanceID));
var graph = MixtureEditorUtils.GetGraphAtPath(path);
var variant = MixtureEditorUtils.GetVariantAtPath(path);
graph ??= variant?.parentGraph;
if (graph == null)
return false;
MixtureGraphWindow.Open(graph);
return true;
}
else if (asset is MixtureVariant variant)
{
MixtureGraphWindow.Open(variant.parentGraph);
}
return false;
}
abstract class MixtureGraphAction : EndNameEditAction
{
public abstract MixtureGraph CreateMixtureGraphAsset();
public override void Action(int instanceId, string pathName, string resourceFile)
{
var mixture = CreateMixtureGraphAsset();
mixture.name = Path.GetFileNameWithoutExtension(pathName);
mixture.hideFlags = HideFlags.HideInHierarchy;
AssetDatabase.CreateAsset(mixture, pathName);
// Generate the output texture:
mixture.outputTextures.Clear();
if (mixture.type == MixtureGraphType.Realtime)
{
mixture.UpdateRealtimeAssetsOnDisk();
}
else
{
MixtureGraphProcessor.RunOnce(mixture);
mixture.SaveAllTextures(false);
}
ProjectWindowUtil.ShowCreatedAsset(mixture.mainOutputTexture);
Selection.activeObject = mixture.mainOutputTexture;
EditorApplication.delayCall += () => EditorGUIUtility.PingObject(mixture.mainOutputTexture);
}
}
class StaticMixtureGraphAction : MixtureGraphAction
{
public static readonly string template = $"{MixtureEditorUtils.mixtureEditorResourcesPath}Templates/StaticMixtureGraphTemplate.asset";
// By default isRealtime is false so we don't need to initialize it like in the realtime mixture create function
public override MixtureGraph CreateMixtureGraphAsset()
{
var g = MixtureEditorUtils.GetGraphAtPath(template);
g = ScriptableObject.Instantiate(g) as MixtureGraph;
g.ClearObjectReferences();
foreach (var node in g.nodes)
{
// Duplicate all the materials from the template
if (node is ShaderNode s && s.material != null)
{
s.material = new Material(s.material);
s.material.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
}
else if (node is OutputNode outputNode)
{
foreach (var outputSettings in outputNode.outputTextureSettings)
{
outputSettings.finalCopyMaterial = new Material(outputSettings.finalCopyMaterial);
outputSettings.finalCopyMaterial.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
}
}
}
return g;
}
}
class RealtimeMixtureGraphAction : MixtureGraphAction
{
public static readonly string template = $"{MixtureEditorUtils.mixtureEditorResourcesPath}Templates/RealtimeMixtureGraphTemplate.asset";
public override MixtureGraph CreateMixtureGraphAsset()
{
var g = MixtureEditorUtils.GetGraphAtPath(template);
g = ScriptableObject.Instantiate(g) as MixtureGraph;
g.ClearObjectReferences();
foreach (var node in g.nodes)
{
// Duplicate all the materials from the template
if (node is ShaderNode s && s.material != null)
{
s.material = new Material(s.material);
s.material.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
}
else if (node is OutputNode outputNode)
{
foreach (var outputSettings in outputNode.outputTextureSettings)
{
outputSettings.finalCopyMaterial = new Material(outputSettings.finalCopyMaterial);
outputSettings.finalCopyMaterial.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
}
}
}
return g;
}
}
class MixtureVariantAction : EndNameEditAction
{
public MixtureGraph targetGraph;
public MixtureVariant parentVariant;
public override void Action(int instanceId, string pathName, string resourceFile)
{
var variant = ScriptableObject.CreateInstance<MixtureVariant>();
if (parentVariant != null)
variant.SetParent(parentVariant);
else
variant.SetParent(targetGraph);
variant.name = Path.GetFileNameWithoutExtension(pathName);
variant.hideFlags = HideFlags.HideInHierarchy;
AssetDatabase.CreateAsset(variant, pathName);
if (parentVariant != null)
variant.CopyTexturesFromParentVariant();
else
variant.CopyTexturesFromGraph();
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
EditorApplication.delayCall += () => {
var mainAsset = AssetDatabase.LoadAssetAtPath<Texture>(pathName);
Selection.activeObject = mainAsset;
EditorGUIUtility.PingObject(mainAsset);
};
}
}
class CustomTextureShaderAction : EndNameEditAction
{
static MethodInfo createScriptAsset = typeof(ProjectWindowUtil).GetMethod("CreateScriptAssetFromTemplate", BindingFlags.Static | BindingFlags.NonPublic);
public override void Action(int instanceId, string pathName, string resourceFile)
{
if (!File.Exists(resourceFile))
{
Debug.LogError("Can't find template: " + resourceFile);
return ;
}
createScriptAsset.Invoke(null, new object[]{ pathName, resourceFile });
ProjectWindowUtil.ShowCreatedAsset(AssetDatabase.LoadAssetAtPath<Shader>(pathName));
AssetDatabase.Refresh();
}
}
static Shader ReimportShaderGraphResource(string resourcePath)
{
string fullPath = "Packages/com.alelievr.mixture/Editor/Resources/" + resourcePath;
AssetDatabase.ImportAsset(fullPath, ImportAssetOptions.DontDownloadFromCacheServer | ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
return Resources.Load< Shader >(resourcePath);
}
class CustomtextureShaderGraphAction : EndNameEditAction
{
public static readonly string template = "Templates/CustomTextureGraphTemplate";
public override void Action(int instanceId, string pathName, string resourceFile)
{
var s = Resources.Load(template, typeof(Shader));
// In case there was a compilation error sg files can be broken so we re-import them
if (s == null)
s = ReimportShaderGraphResource(template);
AssetDatabase.CopyAsset(AssetDatabase.GetAssetPath(s), pathName);
ProjectWindowUtil.ShowCreatedAsset(AssetDatabase.LoadAssetAtPath<Shader>(pathName));
}
}
}
}