182 lines
6.2 KiB
C#
182 lines
6.2 KiB
C#
using System.Linq;
|
||
using System.Reflection;
|
||
using UnityEngine;
|
||
using UnityEditor;
|
||
using static UnityEngine.ShaderVariantCollection;
|
||
using UnityEngine.Rendering;
|
||
using System.Collections.Generic;
|
||
|
||
namespace BFEditor.Resource
|
||
{
|
||
public static class ShaderVariantsTools
|
||
{
|
||
// 手动添加的shader变体信息
|
||
static Dictionary<string, List<VariantInfo>> manualInfoDict = new Dictionary<string, List<VariantInfo>>()
|
||
{
|
||
};
|
||
|
||
// 忽略的shader,不添加到变体收集中
|
||
static List<string> ingoreShaderList = new List<string>()
|
||
{
|
||
"TextMeshPro", // 整理好了再收集
|
||
};
|
||
|
||
class VariantInfo
|
||
{
|
||
public PassType passType;
|
||
public string[] keywords;
|
||
|
||
public VariantInfo(PassType passType, string[] keywords)
|
||
{
|
||
this.passType = passType;
|
||
this.keywords = keywords;
|
||
}
|
||
}
|
||
|
||
static MethodInfo GetShaderVariantEntries = null;
|
||
|
||
static ShaderVariantCollection EnsureColletion()
|
||
{
|
||
var svc = AssetDatabase.LoadAssetAtPath<ShaderVariantCollection>(ResourceProcessConfig.SHADER_VARIANT_COLLECTION);
|
||
if (svc == null)
|
||
{
|
||
svc = new ShaderVariantCollection();
|
||
AssetDatabase.CreateAsset(svc, ResourceProcessConfig.SHADER_VARIANT_COLLECTION);
|
||
}
|
||
return svc;
|
||
}
|
||
|
||
static Shader[] GetShaders()
|
||
{
|
||
var guids = GetBFShaderGUID();
|
||
var shaders = new Shader[guids.Length];
|
||
var index = 0;
|
||
foreach (var guid in guids)
|
||
{
|
||
shaders[index] = AssetDatabase.LoadAssetAtPath<Shader>(AssetDatabase.GUIDToAssetPath(guid));
|
||
index++;
|
||
}
|
||
return shaders;
|
||
}
|
||
|
||
static string[] GetBFShaderGUID()
|
||
{
|
||
return AssetDatabase.FindAssets("t:shader", new string[] { ResourceProcessConfig.SHADER_FOLDER_PATH });
|
||
}
|
||
|
||
static List<VariantInfo> GetShaderVariantInfos(Shader target)
|
||
{
|
||
if (GetShaderVariantEntries == null)
|
||
GetShaderVariantEntries = typeof(ShaderUtil).GetMethod("GetShaderVariantEntries",
|
||
BindingFlags.NonPublic | BindingFlags.Static);
|
||
|
||
int[] passTypes = null;
|
||
string[] allKeywords = null;
|
||
object[] args = new object[] { target, new ShaderVariantCollection(), passTypes, allKeywords };
|
||
GetShaderVariantEntries.Invoke(null, args);
|
||
|
||
passTypes = args[2] as int[];
|
||
allKeywords = args[3] as string[];
|
||
|
||
var infos = new List<VariantInfo>();
|
||
var index = 0;
|
||
foreach (string item in allKeywords)
|
||
{
|
||
var temp = item.Split(' ');
|
||
var keywords = new string[temp.Length];
|
||
var index2 = 0;
|
||
foreach (string t in temp)
|
||
{
|
||
keywords[index2] = t;
|
||
index2++;
|
||
}
|
||
infos.Add(new VariantInfo((PassType)passTypes[index], keywords));
|
||
index++;
|
||
}
|
||
return infos;
|
||
}
|
||
|
||
public static void GenerateVariantCollection(bool needDialog = false)
|
||
{
|
||
// Debug.Log("[bfinfo]收集shader变体...");
|
||
// var svc = EnsureColletion();
|
||
// svc.Clear();
|
||
|
||
// var shaders = GetShaders();
|
||
// var index = 0;
|
||
// var totalCount = shaders.Length;
|
||
// foreach (var shader in shaders)
|
||
// {
|
||
// EditorUtility.DisplayProgressBar("正在收集shader变体", shader.name, (float)(index + 1) / totalCount);
|
||
|
||
// // 收集手动添加的变体信息
|
||
// if (manualInfoDict.TryGetValue(shader.name, out List<VariantInfo> infos))
|
||
// {
|
||
// foreach (var info in infos)
|
||
// {
|
||
// svc.Add(new ShaderVariant(shader, info.passType, info.keywords));
|
||
// }
|
||
// continue;
|
||
// }
|
||
|
||
// // 跳过收集
|
||
// var needContine = false;
|
||
// foreach (var ingore in ingoreShaderList)
|
||
// {
|
||
// if (shader.name.Contains(ingore))
|
||
// {
|
||
// needContine = true;
|
||
// break;
|
||
// }
|
||
// }
|
||
// if (needContine)
|
||
// {
|
||
// continue;
|
||
// }
|
||
|
||
// // 收集所有的变体
|
||
// var variantInfos = GetShaderVariantInfos(shader);
|
||
// if (variantInfos.Count >= 10)
|
||
// {
|
||
// Debug.LogErrorFormat("shader变体收集: 新增shader变体 shaderName : {0} , 变体 count {1}", shader.name, variantInfos.Count);
|
||
// }
|
||
// foreach (var info in variantInfos)
|
||
// {
|
||
// if (info.passType == PassType.Normal || info.passType == PassType.ForwardBase || info.passType == PassType.ShadowCaster)
|
||
// {
|
||
// bool a = false;
|
||
// foreach (var keyword in info.keywords)
|
||
// {
|
||
// if (keyword.Contains("INSTANCING_ON"))
|
||
// {
|
||
// a = true;
|
||
// break;
|
||
// }
|
||
// }
|
||
// if (a)
|
||
// {
|
||
// continue;
|
||
// }
|
||
// svc.Add(new ShaderVariant(shader, info.passType, info.keywords));
|
||
// }
|
||
// }
|
||
|
||
// index++;
|
||
// }
|
||
|
||
// EditorUtility.ClearProgressBar();
|
||
// AssetDatabase.SaveAssets();
|
||
|
||
// if (needDialog)
|
||
// {
|
||
// EditorUtility.DisplayDialog("收集完成", "收集完成", "ok");
|
||
// }
|
||
}
|
||
|
||
public static void GenerateShaderVariantCollection()
|
||
{
|
||
GenerateVariantCollection(true);
|
||
}
|
||
}
|
||
}
|