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: AnimationLocation is broken with BlendTree #1020

Merged
merged 3 commits into from
Apr 24, 2024
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 @@ -19,6 +19,7 @@ The format is based on [Keep a Changelog].

### Fixed
- Box Editor of Remove Mesh in Box can be broke with scale of Skinned Mesh Renderer `#1019`
- Automatic Merge Skinned Mesh is broken with BlendTree `#1020`

### Security

Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The format is based on [Keep a Changelog].
- That's why AAO now configures `Clamp BlendShapes (Deprecated)` to false in edit mode and true in play mode.
- PlayMode is usually used for testing the avatar behavior so it's better to have the same setting as VRChat client.
- If you want not to change this setting, please disable `Tools/Avatar Optimizer/Configure Clamp BlendShape Weight`.
- Automatic Merge Skinned Mesh `#952` `#972` `#1010`
- Automatic Merge Skinned Mesh `#952` `#972` `#1010` `#1020`
- Trace and Optimize now automatically merges Skinned Meshes if possible.
- Trace and Optimize will merge your mesh if the material properties or enablement of the mesh is animated similarly and has no BlendShapes.
- Components API for Scripting Usage `#976`
Expand Down
16 changes: 8 additions & 8 deletions Editor/AnimatorParserV2/AnimationParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private ImmutableNodeContainer ParseBlendTree(GameObject root, BlendTree blendTr

return NodesMerger.Merge<
ImmutableNodeContainer, ImmutablePropModNode<float>, ImmutablePropModNode<Object>,
ImmutablePropModNode<float>, ImmutablePropModNode<Object>,
BlendTreeElement<float>, BlendTreeElement<Object>,
ImmutableNodeContainer, ImmutableNodeContainer, ImmutablePropModNode<float>,
ImmutablePropModNode<Object>,
BlendTreeMergeProperty
Expand All @@ -62,7 +62,7 @@ private ImmutableNodeContainer ParseBlendTree(GameObject root, BlendTree blendTr
internal readonly struct BlendTreeMergeProperty :
IMergeProperty1<
ImmutableNodeContainer, ImmutablePropModNode<float>, ImmutablePropModNode<Object>,
ImmutablePropModNode<float>, ImmutablePropModNode<Object>,
BlendTreeElement<float>, BlendTreeElement<Object>,
ImmutableNodeContainer, ImmutableNodeContainer, ImmutablePropModNode<float>,
ImmutablePropModNode<Object>
>
Expand All @@ -77,16 +77,16 @@ public BlendTreeMergeProperty(BlendTreeType blendType)
public ImmutableNodeContainer CreateContainer() => new ImmutableNodeContainer();
public ImmutableNodeContainer GetContainer(ImmutableNodeContainer source) => source;

public ImmutablePropModNode<float> GetIntermediate(ImmutableNodeContainer source,
ImmutablePropModNode<float> node, int index) => node;
public BlendTreeElement<float> GetIntermediate(ImmutableNodeContainer source,
ImmutablePropModNode<float> node, int index) => new BlendTreeElement<float>(index, node);

public ImmutablePropModNode<Object> GetIntermediate(ImmutableNodeContainer source,
ImmutablePropModNode<Object> node, int index) => node;
public BlendTreeElement<Object> GetIntermediate(ImmutableNodeContainer source,
ImmutablePropModNode<Object> node, int index) => new BlendTreeElement<Object>(index, node);

public ImmutablePropModNode<float> MergeNode(List<ImmutablePropModNode<float>> nodes, int sourceCount) =>
public ImmutablePropModNode<float> MergeNode(List<BlendTreeElement<float>> nodes, int sourceCount) =>
new BlendTreeNode<float>(nodes, _blendType, partial: nodes.Count != sourceCount);

public ImmutablePropModNode<Object> MergeNode(List<ImmutablePropModNode<Object>> nodes, int sourceCount) =>
public ImmutablePropModNode<Object> MergeNode(List<BlendTreeElement<Object>> nodes, int sourceCount) =>
new BlendTreeNode<Object>(nodes, _blendType, partial: nodes.Count != sourceCount);
}

Expand Down
75 changes: 72 additions & 3 deletions Editor/AnimatorParserV2/AnimatorParserDebugWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ private void OnGUI()
{
if (GUILayout.Button("Copy Parsed Text"))
GUIUtility.systemCopyBuffer = CreateText();
if (GUILayout.Button("Copy Detailed Parsed Text"))
GUIUtility.systemCopyBuffer = CreateText(true);
}

if (Container == null) return;
Expand Down Expand Up @@ -63,7 +65,7 @@ private void OnGUI()
GUILayout.EndScrollView();
}

