Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
75a3193
feat: MaterialUnusedPropertyRemove
ReinaS-64892 May 2, 2024
865db01
chore: draw MaterialUnusedPropertyRemove toggle
ReinaS-64892 May 2, 2024
7d5e05f
chore: move in RemoveUnusedObjects
ReinaS-64892 May 2, 2024
6dcb061
chore: If SkinnedMesh then use MeshInfo2
ReinaS-64892 May 2, 2024
38e6a5c
chore: If SkinnedMesh then use MeshInfo2 collect materials
ReinaS-64892 May 4, 2024
82551f4
chore: make MaterialCleaning public
ReinaS-64892 May 4, 2024
7d1ee87
chore: MaterialCleaning run as need time
ReinaS-64892 May 4, 2024
d4cf9a0
Merge branch 'master' into feat-material-unused-property-remove
anatawa12 Aug 23, 2024
6b91ec1
docs(changelog): Automatically remove unnecessary material properties…
anatawa12 Aug 23, 2024
930f047
fix: compilation error due to nullable reference type
anatawa12 Aug 23, 2024
bf72d64
Merge branch 'master' into feat-material-unused-property-remove
anatawa12 Sep 22, 2024
89b0213
Merge branch 'master' into feat-material-unused-property-remove
ReinaS-64892 Oct 16, 2024
9f5e282
chore: cleaning in place
ReinaS-64892 Oct 16, 2024
9fc6a2a
Merge branch 'master' into feat-material-unused-property-remove
anatawa12 Oct 18, 2024
eda5998
chore: rename pass name to RemoveUnusedMaterialProperties
anatawa12 Oct 18, 2024
c3ac6e9
fix: Properties used by fallback shader will be removed incorrectly
anatawa12 Oct 18, 2024
1c36150
feat: support cleaning animation materials
anatawa12 Oct 18, 2024
8542571
chore: check the material is temporary asset before modification
anatawa12 Oct 18, 2024
bae5ed4
chore: add TODO comments for future improvements
anatawa12 Oct 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Editor/Inspector/TraceAndOptimizeEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ protected override void OnInspectorGUIInner()
serializedObject.ApplyModifiedProperties();
}
}
}
}
3 changes: 2 additions & 1 deletion Editor/OptimizerPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Anatawa12.AvatarOptimizer.ndmf;
using nadena.dev.ndmf;
using nadena.dev.ndmf.builtin;
Expand Down Expand Up @@ -71,6 +71,7 @@ protected override void Configure()
.Then.Run(Processors.TraceAndOptimizes.AutoMergeSkinnedMesh.Instance)
.Then.Run(Processors.TraceAndOptimizes.FindUnusedObjects.Instance)
.Then.Run(Processors.TraceAndOptimizes.ConfigureRemoveZeroSizedPolygon.Instance)
.Then.Run(Processors.TraceAndOptimizes.MaterialUnusedPropertyRemove.Instance)
.Then.Run(Processors.MergeBoneProcessor.Instance)
.Then.Run(Processors.RemoveZeroSizedPolygonProcessor.Instance)
.Then.Run(Processors.AnimatorOptimizer.RemoveInvalidProperties.Instance)
Expand Down
91 changes: 91 additions & 0 deletions Editor/Processors/TraceAndOptimize/MaterialUnusedPropertyRemove.cs
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こっちでもMeshInfo2使わないといけないですね。

アニメーションの対応を今後追加することを考えたらSwapMaterialArray(に相当する関数)内で必要に応じてMaterialCleaningを呼び出したいです。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

前者は修正、MaterialCleaningは呼べるようにアクセス装飾氏を調整しました。

少し気になるのですが...MaterialCleaningをアニメーション関連で呼ぶ場合に、ここのswapDictを何かしらの手段で渡さないと、MaterialCleaningされた別の同一マテリアルが発生し、マテリアルが増加する可能性があると思うので、その時に適宜調整する必要があると思います。

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

アニメーションに関する処理もこのPassないでやるつもりです。

そして意図が伝わってなかったのですが先にすべてのマテリアルをDictionaryに追加するのではなく、適宜Dictionaryにあればそれを、なければ作成して追加の形にしてほしいという意味です

.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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そういえばfallback shaderの対応を考え忘れてる気がする

よっぽどのことがないともとシェーダにないプロパティが関係あることないと思うけど

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

カスタム指定ができるliltoonなんかでもOverrideTagでどうにかしてるので、VRCFallbackが指定されてる場合にそれに対応するシェーダのプロパティをどっかで持っておいて、それを下に消さないプロパティを決めると良さそう

{
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.

2 changes: 2 additions & 0 deletions Internal/TraceAndOptimizeBase/TraceAndOptimizeProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class TraceAndOptimizeState
public bool SkipMergeMaterialAnimatingSkinnedMesh;
public bool SkipMergeMaterials;
public bool SkipRemoveEmptySubMesh;
public bool SkipRemoveMaterialUnusedProperties;

public Dictionary<SkinnedMeshRenderer, HashSet<string>> PreserveBlendShapes =
new Dictionary<SkinnedMeshRenderer, HashSet<string>>();
Expand Down Expand Up @@ -68,6 +69,7 @@ internal void Initialize(TraceAndOptimize config)
SkipMergeMaterialAnimatingSkinnedMesh = config.advancedSettings.skipMergeMaterialAnimatingSkinnedMesh;
SkipMergeMaterials = config.advancedSettings.skipMergeMaterials;
SkipRemoveEmptySubMesh = config.advancedSettings.skipRemoveEmptySubMesh;
SkipRemoveMaterialUnusedProperties = config.advancedSettings.skipRemoveMaterialUnusedProperties;

Enabled = true;
}
Expand Down
2 changes: 2 additions & 0 deletions Runtime/TraceAndOptimize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ internal struct AdvancedSettings
public bool skipMergeMaterials;
[ToggleLeft]
public bool skipRemoveEmptySubMesh;
[ToggleLeft]
public bool skipRemoveMaterialUnusedProperties;
}
}
}
Loading