Skip to content

Commit dcf0894

Browse files
Esmeralda SalamoneEvergreen
authored andcommitted
Sg/template filtering
1 parent a882f22 commit dcf0894

15 files changed

Lines changed: 379 additions & 17 deletions

Packages/com.unity.shadergraph/Editor/Generation/SubTarget.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public virtual bool ValidateNodeCompatibility(AbstractMaterialNode node, out str
3232

3333
// Call after SubTarget parent Target has been deserialized and Subtarget.target has been set to a non-null value.
3434
internal virtual void OnAfterParentTargetDeserialized() { }
35+
36+
virtual internal IEnumerable<(string key, string value)> SearchTerms => null;
3537
}
3638

3739
[GenerationAPI] // TODO: Public

Packages/com.unity.shadergraph/Editor/Generation/Target.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,8 @@ public virtual bool DerivativeModificationCallback(
5454
public abstract bool WorksWithSRP(RenderPipelineAsset scriptableRenderPipeline);
5555

5656
virtual public SubTarget activeSubTarget { get; set; }
57+
58+
// For synonyms, add multiple values for each key.
59+
virtual internal IEnumerable<(string key, string value)> SearchTerms => null;
5760
}
5861
}

Packages/com.unity.shadergraph/Editor/Importers/ShaderGraphImporter.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using System.Text;
88
using UnityEditor.AssetImporters;
9+
using UnityEditor.Experimental.GraphView;
910
using UnityEditor.Graphing;
1011
using UnityEditor.Graphing.Util;
1112
using UnityEditor.ShaderGraph.Internal;
@@ -76,6 +77,9 @@ Fallback Off
7677
[SerializeField]
7778
bool m_ExposeTemplateAsShader;
7879

80+
[SerializeField]
81+
ShaderGraphIndexedData m_IndexedData;
82+
7983
public bool UseAsTemplate
8084
{
8185
get => m_UseAsTemplate;
@@ -97,6 +101,8 @@ public ShaderGraphTemplate Template
97101
set => m_Template = value;
98102
}
99103

104+
public DataBag AdditionalSearchTerms => m_IndexedData?.AdditionalSeachTerms ?? default;
105+
100106
public static Texture2D GetIcon() => EditorGUIUtility.IconContent(IconBasePath)?.image as Texture2D;
101107

102108
[SuppressMessage("ReSharper", "UnusedMember.Local")]
@@ -371,6 +377,12 @@ public override void OnImportAsset(AssetImportContext ctx)
371377

372378
ctx.AddObjectToAsset("SGInternal:Metadata", sgMetadata);
373379

