Skip to content

Instantly share code, notes, and snippets.

@guycalledfrank
Created April 29, 2023 09:08
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save guycalledfrank/567df882922d83f3dfe3c19aeeea8cd1 to your computer and use it in GitHub Desktop.
Save guycalledfrank/567df882922d83f3dfe3c19aeeea8cd1 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Rendering;
using UnityEngine;
using UnityEngine.Rendering;
class ShaderBuildProcessor : IPreprocessShaders
{
ShaderVariantCollection whitelist;
public ShaderBuildProcessor()
{
var whitelistAsset = AssetDatabase.LoadAssetAtPath<ShaderVariantCollection>("Assets/Editor/ShaderBuilding/ShaderWhitelist.shadervariants");
if (whitelistAsset == null)
{
Debug.LogError("Can't find shader whitelist");
return;
}
whitelist = whitelistAsset as ShaderVariantCollection;
if (whitelist == null)
{
Debug.LogError("Can't cast shader whitelist");
return;
}
}
public int callbackOrder { get { return 0; } }
public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList<ShaderCompilerData> data)
{
if (whitelist == null) return;
var shaderVar = new ShaderVariantCollection.ShaderVariant();
shaderVar.shader = shader;
shaderVar.passType = snippet.passType;
int skipped = 0;
for (int i = data.Count - 1; i >= 0; --i)
{
var keywordSet = data[i].shaderKeywordSet.GetShaderKeywords();
var keywords = new List<string>();
for(int j=0; j<keywordSet.Length; j++)
{
var keywordName = ShaderKeyword.GetKeywordName(shader, keywordSet[j]);
keywords.Add(keywordName);
}
shaderVar.keywords = keywords.ToArray();
if (!whitelist.Contains(shaderVar))
{
skipped++;
data.RemoveAt(i);
}
}
Debug.Log("Skipped " + skipped + " variants of " + shader.name + " (" + snippet.passType + ")");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment