Created
April 29, 2023 09:08
-
-
Save guycalledfrank/567df882922d83f3dfe3c19aeeea8cd1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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