380+
m_IndexedData = ScriptableObject.CreateInstance<ShaderGraphIndexedData>();
381+
m_IndexedData.AdditionalSeachTerms = ShaderGraphTemplate.GatherSearchTerms(graph);
382+
m_IndexedData.name = "Indexed Data";
383+
m_IndexedData.hideFlags = HideFlags.HideInHierarchy;
384+
ctx.AddObjectToAsset("Metadata", m_IndexedData);
385+
374386
// declare dependencies
375387
foreach (var asset in assetCollection.assets)
376388
{

Packages/com.unity.shadergraph/Editor/Importers/ShaderGraphImporter.cs.meta

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
using UnityEngine;
4+
5+
namespace UnityEditor.ShaderGraph
6+
{
7+
[Serializable]
8+
class ShaderGraphIndexedData : ScriptableObject
9+
{
10+
[SerializeField] internal Experimental.GraphView.DataBag additionalSearchTerms;
11+
12+
13+
public Experimental.GraphView.DataBag AdditionalSeachTerms
14+
{
15+
get => additionalSearchTerms;
16+
set => additionalSearchTerms = value;
17+
}
18+
}
19+
}

Packages/com.unity.shadergraph/Editor/Importers/ShaderGraphIndexedData.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.unity.shadergraph/Editor/Importers/ShaderGraphIndexerExtension.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@ namespace UnityEditor.ShaderGraph
66
{
77
static class ShaderGraphIndexerExtension
88
{
9-
[CustomObjectIndexer(typeof(Shader), version = 0)]
10-
internal static void ShaderGraphImporterIndexer(CustomObjectIndexerTarget context, ObjectIndexer indexer)
9+
static void ShaderGraphIndexer(CustomObjectIndexerTarget context, ObjectIndexer indexer)
1110
{
12-
if (ShaderGraphTemplateHelper.TryGetTemplateStatic(context.id, out var template))
11+
if (ShaderGraphTemplateHelper.TryGetTemplateStatic(context.id, out var template, out var dataBag))
1312
{
1413
GraphViewIndexerExtension.IndexCommonData<ShaderGraphImporter>(context, indexer, template);
14+
GraphViewIndexerExtension.IndexCustomData<ShaderGraphImporter>(context, indexer, dataBag.GetCustomData($"{template.ToolKey}."));
1515
}
1616
}
17+
18+
[CustomObjectIndexer(typeof(ShaderGraphImporter), version = 1)]
19+
internal static void ShaderGraphImporterIndexer(CustomObjectIndexerTarget context, ObjectIndexer indexer)
20+
=> ShaderGraphIndexer(context, indexer);
21+
22+
[CustomObjectIndexer(typeof(Shader), version = 1)]
23+
internal static void ShaderGraphShaderIndexer(CustomObjectIndexerTarget context, ObjectIndexer indexer)
24+
=> ShaderGraphIndexer(context, indexer);
1725
}
1826
}

Packages/com.unity.shadergraph/Editor/Importers/ShaderGraphIndexerExtension.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.unity.shadergraph/Editor/Importers/ShaderGraphTemplate.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using UnityEditor.ShaderGraph.Serialization;
23
using UnityEngine;
34

45
namespace UnityEditor.ShaderGraph
@@ -12,5 +13,40 @@ struct ShaderGraphTemplate
1213
public string description;
1314
public Texture2D icon;
1415
public Texture2D thumbnail;
16+
public int order;
17+
18+
internal static Experimental.GraphView.DataBag GatherSearchTerms(GraphData graph)
19+
{
20+
// clear the existing terms, as this object's serialization persists across imports.
21+
var additionalSearchTerms = default(Experimental.GraphView.DataBag);
22+
23+
if (graph is null || graph.objectIdIsEmpty)
24+
return additionalSearchTerms;
25+
26+
foreach (var target in graph.activeTargets)
27+
{
28+
if (target is null or MultiJsonInternal.UnknownTargetType || target.objectIdIsEmpty || target.activeSubTarget is null || target.activeSubTarget.objectIdIsEmpty)
29+
continue;
30+
31+
additionalSearchTerms.AddCustomData("RenderPipeline", target.displayName);
32+
additionalSearchTerms.AddCustomData("Material", target.activeSubTarget.displayName);
33+
additionalSearchTerms.AddCustomData("VFX", target.SupportsVFX() ? "Supported" : "Not Supported");
34+
35+
if (target.SearchTerms != null)
36+
foreach (var term in target.SearchTerms)
37+
additionalSearchTerms.AddCustomData(term.key, term.value);
38+
39+
if (target.activeSubTarget.SearchTerms != null)
40+
foreach (var term in target.activeSubTarget.SearchTerms)
41+
additionalSearchTerms.AddCustomData(term.key, term.value);
42+
}
43+
44+
foreach (var subData in graph.SubDatas)
45+
if (subData == null || subData is MultiJsonInternal.UnknownGraphDataExtension || subData.objectIdIsEmpty || string.IsNullOrEmpty(subData.displayName))
46+
continue;
47+
else additionalSearchTerms.AddCustomData("DataExtension", subData.displayName);
48+
49+
return additionalSearchTerms;
50+
}
1551
}
1652
}

