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

fix: AvatarMask is not mapped #502

Merged
merged 2 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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