ShaderVariantCollectionManifest.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using UnityEngine;
  7. using UnityEngine.Rendering;
  8. using UnityEditor;
  9. namespace YooAsset.Editor
  10. {
  11. public static class ShaderVariantCollectionReadme
  12. {
  13. [Serializable]
  14. public class ShaderVariantElement
  15. {
  16. /// <summary>
  17. /// Pass type to use in this variant.
  18. /// </summary>
  19. public PassType PassType;
  20. /// <summary>
  21. /// Array of shader keywords to use in this variant.
  22. /// </summary>
  23. public string[] Keywords;
  24. }
  25. [Serializable]
  26. public class ShaderVariantInfo
  27. {
  28. /// <summary>
  29. /// Shader asset path in editor.
  30. /// </summary>
  31. public string AssetPath;
  32. /// <summary>
  33. /// Shader name.
  34. /// </summary>
  35. public string ShaderName;
  36. /// <summary>
  37. /// Shader variants elements list.
  38. /// </summary>
  39. public List<ShaderVariantElement> ShaderVariantElements = new List<ShaderVariantElement>(1000);
  40. }
  41. [Serializable]
  42. public class ShaderVariantCollectionManifest
  43. {
  44. /// <summary>
  45. /// Number of shaders in this collection
  46. /// </summary>
  47. public int ShaderTotalCount;
  48. /// <summary>
  49. /// Number of total varians in this collection
  50. /// </summary>
  51. public int VariantTotalCount;
  52. /// <summary>
  53. /// Shader variants info list.
  54. /// </summary>
  55. public List<ShaderVariantInfo> ShaderVariantInfos = new List<ShaderVariantInfo>(1000);
  56. /// <summary>
  57. /// 添加着色器变种信息
  58. /// </summary>
  59. public void AddShaderVariant(string assetPath, string shaderName, PassType passType, string[] keywords)
  60. {
  61. var info = GetOrCreateShaderVariantInfo(assetPath, shaderName);
  62. ShaderVariantElement element = new ShaderVariantElement();
  63. element.PassType = passType;
  64. element.Keywords = keywords;
  65. info.ShaderVariantElements.Add(element);
  66. }
  67. private ShaderVariantInfo GetOrCreateShaderVariantInfo(string assetPath, string shaderName)
  68. {
  69. var selectList = ShaderVariantInfos.Where(t => t.ShaderName == shaderName && t.AssetPath == assetPath).ToList();
  70. if (selectList.Count == 0)
  71. {
  72. ShaderVariantInfo newInfo = new ShaderVariantInfo();
  73. newInfo.AssetPath = assetPath;
  74. newInfo.ShaderName = shaderName;
  75. ShaderVariantInfos.Add(newInfo);
  76. return newInfo;
  77. }
  78. if (selectList.Count != 1)
  79. throw new Exception("Should never get here !");
  80. return selectList[0];
  81. }
  82. }
  83. /// <summary>
  84. /// 解析SVC文件并将数据写入到清单
  85. /// </summary>
  86. public static ShaderVariantCollectionManifest Extract(ShaderVariantCollection svc)
  87. {
  88. var manifest = new ShaderVariantCollectionManifest();
  89. manifest.ShaderTotalCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionShaderCount();
  90. manifest.VariantTotalCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionVariantCount();
  91. using (var so = new SerializedObject(svc))
  92. {
  93. var shaderArray = so.FindProperty("m_Shaders.Array");
  94. if (shaderArray != null && shaderArray.isArray)
  95. {
  96. for (int i = 0; i < shaderArray.arraySize; ++i)
  97. {
  98. var shaderRef = shaderArray.FindPropertyRelative($"data[{i}].first");
  99. var shaderVariantsArray = shaderArray.FindPropertyRelative($"data[{i}].second.variants");
  100. if (shaderRef != null && shaderRef.propertyType == SerializedPropertyType.ObjectReference && shaderVariantsArray != null && shaderVariantsArray.isArray)
  101. {
  102. var shader = shaderRef.objectReferenceValue as Shader;
  103. if (shader == null)
  104. {
  105. throw new Exception("Invalid shader in ShaderVariantCollection file.");
  106. }
  107. string shaderAssetPath = AssetDatabase.GetAssetPath(shader);
  108. string shaderName = shader.name;
  109. // 添加变种信息
  110. for (int j = 0; j < shaderVariantsArray.arraySize; ++j)
  111. {
  112. var propKeywords = shaderVariantsArray.FindPropertyRelative($"Array.data[{j}].keywords");
  113. var propPassType = shaderVariantsArray.FindPropertyRelative($"Array.data[{j}].passType");
  114. if (propKeywords != null && propPassType != null && propKeywords.propertyType == SerializedPropertyType.String)
  115. {
  116. string[] keywords = propKeywords.stringValue.Split(' ');
  117. PassType pathType = (PassType)propPassType.intValue;
  118. manifest.AddShaderVariant(shaderAssetPath, shaderName, pathType, keywords);
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. return manifest;
  126. }
  127. }
  128. }