Packages/com.unity.shadergraph/Editor/Importers/ShaderGraphTemplateHelper.cs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ namespace UnityEditor.ShaderGraph
1010
{
1111
class ShaderGraphTemplateHelper : ITemplateHelper
1212
{
13-
const string k_TemplateBasePath = "Packages/com.unity.shadergraph/GraphTemplates";
14-
const string k_BuiltInTemplatePath = k_TemplateBasePath + "/Default";
15-
1613
class SaveFileDialog : GraphViewTemplateWindow.ISaveFileDialogHelper
1714
{
1815
public string OpenSaveFileDialog()
@@ -27,8 +24,8 @@ public string OpenSaveFileDialog()
2724
public string packageInfoName => "Shader Graph";
2825
public string learningSampleName => string.Empty;
2926
public string templateWindowDocUrl => Documentation.GetPageLink("index");
30-
public string builtInTemplatePath => k_BuiltInTemplatePath;
31-
public string builtInCategory => "Default Shader Graph Templates";
27+
public string builtInTemplatePath => string.Empty;
28+
public string builtInCategory => string.Empty;
3229
public Type assetType => typeof(Shader);
3330
public string createNewAssetTitle => "Create new Shader Graph Asset";
3431
public string insertTemplateTitle => "Insert a template into current Shader Graph Asset";
@@ -43,11 +40,12 @@ public void RaiseImportSampleDependencies(PackageManager.PackageInfo packageInfo
4340
public void RaiseTemplateUsed(GraphViewTemplateDescriptor usedTemplate) =>
4441
ShaderGraphAnalytics.SendShaderGraphTemplateEvent(usedTemplate);
4542

46-
public bool TryGetTemplate(string assetPath, out GraphViewTemplateDescriptor graphViewTemplate) => TryGetTemplateStatic(assetPath, out graphViewTemplate);
47-
internal static bool TryGetTemplateStatic(string assetPath, out GraphViewTemplateDescriptor graphViewTemplate)
43+
public bool TryGetTemplate(string assetPath, out GraphViewTemplateDescriptor graphViewTemplate) => TryGetTemplateStatic(assetPath, out graphViewTemplate, out _);
44+
internal static bool TryGetTemplateStatic(string assetPath, out GraphViewTemplateDescriptor graphViewTemplate, out DataBag dataBag)
4845
{
4946
if (FileUtilities.TryGetImporter(assetPath, out var importer))
5047
{
48+
dataBag = importer.AdditionalSearchTerms;
5149
var template = importer.Template;
5250
if (importer.UseAsTemplate)
5351
{
@@ -79,14 +77,43 @@ public bool TrySetTemplate(string assetPath, GraphViewTemplateDescriptor graphVi
7977
description = graphViewTemplate.description,
8078
icon = graphViewTemplate.icon,
8179
thumbnail = graphViewTemplate.thumbnail,
80+
order = graphViewTemplate.order,
8281
};
8382
importer.Template = template;
8483
return true;
8584
}
8685
return false;
8786
}
8887

88+
internal static bool TryGetTemplateDescriptor(string assetPath, out GraphViewTemplateDescriptor graphViewTemplate)
89+
{
90+
if (FileUtilities.TryGetImporter(assetPath, out var importer))
91+
{
92+
var template = importer.Template;
93+
if (importer.UseAsTemplate)
94+
{
95+
var templateName = !string.IsNullOrEmpty(template.name) ? template.name : Path.GetFileNameWithoutExtension(assetPath);
96+
graphViewTemplate = new GraphViewTemplateDescriptor(ShaderGraphToolKey)
97+
{
98+
name = templateName,
99+
category = template.category,
100+
description = template.description,
101+
icon = template.icon,
102+
thumbnail = template.thumbnail,
103+
order = template.order,
104+
};
105+
return true;
106+
}
107+
}
108+
graphViewTemplate = default;
109+
return false;
110+
}
111+
89112
public ITemplateSorter[] GetTemplateSorter() => Array.Empty<ITemplateSorter>();
90-
public SearchProposition[] GetSearchPropositions() => Array.Empty<SearchProposition>();
113+
114+
public SearchProposition[] GetSearchPropositions()
115+
{
116+
return Array.Empty<SearchProposition>();
117+
}
91118
}
92119
}

0 commit comments

Comments
 (0)