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

Migrate from unused bones by reference #430

Merged
merged 13 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
8 changes: 8 additions & 0 deletions Editor/AutomaticConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ internal class TraceAndOptimizeEditor : AvatarGlobalComponentEditorBase
{
private SerializedProperty _freezeBlendShape;
private SerializedProperty _removeUnusedObjects;
private SerializedProperty _preserveEndBone;
private SerializedProperty _mmdWorldCompatibility;
private SerializedProperty _advancedAnimatorParser;
private SerializedProperty _advancedSettings;
Expand All @@ -18,6 +19,7 @@ private void OnEnable()
{
_freezeBlendShape = serializedObject.FindProperty(nameof(TraceAndOptimize.freezeBlendShape));
_removeUnusedObjects = serializedObject.FindProperty(nameof(TraceAndOptimize.removeUnusedObjects));
_preserveEndBone = serializedObject.FindProperty(nameof(TraceAndOptimize.preserveEndBone));
_mmdWorldCompatibility = serializedObject.FindProperty(nameof(TraceAndOptimize.mmdWorldCompatibility));
_advancedAnimatorParser = serializedObject.FindProperty(nameof(TraceAndOptimize.advancedAnimatorParser));
_advancedSettings = serializedObject.FindProperty(nameof(TraceAndOptimize.advancedSettings));
Expand All @@ -33,6 +35,12 @@ protected override void OnInspectorGUIInner()
GUILayout.Label("Features", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(_freezeBlendShape);
EditorGUILayout.PropertyField(_removeUnusedObjects);
if (_removeUnusedObjects.boolValue)
{
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(_preserveEndBone);
EditorGUI.indentLevel--;
}

_advancedSettingsLabel.text = CL4EE.Tr("TraceAndOptimize:prop:advancedSettings");
if (EditorGUILayout.PropertyField(_advancedSettings, _advancedSettingsLabel, false))
Expand Down
16 changes: 16 additions & 0 deletions Editor/Processors/TraceAndOptimize/ComponentDependencyCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ static ComponentDependencyCollector()
InitByTypeParsers();
}

private readonly bool _preserveEndBone;

public ComponentDependencyCollector(bool preserveEndBone)
{
_preserveEndBone = preserveEndBone;
}

private readonly Dictionary<Component, ComponentDependencies> _dependencies =
new Dictionary<Component, ComponentDependencies>();

Expand Down Expand Up @@ -221,6 +228,15 @@ private static void InitByTypeParsers()
AddParser<Transform>((collector, deps, transform) =>
{
deps.AddAlwaysDependency(transform.parent);

// For compatibility with UnusedBonesByReferenceTool
// https://github.com/anatawa12/AvatarOptimizer/issues/429
if (collector._preserveEndBone &&
transform.name.EndsWith("end", StringComparison.OrdinalIgnoreCase))
{
collector.GetDependencies(transform.parent)
.AddAlwaysDependency(transform);
}
});
// Animator does not do much for motion, just changes states of other components.
// All State Changes are collected separately
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ class FindUnusedObjectsProcessor
private readonly ImmutableModificationsContainer _modifications;
private readonly OptimizerSession _session;
private readonly HashSet<GameObject> _exclusions;
private readonly bool _preserveEndBone;
private readonly bool _useLegacyGC;

public FindUnusedObjectsProcessor(ImmutableModificationsContainer modifications, OptimizerSession session,
bool preserveEndBone,
bool useLegacyGC,
HashSet<GameObject> exclusions)
{
_modifications = modifications;
_session = session;
_preserveEndBone = preserveEndBone;
_useLegacyGC = useLegacyGC;
_exclusions = exclusions;
}
Expand Down Expand Up @@ -78,7 +81,7 @@ private void MarkComponent(Component component, bool ifTargetCanBeEnabled)
private void ProcessNew()
{
// first, collect usages
var collector = new ComponentDependencyCollector();
var collector = new ComponentDependencyCollector(_preserveEndBone);
collector.CollectAllUsages(_session);

// then, mark and sweep.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ public void ProcessLater(OptimizerSession session)
if (_config is null) return;

if (_config.removeUnusedObjects)
new FindUnusedObjectsProcessor(_modifications, session, _config.advancedSettings.useLegacyGc,
_exclusions).Process();
new FindUnusedObjectsProcessor(_modifications, session,
preserveEndBone: _config.preserveEndBone,
useLegacyGC: _config.advancedSettings.useLegacyGc,
exclusions: _exclusions).Process();

}
}
Expand Down
64 changes: 64 additions & 0 deletions Editor/UnusedBonesByReferencesToolEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Anatawa12.AvatarOptimizer.Processors;
using CustomLocalization4EditorExtension;
using UnityEditor;
using UnityEngine;

namespace Anatawa12.AvatarOptimizer
{
[CustomEditor(typeof(UnusedBonesByReferencesTool))]
class UnusedBonesByReferencesToolEditor : AvatarGlobalComponentEditorBase
{
private SerializedProperty _preserveEndBone;
private SerializedProperty _detectExtraChild;

private void OnEnable()
{
_preserveEndBone = serializedObject.FindProperty(nameof(UnusedBonesByReferencesTool.preserveEndBone));
_detectExtraChild = serializedObject.FindProperty(nameof(UnusedBonesByReferencesTool.detectExtraChild));
}

protected override void OnInspectorGUIInner()
{
EditorGUILayout.PropertyField(_preserveEndBone);
EditorGUILayout.PropertyField(_detectExtraChild);

EditorGUILayout.HelpBox(CL4EE.Tr("UnusedBonesByReferencesTool:suggestMigrate"), MessageType.Info);
if (GUILayout.Button(CL4EE.Tr("UnusedBonesByReferencesTool:migrate")))
{
const string CONTENT_UNDO_NAME = "Migrate UnusedBonesByReferencesTool to Trace and Optimize";

var component = (UnusedBonesByReferencesTool)target;

// first, migrate configuration
var traceAndOptimize = component.GetComponent<TraceAndOptimize>();
if (!traceAndOptimize)
traceAndOptimize = Undo.AddComponent<TraceAndOptimize>(component.gameObject);
Undo.RecordObject(traceAndOptimize, CONTENT_UNDO_NAME);

traceAndOptimize.removeUnusedObjects = true;
traceAndOptimize.advancedSettings.useLegacyGc = false;
traceAndOptimize.preserveEndBone = component.preserveEndBone;
PrefabUtility.RecordPrefabInstancePropertyModifications(traceAndOptimize);

// then, remove EditorOnly from bones
foreach (var boneReference in UnusedBonesByReferencesToolEarlyProcessor.BoneReference
.Make(component.transform))
{
if (boneReference.Bone.CompareTag("EditorOnly"))
{
Undo.RecordObject(boneReference.Bone, CONTENT_UNDO_NAME);
boneReference.Bone.tag = "Untagged";
PrefabUtility.RecordPrefabInstancePropertyModifications(boneReference.Bone);
}
}

Undo.DestroyObjectImmediate(component);
Undo.SetCurrentGroupName(CONTENT_UNDO_NAME);

EditorUtility.DisplayDialog(CL4EE.Tr("UnusedBonesByReferencesTool:migrationFinished:title"),
CL4EE.Tr("UnusedBonesByReferencesTool:migrationFinished:description"),
"Ok");
}
}
}
}
3 changes: 3 additions & 0 deletions Editor/UnusedBonesByReferencesToolEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions Localization/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,21 @@ msgstr "Detect Extra Children"
msgid "UnusedBonesByReferencesTool:tooltip:detectExtraChild"
msgstr "If checked, this tool does not remove bones with non-bone children."

msgid "UnusedBonesByReferencesTool:suggestMigrate"
msgstr ""
"UnusedBonesByReferencesTool is obsoleted by more intelligent Trace and Optimize!\n"
"Do you want to migrate to Trace and Optimize with clicking the button below?"
anatawa12 marked this conversation as resolved.
Show resolved Hide resolved

msgid "UnusedBonesByReferencesTool:migrate"
msgstr "Migrate to Trace and Optimize"

msgid "UnusedBonesByReferencesTool:migrationFinished:title"
msgstr "Migration Finished!"

msgid "UnusedBonesByReferencesTool:migrationFinished:description"
msgstr "Migrating to Trace and Optimize is finished!"


# endregion

# region TraceAndOptimize
Expand Down Expand Up @@ -381,6 +396,12 @@ msgid "TraceAndOptimize:warn:unknown-type"
msgstr "Unknown Component Type '{0}' Found. This will reduce optimization performance and may break your Avatar."
"If your avatar got broken, Please report this error to AvatarOptimizer!"

msgid "TraceAndOptimize:prop:preserveEndBone"
msgstr "Preserve EndBone"

msgid "TraceAndOptimize:tooltip:preserveEndBone"
msgstr "If checked, this tool does not remove end bones with active parent."
anatawa12 marked this conversation as resolved.
Show resolved Hide resolved

# endregion

#region ApplyObjectMapping
Expand Down
20 changes: 20 additions & 0 deletions Localization/ja.po
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,20 @@ msgstr "他の子オブジェクトを認識する"
msgid "UnusedBonesByReferencesTool:tooltip:detectExtraChild"
msgstr "チェックされている場合、子にボーン以外を持つボーンを削除しません。"

msgid "UnusedBonesByReferencesTool:suggestMigrate"
msgstr ""
"UnusedBonesByReferencesToolはさらに賢く使用していないなボーンなどを削除するTrace and Optimizeに置き換えられました!\n"
anatawa12 marked this conversation as resolved.
Show resolved Hide resolved
"下のボタンを押すことでTrace and Optimizeに置き換えませんか"
anatawa12 marked this conversation as resolved.
Show resolved Hide resolved

msgid "UnusedBonesByReferencesTool:migrate"
msgstr "Trace and Optimizeに変換する"
anatawa12 marked this conversation as resolved.
Show resolved Hide resolved

msgid "UnusedBonesByReferencesTool:migrationFinished:title"
msgstr "変換完了"
anatawa12 marked this conversation as resolved.
Show resolved Hide resolved

msgid "UnusedBonesByReferencesTool:migrationFinished:description"
msgstr "Trace and Optimizeへの変換が完了しました!"
anatawa12 marked this conversation as resolved.
Show resolved Hide resolved

# endregion

# region TraceAndOptimize
Expand Down Expand Up @@ -318,6 +332,12 @@ msgid "TraceAndOptimize:warn:unknown-type"
msgstr "未知のコンポーネント'{0}'を検出しました! これにより最適化処理の品質が低下し、処理後のアバターが壊れる可能性があります。(元のアバターには影響しません)"
"アバターが壊れた場合、これをAvatarOptimizerに報告してください!"

msgid "TraceAndOptimize:prop:preserveEndBone"
msgstr "endボーンを残す"

msgid "TraceAndOptimize:tooltip:preserveEndBone"
msgstr "チェックされている場合、親がactiveなendボーンを削除しません。"
anatawa12 marked this conversation as resolved.
Show resolved Hide resolved

# endregion

#region ApplyObjectMapping
Expand Down
6 changes: 6 additions & 0 deletions Runtime/TraceAndOptimize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ internal class TraceAndOptimize : AvatarGlobalComponent
[ToggleLeft]
public bool removeUnusedObjects = true;

// Remove Unused Objects Options
[CL4EELocalized("TraceAndOptimize:prop:preserveEndBone",
"TraceAndOptimize:tooltip:preserveEndBone")]
[ToggleLeft]
public bool preserveEndBone;

// common parsing configuration
[CL4EELocalized("TraceAndOptimize:prop:mmdWorldCompatibility",
"TraceAndOptimize:tooltip:mmdWorldCompatibility")]
Expand Down