Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Dec 6, 2024
1 parent 35024fb commit 6a5fdb8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
19 changes: 15 additions & 4 deletions Packages/src/Runtime/Internal/Utilities/ShaderVariantRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ public override int GetHashCode()

public ShaderVariantCollection shaderVariantCollection => m_Asset;

public Shader FindOptionalShader(Shader shader, string format, string defaultOptionalShaderName)
public Shader FindOptionalShader(Shader shader,
string requiredName,
string format,
string defaultOptionalShaderName)
{
if (!shader) return null;

Expand All @@ -72,6 +75,13 @@ public Shader FindOptionalShader(Shader shader, string format, string defaultOpt
return Shader.Find(optionalShaderName);
}

// Required shader.
if (shader.name.Contains(requiredName))
{
_cachedOptionalShaders[id] = shader.name;
return shader;
}

// Find optional shader.
Shader optionalShader;
var shaderName = shader.name;
Expand Down Expand Up @@ -116,7 +126,8 @@ public void InitializeIfNeeded(Object owner, string optionalName)
{
return File.ReadLines(x.path)
.Take(10)
.Select(line => Regex.Match(line, @"//\s*OptionalShaderFor:\s*(.*)$"))
.Where(line => line.Contains($"OptionalShader@{optionalName}"))
.Select(line => Regex.Match(line, @":\s*(.*)$"))
.Where(match => match.Success)
.Select(match => new StringPair() { key = match.Groups[1].Value, value = x.name });
})
Expand Down Expand Up @@ -375,12 +386,12 @@ private static bool DrawRegisteredShaderVariants(bool expand, SerializedProperty
return expand;
}

private static void ShowShaderDropdown(SerializedProperty property, string option, bool included)
private static void ShowShaderDropdown(SerializedProperty property, string option, bool required)
{
var menu = new GenericMenu();
var current = property.stringValue;
var allShaderNames = ShaderUtil.GetAllShaderInfo()
.Where(s => s.name.Contains(option) == included)
.Where(s => !required || s.name.Contains(option))
.Select(s => s.name);

foreach (var shaderName in allShaderNames)
Expand Down
4 changes: 3 additions & 1 deletion Packages/src/Runtime/Utilities/SoftMaskUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ public static Material CreateSoftMaskable(
var mat = new Material(baseMat)
{
shader = UISoftMaskProjectSettings.shaderRegistry.FindOptionalShader(baseMat.shader,
"Hidden/{0} (SoftMaskable)", "Hidden/UI/Default (SoftMaskable)"),
"(SoftMaskable)",
"Hidden/{0} (SoftMaskable)",
"Hidden/UI/Default (SoftMaskable)"),
hideFlags = HideFlags.HideAndDontSave
};
Profiler.EndSample();
Expand Down

0 comments on commit 6a5fdb8

Please sign in to comment.