-
-
Save Nobuyuki-Kobayashi/70acf028f0e6bcafe4c937bdebe86a27 to your computer and use it in GitHub Desktop.
プロジェクト内にあるShaderVariantCollectionにないKeywordはビルドに含まないようにするEditor拡張です
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
/* | |
MIT License | |
Copyright (c) 2020 Yusuke Kurokawa | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
using UnityEditor.Rendering; | |
using UnityEditor.Build; | |
using UnityEngine.Rendering; | |
public class StrippingByVariantCollection : IPreprocessShaders | |
{ | |
private List<ShaderVariantCollection.ShaderVariant> shaderVariants; | |
public StrippingByVariantCollection() | |
{ | |
var variantCollections = GetProjectShaderVariantCollections(); | |
this.shaderVariants = new List<ShaderVariantCollection.ShaderVariant>(); | |
foreach (var variantCollection in variantCollections) { | |
CollectVariants(this.shaderVariants, variantCollection); | |
} | |
} | |
private static List<ShaderVariantCollection> GetProjectShaderVariantCollections() | |
{ | |
List<ShaderVariantCollection> collections = new List<ShaderVariantCollection>(); | |
var guids = AssetDatabase.FindAssets("t: ShaderVariantCollection"); | |
foreach (var guid in guids) | |
{ | |
var path = AssetDatabase.GUIDToAssetPath(guid); | |
var obj = AssetDatabase.LoadAssetAtPath<ShaderVariantCollection>(path); | |
if (obj != null) | |
{ | |
collections.Add(obj); | |
} | |
} | |
return collections; | |
} | |
private void CollectVariants (List<ShaderVariantCollection.ShaderVariant> shaderVariants, | |
ShaderVariantCollection variantCollection){ | |
var obj = new SerializedObject(variantCollection); | |
var shadersProp = obj.FindProperty("m_Shaders"); | |
for( int i = 0;i < shadersProp.arraySize; ++i) | |
{ | |
var shaderProp = shadersProp.GetArrayElementAtIndex(i); | |
var shader = shaderProp.FindPropertyRelative("first").objectReferenceValue as Shader; | |
var variantsProp = shaderProp.FindPropertyRelative("second.variants"); | |
CollectVariants(shaderVariants, shader, variantsProp); | |
} | |
} | |
private void CollectVariants(List<ShaderVariantCollection.ShaderVariant> shaderVariants, Shader shader , SerializedProperty variantsProp) | |
{ | |
for( int i = 0; i < variantsProp.arraySize;++i) | |
{ | |
var variantProp = variantsProp.GetArrayElementAtIndex(i); | |
var keywords = variantProp.FindPropertyRelative("keywords").stringValue; | |
var passType = variantProp.FindPropertyRelative("passType").intValue; | |
ShaderVariantCollection.ShaderVariant variant = new ShaderVariantCollection.ShaderVariant(); | |
variant.shader = shader; | |
if (keywords != null) | |
{ | |
keywords = keywords.Trim(); | |
} | |
if (string.IsNullOrEmpty(keywords)) | |
{ | |
variant.keywords = new string[] { "" }; | |
} | |
else | |
{ | |
variant.keywords = keywords.Split(' '); | |
} | |
variant.passType = (PassType)passType; | |
shaderVariants.Add(variant); | |
} | |
} | |
private bool IsExistShader(List<ShaderVariantCollection.ShaderVariant> shaderVariants,Shader shader) | |
{ | |
foreach( var variant in shaderVariants) | |
{ | |
if( variant.shader == shader){ | |
return true; | |
} | |
} | |
return false; | |
} | |
private bool IsExist(List<ShaderVariantCollection.ShaderVariant> shaderVariants, | |
Shader shader, ShaderSnippetData snippet, ShaderCompilerData data) | |
{ | |
var keywords = data.shaderKeywordSet.GetShaderKeywords(); | |
var compiledKeyword = Convert(keywords); | |
if (compiledKeyword.Length == 0) | |
{ | |
return true; | |
} | |
foreach (var variant in shaderVariants) | |
{ | |
if (variant.shader != shader) | |
{ | |
continue; | |
} | |
if( variant.passType != snippet.passType ){ | |
continue; | |
} | |
System.Array.Sort(variant.keywords); | |
if( IsMatch(variant.keywords, compiledKeyword) ){ | |
#if true | |
string str = shader.name + ":"; | |
foreach( var w in compiledKeyword) | |
{ | |
str+=w+" "; | |
} | |
Debug.Log(str); | |
#endif | |
return true; | |
} | |
} | |
return false; | |
} | |
private bool IsMatch(string[] a,string[] b) | |
{ | |
if(a.Length != b.Length) { return false; } | |
for (int i = 0; i< a.Length; ++i) | |
{ | |
if(a[i] != b[i]) { return false; } | |
} | |
return true; | |
} | |
private string[] Convert(ShaderKeyword[] keywords) | |
{ | |
string[] converted = new string[keywords.Length]; | |
for( int i = 0; i < converted.Length; ++i) | |
{ | |
converted[i] = keywords[i].GetKeywordName(); | |
} | |
System.Array.Sort(converted); | |
return converted; | |
} | |
public int callbackOrder | |
{ | |
get | |
{ | |
return 0; | |
} | |
} | |
public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList<ShaderCompilerData> shaderCompilerData) | |
{ | |
for (int i = 0; i < shaderCompilerData.Count; ++i) | |
{ | |
bool shouldRemove = false; | |
if (IsExistShader(this.shaderVariants, shader)) | |
{ | |
bool isExists = IsExist(this.shaderVariants, shader, snippet, shaderCompilerData[i]); | |
shouldRemove = !isExists; | |
} | |
if (shouldRemove) | |
{ | |
shaderCompilerData.RemoveAt(i); | |
--i; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment