Skip to content

Commit

Permalink
Merge pull request #502 from anatawa12/fix-avatar-mask
Browse files Browse the repository at this point in the history
fix: AvatarMask is not mapped
  • Loading branch information
anatawa12 authored Sep 24, 2023
2 parents 5344faa + 72235cf commit 66d8187
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
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
- AvatarMask broken with many cases `#502`

### Security

Expand Down
34 changes: 34 additions & 0 deletions Editor/ObjectMapping/AnimationObjectMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,40 @@ public MappedGameObjectInfo(ObjectMapping objectMapping, string newPath,
}
}

[CanBeNull]
public string MapPath(string srcPath, Type type)
{
var gameObjectInfo = GetGameObjectInfo(srcPath);
if (gameObjectInfo == null) return srcPath;
var (instanceId, componentInfo) = gameObjectInfo.GetComponentByType(type);

if (componentInfo != null)
{
var component = EditorUtility.InstanceIDToObject(componentInfo.MergedInto) as Component;
// there's mapping about component.
// this means the component is merged or some prop has mapping
if (!component) return null; // this means removed.

var newPath = Utils.RelativePath(_rootGameObject.transform, component.transform);
if (newPath == null) return null; // this means moved to out of the animator scope

return newPath;
}
else
{
// The component is not merged & no prop mapping so process GameObject mapping

if (type != typeof(GameObject))
{
var component = EditorUtility.InstanceIDToObject(instanceId) as Component;
if (!component) return null; // this means removed
}

if (gameObjectInfo.NewPath == null) return null;
return gameObjectInfo.NewPath;
}
}

public EditorCurveBinding MapBinding(EditorCurveBinding binding)
{
var gameObjectInfo = GetGameObjectInfo(binding.path);
Expand Down
24 changes: 23 additions & 1 deletion Editor/Processors/ApplyObjectMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private AnimatorControllerLayer MapAnimatorControllerLayer(AnimatorControllerLay
new AnimatorControllerLayer
{
name = layer.name,
avatarMask = layer.avatarMask,
avatarMask = DeepClone(layer.avatarMask, CustomClone),
blendingMode = layer.blendingMode,
defaultWeight = layer.defaultWeight,
syncedLayerIndex = layer.syncedLayerIndex,
Expand Down Expand Up @@ -215,6 +215,27 @@ private Object CustomClone(Object o)

return newClip;
}
else if (o is AvatarMask mask)
{
var newMask = _session.AddToAsset(new AvatarMask());
newMask.name = "rebased " + mask.name;
newMask.transformCount = mask.transformCount;
var dstI = 0;
for (var srcI = 0; srcI < mask.transformCount; srcI++)
{
var path = mask.GetTransformPath(srcI);
var newPath = _mapping.MapPath(path, typeof(Transform));
if (newPath != null)
{
newMask.SetTransformPath(dstI, newPath);
newMask.SetTransformActive(dstI, mask.GetTransformActive(srcI));
dstI++;
}
}
newMask.transformCount = dstI;

return newMask;
}
else
{
return null;
Expand All @@ -239,6 +260,7 @@ private T DeepClone<T>(T original, Func<Object, Object> visitor) where T : Objec
case AnimatorStateMachine _:
case AnimatorTransitionBase _:
case StateMachineBehaviour _:
case AvatarMask _:
break; // We want to clone these types

// Leave textures, materials, and script definitions alone
Expand Down

0 comments on commit 66d8187

Please sign in to comment.