Skip to content

Commit

Permalink
fix: Animation index in BlendTree is not considered in AnimationLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
anatawa12 committed Apr 24, 2024
1 parent 1f031e4 commit 55dca4c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
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
5 changes: 4 additions & 1 deletion Editor/AnimatorParserV2/AnimatorParserDebugWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ private void AppendNodeRecursive(PropModNode<float> propState, StringBuilder res
case BlendTreeNode<float> blendTreeNode:
resultText.Append($"{indent}BlendTree:\n");
foreach (var childNode in blendTreeNode.Children)
AppendNodeRecursive(childNode, resultText, indent + " ");
{
resultText.Append($"{indent} BlendTreeElement({childNode.Index}):\n");
AppendNodeRecursive(childNode.Node, resultText, indent + " ");
}
break;
case FloatAnimationCurveNode curve:
resultText.Append($"{indent}AnimationCurve: {curve.Clip.name}\n");
Expand Down
24 changes: 18 additions & 6 deletions Editor/AnimatorParserV2/PropModNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,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 @@ -375,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
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

0 comments on commit 55dca4c

Please sign in to comment.