-
-
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
Merged
anatawa12
merged 19 commits into
anatawa12:master
from
ReinaS-64892:feat-material-unused-property-remove
Oct 18, 2024
Merged
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
75a3193
feat: MaterialUnusedPropertyRemove
ReinaS-64892 865db01
chore: draw MaterialUnusedPropertyRemove toggle
ReinaS-64892 7d5e05f
chore: move in RemoveUnusedObjects
ReinaS-64892 6dcb061
chore: If SkinnedMesh then use MeshInfo2
ReinaS-64892 38e6a5c
chore: If SkinnedMesh then use MeshInfo2 collect materials
ReinaS-64892 82551f4
chore: make MaterialCleaning public
ReinaS-64892 7d1ee87
chore: MaterialCleaning run as need time
ReinaS-64892 d4cf9a0
Merge branch 'master' into feat-material-unused-property-remove
anatawa12 6b91ec1
docs(changelog): Automatically remove unnecessary material properties…
anatawa12 930f047
fix: compilation error due to nullable reference type
anatawa12 bf72d64
Merge branch 'master' into feat-material-unused-property-remove
anatawa12 89b0213
Merge branch 'master' into feat-material-unused-property-remove
ReinaS-64892 9f5e282
chore: cleaning in place
ReinaS-64892 9fc6a2a
Merge branch 'master' into feat-material-unused-property-remove
anatawa12 eda5998
chore: rename pass name to RemoveUnusedMaterialProperties
anatawa12 c3ac6e9
fix: Properties used by fallback shader will be removed incorrectly
anatawa12 1c36150
feat: support cleaning animation materials
anatawa12 8542571
chore: check the material is temporary asset before modification
anatawa12 bae5ed4
chore: add TODO comments for future improvements
anatawa12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
93 changes: 93 additions & 0 deletions
93
Editor/Processors/TraceAndOptimize/MaterialUnusedPropertyRemove.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System.Collections.Generic; | ||
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 = new Dictionary<Material, Material>(); | ||
|
||
void SwapMaterialArray(Material[] matArray) | ||
{ | ||
for (var i = 0; matArray.Length > i; i += 1) | ||
{ | ||
var sourceMaterial = matArray[i]; | ||
if (sourceMaterial == null) { continue; } | ||
|
||
if (swapDict.TryGetValue(sourceMaterial, out var cleanMaterial)) { matArray[i] = cleanMaterial; } | ||
else { swapDict[sourceMaterial] = matArray[i] = MaterialCleaning(sourceMaterial); } | ||
} | ||
} | ||
|
||
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); | ||
} | ||
|
||
public 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) | ||
{ | ||
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Editor/Processors/TraceAndOptimize/MaterialUnusedPropertyRemove.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
そういえばfallback shaderの対応を考え忘れてる気がする
よっぽどのことがないともとシェーダにないプロパティが関係あることないと思うけど
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.
memo: property list https://creators.vrchat.com/avatars/shader-fallback-system/#vrchat-202142-fallback-system-upgrade
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.
カスタム指定ができるliltoonなんかでもOverrideTagでどうにかしてるので、
VRCFallback
が指定されてる場合にそれに対応するシェーダのプロパティをどっかで持っておいて、それを下に消さないプロパティを決めると良さそう