update shader variant collector

着色器变种收集增加分批处理功能。
pull/62/head
hevinci 2023-02-01 19:32:37 +08:00
parent df27e7ba75
commit 3672a7e1fa
2 changed files with 52 additions and 18 deletions

View File

@ -18,24 +18,26 @@ namespace YooAsset.Editor
Prepare, Prepare,
CollectAllMaterial, CollectAllMaterial,
CollectVariants, CollectVariants,
CollectVariantsFinish, CollectSleeping,
WaitingDone, WaitingDone,
} }
private const float WaitMilliseconds = 1000f; private const float WaitMilliseconds = 1000f;
private static string _savePath; private static string _savePath;
private static string _packageName; private static string _packageName;
private static int _processMaxNum;
private static Action _completedCallback; private static Action _completedCallback;
private static ESteps _steps = ESteps.None; private static ESteps _steps = ESteps.None;
private static Stopwatch _elapsedTime; private static Stopwatch _elapsedTime;
private static List<string> _allMaterials; private static List<string> _allMaterials;
private static List<GameObject> _allSpheres = new List<GameObject>(1000);
/// <summary> /// <summary>
/// 开始收集 /// 开始收集
/// </summary> /// </summary>
public static void Run(string savePath, string packageName, Action completedCallback) public static void Run(string savePath, string packageName, int processMaxNum, Action completedCallback)
{ {
if (_steps != ESteps.None) if (_steps != ESteps.None)
return; return;
@ -52,6 +54,7 @@ namespace YooAsset.Editor
EditorTools.CreateFileDirectory(savePath); EditorTools.CreateFileDirectory(savePath);
_savePath = savePath; _savePath = savePath;
_packageName = packageName; _packageName = packageName;
_processMaxNum = processMaxNum;
_completedCallback = completedCallback; _completedCallback = completedCallback;
// 聚焦到游戏窗口 // 聚焦到游戏窗口
@ -73,28 +76,43 @@ namespace YooAsset.Editor
{ {
ShaderVariantCollectionHelper.ClearCurrentShaderVariantCollection(); ShaderVariantCollectionHelper.ClearCurrentShaderVariantCollection();
_steps = ESteps.CollectAllMaterial; _steps = ESteps.CollectAllMaterial;
return; return; //等待一帧
} }
if (_steps == ESteps.CollectAllMaterial) if (_steps == ESteps.CollectAllMaterial)
{ {
_allMaterials = GetAllMaterials(); _allMaterials = GetAllMaterials();
_steps = ESteps.CollectVariants; _steps = ESteps.CollectVariants;
return; return; //等待一帧
} }
if (_steps == ESteps.CollectVariants) if (_steps == ESteps.CollectVariants)
{ {
CollectVariants(_allMaterials); int count = Mathf.Min(_processMaxNum, _allMaterials.Count);
_steps = ESteps.CollectVariantsFinish; List<string> range = _allMaterials.GetRange(0, count);
return; _allMaterials.RemoveRange(0, count);
CollectVariants(range);
if (_allMaterials.Count > 0)
{
_elapsedTime = Stopwatch.StartNew();
_steps = ESteps.CollectSleeping;
}
else
{
_elapsedTime = Stopwatch.StartNew();
_steps = ESteps.WaitingDone;
}
} }
if (_steps == ESteps.CollectVariantsFinish) if (_steps == ESteps.CollectSleeping)
{ {
_elapsedTime = Stopwatch.StartNew(); if (_elapsedTime.ElapsedMilliseconds > WaitMilliseconds)
_steps = ESteps.WaitingDone; {
return; DestroyAllSpheres();
_elapsedTime.Stop();
_steps = ESteps.CollectVariants;
}
} }
if (_steps == ESteps.WaitingDone) if (_steps == ESteps.WaitingDone)
@ -180,7 +198,9 @@ namespace YooAsset.Editor
{ {
var material = materials[i]; var material = materials[i];
var position = new Vector3(x - halfWidth + 1f, y - halfHeight + 1f, 0f); var position = new Vector3(x - halfWidth + 1f, y - halfHeight + 1f, 0f);
CreateSphere(material, position, i); var go = CreateSphere(material, position, i);
if (go != null)
_allSpheres.Add(go);
if (x == xMax) if (x == xMax)
{ {
x = 0; x = 0;
@ -190,21 +210,35 @@ namespace YooAsset.Editor
{ {
x++; x++;
} }
EditorTools.DisplayProgressBar("测试所有材质球", ++progressValue, materials.Count); EditorTools.DisplayProgressBar("照射所有材质球", ++progressValue, materials.Count);
} }
EditorTools.ClearProgressBar(); EditorTools.ClearProgressBar();
} }
private static void CreateSphere(string assetPath, Vector3 position, int index) private static GameObject CreateSphere(string assetPath, Vector3 position, int index)
{ {
var material = AssetDatabase.LoadAssetAtPath<Material>(assetPath); var material = AssetDatabase.LoadAssetAtPath<Material>(assetPath);
var shader = material.shader; var shader = material.shader;
if (shader == null) if (shader == null)
return; return null;
var go = GameObject.CreatePrimitive(PrimitiveType.Sphere); var go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
go.GetComponent<Renderer>().material = material; go.GetComponent<Renderer>().sharedMaterial = material;
go.transform.position = position; go.transform.position = position;
go.name = $"Sphere_{index} | {material.name}"; go.name = $"Sphere_{index} | {material.name}";
return go;
}
private static void DestroyAllSpheres()
{
foreach(var go in _allSpheres)
{
var mat = go.GetComponent<Renderer>().sharedMaterial;
Resources.UnloadAsset(mat);
GameObject.DestroyImmediate(go);
}
_allSpheres.Clear();
// 尝试释放编辑器加载的资源
Resources.UnloadUnusedAssets();
} }
private static void CreateManifest() private static void CreateManifest()
{ {

View File

@ -103,7 +103,7 @@ namespace YooAsset.Editor
{ {
string savePath = ShaderVariantCollectorSettingData.Setting.SavePath; string savePath = ShaderVariantCollectorSettingData.Setting.SavePath;
string packageName = ShaderVariantCollectorSettingData.Setting.CollectPackage; string packageName = ShaderVariantCollectorSettingData.Setting.CollectPackage;
ShaderVariantCollector.Run(savePath, packageName, null); ShaderVariantCollector.Run(savePath, packageName, int.MaxValue, null);
} }
// 构建包裹相关 // 构建包裹相关