private string CreateText()
private string CreateText(bool detailed = false)
{
var root = parsedRootObject.transform;
var resultText = new StringBuilder();
Expand All @@ -86,7 +88,9 @@ private string CreateText()
else
propStateInfo += "Variable";

resultText.Append(propName).Append(": ").Append(propStateInfo).Append('\n');
resultText.Append(" ").Append(propName).Append(": ").Append(propStateInfo).Append('\n');
if (detailed)
AppendNodeRecursive(propState, resultText, " ");
}

resultText.Append('\n');
Expand All @@ -95,6 +99,71 @@ private string CreateText()
return resultText.ToString();
}

private void AppendNodeRecursive(PropModNode<float> propState, StringBuilder resultText, string indent)
{
switch (propState)
{
case AnimatorControllerPropModNode<float> animCont:
resultText.Append($"{indent}AnimatorController: \n");
foreach (var layerInfo in animCont.LayersReversed)
{
resultText.Append($"{indent} Layer {layerInfo.LayerIndex}: {layerInfo.Weight}, {layerInfo.BlendingMode}\n");
AppendNodeRecursive(layerInfo.Node, resultText, indent + " ");
}
break;
case AnimationComponentPropModNode<float> animation:
resultText.Append($"{indent}Animation: {animation.Component.name}\n");
AppendNodeRecursive(animation.Animation, resultText, indent + " ");
break;
case AnimatorPropModNode<float> animator:
resultText.Append($"{indent}Animator: {animator.Component.name}\n");
foreach (var layerInfo in animator.LayersReversed)
{
resultText.Append($"{indent} Layer {layerInfo.LayerIndex}: {layerInfo.Weight}, {layerInfo.BlendingMode}\n");
AppendNodeRecursive(layerInfo.Node, resultText, indent + " ");
}
break;
case HumanoidAnimatorPropModNode humanoid:
resultText.Append($"{indent}Humanoid: {humanoid.Component.name}\n");
break;
case VariableComponentPropModNode<float> variable:
resultText.Append($"{indent}Variable({variable.Component.GetType().Name}): {variable.Component.name}\n");
break;
case AnimatorLayerPropModNode<float> animatorLayer:
resultText.Append($"{indent}AnimatorLayer:\n");
foreach (var childNode in animatorLayer.Children)
AppendNodeRecursive(childNode, resultText, indent + " ");
break;
case AnimatorStatePropModNode<float> stateNode:
resultText.Append($"{indent}AnimatorState: {stateNode.State.name}\n");
AppendNodeRecursive(stateNode.Node, resultText, indent + " ");
break;
case BlendTreeNode<float> blendTreeNode:
resultText.Append($"{indent}BlendTree:\n");
foreach (var childNode in blendTreeNode.Children)
{
resultText.Append($"{indent} BlendTreeElement({childNode.Index}):\n");
AppendNodeRecursive(childNode.Node, resultText, indent + " ");
}
break;
case FloatAnimationCurveNode curve:
resultText.Append($"{indent}AnimationCurve: {curve.Clip.name}\n");
break;
case RootPropModNode<float> rootNode:
resultText.Append($"{indent}Root:\n");
foreach (var rootNodeChild in rootNode.Children)
{
resultText.Append($"{indent} {rootNodeChild.Component.name}:\n");
AppendNodeRecursive(rootNodeChild.Node, resultText, indent + " ");
}
break;
default:
resultText.Append($"{indent}Unknown: {propState.GetType().Name}\n");
break;
}
}


private static void NarrowValueLabelField(string label0, string value)
{
var position = EditorGUI.IndentedRect(EditorGUILayout.GetControlRect(true, 18f));
Expand Down Expand Up @@ -173,4 +242,4 @@ public enum ParserSource
Motion
}
}
}
}
34 changes: 24 additions & 10 deletions Editor/AnimatorParserV2/PropModNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ internal interface ILayer<T> : ILayer

