Skip to content

Commit

Permalink
Merge branch 'master-1.5' into nre-with-mesh-null
Browse files Browse the repository at this point in the history
  • Loading branch information
anatawa12 authored Oct 15, 2023
2 parents 8da3171 + 476cabc commit 55ed552
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog].
### Removed

### Fixed
- Constraints and Animations can be broken with Automatic MergeBone `#594`

### Security

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog].
### Removed

### Fixed
- Constraints and Animations can be broken with Automatic MergeBone `#594`

### Security

Expand Down
43 changes: 30 additions & 13 deletions Editor/Processors/TraceAndOptimize/FindUnusedObjectsProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,53 +356,70 @@ private void ConfigureMergeBone()
{
ConfigureRecursive(_session.GetRootComponent<Transform>(), _modifications);

// returns true if merged
bool ConfigureRecursive(Transform transform, ImmutableModificationsContainer modifications)
// returns (original mergedChildren, list of merged children if merged, and null if not merged)
//[CanBeNull]
(bool, List<Transform>) ConfigureRecursive(Transform transform, ImmutableModificationsContainer modifications)
{
var mergedChildren = true;
var afterChildren = new List<Transform>();
foreach (var child in transform.DirectChildrenEnumerable())
mergedChildren &= ConfigureRecursive(child, modifications);
{
var (newMergedChildren, newChildren) = ConfigureRecursive(child, modifications);
if (newChildren == null)
{
mergedChildren = false;
afterChildren.Add(child);
}
else
{
mergedChildren &= newMergedChildren;
afterChildren.AddRange(newChildren);
}
}

const ComponentDependencyCollector.DependencyType AllowedUsages =
ComponentDependencyCollector.DependencyType.Bone
| ComponentDependencyCollector.DependencyType.Parent
| ComponentDependencyCollector.DependencyType.ComponentToTransform;

// functions for make it easier to know meaning of result
(bool, List<Transform>) YesMerge() => (mergedChildren, afterChildren);
(bool, List<Transform>) NotMerged() => (mergedChildren, null);

// Already Merged
if (transform.GetComponent<MergeBone>()) return true;
if (transform.GetComponent<MergeBone>()) return YesMerge();
// Components must be Transform Only
if (transform.GetComponents<Component>().Length != 1) return false;
if (transform.GetComponents<Component>().Length != 1) return NotMerged();
// The bone cannot be used generally
if ((_marked[transform] & ~AllowedUsages) != 0) return false;
if ((_marked[transform] & ~AllowedUsages) != 0) return NotMerged();
// must not be animated
if (TransformAnimated(transform, modifications)) return false;
if (TransformAnimated(transform, modifications)) return NotMerged();

if (!mergedChildren)
{
if (GameObjectAnimated(transform, modifications)) return false;
if (GameObjectAnimated(transform, modifications)) return NotMerged();

var localScale = transform.localScale;
var identityTransform = localScale == Vector3.one && transform.localPosition == Vector3.zero &&
transform.localRotation == Quaternion.identity;

if (!identityTransform)
{
var childrenTransformAnimated =
transform.DirectChildrenEnumerable().Any(x => TransformAnimated(x, modifications));
var childrenTransformAnimated = afterChildren.Any(x => TransformAnimated(x, modifications));
if (childrenTransformAnimated)
// if this is not identity transform, animating children is not good
return false;
return NotMerged();

if (!MergeBoneProcessor.ScaledEvenly(localScale))
// non even scaling is not possible to reproduce in children
return false;
return NotMerged();
}
}

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

return true;
return YesMerge();
}

bool TransformAnimated(Transform transform, ImmutableModificationsContainer modifications)
Expand Down

0 comments on commit 55ed552

Please sign in to comment.