-
-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat material unused property remove #1041
Changes from 4 commits
75a3193
865db01
7d5e05f
6dcb061
38e6a5c
82551f4
7d1ee87
d4cf9a0
6b91ec1
930f047
bf72d64
89b0213
9f5e282
9fc6a2a
eda5998
c3ac6e9
1c36150
8542571
bae5ed4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,4 +84,4 @@ protected override void OnInspectorGUIInner() | |
serializedObject.ApplyModifiedProperties(); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System.Linq; | ||
using nadena.dev.ndmf; | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace Anatawa12.AvatarOptimizer.Processors.TraceAndOptimizes | ||
{ | ||
internal class MaterialUnusedPropertyRemove : TraceAndOptimizePass<MaterialUnusedPropertyRemove> | ||
{ | ||
public override string DisplayName => "T&O: MaterialUnusedPropertyRemove"; | ||
protected override void Execute(BuildContext context, TraceAndOptimizeState state) | ||
{ | ||
if (!state.RemoveUnusedObjects) { return; } | ||
if (state.SkipRemoveMaterialUnusedProperties) { return; } | ||
|
||
var renderers = context.GetComponents<Renderer>(); | ||
var swapDict = renderers.SelectMany(i => i.sharedMaterials) | ||
.Distinct().Where(i => i != null) | ||
.ToDictionary(i => i, MaterialCleaning); | ||
|
||
void SwapMaterialArray(Material[] matArray) | ||
{ | ||
for (var i = 0; matArray.Length > i; i += 1) | ||
{ | ||
if (matArray[i] == null) { continue; } | ||
matArray[i] = swapDict[matArray[i]]; | ||
} | ||
} | ||
|
||
foreach (var renderer in renderers) | ||
{ | ||
if (renderer is SkinnedMeshRenderer smr) | ||
{ | ||
var meshInfo = context.GetMeshInfoFor(smr); | ||
foreach (var subMesh in meshInfo.SubMeshes) | ||
SwapMaterialArray(subMesh.SharedMaterials); | ||
} | ||
else | ||
{ | ||
var matArray = renderer.sharedMaterials; | ||
SwapMaterialArray(matArray); | ||
renderer.sharedMaterials = matArray; | ||
} | ||
} | ||
|
||
foreach (var matKv in swapDict) ObjectRegistry.RegisterReplacedObject(matKv.Key, matKv.Value); | ||
} | ||
|
||
static Material MaterialCleaning(Material i) | ||
{ | ||
var mat = UnityEngine.Object.Instantiate(i); | ||
mat.name = i.name + "&AAO_MATERIAL_UNUSED_PROPERTIES_REMOLDED"; | ||
RemoveUnusedProperties(mat); | ||
return mat; | ||
} | ||
|
||
//MIT License | ||
//Copyright (c) 2020-2021 lilxyzw | ||
//https://github.com/lilxyzw/lilToon/blob/b96470d3dd9092b840052578048b2307fe6d8786/Assets/lilToon/Editor/lilMaterialUtils.cs#L658-L686 | ||
// | ||
//https://light11.hatenadiary.com/entry/2018/12/04/224253 | ||
public static void RemoveUnusedProperties(Material material) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. そういえばfallback shaderの対応を考え忘れてる気がする よっぽどのことがないともとシェーダにないプロパティが関係あることないと思うけど There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. カスタム指定ができるliltoonなんかでもOverrideTagでどうにかしてるので、 |
||
{ | ||
var so = new SerializedObject(material); | ||
so.Update(); | ||
var savedProps = so.FindProperty("m_SavedProperties"); | ||
|
||
var texs = savedProps.FindPropertyRelative("m_TexEnvs"); | ||
DeleteUnused(ref texs, material); | ||
|
||
var floats = savedProps.FindPropertyRelative("m_Floats"); | ||
DeleteUnused(ref floats, material); | ||
|
||
var colors = savedProps.FindPropertyRelative("m_Colors"); | ||
DeleteUnused(ref colors, material); | ||
|
||
so.ApplyModifiedProperties(); | ||
} | ||
|
||
public static void DeleteUnused(ref SerializedProperty props, Material material) | ||
{ | ||
for (int i = props.arraySize - 1; i >= 0; i--) | ||
{ | ||
if (!material.HasProperty(props.GetArrayElementAtIndex(i).FindPropertyRelative("first").stringValue)) | ||
{ | ||
props.DeleteArrayElementAtIndex(i); | ||
} | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こっちでも
MeshInfo2
使わないといけないですね。アニメーションの対応を今後追加することを考えたら
SwapMaterialArray
(に相当する関数)内で必要に応じてMaterialCleaning
を呼び出したいです。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
前者は修正、
MaterialCleaning
は呼べるようにアクセス装飾氏を調整しました。少し気になるのですが...
MaterialCleaning
をアニメーション関連で呼ぶ場合に、ここのswapDict
を何かしらの手段で渡さないと、MaterialCleaning
された別の同一マテリアルが発生し、マテリアルが増加する可能性があると思うので、その時に適宜調整する必要があると思います。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
アニメーションに関する処理もこのPassないでやるつもりです。
そして意図が伝わってなかったのですが先にすべてのマテリアルをDictionaryに追加するのではなく、適宜Dictionaryにあればそれを、なければ作成して追加の形にしてほしいという意味です