Skip to content

Commit

Permalink
Merge pull request #467 from anatawa12/merge-bone-name-conflict
Browse files Browse the repository at this point in the history
Avoid Name Conflict in MergeBone
  • Loading branch information
anatawa12 authored Sep 14, 2023
2 parents e14753c + 0caa724 commit 56e4709
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 7 deletions.
Binary file modified .docs/content/docs/reference/merge-bone/component.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion .docs/content/docs/reference/merge-bone/index.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ weight: 100

このコンポーネントが付いているGameObjectの全ての子GameObjectは、その親GameObjectの子になります。

今のところ、このコンポーネントに設定項目はありません。
## 設定 {#settings}

![component.png](component.png)

- `名前の競合を避ける` 統合時に子GameObjectの名前を変更することで、名前の重複によってアニメーションが正しく動かなくなる問題を回避します。
4 changes: 4 additions & 0 deletions .docs/content/docs/reference/merge-bone/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ All children of GameObject this component is applied to will belongs to parent o

This Component doesn't have any configuration for now.

## Settings

![component.png](component.png)

- `Avoid Name Conflict` Avoids animation problems with name conflict by renaming child GameObjects
2 changes: 2 additions & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog].

## [Unreleased]
### Added
- Avoid Name Conflict in MergeBone `#467`

### Changed

Expand All @@ -17,6 +18,7 @@ The format is based on [Keep a Changelog].

### Fixed
- Light disappears `#466`
- Automatic MergeBone may break Animation by conflictng GameObject name `#467`

### Security

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog].
- Moved `Use Advanced Animator Parser` to there
- Added `Exclusions` for exclude some GameObjects from optimization
- In this section, there are for debugging GC Objects `#464`
- Avoid Name Conflict in MergeBone `#467`

### Changed
- Improved 'Remove Unused Objects' `#401`
Expand Down
21 changes: 17 additions & 4 deletions Editor/Processors/MergeBoneProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,44 @@ public void Process(OptimizerSession session)
DoBoneMap2(meshInfo2, mergeMapping);
});

var counter = 0;

foreach (var pair in mergeMapping)
{
var mapping = pair.Key;
var mapped = pair.Value;
var avoidNameConflict = mapping.GetComponent<MergeBone>().avoidNameConflict;
// if intermediate objects are inactive, moved bone should be initially inactive
// animations are not performed correctly but if bones activity is animated, automatic
// merge bone doesn't merge such bone so ignore that for manual merge bone.
var activeSelf = ActiveSelfForNow(mapping, mapped);
var (activeSelf, namePrefix) = ActiveSelfAndNamePrefix(mapping, mapped);
foreach (var child in mapping.DirectChildrenEnumerable().ToArray())
{
if (mergeMapping.ContainsKey(child)) continue;
child.parent = mapped;
if (!activeSelf) child.gameObject.SetActive(false);
if (avoidNameConflict)
child.name = namePrefix + "$" + child.name + "$" + (counter++);
}
}

foreach (var pair in mergeMapping.Keys)
if (pair)
Object.DestroyImmediate(pair.gameObject);

bool ActiveSelfForNow(Transform transform, Transform parent)
(bool activeSelf, string namePrefix) ActiveSelfAndNamePrefix(Transform transform, Transform parent)
{
var segments = new List<string>();
var activeSelf = true;
for (; transform != parent; transform = transform.parent)
if (!transform.gameObject.activeSelf) return false;
return true;
{
segments.Add(transform.name);
activeSelf &= transform.gameObject.activeSelf;
}

segments.Reverse();

return (activeSelf, string.Join("$", segments));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ bool ConfigureRecursive(Transform transform, ImmutableModificationsContainer mod
}
}

transform.gameObject.GetOrAddComponent<MergeBone>();
if (!transform.gameObject.GetComponent<MergeBone>())
transform.gameObject.AddComponent<MergeBone>().avoidNameConflict = true;

return true;
}
Expand Down
6 changes: 6 additions & 0 deletions Localization/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ msgstr "Execute Early doesn't support Animation Remapping. Make sure you made an
msgid "MergeBone:description"
msgstr "You will remove this GameObject and merge bone to parent"

msgid "MergeBone:prop:avoidNameConflict"
msgstr "Avoid Name Conflict"

msgid "MergeBone:tooltip:avoidNameConflict"
msgstr "Renames child GameObjects to avoid name conflict"

msgid "MergeBone:validation:thereAreComponent"
msgstr "There are some components other than Transform. This is not supported."

Expand Down
6 changes: 6 additions & 0 deletions Localization/ja.po
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ msgstr "'早期に実行する'はアニメーションのパス修正が動き
msgid "MergeBone:description"
msgstr "このGameObjectを削除して親に統合する。"

msgid "MergeBone:prop:avoidNameConflict"
msgstr "名前の競合を避ける"

msgid "MergeBone:tooltip:avoidNameConflict"
msgstr "統合時に子GameObjectの名前を変更し、名前の重複を回避します"

msgid "MergeBone:validation:thereAreComponent"
msgstr "Transform以外のコンポーネントがあります。これはサポートされていません。"

Expand Down
12 changes: 11 additions & 1 deletion Runtime/MergeBone.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Anatawa12.AvatarOptimizer.ErrorReporting;
using CustomLocalization4EditorExtension;
using UnityEngine;

namespace Anatawa12.AvatarOptimizer
Expand All @@ -7,5 +8,14 @@ namespace Anatawa12.AvatarOptimizer
[DisallowMultipleComponent]
[HelpURL("https://vpm.anatawa12.com/avatar-optimizer/ja/docs/reference/merge-bone/")]
internal class MergeBone : AvatarTagComponent, IStaticValidated
{ }
{
[CL4EELocalized("MergeBone:prop:avoidNameConflict", "MergeBone:tooltip:avoidNameConflict")]
[ToggleLeft]
public bool avoidNameConflict;

private void Reset()
{
avoidNameConflict = true;
}
}
}

0 comments on commit 56e4709

Please sign in to comment.