internal sealed class RootPropModNode<T> : PropModNode<T>, IErrorContext
{
readonly struct ComponentInfo
internal readonly struct ComponentInfo
{
public readonly ComponentPropModNodeBase<T> Node;
public readonly bool AlwaysApplied;
Expand All @@ -227,6 +227,8 @@ public ComponentInfo(ComponentPropModNodeBase<T> node, bool alwaysApplied)

private readonly List<ComponentInfo> _children = new List<ComponentInfo>();

public IEnumerable<ComponentInfo> Children => _children;

public override bool AppliedAlways => _children.All(x => x.AppliedAlways);
public override IEnumerable<ObjectReference> ContextReferences => _children.SelectMany(x => x.ContextReferences);
public override ValueInfo<T> Value => NodeImplUtils.ConstantInfoForSideBySide(_children.Select(x => x.Node));
Expand Down Expand Up @@ -354,13 +356,25 @@ private static ValueInfo<Object> ParseProperty(ObjectReferenceKeyframe[] frames)
new ValueInfo<Object>(frames.Select(x => x.value).Distinct().ToArray());
}

internal struct BlendTreeElement<T>
{
public int Index;
public ImmutablePropModNode<T> Node;

public BlendTreeElement(int index, [NotNull] ImmutablePropModNode<T> node)
{
Index = index;
Node = node ?? throw new ArgumentNullException(nameof(node));
}
}

internal class BlendTreeNode<T> : ImmutablePropModNode<T>
{
private readonly List<ImmutablePropModNode<T>> _children;
private readonly List<BlendTreeElement<T>> _children;
private readonly BlendTreeType _blendTreeType;
private readonly bool _partial;

public BlendTreeNode([NotNull] [ItemNotNull] List<ImmutablePropModNode<T>> children, BlendTreeType blendTreeType, bool partial)
public BlendTreeNode([NotNull] List<BlendTreeElement<T>> children, BlendTreeType blendTreeType, bool partial)
{
// expected to pass list or array
// ReSharper disable once PossibleMultipleEnumeration
Expand All @@ -373,14 +387,14 @@ public BlendTreeNode([NotNull] [ItemNotNull] List<ImmutablePropModNode<T>> child


private bool WeightSumIsOne => _blendTreeType != BlendTreeType.Direct;
public IReadOnlyList<ImmutablePropModNode<T>> Children => _children;
public override bool AppliedAlways => WeightSumIsOne && !_partial && _children.All(x => x.AppliedAlways);
public IReadOnlyList<BlendTreeElement<T>> Children => _children;
public override bool AppliedAlways => WeightSumIsOne && !_partial && _children.All(x => x.Node.AppliedAlways);
public override ValueInfo<T> Value => !WeightSumIsOne
? ValueInfo<T>.Variable
: NodeImplUtils.ConstantInfoForSideBySide(_children);
: NodeImplUtils.ConstantInfoForSideBySide(_children.Select(x => x.Node));

public override IEnumerable<ObjectReference> ContextReferences =>
_children.SelectMany(x => x.ContextReferences);
_children.SelectMany(x => x.Node.ContextReferences);
}

abstract class ComponentPropModNodeBase<T> : PropModNode<T>
Expand Down Expand Up @@ -422,11 +436,11 @@ public VariableComponentPropModNode([NotNull] Component component) : base(compon

class AnimationComponentPropModNode<T> : ComponentPropModNode<T, Animation>
{
private readonly ImmutablePropModNode<T> _animation;
public ImmutablePropModNode<T> Animation { get; }

public AnimationComponentPropModNode([NotNull] Animation component, ImmutablePropModNode<T> animation) : base(component)
{
_animation = animation;
Animation = animation;
_constantInfo = new Lazy<ValueInfo<T>>(() => animation.Value, isThreadSafe: false);
}

Expand All @@ -436,6 +450,6 @@ public AnimationComponentPropModNode([NotNull] Animation component, ImmutablePro
public override ValueInfo<T> Value => _constantInfo.Value;

public override IEnumerable<ObjectReference> ContextReferences =>
base.ContextReferences.Concat(_animation.ContextReferences);
base.ContextReferences.Concat(Animation.ContextReferences);
}
}
8 changes: 4 additions & 4 deletions Editor/Utils/AnimationLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ private static IEnumerable<AnimationLocation> CollectAnimationLocationSlow(Anima
{
var (blendTree, location) = queue.Dequeue();

for (var i = 0; i < blendTree.Children.Count; i++)
foreach (var element in blendTree.Children)
{
var newLocation = location;
ArrayUtility.Add(ref newLocation, i);
switch (blendTree.Children[i])
ArrayUtility.Add(ref newLocation, element.Index);
switch (element.Node)
{
case FloatAnimationCurveNode floatNode:
yield return new AnimationLocation(animator, playableLayer, animatorLayer, state,
Expand All @@ -90,7 +90,7 @@ private static IEnumerable<AnimationLocation> CollectAnimationLocationSlow(Anima
default:
throw new InvalidOperationException(
"Unexpected node type: " +
blendTree.Children[i].GetType().FullName);
element.Node.GetType().FullName);
}
}
}
Expand Down
Loading